import os
import imageio
from dataloader_util import *

def get_training_poses(basedir, translation=0.0, sc_factor=1.0, trainskip=1):
    all_poses, valid = load_poses(os.path.join(basedir, 'trainval_poses.txt'))

    train_frames = []
    for idx in range(0, len(all_poses), trainskip):
        if valid[idx]:
            train_frames.append(idx)

    all_poses = np.array(all_poses).astype(np.float32)
    training_poses = all_poses[train_frames]

    training_poses[:, :3, 3] += translation
    training_poses[:, :3, 3] *= sc_factor

    return training_poses

def get_num_training_frames(basedir, trainskip):
    poses = get_training_poses(basedir, trainskip=trainskip)

    return poses.shape[0]

def get_intrinsics(basedir, crop):
    depth = imageio.imread(os.path.join(basedir, 'depth_filtered', 'depth0.png'))
    H, W = depth.shape[:2]
    H = H - crop / 2
    W = W - crop / 2
    focal = load_focal_length(os.path.join(basedir, 'focal.txt'))

    return H, W, focal

def load_scannet_data(basedir, trainskip, downsample_factor=1, translation=0.0, sc_factor=1., crop=0):

    # Get image filenames, poses and intrinsics
    img_files = [f for f in sorted(os.listdir(os.path.join(basedir, 'images')), key=alphanum_key) if f.endswith('png')]
    depth_files = [f for f in sorted(os.listdir(os.path.join(basedir, 'depth_filtered')), key=alphanum_key) if f.endswith('png')]
    all_poses, valid_poses = load_poses(os.path.join(basedir, 'trainval_poses.txt'))

    # Train, val and test split
    num_frames = len(img_files)
    train_frame_ids = list(range(0, num_frames, trainskip))

    # Lists for the data to load into
    images = []
    depth_maps = []
    poses = []
    frame_indices = []

    # Read images and depth maps for which valid poses exist
    for i in train_frame_ids:
        if valid_poses[i]:
            img = imageio.imread(os.path.join(basedir, 'images', img_files[i]))
            depth = imageio.imread(os.path.join(basedir, 'depth_filtered', depth_files[i]))

            images.append(img)
            depth_maps.append(depth)
            poses.append(all_poses[i])
            frame_indices.append(i)

    # Map images to [0, 1] range
    images = (np.array(images) / 255.).astype(np.float32)

    # Convert depth to meters, then to "network units"
    depth_shift = 1000.0
    depth_maps = (np.array(depth_maps) / depth_shift).astype(np.float32)
    depth_maps *= sc_factor
    depth_maps = depth_maps[..., np.newaxis]

    poses = np.array(poses).astype(np.float32)
    poses[:, :3, 3] += translation
    poses[:, :3, 3] *= sc_factor

    # Intrinsics
    H, W = depth_maps[0].shape[:2]
    focal = load_focal_length(os.path.join(basedir, 'focal.txt'))

    # Resize color frames to match depth
    images = resize_images(images, H, W)

    # Crop the undistortion artifacts
    if crop > 0:
        images = images[:, crop:-crop, crop:-crop, :]
        depth_maps = depth_maps[:, crop:-crop, crop:-crop, :]
        H, W = depth_maps[0].shape[:2]

    if downsample_factor > 1:
        H = H//downsample_factor
        W = W//downsample_factor
        focal = focal/downsample_factor
        images = resize_images(images, H, W)
        depth_maps = resize_images(depth_maps, H, W, interpolation=cv2.INTER_NEAREST)

    return images, depth_maps, poses, [H, W, focal], frame_indices


from setuptools import setup

def required_packages():
    deps = [
        'pandas',
        'numpy',
        'matplotlib',
        'torch',
        'torchvision',
        'pillow',
        'seaborn',
    ]

setup(name='roofnet',
      version='0.1',
      description='Code for RoofNet',
      url='http://github.com/MishaLaskin/roofnet',
      author='ML',
      author_email='laskin.misha@gmail.com',
      install_requires=required_packages(),
      license='MIT',
      packages=['roofnet'],
      zip_safe=False)


import pandas

from bokeh.io import curdoc
from bokeh.plotting import figure, show, output_file, ColumnDataSource
from bokeh.models import Range1d
from bokeh.models.tools import HoverTool
from bokeh.models.widgets import CheckboxGroup

from motion_detector import df #runs motion_detector.py and grabs the df output

curdoc().theme = 'dark_minimal'

# print(df)

# add tooltip column, datetime as strings
df["Enter_tooltip"]=df["Enter"].dt.strftime("%F %T.%3Nms")
df["Exit_tooltip"]=df["Exit"].dt.strftime("%F %T.%3Nms")
# print(df)

source = ColumnDataSource(df)

TOOLTIPS = [
    ("Enter","@Enter_tooltip"),
    ("Exit","@Exit_tooltip"),
]

hover_tool = HoverTool(tooltips=TOOLTIPS)

f = figure(width=500, height=100, x_axis_type="datetime", sizing_mode="scale_width",
           title="Activity Detected")
f.add_tools(hover_tool)

f.y_range = Range1d(0,1)
f.yaxis.ticker = [0,1]
f.yaxis.visible=False

f.xaxis.axis_label = "Time"
f.xaxis.axis_label_text_font_style = "bold"
f.xaxis.major_tick_line_color = 'black'
f.xaxis.minor_tick_line_color = 'black'

f.xgrid.minor_grid_line_color = '#e5e5e5'
# print(f.xgrid.properties_with_values())

d1=f.quad(left="Enter", right="Exit", top=1, bottom=0,
          source=source, color="red", alpha=0.5)

output_file("graph.html")

show(f)


import paramiko
def run(host='13.127.47.153', port=2200, user='grader',
                  command="mpstat | grep 'all'", bufsize=-1, key_filename='/home/ubuntu/grader_key',
                  timeout=120, pkey=None):
    """
    Excecutes a command using paramiko and returns the result.
    :param host: Host to connect
    :param port: The port number
    :param user: The username of the system
    :param command: The command to run
    :param key_filename: SSH private key file.
    :param pkey: RSAKey if we want to login with a in-memory key
    :return:
    """
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    client.connect(hostname=host, port=port,
            username=user, key_filename=key_filename, banner_timeout=10)
    chan = client.get_transport().open_session()
    chan.settimeout(timeout)
    chan.set_combine_stderr(True)
    chan.get_pty()
    chan.exec_command(command)
    stdout = chan.makefile('r', bufsize)
    stdout_text = stdout.read()
    status = int(chan.recv_exit_status())
    client.close()
    temp_text = ''
    try:
        temp_text = str(stdout_text,'utf-8')
    except:
        status = 1
    return temp_text, status


from flask import Flask, request, jsonify, abort
from dotenv import load_dotenv
import os
import time

if os.getenv("IS_PROD") == "true":
    print("Loading in prod")
else:
    print("Loading in dev")
    load_dotenv(verbose=True)

from chalicelib import log_config
from chalicelib.db import ModelsDatabase
from chalicelib.features import FeatureExtractor
from chalicelib.dao.blobstore.s3.s3blobstore import S3Blobstore
from chalicelib.dao.docstore.dynamo import DynamoDocstore as Docstore

app = Flask(__name__)

access_key = os.getenv('ACCESS_KEY')
secret_key = os.getenv('SECRET_KEY')
bucket_name = os.getenv('PRIMARY_BUCKET')
table_name  = os.getenv('DYNAMO_TABLE')

print(bucket_name, "is the bucket")
blobstore = S3Blobstore(bucket_name, access_key, secret_key)
docstore = Docstore(table_name, access_key, secret_key)

models_db = ModelsDatabase(blobstore, docstore)
fe = FeatureExtractor()

@app.route('/')
def index():
    resp = {
        'model_loading_status': models_db.status,
        'models_loaded' : list(models_db.cache.keys())
    }
    return jsonify(resp)

@app.route('/model/<model_id>/<version>/predict', methods=['POST'])
def predict(model_id, version):
    try:
        req = request.get_json(force=True)
    except Exception as e:
        print(str(e))
        return f'Invalid JSON body \n {str(e)} \n', 500
    t0 = time.time()
    try:
        model = models_db.get_model(model_id, version) # LudwigModel
    except KeyError as e:
        print(e)
        return str(e), 404
    # # features = fe.extract_features(model, req) # TODO: implement this
    t1 = time.time()
    try:
        prediction = model.predict(req, blobstore) #TODO: why a delay the first time?
    except ValueError as e:
        print(e)
        return str(e), 404
    t2 = time.time()
    username = models_db.cache[f'{model_id}/{version}']['metadata'].get('username', 'unknown') # TODO turn this into a method
    model.increment_counter(docstore, username, model_id, version)
    t3 = time.time()
    resp = {
        "time_to_get_model" : t1 - t0,
        "time_to_get_prediction" : t2 - t1,
        "time_to_increment_counter" : t3 - t2,
        "total_response_time" : t3 - t0
    }
    if isinstance(prediction, dict):
        resp = {**resp, **prediction}
    else:
        resp['prediction'] = prediction
    # models_db.save_prediction(resp)
    return jsonify(resp)

@app.route('/reload')
def reload():
    try:
        models_db.reload_models()
    except Exception as e:
        print(str(e))
        return 'reload unsuccessful', 400
    return 'reload successful', 200

@app.route('/model/<model_id>/<version>/remove', methods=['DELETE'])
def remove(model_id, version):
    try:
        resp = models_db.remove_model(model_id, version)
    except Exception as e:
        print(str(e))
        return 'removal unsuccessful', 400
    return resp

@app.route('/model/<model_id>/<version>/load', methods=['GET'])
def load(model_id, version):
    try:
        resp = models_db.load_model(model_id, version)
    except Exception as e:
        print(str(e))
        return 'model load unsuccessful', 500
    return resp

def main():

    app.run(debug=False, host=os.getenv("host"), port=int(os.getenv("PORT")), threaded=True)

if __name__ == '__main__':
    main()


import pygsheets

c = pygsheets.authorize()
s = c.open('Example solar calcs')
ws = s.worksheet('title', 'real_data')

# ws.update_col(2 , ["helloworld"], row_offset=2)
# ws.insert_rows(2, number=2, values=["hellohellohello", "hhhh"], inherit=False)

ws.append_table(["helloworld1"], start='A1', end=None, dimension='ROWS', overwrite=False)
ws.append_table(["helloworld2"], start='A1', end=None, dimension='ROWS', overwrite=False)


import turtle

from geometries import Point, Rectangle, VisualPoint, VisualRectangle
from random import randint

RANDINT_RANGE = 150
POINT_SIZE = 10
POINT_COLOR = "#E921CD"

if __name__ == "__main__":

    ll_x = randint(0, RANDINT_RANGE)
    ur_x = ll_x + randint(0, RANDINT_RANGE)
    ll_y = randint(0, RANDINT_RANGE)
    ur_y = ll_y + randint(0, RANDINT_RANGE)
    lower_left = Point(x = ll_x, y = ll_y)
    upper_right = Point(x = ur_x, y = ur_y)
    rectangle = VisualRectangle(lower_left = lower_left,
        upper_right = upper_right)

    print(f"For this game, imagine a {rectangle}")
    print("Your mission is to guess a point inside this rectangle!")

    user_x = float(input("Please choose an x coordinate for your point: "))
    user_y = float(input("Now please choose a y coordinate for your point: "))
    user_point = VisualPoint(x = user_x, y = user_y,
        size = POINT_SIZE, hex_code = POINT_COLOR)

    if rectangle.contains(user_point):
        print("Congratulations! You Win!")
        print("\nNow, try guessing the area of the rectangle")
        guess = float(input("I think the area is... "))
        if guess == rectangle.area():
            print("Shoop-da-Woop! You Win!")
        else:
            print(f"I'm sorry, the area is actually {rectangle.area()}")

    else:
        print("Woof...Please Try Again...")

    game_canvas = turtle.Turtle()
    rectangle.draw(canvas = game_canvas)
    user_point.draw(canvas = game_canvas)
    turtle.done()


