"""
Listens for joinees to connect to the creator.
"""
import socket
import threading
from typing import List, Optional
import pickle

from dchat.ledger.ledger import Ledger
from dchat.ledger.ledger_entry import LedgerEntry
from dchat.actions.action import Action
from dchat.message.message import Message
from dchat.message.command import Command
from dchat.message.dtype import DType
from dchat.utils import constants
from dchat.utils import encode

class ListenClient(Action):
    """
    Listens for new joinees to be connected to the creator,
    and also listens for messages from the joinees.
    """

    def __init__(self):
        super().__init__()
        self.client_list = None

    @staticmethod
    def __send_ledger_entry(client: socket.socket, ledger_entry: LedgerEntry) -> None:
        encode.send_msg_with_end_token(
            cmd=Command.LEDGER_ENTRY,
            dtype=DType.LEDGER_IP,
            msg=ledger_entry.ip_address,
            client=client,
        )

        encode.send_msg_with_end_token(
            cmd=Command.LEDGER_ENTRY,
            dtype=DType.LEDGER_NICKNAME,
            msg=ledger_entry.nick_name,
            client=client,
        )

        encode.send_msg_with_end_token(
            cmd=Command.LEDGER_ENTRY,
            dtype=DType.LEDGER_TIMESTAMP,
            msg=str(ledger_entry.timestamp),
            client=client,
        )

        encode.send_msg_with_end_token(
            cmd=Command.LEDGER_ENTRY,
            dtype=DType.LEDGER_DADDR,
            msg=ledger_entry.daddr,
            client=client,
        )

    def __send_ledger(self, client: socket.socket, ledger: Ledger) -> None:
        for entry in ledger.ledger:
            self.__send_ledger_entry(client=client, ledger_entry=entry)

        message: Message = Message(Command.STOP_SEND, DType.NONE, "")
        client.send(message.serialize())

    def __update_ledger(self, client: socket.socket, ledger_entry: LedgerEntry) -> None:
        self.__send_ledger_entry(client=client, ledger_entry=ledger_entry)

    def __listen_for_client_messages(
            self,
            client: socket.socket
    ) -> None:
        is_message_remaining: bool = False
        current_message: bytes = b""
        msg_len: int = 0
        message_obj: Optional[Message] = None

        while True:
            message: bytes = client.recv(1024)
            messages: List[bytes] = message.split(b"<END>")

            for message in messages:
                if len(message) == 0:
                    continue

                if not is_message_remaining:
                    msg_len = int(message[: constants.MSG_HEADER_LENGTH])
                    remaining_message: bytes = message[constants.MSG_HEADER_LENGTH:]

                    if len(remaining_message) == msg_len:
                        message_obj = pickle.loads(remaining_message)
                        current_message = b""
                    else:
                        current_message += remaining_message
                        is_message_remaining = True
                else:
                    current_message += message
                    is_message_remaining = False

                    if len(current_message) == msg_len:
                        message_obj = pickle.loads(current_message)
                        current_message = b""

                if message_obj:
                    if message_obj.cmd == Command.MSG:
                        print(message_obj.msg)

                        for client_socket in self.client_list:
                            if client_socket != client:
                                encode.send_msg_with_end_token(
                                    cmd=Command.MSG,
                                    dtype=DType.MSG,
                                    msg=message_obj.msg,
                                    client=client_socket,
                                )
                    message_obj = None

    def start(self) -> None:
        """
        Starts the creator, accepts connections and starts listening for messages.
        """
        print("Server is listening...")

        server: Optional[socket.socket] = self._thread_values.get("server")
        clients: Optional[Ledger] = self._thread_values.get("clients")
        self.client_list: Optional[List[socket.socket]] = self._thread_values.get(
            "client_list"
        )

        assert server is not None
        assert clients is not None
        assert self.client_list is not None

        while True:
            client, address = server.accept()
            print(f"Connected with {str(address)}")

            nickname: str = client.recv(1024).decode()

            client_ledger: LedgerEntry = LedgerEntry(
                ip_address=address[0], nick_name=nickname
            )
            clients.add_entry(ledger_entry=client_ledger)

            print("Clients connected: ")
            print(clients)

            self.__send_ledger(client=client, ledger=clients)

            for i, client_send in enumerate(self.client_list):
                self.__update_ledger(client_send, client_ledger)
                print(f"Sent to: {clients[i + 1]}")

            self.client_list.append(client)

            listen_message_thread = threading.Thread(
                target=self.__listen_for_client_messages, args=(client,)
            )
            listen_message_thread.start()


name = "sai"
age = 16

# using expressions
print(f"my name is: {name} and age {age}")
print(f"my name is: {name:10} and age {age:10d}")

# str() , repr()
t = (1,"test")
print(str(t))

#
x = 65
# !a ascii !s str()   !r  repr()
print(f"my name is: {x!a} and age {age}")
# using format
print("{0} and {1}".format(name,age))

# create math table
table = 5
for x in range(1,11):
    print("{0:3d} X {1:3d} = {2:3d}".format(table, x, table*x))

#writing to file
with open('c:\\temp\\test.txt','w') as f:
    f.writelines(["first line","second line"])

# read to file
with open('c:\\temp\\test.txt','r') as f:
    for l in f.readlines():
        print(l)

# JSON
l = ["one", "two", "three"]
import json
print(json.dumps(l))
with open('c:\\temp\\test.txt','w') as f:
    f.write(json.dumps(l))


from pymongo import MongoClient
client = MongoClient('localhost', 27017)

db = client.test

posts = db.posts

post_data_1 = {
	'title': 'Python and MongoDB',
    'content': 'PyMongo is fun, you guys',
    'author': 'Scott'
}

post_data_2 = {
    'title': 'Virtual Environments',
    'content': 'Use virtual environments, you guys',
    'author': 'Scott'
}

post_data_3 = {
    'title': 'Learning Python',
    'content': 'Learn Python, it is easy',
    'author': 'Bill'
}

res = posts.insert(post_data_1)

print('The First Post: '.format(res.inserted_id))

new_res = posts.insert([post_data_1,post_data_2, post_data_3])

print('All of the posts: '.format(new_res.inserted_ids))


from django.urls import path
from . import views

app_name = 'productos'

urlpatterns = [
    path("", views.Listado, name="listado"),
    path("crear", views.CrearProducto, name="crearproducto"),
    path("<int:id_producto>/modificar", views.ModificarProducto, name="modificarproducto"),
    path("<int:id_producto>/eliminar", views.EliminarProducto, name="eliminarproducto"),
]


import re
import html
import jieba
import jieba.analyse
from sklearn.metrics.pairwise import cosine_similarity

class CosineSimilarity(object):

    #余弦相似度

    @staticmethod
    def extract_keyword(content):  # 提取关键词
        # 正则过滤 html 标签
        re_exp = re.compile(r'(<style>.*?</style>)|(<[^>]+>)', re.S)
        content = re_exp.sub(' ', content)
        # html 转义符实体化
        content = html.unescape(content)
        # 切割
        seg = [i for i in jieba.cut(content, cut_all=True) if i != '']
        # 提取关键词
        keywords = jieba.analyse.extract_tags("|".join(seg), topK=200, withWeight=False)
        return keywords

    @staticmethod
    def one_hot(dict, keywords):  # oneHot编码
        lens = len(dict)
        code = [0]*lens
        for word in keywords:
            code[dict[word]] += 1
        return code

    def __init__(self, x, y):
        self.s1 = x
        self.s2 = y

    def main(self):
        # 编码
        dict = {}
        i = 0
        # 去除停用词
        jieba.analyse.set_stop_words(r'C:\test\stopwords.txt')

        # 提取关键词
        keywords1 = self.extract_keyword(self.s1)
        keywords2 = self.extract_keyword(self.s2)
        # 词的并集
        union = set(keywords1).union(set(keywords2))
        for word in union:
            dict[word] = i
            i += 1
        # oneHot编码
        code1 = self.one_hot(dict, keywords1)
        code2 = self.one_hot(dict, keywords2)
        # 余弦相似度计算
        sample = [code1, code2]
        # 除零处理
        try:
            sim = cosine_similarity(sample)
            return sim[1][0]
        except Exception as e:
            print(e)
            return 0.0

            # 测试
if __name__ == '__main__':
    with open(r'C:\test\orig.txt', 'r',encoding='utf-8') as x, open(r'C:\test\orig_0.8_add.txt', 'r',encoding='utf-8') as y:
        content_x = x.read()
        content_y = y.read()
        similarity = CosineSimilarity(content_x, content_y)
        similarity = similarity.main()
        result = similarity*100
        print('相似度: %.1f%%' % result)
        f3 = open(r'C:\test\result.txt','w')
        f3.write('文本相似度:'+str(result)+'%')


import ssl
from bs4 import BeautifulSoup
#import requests
from urllib.request import Request, urlopen

#note some site block the urllib library to protect from bot/spider so
# we use known browser agent like mozilla , chrome and so on.
# in this code too we need that so here is alterntive for that
req = Request(
     url= 'https://www.scrapethissite.com/pages/simple/',
     headers = {'User-Agent': 'Mozilla/5.0'}
     )

html = urlopen(req).read()
soup = BeautifulSoup(html,'html.parser')
all = soup('h3')

for tag in all:
    print(tag.text.strip())


from py2neo import Graph
from py2neo import Node
from py2neo import NodeMatcher
from py2neo import Relationship

'''
创建节点
文件提供了创建节点的八个的函数
'''

graph = Graph("bolt: // localhost:7687", auth=("neo4j", "12345678"))

# 创建user的函数
def create_user(infos):
    for info in infos:
        # Uid，性别，年龄，邮箱
        user_node = Node('user', Uid=info[0], gender=info[1], age=info[2], email=info[3])
        graph.create(user_node)

# 创建application的函数
def create_application(infos):
    for info in infos:
        # Aid,名称，版本，开发者，简述，占用空间大小
        # 外键 databaseserver.DBSid
        application_node = Node('application', Aid=info[0], name=info[1], version=info[2], dev=info[3], profile=info[4],
                                size=info[5])
        graph.create(application_node)

        # 外键处理
        matcher = NodeMatcher(graph)
        databaseserver_node = matcher.match("databaseserver", DBSid=info[-1])
        if len(databaseserver_node) == 0:
            continue
        App_DBS = Relationship(application_node, 'App_DBS', databaseserver_node.first())
        graph.create(App_DBS)

# 创建category的函数
def create_category(infos):
    for info in infos:
        # Cid,名称,说明
        category_node = Node('category', Cid=info[0], name=info[1], profile=info[2])
        graph.create(category_node)

# 创建databaseserver的函数
def create_databaseserver(infos):
    for info in infos:
        # DBSid,名称,IP地址,DBMS类型,版本,处理器类型,内存容量
        databaseserver_node = Node('databaseserver', DBSid=info[0], name=info[1], IP=info[2], DB_class=info[3],
                                   version=info[4], pro_class=info[5], size=info[6])
        graph.create(databaseserver_node)

