"""
Unit tests for the Genus/Hyves api
"""
import unittest

from genus.utils import bool_to_string, string_to_bool
from genus.utils import do_http_call
from genus.utils import calculate_oauth_signature

from genus.api import GenusApi

from genus.oauth.consumer import OAuthConsumer

class UtilsTestCase(unittest.TestCase):
    """Testing the utils library for any inconsistancies etc
    """

    def test_bool_conversion(self):
        """Testing the boolean to string conversions
        """

        self.assertEqual(bool_to_string(False), 'false')
        self.assertEqual(bool_to_string(True), 'true')

        self.assertEqual(string_to_bool('false'), False)
        self.assertEqual(string_to_bool('true'), True)

    def test_do_http_call(self):
        """Try to do an HTTP request
        """

        result = do_http_call(
            'http://youtube.com/crossdomain.xml',
            {
              'test':'value'
            },
            False
        )

        self.assertTrue(result.count('test=value')>0)

    def test_calculate_signatures(self):
        """Calculate and compare signatures. Including the tricky situations
        """

        self.assertEqual(
            calculate_oauth_signature(
                'http://data.hyves.nl/',
                'uri',
                {
                    'test':'value'
                },
                'consumer_secret',
                'token_secret'
            ),
            'AD7pfDU3JJNsaXZW4uGccTt0Mj8='
        )

        self.assertEqual(
            calculate_oauth_signature(
                'POST',
                'TEST',
                {
                    'a':'b test'
                },
                'oauth_consumer_secret',
                'oauth_token_secret'),
            'HPcbjA9VIzxoQDRYGnOVWGpyMec='
        )

        self.assertEqual(
            calculate_oauth_signature(
                'POST',
                'TEST',
                {
                    'a':'b+&test'
                },
                'oauth_consumer_secret',
                'oauth_token_secret'
            ),
            '3pKzqGPLUmQ4jF5QuirKSQFyVtU='
        )

class GenusApiTestCase(unittest.TestCase):
    """Test case to see if an api call can really be made
    """

    CONSUMER_KEY = 'x'
    CONSUMER_SECRET = 'x'

    def setUp(self):
        self.consumer = OAuthConsumer(
            GenusApiTestCase.CONSUMER_KEY,
            GenusApiTestCase.CONSUMER_SECRET
        )

        self.genus_api = GenusApi(self.consumer, "2.0")

    def test_do_method(self):
        """Make an API call to the server.
        """
        response = self.genus_api.do_method(
            "media.getPublic",
            {
                "sorttype":"mostrespect",
                'mediatype':'image',
                'timespan':'day'
            }
        )
        self.assertTrue(len(response) > 0)

if __name__ == '__main__':
    unittest.main()


nombres=input("Ingrese los nombres de los jugadores de la siguiente manera:(nombre1) (nombre2)\n")

nombres_lista=nombres.split()

nombre1=nombres_lista[0][0:3].upper()
nombre2=nombres_lista[1][0:3].upper()

if nombre1==nombre2:
    nombre2+="2"

print("\n")
print(nombre1+" 501")
print(nombre2+" 501\n")

puntaje_y_lanzamientos1=[501,0,0,0]
puntaje_y_lanzamientos2=[501,0,0,0]

error=0

while puntaje_y_lanzamientos1[0]!=0 and puntaje_y_lanzamientos2[0]!=0:

    print("Ingrese los últimos 3 lanzamientos de "+nombres_lista[0]+" seguido de los últimos 3 lanzamientos de "+nombres_lista[1]+".")
    print("Debe ingresar: (multiplicador) (puntaje), Single Bull, Double Bull o Null (para lanzamiento perdido).\n")

    for i in range(1,4):

        lanzamientoi=input()
        if lanzamientoi.upper()=="SINGLE BULL":
            lanzamientoi=25
        elif lanzamientoi.upper()=="DOUBLE BULL":
            lanzamientoi=50
        elif lanzamientoi.upper()=="NULL":
            lanzamientoi=0
        else:
            multiplicacion=lanzamientoi.split()
            if int(multiplicacion[0])<1 or int(multiplicacion[0])>3 or int(multiplicacion[1])<1 or int(multiplicacion[1])>20:
                error=1

            lanzamientoi=int(multiplicacion[0])*int(multiplicacion[1])

        puntaje_y_lanzamientos1[i]=lanzamientoi

    puntaje_y_lanzamientos1[0]=puntaje_y_lanzamientos1[0]-puntaje_y_lanzamientos1[1]-puntaje_y_lanzamientos1[2]-puntaje_y_lanzamientos1[3]

    if puntaje_y_lanzamientos1[0]<0:
        puntaje_y_lanzamientos1[0]=abs(puntaje_y_lanzamientos1[0])

    for i in range(1,4):

        lanzamientoi=input()
        if lanzamientoi.upper()=="SINGLE BULL":
            lanzamientoi=25
        elif lanzamientoi.upper()=="DOUBLE BULL":
            lanzamientoi=50
        elif lanzamientoi.upper()=="NULL":
            lanzamientoi=0
        else:
            multiplicacion=lanzamientoi.split()
            if int(multiplicacion[0])<1 or int(multiplicacion[0])>3 or int(multiplicacion[1])<1 or int(multiplicacion[1])>20:
                error=1

            lanzamientoi=int(multiplicacion[0])*int(multiplicacion[1])

        puntaje_y_lanzamientos2[i]=int(lanzamientoi)

    puntaje_y_lanzamientos2[0]=puntaje_y_lanzamientos2[0]-puntaje_y_lanzamientos2[1]-puntaje_y_lanzamientos2[2]-puntaje_y_lanzamientos2[3]

    if puntaje_y_lanzamientos2[0]<0:
        puntaje_y_lanzamientos2[0]=abs(puntaje_y_lanzamientos2[0])

    if error==1:
        print("\n")
        print("Error al ingresar los lanzamientos (multiplicador o puntaje inexistentes).")
        print("El programa terminará.")
        break

    print("\n")
    print(nombre1+" "+str(puntaje_y_lanzamientos1[0]))
    print(nombre2+" "+str(puntaje_y_lanzamientos2[0])+"\n")

    if puntaje_y_lanzamientos1[0]==0 and puntaje_y_lanzamientos2[0]==0:
        print("Es un empate!")

    elif puntaje_y_lanzamientos1[0]==0:
        print("Gana "+nombres_lista[0]+"! Felicitaciones.")

    elif puntaje_y_lanzamientos2[0]==0:
        print("Gana "+nombres_lista[1]+"! Felicitaciones.")


from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing.image import *
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense, ZeroPadding2D
from keras.layers import BatchNormalization
from keras import initializers
from keras import regularizers
from keras.layers.advanced_activations import LeakyReLU

import pandas as pd
import numpy as np
import os
import keras
import matplotlib.pyplot as plt
from keras.layers import Dense,GlobalAveragePooling2D
from keras.applications import MobileNet
from keras.preprocessing import image
from keras.applications.mobilenet import preprocess_input
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Model
from keras.optimizers import Adam

base_model=MobileNet(weights='imagenet',include_top=False) #imports the mobilenet model and discards the last 1000 neuron layer.

x=base_model.output
x=GlobalAveragePooling2D()(x)
x=Dense(1024,activation='relu')(x) #we add dense layers so that the model can learn more complex functions and classify for better results.
x=Dense(1024,activation='relu')(x) #dense layer 2
x=Dense(512,activation='relu')(x) #dense layer 3
preds=Dense(2,activation='softmax')(x) #final layer with softmax activation

# Sizes of the scaled images
height = 450
width = 600

ROOT_PATH = "../../"

train_directory_path = ROOT_PATH + 'train_new'
test_directory_path = ROOT_PATH + 'test_new'

number_of_epochs = 2
batch_size = 16

train_data = ImageDataGenerator(
     rescale=1 / 255
)#     shear_range=0.2,
#     zoom_range=0.2,
#     horizontal_flip=True)

#train_data = ImageDataGenerator()

test_datagen = ImageDataGenerator()

train_generator = train_data.flow_from_directory(
    train_directory_path,
    target_size=(width, height),
    batch_size=batch_size,
    class_mode='categorical',
    seed=42)

validation_generator = test_datagen.flow_from_directory(
    test_directory_path,
    target_size=(width, height),
    batch_size=batch_size,
    class_mode='categorical',
    seed=42)

model=Model(inputs=base_model.input,outputs=preds)

for layer in model.layers[:-8]:
    layer.trainable=False

model.compile(optimizer='Adam',loss='categorical_crossentropy',metrics=['accuracy'])

step_size_train=train_generator.n//train_generator.batch_size
step_size_validate = validation_generator.n // validation_generator.batch_size