from django.urls import path
from . import views, auth
urlpatterns = [
    path('', views.index),
    path('administrador/', views.administrador),
    path('registro/', auth.registro),
    path('login/', auth.login),
    path('logout/', auth.logout),
    path('mensaje', views.nuevo_mensaje),
    path('comentario/<int:mensaje_id>', views.nuevo_comentario)
]


from cros.factory.probe.lib import cached_probe_function
from cros.factory.utils.arg_utils import Arg
from cros.factory.gooftool import vpd
from cros.factory.utils import type_utils

PARTITION = type_utils.Enum(['ro', 'rw'])

_PARTITION_NAME_MAP = {PARTITION.ro: vpd.VPD_READONLY_PARTITION_NAME,
                       PARTITION.rw: vpd.VPD_READWRITE_PARTITION_NAME}

class VPDFunction(cached_probe_function.LazyCachedProbeFunction):
  """Reads the information from VPD.

  Description
  -----------
  This function probes the VPD data in the firmware by calling the command
  ``vpd``.

  This function supplies 3 modes:
    1. If the user doesn't specify any field name of VPD data to probe
       (i.e. both the arguments ``field`` and ``key`` are `None`), the probed
       result will be a dict contains all fields in the VPD data.
    2. If the user specifies a list of fields (ex: ``fields=['a', 'b', ...]``),
       the probed result will contain only specified fields.  The user also can
       specify the argument ``key='another_key_name'`` to customize the key name
       in the result dict if the specified fields contains only element.

  Examples
  --------
  Let's assume that the read-only VPD partition contains these fields::

    serial=12345
    region=us

  And read-write VPD partition contains::

    k1=v1
    k2=v2
    k3=v3

  And we have the probe config file::

    {
      "all_ro_vpd_data": {  # Simplest example, just dump all fields in RO VPD.
        "from_firmware": {
          "eval": "vpd"
        }
      },

      "region": {
        "from_firmware": {
          "eval": {
            "vpd": {
              "fields": [
                "region"
              ],
              "key": "region_code"  # In this case we rename the key from
                                    # "region" to "region_code".
            }
          }
        }
      },

      "k1k2_rw_vpd_data": {  # In this case we only output k1, k2 from the RW
                             # VPD.
        "from_firmware": {
          "eval": {
            "vpd": {
              "partition": "rw",
              "fields": [
                "k1",
                "k2"
              ]
            }
          }
        }
      }
    }

  Then the corresponding output will be::

    {
      "all_ro_vpd_data": [
        {
          "name": "from_firmware",
          "values": {
            "serial": "12345",
            "region": "us"
          }
        }
      ],

      "region": [
        {
          "name": "from_firmware",
          "values": {
            "region_code": "us"
          }
        }
      ],

      "k1k2_rw_vpd_data": [
        {
          "name": "from_firmware",
          "values": {
            "k1": "v1",
            "k2": "v2"
          }
        }
      ]
    }
  """

  ARGS = [
      Arg('fields', list, 'A list of fields of VPD data to probe.',
          default=None),
      Arg('key', str,
          'The key of the result.  Can be specified only if the `fields` '
          'argument contains exact one element', default=None),
      Arg('partition', PARTITION,
          'The partition name to read, can be either "ro" or "rw"',
          default='ro')
  ]

  def __init__(self, **kwargs):
    super(VPDFunction, self).__init__(**kwargs)

    if self.args.key and len(self.args.fields) != 1:
      raise ValueError('Key remap is only available in single field mode.')

  def Probe(self):
    vpd_data = super(VPDFunction, self).Probe()

    if not self.args.fields:
      return [vpd_data]

    # success only if all fields are found in the vpd data
    if set(self.args.fields) - set(vpd_data.keys()):
      return []

    if self.args.key:
      return [{self.args.key: vpd_data[self.args.fields[0]]}]

    return [{field: vpd_data[field] for field in self.args.fields}]

  def GetCategoryFromArgs(self):
    if self.args.partition not in PARTITION:
      raise cached_probe_function.InvalidCategoryError(
          'partition should be one of %r.' % list(PARTITION))

    return self.args.partition

  @classmethod
  def ProbeDevices(cls, category):
    vpd_tool = vpd.VPDTool()
    vpd_data = vpd_tool.GetAllData(partition=_PARTITION_NAME_MAP[category])

    return vpd_data


# import ttkbootstrap as ttk
# from ttkbootstrap.constants import *
# from ttkbootstrap import utility
from tkinter import Toplevel, Label

class ToolTip:
    def __init__(
        self,
        widget,
        text="widget info",
        bootstyle=None,
        wraplength=None,
        fg=None,
        bg="#f2e9ce",
        **kwargs,
    ):
        """
        Parameters:

            widget (Widget):
                The tooltip window will position over this widget when
                hovering.

            text (str):
                The text to display in the tooltip window.

            wraplength (int):
                The width of the tooltip window in screenunits before the
                text is wrapped to the next line. By default, this will be
                a scaled factor of 300.

            **kwargs (Dict):
                Other keyword arguments passed to the `Toplevel` window.
        """
        self.widget = widget
        self.text = text
        self.bootstyle = bootstyle
        self.wraplength = wraplength
        self.toplevel = None
        self.bg = bg
        self.fg = fg

        # set keyword arguments
        # kwargs["overrideredirect"] = True
        # kwargs["master"] = self.widget
        self.toplevel_kwargs = kwargs

        # event binding
        self.widget.bind("<Enter>", self.show_tip)
        self.widget.bind("<Leave>", self.hide_tip)
        self.widget.bind("<Motion>", self.move_tip)
        self.widget.bind("<ButtonPress>", self.hide_tip)

    def show_tip(self, *_):
        """Create a show the tooltip window"""
        if self.toplevel:
            return

        self.toplevel = Toplevel(**self.toplevel_kwargs)
        self.toplevel.overrideredirect(True)
        self.toplevel.attributes('-alpha', 0.95)
        lbl = Label(
            master=self.toplevel,
            text=self.text,
            justify='left',
            wraplength=self.wraplength,
            background=self.bg,
            foreground=self.fg
        )
        lbl.pack(fill='both', expand=True)

    def move_tip(self, *_):
        """Move the tooltip window to the current mouse position within the
        widget.
        """
        if self.toplevel:
            x = self.widget.winfo_pointerx() + -85
            y = self.widget.winfo_pointery() + -35
            self.toplevel.geometry(f"+{x}+{y}")

    def hide_tip(self, *_):
        """Destroy the tooltip window."""
        if self.toplevel:
            self.toplevel.destroy()
            self.toplevel = None


#!/usr/bin/python3
"""defines a class Square that inherits from Rectangle
so we import the relevant module"""

Rectangle = __import__('9-rectangle').Rectangle

"""creating a subclass Square that inherits from Rectangle"""

class Square(Rectangle):
    """start with th constructor method"""
    def __init__(self, size):
        """to validate size with integer_validator"""
        self.integer_validator("size", size)
        super().__init__(size, size)
        self.__size = size

    """define the str() to print the string representation of square"""
    def __str__(self):
        string = "[" + str(self.__class__.__name__) + "] "
        string += str(self.__size) + "/" + str(self.__size)
        return (string)


file_name = "some_file.txt"
log = "results.txt"

# создадим файл с рандомным IP адресами:
with open(file_name, 'w') as f:
    f.writelines(["8.8.8.8\n", "192.192.192.192\n", "10.29.136.59\n", "127.0.0.2"])

# из этого же файла прочитаем IP адреса
with open(file_name, 'r') as ff:
    data = ff.read().split('\n')
    print(data)
    print(type(data))

ip_dict = dict.fromkeys(data, 0)  # заявляем, что по умолчанию все статусы доступны

print(ip_dict)  # посмотрим, что в словаре

import datetime
import subprocess

while True:
    for IPs in ip_dict.keys():
        status = subprocess.Popen(["ping", "-c", "1", IPs.strip()],
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.STDOUT)
        stdout, stderr = status.communicate()

        if ip_dict[IPs] != status.returncode:
            with open(log, 'a') as dd:
                dd.write(IPs + " status change time: " + str(datetime.datetime.now()) + '\n')
                ip_dict[IPs] = status.returncode
        else:
            pass


import torch
import numpy as np
import torch.multiprocessing as mp
from collections import deque
from ..utils import *

class Replay:
    def __init__(self, memory_size, batch_size):
        self.memory_size = memory_size
        self.batch_size = batch_size
        self.data = []
        self.pos = 0

    def feed(self, experience):
        if self.pos >= len(self.data):
            self.data.append(experience)
        else:
            self.data[self.pos] = experience
        self.pos = (self.pos + 1) % self.memory_size

    def feed_batch(self, experience):
        for exp in experience:
            self.feed(exp)

    def sample(self, batch_size=None):
        if batch_size is None:
            batch_size = self.batch_size

        sampled_indices = [np.random.randint(0, len(self.data)) for _ in range(batch_size)]
        sampled_data = [self.data[ind] for ind in sampled_indices]
        batch_data = list(map(lambda x: np.asarray(x), zip(*sampled_data)))
        return batch_data

    def size(self):
        return len(self.data)

    def empty(self):
        return not len(self.data)

class AsyncReplay(mp.Process):
    FEED = 0
    SAMPLE = 1
    EXIT = 2
    FEED_BATCH = 3
    def __init__(self, memory_size, batch_size):
        mp.Process.__init__(self)
        self.pipe, self.worker_pipe = mp.Pipe()
        self.memory_size = memory_size
        self.batch_size = batch_size
        self.cache_len = 2
        self.start()

    def run(self):
        torch.cuda.is_available()
        replay = Replay(self.memory_size, self.batch_size)
        cache = []
        pending_batch = None

        first = True
        cur_cache = 0

        def set_up_cache():
            batch_data = replay.sample()
            batch_data = [tensor(x) for x in batch_data]
            for i in range(self.cache_len):
                cache.append([x.clone() for x in batch_data])
                for x in cache[i]: x.share_memory_()
            sample(0)
            sample(1)

        def sample(cur_cache):
            batch_data = replay.sample()
            batch_data = [tensor(x) for x in batch_data]
            for cache_x, x in zip(cache[cur_cache], batch_data):
                cache_x.copy_(x)

        while True:
            op, data = self.worker_pipe.recv()
            if op == self.FEED:
                replay.feed(data)
            elif op == self.FEED_BATCH:
                if not first:
                    pending_batch = data
                else:
                    for transition in data:
                        replay.feed(transition)
            elif op == self.SAMPLE:
                if first:
                    set_up_cache()
                    first = False
                    self.worker_pipe.send([cur_cache, cache])
                else:
                    self.worker_pipe.send([cur_cache, None])
                cur_cache = (cur_cache + 1) % 2
                sample(cur_cache)
                if pending_batch is not None:
                    for transition in pending_batch:
                        replay.feed(transition)
                    pending_batch = None
            elif op == self.EXIT:
                self.worker_pipe.close()
                return
            else:
                raise Exception('Unknown command')

    def feed(self, exp):
        self.pipe.send([self.FEED, exp])

    def feed_batch(self, exps):
        self.pipe.send([self.FEED_BATCH, exps])

    def sample(self):
        self.pipe.send([self.SAMPLE, None])
        cache_id, data = self.pipe.recv()
        if data is not None:
            self.cache = data
        return self.cache[cache_id]

    def close(self):
        self.pipe.send([self.EXIT, None])
        self.pipe.close()


# 關鍵字
keyword = input()
# 輸入各行字串，並將其連結
string_list = []
string_keep = input()
while string_keep != "INPUT_END":
    string_list.append(string_keep.strip())
    string_keep = input()
string = " ".join(string_list)

