import pyglet
import sys
import random
from drawable import Drawable
from dungeon_template import DungeonTemplate
from chest import Chest
from enemy import Enemy
from mage import Mage
from pyglet.window import key

class DungeonTile(Drawable):

  TOP_LEFT = 0
  TOP_RIGHT = 1
  BOTTOM_RIGHT = 2
  BOTTOM_LEFT = 3
  FLOOR = 4
  OUTER_TOP_LEFT = 5
  OUTER_TOP_RIGHT = 6
  OUTER_BOTTOM_RIGHT = 7
  OUTER_BOTTOM_LEFT = 8
  TOP = 9
  BOTTOM = 10
  LEFT = 11
  RIGHT = 12
  EMPTY = 13
  CENTER_BOTTOM = 14
  CENTER = 15
  TOP_LEFT_FILL = 16
  TOP_RIGHT_FILL = 17
  BOTTOM_RIGHT_FILL = 18
  BOTTOM_LEFT_FILL = 19

  TYPE_EMPTY = 0
  TYPE_FLOOR = 1
  TYPE_WALL = 2
  TYPE_CHEST = 3
  TYPE_CHEST_STARTER = 4
  TYPE_EXIT = 5

  def __init__(self, game, tx, ty, group, type):
    super().__init__(game, tx, ty)

    self.hitboxes = set()
    self.type = type
    self.group = group
    self.target_state = self.game.world.DUNGEON
    self.group_floor = pyglet.graphics.OrderedGroup(group.order-1)
    self.width = self.game.TILE_WIDTH
    self.height = self.game.TILE_HEIGHT
    self.will_spawn_chest = False
    self.will_spawn_entity = False
    self.chest = None
    self.entities = []
    self.spawn_exit = False
    self.can_spawn_entity = True
    self.enemies = [Enemy, Mage]

    if type == self.TYPE_FLOOR:
      self.will_spawn_entity = random.random() < self.game.world.character.level * 0.1

    if type == self.TYPE_CHEST or type == self.TYPE_CHEST_STARTER:
      self.chest_type = 0 if type == self.TYPE_CHEST_STARTER else 1
      self.will_spawn_chest = random.random() < 0.8 or type == self.TYPE_CHEST_STARTER
      self.type = self.TYPE_FLOOR

    if type == self.TYPE_EXIT:
      self.spawn_exit = True
      self.type = self.TYPE_FLOOR

  def tile_at(self, dx, dy, type=2):
    tile = self.game.world.tile_at(self.tx+dx, self.ty+dy, self.game.world.DUNGEON)
    return (tile and tile.type == type) or (type == self.TYPE_EMPTY and not tile)

  def tile_sprite(self, type):
    return self.init_sprite('tile_stone_'+str(type)+'.png', self.group_floor if type == self.FLOOR else self.group)

  def draw(self):
    if self.chest:
      self.chest.draw()

    for entity in self.entities:
      entity.draw()

    super().draw()

  def update(self, dt):
    Drawable.handle_deletion(self.entities)

    if self.chest:
      self.chest.update(dt)

    offset = 0
    if self.spawn_exit:
      offset = self.game.dungeon_offset / self.game.TILE_WIDTH

    for entity in self.entities:
      entity.update(dt)

    super().update(dt)

    if self.spawn_exit and self.game.world.state_dt > 4 and self.game.world.progression == self.game.world.FREE_ROAM and abs(self.game.world.character.tx - offset - self.tx) < 0.6 and abs(self.game.world.character.ty - offset - self.ty - 0.5) < 0.6:

      if len(self.game.world.textbox.text) == 0 or self.game.keys[key.E]:
        self.game.world.textbox.text = ["Press E to exit the crevice."]
        self.game.world.textbox.faces = [None]

        if self.game.keys[key.E]:
          self.game.world.move_to = self.game.world.OVERWORLD

  def delete(self):
    if self.chest:
      self.chest.delete()

    for entity in self.entities:
      entity.delete()

    super().delete()

  def init_tile(self):
    dirs = {
      'N':  (0,1),
      'NW': (-1,1),
      'NE': (1,1),
      'W':  (-1,0),
      'E':  (1,0),
      'S':  (0,-1),
      'SE': (1,-1),
      'SW': (-1,-1)
    }

    floors = {key: self.tile_at(*dirs[key], self.TYPE_FLOOR) for key in dirs.keys()}
    walls = {key: self.tile_at(*dirs[key]) for key in dirs.keys()}
    surround_count = sum([int(walls[k]) for k in ['N','E','S','W']])
    offset = self.game.dungeon_offset / self.game.TILE_WIDTH

    if self.will_spawn_entity and self.can_spawn_entity:
      enemy_type = self.enemies[0] if random.random() >= self.game.world.character.level * 0.08 - 0.05 else self.enemies[1]
      self.entities.append(enemy_type(self.game, self.tx + offset, self.ty + offset, self.group))

    if self.will_spawn_chest:

      if (surround_count == 3 and not walls['E']) or (walls['N'] and walls['W'] and surround_count == 2 and walls['SW']):
        ori = Chest.LEFT
        cx, cy = Chest.OFFSET[ori]
        self.hitboxes.add((cx+2, cy, cx+8, cy+21))
      elif surround_count == 3 and not walls['W'] or (walls['N'] and walls['E'] and surround_count == 2 and walls['NE']):
        ori = Chest.RIGHT
        cx, cy = Chest.OFFSET[ori]
        self.hitboxes.add((cx, cy, cx+6, cy+21))
      elif surround_count == 3 and not walls['N']:
        ori = Chest.BOTTOM
        cx, cy = Chest.OFFSET[ori]
        self.hitboxes.add((cx, cy, cx+17, cy+7))
      elif walls['N'] or (floors['N'] and floors['S'] and floors['E'] and floors['W']):
        ori = Chest.TOP
        cx, cy = Chest.OFFSET[ori]
        self.hitboxes.add((cx, cy, cx+17, cy+10))
      elif walls['W']:
        ori = Chest.LEFT
        cx, cy = Chest.OFFSET[ori]
        self.hitboxes.add((cx+2, cy, cx+8, cy+21))
      elif walls['E']:
        ori = Chest.RIGHT
        cx, cy = Chest.OFFSET[ori]
        self.hitboxes.add((cx, cy, cx+6, cy+21))
      else:
        ori = Chest.BOTTOM
        cx, cy = Chest.OFFSET[ori]
        self.hitboxes.add((cx, cy, cx+17, cy+7))

      self.chest = Chest(self.game, self.tx + offset, self.ty + offset, self.group, ori, self.chest_type)

    if self.type == self.TYPE_WALL:
      empty = {key: self.tile_at(*dirs[key], self.TYPE_EMPTY) for key in dirs.keys()}

      corners = []

      if walls['E'] and walls['S']:
        if empty['SE']:
          self.hitboxes.add((self.width-12, 0, self.width, 17))
          corners.append(self.OUTER_TOP_LEFT)
        elif walls['SE']:
          self.hitboxes.add((0,0,self.width,self.height))
          corners.append(self.TOP_LEFT_FILL)
        else:
          self.hitboxes.add((0,0,12,self.height))
          self.hitboxes.add((0,self.height-17,self.width,self.height))
          corners.append(self.TOP_LEFT)
      elif walls['W'] and walls['S']:
        if empty['SW']:
          self.hitboxes.add((0, 0, 12, 17))
          corners.append(self.OUTER_TOP_RIGHT)
        elif walls['SW']:
          self.hitboxes.add((0,0,self.width,self.height))
          corners.append(self.TOP_RIGHT_FILL)
        else:
          self.hitboxes.add((self.width-12,0,self.width,self.height))
          self.hitboxes.add((0,self.height-17,self.width,self.height))
          corners.append(self.TOP_RIGHT)
      elif walls['W'] and walls['N']:
        if empty['NW']:
          self.hitboxes.add((0, self.height-16, 12, self.height))
          corners.append(self.OUTER_BOTTOM_RIGHT)
        elif walls['NW']:
          self.hitboxes.add((0,0,self.width,self.height))
          corners.append(self.BOTTOM_RIGHT_FILL)
        else:
          self.hitboxes.add((self.width-12,0,self.width,self.height))
          self.hitboxes.add((0,0,self.width,17))
          corners.append(self.BOTTOM_RIGHT)
      elif walls['E'] and walls['N']:
        if empty['NE']:
          self.hitboxes.add((self.width-12, self.height-16, self.width, self.height))
          corners.append(self.OUTER_BOTTOM_LEFT)
        elif walls['NE']:
          self.hitboxes.add((0,0,self.width,self.height))
          corners.append(self.BOTTOM_LEFT_FILL)
        else:
          self.hitboxes.add((0,0,12,self.height))
          self.hitboxes.add((0,0,self.width,17))
          corners.append(self.BOTTOM_LEFT)

      if surround_count == 1 and floors['S']:
        self.hitboxes.add((0,0,self.width,self.height))
        return self.tile_sprite(self.CENTER_BOTTOM)
      if surround_count <= 2 and floors['S'] and floors['N']:
        self.hitboxes.add((0,0,self.width,self.height))
        return self.tile_sprite(self.CENTER_BOTTOM)

      if len(corners) > 0:
        if surround_count >= 3:
          if not walls['S'] or (not walls['SE'] and not walls['SW']):
            self.hitboxes.add((0,0,self.width,self.height))
            return self.tile_sprite(self.CENTER_BOTTOM)
          else:
            self.hitboxes.add((0,0,self.width,self.height))
            return self.tile_sprite(self.CENTER)

        [self.tile_sprite(corner) for corner in corners]

      elif walls['N'] and walls['S']:
        if not floors['E']:
          self.hitboxes.add((self.width-12,0,self.width,self.height))
          self.tile_sprite(self.RIGHT)
        elif not floors['W']:
          self.hitboxes.add((0,0,12,self.height))
          self.tile_sprite(self.LEFT)
      elif walls['E'] and walls['W']:
        if not floors['N']:
          self.hitboxes.add((0, self.height-17, self.width, self.height))
          self.tile_sprite(self.TOP)
        elif not floors['S']:
          self.hitboxes.add((0, 0, self.width, 17))
          self.tile_sprite(self.BOTTOM)

    if self.type != self.TYPE_EMPTY:
      self.tile_sprite(self.FLOOR)

      if self.spawn_exit:
        self.tile_sprite("exit")


import requests

def fetch_weather_data():
    url = "https://api.open-meteo.com/v1/forecast"
    params = {
        'latitude': 21.028511,
        'longitude': 105.804817,
        'hourly': 'temperature_2m,windspeed_10m,visibility',
        'daily': 'precipitation_sum'# Include windspeed_10m
          # This is to specify the unit of the windspeed
    }
    response = requests.get(url, params=params)
    if response.status_code == 200:
        data = response.json()
        temperature = data['hourly'].get('temperature_2m', [None])[0]
        wind_speed = data['hourly'].get('windspeed_10m', [None])[0]
        rain = data['daily'].get('precipitation_sum', [None])[0]
        visabil = data['hourly'].get('visibility', [None])[0]
        return {
            'latitude': params['latitude'],
            'longitude': params['longitude'],
            'temperature': temperature,
            'windspeed': wind_speed,
            'rainfall': rain,
            'visibility': visabil
        }
    else:
        print(f"Failed to get data: {response.content}")
        return None

import sqlite3

def setup_database():
    conn = sqlite3.connect("weather_data.db")
    cursor = conn.cursor()
    cursor.execute("""
        CREATE TABLE IF NOT EXISTS forecasts (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            latitude REAL,
            longitude REAL,
            temperature REAL,
            windspeed REAL,
            rainfall REAL,
            visibility REAL
        );
    """)
    conn.commit()
    conn.close()

def insert_into_database(data):
    conn = sqlite3.connect("weather_data.db")
    cursor = conn.cursor()
    cursor.execute("INSERT INTO forecasts (latitude, longitude, temperature, windspeed, rainfall, visibility) VALUES (?, ?, ?, ?, ?, ?)",
                   (data['latitude'], data['longitude'], data['temperature'], data['windspeed'], data['rainfall'], data['visibility']))
    conn.commit()
    conn.close()

def fetch_from_database():
    conn = sqlite3.connect("weather_data.db")
    cursor = conn.cursor()
    cursor.execute("SELECT * FROM forecasts")
    rows = cursor.fetchall()
    conn.close()
    return rows