checkpoint = keras.callbacks.ModelCheckpoint('checkpoint.check', monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto', period=1)
callbacks_list = [checkpoint]
model.fit_generator(generator=train_generator,
                    steps_per_epoch=step_size_train,
                    epochs=2,
                    validation_data=validation_generator,
                    validation_steps=step_size_validate,
                    callbacks=callbacks_list)

model.summary()
model.save('new_model.h5')

print(model.metrics_names)
s = model.evaluate_generator(validation_generator, step_size_validate, verbose=1)
print(s)


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

import heapq

# 정점의 개수 n, 간선의 개수 m
n, m = map(int, input().split())

# 무한을 의미하는 INF
INF = int(1e9)

# 그래프 초기화
graph = [[] * (n+1) for _ in range(n+1)]

# 간선 정보 입력
for _ in range(m):
    a, b, c = map(int, input().split())
    # a->b가 c비용
    graph[a].append((b, c))
    graph[b].append((a, c))

v1, v2 = map(int, input().split())

def dijkstra(start):
    # 최단 거리 테이블을 모두 무한으로 초기화
    distance = [INF] * (n + 1)
    q = []
    # 시작 노드로 가기 위한 최단 경로는 0으로 설정하여, 큐에 삽입
    heapq.heappush(q, (0, start))
    distance[start] = 0

    while q:
        # 가장 최단 거리가 짧은 노드에 대한 정보 꺼내기
        dist, now = heapq.heappop(q)
        # 현재 노드가 이미 처리된 적이 있는 노드라면 무시
        if distance[now] < dist:
            continue
        # 현재 노드와 연결된 다른 인접한 노드들을 확인
        for i in graph[now]:
            cost = dist + i[1]
            # 현재 노드를 거쳐서, 다른 노드로 이동하는 거리가 더 짧은 경우
            if cost < distance[i[0]]:
                distance[i[0]] = cost
                heapq.heappush(q, (cost, i[0]))
    return distance

# 다익스트라 알고리즘을 수행
one = dijkstra(1)
v1_ = dijkstra(v1)
v2_ = dijkstra(v2)

count = min(one[v1] + v1_[v2] + v2_[n], one[v2] + v2_[v1] + v1_[n])
if count >= INF:
    print(-1)
else:
    print(count)


from django.db import models
from django.utils import timezone

class Venda(models.Model):
    data = models.DateTimeField(default=timezone.now)
    descricao = models.CharField(max_length=255)
    valor = models.DecimalField(max_digits=5, decimal_places=2)

    def adicionar(self):
        self.data = timezone.now().date()
        self.save()

    def __str__(self):
        return self.descricao


import datetime
import inspect
import io
import os
import random
import sys

from stem import UNDEFINED
from stem.client.datatype import HASH_LEN, ZERO, Address, Certificate, CloseReason, RelayCommand, Size, split
from stem.util import _hash_attr, datetime_to_unix, str_type, str_tools

FIXED_PAYLOAD_LEN = 509
AUTH_CHALLENGE_SIZE = 32

STREAM_ID_REQUIRED = (
  RelayCommand.BEGIN,
  RelayCommand.DATA,
  RelayCommand.END,
  RelayCommand.CONNECTED,
  RelayCommand.RESOLVE,
  RelayCommand.RESOLVED,
  RelayCommand.BEGIN_DIR,
)

STREAM_ID_DISALLOWED = (
  RelayCommand.EXTEND,
  RelayCommand.EXTENDED,
  RelayCommand.TRUNCATE,
  RelayCommand.TRUNCATED,
  RelayCommand.DROP,
  RelayCommand.EXTEND2,
  RelayCommand.EXTENDED2,
)

class Cell(object):
  """
  Metadata for ORPort cells.
  """

  NAME = 'UNKNOWN'
  VALUE = -1
  IS_FIXED_SIZE = False

  @staticmethod
  def by_name(name):
    """
    Provides cell attributes by its name.

    :parm str name: cell command to fetch

    :raise: **ValueError** if cell type is invalid
    """

    for _, cls in inspect.getmembers(sys.modules[__name__]):
      if name == getattr(cls, 'NAME', UNDEFINED):
        return cls

    raise ValueError("'%s' isn't a valid cell type" % name)

  @staticmethod
  def by_value(value):
    """
    Provides cell attributes by its value.

    :parm int value: cell value to fetch

    :raise: **ValueError** if cell type is invalid
    """

    for _, cls in inspect.getmembers(sys.modules[__name__]):
      if value == getattr(cls, 'VALUE', UNDEFINED):
        return cls

    raise ValueError("'%s' isn't a valid cell value" % value)

  def pack(self, link_protocol):
    raise NotImplementedError('Unpacking not yet implemented for %s cells' % type(self).NAME)

  @staticmethod
  def unpack(content, link_protocol):
    """
    Unpacks all cells from a response.

    :param bytes content: payload to decode
    :param int link_protocol: link protocol version

    :returns: :class:`~stem.client.cell.Cell` generator

    :raises:
      * ValueError if content is malformed
      * NotImplementedError if unable to unpack any of the cell types
    """

    while content:
      cell, content = Cell.pop(content, link_protocol)
      yield cell

  @staticmethod
  def pop(content, link_protocol, **kwargs):
    """
    Unpacks the first cell.

    :param bytes content: payload to decode
    :param int link_protocol: link protocol version

    :returns: (:class:`~stem.client.cell.Cell`, remainder) tuple

    :raises:
      * ValueError if content is malformed
      * NotImplementedError if unable to unpack this cell type
    """

    circ_id, content = Size.SHORT.pop(content) if link_protocol < 4 else Size.LONG.pop(content)
    command, content = Size.CHAR.pop(content)
    cls = Cell.by_value(command)

    if cls.IS_FIXED_SIZE:
      payload_len = FIXED_PAYLOAD_LEN
    else:
      payload_len, content = Size.SHORT.pop(content)

    if len(content) < payload_len:
      raise ValueError('%s cell should have a payload of %i bytes, but only had %i' % (cls.NAME, payload_len, len(content)))

    payload, content = split(content, payload_len)
    return cls._unpack(payload, circ_id, link_protocol, **kwargs), content

  @classmethod
  def _pack(cls, link_protocol, payload, circ_id = 0):
    """
    Provides bytes that can be used on the wire for these cell attributes.
    Format of a properly packed cell depends on if it's fixed or variable
    sized...

    ::

      Fixed:    [ CircuitID ][ Command ][ Payload ][ Padding ]
      Variable: [ CircuitID ][ Command ][ Size ][ Payload ]

    :param str name: cell command
    :param int link_protocol: link protocol version
    :param bytes payload: cell payload
    :param int circ_id: circuit id, if a CircuitCell

    :return: **bytes** with the encoded payload

    :raise: **ValueError** if cell type invalid or payload is too large
    """

    if isinstance(cls, CircuitCell) and circ_id is None:
      raise ValueError('%s cells require a circ_id' % cls.NAME)

    cell = io.BytesIO()
    cell.write(Size.LONG.pack(circ_id) if link_protocol > 3 else Size.SHORT.pack(circ_id))
    cell.write(Size.CHAR.pack(cls.VALUE))
    cell.write(b'' if cls.IS_FIXED_SIZE else Size.SHORT.pack(len(payload)))
    cell.write(payload)

    # pad fixed sized cells to the required length

    if cls.IS_FIXED_SIZE:
      cell_size = cell.seek(0, io.SEEK_END)
      fixed_cell_len = 514 if link_protocol > 3 else 512

      if cell_size > fixed_cell_len:
        raise ValueError('Payload of %s is too large (%i bytes), must be less than %i' % (cls.NAME, cell_size, fixed_cell_len))

      cell.write(ZERO * (fixed_cell_len - cell_size))

    return cell.getvalue()

  @classmethod
  def _unpack(cls, content, circ_id, link_protocol):
    """
    Subclass implementation for unpacking cell content.

    :param bytes content: payload to decode
    :param int link_protocol: link protocol version
    :param int circ_id: circuit id cell is for

    :returns: instance of this cell type

    :raises: **ValueError** if content is malformed
    """

    raise NotImplementedError('Unpacking not yet implemented for %s cells' % cls.NAME)

  def __eq__(self, other):
    return hash(self) == hash(other) if isinstance(other, Cell) else False

  def __ne__(self, other):
    return not self == other

class CircuitCell(Cell):
  """
  Cell concerning circuits.

  :var int circ_id: circuit id
  """

  def __init__(self, circ_id):
    self.circ_id = circ_id

class PaddingCell(Cell):
  """
  Randomized content to either keep activity going on a circuit.

  :var bytes payload: randomized payload
  """

  NAME = 'PADDING'
  VALUE = 0
  IS_FIXED_SIZE = True

  def __init__(self, payload = None):
    if not payload:
      payload = os.urandom(FIXED_PAYLOAD_LEN)
    elif len(payload) != FIXED_PAYLOAD_LEN:
      raise ValueError('Padding payload should be %i bytes, but was %i' % (FIXED_PAYLOAD_LEN, len(payload)))

    self.payload = payload

  def pack(self, link_protocol):
    return PaddingCell._pack(link_protocol, self.payload)

  @classmethod
  def _unpack(cls, content, circ_id, link_protocol):
    return PaddingCell(content)

  def __hash__(self):
    return _hash_attr(self, 'payload')

class CreateCell(CircuitCell):
  NAME = 'CREATE'
  VALUE = 1
  IS_FIXED_SIZE = True

class CreatedCell(CircuitCell):
  NAME = 'CREATED'
  VALUE = 2
  IS_FIXED_SIZE = True

class RelayCell(CircuitCell):
  """
  Command concerning a relay circuit.

  :var stem.client.RelayCommand command: command to be issued
  :var int command_int: integer value of our command
  :var bytes data: payload of the cell
  :var int recognized: zero if cell is decrypted, non-zero otherwise
  :var int digest: running digest held with the relay
  :var int stream_id: specific stream this concerns
  """

  NAME = 'RELAY'
  VALUE = 3
  IS_FIXED_SIZE = True

  def __init__(self, circ_id, command, data, digest = 0, stream_id = 0, recognized = 0, length = None):
    if 'hashlib.HASH' in str(type(digest)):
      # Unfortunately hashlib generates from a dynamic private class so
      # isinstance() isn't such a great option.

      digest = Size.LONG.unpack(digest.digest()[:4])
    elif isinstance(digest, (bytes, str_type)):
      digest = Size.LONG.unpack(digest[:4])
    elif isinstance(digest, int):
      pass
    else:
      raise ValueError('RELAY cell digest must be a hash, string, or int but was a %s' % type(digest).__name__)

    super(RelayCell, self).__init__(circ_id)
    self.command, self.command_int = RelayCommand.get(command)
    self.data = str_tools._to_bytes(data)
    self.recognized = recognized
    self.digest = digest
    self.stream_id = stream_id

    if length is None:
        length = len(self.data)
    self.length = length

    if digest == 0:
      if not stream_id and self.command in STREAM_ID_REQUIRED:
        raise ValueError('%s relay cells require a stream id' % self.command)
      elif stream_id and self.command in STREAM_ID_DISALLOWED:
        raise ValueError('%s relay cells concern the circuit itself and cannot have a stream id' % self.command)

  def pack(self, link_protocol):
    payload = io.BytesIO()
    payload.write(Size.CHAR.pack(self.command_int))
    payload.write(Size.SHORT.pack(self.recognized))
    payload.write(Size.SHORT.pack(self.stream_id))
    payload.write(Size.LONG.pack(self.digest))
    payload.write(Size.SHORT.pack(self.length))
    payload.write(self.data)

    return RelayCell._pack(link_protocol, payload.getvalue(), self.circ_id)

  @classmethod
  def _unpack(cls, content, circ_id, link_protocol, is_encrypted=False):
    command, content = Size.CHAR.pop(content)
    recognized, content = Size.SHORT.pop(content)  # 'recognized' field
    stream_id, content = Size.SHORT.pop(content)
    digest, content = Size.LONG.pop(content)

    data_len, content = Size.SHORT.pop(content)
    if is_encrypted:
        real_len = FIXED_PAYLOAD_LEN - (1 + 2 + 2 + 4 + 2) # RELAY header size
    data, content = split(content, data_len if not is_encrypted else real_len)

    return RelayCell(circ_id, command, data, digest, stream_id, recognized, data_len)

  def __hash__(self):
    return _hash_attr(self, 'command_int', 'stream_id', 'digest', 'data')

class DestroyCell(CircuitCell):
  """
  Closes the given circuit.

  :var stem.client.CloseReason reason: reason the circuit is being closed
  :var int reason_int: integer value of our closure reason
  """

  NAME = 'DESTROY'
  VALUE = 4
  IS_FIXED_SIZE = True

  def __init__(self, circ_id, reason = CloseReason.NONE):
    super(DestroyCell, self).__init__(circ_id)
    self.reason, self.reason_int = CloseReason.get(reason)

  def pack(self, link_protocol):
    return DestroyCell._pack(link_protocol, Size.CHAR.pack(self.reason_int), self.circ_id)

  @classmethod
  def _unpack(cls, content, circ_id, link_protocol):
    content = content.rstrip(ZERO)

    if not content:
      content = ZERO
    elif len(content) > 1:
      raise ValueError('Circuit closure reason should be a single byte, but was %i' % len(content))

    return DestroyCell(circ_id, Size.CHAR.unpack(content))

  def __hash__(self):
    return _hash_attr(self, 'circ_id', 'reason_int')

class CreateFastCell(CircuitCell):
  """
  Create a circuit with our first hop. This is lighter weight than further hops
  because we've already established the relay's identity and secret key.

  :var bytes key_material: randomized key material
  """

  NAME = 'CREATE_FAST'
  VALUE = 5
  IS_FIXED_SIZE = True

  def __init__(self, circ_id, key_material = None):
    if not key_material:
      key_material = os.urandom(HASH_LEN)
    elif len(key_material) != HASH_LEN:
      raise ValueError('Key material should be %i bytes, but was %i' % (HASH_LEN, len(key_material)))

    super(CreateFastCell, self).__init__(circ_id)
    self.key_material = key_material

  def pack(self, link_protocol):
    return CreateFastCell._pack(link_protocol, self.key_material, self.circ_id)

  @classmethod
  def _unpack(cls, content, circ_id, link_protocol):
    content = content.rstrip(ZERO)

    if len(content) != HASH_LEN:
      raise ValueError('Key material should be %i bytes, but was %i' % (HASH_LEN, len(content)))

    return CreateFastCell(circ_id, content)

  def __hash__(self):
    return _hash_attr(self, 'circ_id', 'key_material')

class CreatedFastCell(CircuitCell):
  """
  CREATE_FAST reply.

  :var bytes key_material: randomized key material
  :var bytes derivative_key: hash proving the relay knows our shared key
  """

  NAME = 'CREATED_FAST'
  VALUE = 6
  IS_FIXED_SIZE = True

  def __init__(self, circ_id, derivative_key, key_material = None):
    if not key_material:
      key_material = os.urandom(HASH_LEN)
    elif len(key_material) != HASH_LEN:
      raise ValueError('Key material should be %i bytes, but was %i' % (HASH_LEN, len(key_material)))

    if len(derivative_key) != HASH_LEN:
      raise ValueError('Derivatived key should be %i bytes, but was %i' % (HASH_LEN, len(derivative_key)))

    super(CreatedFastCell, self).__init__(circ_id)
    self.key_material = key_material
    self.derivative_key = derivative_key

  def pack(self, link_protocol):
    return CreatedFastCell._pack(link_protocol, self.key_material + self.derivative_key, self.circ_id)

  @classmethod
  def _unpack(cls, content, circ_id, link_protocol):
    content = content.rstrip(ZERO)

    if len(content) != HASH_LEN * 2:
      raise ValueError('Key material and derivatived key should be %i bytes, but was %i' % (HASH_LEN * 2, len(content)))

    return CreatedFastCell(circ_id, content[HASH_LEN:], content[:HASH_LEN])

  def __hash__(self):
    return _hash_attr(self, 'circ_id', 'derivative_key', 'key_material')

class VersionsCell(Cell):
  """
  Link version negotiation cell.

  :var list versions: link versions
  """

  NAME = 'VERSIONS'
  VALUE = 7
  IS_FIXED_SIZE = False

  def __init__(self, versions):
    self.versions = versions

  def pack(self, link_protocol = None):
    # Used for link version negotiation so we don't have that yet. This is fine
    # since VERSION cells avoid most version dependent attributes.

    payload = b''.join([Size.SHORT.pack(v) for v in self.versions])
    return VersionsCell._pack(2, payload)

  @classmethod
  def _unpack(cls, content, circ_id, link_protocol):
    link_protocols = []

    while content:
      version, content = Size.SHORT.pop(content)
      link_protocols.append(version)

    return VersionsCell(link_protocols)

  def __hash__(self):
    return _hash_attr(self, 'versions')

class NetinfoCell(Cell):
  """
  Information relays exchange about each other.

  :var datetime timestamp: current time
  :var stem.client.Address receiver_address: receiver's OR address
  :var list sender_addresses: sender's OR addresses
  """

  NAME = 'NETINFO'
  VALUE = 8
  IS_FIXED_SIZE = True

  def __init__(self, receiver_address, sender_addresses, timestamp = None):
    self.timestamp = timestamp if timestamp else datetime.datetime.now()
    self.receiver_address = receiver_address
    self.sender_addresses = sender_addresses

  def pack(self, link_protocol):
    payload = io.BytesIO()
    payload.write(Size.LONG.pack(int(datetime_to_unix(self.timestamp))))
    payload.write(self.receiver_address.pack())
    payload.write(Size.CHAR.pack(len(self.sender_addresses)))

    for addr in self.sender_addresses:
      payload.write(addr.pack())

    return NetinfoCell._pack(link_protocol, payload.getvalue())

  @classmethod
  def _unpack(cls, content, circ_id, link_protocol):
    if len(content) < 4:
      raise ValueError('NETINFO cell expected to start with a timestamp')

    timestamp, content = Size.LONG.pop(content)
    receiver_address, content = Address.pop(content)

    sender_addresses = []
    sender_addr_count, content = Size.CHAR.pop(content)

    for i in range(sender_addr_count):
      addr, content = Address.pop(content)
      sender_addresses.append(addr)

    return NetinfoCell(receiver_address, sender_addresses, datetime.datetime.utcfromtimestamp(timestamp))

  def __hash__(self):
    return _hash_attr(self, 'timestamp', 'receiver_address', 'sender_addresses')

class RelayEarlyCell(RelayCell):
  NAME = 'RELAY_EARLY'
  VALUE = 9
  IS_FIXED_SIZE = True

  def __init__(self, *kargs, **kwargs):
    super(RelayEarlyCell, self).__init__(*kargs, **kwargs)

  def pack(self, link_protocol):
    payload = io.BytesIO()
    payload.write(Size.CHAR.pack(self.command_int))
    payload.write(Size.SHORT.pack(self.recognized))
    payload.write(Size.SHORT.pack(self.stream_id))
    payload.write(Size.LONG.pack(self.digest))
    payload.write(Size.SHORT.pack(self.length))
    payload.write(self.data)

    return RelayEarlyCell._pack(
        link_protocol, payload.getvalue(), self.circ_id)

  @classmethod
  def _unpack(cls, *kargs, **kwargs):
    return RelayCell._unpack(*kargs, **kwargs)

class Create2Cell(CircuitCell):
  NAME = 'CREATE2'
  VALUE = 10
  IS_FIXED_SIZE = True
  _handshake_type_names = {'ntor': 0x0002, 'tap': 0x0001}

  def __init__(self, circ_id, htype='ntor', hdata='', length=None):
    if isinstance(htype, str):
        htype = self._handshake_type_names[htype.lower()] # in ['ntor', 'tap']

    if htype != 0x0002:
        raise NotImplementedError('No support for handshake {}.'.format(htype))

    super(Create2Cell, self).__init__(circ_id)
    self.type = htype
    self.data = str_tools._to_bytes(hdata)

    if length is None:
        length = len(self.data)
    self.length = int(length)

  def pack(self, link_protocol):
    payload = io.BytesIO()
    payload.write(Size.SHORT.pack(self.type))
    payload.write(Size.SHORT.pack(self.length))
    payload.write(self.data)

    return Create2Cell._pack(link_protocol, payload.getvalue(), self.circ_id)

  @classmethod
  def _unpack(cls, content, circ_id, link_protocol):
    htype, content = Size.SHORT.pop(content)
    length, content = Size.SHORT.pop(content)
    hdata, content = split(content, length)

    return Create2Cell(circ_id, htype, hdata, length)

  def __hash__(self):
    return _hash_attr(self, 'htype', 'data')

class Created2Cell(CircuitCell):
  NAME = 'CREATED2'
  VALUE = 11
  IS_FIXED_SIZE = True

  def __init__(self, circ_id, hdata='', length=None):
    super(Created2Cell, self).__init__(circ_id)
    self.data = str_tools._to_bytes(hdata)

    if length is None:
        length = len(self.data)
    self.length = int(length)

  def pack(self, link_protocol):
    payload = io.BytesIO()
    payload.write(Size.SHORT.pack(self.length))
    payload.write(self.data)

    return Created2Cell._pack(link_protocol, payload.getvalue(), self.circ_id)

  @classmethod
  def _unpack(cls, content, circ_id, link_protocol):
    length, content = Size.SHORT.pop(content)
    hdata, content = split(content, length)

    return Created2Cell(circ_id, hdata, length)

  def __hash__(self):
    return _hash_attr(self, 'data')

class PaddingNegotiateCell(Cell):
  NAME = 'PADDING_NEGOTIATE'
  VALUE = 12
  IS_FIXED_SIZE = True

class VPaddingCell(Cell):
  """
  Variable length randomized content to either keep activity going on a circuit.

  :var bytes payload: randomized payload
  """

  NAME = 'VPADDING'
  VALUE = 128
  IS_FIXED_SIZE = False

  def __init__(self, size = None, payload = None):
    if payload is None:
      payload = os.urandom(size) if size else os.urandom(random.randint(128, 1024))
    elif size is not None and size != len(payload):
      raise ValueError('VPaddingCell constructor specified both a size of %i bytes and payload of %i bytes' % (size, len(payload)))

    self.payload = payload

  def pack(self, link_protocol):
    return VPaddingCell._pack(link_protocol, self.payload)

  @classmethod
  def _unpack(cls, content, circ_id, link_protocol):
    return VPaddingCell(payload = content)

  def __hash__(self):
    return _hash_attr(self, 'payload')

class CertsCell(Cell):
  """
  Certificate held by the relay we're communicating with.

  :var list certificates: :class:`~stem.client.Certificate` of the relay
  """

  NAME = 'CERTS'
  VALUE = 129
  IS_FIXED_SIZE = False

  def __init__(self, certs):
    self.certificates = certs

  def pack(self, link_protocol):
    return CertsCell._pack(link_protocol, Size.CHAR.pack(len(self.certificates)) + b''.join([cert.pack() for cert in self.certificates]))

  @classmethod
  def _unpack(cls, content, circ_id, link_protocol):
    cert_count, content = Size.CHAR.pop(content)
    certs = []

    for i in range(cert_count):
      if not content:
        raise ValueError('CERTS cell indicates it should have %i certificates, but only contained %i' % (cert_count, len(certs)))

      cert, content = Certificate.pop(content)
      certs.append(cert)

    return CertsCell(certs)

  def __hash__(self):
    return _hash_attr(self, 'certificates')

class AuthChallengeCell(Cell):
  """
  First step of the authentication handshake.

  :var bytes challenge: random bytes for us to sign to authenticate
  :var list methods: authentication methods supported by the relay we're
    communicating with
  """

  NAME = 'AUTH_CHALLENGE'
  VALUE = 130
  IS_FIXED_SIZE = False

  def __init__(self, methods, challenge = None):
    if not challenge:
      challenge = os.urandom(AUTH_CHALLENGE_SIZE)
    elif len(challenge) != AUTH_CHALLENGE_SIZE:
      raise ValueError('AUTH_CHALLENGE must be %i bytes, but was %i' % (AUTH_CHALLENGE_SIZE, len(challenge)))

    self.challenge = challenge
    self.methods = methods

  def pack(self, link_protocol):
    payload = io.BytesIO()
    payload.write(self.challenge)
    payload.write(Size.SHORT.pack(len(self.methods)))

    for method in self.methods:
      payload.write(Size.SHORT.pack(method))

    return AuthChallengeCell._pack(link_protocol, payload.getvalue())

  @classmethod
  def _unpack(cls, content, circ_id, link_protocol):
    if len(content) < AUTH_CHALLENGE_SIZE + 2:
      raise ValueError('AUTH_CHALLENGE payload should be at least 34 bytes, but was %i' % len(content))

    challenge, content = split(content, AUTH_CHALLENGE_SIZE)
    method_count, content = Size.SHORT.pop(content)

    if len(content) < method_count * 2:
      raise ValueError('AUTH_CHALLENGE should have %i methods, but only had %i bytes for it' % (method_count, len(content)))

    methods = []

    for i in range(method_count):
      method, content = Size.SHORT.pop(content)
      methods.append(method)

    return AuthChallengeCell(methods, challenge)

  def __hash__(self):
    return _hash_attr(self, 'challenge', 'methods')

class AuthenticateCell(Cell):
  NAME = 'AUTHENTICATE'
  VALUE = 131
  IS_FIXED_SIZE = False

class AuthorizeCell(Cell):
  NAME = 'AUTHORIZE'
  VALUE = 132
  IS_FIXED_SIZE = False


from msrest.serialization import Model

class SkuDescription(Model):
    """Describes a sku for a scalable resource.

    :param name: Name of the resource sku
    :type name: str
    :param tier: Service Tier of the resource sku
    :type tier: str
    :param size: Size specifier of the resource sku
    :type size: str
    :param family: Family code of the resource sku
    :type family: str
    :param capacity: Current number of instances assigned to the resource
    :type capacity: int
    """

    _attribute_map = {
        'name': {'key': 'name', 'type': 'str'},
        'tier': {'key': 'tier', 'type': 'str'},
        'size': {'key': 'size', 'type': 'str'},
        'family': {'key': 'family', 'type': 'str'},
        'capacity': {'key': 'capacity', 'type': 'int'},
    }

    def __init__(self, name=None, tier=None, size=None, family=None, capacity=None):
        self.name = name
        self.tier = tier
        self.size = size
        self.family = family
        self.capacity = capacity


import argparse
import os

import h5py
from tqdm import tqdm

from config import config
from feature_extractor import FeatureExtractor2D
import nets

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('-v', '--video_dpath', type=str,
                        default='/nfs/zhujinguo/datasets/open_source_dataset/msvd_dataset/YouTubeClips',
                        help="The directory path of videos.")
    parser.add_argument('-m', '--model', type=str,
                        default='resnet152',
                        help="The name of model from which you extract features.")
    parser.add_argument('-b', '--batch_size', type=int, default=128, help="The batch size.") # 32gv100
    parser.add_argument('-s', '--stride', type=int, default=1,
                        help="Extract feature from every <s> frames.")
    parser.add_argument('-o', '--out', type=str,
                        default='feature_resnet152_s1/MSVD_ResNet152_{}.hdf5',
                        help="The file path of extracted feature.")
    return parser.parse_args()

def process_vidio(args, video_lists, subset):
    videoss = os.listdir(args.video_dpath)
    videos = []
    for video in videoss:
        if os.path.basename(video).split('.')[0] in video_lists:
            videos.append(video)
    save_path = args.out.format(subset)
    h5 = h5py.File(save_path, 'w') if not os.path.exists(subset) else h5py.File(subset, 'r+')
    for video in tqdm(videos):
        video_id = os.path.splitext(video)[0]
        if video_id in h5.keys():
            continue

        video_fpath = os.path.join(args.video_dpath, video)
        feats = extractor(video_fpath)
        if feats is not None:
            h5[video_id] = feats
    h5.close()

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

    config = config[args.model]

    model = getattr(nets, args.model)(pretrained=True)
    model.cuda()
    model.eval()

    extractor = FeatureExtractor2D(
        stride=args.stride,
        mean=config.mean,
        std=config.std,
        resize_to=config.resize_to,
        crop_to=config.crop_to,
        model=model,
        batch_size=args.batch_size)

    if not os.path.exists(os.path.dirname(args.out)):
        os.makedirs(os.path.dirname(args.out))

    subsets = ["train", "val", "test"]

    videoindex = open("/nfs/zhujinguo/datasets/open_source_dataset/msvd_dataset/txt_labels/youtube_mapping.txt", 'r').readlines()
    name2idx = dict()
    idx2name = dict()
    for v in videoindex:
        name2idx[v.split()[0]] = v.split()[1]
        idx2name[v.split()[1]] = v.split()[0]

    for subset in subsets:
        txtfile = "/nfs/zhujinguo/datasets/open_source_dataset/msvd_dataset/txt_labels/sents_{}_lc_nopunc.txt".format(subset)
        capinfos = open(txtfile, 'r').readlines()
        visited_imames = set()
        for caption in capinfos:
            vidindex = caption.split('\t')[0]
            if vidindex not in visited_imames:
                visited_imames.add(idx2name[vidindex])
        visited_imames = list(visited_imames)
        print("set {} has {} video".format(subset, len(visited_imames)))

        process_vidio(args, visited_imames, subset)


# Xbox(original) remote control plugin for EventGhost by jinxdone. Based on the Generic HID plugin by Bartman.
#
"""<rst>
Xbox remote control plugin, based on the Generic Human Interface Device (HID) plugin.

For this plugin to work you need the `XBCDRC drivers <http://www.redcl0ud.com/xbcd_rc.html>`__ by redcl0ud

|

.. image:: xbcdrc.png
   :align: center
"""

eg.RegisterPlugin(
    name = "Xbox Remote Control",
    author = (
        "jinxdone",
        "Bartman",
    ),
    version = "0.1.3.348",
    kind = "remote",
    guid = "{00F14717-738A-40C0-8BEC-F56F5D9AAF7E}",
    canMultiLoad = False,
    description = __doc__,
    icon = (
        "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAeJJREFUOMul"
        "k09I02EYxz/Pu/e33J80R6UUlEsolDCCYkSJ5S3oD1RCHaJDSEhQEHjpItShSxAU1M2SCCLo0EU6"
        "RV2KQCwkaqXk2PyTy0bbbGtuv/ftEK0xo0Y+3+vz+fLl+fKIxbKcUSxzFIAoccWRYq2QaCmKltLv"
        "BEZkWz/T0q5i0iLxv8H+Dkls6mWGVZID0AAWowJDkt91wc4m4mRkXF7aYRuphr298rQtTM5ZQNl5"
        "Uw8glUcUJe7RmyQDnaSfDOJO+b1v7eVCj5x3HresL23ojhB8FWVy9IztKjPVLSiPuKcHSTUdI/Nh"
        "DD31HDe7EfaG0fEQI4/C9kjlvq6OaVzrWXND0gdLBPd34fMdRi8s4j5oYvhZqC5aU42fR2zD6maM"
        "D3TaUJAcKnOL7QXJD9Rk0HZN3rQ2EwiV0BNRTOorxc27WbnikIwuqbT6BuvOytjJPhqddwTv3cZ4"
        "/Jh6B919kex0EvfhXWKLd+y+MmAr1HCA8asxZo5fIQXKIhiLBcH41vL93AsSPUMk2KNf/2LKcHAn"
        "c5feMxmO8A2UtSyV8lI6MUC27z6zTiufygaBDuY7rzPha6T4J7BSIHbHKb5s7efjz3RB8moLSaVx"
        "/wVXStdRknbmRDy4xrWe//3GH4uN7Ni6Xm5TAAAAAElFTkSuQmCC"

    )
)

import time
import binascii
import ctypes
import _winreg
import sys
import threading
import win32con
import win32event
import win32file
import wx.lib.mixins.listctrl as listmix

from ctypes import Structure, Union, c_byte, c_char, c_int, c_long, c_ulong, c_ushort, c_wchar
from ctypes import pointer, byref, sizeof, POINTER
from ctypes.wintypes import ULONG, BOOLEAN

class Text:
    manufacturer = "Manufacturer"
    deviceName = "Device Name"
    connected = "Connected"
    eventName = "Event prefix (optional):"
    yes = "Yes"
    no = "No"
    enduringEvents = "Trigger enduring events for buttons"
    multipleDeviceOptions = "Options for multiple same devices"
    noOtherPort = "Use selected device only if connected to current port"
    useFirstDevice = "Use first device found"
    errorFind = "Error finding an Xbox IR receiver"
    errorOpen = "Error opening an Xbox IR receiver"
    errorRead = "Error reading an Xbox IR receiver"
    errorRetrieval = "Error getting HID device info."
    errorReportLength = "Report length must not be zero for device "
    errorMultipleDevices = "Multiple devices found. Don't know which to use."
    errorInvalidDataIndex = "Found data index not defined as button or control value."
    vendorID = "Vendor ID "
    unknownCode = "Unknown code received: "
    configurationText = "If you receive unknown codes please report these to the EventGhost forum so we can add support for them"

#structures for ctypes
class GUID(Structure):
    _fields_ = [
        ("Data1", c_ulong),
        ("Data2", c_ushort),
        ("Data3", c_ushort),
        ("Data4", c_byte * 8)
    ]

class SP_DEVICE_INTERFACE_DATA(Structure):
    _fields_ = [("cbSize", c_ulong),
        ("InterfaceClassGuid", GUID),
        ("Flags", c_ulong),
        ("Reserved", POINTER(ULONG))
    ]

class SP_DEVICE_INTERFACE_DETAIL_DATA_A(Structure):
    _fields_ = [("cbSize", c_ulong),
        ("DevicePath", c_char * 255)
    ]

class HIDD_ATTRIBUTES(Structure):
    _fields_ = [("cbSize", c_ulong),
        ("VendorID", c_ushort),
        ("ProductID", c_ushort),
        ("VersionNumber", c_ushort)
    ]

class HIDP_CAPS(Structure):
    _fields_ = [
        ("Usage", c_ushort),
        ("UsagePage", c_ushort),
        ("InputReportByteLength", c_ushort),
        ("OutputReportByteLength", c_ushort),
        ("FeatureReportByteLength", c_ushort),
        ("Reserved", c_ushort * 17),
        ("NumberLinkCollectionNodes", c_ushort),
        ("NumberInputButtonCaps", c_ushort),
        ("NumberInputValueCaps", c_ushort),
        ("NumberInputDataIndices", c_ushort),
        ("NumberOutputButtonCaps", c_ushort),
        ("NumberOutputValueCaps", c_ushort),
        ("NumberOutputDataIndices", c_ushort),
        ("NumberFeatureButtonCaps", c_ushort),
        ("NumberFeatureValueCaps", c_ushort),
        ("NumberFeatureDataIndices", c_ushort)
    ]

class HIDP_CAPS_UNION(Union):
    class HIDP_BUTTON_CAPS_RANGE(Structure):
        _fields_ = [
            ("UsageMin", c_ushort),
            ("UsageMax", c_ushort),
            ("StringMin", c_ushort),
            ("StringMax", c_ushort),
            ("DesignatorMin", c_ushort),
            ("DesignatorMax", c_ushort),
            ("DataIndexMin", c_ushort),
            ("DataIndexMax", c_ushort)
        ]

    class HIDP_BUTTON_CAPS_NOT_RANGE(Structure):
        _fields_ = [
            ("Usage", c_ushort),
            ("Reserved1", c_ushort),
            ("StringIndex", c_ushort),
            ("Reserved2", c_ushort),
            ("DesignatorIndex", c_ushort),
            ("Reserved3", c_ushort),
            ("DataIndex", c_ushort),
            ("Reserved4", c_ushort)
        ]

    _fields_ = [
        ("Range", HIDP_BUTTON_CAPS_RANGE),
        ("NotRange", HIDP_BUTTON_CAPS_NOT_RANGE)
    ]

class HIDP_BUTTON_CAPS(Structure):
    _fields_ = [
        ("UsagePage", c_ushort),
        ("ReportID", c_char),
        ("IsAlias", BOOLEAN),
        ("BitField", c_ushort),
        ("LinkCollection", c_ushort),
        ("LinkUsage", c_ushort),
        ("LinkUsagePage", c_ushort),
        ("IsRange", BOOLEAN),
        ("IsStringRange", BOOLEAN),
        ("IsDesignatorRange", BOOLEAN),
        ("IsAbsolute", BOOLEAN),
        ("Reserved", c_ulong * 10),
        ("Info", HIDP_CAPS_UNION)
    ]

class HIDP_VALUE_CAPS(Structure):
    _fields_ = [
        ("UsagePage", c_ushort),
        ("ReportID", c_char),
        ("IsAlias", BOOLEAN),
        ("BitField", c_ushort),
        ("LinkCollection", c_ushort),
        ("LinkUsage", c_ushort),
        ("LinkUsagePage", c_ushort),
        ("IsRange", BOOLEAN),
        ("IsStringRange", BOOLEAN),
        ("IsDesignatorRange", BOOLEAN),
        ("IsAbsolute", BOOLEAN),
        ("HasNull", BOOLEAN),
        ("Reserved", c_char),
        ("BitSize", c_ushort),
        ("ReportCount", c_ushort),
        ("Reserved2", c_ushort * 5),
        ("UnitsExp", c_ulong),
        ("Units", c_ulong),
        ("LogicalMin", c_long),
        ("LogicalMax", c_long),
        ("PhysicalMin", c_long),
        ("PhysicalMax", c_long),
        ("Info", HIDP_CAPS_UNION)
    ]

class HIDP_DATA(Structure):
    class HIDP_DATA_VALUE(Union):
        _fields_ = [
            ("RawValue", c_ulong),
            ("On", BOOLEAN),
        ]

    _fields_ = [
        ("DataIndex", c_ushort),
        ("Reserved", c_ushort),
        ("Data", HIDP_DATA_VALUE)
    ]

# Flags controlling what is included in the device information set built
# by SetupDiGetClassDevs
DIGCF_DEFAULT         = 0x00000001  # only valid with DIGCF_DEVICEINTERFACE
DIGCF_PRESENT         = 0x00000002
DIGCF_ALLCLASSES      = 0x00000004
DIGCF_PROFILE         = 0x00000008
DIGCF_DEVICEINTERFACE = 0x00000010

#constants to identify the device info
DEVICE_PATH = 0
VENDOR_ID = 1
VENDOR_STRING = 2
PRODUCT_ID = 3
PRODUCT_STRING = 4
VERSION_NUMBER = MAX_INDEX = 5

#helper class to iterate, find and open hid devices
class HIDHelper:
    text = Text
    deviceList = []

    def __init__(self):
        self.UpdateDeviceList()

    def UpdateDeviceList(self):
        self.deviceList = []

        #dll references
        setupapiDLL = ctypes.windll.setupapi
        hidDLL =  ctypes.windll.hid

        #prepare Interfacedata
        interfaceInfo = SP_DEVICE_INTERFACE_DATA()
        interfaceInfo.cbSize = sizeof(interfaceInfo)

        #prepare InterfaceDetailData Structure
        interfaceDetailData = SP_DEVICE_INTERFACE_DETAIL_DATA_A()
        interfaceDetailData.cbSize = 5

        #prepare HIDD_ATTRIBUTES
        hiddAttributes = HIDD_ATTRIBUTES()
        hiddAttributes.cbSize = sizeof(hiddAttributes)

        #get guid for HID device class
        g = GUID()
        hidDLL.HidD_GetHidGuid(byref(g))

        #get handle to the device information set
        hinfo = setupapiDLL.SetupDiGetClassDevsA(byref(g), None, None,
            DIGCF_PRESENT + DIGCF_DEVICEINTERFACE)

        #enumerate devices
        i = 0
        while setupapiDLL.SetupDiEnumDeviceInterfaces(hinfo,
            None, byref(g), i, byref(interfaceInfo)):
            device = {}
            i += 1

            #get the required size
            requiredSize = c_ulong()
            setupapiDLL.SetupDiGetDeviceInterfaceDetailA(hinfo,
                byref(interfaceInfo), None, 0, byref(requiredSize), None)
            if requiredSize.value > 250:
                eg.PrintError(self.text.errorRetrieval)
                continue #prevent a buffer overflow

            #get the actual info
            setupapiDLL.SetupDiGetDeviceInterfaceDetailA(
                hinfo,
                byref(interfaceInfo),
                byref(interfaceDetailData),
                requiredSize,
                pointer(requiredSize),
                None
            )
            device[DEVICE_PATH] = interfaceDetailData.DevicePath

            #get handle to HID device
            try:
                hidHandle = win32file.CreateFile(
                    device[DEVICE_PATH],
                    win32con.GENERIC_READ | win32con.GENERIC_WRITE,
                    win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
                    None,
                    win32con.OPEN_EXISTING,
                    0,
                    0
                )
                #skipping devices which cannot be opened
                #(e.g. mice & keyboards, which are opened exclusivly by OS)
                if int(hidHandle) <= 0:
                    continue
            except:
                continue

            #getting additional info
            hidDLL.HidD_GetAttributes(int(hidHandle), byref(hiddAttributes))
            device[VENDOR_ID] = hiddAttributes.VendorID
            device[PRODUCT_ID] = hiddAttributes.ProductID
            device[VERSION_NUMBER] = hiddAttributes.VersionNumber

            #prepare string buffer for device info strings
            hidpStringType = c_wchar * 128
            infoStr = hidpStringType()

            #getting manufacturer
            result = hidDLL.HidD_GetManufacturerString(
                int(hidHandle), byref(infoStr), ctypes.sizeof(infoStr))
            if not result or len(infoStr.value) == 0:
                #build a generic ManufacturerString with the vendor ID
                device[VENDOR_STRING] = self.text.vendorID + str(hiddAttributes.VendorID)
            else:
                device[VENDOR_STRING] = infoStr.value

            #getting device name
            result = hidDLL.HidD_GetProductString(
                int(hidHandle), byref(infoStr), ctypes.sizeof(infoStr))
            if not result or len(infoStr.value) == 0:
                #getting product name via registry
                devicePathSplit = device[DEVICE_PATH][4:].split("#")
                regHandle = _winreg.OpenKey(
                    _winreg.HKEY_LOCAL_MACHINE,
                    "SYSTEM\\CurrentControlSet\\Enum\\" + devicePathSplit[0] + \
                    "\\" + devicePathSplit[1] + "\\" + devicePathSplit[2])
                device[PRODUCT_STRING], regType = _winreg.QueryValueEx(regHandle, "DeviceDesc")
                _winreg.CloseKey(regHandle)
            else:
                device[PRODUCT_STRING] = infoStr.value

            #close handle
            win32file.CloseHandle(hidHandle)

            #add device to internal list
            self.deviceList.append(device)

        #end loop
        #destroy deviceinfolist
        setupapiDLL.SetupDiDestroyDeviceInfoList(hinfo)

    #gets the devicePath
    #the devicePath parameter is only used with multiple same devices
    def GetDevicePath(self,
        noOtherPort,
        devicePath,
        vendorID,
        productID,
        versionNumber,
        useFirstDevice = False
    ):
        found = 0
        path = ""
        for item in self.deviceList:
            if noOtherPort:
                #just search for devicepath
                if item[DEVICE_PATH] == devicePath:
                    #found right device
                    return devicePath
            else:
                #find the right vendor and product ids
                if item[VENDOR_ID] == vendorID \
                    and item[PRODUCT_ID] == productID:
                    found = found + 1
                    if (item[DEVICE_PATH] == devicePath) or (useFirstDevice):
                        #found right device
                        return item[DEVICE_PATH]
                    path = item[DEVICE_PATH]

        if found == 1:
            return path

        #multiple devices found
        #don't know which to use
        if found > 1:
            eg.PrintError(self.text.errorMultipleDevices)

        return None

class HIDThread(threading.Thread):
    def __init__(self,
        plugin,
        helper,
        enduringEvents,
        noOtherPort,
        devicePath,
        vendorID,
        vendorString,
        productID,
        productString,
        versionNumber,
        useFirstDevice
    ):
        self.plugin = plugin
        self.text = Text
        self.deviceName = vendorString + " " + productString
        self.abort = False
        self._overlappedRead = win32file.OVERLAPPED()
        self._overlappedRead.hEvent = win32event.CreateEvent(None, 1, 0, None)

        self.xbcdrc_mapping = {
            '006d50a': 'DISPLAY', '0066a05': 'DISPLAY', '0066afd': 'DISPLAY',
            '006e20a': 'REVERSE', '00671f5': 'REVERSE', '006710d': 'REVERSE',
            '006ea0a': 'PLAY', '00675f5': 'PLAY', '006750d': 'PLAY',
            '006e30a': 'FORWARD', '0067105': 'FORWARD', '00671fd': 'FORWARD',
            '006dd0a': 'SKIP-', '0066e05': 'SKIP-', '0066efd': 'SKIP-',
            '006e00a': 'STOP', '00670f5': 'STOP', '006700d': 'STOP',
            '006e60a': 'PAUSE', '00673f5': 'PAUSE', '006730d': 'PAUSE',
            '006df0a': 'SKIP+', '0066f05': 'SKIP+', '0066ffd': 'SKIP+',
            '006e50a': 'TITLE', '0067205': 'TITLE', '00672fd': 'TITLE',
            '006c30a': 'INFO', '0066105': 'INFO', '00661fd': 'INFO',
            '006a60a': 'UP', '00653f5': 'UP', '006530d': 'UP',
            '006a70a': 'DOWN', '0065305': 'DOWN', '00653fd': 'DOWN',
            '006a90a': 'LEFT', '0065405': 'LEFT', '00654fd': 'LEFT',
            '006a80a': 'RIGHT', '00654f5': 'RIGHT', '006540d': 'RIGHT',
            '0060b0a': 'SELECT', '0060505': 'SELECT', '00605fd': 'SELECT',
            '006f70a': 'MENU', '0067b05': 'MENU', '0067bfd': 'MENU',
            '006d80a': 'BACK', '0066cf5': 'BACK', '0066c0d': 'BACK',
            '006ce0a': '1', '00667f5': '1', '006670d': '1',
            '006cd0a': '2', '0066605': '2', '00666fd': '2',
            '006cc0a': '3', '00666f5': '3', '006660d': '3',
            '006cb0a': '4', '0066505': '4', '00665fd': '4',
            '006ca0a': '5', '00665f5': '5', '006650d': '5',
            '006c90a': '6', '0066405': '6', '00664fd': '6',
            '006c80a': '7', '00664f5': '7', '006640d': '7',
            '006c70a': '8', '0066305': '8', '00663fd': '8',
            '006c60a': '9', '00663f5': '9', '006630d': '9',
            '006cf0a': '0', '0066705': '0', '00667fd': '0',
            '0064f0a': 'AUDIO',
            '006af0a': 'ENT.',
            '006d100': 'LT',
            '006890a': 'LAST',
            '006c000': 'MUTE',
            '006e70a': 'A*B',
            '006f90a': 'EXIT',
            '0067f0a': 'D',
            '0068c0a': 'REC'
        }

        #getting devicePath
        self.devicePath = helper.GetDevicePath(
            noOtherPort,
            devicePath,
            vendorID,
            productID,
            versionNumber,
            useFirstDevice
        )
        plugin.devicePath = self.devicePath

        if not self.devicePath:
            plugin.PrintError(self.text.errorFind)
            self.plugin.status = 2
            return

        threading.Thread.__init__(self, name = self.devicePath)

        #setting members
        self.helper = helper
        self.enduringEvents = enduringEvents
        self.start()

    def AbortThread(self):
        self.abort = True
        win32event.SetEvent(self._overlappedRead.hEvent)
        self.plugin.status = 2

    def run(self):
        #open file/devcice
        try:
            handle = win32file.CreateFile(
                self.devicePath,
                    win32con.GENERIC_READ | win32con.GENERIC_WRITE,
                    win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
                None, # no security
                win32con.OPEN_EXISTING,
                win32con.FILE_ATTRIBUTE_NORMAL | win32con.FILE_FLAG_OVERLAPPED,
                0
            )
        except:
            self.plugin.PrintError(self.text.errorOpen)
            self.plugin.status = 3
            return

        #getting data to get the right buffer size
        hidDLL =  ctypes.windll.hid
        setupapiDLL = ctypes.windll.setupapi

        #get preparsed data
        preparsedData = c_ulong()
        result = hidDLL.HidD_GetPreparsedData(
            int(handle),
            ctypes.byref(preparsedData)
        )

        #getCaps
        hidpCaps = HIDP_CAPS()
        result = hidDLL.HidP_GetCaps(preparsedData, ctypes.byref(hidpCaps))

        n = hidpCaps.InputReportByteLength
        if n == 0:
            self.abort = True
            self.plugin.PrintError(self.text.errorReportLength + self.deviceName)

        rt = c_int(0)   #report type input
        rl = c_ulong(n)  #report length
        maxDataL = hidDLL.HidP_MaxDataListLength(rt, preparsedData)

        #getting button caps
        bCapsArrL = c_ushort(hidpCaps.NumberInputButtonCaps)
        bCapsArrType = HIDP_BUTTON_CAPS * bCapsArrL.value
        bCapsArr = bCapsArrType()

        hidDLL.HidP_GetButtonCaps(
            rt,
            ctypes.byref(bCapsArr),
            ctypes.byref(bCapsArrL),
            preparsedData
        )

        #getting value caps
        vCapsArrL = c_ushort(hidpCaps.NumberInputValueCaps)
        vCapsArrType = HIDP_VALUE_CAPS * vCapsArrL.value
        vCapsArr = vCapsArrType()

        hidDLL.HidP_GetValueCaps(
            rt,
            ctypes.byref(vCapsArr),
            ctypes.byref(vCapsArrL),
            preparsedData
        )

        #parsing caps
        # prepare a list to find and store for each index
        # whether it is a button or value
        oldValues = {}
        dataIndexType = [0] * hidpCaps.NumberInputDataIndices

        #list entries depending on caps
        for i in range(bCapsArrL.value):
            if bCapsArr[i].IsRange:
                for ii in range(
                    bCapsArr[i].Info.Range.DataIndexMin,
                    bCapsArr[i].Info.Range.DataIndexMax + 1
                ):
                    dataIndexType[ii] = 1
            else:
                ii = bCapsArr[i].Info.NotRange.DataIndex
                dataIndexType[ii] = 1

        for i in range(vCapsArrL.value):
            if vCapsArr[i].IsRange:
                for ii in range(
                    vCapsArr[i].Info.Range.DataIndexMin,
                    vCapsArr[i].Info.Range.DataIndexMax + 1
                ):
                    dataIndexType[ii] = 2
                    oldValues[ii] = sys.maxint
            else:
                ii = vCapsArr[i].Info.NotRange.DataIndex
                dataIndexType[ii] = 2
                oldValues[ii] = sys.maxint

        #prepare data array with maximum possible length
        DataArrayType = HIDP_DATA * maxDataL
        data = DataArrayType()

        #initiziling finished. setting status
        self.plugin.status = 1

        # Some added variables -jinxdone
        btnup_count = 0
        old_event = ""
        event_gen = False

        while not self.abort:
            #try to read and wait for an event to happen
            try:
                win32event.ResetEvent(self._overlappedRead.hEvent)
                rc, buf = win32file.ReadFile(handle, n, self._overlappedRead)
                #waiting for an event
                win32event.WaitForSingleObject(
                    self._overlappedRead.hEvent,
                    win32event.INFINITE
                )
            except:
                self.plugin.PrintError(self.text.errorRead)
                self.abort = True
                #device got disconnected so set status to waiting
                self.plugin.status = 2

            #parse data
            if len(buf) == n and not self.abort:
                mystr = binascii.hexlify(str(buf))[3:10]
                if mystr[0] == "0":
                    if mystr == "0000000":
                        continue
                    try:
                        mystr = self.xbcdrc_mapping[mystr]
                    except:
                        self.plugin.PrintError(self.plugin.text.unknownCode + mystr)
                        continue
                    #if mystr == old_event:
                    #    continue
                    #old_event = mystr
                    #button down event
                    if self.enduringEvents:
                        btnup_count = 0
                        if event_gen == False:
                            #enduring events
                            self.plugin.TriggerEnduringEvent(mystr)
                            event_gen = True
                    else:
                        #normal events
                        self.plugin.TriggerEvent(mystr)
                else:
                    #button up event
                    if self.enduringEvents and event_gen == True:
                        btnup_count += 1
                        if btnup_count > 13:
                            #print("Ending event..")
                            self.plugin.EndLastEvent()
                            btnup_count = 0
                            event_gen = False

        #loop aborted
        if self.enduringEvents:
            self.plugin.EndLastEvent()

        win32file.CloseHandle(handle)

        #free references
        hidDLL.HidD_FreePreparsedData(ctypes.byref(preparsedData))

        #HID thread finished

class XBCDRC(eg.PluginClass):
    helper = None
    text = Text
    thread = None
    status = -1
    # -1: not initizilized
    #  0: stopped
    #  1: running
    #  2: waiting for device
    #  3: error

    # Added to detect removing of the device, so state is updated and
    # can be recognized again when reattached -jinxdone
    def DeviceRemoved(self, event):
        """method to reconnect a disconnect device"""
        if self.status == 1:
            tmppath = event.payload[0].lower()
            if tmppath == self.devicePath:
                self.status = 2

    def ReconnectDevice(self, event):
        """method to reconnect a disconnect device"""
        if self.status == 2:
            #updating devicelist
            self.helper.UpdateDeviceList()

            #check if the right device was connected
            #getting devicePath
            self.devicePath = self.helper.GetDevicePath(
                self.noOtherPort,
                self.devicePath,
                self.vendorID,
                self.productID,
                self.versionNumber,
                self.useFirstDevice
            )
            if not self.devicePath:
                #wrong device
                return

            #create thread
            self.thread = HIDThread(
                self,
                self.helper,
                self.enduringEvents,
                self.noOtherPort,
                self.devicePath,
                self.vendorID,
                self.vendorString,
                self.productID,
                self.productString,
                self.versionNumber,
                self.useFirstDevice
            )

    # Modified version because of the changed config dialog.. -jinxdone
    def GetLabel(self, eventName, enduringEvents):
        return "XBCDRC"

    def __start__(self,
        eventName,
        enduringEvents
    ):

            #saving parameters so they cn be used to reconnect a device
        self.eventName = eventName
        self.enduringEvents = enduringEvents

        #saving parameters so they cn be used to reconnect a device
        self.eventName = eventName
        self.enduringEvents = enduringEvents
        #Settings from generic hid plugin set with xbox ir defaults..
        self.noOtherPort = False
        self.devicePath = ''
        self.vendorID = 1118
        self.vendorString = 'Vendor ID 1118'
        self.productID = 644
        self.productString = ''
        self.versionNumber = 0
        self.useFirstDevice = True

        if eventName:
            self.info.eventPrefix = eventName
        else:
            self.info.eventPrefix = "XBCDRC"
        #ensure helper object is up to date
        if not self.helper:
            self.helper = HIDHelper()
        else:
            self.helper.UpdateDeviceList()

        #create thread
        self.thread = HIDThread(
            self,
            self.helper,
            enduringEvents,
            self.noOtherPort,
            self.devicePath,
            self.vendorID,
            self.vendorString,
            self.productID,
            self.productString,
            self.versionNumber,
            self.useFirstDevice
        )
        #Bind plugin to RegisterDeviceNotification message
        eg.Bind("System.DeviceAttached", self.ReconnectDevice)

        # Added event -jinxdone
        #Bind plugin to RegisterDeviceNotification message
        eg.Bind("System.DeviceRemoved", self.DeviceRemoved)

    def __stop__(self):
        self.thread.AbortThread()

        #unbind from RegisterDeviceNotification message
        eg.Unbind("System.DeviceAttached", self.ReconnectDevice)
        self.status = 0

    def Configure(self,
        eventName = "",
        enduringEvents = True
    ):
        #ensure helper object is up to date
        if not self.helper:
            self.helper = HIDHelper()
        else:
            self.helper.UpdateDeviceList()

        panel = eg.ConfigPanel(self, resizable=True)

        #layout

        #sizers
        optionsSizer = wx.GridBagSizer(0, 5)

        #eventname
        optionsSizer.Add(
            wx.StaticText(panel, -1, self.text.eventName),
            (0, 0),
            flag = wx.ALIGN_CENTER_VERTICAL)
        eventNameCtrl = wx.TextCtrl(panel, value = eventName)
        eventNameCtrl.SetMaxLength(32)
        optionsSizer.Add(eventNameCtrl, (0, 1), (1, 2), flag = wx.EXPAND)

        #checkbox for enduring event option
        enduringEventsCtrl = wx.CheckBox(panel, -1, self.text.enduringEvents)
        enduringEventsCtrl.SetValue(enduringEvents)
        optionsSizer.Add(enduringEventsCtrl, (1, 0), (1, 3))

        panel.sizer.Add(optionsSizer, 0, wx.TOP, 10)

        configurationText = wx.StaticText(panel, -1, self.text.configurationText, (0, 0), (350, 40))
        panel.sizer.Add(configurationText, 0, wx.TOP, 30)

        while panel.Affirmed():
            panel.SetResult(
                eventNameCtrl.GetValue(),
                enduringEventsCtrl.GetValue()
            )


# diagram.py
from diagrams import Cluster, Diagram, Edge
from diagrams.aws.compute import EC2
from diagrams.aws.database import RDS
from diagrams.aws.network import ELB
from diagrams.onprem.ci import Jenkins, Circleci
from diagrams.onprem.container import Docker
from diagrams.onprem.vcs import Git, Github, Gitlab
from diagrams.azure import devops
from diagrams.generic.storage import Storage
from diagrams.alibabacloud.application import RdCloud

with Diagram("Upskilling Malmö", show=True, direction="TB"):
    # Timeline
    now = Storage("Now")

    ## -------------------------------------------------------
    ## PAST
    ## -------------------------------------------------------
    # Castle: Git
    with Cluster("Git"):
        git = Git("")
    git >> now

    ## -------------------------------------------------------
    ## FUTURE
    ## -------------------------------------------------------
    # Castle: Docker
    with Cluster("Docker"):
        docker = Docker("")
    now >> docker

    # Castle: Build Tools
    with Cluster("Build Tools Familiarity"):
        buildtools = ELB("")
        jenkins = Jenkins("Jenkins")
        buildtools << Edge(color="brown", style="dashed") << [
            jenkins,
            Gitlab("Gitlab runners"),
            Github("Github actions"),
            Circleci("Circle CI"),
            devops.Pipelines("Azure pipelines"),
            ]
    now >> buildtools

    # Castle: JCasC
    with Cluster("Jenkins (w/ JCasc)"):
        jenkinsjcasc = Jenkins("Jenkins (w/ JCasc)")
        trainingmaterial = EC2("Training material")
        jenkinsjcasc << Edge(color="brown", style="dashed") << [
            trainingmaterial,
            ]
    docker >> jenkinsjcasc
    jenkins >> jenkinsjcasc
    jenkinsjcasc

    # Castle: Basic knowledge on modern ops tooling
    with Cluster("Modern ops tooling"):
        opstooling = RdCloud("")
    now >> opstooling

    # Castle: Workshop facilitation
    with Cluster("Workshop facilitation"):
        workshop = ELB("")
    now >> workshop

    # Castle: Value Stream Mapping
    with Cluster("Value Stream Mapping"):
        valuestreammapping = ELB("")
    workshop >> valuestreammapping

    # Castle: Coach TDD Mob programming
    with Cluster("Coach TDD Mob programming"):
        tddmobprogramming = ELB("")
        tdd = EC2("Coach TDD")
        mobprogramming = EC2("Mob programming")
        tddmobprogramming << Edge(color="brown", style="dashed") << [
            tdd,
            mobprogramming,
            ]
    now >> tddmobprogramming


import re
import requests
import os

from time import sleep
from bs4 import BeautifulSoup
from urllib.parse import urljoin

HEADERS = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) '
                      'AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/52.0.2743.116 Safari/537.36'
    }