# 定義輸出函數
def printstring(start, string):
    #  第1個index
    if (start - 7) <= 0:
        first = 0
    else:
        first = start - 7
    # 第2個index
    second = start
    # 第3個index
    third = start + len(keyword)
    # 第4個index
    if (third + 7) > len(string):
        fourth = len(string)
    else:
        fourth = third + 7
    # 利用index印出
    print(string[first:second] + "**" + keyword + "**" + string[third:fourth])

# 輸出結果
if string.find(keyword) == -1:
    print("NO_MATCH")

for index in range(len(string)):
    if string[index:index + len(keyword)] == keyword:
        printstring(index, string)


#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2020/4/6 19:23
# @Author: qyh

import numpy as np
import pandas as pd
import re
import jieba
import warnings

warnings.filterwarnings("ignore")

# 将岗位名称标准化：目前岗位名称太多太杂，需要统一
job_list = ['数据分析', "数据统计", "数据专员", '数据挖掘', '算法', '大数据',
            '开发工程师', '运营', '软件工程', '前端开发',
            '深度学习', 'ai', '数据库', '数据库', '数据产品',
            '客服', 'java', '.net', 'android', '人工智能', 'c++',
            '数据管理', "测试", "运维"]
job_list = np.array(job_list)
address_list = ['北京', '上海', '广州', '深圳', '杭州', '苏州', '长沙',
                '武汉', '天津', '成都', '西安', '东莞', '合肥', '佛山',
                '宁波', '南京', '重庆', '长春', '郑州', '常州', '福州',
                '沈阳', '济南', '宁波', '厦门', '贵州', '珠海', '青岛',
                '中山', '大连', '昆山', "惠州", "哈尔滨", "昆明", "南昌", "无锡"]
address_list = np.array(address_list)

def deduplicate(df):
    # 为数据框指定行索引
    df.index = range(len(df))
    df.columns = ["岗位名", "公司名", "工作地点", "工资", "发布日期", "经验与学历", "公司类型", "公司规模",
                  "行业", "工作描述"]
    # 数据去重，如果一个行业的公司名和发布的岗位名一致，我们认为是重复值，因此去除掉。
    print("去重之前的记录数", df.shape)
    df.drop_duplicates(subset=["公司名", "岗位名"], inplace=True)
    print("去重之后的记录数", df.shape)

def get_job_info(df):
    df["岗位名"].value_counts()
    df["岗位名"] = df["岗位名"].apply(lambda x: x.lower())
    # 数据太多太杂，我们需要筛选出我们想要分析的岗位（目标岗位）
    target_job = ['算法', '开发', '分析', '工程师', '数据', '运营', '运维']
    index = [df["岗位名"].str.count(i) for i in target_job]
    index = np.array(index).sum(axis=0) > 0
    job_info = df[index]
    job_info.shape

    return job_info

def handle_job_names(job_info):
    def rename(x=None, name_list=job_list):
        index = [i in x for i in name_list]
        if sum(index) > 0:
            return name_list[index][0]
        else:
            return x

    job_info["岗位名"] = job_info["岗位名"].apply(rename)
    job_info["岗位名"].value_counts()
    # 数据统计、数据专员、数据分析统一归为数据分析
    job_info["岗位名"] = job_info["岗位名"].apply(lambda x: re.sub("数据专员", "数据分析", x))
    job_info["岗位名"] = job_info["岗位名"].apply(lambda x: re.sub("数据统计", "数据分析", x))

def handle_salary(job_info):
    job_info["工资"].str[-1].value_counts()
    job_info["工资"].str[-3].value_counts()

    index1 = job_info["工资"].str[-1].isin(["年", "月"])
    index2 = job_info["工资"].str[-3].isin(["万", "千"])
    job_info = job_info[index1 & index2]

    def get_money_max_min(x):
        try:
            if x[-3] == "万":
                z = [float(i) * 10000 for i in re.findall("[0-9]+\.?[0-9]*", x)]
            elif x[-3] == "千":
                z = [float(i) * 1000 for i in re.findall("[0-9]+\.?[0-9]*", x)]
            if x[-1] == "年":
                z = [i / 12 for i in z]
            return z
        except:
            return x
    salary = job_info["工资"].apply(get_money_max_min)
    job_info["最低工资"] = salary.str[0]
    job_info["最高工资"] = salary.str[1]
    job_info["工资水平"] = job_info[["最低工资", "最高工资"]].mean(axis=1)

def handle_work_place(job_info):
    def rename(x=None, name_list=address_list):
        index = [i in x for i in name_list]
        if sum(index) > 0:
            return name_list[index][0]
        else:
            return x

    job_info["工作地点"] = job_info["工作地点"].apply(rename)

def handle_company_type(job_info):
    job_info.loc[job_info["公司类型"].apply(lambda x: len(x) < 6), "公司类型"] = np.nan
    job_info["公司类型"] = job_info["公司类型"].str[2:-2]

def handle_industry_tag(job_info):
    job_info["行业"] = job_info["行业"].apply(lambda x: re.sub(",", "/", x))
    job_info.loc[job_info["行业"].apply(lambda x: len(x) < 6), "行业"] = np.nan
    job_info["行业"] = job_info["行业"].str[2:-2].str.split("/").str[0]

def handle_exp_education(job_info):
    job_info["学历"] = job_info["经验与学历"].apply(lambda x: re.findall("本科|大专|应届生|在校生|硕士", x))

    def func(x):
        if len(x) == 0:
            return np.nan
        elif len(x) == 1 or len(x) == 2:
            return x[0]
        else:
            return x[2]

    job_info["学历"] = job_info["学历"].apply(func)

def handle_word_desc(job_info):
    with open(r".\stopword.txt", "r") as f:
        stopword = f.read()
    stopword = stopword.split()
    stopword = stopword + ["任职", "职位", " "]

    job_info["工作描述"] = job_info["工作描述"].str[2:-2].apply(lambda x: x.lower()).apply(lambda x: "".join(x)) \
        .apply(jieba.lcut).apply(lambda x: [i for i in x if i not in stopword])
    job_info.loc[job_info["工作描述"].apply(lambda x: len(x) < 6), "工作描述"] = np.nan

def handle_company_scale(job_info):
    def func(x):
        if x == "['少于50人']":
            return "<50"
        elif x == "['50-150人']":
            return "50-150"
        elif x == "['150-500人']":
            return '150-500'
        elif x == "['500-1000人']":
            return '500-1000'
        elif x == "['1000-5000人']":
            return '1000-5000'
        elif x == "['5000-10000人']":
            return '5000-10000'
        elif x == "['10000人以上']":
            return ">10000"
        else:
            return np.nan

    job_info["公司规模"] = job_info["公司规模"].apply(func)

if __name__ == '__main__':
    data = pd.read_csv("./job_info.csv", engine="python", header=None)
    deduplicate(data)
    job_info = get_job_info(data)

    handle_job_names(job_info)
    handle_salary(job_info)
    handle_work_place(job_info)
    handle_company_type(job_info)
    handle_industry_tag(job_info)
    handle_exp_education(job_info)
    handle_word_desc(job_info)
    handle_company_scale(job_info)
    feature = ["公司名", "岗位名", "工作地点", "工资", "发布日期", "学历", "公司类型", "公司规模", "行业", "工作描述"]
    final_df = job_info[feature]
    final_df.to_excel(r".\词云图.xlsx", encoding="gbk", index=None)


# -*- coding: utf-8 -*-
# @Time    :2021/4/26 10:41 上午

from django.http import HttpResponse
from django.shortcuts import render
from TestModel.models import Test
import simplejson as json
from random import randint