# 创建loadbalancer的函数
def create_loadbalancer(infos):
    for info in infos:
        # LBid,名称,IP地址,端口,负载分发算法,SSL加速,会话保持
        # 外键rack.Rid
        loadbalancer_node = Node('loadbalancer', LBid=info[0], name=info[1], IP=info[2], port=info[3], load=info[4],
                                 SSL=info[5], session=info[6])
        graph.create(loadbalancer_node)

        # 外键处理
        matcher = NodeMatcher(graph)
        rack_node = matcher.match("rack", Rid=info[-1])
        if len(rack_node) == 0:
            continue
        LdB_Rack = Relationship(loadbalancer_node, 'LdB_Rack', rack_node.first())
        graph.create(LdB_Rack)

# 创建virtualmachine的函数
def create_virtualmachine(infos):
    for info in infos:
        # VMid，名称，版本，访问控制，认证配置、容错设置
        virtualmachine_node = Node('virtualmachine', VMid=info[0], name=info[1], version=info[2], visitcontrol=info[3],
                                   auth=info[4], allowerror=info[5])
        graph.create(virtualmachine_node)

        # 外键处理
        matcher = NodeMatcher(graph)
        server_node = matcher.match("server", Sid=info[-1])
        if len(server_node) == 0:
            continue
        VM_Srv = Relationship(virtualmachine_node, 'VM_Srv', server_node.first())
        graph.create(VM_Srv)

# 创建rack的函数
def create_rack(infos):
    for info in infos:
        # Rid，名称，位置，容量
        rack_node = Node('rack', Rid=info[0], name=info[1], address=info[2], capacity=info[3])
        graph.create(rack_node)

# 创建server的函数
def create_server(infos):
    for info in infos:
        # Sid，名称，IP地址，操作系统，处理器类型、内存容量
        # rack.Rid
        server_node = Node('server', Sid=info[0], name=info[1], IP=info[2], operationsystem=info[3], processor=info[4],
                           memoryspace=info[5])
        graph.create(server_node)

        # 外键处理
        matcher = NodeMatcher(graph)
        rack_node = matcher.match("rack", Rid=info[-1])
        if len(rack_node) == 0:
            continue
        server_Rack = Relationship(server_node, 'server_Rack', rack_node.first())
        graph.create(server_Rack)


from typing import Dict, List, Callable, Tuple

from PySide6.QtGui import Qt
from PySide6.QtWidgets import QTableWidget, QTableWidgetItem

from api.registerdevice.models.device import Device
from gui.actions.models import (SyncTableWithRegistryParams,
                                TableWidgetCallbackIdentifiers,
                                BatteryLevelToNotify,
                                BatteryNotificationParams)
from gui.notifications import notify_device_charged

DEVICE_NAME_COLUMN = 0
BATTERY_LEVEL_COLUMN = 1
BATTERY_LEVEL_TO_NOTIFY_COLUMN = 2
_DEFAULT_BATTERY_LEVEL_TO_NOTIFY = 80

_FilteredDevicesInfo = Tuple[Dict[str, Device], List[int], List[int]]

def _apply_table_widget_item_styles(item: QTableWidgetItem):
    def _set_cell_as_immutable(item: QTableWidgetItem):
        item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)

    _set_cell_as_immutable(item)
    item.setTextAlignment(Qt.AlignCenter)

def sync_shared_cache_with_gui(devices_table: QTableWidget,
                               sync_table_with_registry_params: SyncTableWithRegistryParams):
    (registered_devices_dict,
     updating_devices_info,
     evicting_devices_row_id) = _identify_evicting_and_registered_devices(devices_table,
                                                                          sync_table_with_registry_params)

    remove_related_entries: Callable[[int], None] = sync_table_with_registry_params.callbacks.get_callback(
        TableWidgetCallbackIdentifiers.REMOVE_MAP_ENTRIES_ASSOCIATED_TO_TABLE_ROW_ID)

    _remove_devices_from_table_and_related_maps(devices_table, evicting_devices_row_id, remove_related_entries)
    _update_devices_from_table_and_related_maps(devices_table, updating_devices_info, sync_table_with_registry_params)
    _insert_new_devices_to_table_and_related_maps(devices_table, registered_devices_dict,
                                                  sync_table_with_registry_params)

def _identify_evicting_and_registered_devices(devices_table: QTableWidget,
                                              sync_table_with_registry_params: SyncTableWithRegistryParams) -> _FilteredDevicesInfo:
    registered_devices_dict: Dict[str, Device] = {}
    updating_devices_info: List[int] = []
    evicting_devices_row_id: List[int] = []

    no_of_devices_in_table: int = devices_table.rowCount()

    device_registry = sync_table_with_registry_params.devices_registry
    table_id_device_map = sync_table_with_registry_params.table_id_device_map

    if no_of_devices_in_table <= 0:
        return registered_devices_dict, updating_devices_info, evicting_devices_row_id

    for i in range(no_of_devices_in_table):
        device_id: str = table_id_device_map[i]

        if device_id not in device_registry:
            evicting_devices_row_id.append(i)
            continue

        registered_devices_dict[device_id] = device_registry[device_id]

        device_in_registry: Device = device_registry[device_id]
        device_battery_level = _get_device_battery_level_from_table(devices_table, i)

        if device_battery_level == device_in_registry.battery_level:
            continue

        updating_devices_info.append(i)

    return registered_devices_dict, updating_devices_info, evicting_devices_row_id

def _remove_devices_from_table_and_related_maps(devices_table: QTableWidget,
                                                evicting_devices_row_id: List[int],
                                                remove_related_entries: Callable) -> None:
    for row_id in evicting_devices_row_id:
        _remove_device_from_table(devices_table, row_id)
        remove_related_entries(row_id)

def _update_devices_from_table_and_related_maps(devices_table: QTableWidget,
                                                updating_devices_info: List[int],
                                                sync_table_with_registry_params: SyncTableWithRegistryParams):
    devices_registry = sync_table_with_registry_params.devices_registry
    table_id_device_map = sync_table_with_registry_params.table_id_device_map

    for row_id in updating_devices_info:
        device_id: str = table_id_device_map[row_id]
        device_in_registry: Device = devices_registry[device_id]
        updated_battery_level: str = device_in_registry.battery_level

        _update_device_battery_in_table(devices_table, row_id, updated_battery_level)

def _insert_new_devices_to_table_and_related_maps(devices_table: QTableWidget,
                                                  registered_devices_dict: Dict[str, Device],
                                                  sync_table_with_registry_params: SyncTableWithRegistryParams):
    devices_registry: Dict[str, Device] = sync_table_with_registry_params.devices_registry
    new_devices: Dict[str, Device] = _identify_new_devices(registered_devices_dict, devices_registry)

    insert_related_entries: Callable[[int, str, BatteryLevelToNotify],
                                     None] = sync_table_with_registry_params.callbacks.get_callback(
        TableWidgetCallbackIdentifiers.MAP_TABLE_ROW_ID_TO_DEVICE
    )

    for device_id, device in new_devices.items():
        inserted_row_id: int = _insert_device_to_table(devices_table, device)
        new_battery_notification_status = BatteryLevelToNotify(_DEFAULT_BATTERY_LEVEL_TO_NOTIFY)

        insert_related_entries(inserted_row_id, device_id, new_battery_notification_status)

def _identify_new_devices(registered_devices_dict: Dict[str, Device], devices_registry: Dict[str, Device]) -> dict:
    registered_devices_set: set = set(registered_devices_dict.items())
    devices_registry_set: set = set(devices_registry.items())

    new_devices_set: set = devices_registry_set - registered_devices_set

    return dict(new_devices_set)

def _get_device_battery_level_from_table(devices_table: QTableWidget, row: int) -> str:
    return _get_item_from_table(devices_table, row, BATTERY_LEVEL_COLUMN)

def _get_device_name_from_table(devices_table: QTableWidget, row: int) -> str:
    return _get_item_from_table(devices_table, row, DEVICE_NAME_COLUMN)

def _get_item_from_table(devices_table: QTableWidget, row: int, column: int) -> str:
    return devices_table.item(row, column).text()

def _remove_device_from_table(devices_table: QTableWidget, row: int) -> None:
    devices_table.removeRow(row)

def _update_device_battery_in_table(devices_table, row: int, item: str):
    updated_item = QTableWidgetItem(item)
    _apply_table_widget_item_styles(updated_item)

    _update_device_in_table(devices_table, row, BATTERY_LEVEL_COLUMN, updated_item)

def _update_device_in_table(devices_table: QTableWidget, row: int, column: int, item: QTableWidgetItem):
    devices_table.setItem(row, column, item)

def _insert_device_to_table(devices_table: QTableWidget, device: Device) -> int:
    row_position = devices_table.rowCount()
    devices_table.insertRow(row_position)

    device_name_item: QTableWidgetItem = QTableWidgetItem(device.device_name)
    _apply_table_widget_item_styles(device_name_item)

    battery_level: QTableWidgetItem = QTableWidgetItem(device.battery_level)
    _apply_table_widget_item_styles(battery_level)

    battery_level_to_notify: QTableWidgetItem = QTableWidgetItem(str(_DEFAULT_BATTERY_LEVEL_TO_NOTIFY))
    _apply_table_widget_item_styles(battery_level_to_notify)

    _update_device_in_table(devices_table, row_position, DEVICE_NAME_COLUMN, device_name_item)
    _update_device_in_table(devices_table, row_position, BATTERY_LEVEL_COLUMN, battery_level)
    _update_device_in_table(devices_table, row_position, BATTERY_LEVEL_TO_NOTIFY_COLUMN, battery_level_to_notify)

    return row_position

def notify_device_battery_level_hits_threshold(devices_table: QTableWidget,
                                               battery_notification_params: BatteryNotificationParams):
    row_count: int = devices_table.rowCount()

    if row_count <= 0:
        return

    for i in range(row_count):
        device_id: str = battery_notification_params.table_row_id_device_map[i]
        device_battery_notification_status: BatteryLevelToNotify = \
            battery_notification_params.device_battery_level_to_notify_map[device_id]

        device_battery_level_in_table: int = int(_get_device_battery_level_from_table(devices_table, i))

        if (device_battery_level_in_table < device_battery_notification_status.battery_level_to_notify or
                device_battery_notification_status.has_notified):
            continue

        device_name: str = _get_device_name_from_table(devices_table, i)
        _send_message(device_id, device_name, battery_notification_params.interface_title)

        device_battery_notification_status.has_notified = True
        battery_notification_params.on_update_battery_notification_status(device_id, device_battery_notification_status)

def _send_message(device_id: str, device_name: str, interface_title: str):
    payload: str = _build_notification_payload(device_name)
    notify_device_charged(payload, interface_title)

    print(f'GUI: \tNotified about device {device_id}, {device_name}')

def _build_notification_payload(device_name: str) -> str:
    message: str = f'Device: {device_name} is charged. \n' \
                   'You can unplug it from the charger now.'

    return message


from collections import deque

def solution(people, limit):
    people = deque(sorted(people))
    answer = 0
    while people:
        right = people.pop()
        if len(people) != 0 and people[0] + right <= limit:
            people.popleft()
        answer += 1
    return answer


import torch
import torch.nn as nn