if __name__ == "__main__":
    setup_database()
    data = fetch_weather_data()
    if data:
        insert_into_database(data)
    # Fetch and print all rows from the database to confirm the data was inserted
    print(fetch_from_database())

rows2 = fetch_from_database()

for row in rows2:
    print(f"ID: {row[0]}, Latitude: {row[1]}, Longitude: {row[2]}, Temperature: {row[3]}, Windspeed: {row[4]}, Rainfall: {row[5]}, Visability: {row[6]}")


import os
import pathlib
import shutil
import subprocess
import tempfile

from bang.UnpackParser import UnpackParser, check_condition
from bang.UnpackParserException import UnpackParserException
from kaitaistruct import ValidationFailedError
from . import lrzip

class LrzipUnpackParser(UnpackParser):
    extensions = []
    signatures = [
        (0, b'LRZI')
    ]
    pretty_name = 'lrzip'

    def parse(self):
        if shutil.which('lrzip') is None:
            raise UnpackParserException("lrzip not installed")

        try:
            self.data = lrzip.Lrzip.from_io(self.infile)
            self.unpacked_size = self.infile.tell()

            # force read blocks to trigger evaluation by kaitai struct
            for rchunk in self.data.rchunks:
                # first process all blocks in stream 0
                cur_stream_header = rchunk.stream_0
                next_block_head = 0
                while cur_stream_header is not None:
                    self.unpacked_size = max(self.unpacked_size, cur_stream_header.start_position + cur_stream_header.len_data + cur_stream_header.size + next_block_head)
                    next_block_head = cur_stream_header.next_block_head
                    cur_stream_header = cur_stream_header.next

                # then process all blocks in stream 1
                cur_stream_header = rchunk.stream_1
                next_block_head = 0
                while cur_stream_header is not None:
                    self.unpacked_size = max(self.unpacked_size, cur_stream_header.start_position + cur_stream_header.len_data + cur_stream_header.size + next_block_head)
                    next_block_head = cur_stream_header.next_block_head
                    cur_stream_header = cur_stream_header.next

            # finally check if there is an md5 sum (16 bytes)
            if self.data.header.has_md5:
                self.unpacked_size += 16

        except (Exception, ValidationFailedError) as e:
            raise UnpackParserException(e.args)

        check_condition(self.unpacked_size <= self.infile.size, "data cannot be outside of file")

        # test unpacking here. lrzip expects the input file to be a
        # single lrzip archive and it will fail if there is data either
        # in front of the lrzip data or is trailing it. If the
        # same lrzip archive is concatened it will work (but will only
        # extract the file once).
        self.havetmpfile = False
        if not (self.offset == 0 and self.infile.size == self.unpacked_size):
            temporary_file = tempfile.mkstemp(dir=self.configuration.temporary_directory)
            havetmpfile = True
            os.sendfile(temporary_file[0], self.infile.fileno(), self.offset, self.unpacked_size)
            os.fdopen(temporary_file[0]).close()

        if self.havetmpfile:
            p = subprocess.Popen(['lrzip', '-d', temporary_file[1], '-o', '-'], stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
        else:
            p = subprocess.Popen(['lrzip', '-d', self.infile.name, '-o', '-'], stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)

        (outputmsg, errormsg) = p.communicate()

        if p.returncode != 0:
            if self.havetmpfile:
                os.unlink(self.temporary_file[1])
            raise UnpackParserException("lrzip failed unpacking")

    # make sure that self.unpacked_size is not overwritten
    def calculate_unpacked_size(self):
        pass

    def unpack(self, meta_directory):
        # determine the name of the output file
        if meta_directory.file_path.suffix.lower() == '.lrz':
            file_path = pathlib.Path(meta_directory.file_path.stem)
            if file_path in ['.', '..']:
                file_path = pathlib.Path("unpacked_from_lrzip")
        else:
            file_path = pathlib.Path('unpacked_from_lrzip')

        with meta_directory.unpack_regular_file(file_path) as (unpacked_md, outfile):
            if self.havetmpfile:
                p = subprocess.Popen(['lrzip', '-d', temporary_file[1], '-o', '-'], stdin=subprocess.PIPE, stdout=outfile, stderr=subprocess.PIPE)
            else:
                p = subprocess.Popen(['lrzip', '-d', self.infile.name, '-o', '-'], stdin=subprocess.PIPE, stdout=outfile, stderr=subprocess.PIPE)

            (outputmsg, errormsg) = p.communicate()

            if self.havetmpfile:
                os.unlink(self.temporary_file[1])

            yield unpacked_md

    labels = ['lrzip', 'compressed']
    metadata = {}


from django.utils import timezone
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models

class Game(models.Model):
    name = models.CharField(db_index=True, max_length=100)
    metacritic = models.URLField()
    gametrailersReviewURL = models.URLField()
    gametrailersVideo = models.URLField()
    description = models.TextField()
    released = models.DateTimeField(db_index=True, default=timezone.now())
    lastUpdated = models.DateTimeField(default=timezone.now())
    def __unicode__(self):
      return '[Game: name=' + self.name + ']'

class Rating(models.Model):
    username = models.CharField(db_index=True, max_length=100)
    game = models.ForeignKey(Game)
    tag = models.CharField(db_index=True, max_length=100)
    value = models.FloatField(db_index=True, validators = [MinValueValidator(0), MaxValueValidator(100)],default=50)
    time = models.DateTimeField(default=timezone.now())
    def __unicode__(self):
      return '[Rating: tag=' + self.tag + ' value=' + str(value) + ']'


from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import sys
import os
import cv2

def report(name):
	img = Image.open(name)
	imgn = np.asarray(img,dtype='float')
	imgn = imgn.reshape(imgn.shape[0]*imgn.shape[1],-1)
	print(imgn.shape)
	mu, std = np.mean(imgn,0), np.std(imgn,0)
	print(f'\nmean: {mu}, std: {std}\n')
	return mu,std

def give_some_new_values(name, mu, extstd):
	img = Image.open(name)
	imgn = np.asarray(img,dtype='float')
	s=imgn.shape
	if s[0]*s[1]>3e6:
		print('size was...'+str(s))
		raise Exception('too big!')
	print(s)
	imgn = imgn.reshape(s[0]*s[1],-1)
	if True:
		return np.mean(imgn.reshape(s[0],s[1],-1),2).reshape(s[0],s[1],1)
	vn=(np.sqrt(sum(mu**2)))**2
	#print('VN IS ',vn)
	#c = np.matmul(np.matmul(imgn,mu).reshape(-1,1), mu.reshape(1,3))/vn
	print(np.matmul(imgn,mu).reshape(-1,1).shape,'MATMUL WAS')
	#plt.hist(np.matmul(imgn,mu).flatten());plt.show()
	c = np.matmul(np.matmul(imgn,mu).reshape(-1,1), mu.reshape(1,3))/vn
	c=c.reshape(*s)
#	for i in range(len(c.shape[0])):
#		for j in range(len(c.shape[1])):
#			if c[i,j,0]
#	c = np.matmul(np.matmul(imgn,mu).reshape(-1,1), mu.reshape(1,3))/vn
#	c=c.reshape(*s)

	if False:
		plt.hist(c[:,:,0].flatten(),label='one',alpha=0.5)
		plt.hist(c[:,:,1].flatten(),label='two',alpha=0.5)
		plt.hist(c[:,:,2].flatten(),label='three',alpha=0.5)
		plt.legend()
	elif False:
		index = range(len(c[:,:,0].flatten()))
		ix = np.random.choice(index,10000)
		if False:
			fig = plt.figure()
			ax = fig.add_subplot(111, projection='3d')
			ax.scatter(c[:,:,0].flatten()[ix],c[:,:,1].flatten()[ix],c[:,:,2].flatten()[ix])
			ax.set_xlabel('r')
			ax.set_ylabel('g')
		else:
			plt.scatter(c[:,:,1].flatten()[ix],c[:,:,2].flatten()[ix])

	elif False:
		f, ax = plt.subplots(1,3,figsize=(20,7))
		for i in range(3): ax[i].imshow(c[:,:,i], cmap='gray')
	elif False:
		plt.imshow(np.abs(c).astype('uint8'))
	plt.show()
	#c = np.where(c.flatten()<0,0,c.flatten()).reshape(c.shape).astype('int')
	return c.astype('uint8')

#mu1,std1 = report(sys.argv[1])

mu1 = np.asarray([181.87769166, 173.34503561, 163.64049016])
std1 = [65.35000492, 65.35622675, 63.92069093]

L=0
errors=[]
errors2=[]
try:
	os.mkdir('../news')
except:
	print('dir news already existed')
for d in [x for x in os.listdir() if x[-3:]!='.py' and x[:3]!='NEW']:
	try:
		os.mkdir(f'../news/NEW-{d}')
	except Exception as ins:
		errors.append(ins.args)
	for f in os.listdir(d):
		nm = f'{d}/{f}'
		nm_out = f'../news/NEW-{d}/{f}'
		try:
			newimg = give_some_new_values(nm,mu1,std1)
			#print(newimg.shape, 'after')
			#img = Image.fromarray(newimg, mode='rgb')
			#print('FINALLY:',img.size)
			cv2.imwrite(nm_out, newimg)#cv2.cvtColor(newimg, cv2.COLOR_RGB2BGR))
			#img.save(nm_out)
			#del(newimg)
		except Exception as ins:
			errors2.append(ins.args)
			print(f'{nm} failed!')
			L+=1

print(f'\n\n{L} FAILS\n\n')

print(errors2)


from datetime import datetime
import logging
import logging.config

from fastapi import FastAPI

from app.common.models.address import IPRange
from app.common.models.config import CertificatesScanConfig, SeachQueryId
from app.common.database.search_query import SearchQuery
from app.common.database.db_setup import DBSetup
from app.common.utils.exceptions_logger import catch_exceptions_middleware
from app.common.utils.logger_config import get_logger_config
from app.common.utils.network import SingleSession, SyncSingleSession
from app.parser.ip_scanner import IPScanner
from app.parser.settings import Settings
from app.parser.worker import ScannerQueue

app = FastAPI(title="Parser")
app.middleware("http")(catch_exceptions_middleware)

@app.on_event("startup")
async def startup():
    await DBSetup.init(Settings.get_settings().default_db_path)
    logging.config.dictConfig(get_logger_config())
    await SingleSession.init()
    SyncSingleSession.init()
    ScannerQueue.init()
    ScannerQueue.get_instance().start()
    logging.info("Server %s has started", app.title)

@app.on_event("shutdown")
async def shutdown():
    ScannerQueue.get_instance().stop()
    await SingleSession.close()
    SyncSingleSession.close()

@app.post("/scan")
async def start_scan(config: CertificatesScanConfig):
    config_dict = config.to_json()
    time_created = datetime.now()
    query = SearchQuery(config=config_dict, time_created=time_created)
    query_id = await query.insert()

    ip_range = IPRange(
        start=config.startAddr,
        end=config.endAddr,
        mask=config.mask
    )
    ScannerQueue.get_instance().put_nowait(IPScanner(ip_range, query_id))

    resp = await SingleSession.request(
        "POST",
        f"{Settings.get_settings().get_analyzer_addr()}/user_params",
        json={"config": config_dict, "query_id": query_id, "time_created": str(time_created)}
    )
    if not resp.ok:
        logging.error("Failed to send query config (query_id %d) to analyzer", query_id)

    return SeachQueryId(query_id=query_id).dict()


__author__ = 'Viveris Technologies'
__credits__ = '''Contributors:
 * Adrien THIBAUD <adrien.thibaud@toulouse.viveris.com>
 * Mathias ETTINGER <mathias.ettinger@toulouse.viveris.com>
 * Joaquin MUGUERZA <joaquin.muguerza@toulouse.viveris.com>
'''

import os
import sys
import time
import syslog
import signal
import argparse
import threading
from functools import partial

os.environ['XTABLES_LIBDIR'] = '$XTABLES_LIBDIR:/usr/lib/x86_64-linux-gnu/xtables' # Required for Ubuntu 20.04
import iptc
from apscheduler.schedulers.blocking import BlockingScheduler

import collect_agent

def signal_term_handler(chain, rule, signal, frame):
    chain.delete_rule(rule)
    sys.exit(0)

def monitor(chain, mutex, previous):
    # Refresh the table (allowing to update the stats)
    table = iptc.Table(iptc.Table.FILTER)
    table.refresh()

    # Get the rule (Attention, the rule shall be in first position)
    rule = chain.rules[0]

    # Get the stats
    timestamp = int(time.perf_counter() * 1000)
    bytes_count = rule.get_counters()[1]

    # Get previous stats and update them
    with mutex:
        previous_timestamp, previous_bytes_count = previous
        previous[:] = timestamp, bytes_count

    diff_timestamp = (timestamp - previous_timestamp) / 1000  # in seconds
    rate = (bytes_count - previous_bytes_count) * 8 / diff_timestamp

    # Send the stat to the Collector
    collect_agent.send_stat(collect_agent.now(), rate=rate)

def main(sampling_interval, chain_name, source_ip=None, destination_ip=None,
         protocol=None, in_interface=None, out_interface=None, dport=None, sport=None):
    table = iptc.Table(iptc.Table.FILTER)
    chains = [chain for chain in table.chains if chain.name == chain_name]
    try:
        chain, = chains
    except ValueError:
        message = 'ERROR: {} does not exist in FILTER table'.format(chain_name)
        collect_agent.send_log(syslog.LOG_ERR, message)
        sys.exit(message)

    # Creation of the Rule
    rule = iptc.Rule(chain=chain)
    signal.signal(signal.SIGTERM, partial(signal_term_handler, chain, rule))

    # Add Matchs
    if source_ip is not None:
        rule.src = source_ip
    if destination_ip is not None:
        rule.dst = destination_ip
    if protocol is not None:
        rule.protocol = protocol
    if in_interface is not None:
        rule.in_interface = in_interface
    if out_interface is not None:
        rule.out_interface = out_interface
    if sport is not None:
        match = iptc.Match(rule, protocol)
        match.sport = sport
        rule.add_match(match)
    if dport is not None:
        match = iptc.Match(rule, protocol)
        match.dport = dport
        rule.add_match(match)

    # Add the Target
    rule.create_target('')
    chain.insert_rule(rule)

    collect_agent.send_log(syslog.LOG_DEBUG, "Added iptables rule for monitoring")

    # Save the first stats for computing the rate
    mutex = threading.Lock()
    previous = [int(time.perf_counter() * 1000), rule.get_counters()[1]]

    # Monitoring
    sched = BlockingScheduler()
    sched.add_job(
            monitor, 'interval',
            seconds=sampling_interval,
            args=(chain, mutex, previous))
    sched.start()

if __name__ == '__main__':
    with collect_agent.use_configuration('/opt/openbach/agent/jobs/rate_monitoring/rate_monitoring_rstats_filter.conf'):
        # Define Usage
        parser = argparse.ArgumentParser(
                description=__doc__,
                formatter_class=argparse.ArgumentDefaultsHelpFormatter)
        parser.add_argument(
                'sampling_interval', type=int,
                help='Time interval (in sec) used to calculate rate')
        parser.add_argument(
                'chain_name', choices=['INPUT','OUTPUT','FORWARD'],
                help='The iptables chain to monitor')
        parser.add_argument(
                '-s', '--source-ip',
                help='The source IPs to monitor (with or without mask[ip/mask])')
        parser.add_argument(
                '-d', '--destination-ip',
                help='The destination IPs to monitor(with or without mask [ip/mask])')
        parser.add_argument(
                '-i', '--in-interface',
                help='The incomming interface of the packets to monitor')
        parser.add_argument(
                '-o', '--out-interface',
                help='The outgoing interface of the packets to monitor')

        # Sub-commands functionnality to split server and client mode
        subparsers = parser.add_subparsers(
                title='Subcommand protocol type', dest='protocol',
                help='Choose the protocol type to monitor')
        parser_tcp = subparsers.add_parser('tcp', help='Monitor tcp flow')
        parser_tcp.add_argument(
                '--dport', type=str,
                help='The destination port to monitor')
        parser_tcp.add_argument(
                '--sport', type=str,
                help='The source port to monitor')
        parser_udp = subparsers.add_parser('udp', help='Monitor udp flow')
        parser_udp.add_argument(
                '--dport', type=str,
                help='The destination port to monitor')
        parser_udp.add_argument(
                '--sport', type=str,
                help='The source port to monitor')
        parser_tcp = subparsers.add_parser('icmp', help='Monitor icmp flow')

        # get args
        args = vars(parser.parse_args())
        main(**args)


import keras
from keras.models import Model
from keras import backend as K
from keras.layers import UpSampling2D, ZeroPadding2D, concatenate
from keras.layers import Conv2D, Input, LeakyReLU, Conv2DTranspose
import numpy as np
# K.set_image_data_format('channels_first')
K.set_learning_phase(0)

def k_model(shape):
    # x = Input(shape=[6,512,512],)
    x = Input(shape=shape, )
    conv0 = Conv2D(64,(3,3),padding='same',name='conv0')(x)
    conv0 = LeakyReLU(0.1)(conv0)
    padding = ZeroPadding2D()(conv0)
    conv1 = Conv2D(64,(3,3),strides=(2,2),padding='valid',name='conv1')(padding)
    conv1 = LeakyReLU(0.1)(conv1)
    conv1_1 = Conv2D(128,(3,3),padding='same',name='conv1_1')(conv1)
    conv1_1 = LeakyReLU(0.1)(conv1_1)
    padding = ZeroPadding2D()(conv1_1)
    conv2 = Conv2D(128,(3,3),strides=(2,2),padding='valid',name='conv2')(padding)
    conv2 = LeakyReLU(0.1)(conv2)
    conv2_1 = Conv2D(128,(3,3),padding='same',name='conv2_1')(conv2)
    conv2_1 = LeakyReLU(0.1)(conv2_1)
    padding = ZeroPadding2D()(conv2_1)
    conv3 = Conv2D(256,(3,3),strides=(2,2),padding='valid',name='conv3')(padding)
    conv3 = LeakyReLU(0.1)(conv3)
    conv3_1 = Conv2D(256,(3,3),padding='same',name='conv3_1')(conv3)
    conv3_1 = LeakyReLU(0.1)(conv3_1)
    padding = ZeroPadding2D()(conv3_1)
    conv4 = Conv2D(512,(3,3),strides=(2,2),padding='valid',name='conv4')(padding)
    conv4 = LeakyReLU(0.1)(conv4)
    conv4_1 = Conv2D(512,(3,3),padding='same',name='conv4_1')(conv4)
    conv4_1 = LeakyReLU(0.1)(conv4_1)
    padding = ZeroPadding2D()(conv4_1)
    conv5 = Conv2D(512,(3,3),strides=(2,2),padding='valid',name='conv5')(padding)
    conv5 = LeakyReLU(0.1)(conv5)
    conv5_1 = Conv2D(512,(3,3),strides=(1,1),padding='same',name='conv5_1')(conv5)
    conv5_1 = LeakyReLU(0.1)(conv5_1)
    padding = ZeroPadding2D()(conv5_1)
    conv6 = Conv2D(1024,(3,3),strides=(2,2),padding='valid',name='conv6')(padding)
    conv6 = LeakyReLU(0.1)(conv6)
    conv6_1 = Conv2D(1024,(3,3),padding='same',name='conv6_1')(conv6)
    conv6_1 = LeakyReLU(0.1)(conv6_1)

    flow6 = Conv2D(2,(3,3),padding='same',name='predict_flow6')(conv6_1)
    flow6_up = Conv2DTranspose(2,(4,4),strides=(2,2),name='upsampled_flow6_to_5',padding='same')(flow6)
    deconv5 = Conv2DTranspose(512,(4,4),strides=(2,2),padding='same',name='deconv5')(conv6_1)
    deconv5 = LeakyReLU(0.1)(deconv5)

    # print(deconv5.get_shape())
    concat5 = concatenate([conv5_1,deconv5,flow6_up],axis=3)  # 16
    inter_conv5 = Conv2D(512,(3,3),padding='same',name='inter_conv5')(concat5)
    flow5 = Conv2D(2,(3,3),padding='same',name='predict_flow5')(inter_conv5)

    flow5_up = Conv2DTranspose(2,(4,4),strides=(2,2),name='upsampled_flow5_to4',padding='same')(flow5) #32
    deconv4 =  Conv2DTranspose(256,(4,4),strides=(2,2),name='deconv4',padding='same')(concat5)
    deconv4 = LeakyReLU(0.1)(deconv4)

    concat4 = concatenate([conv4_1,deconv4,flow5_up],axis=3)
    inter_conv4 = Conv2D(256,(3,3),padding='same',name='inter_conv4')(concat4)
    flow4 = Conv2D(2,(3,3),padding='same',name='predict_flow4')(inter_conv4)  # (1, 2, 32, 32)

    flow4_up = Conv2DTranspose(2,(4,4),strides=(2,2),name='upsampled_flow4_to3',padding='same')(flow4)  #64
    deconv3 = Conv2DTranspose(128,(4,4),strides=(2,2),name='deconv3',padding='same')(concat4)
    deconv3 = LeakyReLU(0.1)(deconv3)

    concat3 = concatenate([conv3_1,deconv3,flow4_up],axis=3)  # 64
    inter_conv3 = Conv2D(128,(3,3),padding='same',name='inter_conv3')(concat3)
    flow3 = Conv2D(2,(3,3),padding='same',name='predict_flow3')(inter_conv3)
    flow3_up = Conv2DTranspose(2,(4,4),strides=(2,2),name='upsampled_flow3_to2',padding='same')(flow3)  #128
    deconv2 = Conv2DTranspose(64,(4,4),strides=(2,2),name='deconv2',padding='same')(concat3)
    deconv2 = LeakyReLU(0.1)(deconv2)

    concat2 = concatenate([conv2_1,deconv2,flow3_up],axis=3)
    inter_conv2 = Conv2D(64,(3,3),padding='same',name='inter_conv2')(concat2)
    flow2 = Conv2D(2,(3,3),padding='same',name='predict_flow2')(inter_conv2)
    result = UpSampling2D(size=(4,4),interpolation='bilinear')(flow2)   # 4*128
    model = Model(x,result)
    return model

if __name__=='__main__':
    model = k_model([512,512,6])
    # keras.layers.Conv2D().set_weights()
    for l in model.layers:
        # print(type(l))
        print(l.name)
        # print(l)
        for i, w in enumerate(l.get_weights()):
            print('%d'%i  , w.shape)
        # print('**********************************')
        # 0(4, 4, 512, 1024)
        # 1(512, )

    # data = np.random.randn(1,6,512,512)
    # output = model.predict(data,verbose=1)

    # model.summary()   # 45,371,666
    # for w in model.get_weights():
    #     print(w.shape)  # kernel_size,kernel_size,input_channel,output_channel
                            # for deconv, [k,k, output_channel, input_channel]
    # model.get_layer()
    # model.save()
    # model.


import boto3
import os

# enviroment
region = os.environ["AWS_DEFAULT_REGION"]
# s3 = boto3.resource('s3')
s3 = boto3.client('s3')

result_bucket = os.environ["s3Result"]
html_bucket = os.environ["s3Html"]
def get_all_s3_objects(s3, **base_kwargs):
    continuation_token = None
    while True:
        list_kwargs = dict(MaxKeys=1000, **base_kwargs)
        if continuation_token:
            list_kwargs['ContinuationToken'] = continuation_token
        response = s3.list_objects_v2(**list_kwargs)
        yield from response.get('Contents', [])
        if not response.get('IsTruncated'):  # At the end of the list?
            break
        continuation_token = response.get('NextContinuationToken')

def handle_remove(record):
    print('Handling REMOVE event')
    #1. parse oldimage and price
    old_image = record['dynamodb']['OldImage']
    submitid = old_image['PK']['S']
    #2. remove output s3 data
    #2.1 remove output s3 data
    for object_file in get_all_s3_objects(s3,Bucket=result_bucket, Prefix=os.path.join("output",submitid+'/'),Delimiter='/'):
        print('Deleting', object_file['Key'])
        s3.delete_object(Bucket=result_bucket, Key=object_file['Key'])

def lambda_handler(event,context):
    """

    """
    print('------------1-------------')
    print(event)
    try:
        #1. Iterate over each record
        for record in event["Records"]:
            if record['eventName']  == "REMOVE":
                handle_remove(record)

        print('------------2-------------')
    except Exception as e:
        print(e)
        print('------------3-------------')


import numpy as np
from lib.task import Task, Task16
from lib.solver.base import BaseSolver
from lib.util.bert import Bert

from typing import List, Optional

class Task16Solver(BaseSolver):
    def __init__(self, bert: Optional[Bert] = None):
        if bert is None:
            bert = Bert()
        self.bert: Bert = bert

    def get_task_type(self) -> str:
        return Task.TYPE_16

    def solve(self, task: Task16) -> List[str]:
        scores = np.array([self.get_one_comma_score(line) for line in task.lines])
        return [str(i + 1) for i in np.argsort(-scores)[:task.num_choices]]

    def get_one_comma_score(self, text: str) -> float:
        words = text.split(' ')

        scores = []
        num_commas = 0
        for i in range(1, len(words)):
            candidate = ' '.join(words[:i]) + ' [MASK] ' + ' '.join(words[i:])
            score = self.bert.get_word_in_text_scores(candidate, ',')[0]
            if score > 0.95:
                num_commas += 1
            scores.append(score)

        max_score = max(scores)

        if num_commas == 0:
            return max_score
        elif num_commas == 1:
            return 1.0 + max_score
        else:
            return 0.0


from market import Market
from backtest import BackTest, backtest_save, plot_asset, plot_signal
from utils.dingtalk import DingTalk
from utils.sendmail import sendmail
from utils.logger import logger
from utils.storage import txt_read, txt_save, save_to_csv_file, read_csv_file
from utils.tools import *
from config import config
from indicators import *
from data.source.baostockdata import BaoStockData
from data.source.dbdata import DBData
from data.stock_data import *

class Strategy:

    def __init__(self):
        #config.loads("config.json")
        self.asset = 10000
        self.pre_code=None
        self.backtest = BackTest()
        df = BaoStockData.query_trade_dates(start_date="2018-06-30")
        df1 = df.loc[df["is_trading_day"] == '1']
        dates = df1.values.tolist()
        #print(data)
        self.buy_signal,self.sell_signal = [],[]
        self.dates = []
        for k in dates:
            self.dates.append(k)
            self.backtest.initialize(self.dates,dates)
            self.begin()
        plot_asset()
        #plot_signal(data,self.buy_signal,self.sell_signal,self.ma20,self.ma30)

    def begin(self):

        cur_date  = self.dates[-1][0]
        print(cur_date)
        klines = DBData.get_all_kline(cur_date,cur_date)
        code = get_min_pe_ttm_stock(klines).code
        close = float(get_min_pe_ttm_stock(klines).close)
        if self.pre_code!=code:
            if self.pre_code is not None:
                pre_close = float(DBData.get_kline(self.pre_code,cur_date,cur_date)[0].close)
                profit = (pre_close - self.backtest.long_avg_price) * self.backtest.long_quantity
                self.asset += profit
                self.backtest.sell(
                    price=pre_close,
                    amount=self.backtest.long_quantity,
                    long_quantity=0,
                    long_avg_price=0,
                    profit=profit,
                    asset=self.asset

                )
                self.sell_signal.append(pre_close)
                print("sell",self.pre_code,pre_close,cur_date)
            self.pre_code=code
            self.backtest.buy(
                price=close,
                amount=self.asset/close,
                long_quantity=self.asset/close,
                long_avg_price=close,
                profit=0,
                asset=self.asset
            )
            self.buy_signal.append(close)
            print("buy", code, close,cur_date)

if __name__ == '__main__':
    strategy = Strategy()


from enum import Enum
import struct

class Endianness(Enum):
    BIG = b"B\0"
    LITTLE = b"L\0"

class TextEncoding(Enum):
    SHIFT_JIS = 0
    UTF_16 = 1

class Header:
    def __init__(self, endianness, version, bounding_box_min, bounding_box_max,
                 default_vertex_index_size, text_encoding, unk4A, unk4C, unk5C,
                 unk5D, unk68):
        self.endianness = endianness
        self.version = version
        self.bounding_box_min = bounding_box_min
        self.bounding_box_max = bounding_box_max
        self.default_vertex_index_size = default_vertex_index_size
        self.text_encoding = text_encoding
        self.unk4A = unk4A
        self.unk4C = unk4C
        self.unk5C = unk5C
        self.unk5D = unk5D
        self.unk68 = unk68

class Dummy:
    def __init__(self, position, color, forward, reference_id,
                 parent_bone_index, upward, attach_bone_index, flag1,
                 use_upward_vector, unk30, unk34):
        self.position = position
        self.color = color
        self.forward = forward
        self.reference_id = reference_id
        self.parent_bone_index = parent_bone_index
        self.upward = upward
        self.attach_bone_index = attach_bone_index
        self.flag1 = flag1
        self.use_upward_vector = use_upward_vector
        self.unk30 = unk30
        self.unk34 = unk34

class Material:
    def __init__(self, name, mtd_path, texture_count, texture_index, flags,
                 unk18):
        self.name = name
        self.mtd_path = mtd_path
        self.texture_count = texture_count
        self.texture_index = texture_index
        self.flags = flags
        self.unk18 = unk18

class Bone:
    def __init__(self, translation, name, rotation, parent_index, child_index,
                 scale, next_sibling_index, previous_sibling_index,
                 bounding_box_min, unk3C, bounding_box_max):
        self.translation = translation
        self.name = name
        self.rotation = rotation
        self.parent_index = parent_index
        self.child_index = child_index
        self.scale = scale
        self.next_sibling_index = next_sibling_index
        self.bounding_box_min = bounding_box_min
        self.unk3C = unk3C
        self.bounding_box_max = bounding_box_max

class Mesh:
    # According to upstream: when 1, mesh is in bind pose; when 0, it isn't.
    # Most likely has further implications.
    class DynamicMode(Enum):
        NON_DYNAMIC = 0
        DYNAMIC = 1

    def __init__(self, dynamic_mode, material_index, default_bone_index,
                 bone_indices, index_buffer_indices, vertex_buffer_indices):
        self.dynamic_mode = dynamic_mode
        self.material_index = material_index
        self.default_bone_index = default_bone_index
        self.bone_indices = bone_indices
        self.index_buffer_indices = index_buffer_indices
        self.vertex_buffer_indices = vertex_buffer_indices

class IndexBuffer:
    class DetailFlags(Enum):
        LOD_LEVEL1 = 0x01000000
        LOD_LEVEL2 = 0x02000000
        MOTION_BLUR = 0x80000000

    class PrimitiveMode(Enum):
        TRIANGLES = 0
        TRIANGLE_STRIP = 1

    class BackfaceVisibility(Enum):
        SHOW = 0
        CULL = 1

    def __init__(self, detail_flags, primitive_mode, backface_visibility,
                 unk06, indices):
        self.detail_flags = detail_flags
        self.primitive_mode = primitive_mode
        self.backface_visibility = backface_visibility
        self.unk06 = unk06
        self.indices = indices

    def _inflate(self, faces):
        if self.primitive_mode == self.PrimitiveMode.TRIANGLES:
            for i in range(0, len(self.indices), 3):
                faces.append(tuple(self.indices[i:i + 3]))
        else:
            direction = -1
            f1 = self.indices[0]
            f2 = self.indices[1]
            for i in range(2, len(self.indices)):
                f3 = self.indices[i]
                direction *= -1
                if f1 != f2 and f2 != f3 and f3 != f1:
                    if direction > 0:
                        faces.append((f1, f2, f3))
                    else:
                        faces.append((f1, f3, f2))
                f1 = f2
                f2 = f3

class VertexBuffer:
    def __init__(self, buffer_index, struct_index, struct_size, vertex_count,
                 buffer_data):
        self.buffer_index = buffer_index
        self.struct_index = struct_index
        self.struct_size = struct_size
        self.vertex_count = vertex_count
        self.buffer_data = buffer_data

    def _inflate(self, vertices, struct, version):
        struct_size = sum(member.size() for member in struct)
        assert self.struct_size == struct_size
        assert len(self.buffer_data) % self.struct_size == 0

        # For now, only select from a limited set of attributes: POSITION,
        # BONE_WEIGHTS, BONE_INDICES, and UV.
        struct_members = [
            member for member in struct if member.attribute_type in {
                VertexBufferStructMember.AttributeType.POSITION,
                VertexBufferStructMember.AttributeType.BONE_WEIGHTS,
                VertexBufferStructMember.AttributeType.BONE_INDICES,
                VertexBufferStructMember.AttributeType.UV,
            }
        ]

        attribute_map = {
            VertexBufferStructMember.AttributeType.POSITION:
            vertices.positions,
            VertexBufferStructMember.AttributeType.BONE_WEIGHTS:
            vertices.bone_weights,
            VertexBufferStructMember.AttributeType.BONE_INDICES:
            vertices.bone_indices,
            VertexBufferStructMember.AttributeType.UV:
            vertices.uv,
        }
        for member in struct_members:
            for i in range(self.vertex_count):
                data = member._unpack(self.buffer_data, i * self.struct_size, version)
                attribute_map[member.attribute_type].append(data)

class VertexBufferStructMember:
    class DataType(Enum):
        # 2 single precision floats
        FLOAT2 = 0x01
        # 3 single precision floats
        FLOAT3 = 0x02
        # 4 single precision floats
        FLOAT4 = 0x03
        # unknown
        BYTE4A = 0x10
        # 4 bytes
        BYTE4B = 0x11
        # 2 shorts
        SHORT2_TO_FLOAT2 = 0x12
        # 4 bytes
        BYTE4C = 0x13
        # 2 shorts
        UV = 0x15
        # 2 pairs of 2 shorts
        UV_PAIR = 0x16
        # 4 shorts (unsigned?)
        SHORT_BONE_INDICES = 0x18
        # 4 shorts
        SHORT4_TO_FLOAT4A = 0x1A
        # unknown
        SHORT4_TO_FLOAT4B = 0x2E
        # unknown
        BYTE4E = 0x2F

    class AttributeType(Enum):
        # Location of the vertex.
        POSITION = 0
        # Weight of the vertex's attachment to bones.
        BONE_WEIGHTS = 1
        # Bones the vertex is weighted to, indexing the parent mesh's bone
        # indices.
        BONE_INDICES = 2
        # Orientation of the vertex.
        NORMAL = 3
        # Texture coordinates of the vertex.
        UV = 5
        # Vector pointing perpendicular to the normal.
        TANGENT = 6
        # Vector pointing perpendicular to the normal and tangent.
        BITANGENT = 7
        # Data used for blending, alpha, etc.
        VERTEX_COLOR = 10

    def __init__(self, unk00, struct_offset, data_type, attribute_type, index):
        self.unk00 = unk00
        self.struct_offset = struct_offset
        self.data_type = data_type
        self.attribute_type = attribute_type
        self.index = index

    def size(self):
        if self.data_type in {
                self.DataType.BYTE4A,
                self.DataType.BYTE4B,
                self.DataType.SHORT2_TO_FLOAT2,
                self.DataType.BYTE4C,
                self.DataType.UV,
                self.DataType.BYTE4E,
        }:
            return 4
        if self.data_type in {
                self.DataType.FLOAT2,
                self.DataType.UV_PAIR,
                self.DataType.SHORT_BONE_INDICES,
                self.DataType.SHORT4_TO_FLOAT4A,
                self.DataType.SHORT4_TO_FLOAT4B,
        }:
            return 8
        if self.data_type == self.DataType.FLOAT3:
            return 12
        if self.data_type == self.DataType.FLOAT4:
            return 16
        raise Exception(f"unknown size for data type: {self.data_type}")

    def _unpack(self, buf, offset, version):
        if version >= 0x2000F:
            uv_divisor = 2048.0
        else:
            uv_divisor = 1024.0
        offset += self.struct_offset

        #print(f'Type: {self.data_type} | Attr: {self.attribute_type}')

        if self.data_type == self.DataType.FLOAT2:
            return tuple(struct.unpack_from("ff", buf, offset))
        if self.data_type == self.DataType.FLOAT3:
            return tuple(struct.unpack_from("fff", buf, offset))
        if self.data_type == self.DataType.FLOAT4:
            return tuple(struct.unpack_from("ffff", buf, offset))

        if (self.data_type == self.DataType.BYTE4A):
            weights = struct.unpack_from("bbbb", buf, offset)
            return tuple(weight / 127.0 for weight in weights)
        if (self.data_type == self.DataType.BYTE4B):
                return tuple(struct.unpack_from("BBBB", buf, offset))
        if (self.data_type == self.DataType.BYTE4C):
            weights = struct.unpack_from("BBBB", buf, offset)
            return tuple(weight / 255.0 for weight in weights)

        if (self.data_type == self.DataType.UV):
            uv = struct.unpack_from("hh", buf, offset)
            return tuple(component / uv_divisor for component in uv)
        if (self.data_type == self.DataType.UV_PAIR):
            uv = struct.unpack_from("hhhh", buf, offset)
            return tuple(component / uv_divisor for component in uv)

        raise Exception(f'Unsupported type {self.data_type}')

class Texture:
    def __init__(self, path, type_name, scale, unk10, unk11, unk14, unk18,
                 unk1C):
        self.path = path
        self.type_name = type_name
        self.scale = scale
        self.unk10 = unk10
        self.unk11 = unk11
        self.unk14 = unk14
        self.unk18 = unk18
        self.unk1C = unk1C

class InflatedMesh:
    class Vertices:
        def __init__(self):
            self.positions = []
            self.bone_weights = []
            self.bone_indices = []
            self.uv = []

    def __init__(self):
        self.faces = []
        self.vertices = self.Vertices()

class Flver:
    def __init__(self, header, dummies, materials, bones, meshes,
                 index_buffers, vertex_buffers, vertex_buffer_structs,
                 textures):
        self.header = header
        self.dummies = dummies
        self.materials = materials
        self.bones = bones
        self.meshes = meshes
        self.index_buffers = index_buffers
        self.vertex_buffers = vertex_buffers
        self.vertex_buffer_structs = vertex_buffer_structs
        self.textures = textures

    # For every mesh, combine all index buffers into a single index buffer and
    # all vertex buffer attributes into individual corresponding attribute
    # lists.
    def inflate(self):
        return [self._inflate_mesh(mesh) for mesh in self.meshes]

    def _inflate_mesh(self, mesh):
        result = InflatedMesh()

        # Triangulate faces
        index_buffers = [
            self.index_buffers[index] for index in mesh.index_buffer_indices
            if len(self.index_buffers[index].detail_flags) == 0
        ]
        if len(index_buffers) == 0:
            return None
        assert len(index_buffers) == 1
        index_buffers[0]._inflate(result.faces)

        # Parse vertex buffer attributes
        vertex_buffers = [
            self.vertex_buffers[index] for index in mesh.vertex_buffer_indices
        ]
        assert len(vertex_buffers) > 0
        for vertex_buffer in vertex_buffers:
            struct = self.vertex_buffer_structs[vertex_buffer.struct_index]
            vertex_buffer._inflate(vertices=result.vertices,
                                   struct=struct,
                                   version=self.header.version)

        return result


# trying to establish logic understanding of PyQt
from PyQt5 import QtWidgets
from ally_go_at_GUI import *
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QPushButton, QAction, QLineEdit, QMessageBox, QLabel
from gui_ally_mk2_acrolite import *
import time

def establish_static_components(win):

    plug = QLabel(win)
    plug.setText("Product of SpaceCows™ est. 2020")
    plug.setGeometry(160, 10, 300, 30)
    version = QLabel(win)
    version.setText("Acrolite Version 2.1")
    version.setGeometry(230, 50, 300, 30)

    # create file name prompt
    filenameprompt = QLabel(win)
    filenameprompt.setText("File Name:")
    filenameprompt.setGeometry(15, 90, 100, 50)
    # Create file name textbox
    file_name_textbox = QLineEdit(win)
    file_name_textbox.move(100, 100)
    file_name_textbox.resize(200, 30)
    # create path name example
    sfilenameprompt = QLabel(win)
    filenameprompt.setText("format: Word.docx")
    filenameprompt.setGeometry(250, 85, 150, 50)

    # create path name prompt
    filenameprompt = QLabel(win)
    filenameprompt.setText("Path Name:")
    filenameprompt.setGeometry(15, 140, 100, 50)
    # Create path textbox
    file_path_textbox = QLineEdit(win)
    file_path_textbox.move(100, 150)
    file_path_textbox.resize(400, 30)

    find_doc_button = QPushButton('Find and Scan', win)
    find_doc_button.setGeometry(250, 200, 115, 30)

    # connect button to function get_file, change file_path global variable to return file name
    find_doc_button.clicked.connect(button1_clicked)

def button1_clicked():
    print("Button1 was clicked")


""" Initial schema

Revision ID: 629958e9b907
Revises:
Create Date: 2017-01-01 17:20:08.514311

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = '629958e9b907'
down_revision = None
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('iiif_image',
    sa.Column('surrogate_id', sa.Integer(), nullable=False),
    sa.Column('id', sa.String(), nullable=True),
    sa.Column('info', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
    sa.PrimaryKeyConstraint('surrogate_id'),
    sa.UniqueConstraint('id')
    )
    op.create_table('manifest',
    sa.Column('surrogate_id', sa.Integer(), nullable=False),
    sa.Column('id', sa.String(), nullable=True),
    sa.Column('origin', sa.String(), nullable=True),
    sa.Column('manifest', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
    sa.Column('label', sa.String(), nullable=True),
    sa.PrimaryKeyConstraint('surrogate_id'),
    sa.UniqueConstraint('id'),
    sa.UniqueConstraint('origin')
    )
    op.create_table('identifier',
    sa.Column('surrogate_id', sa.Integer(), nullable=False),
    sa.Column('id', sa.String(), nullable=True),
    sa.Column('type', sa.String(), nullable=True),
    sa.Column('manifest_id', sa.String(), nullable=False),
    sa.ForeignKeyConstraint(['manifest_id'], ['manifest.id'], ),
    sa.PrimaryKeyConstraint('surrogate_id'),
    sa.UniqueConstraint('id')
    )
    op.create_table('image',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('url', sa.String(), nullable=True),
    sa.Column('width', sa.Integer(), nullable=True),
    sa.Column('height', sa.Integer(), nullable=True),
    sa.Column('format', sa.Text(), nullable=True),
    sa.Column('iiif_id', sa.String(length=22), nullable=True),
    sa.ForeignKeyConstraint(['iiif_id'], ['iiif_image.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('url')
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('image')
    op.drop_table('identifier')
    op.drop_table('manifest')
    op.drop_table('iiif_image')
    # ### end Alembic commands ###


import sys
import traceback
import argparse

import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject  # noqa:F401,F402

# Initializes Gstreamer, it's variables, paths
Gst.init(sys.argv)

DEFAULT_PIPELINE = 'videotestsrc num-buffers=10000 '\
    '! decodebin'\
    '! videoscale'\
    '! video/x-raw,width=800,height=600'\
    '! x264enc'\
    '! rtph264pay'\
    '! udpsink port=10002 host=172.17.0.1'

ap = argparse.ArgumentParser()

ap.add_argument("-p", "--pipeline", required=False,
                default=DEFAULT_PIPELINE, help="Gstreamer pipeline without gst-launch")

args = vars(ap.parse_args())

def on_message(bus: Gst.Bus, message: Gst.Message, loop: GObject.MainLoop):
    mtype = message.type
    if mtype == Gst.MessageType.EOS:
        print("End of stream")
        loop.quit()

    elif mtype == Gst.MessageType.ERROR:
        err, debug = message.parse_error()
        print(err, debug)
        loop.quit()

    elif mtype == Gst.MessageType.WARNING:
        err, debug = message.parse_warning()
        print(err, debug)

    return True

command = args["pipeline"]

pipeline = Gst.parse_launch(command)

Gst.debug_bin_to_dot_file(pipeline,Gst.DebugGraphDetails(-1),'/tmp/teste.dot')

bus = pipeline.get_bus()
bus.add_signal_watch()
pipeline.set_state(Gst.State.PLAYING)
loop = GObject.MainLoop()

bus.connect("message", on_message, loop)

try:
    loop.run()
except Exception:
    traceback.print_exc()
    loop.quit()

# Stop Pipeline
pipeline.set_state(Gst.State.NULL)


from Classes.Client.cli_client import CliClient
from Classes.Backend.config import Config
from Classes.Backend.command import Command
import PySimpleGUI as sg

def exec():
    config = Config()
    config_dict = config.get_config_dict()
    application = CliClient(config_dict)
    window = application.get_window()
    window['input-p'].bind('<Return>','_Enter')
    while True:
        event, values = window.read()
        if event not in (sg.WIN_CLOSED,'_EXIT_', 'Close'):
            if event == "input-p" + "_Enter":
                command = Command(values['input-p'])
                application.add_command(values['input-p'],command.get_result())
                window = application.get_window()
                window['input-p'].bind('<Return>','_Enter')
        else:
            break

exec()


import time
import spidev
import sys

class SPI:
    def __init__(self, no, num):
        self.spi = spidev.SpiDev()
        self.spi.open(no, num)

    def convertVolts(self):
        v = (self.data * 3.3) / float(1023)
        self.volts = round(v, 4)
        return self.volts

    def readAdc(self, channel):
        spi = spidev.SpiDev()
        spi.open(0,0)
#        adc = self.spi.xfer2([0x01,(8+channel)<<4,0x00])
        adc = spi.xfer2([0x68,0x00])
#        self.data = ((adc[0]&3) << 8) | adc[1]))
        self.data = ((adc[0]<<8) + adc[1])&0x3ff
        print(str(adc[0]) + "," + str(adc[1]))
        return self.data

while True:
    spi = SPI(0, 0)
    data = spi.readAdc(0)
    print("adc: {:8}".format(data))
    volts = spi.convertVolts()
    print("volts: {:8.2f}".format(volts))

    time.sleep(0.5)


from django.conf.urls import include, url
from catalog import views as catalog_views

app_name = 'api'

urlpatterns = [
    url(
        regex=r'^albums/$',
        view=catalog_views.AlbumList.as_view(),
        name='album-list'
    ),
    url(
        regex=r'^artists/$',
        view=catalog_views.ArtistList.as_view(),
        name='artist-list'
    ),
    url(
        regex=r'^images/$',
        view=catalog_views.ImageList.as_view(),
        name='image-list'
    ),
    url(
        regex=r'^publishers/$',
        view=catalog_views.PublisherList.as_view(),
        name='publisher-list'
    ),
    url(
        regex=r'^reviews/$',
        view=catalog_views.ReviewList.as_view(),
        name='review-list'
    ),
    url(
        regex=r'^tracks/$',
        view=catalog_views.TrackList.as_view(),
        name='track-list'
    ),
]


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

# # GRIP: The Sparks Foundation #AUG21
# ### Data Science and Business Analytics Intern
# ### Name: Srihari Reddy Kata
# ### Task 1: Prediction using Machine Learning Algorithm
#

# In this task we have to predict the percantage of student based on the number of hours he/she studied.This task have two variables where the feature is no.of hours studied and target value is percantage of score.This can be solved using simple linear regression model.

# In[1]:

#importing libraries required
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# #### Reading data from URL

# In[2]:

url='https://raw.githubusercontent.com/AdiPersonalWorks/Random/master/student_scores%20-%20student_scores.csv'
data=pd.read_csv(url)
data.head()

# In[7]:

data.shape

# #### Exploring data

# In[3]:

data.describe()

# In[9]:

data.info()

# In[4]:

#Plotting Dataset values using plot
data.plot(kind='scatter',x='Hours',y='Scores')
plt.show()

# ##### From the above Plot we can clearly say that it follows Linear Regression Model as the dependent variable is increasing linearly with exploratory variable

# In[ ]:

hours=data['Hours']
scores=data['Scores']
sns.distplot(hours)

# data.corr(method='spearman')

# In[7]:

sns.distplot(scores)

# ### Linear Regression

# In[13]:

X=data.iloc[:,:1].values
y=data.iloc[:,1].values

# In[14]:

from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=50)

# ### Training the Model

# In[ ]:

from sklearn.linear_model import LinearRegression
reg = LinearRegression()
reg.fit(X_train,y_train)

# In[16]:

m= reg.coef_
c=reg.intercept_
line=m*X+c
plt.scatter(X,y)
plt.plot(X,line)
plt.show()

# In[17]:

y_pred = reg.predict(X_test)

# In[18]:

actual_predicted = pd.DataFrame({'Target':y_test,'Predicted':y_pred})
actual_predicted

# In[19]:

sns.set_style('whitegrid')
sns.distplot(np.array(y_test-y_pred))
plt.show()

# What would be the predicted score if a student scores for 9.25 hours/day?

# In[20]:

h=9.25
s=reg.predict([[h]])
print("If a student studies for {} hours per day he/she will score {}% in exam".format(h,s))

# ## Model Evaluation

# In[21]:

from sklearn import metrics
from sklearn.metrics import r2_score
print('Mean Absolute Error:',metrics.mean_absolute_error(y_test,y_pred))
print('R2 score:',r2_score(y_test,y_pred))

# In[ ]:


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

Nparticles = 2                                                          # Number of Particles

r = np.empty([Nparticles, 4], dtype = float)                            # Setting up Array of particles pos and vel
xboundary = [0,100]                                                     # Establishing a boundary in x axis
yboundary = [0,100]                                                     # in the y axis

xiniti = np.random.uniform(low = .01, high = 99.5, size = Nparticles)   # Initial x positions
yintii = np.random.uniform(low = 0.01, high = 99.3, size = Nparticles)  # Initial y positions

vmax = 1.0                                                              # Maximum velocity - in this case THE velcoity
vxin = np.random.uniform(size = 25, low = 0.01, high = vmax - .05  )    # Intial vx velocities
vyin = np.sqrt(vmax - np.square(vxin))                                  # initial vy velocities
vtotali = np.sqrt(np.square(vyin) + np.square(vxin))                    # total Vi - should be 1 in this case

radius = .5                                                             # Radius of all particles

timestep = .001                                                         # Timestep to update positions

# Functions to Update positions and velocities

def Update_Positions(rarray,h):

    xposes = r[0,:]
    xposes += r[2,:]*h

    yposes = r[1,:]
    yposes += r[3,:]*h
    return np.array([xposes, yposes], dtype = float)

def Update_Velocities(rarray,h,a, boundary):

    # Finding the slope to update the velocity to

    a_x = 0.0 # This should be acceleration vector - x direction (From slope)
    a_y = 0.0 # This should be acceleration vector - y direction (From Slope)

    # Updating the change in direction

    vx = rarray[2,:] = h*a_x
    vy = rarray[3,:] = h*a_y
    vmag = np.sqrt(vx) + np.square(vy)      # We should divide both resulting velocities by this

    # Normalize Velcocities back to one - divide by magnitued of v vector
    vx /= vmag
    vy /= vmag

    return np.array([vx,vy], dtype = float)

# Creating a graph of the Particles
def NewGraph():
    plt.figure()

    plt.scatter(x , y)

'''
# Animation Function From matplotlib
fig = plt.figure()

def f(r, y):

    return np.sin(x) + np.cos(y)

x = np.linspace(0, 2 * np.pi, 120)
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
# ims is a list of lists, each row is a list of artists to draw in the
# current frame; here we are just animating one artist, the image, in
# each frame
ims = []
for i in range(60):
    x += np.pi / 15.
    y += np.pi / 20.
    scat = plt.scatter( )
    scat.append([im])

ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True,
                                repeat_delay=1000)

# To save the animation, use e.g.
#
# ani.save("movie.mp4")
#
# or
#
# from matplotlib.animation import FFMpegWriter
# writer = FFMpegWriter(fps=15, metadata=dict(artist='Me'), bitrate=1800)
# ani.save("movie.mp4", writer=writer)

plt.show()

'''


from typing import (
    List,
)

class FindKClosestElements:
    """
    @param a: an integer array
    @param target: An integer
    @param k: An integer
    @return: an integer array
    """
    def k_closest_numbers(self, a: List[int], target: int, k: int) -> List[int]:
        if not a:
            return []
        index = self.find_upper_closest(a, target)
        left, right = index - 1, index
        results = []
        for i in range(k):
            if self.is_left_closer(a, left, right, target):
                results.append(a[left])
                left -= 1
            else:
                results.append(a[right])
                right += 1
        return results

    def is_left_closer(self, a, left, right, target):
        if left < 0:
            return False
        if right == len(a):
            return True
        return target - a[left] <= a[right] - target

    def find_upper_closest(self, a, target):
        start, end = 0, len(a) - 1
        while start + 1 < end:
            mid = (start + end) // 2
            if a[mid] >= target:
                end = mid
            else:
                start = mid
        if a[start] >= target:
            return start
        if a[end] >= target:
            return end
        return len(a)


from django.contrib.sitemaps.views import sitemap
from django.urls import path

from news.sitemaps import NewsSitemap
from news.views import NewsListView, NewsDetailView, NewsCreateView

app_name = 'news'

sitemaps = {
    'news': NewsSitemap,
}

urlpatterns = [
    path('sitemap.xml/', sitemap, {'sitemaps': sitemaps}),
    path('create/', NewsCreateView.as_view(), name='news_create'),
    path('<slug:slug>/', NewsDetailView.as_view(), name='news_detail'),
    path('', NewsListView.as_view(), name='news_list'),
]


from datetime import datetime

from django.conf import settings
from django.contrib.admin import AdminSite
from django.contrib.admin.templatetags.admin_urls import admin_urlname
from django.contrib.auth import get_user_model
from django.contrib.sites.models import Site
from django.core.cache import cache
from django.test import RequestFactory, TestCase
from django.test.utils import override_settings
from django.urls import reverse
from django.utils import translation
from fluent_pages.urlresolvers import PageTypeNotMounted

from fluent_blogs.admin import EntryAdmin
from fluent_blogs.models import Entry
from fluent_blogs.pagetypes.blogpage.models import BlogPage

class BlogPageTests(TestCase):
    """
    Testing integration between django-fluent-pages and django-fluent-blogs
    """

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        User = get_user_model()

        Site.objects.get_or_create(
            id=settings.SITE_ID,
            defaults=dict(domain="django.localhost", name="django at localhost"),
        )
        cls.user = User.objects.create_superuser(
            "fluent-blogs-admin", "admin@example.org", "admin"
        )

    def setUp(self):
        super().setUp()
        self.overrider = override_settings(
            ROOT_URLCONF="fluent_blogs.pagetypes.blogpage.tests.urls"
        )
        self.overrider.enable()

    def tearDown(self):
        self.overrider.disable()
        cache.clear()  # BlogPage URLs are stored in cache
        super().tearDown()

    def test_no_blogpage(self):
        """
        When there is no blog page, the system should detect this.
        """
        date = datetime(year=2016, month=5, day=1)
        entry = Entry.objects.language("en").create(
            author=self.user, slug="foo", publication_date=date
        )
        self.assertRaises(PageTypeNotMounted, lambda: entry.default_url)
        self.assertEqual(
            entry.get_absolute_url_format(), "/.../2016/05/{slug}/"
        )  # slug preview should not crash.

    def test_blogpage_fallback_url(self):
        """
        Testing how translated entries appear on a ``BlogPage`` that has no translation except for the default/fallback.
        """
        page = BlogPage.objects.language("en").create(
            author=self.user, status=BlogPage.PUBLISHED, slug="blogpage"
        )
        self.assertEqual(page.default_url, "/en/blogpage/")

        date = datetime(year=2016, month=5, day=1)
        entry = Entry.objects.language("en").create(
            author=self.user, slug="hello-en", publication_date=date
        )
        self.assertEqual(entry.default_url, "/en/blogpage/2016/05/hello-en/")

        # Simulate fetched entry in new language
        with translation.override("nl"):
            entry.set_current_language("nl")
            self.assertEqual(entry.default_url, "/nl/blogpage/2016/05/hello-en/")

            # Create new language
            entry.create_translation("nl", slug="hello-nl")
            self.assertEqual(entry.default_url, "/nl/blogpage/2016/05/hello-nl/")

    def test_no_blogpage_admin(self):
        """
        When there is no page type mounted, the admin page should still be accessable.
        """
        date = datetime(year=2016, month=5, day=1)
        entry = Entry.objects.language("en-us").create(
            author=self.user, slug="hello-fr", publication_date=date
        )
        url = reverse(admin_urlname(entry._meta, "change"), args=(entry.pk,))

        # Make an admin site, avoid dependency on URLs
        admin = AdminSite()
        admin.register(Entry, EntryAdmin)
        model_admin = admin._registry[Entry]
        request = RequestFactory().get(url)
        request.user = self.user

        response = model_admin.change_view(request, str(entry.pk))
        self.assertContains(response, "page needs to be created first")


import re
from os import environ

from github import Github, ContentFile
from github.GitRelease import GitRelease

GITHUB_REPOSITORY = environ.get("GITHUB_REPOSITORY", "awtkns/fastapi-crudrouter")
GITHUB_TOKEN = environ.get("GH_TOKEN") or environ.get("GITHUB_TOKEN")
GITHUB_URL = "https://github.com"
FILE_PATH = "docs/en/docs/releases.md"
COMMIT_MESSAGE = "ðŸ¤– auto update releases.md"

gh = Github(GITHUB_TOKEN)

def generate_header(r: GitRelease, separator: bool = False):
    header = ""
    if separator:
        header += "\n\n---\n"

    return (
        header
        + f"""
## [{r.title}]({r.html_url}){" { .releases } "}
{r.created_at.date()}
"""
    )

def commit_update(content: str):
    file = repo.get_contents(FILE_PATH)
    old_content = file.decoded_content.decode()

    if new_content == old_content:
        print("No new release information, Skipping.")
    else:
        print("Uploading new release documentation")

    repo.update_file(file.path, message=COMMIT_MESSAGE, content=content, sha=file.sha)

def insert_links(content: str):
    """Replaces both #pull and @author with correct links"""
    url = repo.html_url + "/pull"
    content = re.sub(r"#(\d+)", rf"[#\1]({url}/\1)", content)
    return re.sub(r"@(\S+)", rf"[@\1]({GITHUB_URL}/\1)", content)

if __name__ == "__main__":
    repo = gh.get_repo(GITHUB_REPOSITORY)

    new_content = ""
    first = False
    for r in repo.get_releases():
        if not r.draft:
            new_content += generate_header(r, first)
            new_content += r.body
            first = True

    new_content = insert_links(new_content)
    commit_update(new_content)


import pandas as pd
import numpy as np
import datetime
from sklearn.preprocessing import OneHotEncoder
import os
import shutil

def load_data(data_path):
    df = pd.read_csv(data_path)
    # 过滤掉方向未知或速度未知的记录，同时过滤掉高度小于-50m的记录
    df = df.loc[(df['DIRECTION'] >= 0) & (df['SPEED'] >= 0) & (df['HEIGHT'] >= -50)]
    df = df.drop_duplicates()
    return df

def get_time(unix_time_stamp, time_type='hour'):
    time_value = None
    try:
        dtime = datetime.datetime.fromtimestamp(unix_time_stamp)
        if time_type == 'year':
            time_value = dtime.year
        if time_type == 'month':
            time_value = dtime.month
        if time_type == 'day':
            time_value = dtime.day
        if time_type == 'hour':
            time_value = dtime.hour
        if time_type == 'minute':
            time_value = dtime.minute + dtime.second/60.0
        if time_type == 'weekday':
            time_value = dtime.weekday()
    except Exception as e:
        print("Error:", e)
    return time_value

def extract_feature(data_path, data_process_params=None, target=None):
    # print('Into function "extract_feature":')
    # print('Extracting features...')
    df = load_data(data_path)
    print("Sorting lines by ['TERMINALNO', 'TIME']...")
    df.sort_values(by=['TERMINALNO', 'TIME'], inplace=True)
    features = []
    process_params = {}

    # >>>[1] Time features
    df['month'] = df['TIME'].apply(get_time, args=('month',))
    df['day'] = df['TIME'].apply(get_time, args=('day',))
    df['hour'] = df['TIME'].apply(get_time, args=('hour',))
    df['minute'] = df['TIME'].apply(get_time, args=('minute',))
    df['weekday'] = df['TIME'].apply(get_time, args=('weekday',))

    df['month'] = (df['month'] / 12.0).astype(np.float32)
    df['day'] = (df['day'] / 31.0).astype(np.float32)
    df['hour'] = ((df['hour'] + 1) / 24.0).astype(np.float32)
    df['minute'] = (df['minute'] / 60.0).astype(np.float32)
    df['weekday'] = ((df['weekday'] + 1) / 7.0).astype(np.float32)

    features.extend(['month', 'day', 'hour', 'minute', 'weekday'])

    # >>>[2] Location features
    if target is not None:
        max_lon = df['LONGITUDE'].max()
        min_lon = df['LONGITUDE'].min()
        max_lat = df['LATITUDE'].max()
        min_lat = df['LATITUDE'].min()
        max_h = df['HEIGHT'].max()
        min_h = df['HEIGHT'].min()

        process_params['max_lon'] = max_lon
        process_params['min_lon'] = min_lon
        process_params['max_lat'] = max_lat
        process_params['min_lat'] = min_lat
        process_params['max_h'] = max_h
        process_params['min_h'] = min_h
    else:
        max_lon = data_process_params['max_lon']
        min_lon = data_process_params['min_lon']
        max_lat = data_process_params['max_lat']
        min_lat = data_process_params['min_lat']
        max_h = data_process_params['max_h']
        min_h = data_process_params['min_h']

    df['LONGITUDE'] = ((df['LONGITUDE'] - min_lon) / (max_lon - min_lon)).astype(np.float32)
    df['LATITUDE'] = ((df['LATITUDE'] - min_lat) / (max_lat - min_lat)).astype(np.float32)
    df['HEIGHT'] = ((df['HEIGHT'] - min_h) / (max_h - min_h)).astype(np.float32)

    features.extend(['LONGITUDE', 'LATITUDE', 'HEIGHT'])

    # >>>[3]Speed features
    if target is not None:
        mean_speed = df['SPEED'].mean()
        std_speed = df['SPEED'].std()

        process_params['mean_speed'] = mean_speed
        process_params['std_speed'] = std_speed
    else:
        mean_speed = data_process_params['mean_speed']
        std_speed = data_process_params['std_speed']

    df['SPEED'] = ((df['SPEED'] - mean_speed) / std_speed).astype(np.float32)

    features.append('SPEED')

    # >>>[4]Direction features
    df['DIRECTION'] = (df['DIRECTION'] / 360.0).astype(np.float32)

    features.append('DIRECTION')

    # >>>[5] Call state features
    if target is not None:
        ohe = OneHotEncoder()
        ohe_feat = ohe.fit_transform(df[['CALLSTATE']]).toarray().astype(np.uint8)
        process_params['ohe'] = ohe
    else:
        ohe = data_process_params['ohe']
        ohe_feat = ohe.transform(df[['CALLSTATE']]).toarray().astype(np.uint8)
    dummy_feat_names = ['CALLSTATE_'+str(i) for i in range(ohe_feat.shape[1])]
    dummy_feat = pd.DataFrame(ohe_feat, index=df.index, columns=dummy_feat_names)
    df = df.join(dummy_feat)

    features.extend(dummy_feat_names)
    # print('Quit from function "extract_feature"')

    if target is None:
        return df[['TERMINALNO']+features], features, None
    else:
        return df[['TERMINALNO']+features + [target]], features, process_params

def save_data(df, feature_names, data_dir, target=None):

    length = []

    users = df['TERMINALNO'].unique()
    # make dir to save datas
    if os.path.exists(data_dir):
        shutil.rmtree(data_dir)
    os.makedirs(data_dir)

    os.chdir(data_dir)
    features_dir = os.path.join(data_dir, 'datas/')
    os.makedirs('datas/')

    # save a feature file for each user
    id_target = np.zeros((len(users), 2), dtype=np.float32)
    for idx, uid in enumerate(users):
        user_features = df.loc[df['TERMINALNO'] == uid, feature_names]
        length.append(user_features.shape[0])
        file_name = os.path.join(features_dir, str(idx)+r'.npy')
        np.save(file_name, user_features)
        id_target[idx, 0] = uid
        if target is not None:
            id_target[idx, 1] = df.loc[df['TERMINALNO'] == uid, target].values[0]

    # save the targes file
    targets_file_name = os.path.join(data_dir, 'targets.npy')
    np.save(targets_file_name, id_target)
    return length

def prepare_data(raw_data_path, target_data_dir, process_params=None, target=None):
    df_feat, feature_names, params = extract_feature(raw_data_path, process_params, target=target)
    lens = save_data(df_feat, feature_names, target_data_dir, target=target)
    return params, len(feature_names), lens

def train_test_split(data_path, test_ratio=0.25, random_state=0):

    feature_path = os.path.join(data_path, 'datas')
    data_files = np.array([os.path.join(feature_path, df) for df in os.listdir(feature_path)])
    data_files.sort()
    np.random.seed(random_state)
    np.random.shuffle(data_files)
    k = int(test_ratio*len(data_files))
    train = data_files[k:]
    test = data_files[:k]
    return train, test

def generate_xy(data_files, target_file, x_dim, batch_size=128, max_len=128, x_num=1):

    targets = np.load(target_file)
    if len(data_files) < batch_size:
        batches = 1
        batch_size = len(data_files)
    else:
        batches = len(data_files)//batch_size
    data_files = data_files.copy()
    while True:
        np.random.shuffle(data_files)
        for batch in range(batches):
            x = np.zeros((batch_size, max_len, x_dim), dtype=np.float32)
            y = []
            for idx, file_name in enumerate(data_files[batch:batch+batch_size]):
                user_idx = int(os.path.split(file_name)[1].split(r'.')[0])
                x_values = np.load(file_name)
                x_len = x_values.shape[0]
                # padding
                if x_len < max_len:
                    pad_len = max_len - x_len
                    pad_values = np.zeros((pad_len, x_dim))
                    x_values = np.concatenate([x_values, pad_values])
                # truncating
                if x_len > max_len:
                    trunc_len = x_len - max_len
                    k = np.random.choice(trunc_len)
                    x_values = x_values[k:x_len-trunc_len+k, :]
                x[idx, :, :] = x_values
                prob = targets[user_idx, 1]
                y.append(prob)
            if x_num > 1:
                x = [x]*x_num
            yield x, np.array(y)

def generate_x(data_files, x_dim, batch_size=128, max_len=128, x_num=1):

    data_len = len(data_files)
    if data_len < batch_size:
        batches = 1
    elif (data_len % batch_size) > 0:
        batches = data_len//batch_size+1
    else:
        batches = data_len//batch_size
    while True:
        for batch in range(batches):
            x = np.zeros((batch_size, max_len, x_dim), dtype=np.float32)
            for idx, file_name in enumerate(data_files[batch:min(batch+batch_size, data_len)]):
                user_idx = int(os.path.split(file_name)[1].split(r'.')[0])
                x_values = np.load(file_name)
                # padding
                if x_values.shape[0] < max_len:
                    pad_len = max_len - x_values.shape[0]
                    pad_values = np.zeros((pad_len, x_dim))
                    x_values = np.concatenate([x_values, pad_values])
                # truncating
                if x_values.shape[0] > max_len:
                    trunc_len = x_values.shape[0] - max_len
                    x_values = x_values[trunc_len:, :]
                x[idx, :, :] = x_values
            if x_num > 1:
                x = [x]*x_num
            yield x


import sys
from collections import Counter

N = int(sys.stdin.readline())
L = []

for _ in range(N):
    L.append(int(sys.stdin.readline()))

L.sort()

#average
print(round(sum(L)/N))

#median
print(L[N//2])

#mode
L_mode = Counter(L).most_common(2)
if len(L_mode) > 1 and L_mode[0][1] == L_mode[1][1]:
    print(L_mode[1][0])
else:
    print(L_mode[0][0])

#interval
print(max(L) - min(L))


import json

from odoo import exceptions
from odoo.modules.module import get_resource_path

from odoo.addons.l10n_es_aeat.tests.test_l10n_es_aeat_certificate import (
    TestL10nEsAeatCertificateBase,
)
from odoo.addons.l10n_es_aeat.tests.test_l10n_es_aeat_mod_base import (
    TestL10nEsAeatModBase,
)

class TestL10nEsAeatSiiBase(TestL10nEsAeatModBase, TestL10nEsAeatCertificateBase):
    @classmethod
    def _get_or_create_tax(cls, xml_id, name, tax_type, percentage, extra_vals=None):
        """Helper for quick-creating a tax with an specific XML-ID"""
        tax = cls.env.ref("l10n_es." + xml_id, raise_if_not_found=False)
        if not tax:
            vals = {
                "name": name,
                "type_tax_use": tax_type,
                "amount_type": "percent",
                "amount": percentage,
            }
            if extra_vals:
                vals.update(extra_vals)
            tax = cls.env["account.tax"].create(vals)
            cls.env["ir.model.data"].create(
                {
                    "module": "l10n_es",
                    "name": xml_id,
                    "model": tax._name,
                    "res_id": tax.id,
                }
            )
        return tax

    def _create_and_test_invoice_sii_dict(
        self, inv_type, lines, extra_vals, module=None
    ):
        vals = []
        tax_names = []
        for line in lines:
            taxes = self.env["account.tax"]
            for tax in line[1]:
                if "." in tax:
                    xml_id = tax
                else:
                    xml_id = "l10n_es.{}_account_tax_template_{}".format(
                        self.company.id, tax
                    )
                taxes += self.env.ref(xml_id)
                tax_names.append(tax)
            vals.append({"price_unit": line[0], "taxes": taxes})
        return self._compare_sii_dict(
            "sii_{}_{}_dict.json".format(inv_type, "_".join(tax_names)),
            inv_type,
            vals,
            extra_vals=extra_vals,
            module=module,
        )

    def _compare_sii_dict(
        self, json_file, inv_type, lines, extra_vals=None, module=None
    ):
        """Helper method for creating an invoice according arguments, and
        comparing the expected SII dict with .
        """
        module = module or "l10n_es_aeat_sii_oca"
        domain = [
            ("code", "=", "01"),
            ("type", "=", "sale" if "out" in inv_type else "purchase"),
        ]
        sii_key_obj = self.env["aeat.sii.mapping.registration.keys"]
        vals = {
            "name": "TEST001",
            "partner_id": self.partner.id,
            "invoice_date": "2020-01-01",
            "move_type": inv_type,
            # FIXME: This should be auto-assigned, but not working due to unknown glitch
            "sii_registration_key": sii_key_obj.search(domain, limit=1).id,
            "invoice_line_ids": [],
        }
        for line in lines:
            vals["invoice_line_ids"].append(
                (
                    0,
                    0,
                    {
                        "product_id": self.product.id,
                        "account_id": self.account_expense.id,
                        "name": "Test line",
                        "price_unit": line["price_unit"],
                        "quantity": 1,
                        "tax_ids": [(6, 0, line["taxes"].ids)],
                    },
                )
            )
        if extra_vals:
            vals.update(extra_vals)
        invoice = self.env["account.move"].create(vals)
        result_dict = invoice._get_sii_invoice_dict()
        path = get_resource_path(module, "tests/json", json_file)
        if not path:
            raise Exception("Incorrect JSON file: %s" % json_file)
        with open(path, "r") as f:
            expected_dict = json.loads(f.read())
        self.assertEqual(expected_dict, result_dict)
        return invoice

    @classmethod
    def _create_invoice(cls, move_type):
        return cls.env["account.move"].create(
            {
                "company_id": cls.company.id,
                "partner_id": cls.partner.id,
                "invoice_date": "2018-02-01",
                "move_type": move_type,
                "invoice_line_ids": [
                    (
                        0,
                        0,
                        {
                            "product_id": cls.product.id,
                            "account_id": cls.account_expense.id,
                            "name": "Test line",
                            "price_unit": 100,
                            "quantity": 1,
                        },
                    )
                ],
            }
        )

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.maxDiff = None  # needed for the dict comparison
        cls.partner = cls.env["res.partner"].create(
            {"name": "Test partner", "vat": "ESF35999705"}
        )
        cls.product = cls.env["product.product"].create(
            {"name": "Test product", "sii_exempt_cause": "E5"}
        )
        cls.account_expense = cls.env.ref(
            "l10n_es.%s_account_common_600" % cls.company.id
        )
        cls.invoice = cls._create_invoice("out_invoice")
        cls.company.write(
            {
                "sii_enabled": True,
                "sii_test": True,
                "use_connector": True,
                "vat": "ESU2687761C",
                "sii_description_method": "manual",
                "tax_agency_id": cls.env.ref("l10n_es_aeat.aeat_tax_agency_spain"),
            }
        )

class TestL10nEsAeatSii(TestL10nEsAeatSiiBase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.invoice.action_post()
        cls.invoice.name = "INV001"
        cls.invoice.refund_invoice_ids = cls.invoice.copy()
        cls.user = cls.env["res.users"].create(
            {
                "name": "Test user",
                "login": "test_user",
                "groups_id": [(4, cls.env.ref("account.group_account_invoice").id)],
                "email": "somebody@somewhere.com",
            }
        )
        cls.tax_agencies = cls.env["aeat.tax.agency"].search(
            [("sii_wsdl_out", "!=", False)]
        )

    def test_intracomunitary_customer_extracomunitary_delivery(self):
        """Comprobar venta a un cliente intracomunitario enviada al extranjero.

        Este caso se puede dar cuando una asesoría contable contabiliza facturas
        para otro cliente, en caso de que ese cliente le venda a otro cliente
        intracomunitario pero envíe a una dirección extracomunitaria.

        También se puede dar cuando se instala el módulo `sale` en Odoo. Al instalarlo,
        se añade el campo `partner_shipping_id`, que permite indicar una dirección
        de entrega extracomunitaria para clientes intracomunitarios.
        """
        self._activate_certificate(self.certificate_password)
        eu_customer = self.env["res.partner"].create(
            {
                "name": "French Customer",
                "country_id": self.ref("base.fr"),
                "vat": "FR23334175221",
            }
        )
        fp_extra = self.browse_ref(f"l10n_es.{self.company.id}_fp_extra")
        fp_extra.sii_partner_identification_type = "3"
        invoice = self.invoice.copy(
            {"partner_id": eu_customer.id, "fiscal_position_id": fp_extra.id}
        )
        invoice.action_post()
        sii_info = invoice._get_sii_invoice_dict()
        self.assertEqual(
            sii_info["FacturaExpedida"]["Contraparte"],
            {
                "NombreRazon": "French Customer",
                "IDOtro": {"CodigoPais": "FR", "IDType": "06", "ID": "23334175221"},
            },
        )

    def test_job_creation(self):
        self.assertTrue(self.invoice.invoice_jobs_ids)

    def test_partner_sii_enabled(self):
        company_02 = self.env["res.company"].create({"name": "Company 02"})
        self.env.user.company_ids += company_02
        self.assertTrue(self.partner.sii_enabled)
        self.partner.company_id = company_02
        self.assertFalse(self.partner.sii_enabled)

    def test_get_invoice_data(self):
        mapping = [
            ("out_invoice", [(100, ["s_iva10b"]), (200, ["s_iva21s"])], {}),
            ("out_invoice", [(100, ["s_iva10b"]), (200, ["s_iva0_ns"])], {}),
            (
                "out_invoice",
                [(100, ["s_iva10b", "s_req014"]), (200, ["s_iva21s", "s_req52"])],
                {},
            ),
            (
                "out_refund",
                [(100, ["s_iva10b"]), (100, ["s_iva10b"]), (200, ["s_iva21s"])],
                {},
            ),
            ("out_invoice", [(100, ["s_iva0_sp_i"]), (200, ["s_iva0_ic"])], {}),
            ("out_refund", [(100, ["s_iva0_sp_i"]), (200, ["s_iva0_ic"])], {}),
            ("out_invoice", [(100, ["s_iva_e"]), (200, ["s_iva0_e"])], {}),
            ("out_refund", [(100, ["s_iva_e"]), (200, ["s_iva0_e"])], {}),
            (
                "in_invoice",
                [(100, ["p_iva10_bc", "p_irpf19"]), (200, ["p_iva21_sc", "p_irpf19"])],
                {
                    "ref": "sup0001",
                    "date": "2020-02-01",
                    "sii_account_registration_date": "2020-10-01",
                },
            ),
            (
                "in_refund",
                [(100, ["p_iva10_bc"])],
                {"ref": "sup0002", "sii_account_registration_date": "2020-10-01"},
            ),
            (
                "in_invoice",
                [(100, ["p_iva10_bc", "p_req014"]), (200, ["p_iva21_sc", "p_req52"])],
                {"ref": "sup0003", "sii_account_registration_date": "2020-10-01"},
            ),
            (
                "in_invoice",
                [(100, ["p_iva21_sp_ex"])],
                {"ref": "sup0004", "sii_account_registration_date": "2020-10-01"},
            ),
            (
                "in_invoice",
                [(100, ["p_iva0_ns"]), (200, ["p_iva10_bc"])],
                {"ref": "sup0005", "sii_account_registration_date": "2020-10-01"},
            ),
            # Out invoice with currency
            ("out_invoice", [(100, ["s_iva10b"])], {"currency_id": self.usd.id}),
            # Out invoice with currency and with not included in total amount
            (
                "out_invoice",
                [(100, ["s_iva10b", "s_irpf1"])],
                {"currency_id": self.usd.id},
            ),
            # In invoice with currency
            (
                "in_invoice",
                [(100, ["p_iva10_bc"])],
                {
                    "ref": "sup0006",
                    "sii_account_registration_date": "2020-10-01",
                    "currency_id": self.usd.id,
                },
            ),
            # In invoice with currency and with not included in total amount
            (
                "in_invoice",
                [(100, ["p_iva10_bc", "p_irpf1"])],
                {
                    "ref": "sup0007",
                    "sii_account_registration_date": "2020-10-01",
                    "currency_id": self.usd.id,
                },
            ),
            # Intra-community supplier refund with ImporteTotal with "one side"
            (
                "in_refund",
                [(100, ["p_iva21_sp_in"])],
                {"ref": "sup0008", "sii_account_registration_date": "2020-10-01"},
            ),
        ]
        for inv_type, lines, extra_vals in mapping:
            self._create_and_test_invoice_sii_dict(inv_type, lines, extra_vals)
        return

    def test_action_cancel(self):
        self.invoice.invoice_jobs_ids.state = "started"
        with self.assertRaises(exceptions.UserError):
            self.invoice.button_cancel()

    def test_sii_description(self):
        company = self.invoice.company_id
        company.write(
            {
                "sii_header_customer": "Test customer header",
                "sii_header_supplier": "Test supplier header",
                "sii_description": " | Test description",
                "sii_description_method": "fixed",
            }
        )
        self.invoice._compute_sii_description()
        self.assertEqual(
            self.invoice.sii_description,
            "Test customer header | Test description",
        )
        invoice_temp = self._create_invoice("in_invoice")
        self.assertEqual(
            invoice_temp.sii_description,
            "Test supplier header | Test description",
        )
        company.sii_description = False
        company.sii_description_method = "manual"
        self.invoice.sii_description = "/"
        self.invoice._compute_sii_description()
        self.assertEqual(self.invoice.sii_description, "Test customer header")
        self.invoice.sii_description = "Other thing"
        self.assertEqual(self.invoice.sii_description, "Other thing")
        company.sii_description_method = "auto"
        self.invoice._compute_sii_description()
        self.assertEqual(
            self.invoice.sii_description,
            "Test customer header | Test line",
        )

    def test_vat_number_check(self):
        self.partner.write(
            {"vat": "F35999705", "country_id": self.env.ref("base.es").id}
        )
        # Repeat get invoice data tests to ensure no change is due to the VAT number
        # expressed without country, but setting the country
        self.test_get_invoice_data()

    def _check_binding_address(self, invoice):
        company = invoice.company_id
        tax_agency = company.tax_agency_id
        self.sii_cert.company_id.tax_agency_id = tax_agency
        proxy = invoice._connect_sii(invoice.move_type)
        address = proxy._binding_options["address"]
        self.assertTrue(address)
        if company.sii_test and tax_agency:
            params = tax_agency._connect_params_sii(invoice.move_type, company)
            if params["address"]:
                self.assertEqual(address, params["address"])

    def _check_tax_agencies(self, invoice):
        for tax_agency in self.tax_agencies:
            invoice.company_id.tax_agency_id = tax_agency
            self._check_binding_address(invoice)
        else:
            invoice.company_id.tax_agency_id = False
            self._check_binding_address(invoice)

    def test_tax_agencies_sandbox(self):
        self.sii_cert.company_id = self.invoice.company_id.id
        self._activate_certificate()
        self.invoice.company_id.sii_test = True
        self._check_tax_agencies(self.invoice)
        in_invoice = self._create_invoice("in_invoice")
        self._check_tax_agencies(in_invoice)

    def test_tax_agencies_production(self):
        self.sii_cert.company_id = self.invoice.company_id.id
        self._activate_certificate()
        self.invoice.company_id.sii_test = False
        self._check_tax_agencies(self.invoice)
        in_invoice = self._create_invoice("in_invoice")
        self._check_tax_agencies(in_invoice)

    def test_refund_sii_refund_type(self):
        invoice = self.env["account.move"].create(
            {
                "partner_id": self.partner.id,
                "invoice_date": "2018-02-01",
                "move_type": "out_refund",
            }
        )
        self.assertEqual(invoice.sii_refund_type, "I")

    def test_refund_sii_refund_type_write(self):
        invoice = self.env["account.move"].create(
            {
                "partner_id": self.partner.id,
                "invoice_date": "2018-02-01",
                "move_type": "out_invoice",
            }
        )
        self.assertFalse(invoice.sii_refund_type)
        invoice.move_type = "out_refund"
        self.assertEqual(invoice.sii_refund_type, "I")

    def test_is_sii_simplified_invoice(self):
        self.assertFalse(self.invoice._is_sii_simplified_invoice())
        self.partner.sii_simplified_invoice = True
        self.assertTrue(self.invoice._is_sii_simplified_invoice())

    def test_sii_check_exceptions_case_supplier_simplified(self):
        self.partner.sii_simplified_invoice = True
        invoice = self.env["account.move"].create(
            {
                "partner_id": self.partner.id,
                "invoice_date": "2018-02-01",
                "move_type": "in_invoice",
            }
        )
        with self.assertRaises(exceptions.UserError):
            invoice._sii_check_exceptions()

    def test_sii_check_exceptions_case_supplier_on_post(self):
        """Check sii exceptions when posting supplier bills"""
        supplier = self.supplier.copy()
        supplier.country_id = self.env.ref("base.es")
        supplier.vat = "A46180576"
        # Extra data without `ref` field
        extra_data_wo_ref = {
            "partner_id": supplier.id,
            "invoice_line_ids": [
                (
                    0,
                    0,
                    {
                        "name": "Test line",
                        "account_id": self.accounts["600000"].id,
                        "price_unit": 100.0,
                        "quantity": 1,
                    },
                )
            ],
        }
        with self.assertRaises(exceptions.UserError):
            self._invoice_purchase_create("2018-02-01", extra_vals=extra_data_wo_ref)
        self.assertTrue(
            self._invoice_purchase_create(
                "2018-02-01", extra_vals=dict(extra_data_wo_ref, ref="TEST REF")
            )
        )

    def test_unlink_draft_invoice_when_not_sent_to_sii(self):
        draft_invoice = self.invoice.copy({})
        draft_invoice.unlink()
        self.assertFalse(draft_invoice.exists())

    def test_unlink_invoice_when_sent_to_sii(self):
        self.invoice.sii_state = "sent"
        self.invoice.button_draft()  # Convert to draft to check only SII exception
        with self.assertRaises(exceptions.UserError):
            self.invoice.unlink()

    def test_account_move_sii_write_exceptions(self):
        # out_invoice
        self.invoice.sii_state = "sent"
        with self.assertRaises(exceptions.UserError):
            self.invoice.write({"invoice_date": "2022-01-01"})
        with self.assertRaises(exceptions.UserError):
            self.invoice.write({"thirdparty_number": "CUSTOM"})
        # in_invoice
        in_invoice = self._create_invoice("in_invoice")
        in_invoice.ref = "REF"
        in_invoice.sii_state = "sent"
        partner = self.partner.copy()
        with self.assertRaises(exceptions.UserError):
            in_invoice.write({"partner_id": partner.id})
        with self.assertRaises(exceptions.UserError):
            in_invoice.write({"ref": "REF2"})

    def test_account_move_reversal_out_invoice(self):
        reversal = (
            self.env["account.move.reversal"]
            .with_context(
                active_id=self.invoice.id,
                active_model=self.invoice._name,
                active_ids=self.invoice.ids,
            )
            .create(
                {
                    "journal_id": self.invoice.journal_id.id,
                }
            )
        )
        self.assertEqual(reversal.sii_refund_type, "I")
        self.assertTrue(reversal.sii_refund_type_required)
        self.assertFalse(reversal.supplier_invoice_number_refund_required)