# 数据库操作
def insert(request):
    test1 = Test(name='runoob')
    test1.save()
    return HttpResponse("
数据添加成功！
")

def insert_json(request):
    db_name = "db_dsp"
    table_name = "tb_dsp"
    tag = {
        "statement_id": 0,
        "series": [
            {
                "name": "dsp",
                "columns": ["tagKey"],
                "values": [["host"], ["requesttype"], ["status"], ["url"]]
            }
        ]
    }
    tag_desc = {
        "series": [
            {
                "name": "dsp",
                "columns": ["tagKey"],
                "values": [["host", "主机"], ["requesttype", "请求类型"], ["status", "状态码"], ["url", "链接"]]
            }
        ]
    }
    field = {
        "statement_id": 1,
        "series": [
            {
                "name": "dsp",
                "columns": ["fieldKey", "fieldType"],
                "values": [["cost", "float"], ["pv", "float"], ["up_100", "float"], ["up_1000", "float"]]
            }
        ]
    }
    field_desc = {
        "statement_id": 1,
        "series": [
            {
                "name": "dsp",
                "columns": ["fieldKey", "fieldType"],
                "values": [["cost", "float", "开销"], ["pv", "float", "每秒请求量"], ["up_100", "float", "每百次请求数"],
                           ["up_1000", "float", "每千次请求数"]]
            }
        ]
    }
    test1 = Test(name='luffy' + str(randint(1, 1000)), db_name=db_name,table_name=table_name,
                 tag=(tag), tag_desc=(tag_desc),field=(field), field_desc=(field_desc))

    test1.save()
    return HttpResponse("
数据添加成功！
")

def show(request):
    response = ""
    obs = Test.objects.all()
    # for var in test_obs:
    #     # tag:{var.tag}
    #     temp = f"id : {var.id} name: {var.name} \n"
    #     response += temp
    # return HttpResponse("
" + response + "
")

    for ob in obs:
        print(ob.tag)
    context = {
        "name": "show",
        "obs": obs
    }
    return render(request, 'test_db/show.html', context)


from os import scandir, path

source_path = path.abspath(path.join(path.dirname(__file__), '..'))
dirs = [d for d in scandir(source_path) if d.is_dir()]

scriptify = False

for d in dirs:
    for f in scandir(path.join(source_path, d.name)):
        f_name, _ = path.splitext(f.name)
        new_file = path.join(source_path, f_name + '.bat')
        new_path = path.join(source_path, d.name, f.name)
        with open(new_file, 'a+') as file:
            file.seek(0)
            if not file.read(1):
                file.write(f'@echo off\n{d.name} {new_path}')
                print(f'\n{new_file}\n - > @echo off\n - > {d.name} {new_path}\n')
                scriptify = True

if not scriptify:
    print('\nNothing to Scriptify...\n')


# -*-coding:utf-8-*-
import sys

from game.omok.MyOmokParser import MyOMOKParser
from gamebase.client.Client import Client

# HOST = '104.199.218.103'
HOST = '127.0.0.1'
PORT = 9001

client = Client()
if client.connect_server(HOST, PORT) is False:
    print('서버 연결오류')
    sys.exit()

player_parser = MyOMOKParser()
client.set_parser(player_parser)

client.client_run()


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
        answer, nums = None, []

        for list in lists:
            while list:
                nums.append(list.val)
                list = list.next

        for num in sorted(nums)[::-1]:
            answer = ListNode(num, answer)

        return answer


import logging
import re
import subprocess

from timeit import default_timer as timer
import os

BENCHMARK_CONFIG = """
mock_benchmark:
  description: Illustrative mock example benchmark.
  provider: aws
  region: us-east-1
  endpoint: DYNAMIC
"""

def prepare(spec):
    logging.info('prepare(): building and deploying mock benchmark')
    spec['endpoint'] = f"https://my-function.com/{spec['region']}/invoke"
    # Executing shell commands with Python: https://janakiev.com/blog/python-shell-commands/
    # a) os.system(CMD)
    # b) subprocess.run(["ls", "-l"])
    # c) list_dir = subprocess.Popen(["ls", "-l"]); list_dir.wait()
    # Returns a byte object. Decode with: .decode("ascii") or decode("utf-8")
    spec['pwd'] = subprocess.run('pwd', shell=True, stdout=subprocess.PIPE).stdout.decode("utf-8").strip()

def invoke(spec):
    start = timer()
    logging.info('invoke(): invoke app a single time (via HTTP or other trigger)')
    result = re.search("https?://([A-Za-z_0-9.-]+).*", spec['endpoint'])
    logging.info(f"pwd={spec['pwd']}")
    # Suggestion: expose Python object for logging results or log parsing
    logging.info(f"result={result.group(1)}")
    spec.run('mvn --version', image = 'maven:3.6.3-jdk-8-slim')
    # spec.run('echo "Hello from within L2 container" >> test.txt')
    end = timer()
    logging.info(f"time={end - start}")

def cleanup(spec):
    logging.info('cleanup(): delete all functions and other created resources (e.g., test files or databases)')


import importlib
from pathlib import Path

_PROJECTS = {
    "point_rend": "PointRend",
    "deeplab": "DeepLab",
    "panoptic_deeplab": "Panoptic-DeepLab",
}
_PROJECT_ROOT = Path(__file__).parent.parent.parent / "projects"

if _PROJECT_ROOT.is_dir():
    # This is true only for in-place installation (pip install -e, setup.py develop),
    # where setup(package_dir=) does not work: https://github.com/pypa/setuptools/issues/230

    class _D2ProjectsFinder(importlib.abc.MetaPathFinder):
        def find_spec(self, name, path, target=None):
            if not name.startswith("detectron2.projects."):
                return
            project_name = name.split(".")[-1]
            project_dir = _PROJECTS.get(project_name)
            if not project_dir:
                return
            target_file = _PROJECT_ROOT / f"{project_dir}/{project_name}/__init__.py"
            if not target_file.is_file():
                return
            return importlib.util.spec_from_file_location(name, target_file)

    import sys

    sys.meta_path.append(_D2ProjectsFinder())


from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QWidget, QGridLayout, QVBoxLayout, QLabel, QGroupBox, QHBoxLayout, QPushButton, QLineEdit
from board import board

class sideBar(QWidget):
    def __init__(self):
        super().__init__()
        self.font = QFont("Arial", 13)
        self.sideBarVertical = QVBoxLayout()
        # self.sideBarVertical.setSpacing(-2)
        windowHeight = QWidget().height()
        self.setStyleSheet("border: 1px solid black;"
                           "font-size: 15px;")

        self.board = board()

        self.player1 = QGroupBox("Player 1")
        self.player1.setStyleSheet("padding: 6px")
        self.player1Vertical = QVBoxLayout()
        self.player1Color = QLabel()
        self.player1Color.setText("Color: ")
        self.player1Color.setStyleSheet("border: 0px solid black")
        self.player1StonesCaptured = QLabel()
        self.player1StonesCaptured.setText("Stones captured: ")
        self.player1StonesCaptured.setStyleSheet("border: 0px solid black")
        self.player1StonesCapturedValue = QLabel()
        self.player1StonesCapturedValue.setText(str(self.board.getWhiteStonesCaptured()))
        self.player1StonesCapturedValue.setStyleSheet("border: 0px solid black")
        self.player1TerritoriesCaptured = QLabel()
        self.player1TerritoriesCaptured.setText("Territories captured: ")
        self.player1TerritoriesCaptured.setStyleSheet("border: 0px solid black")
        self.player1Vertical.addWidget(self.player1Color)
        self.player1Vertical.addWidget(self.player1StonesCaptured)
        self.player1Vertical.addWidget(self.player1StonesCapturedValue)
        self.player1Vertical.addWidget(self.player1TerritoriesCaptured)
        self.player1.setLayout(self.player1Vertical)
        self.player1.setMinimumHeight(windowHeight / 3)

        self.player2 = QGroupBox("Player 2")
        self.player2.setStyleSheet("padding: 6px")
        self.player2Vertical = QVBoxLayout()
        self.player2Color = QLabel()
        self.player2Color.setText("Color: ")
        self.player2Color.setStyleSheet("border: 0px solid black")
        self.player2StonesCaptured = QLabel()
        self.player2StonesCaptured.setText("Stones captured: ")
        self.player2StonesCaptured.setStyleSheet("border: 0px solid black")
        self.player2TerritoriesCaptured = QLabel()
        self.player2TerritoriesCaptured.setText("Territories captured: ")
        self.player2TerritoriesCaptured.setStyleSheet("border: 0px solid black")
        self.player2Vertical.addWidget(self.player2Color)
        self.player2Vertical.addWidget(self.player2StonesCaptured)
        self.player2Vertical.addWidget(self.player2TerritoriesCaptured)
        self.player2.setLayout(self.player2Vertical)
        self.player2.setMinimumHeight(windowHeight / 3)

        self.extras = QGroupBox("")
        self.extras.setStyleSheet("padding: 6px")
        self.extrasVertical = QVBoxLayout()
        self.roundTimer = QLabel()
        self.roundTimer.setText("Round time: ")
        self.roundTimer.setStyleSheet("border: 0px solid black")
        self.roundTurn = QLabel()
        self.roundTurn.setText("Round turn: ")
        self.roundTurn.setStyleSheet("border: 0px solid black")
        self.buttonGroup = QWidget()
        self.buttonGroup.setStyleSheet("border: 0px solid black")
        self.buttons = QHBoxLayout()
        self.newGameButton = QPushButton("Pass")
        self.newGameButton.setStyleSheet("border: 1px solid black")
        self.newGameButton.clicked.connect(self.passTurn)
        self.resetGameButton = QPushButton("Reset")
        self.resetGameButton.setStyleSheet("border: 1px solid black")
        self.resetGameButton.clicked.connect(self.resetGame)
        self.buttons.addWidget(self.newGameButton)
        self.buttons.addWidget(self.resetGameButton)
        self.buttonGroup.setLayout(self.buttons)
        self.extrasVertical.addWidget(self.roundTimer)
        self.extrasVertical.addWidget(self.roundTurn)
        self.extrasVertical.addWidget(self.buttonGroup)
        self.extras.setLayout(self.extrasVertical)
        self.extras.setMaximumHeight(windowHeight / 3)

        self.sideBarVertical.addWidget(self.player1)
        self.sideBarVertical.addWidget(self.player2)
        self.sideBarVertical.addWidget(self.extras)

        self.setLayout(self.sideBarVertical)

    def passTurn(self):
        self.board.passTurn()

    def resetGame(self):
        self.board.resetGame()


import requests
from bing_maps_api_key import bing_maps_api_key

def get_lat_lon(city, state):
    """
    input: city name, state postal code
    output: latitude, longitude
    Get the latitude and longitude for a certain city in the united states to get its weather data
    """

    #Get the api key
    bing_maps_key = bing_maps_api_key

    #Construct the API URL with city, state, and api key
    url = f"http://dev.virtualearth.net/REST/v1/Locations/US/{state}/{city}/?output=json&key={bing_maps_key}"

    #Send a GET request to the API URL
    response = requests.get(url)

    #If the response is successful extract latitude and longitude data
    if response.ok:
        lat = response.json()['resourceSets'][0]['resources'][0]['geocodePoints'][0]['coordinates'][0]
        lon = response.json()['resourceSets'][0]['resources'][0]['geocodePoints'][0]['coordinates'][1]
        return lat, lon

    #If the response is unsuccesful raise an exception
    else:
        raise Exception('Failed to get latitude and longitude')

def get_office_id(lat, lon):
    """
    input: latitude, longitude
    output: NWS office name, NWS gridpoints X and Y, city name, state name
    Get the NWS office information for a certain latitude and longitude in the United States
    """
    # Construct the API URL for the given latitude and longitude
    url = f'https://api.weather.gov/points/{lat},{lon}'

    # Send a GET request to the API URL
    response = requests.get(url)

    # If the response is successful, extract the office data
    if response.ok:
        office = str(response.json()['properties']['gridId'])
        gridX = response.json()['properties']['gridX']
        gridY = response.json()['properties']['gridY']
        city = response.json()['properties']['relativeLocation']['properties']['city']
        state = response.json()['properties']['relativeLocation']['properties']['state']
        return office, gridX, gridY, city, state

    # If the response is not successful, raise an exception
    else:
        raise Exception('Failed to fetch office information.')

def get_forecast(office, gridX, gridY):
    """
    input: NWS office name, NWS gridpoints X and Y
    output: current forecast data and upcoming forecast data in a string
    Get the current and upcoming forecast for the specified location in the United States
    """

    # Construct the API URL for the given NWS office information
    url = f'https://api.weather.gov/gridpoints/{office}/{gridX},{gridY}/forecast'

    #Send a GET request to the API URL
    response = requests.get(url)

    #If the response is successful, get forecast data
    if response.ok:
        forecast = "\t"+response.json()['properties']['periods'][0]['name']
        forecast += " - "+response.json()['properties']['periods'][0]['detailedForecast']
        forecast += "\n\t"+response.json()['properties']['periods'][1]['name']
        forecast += " - "+response.json()['properties']['periods'][1]['detailedForecast']

        return forecast

    #If the response is not successful, raise an exception
    else:
        raise Exception('Failed to fetch forecast.')

def get_coordinate(coordinate):
    """
    input: string value of the type of coordinate you are asking the user for
    output: float coordinate value
    Ask the user for a coordinate value and ensure the input can be and is used as a float
    """
    while True:
        try:
            coordinate_value = float(input(f"{coordinate.title()}: "))
        except ValueError:
            print(f"Please enter a float value for the {coordinate.lower()}.")
        else:
            return coordinate_value

def get_city():
    """
    input: none
    output: string of a city name specified by the user
    Get a name of a city from the user
    """
    city = str(input("City Name: "))
    return city

def get_state():
    """
    input: none
    output: string of a state postal code specified by the user
    Get the postal code of a state from the user
    """
    state = str(input("Postal Code of State: "))
    return state

def main():
    """
    input: none
    output: none
    Ask the user for a city name and state postal and print out the current and upcoming forecast for the city
    Using rest api's through NWS and Bing Maps
    """
    #Instruct the user and ask for city and state information
    print("Please enter the city and state from which you want to see the weather forecast.")
    city = get_city()
    state = get_state()

    #Get the latitude and longitude
    lat, lon = get_lat_lon(city, state)

    #Get the NWS office information
    office, gridX, gridY, city, state = get_office_id(lat, lon)

    #Get the forecast data.  If it can't be retrieved in three tries stop trying
    x = 0
    while x < 3:
        try:
            forecast = get_forecast(office, gridX, gridY)
        except:
            if x < 2:
                print("Couldn't receive data trying again.")
                x+=1
            else:
                print("Couldn't receive data.  Try entering another location.")
        else:
            break

    #Display the forecast information to the user
    print(f"The current forecast for {city}, {state} is:")
    print(forecast)

main()


def collect(item, inventory):
    if item not in inventory:
        inventory.append(item)

def drop(item, inventory):
    if item in inventory:
        inventory.remove(item)

def combine(item, inventory):
    items = item.split(":")
    if items[0] in inventory:
        inventory.insert((inventory.index(items[0]) + 1), items[1])

def renew(item, inventory):
    if item in inventory:
        inventory.append(inventory.pop(inventory.index(item)))

item_list = input().split(", ")
command = input().split(" - ")
while command[0] != "Craft!":
    if command[0] == "Collect":
        collect(command[1], item_list)
    elif command[0] == "Drop":
        drop(command[1], item_list)
    elif command[0] == "Combine Items":
        combine(command[1], item_list)
    elif command[0] == "Renew":
        renew(command[1], item_list)
    command = input().split(" - ")
print(', '.join([str(elem) for elem in item_list]))


""" This module defines methods to configure an instance of Chrome """
from typing import List, Optional
from blaze.config import Config

def get_chrome_flags(user_data_dir: str, extra_flags: Optional[List[str]] = None) -> List[str]:
    """
    Returns the default flags to run chrome with. Allows extra flags to be incorporated.
    Passing a temporary user_data_dir ensures that Chrome page loads are not affected by
    caches and other settings.
    """
    # https://peter.sh/experiments/chromium-command-line-switches/
    return [
        "--allow-insecure-localhost",
        "--disable-background-networking",
        "--disable-default-apps",
        # '--disable-gpu',
        "--disable-logging",
        # '--disable-renderer-backgrounding',
        # '--disable-threaded-animation',
        # '--disable-threaded-compositing',
        # '--disable-threaded-scrolling',
        # '--disable-threaded-animation',
        "--headless",
        "--ignore-certificate-errors",
        "--no-check-certificate",
        "--no-default-browser-check",
        "--no-first-run",
        "--user-data-dir={}".format(user_data_dir),
        *(extra_flags if extra_flags else []),
    ]

def get_chrome_command(url: str, flags: List[str], config: Config) -> List[str]:
    """ Given a url, flags, and config, return a command to run chrome """
    return [config.chrome_bin, *flags, url]


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import logging
import time
import sys
import numpy as np

_logger = logging.getLogger(__name__)

class Experiment(object):
    """
    Base class for all experiment implementations.
    """
    def __init__(self, args, paths):
        _logger.info("Initializing Experiment (Steps: %i)" %
                     args.steps)
        self.args = args
        self.paths = paths

        # Initialize important stats
        self.time_start = time.time()
        self.time_current = time.time()
        self.episodes = 0
        self.episodes_success = 0
        self.steps_current = 0
        self.steps_episode = 0
        self.reward_total = 0
        self.reward_episode = 0

        # Mersenne Twister pseudo-random number generator
        self.rng = np.random.RandomState(self.args.random_seed)

        # Initialize environment
        if self.args.env == 'LabLimitedActions':
            from lab_rl.environments import LabLimitedActions
            self.env = LabLimitedActions(self.args, self.rng)
        elif self.args.env == 'LabAllActions':
            from lab_rl.environments import LabAllActions
            self.env = LabAllActions(self.args, self.rng)

        # Initialize agent
        if self.args.agent == 'DQNAgent':
            from lab_rl.agents import DQNAgent
            self.agent = DQNAgent(self.args, self.rng, self.env, self.paths)
        elif self.args.agent == 'RandomAgent':
            from lab_rl.agents import RandomAgent
            self.agent = RandomAgent(self.args, self.rng, self.env, self.paths)
        elif self.args.agent == 'DummyAgent':
            from lab_rl.agents import DummyAgent
            self.agent = DummyAgent(self.args, self.rng, self.env, self.paths)
        elif self.args.agent == 'ADAAPT':
            from lab_rl.agents import ADAAPTAgent
            self.agent = ADAAPTAgent(self.args, self.rng, self.env, self.paths)
        else:
            print('No agent:', self.args.agent)
            sys.exit(1)

    def run(self):
        """ Organizes the training of the agent. """
        if not self.args.play:
            # Train the agent
            self.agent.train()
        else:
            self.agent.test(1)


from pathlib import Path

import eyed3

def real_main():
    print("Starting")
    path = Path(".").resolve()
    album_name = path.name
    artist_name = Path(path, "..").resolve().name
    for file in path.iterdir():
        if file.is_file() and file.name.endswith(".mp3"):
            first_part = file.name.split(" ")[0]
            number = None
            try:
                number = int(first_part)
            except ValueError:
                pass

            audio_file = eyed3.load(file)
            # audio_file.tag.artist = ""
            audio_file.tag.album = album_name
            audio_file.tag.album_artist = artist_name
            # audio_file.tag.title = ""
            if number is not None:
                audio_file.tag.track_num = number

            audio_file.tag.save()
    print("Done")

def main():
    try:
        real_main()
    except KeyboardInterrupt:
        print("\nExiting")

if __name__ == '__main__':
    main()


import tkinter as tk

from tkinter.font import Font

class ColorkeyFrame(tk.LabelFrame):
    def __init__(self, options_manager, display_manager, file_manager):
        super().__init__(options_manager, text = "Colour Key", bg = "#333333",
                         fg = "#e8e8e8", relief = tk.FLAT,
                         font = Font(size = 15, weight = "bold", underline = True),
                         padx = 5, pady = 5)
        self.options_manager = options_manager
        self.display_manager = display_manager
        self.file_manager = file_manager
        self.pack(fill = tk.X, side = tk.TOP, pady = 5)
        self.create_widgets()

    def create_widgets(self):
        checkbox_frame = tk.Frame(self, bg = "#333333")
        checkbox_frame.pack(anchor = tk.W)

        self.is_colorkey = tk.IntVar()
        self.checkbox = tk.Checkbutton(checkbox_frame, variable = self.is_colorkey,
                                       onvalue = 1, offvalue = 0,
                                       command = self.update_colorkey, state = tk.DISABLED,
                                       bg = "#333333", relief = tk.FLAT, bd = 0, padx = 0,
                                       activebackground = "#333333", highlightthickness = 0)
        self.checkbox.pack(side = tk.LEFT)

        self.checkbox_label = tk.Label(checkbox_frame, text = "Use Colour Key", bg = "#333333",
                                       fg = "#a7a8a9", font = Font(size = 10), bd = 0, padx = 0)
        self.checkbox_label.pack(side = tk.LEFT)

        color_frame = tk.Frame(self, bg = "#333333")
        color_frame.pack(anchor = tk.W, pady = (5, 5), fill = tk.X)

        self.colors = []
        for i in range(3):
            self.colors.append(tk.Entry(color_frame, relief = tk.FLAT, bg = "#e8e8e8",
                                        disabledbackground = "#a7a8a9"))
            if i != 2:
                right_pad = 3
            else:
                right_pad = 0
            self.colors[i].grid(row = 0, column = i, sticky = "WE", padx = (0, right_pad))
            color_frame.grid_columnconfigure(i, weight = 1)

        self.button = tk.Button(self, text = "Set Colour Key",
                                command = self.update_colorkey, state = tk.DISABLED,
                                relief = tk.FLAT, bg = "#a7a8a9")
        self.button.pack(anchor = tk.W)
        self.disable_colors()

    def disable_colors(self, reset_values = True):
        for color in self.colors:
            if reset_values:
                color.config(state = tk.NORMAL)
                color.delete(0, tk.END)
                color.insert(tk.END, "0")
            color.config(state = tk.DISABLED)
        self.button.config(state = tk.DISABLED, bg = "#a7a8a9")

    def set_colors(self, color):
        for i in range(3):
            self.colors[i].config(state = tk.NORMAL)
            self.colors[i].delete(0, tk.END)
            self.colors[i].insert(tk.END, str(color[i]))
        self.button.config(state = tk.NORMAL, bg = "#e8e8e8")

    def update_colorkey(self):
        if self.is_colorkey.get():
            new_color = []
            for color in self.colors:
                try:
                    new_color.append(int(color.get()))
                    new_color[-1] = max(min(new_color[-1], 255), 0)
                except ValueError:
                    new_color.append(0)
            self.display_manager.set_colorkey(new_color)
            self.file_manager.data["colorkey"] = new_color
            self.file_manager.unsaved = True
            self.set_colors(new_color)
        else:
            self.display_manager.set_colorkey(None)
            self.file_manager.data["colorkey"] = False
            self.file_manager.unsaved = True
            self.disable_colors(False)


import pygame
import rgbcolors
import os

import json
import datetime as dt

SCORE = 0
NAME = 'Player1'
DIFF = 5

class Scene:
    def __init__(self, screen):
        self._screen = screen
        self._background = pygame.Surface(self._screen.get_size())
        self._background.fill(rgbcolors.purple)
        self._is_valid = True

    def draw(self):

        self._screen.blit(self._background, (0, 0))
    def process_event(self, event):

        print(str(event))
        if event.type == pygame.QUIT:
            print('Good Bye!')
            self._is_valid = False
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            print('Bye bye!')
            self._is_valid = False
    def is_valid(self):

        return self._is_valid

    def play_music(self):

        pass
    def stop_music(self):

        pass
    def update(self):

        pass
    def unload_song(self):

        pass

class TitleScene(Scene):
    def __init__(self, screen, title, title_color, title_size):
        super().__init__(screen)
        self._background.fill(title_color)
        press_any_key_font = pygame.font.Font(pygame.font.get_default_font(), 18)
        self._press_any_key = press_any_key_font.render('Press any key.', True, rgbcolors.black)
        (w, h) = self._screen.get_size()
        self._press_any_key_pos = self._press_any_key.get_rect(center=(w/2, h - 50))
        self._image_folder_path = os.getcwd() + '/Images/SNAKE-2.png'
        self._title = pygame.image.load(self._image_folder_path)
        self._title_pos = (225, w/3)

    def draw(self):
        super().draw()
        self._screen.blit(self._title, self._title_pos)
        self._screen.blit(self._press_any_key, self._press_any_key_pos)

    def process_event(self, event):
        super().process_event(event)
        if event.type == pygame.KEYDOWN:
            self._is_valid = False

class InstructionScene(Scene):

    def __init__(self, screen, title, title_color, title_size):
        super().__init__(screen)
        self._background.fill(title_color)
        inst_font = pygame.font.Font(pygame.font.get_default_font(), 17)
        inst_message = "Move the snake using the arrow keys and collect as many apples as possible."
        inst_message2 = "Try to avoid hiting the boudary or the snake crashing into itself."
        self._inst1 = inst_font.render(inst_message, True, rgbcolors.black)
        self._inst2 = inst_font.render(inst_message2, True, rgbcolors.black)
        press_any_key_font = pygame.font.Font(pygame.font.get_default_font(), 18)
        self._press_any_key = press_any_key_font.render('Press e for easy, n for normal, and h for hard.', True, rgbcolors.black)
        (w, h) = self._screen.get_size()
        self._press_any_key_pos = self._press_any_key.get_rect(center = (w / 2, h - 50))
        self._inst1_pos = self._inst1.get_rect(center = (w - 400, h - 700))
        self._inst2_pos = self._inst2.get_rect(center = (w - 400, h - 650))

    def draw(self):
        super().draw()
        self._screen.blit(self._press_any_key, self._press_any_key_pos)
        self._screen.blit(self._inst1, self._inst1_pos)
        self._screen.blit(self._inst2, self._inst2_pos)

    def process_event(self, event):
        global DIFF
        super().process_event(event)
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_e:
                DIFF = 3
                self._is_valid = False

            elif event.key == pygame.K_n:
                DIFF = 5
                self._is_valid = False
            elif event.key == pygame.K_h:
                DIFF = 9
                self._is_valid = False
        elif event.type == pygame.QUIT:
            pygame.quit()

class GameLevel(Scene):

    def __init__(self, screen, snake, apple):
        super().__init__(screen)
        self._background.fill(rgbcolors.aquamarine)
        self._screen = screen
        self._snake = snake
        self._apple = apple
        global SCORE
        score_font = pygame.font.Font(pygame.font.get_default_font(), 30)
        self._score = score_font.render(f'Score:  {SCORE}', True, rgbcolors.yellow)
        self._score_pos = self._score.get_rect(center = (600, 20))
        self._folder_path = os.getcwd() + '/Sounds/'
        self._sound = pygame.mixer.Sound(self._folder_path + 'eatingsound.wav')
        self._sound2 = pygame.mixer.Sound(self._folder_path + 'crash.wav')
        (w, h) = screen.get_size()
        self._dimension = (w/16, h/16)
        self._boundary_rect = pygame.Rect((0, 0), (w, h))

    def draw_border(self):
        (w, h) = self._screen.get_size()
        pygame.draw.rect(self._screen, rgbcolors.red, self._boundary_rect, (w//100), (h//200))

    def draw(self):
        super().draw()
        self._snake.draw()
        self._apple.draw()
        self._screen.blit(self._score, self._score_pos)
        self.draw_border()

    def process_event(self, event):
        super().process_event(event)
        self._snake.process_event(event)

    def write_json(self ,filename, name, score):
        time = 0
        date = dt.date.today()
        d = {'name': name, 'score':score, 'time': str(time), 'date': str(date) }

        print(d)
        with open(filename, 'r+') as fh:
            file_data = json.load(fh)
            file_data.append(d)
            fh.seek(0)
            json.dump(file_data, fh, indent = 2)

    def update(self):

        global SCORE
        global DIFF
        global NAME
        self._snake.move()
        clock = pygame.time.Clock()
        clock.tick(DIFF)
        if pygame.Rect.collidepoint(self._apple._avatar, (self._snake._snake_body[0]['x'], self._snake._snake_body[0]['y'])):
            self._sound.play()
            self._apple.apple_reset()
            self._snake.grow()
            SCORE += 1
            score_font = pygame.font.Font(pygame.font.get_default_font(), 30)
            self._score = score_font.render(f'Score:  {SCORE}', True, rgbcolors.yellow)
            self._score_pos = self._score.get_rect(center = (600, 20))

    def is_valid(self):
        if self._snake.detect_off_screen() or self._snake.detect_snake_collision():
            self._sound2.play()
            self.write_json('scores.json', NAME, SCORE)
            return False
        else:
            return True

    def play_music(self):
        pygame.mixer.music.load(self._folder_path + 'game_music.wav')
        pygame.mixer.music.play(-1)
    def stop_music(self):
        pygame.mixer.music.stop()
        pygame.mixer.music.unload()

class GameOverScreen(Scene):
    def __init__(self, screen, title, title_color, title_size):
        super().__init__(screen)
        global SCORE
        self._background.fill(rgbcolors.red)
        title_font = pygame.font.Font(pygame.font.get_default_font(), title_size)
        self._title = title_font.render(title, True, rgbcolors.gray)
        press_any_key_font = pygame.font.Font(pygame.font.get_default_font(), 18)
        self._press_any_key = press_any_key_font.render('Press any Key. Your Score: ' + str(SCORE), True, rgbcolors.black)
        (w, h) = self._screen.get_size()
        self._title_pos = self._title.get_rect(center=(w/2, h/2))
        self._press_any_key_pos = self._press_any_key.get_rect(center=(w/2, h - 50))
        self._folder_path = os.getcwd() + '/Sounds'

    def draw(self):
        super().draw()
        self._screen.blit(self._title, self._title_pos)
        self._screen.blit(self._press_any_key, self._press_any_key_pos)

    def process_event(self, event):
        super().process_event(event)
        if event.type == pygame.KEYDOWN:
            self._is_valid = False

    def play_music(self):
        pygame.mixer.music.load(self._folder_path+'/game_over_sound.wav')
        pygame.mixer.music.play()

    def stop_music(self):
        pass
        pygame.mixer.music.stop()
        pygame.mixer.music.unload()
    def update(self):
        global NAME
        (w, h) = self._screen.get_size()
        press_any_key_font = pygame.font.Font(pygame.font.get_default_font(), 18)
        self._press_any_key = press_any_key_font.render(NAME + '\'s  Score: ' + str(SCORE), True, rgbcolors.black)
        self._title_pos = self._title.get_rect(center=(w/2, h/2))

class enterNameScreen(Scene):
    def __init__(self, screen, title, title_color, title_size):
        super().__init__(screen)

        self._background.fill(rgbcolors.purple)
        self._base_font = pygame.font.Font(pygame.font.get_default_font(), 40)
        self._user_text = ''
        self._text_surface = self._base_font.render(self._user_text, True, rgbcolors.black)
        self._input_rect = pygame.Rect(100, 400, 500, 50)
        self._active = False

        message = 'Click on the box and enter your name to keep'
        message2 = 'track of your scores.Press enter when done.'
        self._prompt_font = pygame.font.Font(pygame.font.get_default_font(), 30)
        self._prompt1 = self._prompt_font.render(message, True, rgbcolors.white)
        self._prompt2 = self._prompt_font.render(message2, True, rgbcolors.white)

    def draw(self):
        super().draw()
        pygame.draw.rect(self._screen, rgbcolors.white, self._input_rect)
        self._input_rect.w = max(500,self._text_surface.get_width() + 10)
        self._text_surface = self._base_font.render(self._user_text, True, rgbcolors.black)
        self._screen.blit(self._text_surface, (self._input_rect.x + 5, self._input_rect.y + 5))
        self._screen.blit(self._prompt1, (50, 300))
        self._screen.blit(self._prompt2, (50, 350))

    def process_event(self,event):
        global NAME
        super().process_event(event)
        if event.type == pygame.MOUSEBUTTONDOWN:
            if self._input_rect.collidepoint(event.pos):
                self._active = True

        if event.type == pygame.KEYDOWN:
            if self._active:
                if event.key == pygame.K_BACKSPACE:
                    self._user_text = self._user_text[:-1]
                elif event.key == pygame.K_RETURN:
                    if not self._user_text:
                        NAME = 'Player1'
                    else:
                        NAME = self._user_text
                    self._is_valid = False
                else:
                    self._user_text += event.unicode
        self.draw()

    def update(self):
        pass
    def play_music(self):
        pass
    def stop_music(self):
        pass
class highScores(Scene):
    def __init__(self, screen, title, title_size):
        super().__init__(screen)

        self._background.fill(rgbcolors.grey)
        self._high_score_font = pygame.font.Font(pygame.font.get_default_font(), 30)

    def draw(self):
        super().draw()
        high_score_text = self._high_score_font.render("HIGH SCORES: ", True, rgbcolors.red)
        self._screen.blit(high_score_text,(1,1))

        with open('scores.json', 'r') as fh:
            self._scores = json.load(fh)
        self._scores = sorted(self._scores, key = lambda i: i['score'], reverse= True)

        scores_limit = 10
        i = 1

        for a in self._scores:
            high_score_text = self._high_score_font.render( str(i)+ ". " + a["name"]  + "      "+str(a["score"]),True,rgbcolors.black )
            self._screen.blit(high_score_text, (50,i*70))
            i+= 1
            if i > scores_limit:
                break


from django.contrib import admin
from modelodjango.turmas.models import Turma

class MatriculaInline(admin.TabularInline):  # tbm tem o 'StackedInline'
    model = Turma.alunos.through
    extra = 1  # por default vao aparecer 3 campos(para adicionar 3 matriculas por vez, mas vc pode alterar)
    readonly_fields = ('data',)   # campos que vao aparecer mas nao poderam ser editados
    autocomplete_fields = ('usuario',)  # campo de buscar por nome
    ordering = ('-data',)  # ordenação decrescente por data.

@admin.register(Turma)
class TurmaAdmin(admin.ModelAdmin):  # a classe TurmaAdmin será responsavel por administrar o modelo Turma
    inlines = [MatriculaInline]
    list_display = ('nome', 'slug', 'inicio', 'fim')
    prepopulated_fields = {'slug': ('nome',)}
    ordering = ('-inicio',)  # ordenar de forma decrescente de acordo com o 'inicio'.


from typing import List, cast

from traffic.core.flightplan import (
    Airway,
    CoordinatePoint,
    Direct,
    FlightPlan,
    Point,
    SpeedLevel,
    _ElementaryBlock,
)
from traffic.data.basic.airways import Airways

import pandas as pd

class ExtraData(Airways):
    name = "extra_for_test"
    available = True

    def __init__(self) -> None:
        self._data = pd.DataFrame.from_records(
            [
                {
                    "route": "LACOU5ALFBO",
                    "navaid": "TOU",
                    "id": 1,
                    "latitude": 43.680833333333304,
                    "longitude": 1.30972222222222,
                },
                {
                    "route": "LACOU5ALFBO",
                    "navaid": "ALIVA",
                    "id": 2,
                    "latitude": 43.933055555555605,
                    "longitude": 1.14694444444444,
                },
                {
                    "route": "LACOU5ALFBO",
                    "navaid": "LACOU",
                    "id": 3,
                    "latitude": 44.296666666666695,
                    "longitude": 0.904444444444444,
                },
                {
                    "route": "ROXOG1HEGLL",
                    "navaid": "ROXOG",
                    "id": 1,
                    "latitude": 50.266111111111094,
                    "longitude": -1.56222222222222,
                },
                {
                    "route": "ROXOG1HEGLL",
                    "navaid": "AMTOD",
                    "id": 2,
                    "latitude": 50.5377777777778,
                    "longitude": -1.4288888888888898,
                },
                {
                    "route": "ROXOG1HEGLL",
                    "navaid": "BEGTO",
                    "id": 3,
                    "latitude": 50.7625,
                    "longitude": -1.23555555555556,
                },
                {
                    "route": "ROXOG1HEGLL",
                    "navaid": "HAZEL",
                    "id": 4,
                    "latitude": 51.0052777777778,
                    "longitude": -0.984444444444444,
                },
                {
                    "route": "ROXOG1HEGLL",
                    "navaid": "*2LK",
                    "id": 5,
                    "latitude": 51.172777777777796,
                    "longitude": -0.6855555555555559,
                },
                {
                    "route": "ROXOG1HEGLL",
                    "navaid": "OCK",
                    "id": 6,
                    "latitude": 51.305,
                    "longitude": -0.447222222222222,
                },
            ]
        )

extra_data = ExtraData()

flightplans: List[FlightPlan] = [
    # Local flight plan, LFBO to EGLL: easy
    FlightPlan(
        "N0456F340 LACOU5A LACOU UM184 CNA UN863 MANAK UY110 REVTU UP87 ROXOG ROXOG1H",  # noqa: E501
        "LFBO",
        "EGLL",
    ),
    # With speed and unit changes, LFPG to RJBB
    FlightPlan(
        "N0495F320 RANUX3D RANUX UN858 VALEK/N0491F330 UM163 DIK UN853 ARCKY DCT NVO DCT BERIM DCT BIKRU/N0482F350 DCT VEDEN DCT NETNA DCT TUKMA L738 MEGAS/K0893F350 P989 TRAFA R492 ESAPA R959 LERTA G814 ABODI/K0883F370 G814 SADER T580 ADMUR G117 XV R229 BA R104 ABK A308 TR P982 DARNO/K0898S1130 A575 UDA B339 POLHO/K0905S1130 W33 UKDUM W201 UNSEK A326 DONVO G597 AGAVO/N0489F370 Y644 EGOBA G597 LANAT/N0491F370 Y597 MIHOU Y361 SAEKI Y36 ALISA ALISAB",  # noqa: E501
        "LFPG",
        "RJBB",
    ),
    # Europe (EHAM) to US, with coordinate points
    FlightPlan(
        "N0458F320 BERGI UL602 SUPUR UP1 GODOS P1 ROLUM P13 ASKAM L7 SUM DCT PEMOS/M079F320 DCT 62N010W 63N020W 63N030W 64N040W 64N050W DCT CLAVY DCT BERUS DCT FEDDY DCT 61N070W 60N080W 57N090W/N0456F340 55N096W 53N100W DCT GGW/N0457F360 DCT BIL DCT JAC NORDK6",  # noqa: E501
        "EHAM",
        "KSLC",
    ),
    # With some \n in between
    FlightPlan(
        "N0427F230 DET1J DET L6 DVR L9 KONAN/N0470F350 UL607 MATUG UZ660 BOREP DCT ENITA DCT ETVIS DCT ABUDO/N0470F370 Z37 BUDEX DCT PESAT DCT TEGRI L605 NEKUL P975 ARTAT UP975 ERGUN UL124 ASVOD/N0468F390 UL124 KONUK UM860 RENGI UT253 OTKEP UM688 RATVO/N0470F390 UM688 SIDAD/N0475F390 UP975 SESRA M677 RABAP UM677 OBNET M677 LOVEM L562 SERSA P307 PARAR N571 SUGID",  # noqa: E501
        "EGLL",
        "VABB",
    ),
    # LFPG, confusion between AS and *AS
    FlightPlan(
        "N0463F350 ERIXU3B ERIXU UN860 ETAMO UZ271 ADEKA UT18 AMLIR/N0461F370 UT18 BADAM UZ151 FJR UM731 DIVKO UM989 BALEN UM998 KAMER/N0465F370 UM998 BOD UR978 ATAFA/N0469F370 UR978 ERKEL/N0473F370 UR978 AS UA604 KOKAM KOKAM1V",  # noqa: E501
        "LFPG",
        "FKYS",
    ),
    # EDDF to MMUN
    FlightPlan(
        "N0459F320 OBOKA UZ29 TORNU DCT RAVLO Y70 OTBED L60 PENIL M144 BAGSO DCT RINUS DCT GISTI/M079F330 DCT MALOT/M079F340 DCT 54N020W 55N030W 54N040W 51N050W DCT ALLRY/N0463F360 DCT YQX DCT ELERI/M079F360 DCT AVAST DCT DRYED M201 TILED DCT ONGOT/M079F380 M202 OMALA DCT SNAGY/N0453F380 DCT RAMJT A699 PERMT DCT FIS B646 CANOA UB879 NOSAT NOSAT1A",  # noqa: E501
        "EDDF",
        "MMUN",
    ),
]

def test_direct() -> None:
    assert Direct.valid("DCT")
    assert not Direct.valid("N0450F340")
    assert type(_ElementaryBlock.parse("DCT")) is Direct

def test_speedlevel() -> None:
    assert SpeedLevel.valid("N0450F340")
    sl = cast(SpeedLevel, _ElementaryBlock.parse("N0450F340"))
    assert sl is not None
    assert sl.speed == 450
    assert sl.altitude == 340

def test_airway() -> None:
    assert Airway.valid("L888")
    assert not Airway.valid("123456789")
    a = Airway("L888")
    assert a.name == "L888"
    # weird bug on Travis, commenting for now...
    # r = a.get()
    # assert r is not None
    # assert "SANLI" in r.navaids
    # assert r.project_shape().length > 2.9e6

def test_point() -> None:
    assert Point.valid("NARAK")
    p = Point("NARAK")
    assert p.name == "NARAK"
    # weird bug on Travis, commenting for now...
    # n = p.get()
    # assert n is not None
    # assert n.name == "NARAK"

    assert CoordinatePoint.valid("61N070W")
    c = CoordinatePoint("61N070W")
    assert c.lat == 61
    assert c.lon == -70
    n = c.get()
    assert n is not None
    assert n.latlon == (61, -70)

def test_flightplan() -> None:
    for fp in flightplans:
        elts = fp.decompose()

        assert any(isinstance(p, SpeedLevel) for p in elts)
        # we can parse everything
        assert all(p is not None for p in elts)
        for cur_, next_ in zip(elts, elts[1:]):
            # never two consecutive airways
            assert not isinstance(cur_, Airway) or not isinstance(next_, Airway)
            # never two consecutive navaids (coordinate points are ok though)
            assert not isinstance(cur_, Point) or not isinstance(next_, Point)

def test_points() -> None:
    one_fp = flightplans[0]
    main_points = {"TOU", "LACOU", "CNA", "MANAK", "REVTU", "ROXOG", "OCK"}
    extra_points = {
        "*2LK",
        "ADILU",
        "ALIVA",
        "AMTOD",
        "BAKUL",
        "BEGTO",
        "BOLRO",
        "CHALA",
        "DIXAD",
        "HAZEL",
        "NTS",
        "REN",
        "TIRAV",
        "UPALO",
    }
    points = set(x.name for x in one_fp.points)
    assert main_points.intersection(points) == points  # HACK
    if main_points != points:
        # weird stuff on travis only...
        print(main_points, points)

    points = set(x.name for x in one_fp.all_points)
    all_points = main_points.union(extra_points)

    assert points == all_points.intersection(points)  # HACK
    if all_points != points:
        # weird stuff on travis only...
        print(all_points, points)


"""Contains useful functions for plotting neural network architecture."""
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np

import context
import plot_utils
from tensorno.layers import MaxOut, ChannelOut

def group_nodes(x: float, nodes: int, groups: int, ax):
    """Plots rectangles around each group of competitors in a LWTA layer.

    Args:
        x (float): x-position of the layer on ax.
        nodes (int): Number of nodes in the layer.
        groups (int): Number of groups in the layer.
        ax (ax): plt ax object on which to plot.

    Returns:
        tuple: ax object on which the function plotted.
    """
    competitors = nodes // groups
    for group in range(groups):
        ax.add_patch(plt.Rectangle((x-0.1,
                                    group*competitors - nodes/2 - 0.3),
                                   0.2, competitors-0.4,
                                   fc='none',
                                   ec=plot_utils.colors[-1],
                                   lw=1.2,
                                   clip_on=False))

    return ax

def plot_nodes(layers: list, ax=None):
    """Plots all the nodes in every layer in layers, grouping LWTA groups.

    Args:
        layers (list): list of tf.keras.layers.Layer instances.
        ax (ax, optional): plt ax on which to plot. Defaults to None.

    Returns:
        tuple: ax object on which the function plotted.
    """
    if ax is None:
        _, ax = plt.subplots()
        ax.set_facecolor("white")
        ax.set_xticks([])
        ax.set_yticks([])

    x = 0  # Position to plot current layer
    for layer in layers:
        try:
            nodes = layer.units
        except AttributeError:
            continue

        for node in np.arange(-nodes/2, nodes/2):
            ax.scatter([x], [node], color=plot_utils.colors[0])

        if isinstance(layer, (MaxOut, ChannelOut)):
            group_nodes(x, nodes, layer.num_groups, ax=ax)

        x += 1
    return ax

def plot_active_nodes(layers: list, isactive: list, ax=None):
    """Plots all the nodes in every layer in layers, grouping LWTA
    and indicating whether the nodes are active or not.

    Args:
        layers (list): list of tf.keras.layers.Layer instances.
        isactive (list): list of boolean np.ndarrays indicating whether node
                         is active or not by layer.
        ax (ax, optional): plt ax on which to plot. Defaults to None.

    Returns:
        tuple: ax object on which the function plotted.
    """
    if ax is None:
        _, ax = plt.subplots()
        ax.set_facecolor("white")
        ax.set_xticks([])
        ax.set_yticks([])

    x = 0  # Position to plot current layer
    for layer, active in zip(layers, isactive):
        colors = np.where(active, "r", "b")
        try:
            nodes = layer.units
        except AttributeError:
            continue
        for node, color in zip(np.arange(-nodes/2, nodes/2), colors):
            ax.scatter([x], [node], c=color)

        if isinstance(layer, (MaxOut, ChannelOut)):
            group_nodes(x, nodes, layer.num_groups, ax=ax)

        x += 1
    return ax

def plot_pathways(layers: list, isactive: list, ax=None, **plot_kwargs):
    """Plots lines going through all active nodes of all layers.

    Args:
        layers (list): list of tf.keras.layers.Layer instances.
        isactive (list): list of boolean np.ndarrays indicating whether node
                         is active or not by layer.
        ax (ax, optional): plt ax on which to plot. Defaults to None.

    Returns:
        tuple: ax object on which the function plotted.
    """
    if ax is None:
        _, ax = plt.subplots()
        ax.set_facecolor("white")
        ax.set_xticks([])
        ax.set_yticks([])

    line_kwargs = dict(
        color=plot_utils.colors[1],
        lw=1,
        alpha=0.05
    )
    line_kwargs.update(plot_kwargs)

    active_nodes_old = [np.nan]

    x = 0  # Position to plot current layer
    for layer, active in zip(layers, isactive):
        try:
            nodes = layer.units
        except AttributeError:
            continue
        active_nodes_new = np.where(active,
                                    np.arange(-nodes/2, nodes/2),
                                    np.nan)
        for old_node in active_nodes_old:
            for new_node in active_nodes_new:
                ax.plot([x-1, x], [old_node, new_node], **line_kwargs)
        active_nodes_old = active_nodes_new

        x += 1
    return ax


from validator import Validator, DictField, IntegerField, create_validator

class BorderValidator(Validator):
    width = IntegerField()
    color = IntegerField()

class RectangleValidator(Validator):
    width = IntegerField()
    height = IntegerField()
    border = DictField(validator=BorderValidator)

class ShapeValidator(Validator):
    rectangle = DictField(validator=RectangleValidator)

def test_ok():
    data = {'rectangle': {'width': 10, 'height': 50, 'border': {'width': 1, 'color': 0xff0000}}}
    v = ShapeValidator(data)
    assert v.is_valid()

def test_ok_2():
    data = {'rectangle': {}}
    v = ShapeValidator(data)
    assert v.is_valid()

def test_data():
    data = {'rectangle': {'width': 10, 'height': 50, 'border': {'width': 1, 'color': 0xff0000}}}
    v = ShapeValidator(data)
    assert v.is_valid()

    validated_data = v.validated_data
    assert data == validated_data

    data['rectangle']['border']['width'] = 2
    assert validated_data['rectangle']['border']['width'] != 2

def test_create_valiadtor():
    data = {
        'rectangle': {
            'type': 'dict',
            'validator': {
                'width': {
                    'type': 'integer',
                },
                'height': {
                    'type': 'integer',
                },
                'border': {
                    'type': 'dict',
                    'validator': {
                        'width': {
                            'type': 'integer'
                        },
                        'color': {
                            'type': 'integer'
                        }
                    }
                }
            },
        }
    }
    V = create_validator(data)
    assert issubclass(V, Validator)

    data = {'rectangle': {'width': 10, 'height': 50, 'border': {'width': 1, 'color': 0xff0000}}}
    v = V(data)
    assert v.is_valid()


from abc import ABC, abstractmethod
import requests
import json
import os

SJ_TOKEN: str = os.getenv('SJ_TOKEN')

class ApiJobSites(ABC):
    """
    Абстрактный класс для работы с API сайтов.
    """

    @abstractmethod
    def get_vacancy(self):
        """
        Получение вакансий с НН и SJ по API.
        :param keyword: Слово для поиска вакансий.
        :return: список вакансий.
        """
        pass

class HeadHunterAPI(ApiJobSites):
    HH_API = 'https://api.hh.ru/vacancies'

    def __init__(self, keyword: str):
        self.params: dict = {
            'text': keyword,
            'area': '1',
            'per_page': 50,
        }

    def get_vacancy(self):
        req = requests.get(self.HH_API, self.params)
        data = req.content.decode()
        json_data = json.loads(data)['items']
        return json_data

class SuperJobApi(ApiJobSites):
    SJ_API = 'https://api.superjob.ru/2.0/vacancies/'

    def __init__(self, keyword: str):
        self.params: dict = {
            'keyword': keyword,
            'count': 50,
            'town': 'Москва',
        }

    def get_vacancy(self):
        headers = {
            'X-Api-App-Id': SJ_TOKEN
        }
        req = requests.get(self.SJ_API, headers=headers, params=self.params)
        data = req.content.decode()
        json_data = json.loads(data)['objects']
        return json_data


import tensorflow as tf
from tensorflow.contrib.layers.python.layers import layers
from ..configuration import *
from .text_classification_model_simple import ModelSimple
from .text_classification_train import main

class ModelSimpleCNN(ModelSimple):
    """
    Text classification using convolution layers before the stack of recurrent GRU cells
    """

    def rnn(self, sequence, sequence_length, max_length, dropout, batch_size, training,
            num_hidden=TC_MODEL_HIDDEN, num_layers=TC_MODEL_LAYERS,
            cnn_filters=TC_CNN_FILTERS, cnn_layers=TC_CNN_LAYERS):

        conv_sequence = sequence
        for _ in range(cnn_layers):
            conv_sequence = layers.convolution(conv_sequence, cnn_filters, [5], activation_fn=None)

        # Recurrent network.
        cells = []
        for _ in range(num_layers):
            cell = tf.nn.rnn_cell.GRUCell(num_hidden)
            if training:
                cell = tf.nn.rnn_cell.DropoutWrapper(cell, output_keep_prob=dropout)
            cells.append(cell)
        network = tf.nn.rnn_cell.MultiRNNCell(cells)
        type = sequence.dtype

        sequence_output, _ = tf.nn.dynamic_rnn(network, conv_sequence, dtype=tf.float32,
                                               sequence_length=sequence_length,
                                               initial_state=network.zero_state(batch_size, type))
        # get last output of the dynamic_rnn
        sequence_output = tf.reshape(sequence_output, [batch_size * max_length, num_hidden])
        indexes = tf.range(batch_size) * max_length + (sequence_length - 1)
        output = tf.gather(sequence_output, indexes)
        return output

if __name__ == '__main__':
    main(ModelSimpleCNN(), 'cnn', batch_size=TC_BATCH_SIZE)


import ast
import socket
import threading

conns = []
stop = False

def main():
    """Запуск сервера - центра клиентов"""
    host = socket.gethostname()
    port = 5000
    serv = socket.socket()
    serv.bind((host, port))
    serv.listen(100)
    global conns

    def sender():
        """Отправляет команды клиентам"""
        global stop
        while not stop:
            global conns
            message = input(">>> ")
            if message == 'exit':
                stop = True
                serv.close()
                break
            commands = message.split(maxsplit=2)
            if commands[0] == 'add':
                add(message)
            elif commands[0] == 'get' or commands[0] == 'del':
                get_and_delete(message)
            elif commands[0] == 'exists':
                exists(message)
            elif commands[0] == 'save' or commands[0] == 'change' \
                    or commands[0] == 'clone':
                save_change_clone(message)
            elif commands[0] == 'keys' or commands[0] == 'values':
                keys_values(message)
            elif commands[0] == 'all':
                get_all(message)
            elif commands[0] == 'size':
                size(message)
            else:
                for i in range(len(conns)):
                    conns[i].send(message.encode())
                for j in range(len(conns)):
                    try:
                        data = conns[j].recv(1024)
                        if data:
                            print(data.decode())
                    except socket.error as e:
                        if e.errno == 10053:
                            conns.pop(j)
                            print("Подключено пользователй:", len(conns))
                        else:
                            raise

    def reliable_send(mess: bytes) -> None:
        """
        Функция отправки данных в сокет
        Обратите внимание, что данные ожидаются сразу типа bytes
        """
        # Разбиваем передаваемые данные на куски максимальной длины 0xffff (65535)
        global conns
        for chunk in (mess[_:_ + 0xffff] for _ in range(0, len(mess), 0xffff)):
            conns[i].send(len(chunk).to_bytes(2, "big"))  # Отправляем длину куска (2 байта)
            conns[i].send(chunk)  # Отправляем сам кусок
            conns[i].send(b"\x00\x00")  # Обозначаем конец передачи куском нулевой длины

    def acceptor():
        """Постоянно принимает новых клиентов"""
        global stop
        while not stop:
            global conns
            conn, address = serv.accept()
            conns.append(conn)
            print(f"Connection from: {address}")
            print("Подключено пользователй:", len(conns))

    t1 = threading.Thread(target=acceptor)
    t1.start()

    storage_name = input("Please write modules name: ")
    if storage_name:
        for i in range(len(conns)):
            conns[i].send(storage_name.encode())

    for i in range(len(conns)):
        try:
            data = conns[i].recv(1024)
            if data:
                print(data.decode())
        except socket.error as e:
            if e.errno == 10053:
                conns.pop(i)
                print("Подключено пользователй:", len(conns))
            else:
                raise

    t2 = threading.Thread(target=sender)
    t2.start()

    def add(message):
        global conns
        sizes = {}
        for i in range(len(conns)):
            conns[i].send('size'.encode())
        for j in range(len(conns)):
            try:
                data = conns[j].recv(1024).decode()
                if data:
                    sizes[conns[j]] = int(data)
            except socket.error as e:
                if e.errno == 10053:
                    conns.pop(j)
                    print("Подключено пользователй:", len(conns))
                else:
                    raise
        to_client = max(sizes, key=sizes.get)
        to_client.send(message.encode())
        dat = to_client.recv(1024)
        print(dat.decode())

    def get_and_delete(message):
        global conns
        gets = []
        for i in range(len(conns)):
            conns[i].send(message.encode())
        for j in range(len(conns)):
            try:
                data = conns[j].recv(1024).decode()
                if data:
                    gets.append(data)
            except socket.error as e:
                if e.errno == 10053:
                    conns.pop(j)
                    print("Подключено пользователй:", len(conns))
                else:
                    raise
        if not [i for i in gets if i != gets[0]]:
            print(gets[0])
        else:
            for x in gets:
                if not x.startswith('There is no data with the key'):
                    print(x)

    def exists(message):
        global conns
        exists = []
        for i in range(len(conns)):
            conns[i].send(message.encode())
        for j in range(len(conns)):
            try:
                data = conns[j].recv(1024).decode()
                if data:
                    exists.append(data)
            except socket.error as e:
                if e.errno == 10053:
                    conns.pop(j)
                    print("Подключено пользователй:", len(conns))
                else:
                    raise
        if not [i for i in exists if i != exists[0]]:
            print(exists[0])
        else:
            for x in exists:
                if not x.startswith('There is no key'):
                    print(x)

    def save_change_clone(message):
        global conns
        gets = []
        for i in range(len(conns)):
            conns[i].send(message.encode())
        for j in range(len(conns)):
            try:
                data = conns[j].recv(1024).decode()
                if data:
                    gets.append(data)
            except socket.error as e:
                if e.errno == 10053:
                    conns.pop(j)
                    print("Подключено пользователй:", len(conns))
                else:
                    raise
        if not [i for i in gets if i != gets[0]]:
            print(gets[0])

    def keys_values(message):
        global conns
        keys = []
        for i in range(len(conns)):
            conns[i].send(message.encode())
        for j in range(len(conns)):
            try:
                data = conns[j].recv(1024).decode()
                if data:
                    new_data = data.split()
                    keys = keys + new_data
            except socket.error as e:
                if e.errno == 10053:
                    conns.pop(j)
                    print("Подключено пользователй:", len(conns))
                else:
                    raise
        print(keys)

    def get_all(message):
        global conns
        all = {}
        for i in range(len(conns)):
            conns[i].send(message.encode())
        for j in range(len(conns)):
            try:
                data = conns[j].recv(1024).decode()
                if data:
                    new_data = ast.literal_eval(data)
                    all.update(new_data)
            except socket.error as e:
                if e.errno == 10053:
                    conns.pop(j)
                    print("Подключено пользователй:", len(conns))
                else:
                    raise
        print(all)

    def size(message):
        global conns
        size = 0
        for i in range(len(conns)):
            conns[i].send(message.encode())
        for j in range(len(conns)):
            try:
                data = conns[j].recv(1024).decode()
                if data:
                    size = size + int(data)
            except socket.error as e:
                if e.errno == 10053:
                    conns.pop(j)
                    print("Подключено пользователй:", len(conns))
                else:
                    raise
        print(size)

if __name__ == '__main__':
    main()


import pandas as pd

from nltk.tokenize import word_tokenize
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn import model_selection

if __name__ == '__main__':
    df = pd.read_csv('imdb.csv')

    df['kfold'] = -1

    df = df.sample(frac=1).reset_index(drop=True)

    y = df.label.values

    kf = model_selection.StratifiedKFold(n_splits=5)

    for f, (t_, v_) in enumerate(kf.split(X=df,y=y)):
        df.loc[v_, 'kfold'] = f

    for fold_ in range(5):

        test_df = df[df.kfold == fold_].reset_index(drop=True)
        train_df = df[df.kfold != fold_].reset_index(drop=True)

        tfidf_vec = TfidfVectorizer(tokenizer=word_tokenize, token_pattern=None, ngram_range=(2,2))
        tfidf_vec.fit(train_df.text)

        Xtrain = tfidf_vec.transform(train_df.text)
        Xtest = tfidf_vec.transform(test_df.text)

        model = LogisticRegression()
        model.fit(Xtrain, train_df.label)

        preds = model.predict(Xtest)
        accuracy = metrics.accuracy_score(test_df.label, preds)

        print(f"kfold : {fold_}")
        print(f"accuracy : {accuracy}")
        print("")


import logging
from telegram import Update
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from datetime import datetime
from fpdf import FPDF
import os

# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.INFO)

# Telegram bot token (replace 'YOUR_BOT_TOKEN' with your bot token)
TOKEN = '6082028232:AAG0tngUrjsf7HvPO_HOFddVr2Uiw2o5J8s'

class UnicodePDF(FPDF):
    def header(self):
        pass

    def footer(self):
        pass

def start(update: Update, context):
    context.bot.send_message(chat_id=update.effective_chat.id,
                             text="Welcome to the Attendance Bot!")

# Register the start command handler
start_handler = CommandHandler('start', start)

def help(update: Update, context):
    context.bot.send_message(chat_id=update.effective_chat.id,
                             text="""
                                This bot allows users to mark their attendance by sending "+" or "-" messages.
                                It collects attendance information such as user name, date, and arrival/departure times,
                                and stores it in a PostgreSQL database. The attendance data can be retrieved and
                                displayed as a PDF report.""")

help_handler = CommandHandler('help', help)

def collect_attendance(update: Update, context):
    message_text = update.message.text.strip()
    user = update.message.from_user
    now = datetime.now()
    current_time = now.strftime("%H:%M")
    current_date = now.strftime("%Y-%m-%d")

    if message_text == '+':
        try:
            # Store the attendance record
            # You can add your logic here to store the data in the desired format or database
            context.bot.send_message(chat_id=update.effective_chat.id,
                                     text="Attendance recorded successfully.")
        except Exception as error:
            logging.error("Error while recording attendance: %s", str(error))
            context.bot.send_message(chat_id=update.effective_chat.id,
                                     text="Failed to record attendance.")

    elif message_text == '-':
        try:
            # Store the exit time record
            # You can add your logic here to store the data in the desired format or database
            context.bot.send_message(chat_id=update.effective_chat.id,
                                     text="Exit time recorded successfully.")
        except Exception as error:
            logging.error("Error while recording exit time: %s", str(error))
            context.bot.send_message(chat_id=update.effective_chat.id,
                                     text="Failed to record exit time.")

collect_attendance_handler = MessageHandler(Filters.text & (~Filters.command), collect_attendance)

def generate_attendance_table(update: Update, context):
    now = datetime.now()
    current_date = now.strftime("%Y-%m-%d")

    # Generate the PDF table
    pdf = UnicodePDF()
    pdf.add_page()
    pdf.set_font('Arial', 'B', 12)
    pdf.cell(40, 10, 'User')
    pdf.cell(40, 10, 'Date')
    pdf.cell(40, 10, 'Entry Time')
    pdf.cell(40, 10, 'Exit Time', ln=True)

    try:
        # Fetch attendance data from the database or any other source
        # You can add your logic here to fetch the attendance data
        # and populate the PDF table

        # Save the PDF file
        filename = f"attendance_{current_date}.pdf"
        pdf.output(filename)

        # Send the PDF file to the user
        context.bot.send_document(chat_id=update.effective_chat.id, document=open(filename, 'rb'))

        # Delete the PDF file
        os.remove(filename)
    except Exception as error:
        logging.error("Error while generating attendance table: %s", str(error))
        context.bot.send_message(chat_id=update.effective_chat.id, text="Failed to generate table.")

generate_attendance_table_handler = CommandHandler('generate_table', generate_attendance_table)

def main():
    # Create an instance of the Updater class
    updater = Updater(token=TOKEN, use_context=True)
    dispatcher = updater.dispatcher

    # Register the handlers
    dispatcher.add_handler(start_handler)
    dispatcher.add_handler(help_handler)
    dispatcher.add_handler(collect_attendance_handler)
    dispatcher.add_handler(generate_attendance_table_handler)

    # Start the bot
    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()