class conv_bn_re(nn.Module):
    def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, is_re=True):
        super(conv_bn_re, self).__init__()
        self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding)
        self.bn = nn.BatchNorm2d(out_channels)
        self.re = nn.ReLU()
        self.is_re = is_re

    def forward(self, x):
        if self.is_re:
            return self.re(self.bn(self.conv(x)))
        else:
            return self.bn(self.conv(x))

class basicblock(nn.Module):
    def __init__(self, in_channels, out_channels, stride, kernel_size=3, padding=1):
        super(basicblock, self).__init__()
        self.conv1 = conv_bn_re(in_channels, out_channels, kernel_size, stride, padding)
        self.conv2 = conv_bn_re(out_channels, out_channels, kernel_size, 1, padding, is_re=False)
        if stride == 2:
            self.downsample = conv_bn_re(in_channels, out_channels, kernel_size=1, stride=2, is_re=False)
        else:
            self.downsample = None
        self.re = nn.ReLU(inplace=True)

    def forward(self, x):
        y = self.conv1(x)
        y = self.conv2(y)
        if self.downsample:
            x = self.downsample(x)
        return self.re(y + x)

class SElayer(nn.Module):
    def __init__(self,channel,reduction=16):
        super(SElayer, self).__init__()
        self.pool = nn.AdaptiveAvgPool2d(1)
        self.func = nn.Sequential(
            nn.Conv2d(
                in_channels=channel,
                out_channels=int(channel/reduction)+1,
                kernel_size=1,
            ),
            nn.ReLU(inplace=True),
            nn.Conv2d(
                in_channels=int(channel/reduction)+1,
                out_channels=channel,
                kernel_size=1,
            ),
            nn.Sigmoid()
        )

    def forward(self,x):
        atten_x = self.pool(x)
        atten_x = self.func(atten_x).view(x.shape[0],-1,1,1)
        y = x * atten_x
        return y

class SpatialAttention(nn.Module):
    def __init__(self, kernel_size=7):
        super(SpatialAttention, self).__init__()

        assert kernel_size in (3, 7), 'kernel size must be 3 or 7'
        padding = 3 if kernel_size == 7 else 1

        self.conv1 = nn.Conv2d(2, 1, kernel_size, padding=padding, bias=False)
        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        avg_out = torch.mean(x, dim=1, keepdim=True)
        max_out, _ = torch.max(x, dim=1, keepdim=True)
        x = torch.cat([avg_out, max_out], dim=1)
        x = self.conv1(x)
        return self.sigmoid(x)

class cbam(nn.Module):
    def __init__(self,channels):
        super(cbam, self).__init__()
        self.cattn = SElayer(channels)
        self.sattn = SpatialAttention()

    def forward(self,x):
        out = self.cattn(x)
        out = self.sattn(out) * out
        return out

class SCVfliter(nn.Module):
    def __init__(self, dict_len, embed_size, num_hiddens, num_layers, nclass):
        super(SCVfliter, self).__init__()
        self.emb = nn.Embedding(dict_len, embed_size)
        self.conv = nn.Sequential(
            nn.Conv2d(1, 16, 3, 1),
            basicblock(16, 32, 2, 3, 1),
            nn.MaxPool2d(1, 2),
            basicblock(32, 64, 2, 3, 1),
            cbam(64),
            basicblock(64, 128, 2, 3, 1),
            nn.MaxPool2d(1, 2),
            basicblock(128, 256, 2, 3, 1),
            cbam(256),
            basicblock(256, 512, 2, 3, 1),
            nn.MaxPool2d(1, 2),
            basicblock(512, 512, 1, 3, 1),
            cbam(512),
        )
        self.lstm = nn.LSTM(
            input_size=118,
            hidden_size=num_hiddens,
            num_layers=num_layers,
            bidirectional=True,
            batch_first=True,
            dropout=0.3
         )

        self.fc1 = nn.Linear(2 * num_hiddens, num_hiddens)
        self.re = nn.ReLU()
        self.fc2 = nn.Linear(num_hiddens, nclass)
        self.drop = nn.Dropout(0.2)

    def forward(self, x, return_layer=0):
        emb = self.emb(x)
        if return_layer == 1:
            return emb
        emb = self.conv(emb.unsqueeze(1))
        emb = emb.squeeze(dim=3)
        if return_layer == 2:
            return emb
        out, _ = self.lstm(emb)
        out = out.permute(1, 0, 2)
        encoding = out[-1]
        if return_layer == 3:
            return encoding
        fearures = self.re(self.drop(self.fc1(encoding)))
        outs = self.fc2(fearures)

        return outs

if __name__ == '__main__':
    x = torch.ones((2, 30100), dtype=int)
    net = SCVfliter(6, 20, 100, 2, 2)
    print(net(x).shape)


import os
import re
import random
from collections import ChainMap, deque
from typing import Union, Callable, Collection
import operator
from utils import *

__all__ = ['MacroStore']

class MacroStore():

    stdlib = {'': ''}

    @classmethod
    def addStdlib(cls, name:str, value:Union[int, str]):
        '''with two strings, add text macro
        with a string and an int, use as a decorator to add a function'''
        def inner(func:Callable):
            if type(func) == str:
                cls.stdlib[name] = func
            else:
                cls.stdlib[name] = (func, value)
            return func
        if type(value) == str:
            inner(value)
        else:
            return inner

    def __init__(self):
        self.macros = ChainMap({}, self.stdlib)

    def copy(self):
        '''make a copy of this MacroStore'''
        new = MacroStore()
        new.macros = self.macros.new_child()
        return new

    def add(self, name:str, value:Union[list[int], str]):
        '''with two strings, add text macro
        with a string and an int, use as a decorator to add a function'''
        def inner(func:Callable):
            if type(func) == str:
                self.macros[name] = func
            else:
                self.macros[name] = (func, value)
            return func

        if type(value) == str:
            inner(value)
        else:
            return inner

    def call(self, name:str, context:Collection, args:str) -> str:
        '''call a macro by name'''
        #print(f'{name!r} : {args!r}')
        if name in self.macros:
            macro = self.macros[name]
            if type(macro) == str:
                return macro
            else:
                func, signature = macro
                if type(signature) is int:
                    if len(args) != signature:
                        raise CLSError('wrong number of arguments for [{macro}| ], expected {num} got {badnum}',
                        elem=context.elem, prop=context.prop, macro=name, num=signature, badnum=len(args)
                        )
                    return func(context, *args)

                else:
                    min, max = signature
                    if len(args) < min:
                        raise CLSError('too few arguments for [{macro}| ], expected at least {num} got {badnum}',
                        elem=context.elem, prop=context.prop, macro=name, num=min, badnum=len(args)
                        )
                    elif len(args) > max:
                        raise CLSError('too many arguments for [{macro}| ], expected at most {num} got {badnum}',
                        elem=context.elem, prop=context.prop, macro=name, num=max, badnum=len(args)
                        )
                    return func(context, *args)
        else:
            return ''

    def parseMacroArg(self, ps, context) -> str:
        argB = []
        macroStack = []
        char = ''

        while ps.pos < len(ps.string):
            char = ps.string[ps.pos]

            if char == '\\':
                argB.append(ps.string[ps.pos:ps.pos+2])
                ps.pos += 1

            elif char in '[(':
                macroStack.append(char)
                argB.append(char)

            elif char == ')':
                if len(macroStack) == 0:
                    #we never saw the opener
                    raise ImbalancedDelimError(context.elem, context.prop, ps.string)
                opener = macroStack.pop()
                if opener == '[':
                    raise ImbalancedDelimError(context.elem, context.prop, ps.string)
                argB.append(char)

            elif char in ',|]':
                if len(macroStack) == 0:
                    ps.pos -= 1 #backtrack so evalMacro can see the comma/bracket
                    return ''.join(argB).strip()
                else:
                    if char == ']':
                        opener = macroStack.pop()
                        if opener == '(':
                            raise ImbalancedDelimError(context.elem, context.prop, ps.string)
                    argB.append(char)

            else:
                argB.append(char)

            ps.pos += 1

        raise ImbalancedDelimError(context.elem, context.prop, ps.string)

    def evalMacro(self, ps) -> str:
        '''evalMacro parses out a macro then calls it with its arguments and context
        context includes:
         - store - this macroStore object
         - parse - a reference to the parse method of this object
         - source - the string the macro was found in, used for errors
         - prop - the property this macro was found in, used for errors
         - elem - the element this macro was found in, used for errors'''
        nameB = []
        args = []
        char = ''

        context = Collection(
            store=self,
            parse=self.parse,
            source=ps.string,
            prop = self.macros['propertyName'],
            elem = self.macros['elementName']
        )
        while ps.pos < len(ps.string):
            char = ps.string[ps.pos]
            if char == '\\':
                nameB.append(ps.string[ps.pos:ps.pos+2])
                ps.pos += 1

            elif char in ',|':
                ps.pos += 1
                args.append(self.parseMacroArg(ps, context))

            elif char == ']':
                name = build(nameB)
                context.name = name
                return self.call(name, context, args)

            elif char == ')': #? might be wrong, or might need a stack
                raise ImbalancedDelimError(context.elem, context.prop, ps.string)

            else:
                nameB.append(char)

            ps.pos += 1
        raise UnclosedMacroError(context.elem, context.prop, ps.string)

    def evalValue(self, string:str) -> str:
        '''looks at a string, and if it finds a macro, parses it'''
        ps = Collection()
        ps.string = string
        ps.pos = 0
        result = []

        char = ''

        while ps.pos < len(ps.string):
            char = ps.string[ps.pos]

            if char == '\\':
                result.append(ps.string[ps.pos:ps.pos+2])
                ps.pos += 1
                #ignore escapes

            elif char == '[':
                ps.pos += 1
                result.append(self.evalMacro(ps))

            else:
                result.append(char)

            ps.pos += 1

        return ''.join(result)

    def parse(self, string:str) -> str:
        '''parse() turns a value into a real usable object, and evalValue
        processes the top layer of macros, this function does the dirty work
        to make sure that there are no more macros left in the string before
        the final validation passes. It's also used by macros to get values
        that don't have anymore macros, but still have escapes incase of more
        square brackets.
        '''
        while True:
            match = re.search(r'(?<!\\)\[', string)
            if match:
                string = self.evalValue(string)
            else:
                break
        return string

###############################################################################
## BEGIN STDLIB
###############################################################################

@MacroStore.addStdlib('if', (2,3))
def ifMacro(context, test, true, false=''):

    if test[0] == '?' :
        b = asBool(comparisonMacro(context, test[1:]))
    else:
        b = asBool(context.parse(test), err=InvalidArgError(
            context.elem, context.prop, 'if', 'TEST', test
        ))
    if b:
        return true
    else:
        return false

@MacroStore.addStdlib('eq', 2)
def eqMacro(context, left, right):
    if context.parse(left) == context.parse(right):
        return 'true'
    else:
        return 'false'

@MacroStore.addStdlib('ne', 2)
def neMacro(context, left, right):
    if context.parse(left) != context.parse(right):
        return 'true'
    else:
        return 'false'

@MacroStore.addStdlib('in', (2, 99))
def inMacro(context, value, *args):
    parsed = evalEscapes(context.parse(value))
    if len(args) == 0:
        return 'false'
    parsedList = ListParser(args[0], context.elem, context.prop).parse()
    if parsedList == None:
        parsedList = args
    for arg in parsedList:
        if parsed == evalEscapes(context.parse(arg)):
            return 'true'
    return 'false'