def get_novel_title(book_url):
    response = requests.get(book_url, headers=HEADERS)
    response.encoding = 'utf8'
    if response.status_code == 200:
        pattern = re.compile("
.*?'(.*?)' >(.*?).*?
", re.S)
        results = re.findall(pattern, response.text)
        for result in results:
            yield{
                'title': result[1],
                'link': urljoin(book_url, result[0])
            }

def get_novel_text(url):
    response = requests.get(url, headers=HEADERS)
    response.encoding = 'utf8'
    soup = BeautifulSoup(response.text, 'lxml')
    html_text = soup.prettify()
    pattern = re.compile('
(.*?)
|"content">(.*?)
', re.S)
    results = pattern.findall(html_text)
    novel = results[0][1].strip() + '\n'
    for result in results[1:-2]:
        novel += result[0].strip() + '\n'
    return novel

def write_to_local(chapter_title, content, novel_path):
    try:
        chapter_path = os.path.join(novel_path, chapter_title)
        with open(chapter_path + '.txt', 'a', encoding='utf8') as f:
            f.write(content)
    except FileNotFoundError:
        print('目录不存在')
    except FileExistsError:
        print('目录已经存在')

def main(base_url, path):
    for item in get_novel_title(base_url):
        print(f'正在下载----{item["title"]}')
        novel = get_novel_text(item['link'])
        write_to_local(item['title'], novel, path)
        sleep(0.2)

if __name__ == '__main__':
    try:
        title, url, path = input('请依次输入要爬虫的小说的名称、网址和保存地址：').split()
        novel_path = os.path.join(path, title)
        os.mkdir(novel_path)
        main(url, novel_path)
    except FileNotFoundError:
        print('目录不存在')
    except FileExistsError:
        print('目录已经存在了')


import os
import sys
from itertools import chain
from logging import Logger, getLogger
from pathlib import Path
from typing import Dict, Iterable, Union

from .config import ConfigParser
from .processor import ProcessManager

class BaseSparkCommand:

    def __init__(self,
                 config: ConfigParser = None,
                 spark_executable: str = None,
                 master: str = None,
                 deploy_mode: str = None,
                 queue: str = None,
                 conf: Iterable[str] = None,
                 packages: Union[Iterable[str], str] = None,
                 exclude_packages: Union[Iterable[str], str] = None,
                 repositories: Union[Iterable[str], str] = None,
                 reqs_paths: Union[Iterable[str], str] = None,
                 env: Dict[str, str] = None,
                 properties_file: str = None,
                 klass: str = None,
                 logger: Logger = None):
        try:
            cmd_config = config['spark']
        except KeyError:
            config.add_section('spark')
            cmd_config = config['spark']
        except TypeError:
            config = ConfigParser(default_sections=('spark', 'spark_env'))
            cmd_config = config['spark']

        try:
            env_config = config['spark-env']
        except KeyError:
            config.add_section('spark-env')
            env_config = config['spark-env']

        self.spark_executable = spark_executable or cmd_config.get('spark-executable', fallback='spark-submit')
        self.master = master or cmd_config.get('master')
        self.deploy_mode = deploy_mode or cmd_config.get('deploy-mode')
        self.queue = queue or cmd_config.get('queue')
        self.conf = cmd_config.getlist('conf', fallback=[])
        self.packages = cmd_config.getlist('packages', fallback=[])
        self.exclude_packages = cmd_config.getlist('exclude-packages', fallback=[])
        self.repositories = cmd_config.getlist('repositories', fallback=[])
        self.reqs_paths = cmd_config.getlist('reqs_paths', fallback=[])

        self.property_file = properties_file or cmd_config.get('property-file')
        self.klass = klass or cmd_config.get('class')

        try:
            self.env = {k: v for k, v in env_config.items()}
        except KeyError:
            self.env = {}

        self.env.update(env or {})

        self.logger = logger or getLogger(__name__)

        if conf:
            self.conf.extend(conf)

        if packages:
            if isinstance(packages, str):
                packages = packages.split(',')

            packages = [p.strip() for p in packages if p.strip()]
            if packages:
                self.packages.extend(packages)

        if exclude_packages:
            if isinstance(exclude_packages, str):
                exclude_packages = exclude_packages.split(',')

            exclude_packages = [p.strip() for p in exclude_packages if p.strip()]
            if packages:
                self.exclude_packages.extend(exclude_packages)

        if repositories:
            if isinstance(repositories, str):
                repositories = repositories.split(',')

            repositories = [p.strip() for p in repositories if p.strip()]
            if repositories:
                self.repositories.extend(repositories)

        if reqs_paths:
            if isinstance(reqs_paths, str):
                reqs_paths = reqs_paths.split(',')
            reqs_paths = [Path(p.strip()) for p in reqs_paths if p.strip()]

            if reqs_paths:
                self.reqs_paths.extend(reqs_paths)

    def build_command(self, *, executable):
        spark_cmd = [executable, ]
        if self.master:
            spark_cmd.extend(['--master', self.master])

        if self.deploy_mode:
            spark_cmd.extend(['--deploy-mode', self.deploy_mode])

        if self.queue:
            spark_cmd.extend(['--queue', self.queue])

        if self.conf:
            spark_cmd.extend(chain(*[['--conf', c] for c in self.conf]))

        if self.packages:
            spark_cmd.extend(['--packages', ','.join(self.packages)])

        if self.exclude_packages:
            spark_cmd.extend(['--exclude-packages', ','.join(self.exclude_packages)])

        if self.repositories:
            spark_cmd.extend(['--repositories', ','.join(self.repositories)])

        if self.property_file:
            spark_cmd.extend(['--properties-file', self.property_file])

        if self.klass:
            spark_cmd.extend(['--class', self.klass])

        if self.reqs_paths:
            ps = [r.strip() for r in chain(*[[str(p.resolve())
                                              for p in chain(Path(rp).rglob('*.egg'),
                                                             Path(rp).rglob('*.whl'),
                                                             Path(rp).rglob('*.zip'))
                                              if p.is_file()] for rp in self.reqs_paths]) if r.strip()]

            if len(ps):
                spark_cmd.extend(['--py-files', ','.join(ps)])

        return spark_cmd

class SparkSubmitCommand(BaseSparkCommand):

    def build_command(self, *, job_args: Iterable[str], **kwargs):
        kwargs.setdefault('executable', self.spark_executable)
        spark_cmd = super(SparkSubmitCommand, self).build_command(**kwargs)

        spark_cmd.extend(job_args)

        return spark_cmd

    def run(self, job_args: Iterable[str]):
        self.logger.info('Executing Spark job...')
        spark_command = self.build_command(job_args=job_args)

        env = os.environ.copy()

        env['PYSPARK_PYTHON'] = sys.executable
        env['PYSPARK_DRIVER_PYTHON'] = sys.executable

        env.update(self.env or {})

        self.logger.info(' '.join(spark_command))

        process = ProcessManager(spark_command, pass_through=True, env=env)
        process.start_process()
        process.wait()

        if process.returncode != 0:
            raise RuntimeError(f'Spark job failed with error: {process.returncode}')

class SparkInteractiveCommand(BaseSparkCommand):

    def __init__(self,
                 cmd_config,
                 *args,
                 pyspark_executable: str = None,
                 python_interactive_driver: str = None,
                 **kwargs):
        super(SparkInteractiveCommand, self).__init__(cmd_config, *args, **kwargs)

        try:
            cmd_config = cmd_config['interactive']
        except (KeyError, TypeError):
            cmd_config = ConfigParser(default_sections=('interactive',))
            cmd_config = cmd_config['interactive']

        self.pyspark_executable = pyspark_executable or cmd_config.get('pyspark-executable', fallback='pyspark')
        self.python_interactive_driver = python_interactive_driver or cmd_config.get('python-interactive-driver',
                                                                                fallback=sys.executable)

        # Force client deploy mode
        self.deploy_mode = 'client'

    def build_command(self, **kwargs):
        kwargs.setdefault('executable', self.pyspark_executable)
        return super(SparkInteractiveCommand, self).build_command(**kwargs)

    def run(self):

        self.logger.info('Executing Spark interactive...')

        spark_command = self.build_command()

        env = os.environ.copy()

        env['PYSPARK_PYTHON'] = sys.executable
        env['PYSPARK_DRIVER_PYTHON'] = self.python_interactive_driver

        env.update(self.env)

        self.logger.info(' '.join(spark_command))

        process = ProcessManager(spark_command, pass_through=True, env=env)
        process.start_process()
        process.wait()

        if process.returncode != 0:
            raise RuntimeError(f'Interactive Spark failed with error: {process.returncode}')


from django.urls import path
from .views import (TourListView, TourDetailView,
                    CategoryDetailView, TypesDetailView,
                    CreateTourViews, EntryTourViews,
                    CreateCommentView, DeleteCommentView, LikeCreateView, LikeDeleteView)

urlpatterns = [
    path('create/', CreateTourViews.as_view(), name='tour-create'),
    path('all/', TourListView.as_view(), name='tour-list'),
    path('detail/<int:pk>/', TourDetailView.as_view(), name='tour-detail'),

    path('category/<int:pk>/', CategoryDetailView.as_view(), name='tour-category'),
    path('types/<int:pk>/', TypesDetailView.as_view(), name='tour-types'),
    path('entry/', EntryTourViews.as_view(), name='tour-entry'),

    path('comment/', CreateCommentView.as_view(), name='tour-comments'),
    path('delete_comment/<int:pk>/', DeleteCommentView.as_view(), name='comments-delete'),
    path('like/', LikeCreateView.as_view(), name='like-create'),
    path('like_delete/<int:pk>/', LikeDeleteView.as_view(), name='like-delete'),

]


import pyautogui
import sys, os, time
from PIL import Image

class crafter:
    def __init__(self):
        print("INITIALIZED")
        self.log_im_region = [0,0,884,625]
        #PUT STUFF HERE LATER

    def set_screen(self, icon_loc):
        #find the icon location
        locs = pyautogui.locateOnScreen(icon_loc)
        if locs is not None:
            x = locs[0]
            y = locs[1]
            #move mouse in positions
            pyautogui.moveTo(x+30,y+10)
            #drag screen to corner
            pyautogui.mouseDown()
            pyautogui.moveTo(31,11,duration=.5)
            pyautogui.mouseUp()
        else:
            print("NOT FOUND")

    def slice_Log(self):
        #get crafting image screenshot
        log_loc = pyautogui.locateOnScreen(".\\ff14_crafting_icon.png")
        self.log_im_region[0]=log_loc[0]
        self.log_im_region[1]=log_loc[1]
        self.log_im = pyautogui.screenshot(region=self.log_im_region)
        #get width and height
        w, h = self.log_im.size
        '''for a in range(0,w,20):
            for b in range(h):
                self.log_im.putpixel((a,b),(255,0,0,))'''
        self.log_im.save(".\\sliced_Log.png")
        #SLICE UP THE CRAFTING LOG INTO USEFUL IMAGES

    def craft_loop(self):
        icon_loc = pyautogui.locateOnScreen("dbmg.png")
        pyautogui.moveTo(icon_loc[0]+15,icon_loc[1]+15)
        time.sleep(.5)
        pyautogui.click()
        pyautogui.click()
        time.sleep(.5)
        pyautogui.mouseUp()
        icon_loc = pyautogui.locateOnScreen("pp.png")
        pyautogui.moveTo(icon_loc[0]+40,icon_loc[1]+20,duration=1)
        time.sleep(.5)
        pyautogui.click()
        pyautogui.click()
        time.sleep(1)
        pyautogui.moveTo(1,1,duration=.25)
        pyautogui.mouseUp()
        table_loc = pyautogui.locateOnScreen("Capture.png")
        pyautogui.moveTo(table_loc[0]+20,table_loc[1]+20)
        time.sleep(.5)
        pyautogui.click()
        pyautogui.click()
        time.sleep(.5)
        pyautogui.mouseUp()
        synth_loc = pyautogui.locateOnScreen("synth.png")
        pyautogui.moveTo(synth_loc[0]+30,synth_loc[1]+10)
        time.sleep(.5)
        pyautogui.click()
        pyautogui.click()
        time.sleep(2)
        #craft loop
        log_up = pyautogui.locateOnScreen("ff14_crafting_icon.png")
        while log_up is None:
            pyautogui.press("1")
            time.sleep(3)
            log_up = pyautogui.locateOnScreen("ff14_crafting_icon.png")
        time.sleep(10)

def main():
    c = crafter()
    c.set_screen("FF14_top_left_icon.png")
    c.slice_Log()
    while True:
        c.craft_loop()

main()


"""
Day 3: Rucksack Reorganization
"""

def get_data(filename: str) -> list:
    with open(filename) as f:
        return [list(line) for line in f.read().split("\n")]

def format_data(data: list, type: int) -> list:
    if type == 1:
        return [[line[: len(line) // 2], line[len(line) // 2 :]] for line in data]
    elif type == 2:
        return [data[i : i + 3] for i in range(0, len(data), 3)]

def find_duplicate_items(group: list) -> list:
    if not group:
        return []
    elif len(group) == 1:
        return group[0]
    duplicate_items = set(filter(lambda item: item in group[1], group[0]))
    return find_duplicate_items([list(duplicate_items), *group[2:]])

def convert_letter(letter: str) -> int:
    return ord(letter) - 96 if letter.lower() == letter else ord(letter) - 38

def get_priority_sum(data: list) -> int:
    priority_sum = 0
    for group in data:
        duplicate_items = find_duplicate_items(group)
        priority_sum += sum(convert_letter(item) for item in duplicate_items)
    return priority_sum

def main(filename: str) -> tuple:
    data = get_data(filename)

    data1 = format_data(data, 1)
    data2 = format_data(data, 2)

    return get_priority_sum(data1), get_priority_sum(data2)

if __name__ == "__main__":
    print(main("day03/test-data.txt"))  # (157, 70)
    print(main("day03/data.txt"))  # (7795, 2703)


from Data_Output import Data_Output
from Html_Downloader import Html_Downloader
from Html_Parser import Html_Parser
from URL_Manager import URL_Manager

class SpiderMan(object):
    def __init__(self):
        self.mamager = URL_Manager()
        self.downloader = Html_Downloader()
        self.parser = Html_Parser()
        self.output = Data_Output()

    def crawl(self, root_url):
        # 添加入口URL
        self.mamager.add_new_url(root_url)
        # 判断url管理器中是否有新的url, 同时判断抓取了多少个url
        while (self.mamager.has_new_url() and self.mamager.old_url_size()<100):
            try:
                # 从URL管理器获取新的url
                new_url = self.mamager.get_new_url()
                html = self.downloader.download(new_url)
                # 从html解析器抽取网页数据
                new_url, data = self.parser.parser(new_url, html)
                # 将抽取的url 添加到URL管理器中
                # print(new_url, data)
                self.mamager.add_new_urls(new_url)
                # 数据存储器存储文件
                # print('***************8')
                self.output.store_data(data)

                print(f"已经抓取{self.mamager.old_url_size()}个链接")
            except Exception as e:
                print(e)
        self.output.output_html()

if __name__ == '__main__':
    spider_man = SpiderMan()
    spider_man.crawl("http://baike.baidu.com/view/284853.htm")


# Insertion Sort
from operator import ge, gt, lt, add, sub

n = int(input())  # size of input array
a = list(map(int, input().strip().split(' ')))[:n]  # input array
asc = input("Ascending? Y/N")

for i in range(1, n):
    key = a[i]  # card to be inserted
    j = sub(i, 1)
    while ge(j, 0):
        if (asc in ['Y', 'y'] and gt(a[j], key)) or (asc in ['N', 'n'] and lt(a[j], key)):
            # keep shifting previous cards right till the right place for the key is found
            a[add(j, 1)] = a[j]
        j = sub(j, 1)
    a[add(j, 1)] = key  # place card at the right of the card reached after iteration

print(a)


from unittest import TestCase
from problems.problem_1.solution.solution_problem_1 import get_square

class ProblemOneTests(TestCase):
    def test_x(self):
        coords = [(1, 2), (3, 4), (6, 5), (5, 3)]
        result = get_square(coords)
        expected = [(1, 5), (1, 2), (6, 2), (6, 5)]
        self.assertCountEqual(expected, result)


import os
from werkzeug.wrappers import Request, Response
import json
import requests

class AuthenticationMiddleware:
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        request = Request(environ)
        secret = request.headers.get('ADMIN_AUTH', '')

        if secret == os.environ.get('ADMIN_AUTH_KEY'):
            environ['is_admin'] = True
            return self.app(environ, start_response)

        token = request.headers.get("Authorization", "")

        if not token:
            error = {"message": "Missing or Invalid token"}
            response = Response(json.dumps(error), mimetype='application/json', status=401)
            return response(environ, start_response)
        try:
            response = requests.post(
                f"http://{os.environ.get('AUTH_HOSTNAME','localhost')}:5000/auth/validate",
                headers={'Authorization': token}
            )
        except ConnectionError as e:
            error = {"message": e}
            response = Response(json.dumps(error), mimetype='application/json', status=401)
            return response(environ, start_response)

        if response.status_code != 200:
            response = Response(response.text, mimetype='application/json', status=401)
            return response(environ, start_response)

        user_id = json.loads(response.text).get('user_id')
        environ['user_id'] = user_id

        return self.app(environ, start_response)


from functools import lru_cache
import csv

@lru_cache
def read(path):
    with open(path, encoding="utf-8") as file:
        jobs_reader = csv.DictReader(file)
        jobs_info = []
        for info in jobs_reader:
            jobs_info.append(info)

        # print("info", jobs_info[1])
    return jobs_info


import os
import openai
from flask import Flask, render_template, request
app = Flask(__name__)
openai.api_key = 'YOUR-API-KEY'

prompt = 'The following is a conversation with Leonardo. Leonardo is a bot acting like the painter and inventor Leonardo Da Vinci.\nYou: Can you paint?\nLeonardo: Yes I can actually paint very well, I painted the famous Mona Lisa\nYou: When were you born?\nLeonardo: I was born on 15 April 1452\nYou: When did you die?\nLeonardo: I died on the 2nd of May 1519, I was 67 years old\n'

def get_response(q):
    global prompt
    prompt=prompt+'You: '+q+'\n'
    response = openai.Completion.create(
    engine="davinci",
    prompt=prompt,
    temperature=0.9,
    max_tokens=150,
    top_p=1,
    frequency_penalty=0.0,
    presence_penalty=0.6,
    stop=["\n", " You:", " Leonardo:"],
    )
    return response['choices'][0]['text']
@app.route("/")
def index():
    return render_template("index.html")

@app.route("/get")
#function for the bot response
def get_bot_response():
    global prompt
    userText = request.args.get('msg')
    response = get_response(userText)
    prompt = prompt + response
    return str(response)

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


import os
import cv2
import time

def resize_images(images, new_size):
    return [cv2.resize(im, new_size) for im in images]

def read_directory_images(path, extension, n=None):
    """
    Read images from a directory based on file extension
    :param path: Path to the directory to read from
    :param extension: The image extension i.e '.png', '.jpg', etc.
    :param n: Number of files to read
    :return: Set of images from the directory
    """

    # Set n to the total number of files in the directory if no value is given
    n = n if n else len(os.listdir(path))

    for filename in os.listdir(path)[:n]:
        if filename.endswith(extension):
            yield cv2.imread(os.path.join(path, filename))

def validate_image_directory(path, extension):
    """ Clean up empty images in a directory. Happens surprisingly often when importing a dataset """
    for filename in os.listdir(path):
        if filename.endswith(extension):
            file_path = os.path.join(path, filename)
            if cv2.imread(file_path) is None:
                os.unlink(file_path)

def cutoff_lower(image, percent):
    """ Remove the lower part of an image """
    y = image.shape[0]
    cutoff = int(y - (percent * y))
    return image[:cutoff, :]

def sliding_window(image, win_size, step_size):
    """ Run a sliding window across an image and extract each window """
    for y in range(0, image.shape[0] - (win_size[1] - step_size[1]), step_size[1]):
        for x in range(0,  image.shape[1] - (win_size[0] - step_size[0]), step_size[0]):
            window = image[y: y+win_size[1], x:x + win_size[0]]
            yield x, y, window

def inbounds(image, x, y):
    return 0 <= x < image.shape[1] and 0 <= y < image.shape[0]

def extract_window(image, center, size):
    """ Extract a sub-matrix from an image around a given point. Center given as (x, y) coordinate """

    x_offset = int((size[0] - 1) / 2)
    y_offset = int((size[1] - 1) / 2)
    (x, y) = center

    min_x, max_x = x - x_offset,  x + x_offset + 1
    min_y, max_y = y - y_offset, y + y_offset + 1

    # Even-sized windows pad one pixel to the right and one below
    if size[0] % 2 == 0:
        max_x += 1
        max_y += 1

    # If it's out of bounds don't return a window
    if not (inbounds(image, min_x, min_y) and inbounds(image, max_x, max_y)):
        return None

    return image[min_y:max_y, min_x:max_x]

def timeit(func):
    """ Decorator to time how long functions take to run """
    def timed(*args, **kwargs):
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()
        print(func.__name__ + " Run time: {:2.2f} sec".format(end_time - start_time))
        return result

    return timed

def draw_boxes(image, coordinates, win_size):
    """
    Draw a list of boxes onto an image
    :param image: Image to draw on
    :param coordinates: List of (x, y) center coordinates
    :param win_size: How large the boxes should be
    :return:
    """
    x_offset = int((win_size[0] - 1) / 2)
    y_offset = int((win_size[1] - 1) / 2)

    for (x, y) in coordinates:
        if inbounds(image, x - x_offset, y - y_offset) and inbounds(image, x + x_offset, y + y_offset):
            cv2.rectangle(image, (x - x_offset, y - y_offset), (x + x_offset, y + y_offset), (0, 255, 0), 2)

def unpack_box(box):
    x_min = int(round(box['x_min']))
    x_max = int(round(box['x_max']))
    y_min = int(round(box['y_min']))
    y_max = int(round(box['y_max']))
    return x_min, x_max, y_min, y_max


# -*- coding:utf-8 -*-
import tkinter as tk
from tkinter import messagebox

# 调用Tk()创建主窗口
top =tk.Tk()

top.geometry('450x400+300+200')

# 给主窗口起一个名字，也就是窗口的名字
top.title('赵赛赛')

# 设置回调函数
def callback():
    messagebox.showinfo('你输入的是', dstr.get())

# 生成动态字符串
dstr = tk.StringVar()

# 利用 textvariable 来实现文本变化
lb = tk.Entry(top,textvariable=dstr)

# 将按钮放置在主窗口内
lb.pack()

lb.insert(0,'这个是默认值')

b=tk.Button(top, text="登录", command=callback)
b.pack()

#开启主循环，让窗口处于显示状态
top.mainloop()


""" this is the collection of functions used to map a genome and find the ORI of the genome
"""

if __name__ == '__main__':
    import matplotlib.pyplot as plt
    import seaborn as sns
    import numpy as np
    from main import read_fasta

    with open('e_coli-strain_973250-genome.fasta') as fp:
        name, seq = next(read_fasta(fp))
        seq = seq.replace('\n', '')

def skew(genome):
    """ the skew plot takes adavntage of the fact that DNA polymerase only replicates in the 3' to 5' (reverse half strand)
     direction.Therefore, Okazaki fragments are created in the 5' to 3' direction and the single stranded DNA needs to
    'hangout' for a period of time making it more susceptible to mutation. the nucleotide that is extremely susceptible
    is Cytosine. Thus on the forward half strand, there is a decrease in the amount of Cytosine as it mutates into Thymine.
    The repair mechanisms compound this by repairing Guanine into Adenine on the reverse half strand.

    In conclusion this implies that there will be a high C:G ratio in the reverse half strand and a low C:G ratio in the
    forward half strand. So if we walk along the DNA strand keeping track of this C:G ratio and we see a change from
    low to high then we know the replication origin is at that location"""
    c_count = 0
    g_count = 0
    skw = []
    genome = genome.lower()
    for i in genome:
        if i == 'c':
            c_count += 1
        elif i == 'g':
            g_count += 1
        skw.append(c_count - g_count)
    return skw, skw.index(min(skw))

# Generate the line plot for the skew
""" note this can take a few minutes to generate
for e coli the image is saved as a .png
Note the V shape of the graph with a minimum as ~3e6 base pairs
This is our hypothesized origin of replication"""

# print('calcuating skew')
# skw = skew(seq)
# print('skew calculation completed')
# print('generating line plot object')
# sns.lineplot(x=np.arange(len(seq)), y=skw)
# print('rendering object')
# plt.show()


import time
from logging import exception

from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from MakeMyTrip import Utilites
from MakeMyTrip.AllElementsPath import Test_AllElementsPath
from MakeMyTrip.HomePageObjects import Test_HomePage

class Test_Activites(Test_AllElementsPath):
    global Logs
    global MoreActivitesText
    Logs = Utilites.getlogs()
    MoreActivitesText = []

    def __init__(self, driver):
        self.driver = driver

    def Activites(self):
        try:
            global TestHomePageObj

            TestHomePageObj = Test_HomePage(self.driver)
            MMTLogoLink = TestHomePageObj.MMTLogoClick()
            Utilites.webelementclick(self.driver, MMTLogoLink)
            time.sleep(3)
            Logs.info("::::::::::::::Verifying Activites tab::::::::::::::::::::::::::")
            Activitylink = TestHomePageObj.ActivitiesTab()
            Utilites.webelementclick(self.driver, Activitylink)
            ActivityMoreLink = TestHomePageObj.ActivitiesMore()
            try:
                assert ActivityMoreLink
                Logs.info("Activity More link exist")
                a = ActionChains(self.driver)
                a.move_to_element(ActivityMoreLink).perform()
                MoreActivitesWebElementList = TestHomePageObj.ActivitesList()
                for ActivitesText in MoreActivitesWebElementList:
                    MoreActivitesText.append(ActivitesText.text)
                LISTOFALLACTIVITES = self.ListOfActivites()
                j = 0
                for MAT in MoreActivitesText:
                    try:
                        assert MAT ==  LISTOFALLACTIVITES[j]
                        Logs.info(str(MAT) + " Exist in drop down")
                    except Exception as e:
                        Logs.error(str(MAT) + " Not exist in drop down")
                        raise Exception(e)
                    j = j+1

            except AssertionError:
                Logs.error("Activity link doesn't exist")

        except Exception as e:
            raise Exception(e)

    def ListOfActivites(self):
        listofact = ["Charter Flights", "Activities", "Trip Ideas", "Giftcards", "Nearby Getaways", "Trip Money"]
        return listofact