@MacroStore.addStdlib('not', 1)
def notMacro(context, value):
    parseVal = context.parse(value)
    boolVal = asBool(parseVal)
    if boolVal is not None:
        if boolVal:
            return 'false'
        else:
            return 'true'
    else:
        return 'false'

@MacroStore.addStdlib('either', 2)
def eitherMacro(context, left, right):
    parsedVal = context.parse(left)
    if asBool(parsedVal) is False:
        return right
    else:
        return left

@MacroStore.addStdlib('i', 1)
def italicMacro(context, string):
    return f'{string}'

@MacroStore.addStdlib('b', 1)
def boldMacro(context, string):
    return f'{string}'

@MacroStore.addStdlib('s', 1)
def strikedMacro(context, string):
    return f'<s>{string}</s>'

@MacroStore.addStdlib('u', 1)
def underlineMacro(context, string):
    return f'{string}'

@MacroStore.addStdlib('dup', 2)
def repeatMacro(context, times, value):
    nTimes = context.parse(times)
    unit = Unit.fromStr(nTimes, signs='+0', units=('',))
    if unit is None:
        raise InvalidArgError(context.elem, context.prop, 'dup', 'TIMES', nTimes)
    times = unit.toInt()
    ofs = 0 if unit.sign == '0' else 1
    result = []
    newStore = context.store.copy()
    for i in range(times):
        newStore.add('d', str(i+ofs))
        result.append(newStore.parse(value))
    return ''.join(result)

@MacroStore.addStdlib('for-each', 2)
def forMacro(context, lst, body):
    lst = context.parse(lst)
    parsedList = ListParser(lst, context.elem, context.prop).parse()
    if parsedList == None:
        raise InvalidArgError(context.elem, context.prop, 'for-each', 'LIST', lst)
    result = []
    newStore = context.store.copy()
    for item in parsedList:
        newStore.add('item', item)
        result.append(newStore.parse(body))
    return ''.join(result)

@MacroStore.addStdlib('capitalize', 1)
def capitalizeMacro(context, value):
    value = context.parse(value)
    value = value[0].upper()+value[1:]
    def repl(m):
        return m.group(1).upper() + m.group(2)
    return re.sub(r'\b([a-z])(\w{3,})\b', repl, value)

@MacroStore.addStdlib('upper', 1)
def upperMacro(context, value):
    value = context.parse(value)
    def repl(m):
        return m.group(1).upper()
    return re.sub(r'(?<!\\)([a-z])', repl, value)

@MacroStore.addStdlib('lower', 1)
def upperMacro(context, value):
    value = context.parse(value)
    def repl(m):
        return m.group(1).lower()
    return re.sub(r'(?<!\\)([A-Z])', repl, value)

@MacroStore.addStdlib('substr', 3)
def substrMacro(context, value, start, length):
    value = context.parse(value)

    start = context.parse(start)
    startUnit = Unit.fromStr(context.parse(start), signs='+0', units=('',))
    if startUnit is None:
        raise InvalidArgError(context.elem, context.prop, 'substr', 'START', start)
    start = startUnit.toInt()
    if startUnit.sign != '0':
        start -= 1

    length = context.parse(length)
    lengthUnit = Unit.fromStr(context.parse(length), signs='+0', units=('',))
    if lengthUnit is None:
        raise InvalidArgError(context.elem, context.prop, 'substr', 'LENGTH', length)
    length = lengthUnit.toInt()

    return value[start:start+length]

@MacroStore.addStdlib('slice', (2, 3))
def sliceMacro(context, value, start, stop=None):
    value = context.parse(value)

    start = context.parse(start)
    startUnit = Unit.fromStr(context.parse(start), signs='+-0', units=('',))
    if startUnit is None:
        raise InvalidArgError(context.elem, context.prop, 'slice', 'START', start)
    start = startUnit.toInt()
    if startUnit.sign in ('+', ''):
        start -= 1

    if stop is not None:
        stop = context.parse(stop)
        stopUnit = Unit.fromStr(context.parse(stop), signs='+-0', units=('',))
        if stopUnit is None:
            raise InvalidArgError(context.elem, context.prop, 'slice', 'STOP', stop)
        stop = stopUnit.toInt()
        if stopUnit.sign in ('+', ''):
            stop -= 1

    parsedList = ListParser(value, context.elem, context.prop).parse()
    if parsedList is not None:
        return makeList(parsedList[start:stop])
    else:
        return value[start:stop]

@MacroStore.addStdlib('length', 1)
def lengthMacro(context, value):
    parsedValue = evalEscapes(context.parse(value))
    parsedList = ListParser(context.parse(value), context.elem, context.prop).parse()
    if parsedList is not None:
        return len(parsedList)
    else:
        return len(parsedValue)

@MacroStore.addStdlib('rnd', (1,2))
def randomMacro(context, start, stop=None):
    if stop == None:
        start, stop = '+1', start
    startUnit = Unit.fromStr(start, signs='+-0', units=('',))
    if startUnit is None:
        raise InvalidArgError(context.elem, context.prop, 'rnd', 'START', start)
    start = startUnit.toInt()
    stopUnit = Unit.fromStr(stop, signs='+-0', units=('',))
    if stopUnit is None:
        raise InvalidArgError(context.elem, context.prop, 'rnd', 'STOP', stop)
    stop = stopUnit.toInt()
    try:
        num = random.randint(start, stop)
    except ValueError:
        raise InvalidArgError(context.elem, context.prop, 'rnd', 'START', start)
    return str(num)

@MacroStore.addStdlib('/', 1)
def expansionMacro(context, value):
    value = context.parse(value)
    return evalEscapes(value)

@MacroStore.addStdlib('?', 1)
def comparisonMacro(context, value):
    nValue = context.parse(value)
    try:
        left, op, right = re.split(r'(==|!=|<=?|>=?)', nValue, maxsplit=1)
    except ValueError as e:
        raise CLSError("'{value}' does not contain a valid comparison",
        prop=context.prop, elem=context.elem, value=value
        )
    leftUnit = Unit.fromStr(left.strip(), units='all')
    if leftUnit is None:
        raise InvalidArgError(context.elem, context.prop, '?', 'OPERAND', left)
    rightUnit = Unit.fromStr(right.strip(), units='all')
    if rightUnit is None:
        raise InvalidArgError(context.elem, context.prop, '?', 'OPERAND', right)
    left = leftUnit.toFloat()
    right = rightUnit.toFloat()
    op = op.strip()
    if op == '==':
        final = left == right
    elif op == '!=':
        final = left != right
    elif op == '>':
        final = left > right
    elif op == '>=':
        final = left >= right
    elif op == '<':
        final = left < right
    elif op == '<=':
        final = left <= right
    else:
        raise CLSError("'{value}' does not contain a valid comparison",
        prop=context.prop, elem=context.elem, value=value
        )
    if final:
        return 'true'
    else:
        return 'false'

def makeOp(op):
    if op not in '+-*/%(':
        return None
    elif op == '+':
        return AttrDict(prec=1, op=operator.add, name=op)
    elif op == '-':
        return AttrDict(prec=1, op=operator.sub, name=op)
    elif op == '*':
        return AttrDict(prec=2, op=operator.mul, name=op)
    elif op == '/':
        return AttrDict(prec=2, op=operator.truediv, name=op)
    elif op == '%':
        return AttrDict(prec=2, op=operator.mod, name=op)
    elif op == '(':
        return AttrDict(prec=0, op=(lambda x, y: None), name=op)

@MacroStore.addStdlib('=', 1)
def mathMacro(context, value):
    '''makes use of dijkstra's shunting yard algorithm to conver to
    reverse polish notation, then parses the rpn'''
    #tokens = context.parse(value).split()

    tokens = re.split(r"\s+(\+|\-|\*|\%|/|\(|\))\s+", context.parse(value.strip()))

    rpn = deque()
    opStack = []
    for token in tokens:
        if token == '':
            continue
        num = Unit.fromStr(token)
        op = makeOp(token)
        length = len(opStack)
        if num is not None:
            rpn.append(num.toFloat())
        elif token == '(':
            opStack.append(op)
        elif token == ')':
            if length == 0:
                raise CLSError("'{value}' is missing an opening parenthesis in macro [{name}| ]",
                elem=context.elem, prop=context.prop, name=context.name, value=value
                )
            while length > 0:
                if opStack[length-1].name != '(':
                    rpn.append(opStack.pop())
                else:
                    break
                length = len(opStack)
            else:
                raise CLSError("'{value}' is missing an opening parenthesis in macro [{name}| ]",
                elem=context.elem, prop=context.prop, name=context.name, value=value
                )
            opStack.pop()
        elif op != None:
            if length > 0 and opStack[length-1].prec >= op.prec:
                rpn.append(opStack.pop())
            opStack.append(op)
        else:
            raise CLSError("'{value}' is an unknown operator in macro [{name}| ]",
                elem=context.elem, prop=context.prop, name=context.name, value=token
                )
    while len(opStack) > 0:
        op = opStack.pop()
        if op.name == '(':
            raise CLSError("'{value}' is missing a closing parenthesis in macro [{name}| ]",
                elem=context.elem, prop=context.prop, name=context.name, value=value
                )
        rpn.append(op)

    accum = []
    if len(rpn) < 3:
        raise CLSError("'{value}' is not a valid mathematical expression in macro [{name}| ]",
                elem=context.elem, prop=context.prop, name=context.name, value=value
                )
    for op in rpn:
        if type(op) == float:
            accum.append(op)
        else:
            if len(accum) < 2:
                print(accum, op)
                raise CLSError("'{value}' does not have enough operands in macro [{name}| ]",
                elem=context.elem, prop=context.prop, name=context.name, value=value
                )
            right = accum.pop()
            left = accum.pop()
            accum.append(op.op(left, right))
    if len(accum) != 1:
        raise CLSError("'{value}' has too many operands in macro [{name}| ]",
                elem=context.elem, prop=context.prop, name=context.name, value=value
                )

    if int(accum[0]) == accum[0]:
        return str(int(accum[0]))
    else:
        return str(accum[0])

@MacroStore.addStdlib('file', 1)
def fileMacro(context, filename):
    name = context.parse(filename)
    if not os.path.isfile(name):
        raise InvalidArgError(context.elem, context.prop, 'file', 'FILENAME', filename)
    try:
        with open(name, encoding='utf-8') as file:
            fileContents = file.read()
    except OSError:
        raise CLSError("Could not open '{filename}'", elem=context.elem, prop=context.prop, filename=filename)
    return fileContents

@MacroStore.addStdlib('switch', (3, 99))
def switchMacro(context, sentinal, *args):
    if len(args)% 2 != 0:
        raise CLSError("case and results are not balanced in macro [switch| ]", elem=context.elem, prop=context.prop)

    sentinal = evalEscapes(context.parse(sentinal))
    args = list(args)

    if args[-2] == 'default':
        default = True
        defaultValue = args.pop()
        args.pop()
    else:
        default = False

    for pair in range(0, len(args), 2):
        testValue = context.parse(args[pair])
        testList = ListParser(testValue, context.elem, context.prop).parse()
        if testList is not None:
            if sentinal in testList:
                return args[pair+1]
        else:
            if sentinal == evalEscapes(testValue):
                return args[pair+1]
    if default:
        return defaultValue
    else:
        return ''

if __name__ == '__main__':
    context = AttrDict(elem='<test>', prop='<test>', name='switch', parse=(lambda x: x))
    foo = ['a', 'foo', '(b, c)', 'bc', 'default', 'else']
    print(switchMacro(context, 'd', *foo))
    #print(mathMacro(context, ''))


import numpy as np
import numpy.testing as npt
import pytest

from pulse2percept.utils import (center_image, shift_image, scale_image,
                                 trim_image)

def test_shift_image():
    img = np.zeros((5, 5), dtype=np.uint8)
    img[2, :] = 255
    # Top row:
    top = shift_image(img, 0, -2)
    npt.assert_almost_equal(top[0, :], 255)
    npt.assert_almost_equal(top[1:, :], 0)
    # Bottom row:
    bottom = shift_image(img, 0, 2)
    npt.assert_almost_equal(bottom[:4, :], 0)
    npt.assert_almost_equal(bottom[4, :], 255)
    # Bottom right pixel:
    bottom = shift_image(img, 4, 2)
    npt.assert_almost_equal(bottom[4, 4], 255)
    npt.assert_almost_equal(bottom[:4, :], 0)
    npt.assert_almost_equal(bottom[:, :4], 0)
    with pytest.raises(ValueError):
        shift_image(np.ones(10), 1, 1)
    with pytest.raises(ValueError):
        shift_image(np.ones((3, 4, 5, 6)), 1, 1)

def test_center_image():
    # Create a horizontal bar:
    img = np.zeros((5, 5), dtype=np.uint8)
    img[2, :] = 255
    # Center phosphene:
    npt.assert_almost_equal(img, center_image(img), decimal=3)
    npt.assert_almost_equal(img, center_image(shift_image(img, 0, 2)))
    with pytest.raises(ValueError):
        center_image(np.ones(10))
    with pytest.raises(ValueError):
        center_image(np.ones((3, 4, 5, 6)))

def test_scale_image():
    # Create a horizontal bar:
    img = np.zeros((5, 5), dtype=np.uint8)
    img[2, :] = 255
    # Scale phosphene:
    npt.assert_almost_equal(img, scale_image(img, 1))
    npt.assert_almost_equal(scale_image(img, 0.1).ravel()[12], 255)
    npt.assert_almost_equal(scale_image(img, 0.1).ravel()[:12], 0)
    npt.assert_almost_equal(scale_image(img, 0.1).ravel()[13:], 0)
    with pytest.raises(ValueError):
        scale_image(np.ones(10), 1)
    with pytest.raises(ValueError):
        scale_image(np.ones((3, 4, 5, 6)), 1)
    with pytest.raises(ValueError):
        scale_image(img, 0)

@pytest.mark.parametrize('n_channels', (1, 3))
def test_trim_image(n_channels):
    img = np.squeeze(np.zeros((13, 29, n_channels), dtype=float))
    shape = img.shape
    img[1:-1, 1:-1, ...] = 0.1
    img[2:-2, 2:-2, ...] = 0.2
    npt.assert_equal(trim_image(img).shape[:2], (shape[0] - 2, shape[1] - 2))
    npt.assert_equal(trim_image(img, tol=0.05).shape[:2],
                     (shape[0] - 2, shape[1] - 2))
    npt.assert_equal(trim_image(img, tol=0.1).shape[:2],
                     (shape[0] - 4, shape[1] - 4))
    npt.assert_equal(trim_image(img, tol=0.2).shape[:2], (1, 0))
    npt.assert_equal(trim_image(img, tol=0.1).shape[:2],
                     trim_image(trim_image(img), tol=0.1).shape[:2])
    trimmed, rows, cols = trim_image(img, return_coords=True)
    npt.assert_almost_equal(trimmed, trim_image(img))
    npt.assert_equal(rows, (1, 12))
    npt.assert_equal(cols, (1, 28))

    with pytest.raises(ValueError):
        trim_image(np.ones(10))
    with pytest.raises(ValueError):
        trim_image(np.ones((3, 4, 5, 6)))
    with pytest.raises(ValueError):
        trim_image(img, tol=-1)


#/usr/bin/python
#coding=utf-8
import os,sys
import zipfile
from unrar import rarfile
import jieba,codecs
import chardet

def detect_walk(dir_path):
    for root, dirs, files in os.walk(dir_path):
        for filename in files:
            # if filename.endswith('.zip'):
            #     print(filename)
            #     zpfd = zipfile.ZipFile(root+'\\'+filename)  # 读取压缩文件
            #     os.chdir(r'E:\sunbo\武侠玄幻合集\武侠玄幻合集\武侠全集\decompress')  # 转到存储路径
            #     zpfd.extractall()
            #     zpfd.close()
            if filename.endswith('.rar'):
                print(filename)
                file = rarfile.RarFile(root+'\\'+filename)
                os.chdir(r'E:\sunbo\武侠玄幻合集\武侠玄幻合集\武侠全集\decompress')  # 转到存储路径
                file.extractall('you_want_path')  # 这里写入的是你想要解压到的文件夹
def detect_file(dir_path):
    count=0
    fw = codecs.open(r'E:\sunbo\武侠玄幻合集\武侠玄幻合集\wuxia.text', 'a', 'utf-8')
    for root, dirs, files in os.walk(dir_path):
        for filename in files:
            if filename.endswith('.txt') or filename.endswith('.TXT'):
                try:
                    f = codecs.open(root + '\\' + filename, 'r','utf-8')
                    text = f.readlines()
                    f.close()
                except:
                    try:
                        f = codecs.open(root + '\\' + filename, 'r', 'gbk')
                        text = f.readlines()
                        f.close()
                    except:
                        try:
                            f = codecs.open(root + '\\' + filename, 'r', 'ascii')
                            text = f.readlines()
                            f.close()
                        except:
                            try:
                                f = codecs.open(root + '\\' + filename, 'r', encoding='gb2312')
                                text = f.readlines()
                                f.close()
                            except:
                                try:
                                    f = codecs.open(root + '\\' + filename, 'r', encoding='utf-16')
                                    text = f.readlines()
                                    f.close()
                                except:
                                    continue
                                    # count+=1
                                    # print(root + '\\' + filename)

                for line in text:
                    word_list = list(jieba.cut(line.strip()))
                    for e in word_list:
                        if e in [u'。', u'？', u'！', u'，','：',u'《',u'》',u'“',u'、',u'”']:
                            word_list.remove(e)
                    toked_text = ' '.join(word_list)
                    fw.write(toked_text + '\n')
    fw.close()
    print(count)

def to_utf8(dir_path):
    for root, dirs, files in os.walk(dir_path):
        for filename in files:
            if filename.endswith('.txt') or filename.endswith('.TXT'):
                f = codecs.open(root + '\\'+ filename, 'r')
                ff = f.read()
                print(ff)
                exit()
                file_object = codecs.open(root + '\\' + filename, 'w', 'utf-8')
                file_object.write(ff)

if __name__ == '__main__':
    # detect_walk(r'E:\sunbo\武侠玄幻合集\武侠玄幻合集\武侠全集')
    detect_file(r'E:\sunbo\武侠玄幻合集\武侠玄幻合集\武侠全集')
    # to_utf8(r'E:\sunbo\武侠玄幻合集\武侠玄幻合集\武侠全集')
    # f=codecs.open(r'E:\sunbo\武侠玄幻合集\武侠玄幻合集\武侠全集\《金庸小说全集 14部》全（TXT）作者：金庸\《金庸小说全集 14部》全（TXT）作者：金庸\倚天屠龙记\YITIAN01.TXT','r')
    # text=f.read()
    # f.close()
    # print(text)


from pyrogram.errors import PeerIdInvalid, UserIsBlocked
from bot.helpers import *

_logger = logging.getLogger(__name__)

@db_session
def report_to_admins(admin: User, user: User, c: Client, message: str):
    """
    Function to report the admins that admin blocked / released user.
    :param admin: the admin that blocked / released the user.
    :param user: the user that was blocked / released.
    :param c: pyrogram.Client to send the message.
    :param message: the key of which message to send to the admins.
    """
    for k, v in get_admins().items():
        if k != admin.uid:
            try:
                c.send_message(
                    k,
                    format_message(message, user, admin=admin.link(), lang=v.language),
                    disable_web_page_preview=True
                )
            except PeerIdInvalid:
                _logger.error(f"Wasn't allow to send message to {v.name}")

@Client.on_message(is_admin & filters.reply & filters.private &
                   filters.command(COMMANDS['block']))
def block(c: Client, m: Message):
    """
    Function to block a user from using the bot.
    :param c: reference to the Client.
    :param m: the message.
    """
    user = get_id(m)
    if not user:
        return _logger.debug("couldn't find user ID in the message")
    user = get_user(user)
    if user.is_admin:
        return
    admin = get_user(m.from_user.id)
    if str(user.uid) not in data['ban']:
        data['ban'].append(str(user.uid))
        m.reply(format_message('user_block', user, lang=admin.language), quote=True,
                disable_web_page_preview=True)
        save_data()
    else:
        return m.reply(
            format_message('already_blocked', user, lang=admin.language), quote=True)
    report_to_admins(admin, user, c, 'user_block_admin')

@Client.on_message(is_admin & filters.reply & filters.private &
                   filters.command(COMMANDS['unblock']))
def unblock(c: Client, m: Message):
    """
    Unblock users, allow user to use the bot again. This function tell the user
    that he is not blocked.
    :param c: reference to the Client.
    :param m: the message.
    """
    user = get_id(m)
    if not user:
        return _logger.debug("couldn't find user ID in the message")
    user = get_user(user)
    if user.is_admin:
        return
    admin = get_user(m.from_user.id)
    if str(user.uid) in data['ban']:
        data['ban'].remove(str(user.uid))
        m.reply(format_message('user_unblock', user, lang=admin.language), quote=True,
                disable_web_page_preview=True)
        save_data()
        try:
            c.send_message(user.uid, MSG('unban_msg', user.language))
        except UserIsBlocked:
            with db_session:
                delete(u for u in User if u.uid == user.uid)
    else:
        return m.reply(format_message('not_blocked', user, lang=admin.language),
                       quote=True)
    report_to_admins(admin, user, c, 'user_unblock_admin')


import logging

import sys

import asyncio

from aiocoap import *

logging.basicConfig(level=logging.INFO)

@asyncio.coroutine

def main():

    num = str(sys.argv[1])

    protocol = yield from Context.create_client_context()

    request = Message(code=GET)

    address = "coap://[cafe::c30c:0:0:" + num + "]/actuators/ledstatus"

    request.set_request_uri(address)

    try:

        response = yield from protocol.request(request).response

    except Exception as e:

        print('Failed to fetch resource:')

        print(e)

    else:

        print('Result: %s[barra-n]%r'%(response.code, response.payload))

if __name__ == "__main__":

   asyncio.get_event_loop().run_until_complete(main())


import unittest

import random

from fuse.utils import (
    Uniform,
    Choice,
    RandInt,
    RandBool,
    draw_samples_recursively,
    Seed,
)

class TestParamSampler(unittest.TestCase):
    """
    Test ParamSampleBase sub classes
    """

    def test_uniform(self) -> None:
        Seed.set_seed(0)
        min = random.random() * 1000
        max = random.random() * 1000 + min
        uniform = Uniform(min, max)
        value = uniform.sample()

        # test range
        self.assertGreaterEqual(value, min)
        self.assertLessEqual(uniform.sample(), max)

        # test generate more than a single number
        self.assertNotEqual(value, uniform.sample())

        # test fixed per seed
        Seed.set_seed(1234)
        value0 = uniform.sample()
        Seed.set_seed(1234)
        value1 = uniform.sample()
        self.assertEqual(value0, value1)

    def test_randint(self) -> None:
        Seed.set_seed(0)
        min = random.randint(0, 1000)
        max = random.randint(0, 1000) + min
        randint = RandInt(min, max)
        value = randint.sample()

        # test range
        self.assertGreaterEqual(value, min)
        self.assertLessEqual(randint.sample(), max)

        # test generate more than a single number
        self.assertNotEqual(value, randint.sample())

        # test fixed per seed
        Seed.set_seed(1234)
        value0 = randint.sample()
        Seed.set_seed(1234)
        value1 = randint.sample()
        self.assertEqual(value0, value1)

    def test_randbool(self) -> None:
        Seed.set_seed(0)
        randbool = RandBool(0.5)
        value = randbool.sample()

        # test range
        self.assertIn(value, [True, False])

        # test generate more than a single number
        Seed.set_seed(0)
        values = [randbool.sample() for _ in range(4)]
        self.assertIn(True, values)
        self.assertIn(False, values)

        # test fixed per seed
        Seed.set_seed(1234)
        value0 = randbool.sample()
        Seed.set_seed(1234)
        value1 = randbool.sample()
        self.assertEqual(value0, value1)

        # test probs
        Seed.set_seed(0)
        randbool = RandBool(0.99)
        count = 0
        for _ in range(1000):
            if randbool.sample() == True:
                count += 1
        self.assertGreaterEqual(count, 980)

    def test_choice(self) -> None:
        Seed.set_seed(0)
        lst = list(range(1000))
        choice = Choice(lst)
        value = choice.sample()

        # test range
        self.assertIn(value, lst)

        # test generate more than a single number
        self.assertNotEqual(value, choice.sample())

        # test fixed per seed
        Seed.set_seed(1234)
        value0 = choice.sample()
        Seed.set_seed(1234)
        value1 = choice.sample()
        self.assertEqual(value0, value1)

        # test probs
        Seed.set_seed(0)
        probs = [0.01 / 999] * 1000
        probs[5] = 0.99
        choice = Choice(lst, probs)
        count = 0
        for _ in range(1000):
            if choice.sample() == 5:
                count += 1
        self.assertGreaterEqual(count, 980)

    def test_draw_samples_recursively(self) -> None:
        Seed.set_seed(0)
        a = {
            "a": 5,
            "b": [3, RandInt(1, 5), 9],
            "c": {"d": 3, "f": [1, 2, RandBool(0.5), {"h": RandInt(10, 15)}]},
            "e": {"g": Choice([6, 7, 8])},
        }
        b = draw_samples_recursively(a)

        self.assertEqual(a["a"], a["a"])
        self.assertEqual(b["b"][0], a["b"][0])
        self.assertEqual(b["b"][2], a["b"][2])
        self.assertEqual(b["c"]["d"], a["c"]["d"])
        self.assertEqual(b["c"]["f"][1], a["c"]["f"][1])
        self.assertIn(b["b"][1], [1, 2, 3, 4, 5])
        self.assertIn(b["c"]["f"][2], [True, False])
        self.assertIn(b["c"]["f"][3]["h"], [10, 11, 12, 13, 14, 15])
        self.assertIn(b["e"]["g"], [6, 7, 8])

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


import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches

def bilinear_upsampler(tensor, new_shape):
	return tf.image.resize(tensor, new_shape)

#Reference : https://github.com/tadax/m2det/blob/master/utils/generate_priors.py
def generate_anchors(num_scales=3, anchor_scale=2.0, image_size=320, shapes=[40, 20, 10, 5, 3, 1]):
	anchor_configs = {}
	for shape in shapes:
		anchor_configs[shape] = []
		for scale_octave in range(num_scales):
			for aspect_ratio in [(1, 1), (1.41, 0.71), (0.71, 1.41)]:
				anchor_configs[shape].append(
					(
						image_size / shape,
						scale_octave / float(num_scales),
						aspect_ratio
					)
				)

	boxes_all = []
	for _, configs in anchor_configs.items():
		boxes_level = []
		for config in configs:
			stride, octave_scale, aspect = config
			base_anchor_size = anchor_scale * stride * (2 ** octave_scale)
			anchor_size_x_2 = base_anchor_size * aspect[0] / 2.0
			anchor_size_y_2 = base_anchor_size * aspect[1] / 2.0
			x = np.arange(stride / 2, image_size, stride)
			y = np.arange(stride / 2, image_size, stride)
			xv, yv = np.meshgrid(x, y)
			xv = xv.reshape(-1)
			yv = yv.reshape(-1)
			boxes = np.vstack(
				(
					yv - anchor_size_y_2, xv - anchor_size_x_2,
					yv + anchor_size_y_2, xv + anchor_size_x_2)
				)
			boxes = np.swapaxes(boxes, 0, 1)
			boxes_level.append(np.expand_dims(boxes, axis=1))
		boxes_level = np.concatenate(boxes_level, axis=1)
		boxes_level /= image_size
		boxes_all.append(boxes_level.reshape([-1, 4]))

	anchor_boxes = np.vstack(boxes_all)

	return anchor_boxes

def clip_boxes(boxes, img_shape=(320, 320)):
	"""Clip boxes to image boundaries."""
	# x1 >= 0
	boxes[:, 0] = np.maximum(boxes[:, 0], 0)
	# y1 >= 0
	boxes[:, 1] = np.maximum(boxes[:, 1], 0)
	# x2 < im_width
	boxes[:, 2] = np.minimum(boxes[:, 2], img_shape[0] - 1)
	# y2 < img_height
	boxes[:, 3] = np.minimum(boxes[:, 3], img_shape[1] - 1)
	return boxes

def iou(anchors, boxes):
	if len(boxes.shape) == 2:
		boxes = boxes[0,:]
	if len(anchors.shape) == 1:
		anchors = anchors.reshape(1,-1)
	x1 = np.maximum(anchors[:, 0], boxes[0])
	y1 = np.maximum(anchors[:, 1], boxes[1])
	x2 = np.minimum(anchors[:, 2], boxes[2])
	y2 = np.minimum(anchors[:, 3], boxes[3])

	width = (x2 - x1)
	height = (y2 - y1)
	area_overlap = np.maximum(width*height, 0)
	l = (anchors[:, 2] - anchors[:, 0])
	b = (anchors[:, 3] - anchors[:, 1])
	area_an = l * b
	l = (boxes[2] - boxes[0])
	b = (boxes[3] - boxes[1])
	area_bx = l * b
	union = np.maximum((area_an + area_bx) - area_overlap,0)
	iou = area_overlap / (union)

	#setting iou to 0 which doesn't have overlap with ground truth
	iou[iou == np.inf] = 0
	iou[iou == -np.inf] = 0
	iou[anchors[:,2] < boxes[0]] = 0
	iou[anchors[:,0] > boxes[2]] = 0
	iou[anchors[:,3] < boxes[1]] = 0
	iou[anchors[:,1] > boxes[3]] = 0

	return iou

def nms(dets, confidence, thresh=0.4):
	outputs = []
	while len(dets) > 0:
		ix = np.argmax(confidence)
		result = dets[ix]
		outputs.append(result)
		dets = np.delete(dets, ix, axis=0)
		box1 = np.expand_dims(result, axis=0)
		to_delete = []
		for jx in range(len(dets)):
			box2 = np.expand_dims(dets[jx], axis=0)
			iou_ = iou(box1, box2)
			if iou_ >= thresh:
				to_delete.append(jx)
		for jx in to_delete[::-1]:
			dets = np.delete(dets, jx, axis=0)
	return np.array(outputs, dtype=np.float32)

def visualize(img, boxes):
	if len(boxes.shape) == 1:
		boxes = boxes.reshape(1,-1)
	fig, ax = plt.subplots(1)
	ax.imshow(img)
	boxes_xy_wh = boxes
	boxes_xy_wh[:, 0] = boxes[:, 0]
	boxes_xy_wh[:, 1] = boxes[:, 1]
	boxes_xy_wh[:, 2] = boxes[:, 2] - boxes[:, 0]
	boxes_xy_wh[:, 3] = boxes[:, 3] - boxes[:, 1]
	for i in range(len(boxes_xy_wh)):
		k = 0
		if i == 0:
			ec = 'r'
		else:
			ec = 'g'
		s = patches.Rectangle(
			(boxes_xy_wh[i][k], boxes_xy_wh[i][k+1]),
			boxes_xy_wh[i][k+2], boxes_xy_wh[i][k+3],
			linewidth=1, edgecolor=ec,
			facecolor="none"
			)
		ax.add_patch(s)
	plt.show()

if __name__ == "__main__":
	gt_box = np.array([[0, 0, 99, 99]])
	boxes = np.array(
		[[0, 0, 74, 74],
		[0, 0, 24, 24],
		[0, 0, 70, 70],
		[0, 0, 50, 50],
		[10, 20, 60, 70]]
		)
	ans = np.array(
		[0.5, 0.062, 0.5, 0.25]
		)
	iou_ = iou(boxes, gt_box)
	check = iou_[:4]/ans
	print (iou_)
	print("iou cases %s"%(np.greater(check, 0.9)))
	nms_box = nms(boxes, iou_)
	print ("nms cases are %s"%(np.allclose(nms_box, boxes[:2, :])))


from django.forms import ValidationError
from django.forms.models import ModelForm
from django.contrib import admin
from sorl.thumbnail import get_thumbnail

from sorl.thumbnail.admin import AdminImageMixin

from django.utils.translation import ugettext as _

from realestate.listing.models import ListingImage, AttributeListing, Listing, Location, Agent, \
    Deal, Attribute
from realestate.listing.templatetags.extra_functions import currency
from realestate.listing.utils import validate_attribute_value, \
    copy_model_instance, import_validator

class ImageAdmin(admin.ModelAdmin):
    def friendly_title(self, obj):
        return obj.name

    def image_miniatura(self, obj):
        image = get_thumbnail(obj.image, '75x50', crop='center', quality=99)
        if image is not None:
            return '' % image.url
        return _('Image not Available')

    image_miniatura.short_description = _(u'Image')
    image_miniatura.allow_tags = True
    friendly_title.short_description = _(u'Image title')

    list_display = ('listing', 'image_miniatura', 'friendly_title', 'order')
    search_fields = ['listing', 'title']
    date_hierarchy = 'added'

def clean_attribute_value(cleaned_data):
    value = cleaned_data['value']
    attribute = cleaned_data['attribute']
    obj = cleaned_data['listing']
    success, valid_value, error_message = validate_attribute_value(attribute,
                                                                   value, obj)

    if not success:
        raise ValidationError(error_message)
    return valid_value

class ImageListingInline(AdminImageMixin, admin.TabularInline):
    model = ListingImage

class AttributeListingInlineForm(ModelForm):
    def clean_value(self):
        return clean_attribute_value(self.cleaned_data)

class AttributeListingInline(admin.TabularInline):
    model = AttributeListing
    form = AttributeListingInlineForm

class ListingAdmin(admin.ModelAdmin):
    change_form_template = "admin/realestate/listing/change_form.html"
    fieldsets = [
        (_(u'Listing Description'),
         {
             'fields': [
                 'title', 'description', 'price', ('baths', 'beds', 'size'),
                 'location', 'type', 'offer', 'active', 'featured',
             ]
         }
         ),
        (_('Contact Info'),
         {
             'fields': [
                 'agent', 'contact', 'notes', 'coords',
             ]
         }
         )
    ]

    inlines = [AttributeListingInline, ImageListingInline, ]

    list_display = (
        'id', 'title', 'slug', 'currency_price', 'active', 'type', 'location',
        'agent', 'created_at', 'featured', 'thumb_nail'
    )

    actions = ['duplicate_listing', ]

    def currency_price(self, listing):
        return currency(listing.price)

    currency_price.short_description = _(u'Price')

    list_display_links = ('id', 'title')
    search_fields = ['title', 'location']
    list_filter = ['created_at', 'agent', 'title', 'active', ]
    date_hierarchy = 'created_at'

    def thumb_nail(self, obj):
        imageobj = obj.main_image
        if imageobj:
            image = get_thumbnail(imageobj.image, '75x50', quality=99)
            if image is not None:
                return '' % image.url
        return _(u'No image')

    thumb_nail.short_description = _(u'Image')
    thumb_nail.allow_tags = True

    def duplicate_listing(self, request, queryset):
        for listing in queryset:
            new_listing = copy_model_instance(listing)
            new_listing.save()
            for attr in listing.attributelisting_set.all():
                new_attr = copy_model_instance(attr)
                new_attr.listing = new_listing
                new_attr.save()
            for image in listing.images.all():
                new_image = copy_model_instance(image)
                new_image.listing = new_listing
                new_image.save()

    class Media:
        js = ('js/admin/propiedades.js',)

class DealsAdmin(admin.ModelAdmin):
    list_display = ('listing', 'active', 'price', 'start_date', 'end_date')

class AgentAdmin(AdminImageMixin, admin.ModelAdmin):
    list_display = ('name', 'email', 'phone', 'mobile', 'photo',)

    def photo(self, obj):
        imageobj = obj.image
        if imageobj:
            image = get_thumbnail(imageobj, '133x100', quality=99)
            return '' % image.url
        else:
            return _(u'Please, add an image')

    photo.short_description = _(u'Image')
    photo.allow_tags = True

class AtributosForm(ModelForm):
    def clean_validation(self):
        validation = self.cleaned_data['validation']
        try:
            import_validator(validation)
        except ImportError:
            raise ValidationError(_("Invalid validation function specifed!"))
        return validation

class AttributesAdmin(admin.ModelAdmin):
    form = AtributosForm

class LocationAdmin(admin.ModelAdmin):
    list_display = ('name', 'parent', 'location_type')

admin.site.register(Listing, ListingAdmin)
admin.site.register(ListingImage, ImageAdmin)
admin.site.register(Location, LocationAdmin)
admin.site.register(Agent, AgentAdmin)
admin.site.register(Deal, DealsAdmin)
admin.site.register(Attribute, AttributesAdmin)


import os

import setuptools
import setuptools.command.install

# don't import: import * is unreliable and there is no need, since this is
# compile time and we have source files
def get_console_scripts():
    yield 'qrexec-policy', 'vanirpolicy.cli'
    yield 'qrexec-policy-agent', 'vanirpolicy.agent'
    yield 'qrexec-policy-graph', 'vanirpolicy.graph'
    for filename in os.listdir('./vanir/tools'):
        basename, ext = os.path.splitext(os.path.basename(filename))
        if basename == '__init__' or ext != '.py':
            continue
        yield basename.replace('_', '-'), 'vanir.tools.{}'.format(basename)

# create simple scripts that run much faster than "console entry points"
class CustomInstall(setuptools.command.install.install):
    def run(self):
        bin = os.path.join(self.root, "usr/bin")
        try:
            os.makedirs(bin)
        except:
            pass
        for file, pkg in get_console_scripts():
           path = os.path.join(bin, file)
           with open(path, "w") as f:
               f.write(
"""#!/usr/bin/python3
from {} import main
import sys
if __name__ == '__main__':
	sys.exit(main())
""".format(pkg))

           os.chmod(path, 0o755)
        setuptools.command.install.install.run(self)

if __name__ == '__main__':
    setuptools.setup(
        name='vanir',
        version=open('version').read().strip(),
        author='Cybertrigo',
        description='vanir core package',
        license='GPL2+',
        url='https://github.com/VanirLab/VOS',
        packages=setuptools.find_packages(exclude=('core*', 'tests')),
        package_data = {
            'vanirpolicy': ['glade/*.glade'],
        },
        cmdclass={
            'install': CustomInstall,
        },
        entry_points={
            'vanir.vm': [
                'AppVM = vanir.vm.appvm:AppVM',
                'TemplateVM = vanir.vm.templatevm:TemplateVM',
                'StandaloneVM = vanir.vm.standalonevm:StandaloneVM',
                'AdminVM = vanir.vm.adminvm:AdminVM',
                'DispVM = vanir.vm.dispvm:DispVM',
            ],
            'vanir.ext': [
                'vanir.ext.admin = vanir.ext.admin:AdminExtension',
                'vanir.ext.core_features = vanir.ext.core_features:CoreFeatures',
                'vanir.ext.VanirManager = vanir.ext.VanirManager:VanirManager',
                'vanir.ext.gui = vanir.ext.gui:GUI',
                'vanir.ext.r3compatibility = vanir.ext.r3compatibility:R3Compatibility',
                'vanir.ext.pci = vanir.ext.pci:PCIDeviceExtension',
                'vanir.ext.block = vanir.ext.block:BlockDeviceExtension',
                'vanir.ext.services = vanir.ext.services:ServicesExtension',
                'vanir.ext.windows = vanir.ext.windows:WindowsFeatures',
            ],
            'vanir.devices': [
                'pci = vanir.ext.pci:PCIDevice',
                'block = vanir.ext.block:BlockDevice',
                'testclass = vanir.tests.devices:TestDevice',
            ],
            'vanir.storage': [
                'file = vanir.storage.file:FilePool',
                'file-reflink = vanir.storage.reflink:ReflinkPool',
                'linux-kernel = vanir.storage.kernels:LinuxKernel',
                'lvm_thin = vanir.storage.lvm:ThinPool',
            ],
            'vanir.tests.storage': [
                'test = vanir.tests.storage:TestPool',
                'file = vanir.storage.file:FilePool',
                'file-reflink = vanir.storage.reflink:ReflinkPool',
                'linux-kernel = vanir.storage.kernels:LinuxKernel',
                'lvm_thin = vanir.storage.lvm:ThinPool',
            ],
	   'vanir.mechanize':[
	   '    vanir.setup = vanir.setup:Settings'
	   ],
        })


from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator
from django.shortcuts import get_object_or_404, redirect, render
from django.utils import timezone

from .forms import CommentForm, PostForm
from .models import Follow, Group, Post, User

PER_PAGE = 10

def index(request):
    post_list = Post.objects.all()
    paginator = Paginator(post_list, PER_PAGE)
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)

    template = 'posts/index.html'
    context = {
        'page_obj': page_obj,
    }
    return render(request, template, context)

def group_posts(request, slug):
    group = get_object_or_404(Group, slug=slug)
    post_list = group.posts.all()
    paginator = Paginator(post_list, PER_PAGE)
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    template = 'posts/group_list.html'
    context = {
        'group': group,
        'page_obj': page_obj,
    }
    return render(request, template, context)

def profile(request, username):
    author = get_object_or_404(User, username=username)
    post_list = Post.objects.filter(author__username=username)
    paginator = Paginator(post_list, PER_PAGE)
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    following = Follow.objects.filter(user__username=request.user,
                                      author=author)
    context = {
        'author': author,
        'page_obj': page_obj,
        'following': following,
    }
    return render(request, 'posts/profile.html', context)

def post_detail(request, post_id):
    post = get_object_or_404(Post, id=post_id)
    comment_form = CommentForm(request.POST or None)
    comments = post.comments.all()
    author = post.author
    following = Follow.objects.filter(user__username=request.user,
                                      author=author)
    context = {
        'author': author,
        'post': post,
        'form': comment_form,
        'following': following,
        'comments': comments,
    }
    return render(request, 'posts/post_detail.html', context)

@login_required
def post_create(request):
    form = PostForm(request.POST or None, files=request.FILES or None)
    if request.method == "POST" and form.is_valid():
        post = form.save(commit=False)
        post.author = request.user
        post.published_date = timezone.now()
        post.save()
        return redirect('posts:profile', username=post.author)
    context = {
        'form': form
    }
    return render(request, 'posts/create_post.html', context)

@login_required
def post_edit(request, post_id):
    post = get_object_or_404(Post, pk=post_id)
    if request.user != post.author:
        return redirect('posts:post_detail', post_id=post_id)
    form = PostForm(
        request.POST or None,
        files=request.FILES or None,
        instance=post)
    if request.method == "POST" and form.is_valid():
        form.save()
        return redirect('posts:post_detail', post_id=post_id)
    context = {
        'form': form,
        'is_edit': True,
    }
    return render(request, 'posts/create_post.html', context)

@login_required
def add_comment(request, post_id):
    post = get_object_or_404(Post, pk=post_id)
    form = CommentForm(request.POST or None)
    if form.is_valid():
        comment = form.save(commit=False)
        comment.author = request.user
        comment.post = post
        comment.save()
    return redirect('posts:post_detail', post_id=post_id)

@login_required
def follow_index(request):
    post_list = Post.objects.filter(author__following__user=request.user)
    paginator = Paginator(post_list, PER_PAGE)
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    context = {
        'page_obj': page_obj,
    }
    return render(request, 'posts/follow.html', context)

@login_required
def profile_follow(request, username):
    user = get_object_or_404(User, username=username)
    Follow.objects.get_or_create(user=request.user, author=user)
    return redirect('posts:profile', username)

@login_required
def profile_unfollow(request, username):
    user = get_object_or_404(User, username=username)
    follow, created = Follow.objects.get_or_create(
        user=request.user, author=user)
    follow.delete()
    return redirect('posts:profile', username)


from rest_framework.decorators import api_view
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework import serializers

from .models import Exchange, Currency
from .services import get_exange_rate
from .selectors import (get_currency_obj,
                        queryset_currencies,
                        queryset_exchange_activity)

@api_view(['GET'])
def exchange_view(request, sell, buys):
    """Exchange currency rate view

    :in
        sell : str
        buys : str
    :out
        json Responce with currency rate for sell('base currency') to buys currency
        save rate in database
    """

    # uppercases arguments
    sell, buys = sell.upper(), buys.upper()

    # get currency objects
    currency_to_sell, currency_to_buys = get_currency_obj(sell, ['pk']), get_currency_obj(buys, ['pk'])

    # if this objects exists in db
    if currency_to_sell and currency_to_buys:
        data, status_code = get_exange_rate(request, sell, buys)

        # create an exchange object if the data has been successfully download
        if status_code == 200:
            Exchange.objects.create(sell=currency_to_sell, buys=currency_to_buys, exchange_rate=data['data'][buys])

        return Response(data, status=status.HTTP_200_OK)

    # inform user that this currency is not available
    else:
        if currency_to_sell:
            error_currency = buys
        else:
            error_currency = sell

        return Response(f'this ({error_currency}) currency is not available', status=status.HTTP_404_NOT_FOUND)

class ListCurrencies(APIView):
    """
    List Currencies objects and list Exchanges objects

    serializers
    ---------
    ListCurrenciesSerializers
        model = Currency
    ListCurrenciesExchangeSerializers
        model = Exchange

    Methods
    --------
    [GET]
        return json with Currency objects and Exchanges objects

    Parameters
    --------
        most_frequent : str (seles/shopping)
        filter_currency : str
        all_activity : bool
        page : int
        filter_activity : str
    """

    class ListCurrenciesSerializers(serializers.ModelSerializer):
        """Serializing Currency model data"""
        class Meta:
            model = Currency
            fields = ('symbol', 'name', 'symbol_native', 'name_plural')

    class ListCurrenciesExchangeSerializers(serializers.ModelSerializer):
        """Serializing Exchange model data ,related fields to Currency model (sell, buys)"""
        sell = serializers.StringRelatedField(many=False)
        buys = serializers.StringRelatedField(many=False)

        class Meta:
            model = Exchange
            fields = ('sell', 'buys', 'exchange_rate', 'timestamp')

    def get(self, request):
        """
        filters:
            :Currrencies:
                filter_currency : str
                most_frequent : str and filter_currency : str
            :Exchanges:
                all_activity : bool
                page : int
                filter_activity : str

        sorting:
            :Currrencies:
                most_frequent:bool
        """
        response = dict()

        # list currencies
        currencies = queryset_currencies(('symbol', 'name', 'symbol_native', 'name_plural'), response, **request.GET)

        serializer_currency = self.ListCurrenciesSerializers(currencies, many=True)
        response["**CURENCIES**"] = serializer_currency.data

        # list exchanging activities
        exchanges = queryset_exchange_activity(('sell', 'buys', 'exchange_rate', 'timestamp'), response=response, **request.GET)

        serializer_exchange = self.ListCurrenciesExchangeSerializers(exchanges, many=True)
        response["**EXCHANGE ACTIVITY**"] = serializer_exchange.data

        return Response(response, status=status.HTTP_200_OK)


from django.core.paginator import Paginator
from django.shortcuts import get_object_or_404, render, redirect
from django.contrib.auth.models import User
from .models import Group, Post
from .forms import PostForm
POSTS_PER_PAGE = 10

def index(request):
    title = 'Главная страница проекта Yatube'
    post_list = Post.objects.all().order_by('-pub_date')
    paginator = Paginator(post_list, 10)
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    context = {
        'title': title,
        'page_obj': page_obj,
    }
    return render(request, 'posts/index.html', context)

def group_posts(request, slug):
    group = get_object_or_404(Group, slug=slug)
    template = 'posts/group_list.html'
    title = 'Здесь будет информация о группах проекта Yatube'
    posts = group.posts.all()[:POSTS_PER_PAGE]
    post_list = Post.objects.all().order_by('-pub_date')
    paginator = Paginator(post_list, 10)
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    context = {
        'group': group,
        'title': title,
        'posts': posts,
        'page_obj': page_obj,

    }
    return render(request, template, context)

def profile(request, username):
    template = 'posts/profile.html'
    author = get_object_or_404(User, username=username)
    post_list = Post.objects.filter(author=author).order_by('-pub_date')
    title = f'Профайл пользователя {author.username}'
    paginator = Paginator(post_list, 10)
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    context = {
        'author': author,
        'title': title,
        'page_obj': page_obj,
        'total_posts': post_list.count(),
    }
    return render(request, template, context)

def post_detail(request, post_id):
    template = 'posts/post_detail.html'
    post = get_object_or_404(Post, pk=post_id)
    post_number = post.author.posts.count()
    context = {
        'post': post,
        'post_number': post_number,
    }
    return render(request, template, context)

def post_create(request):
    form = PostForm(request.POST or None,
        files=request.FILES or None
    )
    if form.is_valid():
        post = form.save(commit=False)
        post.author = request.user
        post.save()
        return redirect('posts:profile', post.author)
    context = {
        'form': form,
    }
    return render(request, 'posts/create_post.html', context)

def post_edit(request, post_id):
    post = get_object_or_404(Post, pk=post_id)
    if request.user != post.author:
        return redirect('posts:post_detail', post_id)
    form = PostForm(
        request.POST or None,
        files=request.FILES or None,
        instance=post
    )
    if form.is_valid():
        post = form.save()
        return redirect('posts:post_detail', post_id)
    is_edit = True
    context = {
        'form': form,
        'is_edit': is_edit,
        'post_id': post_id,
    }
    return render(request, 'posts/create_post.html', context)


import pygame
import sys
import random
from time import sleep

BLACK = (0,0,0)
padWidth = 480
padHeight = 640

faceImage = ['game-resource/detua.png','game-resource/hong.png','game-resource/wakeup.png','game-resource/tiffany.png']
explosionSound = ['game-resource/chanho.wav','game-resource/detua.wav','game-resource/tiffany.wav']

def drawObject(obj,x,y):
    global gamePad
    gamePad.blit(obj,(x,y))

def initGame():
    global gamePad, clock, background, tuna, nail, explosion, nailSound, gameOverSound
    pygame.init()
    gamePad = pygame.display.set_mode((padWidth,padHeight))
    pygame.display.set_caption("Nail Tuna")
    background = pygame.image.load("game-resource/background.png")
    tuna = pygame.image.load('game-resource/tuna.png')
    nail = pygame.image.load('game-resource/nail.png')
    explosion = pygame.image.load('game-resource/explosion.png')

    pygame.mixer.music.load('game-resource/background.wav')
    pygame.mixer.music.play(-1)
    nailSound = pygame.mixer.Sound('game-resource/missile.wav')
    gameOverSound = pygame.mixer.Sound('game-resource/gameover.wav')

    clock = pygame.time.Clock()

def writeScore(count):
    global gamePad
    font = pygame.font.Font('game-resource/NanumGothic-Regular.ttf',15)
    text = font.render('Destroyed faces:' + str(count), True, (255,255,255))
    gamePad.blit(text,(10,0))

def writePassed(count):
    global gamePad
    font = pygame.font.Font('game-resource/NanumGothic-Regular.ttf',15)
    text = font.render('Loss faces:' + str(count), True, (255,0,0))
    gamePad.blit(text,(360,0))

def writeMessage(text):
    global gamePad, gameOverSound
    textfont = pygame.font.Font('game-resource/NanumGothic-Regular.ttf',80)
    text = textfont.render(text,True,(255,0,0))
    textpos = text.get_rect()
    textpos.center = (padWidth/2, padHeight/2)

    gamePad.blit(text,textpos)
    pygame.display.update()
    pygame.mixer_music.stop()
    gameOverSound.play()

    sleep(2)

    pygame.mixer_music.play(-1)

    runGame()

def crash():
    global gamePad
    writeMessage("못참치!")

def gameOver():
    global gamePad
    writeMessage("못~~참치!")

def runGame():
    global gamepad, clock, background, tuna, nail, explosion, nailSound

    tunaSize = tuna.get_rect().size
    tunaWidth = tunaSize[0]
    tunaHeight = tunaSize[1]

    nailXY = []

    face = pygame.image.load(random.choice(faceImage))
    faceSize = face.get_rect().size
    faceWidth = faceSize[0]
    faceHeight = faceSize[1]
    destroySound = pygame.mixer.Sound(random.choice(explosionSound))

    faceX = random.randrange(0,padWidth- faceWidth)
    faceY = 0
    faceSpeed = 2

    x = padWidth * 0.45
    y = padHeight * 0.9
    tunaX = 0

    isShot = False
    shotCount = 0
    facePassed = 0

    onGame = False

    while not onGame:
        for event in pygame.event.get():
            if event.type in [pygame.QUIT]:
                pygame.quit()
                sys.exit()

            if event.type in [pygame.KEYDOWN]:
                if event.key == pygame.K_LEFT:
                    tunaX -= 5

                elif event.key == pygame.K_RIGHT:
                    tunaX += 5

                elif event.key == pygame.K_SPACE:
                    nailSound.play()
                    nailX = x + tunaWidth/2
                    nailY = y - tunaHeight
                    nailXY.append([nailX,nailY])

            if event.type in [pygame.KEYUP]:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    tunaX = 0

        drawObject(background,0,0)

        x += tunaX
        if x < 0 :
            x = 0
        elif x > padWidth - tunaWidth:
            x = padWidth - tunaWidth

        if y < faceY + faceHeight:
            if (faceX > x and faceX < x + tunaWidth) or \
                (faceX + faceWidth > x and faceX + faceWidth < x + tunaWidth):
                crash()

        drawObject(tuna,x,y)

        if len(nailXY) != 0:
            for i, bxy in enumerate(nailXY):
                bxy[1] -= 10
                nailXY[i][1] = bxy[1]

            if bxy[1] < faceY:
                if bxy[0] > faceX and bxy[0] < faceX + faceWidth:
                    nailXY.remove(bxy)
                    isShot = True
                    shotCount += 1

            if bxy[1] <= 0:
                try:
                    nailXY.remove(bxy)
                except:
                    pass
        if len(nailXY) != 0:
            for bx, by in nailXY:
                drawObject(nail,bx,by)

        writeScore(shotCount)
        faceY += faceSpeed

        if faceY > padHeight:

            face = pygame.image.load(random.choice(faceImage))
            faceSize = face.get_rect().size
            faceWidth = faceSize[0]
            faceHeight = faceSize[1]

            faceX = random.randrange(0, padWidth - faceWidth)
            faceY = 0
            facePassed += 1

        if facePassed == 3:
            gameOver()

        writePassed(facePassed)

        if isShot:
            drawObject(explosion,faceX,faceY)
            destroySound.play()

            face = pygame.image.load(random.choice(faceImage))
            faceSize = face.get_rect().size
            faceWidth = faceSize[0]
            faceHeight = faceSize[1]

            faceX = random.randrange(0, padWidth - faceWidth)
            faceY = 0
            destroySound = pygame.mixer.Sound(random.choice(explosionSound))
            isShot = False

            faceSpeed += 0.1
            if faceSpeed >= 10:
                faceSpeed = 10

        drawObject(face,faceX,faceY)

        #pygame.display.update()

        #gamePad.fill(BLACK)

        pygame.display.update()

        clock.tick(60)

    pygame.quit()

initGame()
runGame()