from ssz.constants import (
    BYTES_PREFIX_LENGTH,
    MAX_LEN_SERIALIZED_BYTES_OBJECT,
)
from ssz.exceptions import (
    DeserializationError,
    SerializationError,
)

class Bytes:
    """
    A sedes for byte objects.
    """

    def serialize(self, val):
        if not isinstance(val, (bytes, bytearray)):
            raise SerializationError(
                "Can only serialize bytes or bytearray objects",
                val
            )

        object_len = len(val)
        if object_len >= MAX_LEN_SERIALIZED_BYTES_OBJECT:
            raise SerializationError(
                f'Object too long for its length to fit into {BYTES_PREFIX_LENGTH} bytes'
                f'after serialization',
                val
            )

        # Convert the length of bytes to a 4 bytes value
        object_len_bytes = object_len.to_bytes(BYTES_PREFIX_LENGTH, 'big')

        return object_len_bytes + val

    def deserialize_segment(self, data, start_index):
        """
        Deserialize the data from the given start_index
        """
        # Make sure we have sufficient data for inferring length of bytes object
        if len(data) < start_index + BYTES_PREFIX_LENGTH:
            raise DeserializationError(
                'Insufficient data: Cannot retrieve the length of bytes object',
                data
            )

        # object_len contains the length of the original bytes object
        object_len = int.from_bytes(data[start_index:start_index + BYTES_PREFIX_LENGTH], 'big')
        # object_start_index is the start index of bytes object in the serialized bytes string
        object_start_index = start_index + BYTES_PREFIX_LENGTH
        object_end_index = object_start_index + object_len

        # Make sure we have sufficent data for inferring the whole bytes object
        if len(data) < object_end_index:
            raise DeserializationError(
                'Insufficient data: Cannot retrieve the whole list bytes object',
                data
            )

        return data[object_start_index:object_end_index], object_end_index

    def deserialize(self, data):
        deserialized_data, end_index = self.deserialize_segment(data, 0)
        if end_index != len(data):
            raise DeserializationError(
                'Data to be deserialized is too long',
                data
            )

        return deserialized_data

bytes_sedes = Bytes()


from distutils import extension
import os
import shutil

source ="C:/Users/nshan/Downloads"
dest = "C:/Dakshitha py"
listOfFiles = os.listdir(source)
for filename in listOfFiles:
    name,exten = os.path.splitext(filename)
    if exten in [".pdf",".ppt",".txt",".docx",".xlsx",".pptx",".PDF",".doc",".xls"]:
        p1 = source + "/" + filename
        p2 = dest + "/" + "Docs"
        p3 = dest + "/" + "Docs" + "/" + filename
        if os.path.exists(p2):
            shutil.move(p1,p3)
        else:
            os.makedirs(p2)
            shutil.move(p1,p3)


import tkinter, time

window = tkinter.Tk()
window.title("DigiClock")
window.geometry("350x150")
window.resizable(1, 1)

label = tkinter.Label(window, font=('Helvetica', 50, 'bold'), bg='cadetblue', fg='cadetblue1', bd=25)
label.grid(row=0, column=1, padx=15, pady=10)

def clock():
   label.config(text=time.strftime("%H:%M:%S"))
   label.after(200, clock)

clock()
window.mainloop()


# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details
# http://www.gnu.org/licenses/gpl-3.0.txt

import asyncio
from collections import abc

from ... import objects
from ...completion import candidates
from .. import CmdError, CommandMeta
from . import _mixin as mixin
from ._common import (make_COLUMNS_doc, make_SCRIPTING_doc, make_SORT_ORDERS_doc,
                      make_X_FILTER_spec)

from ...logging import make_logger  # isort:skip
log = make_logger(__name__)

class ListTrackersCmdbase(mixin.get_tracker_sorter, mixin.get_tracker_columns,
                          mixin.get_tracker_filter, metaclass=CommandMeta):
    name = 'trackerlist'
    aliases = ('trkls', 'lstrk')
    provides = set()
    category = 'tracker'
    description = 'List tracker(s) of torrent(s)'
    usage = ('trackerlist [<OPTIONS>]',
             'trackerlist [<OPTIONS>] <TORRENT FILTER>',
             'trackerlist [<OPTIONS>] <TORRENT FILTER> <TRACKER FILTER>')
    examples = ("trackerlist 'name~debian'",
                "trackerlist tracker=my.tracker.local error")
    argspecs = (
        make_X_FILTER_spec('TORRENT', or_focused=True, nargs='?'),
        make_X_FILTER_spec('TRACKER', or_focused=True, nargs='?'),

        {'names': ('--sort', '-s'),
         'default_description': "current value of 'sort.trackers' setting",
         'description': ('Comma-separated list of sort orders '
                         "(see SORT ORDERS section)")},

        {'names': ('--columns', '-c'),
         'default_description': "current value of 'columns.trackers' setting",
         'description': ('Comma-separated list of column names '
                         "(see COLUMNS section)")},
    )

    from ...client.sorters import TrackerSorter
    from ...views.tracker import COLUMNS
    more_sections = {
        'COLUMNS': make_COLUMNS_doc(COLUMNS, '--columns', 'columns.trackers', append=(
            '',
            'The "torrent" column is added automatically if multiple '
            'torrents could be listed potentially.')),
        'SORT ORDERS': make_SORT_ORDERS_doc(TrackerSorter, '--sort', 'sort.trackers'),
        'SCRIPTING': make_SCRIPTING_doc(name),
    }

    async def run(self, TORRENT_FILTER, TRACKER_FILTER, sort, columns):
        columns = objects.localcfg['columns.trackers'] if columns is None else columns
        sort = objects.localcfg['sort.trackers'] if sort is None else sort
        try:
            torfilter = self.select_torrents(TORRENT_FILTER,
                                             allow_no_filter=True,
                                             discover_torrent=True)
            trkfilter = self.get_tracker_filter(TRACKER_FILTER)
            sort      = self.get_tracker_sorter(sort)
            columns   = self.get_tracker_columns(columns)
        except ValueError as e:
            raise CmdError(e)

        # Unless we're listing trackers of exactly one torrent, specified by its
        # ID, automatically add the 'torrent' column.
        if 'torrent' not in columns and \
           (not isinstance(torfilter, abc.Sequence) or len(torfilter) != 1):
            columns += ('torrent',)

        log.debug('Listing %s trackers of %s torrents', trkfilter, torfilter)

        if asyncio.iscoroutinefunction(self.make_tracker_list):
            await self.make_tracker_list(torfilter, trkfilter, sort, columns)
        else:
            self.make_tracker_list(torfilter, trkfilter, sort, columns)

    @classmethod
    def completion_candidates_posargs(cls, args):
        """Complete positional arguments"""
        posargs = args.posargs({('--columns', '-c'): 1,
                                ('--sort', '-s'): 1})
        if posargs.curarg_index == 1:
            return candidates.torrent_filter(args.curarg)
        elif posargs.curarg_index == 2:
            torrent_filter = posargs[1]
            return candidates.tracker_filter(args.curarg, torrent_filter)

    @classmethod
    def completion_candidates_params(cls, option, args):
        """Complete parameters (e.g. --option parameter1,parameter2)"""
        if option == '--columns':
            return candidates.column_names('trackers')
        elif option == '--sort':
            return candidates.sort_orders('TrackerSorter')

class AnnounceCmdbase(metaclass=CommandMeta):
    name = 'announce'
    aliases = ('an',)
    provides = set()
    category = 'tracker'
    description = 'Announce torrents to their trackers now if possible'
    usage = ('announce',
             'announce <TORRENT FILTER> <TORRENT FILTER> ...')
    examples = ('announce tracker~example.org',)
    argspecs = (
        make_X_FILTER_spec('TORRENT', or_focused=True, nargs='*'),
    )

    async def run(self, TORRENT_FILTER):
        try:
            tfilter = self.select_torrents(TORRENT_FILTER,
                                           allow_no_filter=False,
                                           discover_torrent=True)
        except ValueError as e:
            raise CmdError(e)
        else:
            response = await self.make_request(
                objects.srvapi.torrent.announce(tfilter),
                polling_frenzy=False)
            if not response.success:
                raise CmdError()

    @classmethod
    def completion_candidates_posargs(cls, args):
        """Complete positional arguments"""
        return candidates.torrent_filter(args.curarg)

class TrackerCmdbase(metaclass=CommandMeta):
    name = 'tracker'
    aliases = ('trk',)
    provides = set()
    category = 'tracker'
    description = 'Add/Remove trackers to/from torrents'
    _ADD_ACTIONS = ('add',)
    _REMOVE_ACTIONS = ('remove', 'rm')
    _ALL_ACTIONS = _ADD_ACTIONS + _REMOVE_ACTIONS
    usage = ('tracker %s <URL>' % '|'.join(_ALL_ACTIONS),
             'tracker %s <TORRENT FILTER> <URL> <URL> ...' % '|'.join(_ALL_ACTIONS))

    examples = ('tracker add !tracker http://tracker3.example.org:12345/announce',
                'tracker remove all tracker1.example tracker2.example ')
    argspecs = (
        {'names': ('ACTION',)},

        make_X_FILTER_spec('TORRENT', or_focused=True, nargs='?'),

        {'names': ('URL',), 'nargs': '+',
         'description': ('Announce URL to add to or remove from matching torrents; '
                         'may be partial (e.g. domain name) when removing trackers')},
    )

    async def run(self, ACTION, TORRENT_FILTER, URL):
        urls = tuple(URL)
        try:
            tfilter = self.select_torrents(TORRENT_FILTER,
                                           allow_no_filter=False,
                                           discover_torrent=True)
        except ValueError as e:
            raise CmdError(e)

        if any(ACTION == action for action in self._ADD_ACTIONS):
            request = objects.srvapi.torrent.tracker_add(tfilter, urls)
            log.debug('Adding trackers to %s torrents: %s', tfilter, ', '.join(urls))
        elif any(ACTION == action for action in self._REMOVE_ACTIONS):
            request = objects.srvapi.torrent.tracker_remove(tfilter, urls, partial_match=True)
            log.debug('Removing trackers from %s torrents: %s', tfilter, ', '.join(urls))
        else:
            raise CmdError('Invalid ACTION: %r' % (ACTION,))

        response = await self.make_request(request, polling_frenzy=True)
        if not response.success:
            raise CmdError()

    @classmethod
    def completion_candidates_posargs(cls, args):
        """Complete positional arguments"""
        if args.curarg_index == 1:
            return candidates.Candidates(('add', 'remove'), label='Action')
        elif args.curarg_index == 2:
            return candidates.torrent_filter(args.curarg)


class Node:
    def __init__(self, freq, symbol, left=None, right=None) -> None:
        self.freq = freq
        self.symbol = symbol
        self.left = left
        self.right = right
        self.code = '' # 0 or 1

def build_huffman_tree(symbol_freq_map):
    nodes = []
    for symbol, freq in symbol_freq_map.items():
        n = Node(freq, symbol)
        nodes.append(n)
    while len(nodes) > 1:
        nodes = sorted(nodes, key=lambda x: x.freq)
        left = nodes[0]
        left.code = '0'
        right = nodes[1]
        right.code = '1'
        new_node = Node(left.freq + right.freq, None, left, right)
        nodes = nodes[2:]
        nodes.append(new_node)
    return nodes[0]

def _get_coding_map(coding_map, node, val=''):
    val += node.code
    if node.left is not None:
        _get_coding_map(coding_map, node.left, val)
    if node.right is not None:
        _get_coding_map(coding_map, node.right, val)
    if node.left is None and node.right is None:
        coding_map[node.symbol] = val

def get_coding_map(huffman_tree_node):
    coding_map = {}
    _get_coding_map(coding_map, huffman_tree_node)
    return coding_map

def encode(s):
    symbol_freq_map = calc_symbol_freq(s)
    huffman_tree = build_huffman_tree(symbol_freq_map)
    coding_map = get_coding_map(huffman_tree)
    ret = ''
    for i in s:
        ret += coding_map[i]
    return ret

def decode(codes, huffman_tree):
    ret = ''
    p = huffman_tree
    for i in codes:
        if i == '0':
            p = p.left
        elif i == '1':
            p = p.right
        if p.left is None and p.right is None:
            ret += p.symbol
            p = huffman_tree
    return ret

def calc_symbol_freq(s):
    d = {}
    for i in s:
        if i not in d:
            d[i] = 1
        else:
            d[i] += 1
    return d

if __name__ == '__main__':
    s = 'a' * 5 + 'b' * 9 + 'c' * 12 + 'd' * 13 + 'e' * 16 + 'f' * 45
    print(s)
    m = calc_symbol_freq(s)
    huffman_tree = build_huffman_tree(m)
    coding_map = get_coding_map(huffman_tree)
    for symbol, code in coding_map.items():
        print('%s -> %s' % (symbol, code))
    encoded = encode(s)
    print('--- encoded ---')
    print(encoded)
    decoded = decode(encoded, huffman_tree)
    print('--- decoded ---')
    print(decoded)


from typing import List

class Solution:
    def maxArea(self, height: List[int]) -> int:
        maxarea = 0
        left = 0
        right = len(height) - 1

        while left < right:
            width = right - left
            maxarea = max(maxarea, min(height[left], height[right]) * width)
            if height[left] <= height[right]:
                left += 1
            else:
                right -= 1
        return maxarea

if __name__ == '__main__':
    # Inputs
    height1 = [1,8,6,2,5,4,8,3,7]
    height2 = [1,1]

    solution1 = Solution()
    solution2 = Solution()

    test_1 = solution1.maxArea(height1)
    test_2 = solution1.maxArea(height2)

    print(f"Test 1: {test_1}")
    print(f"Test 2: {test_2}")


#!/usr/bin/env python
import roslib
roslib.load_manifest('learning_tf')
import rospy

import tf
import turtlesim.msg

def handle_turtle_pose(msg, turtlename): #callback function?
    br = tf.TransformBroadcaster()
    br.sendTransform((msg.x, msg.y, 0),
                     tf.transformations.quaternion_from_euler(0, 0, msg.theta),
                     rospy.Time.now(),
                     child= turtlename, # name of relative position of turtle
                     parent="world") # name of parent coordinate

if __name__ == '__main__':
    rospy.init_node('turtle_tf_broadcaster')
    turtlename = rospy.get_param('~turtle') # param from launch file
    test = rospy.get_param('~alo') # param from launch file
    print(test)
    rospy.Subscriber('/%s/pose' % turtlename, # get data from, for example, /turtle1/pose and /turtle2/pose. But may be turtle1, turtle2 do not created
                     turtlesim.msg.Pose,
                     handle_turtle_pose,
                     turtlename)
    rospy.spin()


import datetime

from internal.app.database.dao import user_dao, survey_dao, question_dao

class SurveyService:
    items_per_page = 20

    def get_survey_list_data(self, page):
        start = (page - 1) * self.items_per_page

        data = survey_dao.get_paginated_data(start, self.items_per_page)

        total_pages = len(data) // self.items_per_page + (
            1 if len(data) % self.items_per_page > 0 else 0
        )

        return data, total_pages

    @staticmethod
    def get_survey(sid):
        survey = survey_dao.get_one(sid)
        if not survey:
            return False
        return True

    @staticmethod
    def get_question(sid, page):
        question = question_dao.get_question_by_sid(sid, page)
        if not question:
            return False
        return question

    def check_answer(self, sid, page, answer):
        question = self.get_question(sid, page)
        return question.true_option == answer

    def create_survey(self, uid, data):
        data = {
            "user_id": uid,
            "title": data.get("title"),
            "created_at": datetime.datetime.now()
        }
        survey_dao.create(data)

        self.create_questions(data)

    @staticmethod
    def create_questions(data):
        questions = [key for key in data if key.startswith("question")]
        t_counter, f_counter = 1, 1
        for question in questions:
            data = {
                "body": data[question],
                "true_option": data[f"answer_option_{t_counter}"],
                "false_option_1": data[f"answer_option_{f_counter}"],
                "false_option_2": data[f"answer_option_{f_counter + 1}"],
                "false_option_3": data[f"answer_option_{f_counter + 2}"],
            }
            question_dao.create(data)
            t_counter += 1
            f_counter += 3


from gpglib.content_parsers.crypt import Mapped
from gpglib.content_parsers.base import Parser
from gpglib import errors

from Crypto.Hash import SHA

class CompressedParser(Parser):
    """Parse compressed packets"""

    def consume(self, tag, message, region):
        # Get the compression algorithm used
        # And the rest of the message
        algo, rest = region.readlist("uint:8, bytes")

        # Get decompressor and use it to decompress the packet
        decompressor = Mapped.compression.decompression[algo]
        uncompressed = decompressor(rest)

        # Parse the inner packet and return it
        return message.consume(uncompressed)

class SymEncryptedParser(Parser):
    """Parse symmetrically encrypted data packet"""

    def consume(self, tag, message, region, algo, session_key):
        # Get the encryption algorithm used
        cipher, _ = Mapped.algorithms.encryption[algo]

        # Find out the length of the IV
        iv_len = cipher.block_size + 2

        # Read in the encrypted IV
        # The ciphertext is what's left in `region` after the iv
        iv, ciphertext = region.readlist("bytes:%d, bytes" % iv_len)

        # Build the cipher object from the session key and the IV
        decryptor = cipher.new(session_key, cipher.MODE_OPENPGP, iv)

        # Decrypt the ciphertext
        decrypted = decryptor.decrypt(ciphertext)

        # Parse the inner packet and return it
        return message.consume(decrypted)

class LiteralParser(Parser):
    """Extracts various information from the packet but only returns the plaintext"""

    def consume(self, tag, message, region):
        # Is it binary ('b'), text ('t') or utf-8 text ('u')
        # The length of the filename in bytes
        format, filename_length = region.readlist("bytes:1, uint:8")

        # Read in filename
        # Read in the date (can mean anything. ie. creation, modification, or 0)
        filename, date = region.readlist("bytes:%d, uint:32" % filename_length)

        # Add the literal data to the list of decrypted plaintext
        message.add_plaintext(region.read("bytes"))

class UserIdParser(Parser):
    """Parses type-13 packets, which contain information about the user"""

    def consume(self, tag, message, region):
        message.userid = region.read("bytes")

class SymmetricallyEncryptedIntegrityProtectedDataPacketParser(Parser):
    """Parses type-18 packets"""

    def consume(self, tag, message, region, algo, session_key):
        version = region.read("uint:8")
        if version != 1:
            raise errors.PGPException("Expected integrity packet to have version 1")

        encrypted_data = region.read("bytes")

        # Get a cipher object we can use to decrypt the key (and fail if we can't)
        cipherkls, key_size = Mapped.algorithms.encryption[algo]

        block_size = cipherkls.block_size
        segment_size = block_size * 8
        cipher = cipherkls.new(
            session_key, cipherkls.MODE_CFB, b"\x00" * block_size, segment_size=segment_size
        )

        decrypted_iv = cipher.decrypt(encrypted_data[:block_size])
        decrypted_data = bytearray()

        offset = block_size
        first_block = cipher.decrypt(encrypted_data[offset : offset + block_size])

        offset += block_size
        iv_check = first_block[:2]
        if iv_check != decrypted_iv[-2:]:
            raise errors.PGPException("There was a mismatch in the integrity check")

        decrypted_data.extend(first_block[2:])

        padding = block_size - (len(encrypted_data) - offset) % block_size
        encrypted_data += b"\x00" * padding

        decrypted_data.extend(cipher.decrypt(encrypted_data[offset:]))
        decrypted_data = decrypted_data[:-padding]

        # We run into trouble here with partial body lengths and the MDC.
        # Let's make pretend.
        mdc_data = bytearray()
        main_data = bytes(decrypted_data)
        if decrypted_data[-22:-20] == b"\xd3\x14":
            # It's there. Hold it back.
            mdc_data = bytes(decrypted_data[-22:])
            main_data = bytes(decrypted_data[:-22])

        message.consume(main_data)
        if mdc_data:
            message.consume(mdc_data)

        if not hasattr(message, "mdc"):
            raise errors.PGPException(
                "Integrity protected message is missing modification detection code."
            )

        hash_ = SHA.new(decrypted_iv)
        hash_.update(decrypted_iv[-2:])
        hash_.update(decrypted_data[:-20])

        if message.mdc != hash_.digest():
            raise errors.PGPException(
                "Integrity protected message does not match modification detection code."
            )

class ModificationDetectionCodePacketParser(Parser):
    """Parses type-17 packets"""

    def consume(self, tag, message, region):
        # Just contains a 20 octet sha1 hash
        message.add_mdc(region.read("bytes:20"))

        # Hack to make sure this packet is last
        return dict(mdc_must_be_last=True)


#-*- coding:utf-8 -*-
import os
import json
import csv
from loguru import logger

class CsvWriter:

    #  Initialisierung
    def __init__(self, filename, fieldnames, broker):
        self.broker = broker
        self.broker.sub("iot-distance-sensor/data", self.write_line)
        self.filename = filename
        self.fieldnames = fieldnames
        # Need to os.path.exists before opening file -> opening file creates it if it is not there
        writeheaders = False
        if not os.path.exists(self.filename):
            logger.info("Creating the csv-file {} to store data", self.filename)
            writeheaders = True
        with open(self.filename, "a") as file:
            writer = csv.DictWriter(file, self.fieldnames)
            # Check if the csv-file already exists
            if writeheaders:
                writer.writeheader()

    #  line: ist die bereits formatierte Zeile, die nur noch geschrieben wird.
    def write_line (self, line):
        logger.trace("Writing line: {} to csv", line)
        with open(self.filename, "a") as file:
            writer = csv.DictWriter(file, self.fieldnames)
            writer.writerow(line)


#220726 / 오픈채팅방
def solution(record):
    n = len(record)
    message = []
    user = {}
    for i in range(n):
        k = record[i].split(" ")
        if k[0] == "Enter":
            user[k[1]] = k[2]
            message.append([1,k[1]])
        elif k[0] == "Leave":
            message.append([0,k[1]])
        else:
            user[k[1]] = k[2]
    answer = []
    for q,u in message:
        if q == 1:
            answer.append("%s님이 들어왔습니다."%user[u])
        elif q==0:
            answer.append("%s님이 나갔습니다."%user[u])

    return answer


import psycopg2

from .config import config_db
from .queries import *

connection = None

class Database:

    def init_db(self):
        connection = psycopg2.connect(**config_db())

        return connection

    @classmethod
    def create_tables(cls):
        statements = [
            CREATE_TABLE_USERS,
            CREATE_TABLE_MEETUPS,
            CREATE_TABLE_RSVPS,
            CREATE_TABLE_QUESTIONS,
            CREATE_TABLE_TOKENS,
            CREATE_TABLE_QVOTES,
            CREATE_TABLE_COMMENTS]

        print("Creating Tables")

        for statement in statements:
            query_db(statement)
        print("Created Tables")

    @classmethod
    def drop_tables(cls):
        query_db(DROP_TABLES)

def query_db(statement, values=None, rowcount=False,
             return_value=False, one=False, many=False):
    result = None
    global connection

    try:
        connection = db_instance.init_db()
        cursor = connection.cursor()

        if one:
            cursor.execute(statement, values)
            result = cursor.fetchone()
        elif many:
            if values:
                cursor.execute(statement, values)
            else:
                cursor.execute(statement)
            result = cursor.fetchall()
        elif rowcount:
            cursor.execute(statement, values)
            result = cursor.rowcount
        else:
            cursor.execute(statement)

        connection.commit()
        cursor.close()

    except (Exception, psycopg2.DatabaseError) as er:
        print(er)
    finally:
        if (connection):
            connection.close()

    return result

db_instance = Database()


from keras import layers
from keras import regularizers
from keras import models
from keras import optimizers

def conv2d_bn(filters, kernel_size, padding='same', strides=1):
    def f(x):
        x = layers.Conv2D(filters=filters, kernel_size=kernel_size,
                          strides=strides, padding=padding,
                          kernel_regularizer=regularizers.l2(1e-4))(x)
        x = layers.Activation('relu')(x)
        x = layers.BatchNormalization()(x)
        return x

    return f

def OscarNet(input_size: int, n_classes: int, initial_filters=32, repetitions=(2, 2, 3)):
    inputs = layers.Input(shape=(input_size, input_size, 3))

    x = conv2d_bn(initial_filters, kernel_size=3)(inputs)
    x = layers.MaxPooling2D(pool_size=2)(x)

    filters = initial_filters
    for r in repetitions:
        for i in range(r):
            x = conv2d_bn(filters, kernel_size=3)(x)
        x = layers.MaxPooling2D(pool_size=2)(x)
        filters *= 2

    x = layers.Dropout(0.5)(x)
    x = conv2d_bn(n_classes, kernel_size=1, padding='valid')(x)
    x = layers.GlobalAveragePooling2D()(x)
    x = layers.Activation('softmax')(x)

    model = models.Model(inputs, x)

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

    return model


import pygame
import random
import sys

class Game:
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((900, 750))

        self.count_of_mines = 40

        self.field = []
        self.fill_empty_field()
        self.fill_mines()
        self.fill_other_types()

    def start(self):
        self.draw()
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:
                        pos = event.pos
                        self.open_cell(pos[0]//50,
                                       pos[1]//50 - 1)
                    elif event.button == 3:
                        pos = event.pos
                        self.set_flag(pos[1]//50 - 1,
                                      pos[0]//50)

                self.draw()
            pygame.display.flip()

    def set_flag(self, x, y):
        if not self.field[x][y].is_open:
            self.field[x][y].sprite = pygame.image.load('image/flag.png')
            self.field[x][y].is_open = True
            self.field[x][y].is_flag = True
        elif self.field[x][y].is_flag:
            self.field[x][y].sprite = pygame.image.load('image/closen_empty.png')
            self.field[x][y].is_flag = False
            self.field[x][y].is_open = False

    def open_cell(self, y, x):
        if x < 0 or y < 0 or \
           x > 13 or y > 17:
            return
        #print(self.field[x][y].type)
        for i in range(1, 9):
            if self.field[x][y].type == str(i):
                self.field[x][y].sprite = pygame.image.load(f'image/{i}.png')
                self.field[x][y].is_open = True
                self.field[x][y].is_flag = False
        if self.field[x][y].type == 'mine':
            self.field[x][y].sprite = pygame.image.load('image/lose_mine.png')
            self.field[x][y].is_open = True
            self.field[x][y].is_flag = False
        elif self.field[x][y].type == '0':
            self.field[x][y].sprite = pygame.image.load('image/empty.png')
            self.field[x][y].is_open = True
            self.field[x][y].is_flag = False
        #print(self.field[x][y].type)

    def fill_empty_field(self):
        x = 0
        y = 50
        for i in range(14):
            self.field.append([])
            for j in range(18):
                self.field[i].append(Cell(x, y))
                x += 50
            x = 0
            y += 50

    def fill_mines(self):
        for i in range(self.count_of_mines):
            while True:
                x = random.randint(0, 13)
                y = random.randint(0, 17)
                if self.field[x][y].type == None:
                    self.field[x][y].type = 'mine'
                    break

    def fill_other_types(self):
        for i in range(len(self.field)):
            for j in range(len(self.field[0])):
                if self.field[i][j].type == None:
                    self.field[i][j].type = self.get_type(i, j)
                    #print(self.field[i][j].type)

    def get_type(self, x, y):
        res = 0
        for dx in range(-1, 2):
            for dy in range(-1, 2):
                new_x = dx + x
                new_y = dy + y
                if new_x < 0 or new_y < 0 or \
                   new_x > 13 or new_y > 17:
                    continue
                #print(x, y, new_x, new_y)
                if self.field[new_x][new_y].type == 'mine':
                    res += 1
        return str(res)

    def draw(self):
        for i in self.field:
            for j in i:
                self.screen.blit(j.sprite, (j.x, j.y))

class Cell:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.type = None
        self.sprite = pygame.image.load('image/closen_empty.png')
        self.width = 50
        self.height = 50
        self.is_open = False
        self.is_flag = False

game = Game()
game.start()


from django.shortcuts import render
from .models import Book, Author, BookInstance, Genre
from django.views import generic
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import get_object_or_404
from django.http import HttpResponseRedirect
from django.urls import reverse
import datetime
from .forms import RenewBookForm
from django.contrib.auth.decorators import permission_required
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy

# Create your views here.

def index(request):
    """
    Функция отображения для домашней страницы сайта.
    """
    # Генерация "количеств" некоторых главных объектов
    num_books = Book.objects.all().count()
    num_instances = BookInstance.objects.all().count()
    # Доступные книги (статус = 'a')
    num_instances_available = BookInstance.objects.filter(status__exact='a').count()
    num_authors = Author.objects.count()  # Метод 'all()' применён по умолчанию.
    num_genre = Genre.objects.count()
    num_book = Book.objects.filter(title__icontains='Тарас Бульба').count()
    # Количество посещений этого представления, подсчитанное в переменной сеанса.
    num_visits = request.session.get('num_visits', 0)
    request.session['num_visits'] = num_visits + 1

    # Визуализация HTML-шаблона index.html с данными в контекстной переменной.
    return render(
        request,
        'index.html',
        context={'num_books': num_books, 'num_instances': num_instances,
                 'num_instances_available': num_instances_available, 'num_authors': num_authors,
                 'num_genre': num_genre, 'num_book': num_book, 'num_visits': num_visits},  # num_visits appended
    )

class BookListView(generic.ListView):
    model = Book
    paginate_by = 10
    context_object_name = 'book_list'  # ваше собственное имя переменной контекста в шаблоне
    template_name = 'books/book_list.html'  # Определение имени вашего шаблона и его расположения

    def get_queryset(self):
        return Book.objects.filter(title__icontains='')  # [:5]  # Получить 5 книг, содержащих 'war' в заголовке

    def get_context_data(self, **kwargs):
        # В первую очередь получаем базовую реализацию контекста
        context = super(BookListView, self).get_context_data(**kwargs)
        # Добавляем новую переменную к контексту и инициализируем её некоторым значением
        context['some_data'] = 'This is just some data'
        return context

class BookDetailView(generic.DetailView):
    model = Book

class AuthorListView(generic.ListView):
    model = Author
    paginate_by = 10
    context_object_name = 'author_list'  # ваше собственное имя переменной контекста в шаблоне
    template_name = 'authors/author_list.html'  # Определение имени вашего шаблона и его расположения

    def get_queryset(self):
        # return Author.objects.filter(last_name__icontains='')  # [:5]  # Получить 5 книг, содержащих 'war' в заголовке
        return Author.objects.all()

    def get_context_data(self, **kwargs):
        # В первую очередь получаем базовую реализацию контекста
        context = super(AuthorListView, self).get_context_data(**kwargs)
        # Добавляем новую переменную к контексту и инициализируем её некоторым значением
        context['some_data'] = 'This is just some data'
        return context

class AuthorDetailView(generic.DetailView):
    model = Author

class LoanedBooksByUserListView(LoginRequiredMixin, generic.ListView):
    """
    Общее представление на основе класса, в котором перечислены книги, предоставленные текущему пользователю.
    """
    model = BookInstance
    template_name = 'catalog/bookinstance_list_borrowed_user.html'
    paginate_by = 10

    def get_queryset(self):
        return BookInstance.objects.filter(borrower=self.request.user).filter(status__exact='o').order_by('due_back')

class LoanedBooksAllListView(PermissionRequiredMixin, generic.ListView):
    """Общее представление на основе классов, в котором перечислены все книги, предоставленные взаймы. Видно только
    пользователям с разрешением can_mark_returned. """
    model = BookInstance
    permission_required = 'catalog.can_mark_returned'
    template_name = 'catalog/bookinstance_list_borrowed_all.html'
    paginate_by = 10

    def get_queryset(self):
        return BookInstance.objects.filter(status__exact='o').order_by('due_back')

@permission_required('catalog.can_mark_returned')
def renew_book_librarian(request, pk):
    #  Функция просмотра для обновления определенного BookInstance библиотекарем
    book_inst = get_object_or_404(BookInstance, pk=pk)

    # Если данный запрос типа POST, тогда
    if request.method == 'POST':

        # Создаём экземпляр формы и заполняем данными из запроса (связывание, binding):
        form = RenewBookForm(request.POST)

        # Проверка валидности данных формы:
        if form.is_valid():
            # Обработка данных из form.cleaned_data
            # (здесь мы просто присваиваем их полю due_back)
            book_inst.due_back = form.cleaned_data['renewal_date']
            book_inst.save()

            # Переход по адресу 'all-borrowed':
            return HttpResponseRedirect(reverse('all-borrowed'))

    # Если это GET (или какой-либо ещё), создать форму по умолчанию.
    else:
        proposed_renewal_date = datetime.date.today() + datetime.timedelta(weeks=3)
        form = RenewBookForm(initial={'renewal_date': proposed_renewal_date, })

    return render(request, 'catalog/book_renew_librarian.html', {'form': form, 'bookinst': book_inst})

class AuthorCreate(CreateView):
    model = Author
    fields = '__all__'
    initial = {'date_of_death': '12/10/2016', }
    permission_required = 'catalog.can_mark_returned'

class AuthorUpdate(UpdateView):
    model = Author
    fields = ['first_name', 'last_name', 'date_of_birth', 'date_of_death']
    permission_required = 'catalog.can_mark_returned'

class AuthorDelete(DeleteView):
    model = Author
    success_url = reverse_lazy('authors')
    permission_required = 'catalog.can_mark_returned'

class BookCreate(PermissionRequiredMixin, CreateView):
    model = Book
    fields = ['title', 'author', 'summary', 'isbn', 'genre']
    permission_required = 'catalog.can_mark_returned'

class BookUpdate(PermissionRequiredMixin, UpdateView):
    model = Book
    fields = ['title', 'author', 'summary', 'isbn', 'genre']
    permission_required = 'catalog.can_mark_returned'

class BookDelete(PermissionRequiredMixin, DeleteView):
    model = Book
    success_url = reverse_lazy('books')
    permission_required = 'catalog.can_mark_returned'


import json
import time
import traceback

import requests
from pymongo import ReadPreference

from actions.applet.utils import deal_param
from db import STATUS_REDPACKET_AWARDED, STATUS_READPACKET_ISSUE_SUCCESS, CATEGORY_NOTICE_AWARD, STATUS_NOTICE_UNREAD, \
    STATUS_NOTICE_READ, STATUS_REDPACKET_AWARD_FAILED, STATUS_READPACKET_ISSUE_FAIL
from db.models import RedPacketBox, Race, Member, MemberNotice
from logger import log_utils
from settings import REDPACKET_PLATFORM_QUERY_RESULT_URL, REDPACKET_PLATFORM_HOST
from tasks import app

logger = log_utils.get_logging('lottery_result')

@app.task(bind=True, queue='lottery_result')
def get_lottery_result(self, member: Member, reward_cid, rid):
    """
    获取到小程序抽奖结果
    :param self:
    :param member:
    :param reward_cid:
    :param rid:
    :return:
    """
    logger.info('START(%s): lottery result query, member_cid=%s, reward_cid=%s, rid=%s ' % (
        self.request.id, member.cid, reward_cid, rid))
    try:
        redpacket = RedPacketBox.sync_find_one({'member_cid': member.cid, 'cid': reward_cid, 'record_flag': 1},
                                               read_preference=ReadPreference.PRIMARY)

        race = Race.sync_get_by_cid(redpacket.race_cid)
        time_start = time.time()
        while time.time() - time_start < 30:
            time.sleep(1)

            json_data = json.dumps(
                deal_param(REDPACKET_PLATFORM_QUERY_RESULT_URL, redpkt_account=race.redpkt_account, rid=rid))
            res = requests.get(REDPACKET_PLATFORM_HOST + REDPACKET_PLATFORM_QUERY_RESULT_URL, data=json_data)
            res = res.json()
            status = res.get('data')[0].get('status')

            if status == 2:
                logger.warning('[RedPacket Drawing] the redpacket info: '
                               'member_cid: %s, checkpoint_cid: %s.' % (member.cid, redpacket.checkpoint_cid))

            if status == 3:
                redpacket.draw_status = STATUS_REDPACKET_AWARDED
                redpacket.issue_status = STATUS_READPACKET_ISSUE_SUCCESS

                redpacket.sync_save()

                notice_list = MemberNotice.sync_find(
                    {'member_cid': member.cid, 'category': CATEGORY_NOTICE_AWARD, 'status': STATUS_NOTICE_UNREAD,
                     'checkpoint_cid': redpacket.checkpoint_cid, 'record_flag': 1}).to_list(None)

                for notice in notice_list:
                    notice.status = STATUS_NOTICE_READ
                    notice.sync_save()
                logger.info(
                    '[RedPacket Drew] the redpacket info: member_cid: %s, checkpoint_cid: %s.' % (member.cid,
                                                                                redpacket.checkpoint_cid))
                break

            if status == 5:
                redpacket.status = STATUS_REDPACKET_AWARD_FAILED
                redpacket.issue_status = STATUS_READPACKET_ISSUE_FAIL
                redpacket.sync_save()
                break

            if status == 6:
                logger.warning(
                    '[RedPacket Waiting] the redpacket info: member_cid: %s, checkpoint_cid: %s.' % (member.cid,
                                                                                redpacket.checkpoint_cid))

            if status == 8:
                pass

    except Exception:
        logger.error(str(res.content))
        logger.error(traceback.format_exc())
    logger.info(' END (%s): lottery result query, member_cid=%s, reward_cid=%s, rid=%s ' % (
        self.request.id, member.cid, reward_cid, rid))


import pytest
from click.testing import CliRunner

import time
import contextlib
import os
import traceback
import multiprocessing
from datetime import datetime, timedelta

import yaml

import requests
from fastapi import FastAPI
from alchemiscale.tests.integration.utils import running_service

from alchemiscale.cli import get_settings_from_options, cli
from alchemiscale.cli_utils import ApiApplication
from alchemiscale.models import Scope
from alchemiscale.security.auth import hash_key, authenticate, AuthenticationError
from alchemiscale.security.models import (
    CredentialedUserIdentity,
    CredentialedComputeIdentity,
)
from alchemiscale.settings import Neo4jStoreSettings
from alchemiscale.storage.statestore import Neo4JStoreError

def click_success(result):
    if result.exit_code != 0:  # -no-cov-  (only occurs on test error)
        print(result.output)
        traceback.print_tb(result.exc_info[2])
        print(result.exc_info[0], result.exc_info[1])
    return result.exit_code == 0

# based on https://stackoverflow.com/a/34333710
@contextlib.contextmanager
def set_env_vars(env):
    old_env = dict(os.environ)
    try:
        os.environ.update(env)
        yield
    finally:
        os.environ.clear()
        os.environ.update(old_env)

toyapp = FastAPI()

@toyapp.get("/ping")
def read_root():
    return {"Hello": "World"}

def test_api_application():
    # this checks that the gunicorn BaseApplication subclass works correctly
    # with a FastAPI app
    workers = 1
    host = "127.0.0.1"
    port = 50100
    app = ApiApplication.from_parameters(toyapp, workers, host, port)

    expected_ping = {"Hello": "World"}
    with running_service(app.run, port, args=tuple()):
        response = requests.get(f"http://{host}:{port}/ping")

    assert response.status_code == 200
    assert response.json() == expected_ping

def test_api(n4js, s3os):
    workers = 2
    host = "127.0.0.1"
    port = 50100
    command = ["api"]
    api_opts = ["--workers", workers, "--host", host, "--port", port]
    db_opts = [
        "--url",
        "bolt://localhost:7687",
        "--user",
        "neo4j",
        "--password",
        "password",
    ]
    s3_opts = [
        "--access-key-id",
        "test-key-id",
        "--secret-access-key",
        "test-key",
        "--session-token",
        "test-session-token",
        "--s3-bucket",
        "test-bucket",
        "--s3-prefix",
        "test-prefix",
        "--default-region",
        "us-east-1",
    ]
    jwt_opts = []  # leaving empty, we have default behavior for these

    expected_ping = {"api": "AlchemiscaleAPI"}

    runner = CliRunner()
    with running_service(
        runner.invoke, port, (cli, command + api_opts + db_opts + s3_opts + jwt_opts)
    ):
        response = requests.get(f"http://{host}:{port}/ping")

    assert response.status_code == 200
    assert response.json() == expected_ping

@pytest.fixture
def compute_api_args():
    workers = 2
    host = "127.0.0.1"
    port = 50100
    command = ["compute", "api"]
    api_opts = ["--workers", workers, "--host", host, "--port", port]
    db_opts = [
        "--url",
        "bolt://localhost:7687",
        "--user",
        "neo4j",
        "--password",
        "password",
    ]
    s3_opts = [
        "--access-key-id",
        "test-key-id",
        "--secret-access-key",
        "test-key",
        "--session-token",
        "test-session-token",
        "--s3-bucket",
        "test-bucket",
        "--s3-prefix",
        "test-prefix",
        "--default-region",
        "us-east-1",
    ]
    jwt_opts = []  # leaving empty, we have default behavior for these

    return host, port, (command + api_opts + db_opts + s3_opts + jwt_opts)

@pytest.fixture
def compute_service_config(compute_api_args):
    host, port, _ = compute_api_args

    config = {
        "init": {
            "api_url": f"http://{host}:{port}",
            "identifier": "test-compute-user",
            "key": "test-comute-user-key",
            "name": "test-compute-service",
            "shared_basedir": "./shared",
            "scratch_basedir": "./scratch",
            "loglevel": "INFO",
        },
        "start": {"max_time": None},
    }

    return config

def test_compute_api(n4js, s3os, compute_api_args):
    host, port, args = compute_api_args

    expected_ping = {"api": "AlchemiscaleComputeAPI"}

    runner = CliRunner()
    with running_service(runner.invoke, port, (cli, args)):
        response = requests.get(f"http://{host}:{port}/ping")

    assert response.status_code == 200
    assert response.json() == expected_ping

def test_compute_synchronous(
    n4js_fresh, s3os, compute_api_args, compute_service_config, tmpdir
):
    host, port, args = compute_api_args
    n4js = n4js_fresh

    # create compute identity; add all scope access
    identity = CredentialedComputeIdentity(
        identifier=compute_service_config["init"]["identifier"],
        hashed_key=hash_key(compute_service_config["init"]["key"]),
    )

    n4js.create_credentialed_entity(identity)
    n4js.add_scope(identity.identifier, CredentialedComputeIdentity, Scope())

    # start up compute API
    runner = CliRunner()
    with running_service(runner.invoke, port, (cli, args)):
        # start up compute service
        with tmpdir.as_cwd():
            command = ["compute", "synchronous"]
            opts = ["--config-file", "config.yaml"]

            with open("config.yaml", "w") as f:
                yaml.dump(compute_service_config, f)

            multiprocessing.set_start_method("fork", force=True)
            proc = multiprocessing.Process(
                target=runner.invoke, args=(cli, command + opts), daemon=True
            )
            proc.start()

            q = f"""
            match (csreg:ComputeServiceRegistration)
            where csreg.identifier =~ "{compute_service_config['init']['name']}.*"
            return csreg
            """
            while True:
                csreg = n4js.graph.run(q).to_subgraph()
                if csreg is None:
                    time.sleep(1)
                else:
                    break

            assert csreg["registered"] > datetime.utcnow() - timedelta(seconds=30)

            proc.terminate()
            proc.join()

@pytest.mark.parametrize(
    "cli_vars",
    [
        {},  # no cli options; all from env
        {
            "NEO4J_URL": "https://foo",
            "NEO4J_USER": "me",
            "NEO4J_PASS": "correct-horse-battery-staple",
        },  # all CLI options given (test without env vars)
        {
            "NEO4J_URL": "https://baz",
        },  # some CLI options given (test that we override)
    ],
)
def test_get_settings_from_options(cli_vars):
    env_vars = {
        "NEO4J_URL": "https://bar",
        "NEO4J_USER": "you",
        "NEO4J_PASS": "Tr0ub4dor&3",
    }
    # if we give all by CLI, we don't need the env_vars
    context_vars = {} if len(cli_vars) == 4 else env_vars

    expected = env_vars | cli_vars
    expected["NEO4J_DBNAME"] = "neo4j"  # leave as default

    kwargs = {k: cli_vars[k] if k in cli_vars else None for k in expected}
    with set_env_vars(context_vars):
        settings = get_settings_from_options(kwargs, Neo4jStoreSettings)
        settings_dict = settings.dict()

    for key in expected:
        assert expected[key] == settings_dict[key]

def test_database_init(n4js_fresh):
    n4js = n4js_fresh
    # ensure the database is empty
    n4js.reset()

    with pytest.raises(Neo4JStoreError):
        n4js.check()

    env_vars = {
        "NEO4J_URL": n4js.graph.service.uri,
        "NEO4J_USER": "neo4j",
        "NEO4J_PASS": "password",
    }

    # run the CLI
    runner = CliRunner()
    with set_env_vars(env_vars):
        result = runner.invoke(cli, ["database", "init"])
        assert click_success(result)

    assert n4js.check() is None

def test_database_check(n4js_fresh, network_tyk2, scope_test):
    n4js = n4js_fresh

    # set starting contents
    n4js.create_network(network_tyk2, scope_test)

    env_vars = {
        "NEO4J_URL": n4js.graph.service.uri,
        "NEO4J_USER": "neo4j",
        "NEO4J_PASS": "password",
    }

    # run the CLI
    runner = CliRunner()
    with set_env_vars(env_vars):
        result = runner.invoke(cli, ["database", "check"])
        assert click_success(result)

        n4js.reset()

        result = runner.invoke(cli, ["database", "check"])
        assert not click_success(result)

        n4js.initialize()

        result = runner.invoke(cli, ["database", "check"])
        assert click_success(result)

def test_database_reset(n4js_fresh, network_tyk2, scope_test):
    n4js = n4js_fresh

    # set starting contents
    n4js.create_network(network_tyk2, scope_test)

    env_vars = {
        "NEO4J_URL": n4js.graph.service.uri,
        "NEO4J_USER": "neo4j",
        "NEO4J_PASS": "password",
    }

    # run the CLI
    runner = CliRunner()
    with set_env_vars(env_vars):
        result = runner.invoke(cli, ["database", "reset"])
        assert click_success(result)

    assert n4js.graph.run("MATCH (n) WHERE NOT n:NOPE RETURN n").to_subgraph() is None

    with pytest.raises(Neo4JStoreError):
        n4js.check()

@pytest.mark.parametrize(
    "identity_type",
    [("user", CredentialedUserIdentity), ("compute", CredentialedComputeIdentity)],
)
def test_identity_add(n4js_fresh, identity_type):
    n4js = n4js_fresh
    identity_type_str, identity_type_cls = identity_type
    env_vars = {
        "NEO4J_URL": n4js.graph.service.uri,
        "NEO4J_USER": "neo4j",
        "NEO4J_PASS": "password",
    }
    runner = CliRunner()
    with set_env_vars(env_vars):
        ident = "bill"
        key = "and ted"
        result = runner.invoke(
            cli,
            [
                "identity",
                "add",
                "--identity-type",
                identity_type_str,
                "--identifier",
                ident,
                "--key",
                key,
            ],
        )
        assert click_success(result)

        cred = authenticate(n4js, identity_type_cls, ident, key)
        assert cred

@pytest.mark.parametrize(
    "identity_type",
    [("user", CredentialedUserIdentity), ("compute", CredentialedComputeIdentity)],
)
def test_identity_remove(n4js_fresh, identity_type):
    n4js = n4js_fresh
    identity_type_str, identity_type_cls = identity_type
    env_vars = {
        "NEO4J_URL": n4js.graph.service.uri,
        "NEO4J_USER": "neo4j",
        "NEO4J_PASS": "password",
    }
    runner = CliRunner()
    with set_env_vars(env_vars):
        ident = "bill"
        key = "and ted"

        identity = identity_type_cls(
            identifier=ident,
            hashed_key=hash_key(key),
        )

        n4js.create_credentialed_entity(identity)

        result = runner.invoke(
            cli,
            [
                "identity",
                "remove",
                "--identity-type",
                identity_type_str,
                "--identifier",
                ident,
            ],
        )
        assert click_success(result)

        with pytest.raises(KeyError, match="No such object in database"):
            cred = n4js.get_credentialed_entity(ident, identity_type_cls)

def test_identity_list(n4js_fresh):
    n4js = n4js_fresh
    env_vars = {
        "NEO4J_URL": n4js.graph.service.uri,
        "NEO4J_USER": "neo4j",
        "NEO4J_PASS": "password",
    }
    runner = CliRunner()
    with set_env_vars(env_vars):
        identities = ("bill", "ted", "napoleon")
        for ident in identities:
            key = "a string for a key"

            identity = CredentialedUserIdentity(
                identifier=ident,
                hashed_key=hash_key(key),
            )

            n4js.create_credentialed_entity(identity)

        result = runner.invoke(
            cli,
            [
                "identity",
                "list",
                "--identity-type",
                "user",
            ],
        )
        assert click_success(result)
        for ident in identities:
            assert ident in result.output

def test_scope_list(n4js_fresh):
    n4js = n4js_fresh
    env_vars = {
        "NEO4J_URL": n4js.graph.service.uri,
        "NEO4J_USER": "neo4j",
        "NEO4J_PASS": "password",
    }
    runner = CliRunner()
    with set_env_vars(env_vars):
        ident = "bill"
        key = "a string for a key"

        identity = CredentialedUserIdentity(
            identifier=ident,
            hashed_key=hash_key(key),
        )

        n4js.create_credentialed_entity(identity)
        n4js.add_scope(
            "bill", CredentialedUserIdentity, Scope.from_str("org1-campaign2-project3")
        )
        n4js.add_scope(
            "bill", CredentialedUserIdentity, Scope.from_str("org4-campaign5-project6")
        )
        n4js.add_scope("bill", CredentialedUserIdentity, Scope.from_str("org7-*-*"))
        result = runner.invoke(
            cli,
            [
                "identity",
                "list-scope",
                "--identity-type",
                "user",
                "--identifier",
                ident,
            ],
        )
        assert click_success(result)
        assert "org1-campaign2-project3" in result.output
        assert "org4-campaign5-project6" in result.output
        assert "org7-*-*" in result.output

@pytest.mark.parametrize(
    "scopes",
    [
        ("org1-campaign2-project3",),
        (
            "org1-campaign2-project3",
            "org1-campaign2-project4",
        ),
    ],
)
def test_scope_add(n4js_fresh, scopes):
    n4js = n4js_fresh
    env_vars = {
        "NEO4J_URL": n4js.graph.service.uri,
        "NEO4J_USER": "neo4j",
        "NEO4J_PASS": "password",
    }
    runner = CliRunner()
    with set_env_vars(env_vars):
        ident = "bill"
        key = "a string for a key"

        identity = CredentialedUserIdentity(
            identifier=ident,
            hashed_key=hash_key(key),
        )

        n4js.create_credentialed_entity(identity)

        scopes_cli = []
        for scope in scopes:
            scopes_cli.append("--scope")
            scopes_cli.append(scope)

        result = runner.invoke(
            cli,
            [
                "identity",
                "add-scope",
                "--identity-type",
                "user",
                "--identifier",
                ident,
            ]
            + scopes_cli,
        )
        assert click_success(result)
        scopes_ = n4js.list_scopes("bill", CredentialedUserIdentity)
        assert len(scopes_) == len(scopes)
        assert set(scopes_) == set([Scope.from_str(scope) for scope in scopes])

@pytest.mark.parametrize(
    "scopes_remove",
    [
        ("org1-campaign2-project3",),
        (
            "org1-campaign2-project3",
            "org1-campaign2-project4",
        ),
    ],
)
def test_scope_remove(n4js_fresh, scopes_remove):
    n4js = n4js_fresh
    env_vars = {
        "NEO4J_URL": n4js.graph.service.uri,
        "NEO4J_USER": "neo4j",
        "NEO4J_PASS": "password",
    }
    runner = CliRunner()
    with set_env_vars(env_vars):
        ident = "bill"
        key = "a string for a key"

        identity = CredentialedUserIdentity(
            identifier=ident,
            hashed_key=hash_key(key),
        )
        n4js.create_credentialed_entity(identity)

        n4js.add_scope(
            ident, CredentialedUserIdentity, Scope.from_str("org1-campaign2-project3")
        )
        n4js.add_scope(
            ident, CredentialedUserIdentity, Scope.from_str("org1-campaign2-project4")
        )
        n4js.add_scope(
            ident, CredentialedUserIdentity, Scope.from_str("org4-campaign5-project6")
        )

        scopes_cli = []
        for scope in scopes_remove:
            scopes_cli.append("--scope")
            scopes_cli.append(scope)

        result = runner.invoke(
            cli,
            [
                "identity",
                "remove-scope",
                "--identity-type",
                "user",
                "--identifier",
                ident,
            ]
            + scopes_cli,
        )
        assert click_success(result)
        scopes = n4js.list_scopes("bill", CredentialedUserIdentity)
        scope_strs = [str(s) for s in scopes]
        for scope in scopes_remove:
            assert scope not in scope_strs
        assert "org4-campaign5-project6" in scope_strs


#!/usr/bin/env python3

import asyncio
import websockets
import ssl

async def hello(websocket, path):

    while True:
        m = await websocket.recv()
        print(m)

sc = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
# sc.check_hostname = False
# sc.verify_mode = ssl.CERT_NONE
sc.load_cert_chain('./resource/server.pem', './resource/server.key')

start_server = websockets.serve(hello, 'localhost', 8765, ssl=sc, subprotocols=[123, 32, "abc"])
# start_server = websockets.serve(hello, 'localhost', 8765)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()


from django.urls import path
from inec.views import (
    home,PollDetailView,local_gov_results,search,new_polling_unit
)
urlpatterns = [
    path('', home,name='home'),
    path('poll/unit/<int:pk>/',PollDetailView.as_view(),name='detail'),
    path('local/govt/result/',local_gov_results,name = 'results'),
    path('search/',search, name = 'search'),
    path('add/new/unit/',new_polling_unit,name = 'new_unit')

]


import numpy as np
import nibabel as ni
import os
import seaborn as sns

from mne.viz import circular_layout

import logging
logger = logging.getLogger(__name__)

currdir = os.path.dirname(os.path.abspath(__file__))
currdir = os.path.abspath(os.path.join(currdir, os.pardir))
atlasdir = os.path.join(currdir, 'io', 'data', 'atlas')

def find_roi_center(img, roi_value):
    """
    This function gives the x,y,z coordinates of a particular ROI using
    the given segmented image and the image level value used to select the ROI.

    Parameters
    ----------
    img : nibabel Nifti1Image instance
        The Nifti1Image instance of the segmented image.
    roi_value : int
        The value of the ROI as represented in the segmented image

    Returns
    -------
    xyz : tuple
        A triplets representing the xyz coordinate of the selected ROI.
    """

    affine = img.affine

    mask_ = np.int_(img.get_fdata()) == roi_value
    ijk_coords = np.array(np.nonzero(mask_)).mean(1)

    xyz_coords = ijk_coords * affine.diagonal()[:-1] + affine[:-1,-1]

    return xyz_coords

def get_aal_coords(fname):
    """Function used to obtain coordinates of the ROIs contained in the AAL90 atlas.
    The atlas used is the 2mm nifti version of the atlas.

    Parameters
    ----------
    fname : string
        The path of the atlas to be used.

    Returns
    -------
    coords : n x 3 numpy array
        The array containing n xyz coordinates in MNI space, one for each unique value of the atlas
    """

    atlas90 = ni.load(fname)
    coords = [find_roi_center(atlas90, roi_value=i) for i in np.unique(atlas90.get_data())[:]]

    return np.array(coords)

def get_findlab_coords():
    """
    Function used to obtain coordinates of the networks contained in the findlab atlas.
    The atlas used is the 2mm nifti version of the atlas.

    Returns
    -------
    coords : n x 3 numpy array
        The array containing n xyz coordinates in MNI space, one for each unique value of the atlas
    """
    atlas_dir = os.path.join(atlasdir, 'findlab')
    roi_list = os.listdir(atlas_dir)
    roi_list.sort()
    findlab = [ni.load(os.path.join(atlas_dir, roi)) for roi in roi_list if roi.find("nii") != -1]
    f_coords = []
    for img_ in findlab:

        centers = [find_roi_center(img_, roi_value=int(i)) for i in np.unique(img_.get_fdata())[1:]]
        f_coords.append(np.array(centers))

    return np.vstack(f_coords)

def get_atlas90_info(background='black'):

    """
    Utility function used to load informations about the atlas used

    Parameters
    ----------

    background : string | {'black', 'white'}
        A string used to build colors for plots.

    Returns
    -------
    names : list of string
        The list of ROI names.

    colors : list of string
        The list of colors used in other functions

    index_ : list of int
        How node values should be ordered if the atlas has another order
        (used to separate left/right in the atlas90)

    coords : list of tuple (x,y,z)
        Coordinates of the ROI center (used in plot_connectomics)

    networks : list of string
        The list of network names.

    """

    atlas_dir =  os.path.join(atlasdir, 'aal')
    coords = get_aal_coords(os.path.join(atlas_dir, "atlas90_mni_2mm.nii.gz"))
    roi_list = np.loadtxt(os.path.join(atlas_dir, "atlas90.cod"),
                            delimiter='=',
                            dtype=np.str)
    names = roi_list.T[1]
    names_inv = np.array([n[::-1] for n in names])
    index_ = np.argsort(names_inv)
    names_lr = names[index_]
    names = np.array([n.replace('_', ' ') for n in names])

    dict_ = {'L':'#89CC74',
             'R':'#7A84CC'}
    colors_lr = np.array([dict_[n[:1]] for n in names_inv])
    colors_lr = sns.husl_palette(2)
    networks = names

    node_angles = circular_layout(names.tolist(),
                                  names[index_].tolist(),
                                  start_pos=90,
                                  group_boundaries=[0, len(names) / 2.+1],
                                  group_sep=0.)

    return names, colors_lr, index_, coords, networks, node_angles

def get_findlab_info(background='black'):

    coords = get_findlab_coords()
    roi_file =  os.path.join(atlasdir, 'findlab', 'findlab_rois.txt')
    roi_list = np.loadtxt(roi_file, delimiter=',', dtype=str)
    networks = roi_list.T[-2]
    names = roi_list.T[2]

    dict_ = {
                'Auditory'          :'lightgray',
                'Basal_Ganglia'     :'lavender',
                #'Basal_Ganglia'    :'honeydew',
                'LECN'              :'tomato',
                'Language'          :'darkgrey',
                'Precuneus'         :'teal',
                'RECN'              :'lightsalmon',
                'Sensorimotor'      :'plum',
                'Visuospatial'      :'slateblue',
                'anterior_Salience' :'yellowgreen',
                #'dorsal_DMN'       :'lightsteelblue',
                'dorsal_DMN'        :'cadetblue',
                'high_Visual'       :'khaki',
                'post_Salience'     :'mediumseagreen',
                'prim_Visual'       :'gold',
                'ventral_DMN'       :'lightblue'
            }

    if background == 'white':
        dict_['anterior_Salience'] = 'gray'
        dict_['Basal_Ganglia'] = 'black'

    colors_lr = np.array([dict_[r.T[-2]] for r in roi_list])
    index_ = np.arange(90)

    _, count_ = np.unique(networks, return_counts=True)
    boundaries = np.cumsum(np.hstack(([0], count_)))[:-1]
    node_angles = circular_layout(names.tolist(),
                                  names.tolist(),
                                  start_pos=90,
                                  group_boundaries=boundaries.tolist(),
                                  group_sep=3.5)

    return names, colors_lr, index_, coords, networks, node_angles

def get_aalmeg_info(background='black', grouping='LR'):
    """[summary]

    Parameters
    ----------
    background : str, optional
        [description], by default 'black'
    grouping : str, optional
        [description], by default 'LR' or 'lobes'

    Returns
    -------
    [type]
        [description]
    """

    atlas_fname = os.path.join(atlasdir, 'aal', 'ROI_MNI_V4.nii')
    labels_fname = os.path.join(atlasdir, 'aal', 'ROI_MNI_V4.csv')
    coords = get_aal_coords(atlas_fname)

    labels = np.loadtxt(labels_fname, dtype=np.str, delimiter=',')
    node_names = labels.T[1][:99]

    if grouping == 'LR':
        node_idx = np.argsort(np.array([node[-1] for node in node_names]))
        group_boundaries = [0, len(node_names) / 2.+1]
        colors = sns.husl_palette(2)
        networks = node_names.copy()
    else:
        node_network = labels.T[3][:99]
        node_idx = np.argsort(node_network)
        networks, count = np.unique(node_network, return_counts=True)
        group_boundaries = np.cumsum(np.hstack(([0], count)))[:-1]
        color_network = sns.color_palette("Paired", len(networks)+1)
        colors_ = dict(zip(networks, color_network[1:]))
        colors = [colors_[n] for n in node_network]

    order = node_names[node_idx].tolist()
    node_angles = circular_layout(node_names.tolist(),
                                  order,
                                  start_pos=90,
                                  group_boundaries=group_boundaries,
                                  group_sep=3.
                                  )

    return labels, colors, node_idx, coords, networks, node_angles

def get_eeg_channels(n_channels=159):

    index_ = range(n_channels)
    names = list()
    colors_lr = list()
    coords = list()
    networks = list()
    for i in index_:
        names.append("ch-%03d" % (i+1))
        colors_lr.append("green")
        coords.append([0, 0, i])
        networks.append("Brain")

    node_angles = circular_layout(index_,
                                  index_,
                                  start_pos=90,
                                  group_sep=3.
                                  )

    return names, colors_lr, index_, coords, names, node_angles

def get_viviana_info():

    network = ['DAN','VAN','SMN','VIS','AUD','LAN','DMN']
    number = [6, 5, 8, 10, 4, 5, 7]
    color = ['lightgray', 'lavender', 'honeydew', 'tomato',
              'darkgrey', 'teal', 'lightsalmon']

    labels = ["%s_%02d" % (network[x], i+1) for x in range(len(network)) for i in range(number[x])]
    colors = [color[x] for x in range(len(network)) for i in range(number[x])]

    node_idx = np.arange(len(labels))

    coords = np.random.randint(-20, 20, (3, len(labels)))
    networks = [network[x] for x in range(len(network)) for i in range(number[x])]

    group_boundaries = np.cumsum(np.hstack(([0], number)))[:-1]
    node_angles = circular_layout(labels,
                                  labels,
                                  start_pos=90,
                                  group_boundaries=group_boundaries,
                                  group_sep=3.
                                  )

    return labels, colors, node_idx, coords, networks, node_angles

def get_atlas_info(atlas_name='findlab'):
    """This function is used to obtain information on
    different atlases.

    Parameters
    ----------
    atlas_name : str, optional ('findlab', 'aal_meg', 'atlas90')
        atlas name to get information

    Returns
    -------
    List of informations of the atlas:
        labels : names of nodes (n_nodes, string array)
        colors : colors of each node representing some classification
        node_idx : order of nodes for plot purposes
        coords : MNI coordinates of nodes
        networks : name of networks the node belongs to
        node_angles : angles for circular plot
    """

    mapper = {
        'findlab': get_findlab_info,
        'atlas90': get_atlas90_info,
        'aal_meg': get_aalmeg_info,
        'viviana': get_viviana_info,
        'eeg_channels': get_eeg_channels,

    }

    return mapper[atlas_name]()

def where_am_i(fname):
    """This function is used to find the ROI name in a mask file using
    AFNI whereami.

    Parameters
    ----------
    fname : string,
        The complete filename of the mask.

    Returns
    -------
    table : list
        The list has a lenght equal to the number of ROIs contained in the file
        Elements of the list:
        (x, y, z): coordinates of ROI's center of mass.
        label: name of the brain area closest to the center of mass
        num: number of voxels included in the ROI
        value: value of the ROI in the input file

    """

    logger.info("Looking for ROI names in "+fname)
    img = ni.load(fname)
    center_711 = np.array([-70.5, -105, -60.])
    data = img.get_data().squeeze()
    table = []
    for f in np.unique(data)[1:]:
        mask_roi = data == f

        center_mass = np.mean(np.nonzero(mask_roi), axis=1)
        x,y,z = np.rint([3,3,3]*center_mass+center_711)

        command = "whereami %s %s %s -lpi -space TLRC -tab" %(str(x+2.), str(y+2), str(z+2))
        var = os.popen(command).read()
        lines = var.split("\n")
        index = [i for i, l in enumerate(lines) if l[:5] == 'Atlas']
        label1 = lines[index[0]+1]
        label2 = lines[index[0]+2]
        if label1[0] == '*':
            area1 = area2 = "None"
        else:
            area1 = label1.split("\t")[2]
            area2 = label2.split("\t")[2]
        table.append([x, y, z, area1, np.count_nonzero(mask_roi), f])

    return table

def get_rois_names(path):
    import glob
    big_table = {}
    rois = glob.glob(path+"*mask.nii.gz")
    for fname in rois:
        table = where_am_i(fname)
        key = fname.split('/')[-1].split('.')[0][:-5]
        big_table[key] = table

    return big_table


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('app', '0002_auto_20151129_1222'),
    ]

    operations = [
        migrations.AddField(
            model_name='feature',
            name='project',
            field=models.ForeignKey(to='app.Project', default=1),
            preserve_default=False,
        ),
    ]


import sys
import os
import pathlib
import argparse

import pickle

import torch

import numpy as np

import tqdm

import gzip

import matplotlib as mpl

import skimage.io

import subprocess

def wind_kernel( direction, bone ):

	t = np.abs( np.tan( direction ) )
	x = 1.0 / ( 1.0 + t )
	y = t / ( 1.0 + t )

	ret = np.zeros( ( 3, 3 ) )

	if 0 < direction <= np.pi*0.5:
		ret[1,0] = x * bone
		ret[2,1] = y * bone

	elif np.pi*0.5 < direction <= np.pi:
		ret[1,2] = x * bone
		ret[2,1] = y * bone

	elif np.pi < direction <= np.pi*1.5:
		ret[1,2] = x * bone
		ret[0,1] = y * bone

	elif np.pi*1.5 < direction <= np.pi*2:
		ret[1,0] = x * bone
		ret[0,1] = y * bone

	ret[1,1] = ( 1.0 - bone )

	return ret

def main( argv ):

	me_path = pathlib.Path( __file__ ).parent.resolve()

	ap = argparse.ArgumentParser()

	ap.add_argument( '--cuda', action='store_true', default=False )

	ap.add_argument( '--emission-map', type=pathlib.Path, default=( me_path / 'emission.pkl' ) )

	ap.add_argument( '--steps', type=int, default=1000 )

	ap.add_argument( '--mix-angle', type=float, default=130 )

	ap.add_argument( '--mix-ratio', type=float, default=0.6 )

	ap.add_argument( '--output-video', type=pathlib.Path, default=None )

	ap.add_argument( '--output-epochs', type=pathlib.Path, default=None )

	ap.add_argument( '--output-result', type=pathlib.Path, default=None )

	args = ap.parse_args( argv )

	print( 'initializing...' )

	if args.output_video:
		os.makedirs( args.output_video.parent, exist_ok=True )

	if args.output_result:
		os.makedirs( args.output_result.parent, exist_ok=True )

	if args.output_epochs:
		os.makedirs( args.output_epochs, exist_ok=True )

	output_frames = args.output_epochs or args.output_video

	if args.cuda:
		print( 'using CUDA' )
		device = torch.device( 'cuda' )
	else:
		print( 'using CPU' )
		device = torch.device( 'cpu' )

	print( 'loading emission map...' )

	emission = pickle.loads( args.emission_map.read_bytes() )
	emission = torch.tensor( emission, dtype=torch.float32 ).to( device )

	print( 'building field...' )

	field = torch.zeros( 1, 1, emission.size( 0 ), emission.size( 1 ), dtype=torch.float32 ).to( device )

	print( 'building kernel...' )

	kernel = wind_kernel( np.deg2rad( args.mix_angle ), args.mix_ratio )
	kernel = torch.tensor( kernel.reshape( 1, 1, 3, 3 ), dtype=torch.float32 ).to( device )

	cm = mpl.cm.get_cmap( 'jet' )

	if args.output_video:

		video_proc = subprocess.Popen( [
			'ffmpeg',
		#	'-v', 'debug',
			'-loglevel', 'panic',
			'-f', 'rawvideo',
			'-framerate', '24',
			'-pixel_format', 'rgb24',
			'-video_size', f'{field.size(2)}x{field.size(3)}',
			'-i', '-',
			'-vcodec', 'h264',
			'-y',
			str( args.output_video ),
			], stdin=subprocess.PIPE )

	field_max = 0.0

	print( 'processing...' )

	for epoch_n in tqdm.tqdm( range( args.steps ) ):

		field += emission
		torch.clamp( field, min=0, out=field )

		field = torch.nn.functional.conv2d( field, kernel, padding=1 )

		if output_frames:

			cur_frame = field.cpu().numpy()
			cur_frame.resize( cur_frame.shape[2:] )

			if args.output_epochs:

				dst_fn = args.output_epochs / f'epoch-{epoch_n:06d}.pkl.gz'
				with gzip.open( dst_fn, 'wb', compresslevel=6 ) as dst_f:
					pickle.dump( cur_frame, dst_f, -1 )

			if args.output_video:

				cur_field_max = cur_frame.max()

				if field_max < cur_field_max:
					field_max = cur_field_max

				cur_field = cur_frame / field_max

				cur_field_cm = ( cm( cur_field ) * 255.0 )[:,:,:3].astype( np.uint8 )

				video_proc.stdin.write( cur_field_cm.tobytes() )

	if args.output_video:

		video_proc.stdin.close()

		video_proc.communicate()

	if args.output_result:

		print( 'saving result...' )

		if not output_frames:

			cur_frame = field.cpu().numpy()
			cur_frame.resize( cur_frame.shape[2:] )

		cur_field = cur_frame.copy()

		args.output_result.write_bytes( pickle.dumps( cur_field, -1 ) )

		cur_field /= cur_field.max()

		cur_field_cm = ( cm( cur_field ) * 255.0 )[:,:,:3].astype( np.uint8 )

		skimage.io.imsave( str( args.output_result.with_suffix( '.tiff' ) ), cur_field_cm, compress=6 )

	print( 'done...' )

	return 0

if __name__ == '__main__':
	sys.exit( main( sys.argv[1:] ) )


import time
import json
from datetime import date
from dynaconf import settings
from requests.exceptions import HTTPError
from investagram_data_loader.logger import logging
from investagram_data_loader.repository.ig_api import InvestagramApi
from investagram_data_loader.constants.mode import DateMode
from investagram_data_loader.date.date_handler import generate_date_range
from investagram_data_loader.repository.sqlite_dao import SqliteDao
from investagram_data_loader.reader.data_reader import get_stocks
from investagram_data_loader.exceptions import NoDataFoundException

# global settings here
SLEEP_TIME = 10
FROM_DATE, TO_DATE = date(2020, 1, 1), date(2021, 2, 20)

def load_transactions_by_stock(api: InvestagramApi, stock_code: str, mode: DateMode, from_date: date,
                               to_date: date) -> list:
    transactions = []
    logging.info(f'Loading transaction by stock code {stock_code} from {from_date} to {to_date} on a {mode} basis...')

    stock = api.get_stock_info(stock_code)

    for range_start, range_end in generate_date_range(mode, from_date, to_date):
        transactions.extend(api.get_stock_transaction_by_stock_id_and_date(stock.stock_id, range_start, range_end))

    return transactions

def load_transactions_by_broker(api: InvestagramApi, broker_code: str, mode: DateMode, from_date: date, to_date: date):
    transactions = []
    logging.info(f'Loading transaction by broker code {broker_code} from {from_date} to {to_date} on a {mode} basis...')

    broker = api.get_broker_info(broker_code)

    for range_start, range_end in generate_date_range(mode, from_date, to_date):
        transactions.extend(api.get_stock_transaction_by_broker_id_and_date(
            broker.stock_broker_id, range_start, range_end)
        )

    return transactions

def main():
    logging.info("Downloading data from investagram...")
    # replace with 'size=-1' to download all the stocks data
    stocks, unprocessed = get_stocks(size=-1), []

    with SqliteDao() as dao:
        with InvestagramApi() as api:
            for stock in stocks:
                try:
                    transactions = load_transactions_by_stock(api, stock, DateMode.DAILY, FROM_DATE, TO_DATE)
                    dao.bulk_insert_transactions(transactions)

                    logging.info(f'Sleeping for {SLEEP_TIME} seconds...')
                    time.sleep(SLEEP_TIME)

                except NoDataFoundException as exp:
                    logging.error(f'No data found for stock code {stock}.')
                    unprocessed.append(stock)

                except HTTPError as exp:
                    logging.error(f'Non-zero http status found {exp}.')
                    unprocessed.append(stock)

    if len(unprocessed) != 0:
        logging.error(f'Found {len(unprocessed)} unprocessed stock codes: {unprocessed}. Dumping to a file.')

        with open(settings.UNPROCESSED_CODES_FILE, 'w') as fd:
            fd.writelines(unprocessed)

if __name__ == '__main__':
    main()


import os

sample_start = 1
sample_stop = 20

isoc_start = 1
isoc_stop = 100

rcut = 2.5

isoc_dir = "isoc_dt1_wi50_data.T0.38.Equi.DPD"
pairstyle_t = "none"
pairstyle_t0 = "none"
boxtype_t = "tilt"
boxtype_t0 = "orthogonal"
ifname_t0 = "data.T0.38.Equi.DPD"
set_ifname_t = "{}.isco"
set_ifpath = "./{sampledir}/{isoc_dir}/"
set_ofname = "D2.{}"
for it_sample in range(sample_start, sample_stop):
    ifpath = set_ifpath.format(sampledir=it_sample, isoc_dir=isoc_dir)
    ofpath = ifpath + "D2/"
    for it_isoc in range(isoc_start, isoc_stop):
        ifname_t = set_ifname_t.format(it_isoc)
        ofname = set_ofname.format(it_isoc)

        set_run_D2 = "./D2.exe -r {r} -t {t} -0 {t0} -I {I} --boxtype_t {boxtype_t} --boxtype_t_ {boxtype_t_} -o {o} -O {O}"
        flag = os.system(
            set_run_D2.format(r=rcut,
                              t=ifname_t,
                              t0=ifname_t0,
                              I=ifpath,
                              boxtype_t=boxtype_t,
                              boxtype_t_=boxtype_t0,
                              o=ofname,
                              O=ofpath))
        if (0 != flag):
            print("erro code: {}".format(flag))
            exit(flag)


from typing import *

import os
import sys
import tempfile
import subprocess

import enum
import attrs
import hydra.types
import hydra.utils
import hydra.core.config_store
from omegaconf import OmegaConf, MISSING, DictConfig

import torch

from denoised_mdp import utils
from denoised_mdp.envs import EnvConfig
from denoised_mdp.memory import ExperienceReplayConfig
from denoised_mdp.agents import (
    DenoisedMDP, DynamicsBackpropagateActorCritic, SoftActorCritic,
    BasePolicyLearning, VariationalModelLearning
)

@attrs.define(kw_only=True, auto_attribs=True)
class DeviceConfig:
    _target_: str = 'torch.device'

    type: str = 'cuda'
    index: Optional[int] = 0

def get_dotted_name(cls: Type):
    return f"{cls.__module__}.{cls.__qualname__}"

cs = hydra.core.config_store.ConfigStore.instance()

@attrs.define(kw_only=True, auto_attribs=True)
class Config:
    defaults: List = attrs.Factory(lambda: [
        {'learning/policy_learning': 'dynamics_backprop'},
        {'override hydra/hydra_logging': 'disabled'},  # we handle ourselves
        {'override hydra/job_logging': 'disabled'},  # we handle ourselves
        '_self_',
    ])
    _target_: str = attrs.Factory(lambda: get_dotted_name(InstantiatedConfig))

    # Disable hydra working directory creation
    hydra: Dict = dict(
        output_subdir=None,
        job=dict(chdir=False),
        run=dict(dir=tempfile.TemporaryDirectory().name),  # can't disable https://github.com/facebookresearch/hydra/issues/1937
        mode=hydra.types.RunMode.RUN,  # sigh: https://github.com/facebookresearch/hydra/issues/2262
    )

    # Directories: e.g., output

    base_git_dir: str = subprocess.check_output(
        r'git rev-parse --show-toplevel'.split(),
        cwd=os.path.dirname(__file__), encoding='utf-8').strip()

    def fmtdir(self, dir):
        return f"$GIT/{os.path.relpath(dir, self.base_git_dir)}"

    overwrite_output: bool = False
    output_base_dir: str = attrs.field(default=os.path.join(base_git_dir, 'results'))

    @output_base_dir.validator
    def exists_path(self, attribute, value):
        assert os.path.exists(value)

    output_folder: Optional[str] = None

    # Seeding
    seed: int = 60912
    test_seed: int = 1841
    device: DeviceConfig = DeviceConfig()

    # Env
    env: EnvConfig = EnvConfig()

    @attrs.define(kw_only=True, auto_attribs=True)
    class LearningConfig:
        _target_: str = attrs.Factory(lambda: get_dotted_name(InstantiatedConfig.LearningConfig))

        experience_replay: ExperienceReplayConfig = ExperienceReplayConfig()

        @attrs.define(kw_only=True, auto_attribs=True)
        class OptimizationConfig:
            _target_: str = attrs.Factory(lambda: get_dotted_name(InstantiatedConfig.LearningConfig.OptimizationConfig))

            adam_eps: float = attrs.field(default=1e-7, validator=attrs.validators.gt(0))
            train_interval: int = attrs.field(default=1000, validator=attrs.validators.gt(0))  # num steps between training
            train_iterations: int = attrs.field(default=100, validator=attrs.validators.gt(0))  # num gd steps per training
            model_every: int = attrs.field(default=1, validator=attrs.validators.gt(0))  # during train_iterations steps, frequency of model updatres
            policy_every: int = attrs.field(default=1, validator=attrs.validators.gt(0))  # during train_iterations steps, frequency of policy updatres

        optimization: OptimizationConfig = attrs.Factory(OptimizationConfig)

        model: DenoisedMDP.Config = DenoisedMDP.Config()
        model_learning: VariationalModelLearning.Config = VariationalModelLearning.Config()
        # See `defaults` list above. Related: https://github.com/facebookresearch/hydra/issues/2227
        policy_learning: BasePolicyLearning.Config = MISSING

        @attrs.define(kw_only=True, auto_attribs=True)
        class ExplorationConfig:
            _target_: str = attrs.Factory(lambda: get_dotted_name(InstantiatedConfig.LearningConfig.ExplorationConfig))

            action_noise: float = attrs.field(default=0.3, validator=attrs.validators.gt(0))
            prefill_steps: int = attrs.field(default=5000, validator=attrs.validators.gt(0))
            total_steps: int = attrs.field(default=int(1e6), validator=attrs.validators.gt(0))

        exploration: ExplorationConfig = attrs.Factory(ExplorationConfig)

        batch_size: int = attrs.field(default=50, validator=attrs.validators.gt(0))
        chunk_length: int = attrs.field(default=50, validator=attrs.validators.gt(0))

        @attrs.define(kw_only=True, auto_attribs=True)
        class TestConfig:
            _target_: str = attrs.Factory(lambda: get_dotted_name(InstantiatedConfig.LearningConfig.TestConfig))

            num_episodes: int = attrs.field(default=10, validator=attrs.validators.gt(0))
            visualize_num_episodes: int = attrs.field(default=3, validator=attrs.validators.gt(0))
            test_interval: int = attrs.field(default=10000, validator=attrs.validators.gt(0))
            visualize_interval: int = attrs.field(default=20000, validator=attrs.validators.gt(0))

        test: TestConfig = attrs.Factory(TestConfig)

        checkpoint_interval: int = attrs.field(default=250000, validator=attrs.validators.gt(0))

    learning: LearningConfig = attrs.Factory(LearningConfig)

global_config: Optional[Config] = None

@attrs.define(kw_only=True, auto_attribs=True)
class InstantiatedConfig(Config):
    config: Config = attrs.field(init=False)

    output_dir: str = attrs.field(init=False)

    # NOTE [ Pre-emption ]
    #
    # SIGUSR1 is our mechanism to pre-empt. Unpon SIGUSR1, we catch the signal,
    # start checkpointing (incl. replay buffer) via `ModelTrainer.save_resumable_state_if_possible`,
    # and finally kill the training job.
    #
    # We make 3 files/dirs for handling preemption.
    # 1. a COMPLETE file which signals that the jobs is done, so, upon SIGUSR1, no-op
    # 2. a temp CKPT dir, which we will write ckpt to upon SIGUSR1
    # 3. an actual CKPT dir, which we will copy/rename temp CKPT to, after finish writing to
    #    the temp CKPT, in case the job gets killed when we write to temp CKPT dir, leaving
    #    the ckpt in an inconsistent state.
    #
    # When we start running with the same `output_dir`, we will attempt to resume via `ModelTrainer.resume_from_saved_state_if_exists`.

    job_complete_file: str = attrs.field(init=False)
    tmp_preempt_ckpt_dir: str = attrs.field(init=False)
    preempt_ckpt_dir: str = attrs.field(init=False)
    received_SIGUSR1: bool = attrs.field(init=False)

    # logging
    logging_file: bool = attrs.field(init=False)

    def __attrs_post_init__(self):
        # Validate several entries
        assert global_config is not None
        self.config = global_config
        # Set output directory
        if self.output_folder is None:
            specs = [
                f'alpha={self.config.learning.model_learning.kl.alpha:g}',
                f'betaX={self.config.learning.model_learning.kl.beta_x:g}',
                f'betaY={self.config.learning.model_learning.kl.beta_y:g}',
                f'betaZ={self.config.learning.model_learning.kl.beta_z:g}',
                self.config.learning.policy_learning._kind_,
                f'seed={self.seed}',
            ]
            if self.config.learning.model_learning.kl.beta_free_nats_x is not None:
                specs.insert(2, f'betaFreeNatsX={self.config.learning.model_learning.kl.beta_free_nats_x:g}')
            self.output_folder = os.path.join(
                f'{self.config.env.kind.name}_{self.config.env.spec}',
                '_'.join(specs),
            )
        self.output_dir = os.path.join(self.output_base_dir, self.output_folder)
        os.makedirs(self.output_dir, exist_ok=True)
        # See NOTE [ Pre-emption ]
        self.job_complete_file = os.path.join(self.output_dir, 'COMPLETE')
        if os.path.exists(self.job_complete_file) and not self.overwrite_output:
            print(f"Complete run found at {self.output_dir} and overwrite_output=False, exiting.")
            sys.exit(0)
        self.tmp_preempt_ckpt_dir = os.path.join(self.output_dir, 'preempt.tmp')
        if os.path.exists(self.tmp_preempt_ckpt_dir):
            utils.rm_if_exists(self.tmp_preempt_ckpt_dir, maybe_dir=True)
        self.preempt_ckpt_dir = os.path.join(self.output_dir, 'preempt')
        self.received_SIGUSR1 = False
        # Logging
        self.logging_file = os.path.join(self.output_dir, 'output.log')
        utils.logging.configure(self.logging_file)

    device: torch.device
    env: EnvConfig.InstantiatedT

    @attrs.define(kw_only=True, auto_attribs=True)
    class LearningConfig(Config.LearningConfig):
        experience_replay: ExperienceReplayConfig.InstantiatedT
        model: DenoisedMDP.Config.InstantiatedT
        model_learning: VariationalModelLearning.Config.InstantiatedT
        policy_learning: BasePolicyLearning.Config.InstantiatedT

    learning: LearningConfig

cs.store(name='config', node=Config())
cs.store(group='learning/policy_learning', name='dynamics_backprop', node=DynamicsBackpropagateActorCritic.Config())
cs.store(group='learning/policy_learning', name='sac', node=SoftActorCritic.Config())

def to_config_and_instantiate(dict_cfg: DictConfig) -> Tuple[Config, InstantiatedConfig]:
    global global_config

    def convert(v: Any, desired_ty: Type):
        if isinstance(v, DictConfig):
            ty = OmegaConf.get_type(v)
            assert issubclass(ty, desired_ty)
            if attrs.has(ty):
                fields = attrs.fields_dict(ty)
                kwargs = {}
                for k, subv in v.items():
                    # sign, attrs auto-strip leading underscore so we have to manually do this
                    # rather than using `OmegaConf.to_object`
                    kwargs[k.lstrip('_')] = convert(subv, fields[k].type)
                v = ty(**kwargs)
        elif isinstance(desired_ty, type) and issubclass(desired_ty, enum.Enum):
            if isinstance(v, str) and v != MISSING:
                v = desired_ty[v]
        return v

    global_config = convert(dict_cfg, Config)
    return global_config, hydra.utils.instantiate(global_config)


### --- ARCHIVOS FUNCIONES AUXILIARES --- ###

# Librerias

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.gridspec import GridSpec
from matplotlib.lines import Line2D
from pylab import rcParams
from scipy import stats

# Funcion Correlacion entre atributos y vector objetivo

def fetch_features (df, vec_ob):
    columns = df.columns #extraemos los nombres de las columnas en la base de datos

    # generamos 3 arrays vacíos para guardar los valores
    attr_name = [] # nombre de la variable
    pearson_r = [] # correlación de pearson
    abs_pearson_r = [] # valor absoluto de la correlación

    # para cada columna en el array de columnas
    for col in columns:
        # si la columna no es la dependiente
        if col != vec_ob:
            # adjuntar el nombre de la variable en attr_name
            attr_name.append(col)
            # adjuntar la correlación de pearson
            pearson_r.append(df[col].corr(df[vec_ob]))
            # adjuntar el absoluto de la correlación de pearson
            abs_pearson_r.append(abs(df[col].corr(df[vec_ob])))

    # transformamos los arrays en un DataFrame
    features = pd.DataFrame({'attribute': attr_name,'corr':pearson_r,'abs_corr':abs_pearson_r})
    # generamos el index con los nombres de las variables
    features = features.set_index('attribute')
    # ordenamos los valores de forma descendiente
    feat_sort = features.sort_values(by=['abs_corr'], ascending=False)

    return feat_sort

# Función para la importancia de atributos

def plot_feature_importance(fit_model, feat_names):
    """
    Plot relative importance of a feature subset given a fitted model.
    """

    # Seteamos el tama{o de nuestro plot
    rcParams['figure.figsize'] = 10, 5
    # Guardamos las columnas de nuestro conjunto de entrenamiento
    features = feat_names
    # Obtenemos la importancia de nuestros atributos desde el modelo entrenado
    importances = fit_model.feature_importances_
    # Ordenamos de maoyr a menor la importancia de nustro atributos
    indices = np.argsort(importances)

    # Graficamos
    plt.title('Feature Importance')
    plt.barh(range(len(indices)), importances[indices], color='b', align='center')
    plt.yticks(range(len(indices)), [features[i] for i in indices])
    plt.xlabel('porcentaje de importancia relativa')
    plt.show()

# Función para gráficar metricas de desempeño

def plot_metrics (metrics, models, title):

    length = len(metrics)
    x_labels = models

    # Set plot parameters
    rcParams['figure.figsize'] = 10, 6
    fig, ax = plt.subplots()
    width = 0.2 # width of bar
    x = np.arange(length)

    rects1 = ax.bar(x, metrics[:,0], width, color='#000080', label='MAE s/imputar')
    rects2 = ax.bar(x + width, metrics[:,1], width, color='#0F52BA', label='MAE imputados')
    rects3 = ax.bar(x + (2 * width), metrics[:,2], width, color='#6593F5', label='RMSE s/imputar')
    rects4 = ax.bar(x + (3 * width), metrics[:,3], width, color='#73C2FB', label='RMSE imputados')

    ax.set_ylabel('Minutos')
    ax.set_ylim(0,75)
    ax.set_xticks(x + width + width/2)
    ax.set_xticklabels(x_labels)
    ax.set_xlabel('Modelos')
    ax.set_title(title)
    ax.legend()
    plt.grid(True, 'major', 'y', ls='--', lw=.9, c='k', alpha=.3)
    fig.tight_layout()
    plt.show()


from iqoptionapi.stable_api import IQ_Option
from datetime import datetime, date, timedelta
from dateutil import tz
import time, json

# função open config.json
def config():

    with open("config.json") as f:
        return json.load(f)

    return False

# tornar config GLOBAL
config = config()

# função converter time em data
def timestamp_converter(x, retorno = 1):

    timestamp = datetime.fromtimestamp(x).strftime('%Y-%m-%d')

    return timestamp

API = IQ_Option(config['user_email'], config['password'])

# realizar conexão
API.connect()
API.change_balance(config['change_balance'])

# checar conexão e reconnectar caso não esteja logado
tentativas = 0
while not API.check_connect():
    if tentativas < config['max_reconnetion']:
        print("Conectando ao servidor IQ Option, tentativa "+str(tentativas))
        API.connect()
        tentativas = tentativas+1
    else:
        print("Erro ao realizr conexão, tentativas excedidas!")
        break

    time.sleep(1)

print("Usuário logada na API IQOptions com sucesso!")

def get_list_position():

    list_id_trader = []
    ranking = API.get_leader_board('Worldwide', 1, 100, 5)

    for i in ranking['result']['positional']:

        id = ranking['result']['positional'][i]['user_id']

        list_id_trader.append(id)

    return list_id_trader

# carregar list ranking
print("Carregando list do ranking")
list_ranking = get_list_position()

print(list_ranking)
print("======================================================")
print("Aguardando capturar traders ranking")
API.subscribe_live_deal(config['type_par'], config['par_change'], config['timeframe'], 10)

trader_old = 0
while True:
    traders = API.get_live_deal(config['type_par'], config['par_change'], config['timeframe'])

    if len(traders) > 0 and traders[0]['user_id'] != trader_old:

         #print(json.dumps(traders[0], indent=1))

         id = traders[0]['user_id']
         if id in list_ranking:
            print("O Trader "+str(traders[0]['name'])+" estar operando")

            status, id = API.buy(traders[0]['amount_enrolled'], config['par_change'], traders[0]['instrument_dir'], 1)

            if status:
                print("[!] Ordem enviada com sucesso. Detalhes: DIR -> "+str(traders[0]['instrument_dir']))
         trader_old = traders[0]['user_id']

    time.sleep(1)


def solution(N, number):
    answer = -1
    calc_list = []
    for i in range(1,9):
        calc = set()
        calc.add(int(str(N)*i))
        for j in range(0,i-1):
            for a in calc_list[j]:
                for b in calc_list[-j-1]:
                    calc.add(a-b)
                    calc.add(a+b)
                    calc.add(a*b)
                    if b != 0:
                        calc.add(a//b)
        if number in calc:
            answer = i
            break
        calc_list.append(calc)
    return answer

print(solution(2,11))


from bson import ObjectId
from nose import with_setup

from mlight.attributes import FieldProperty
from mlight.meta_model import MetaModel
from tests.common import get_db_session, drop_all_collections, loop_runner, drop_database

db_session = get_db_session()

def setup_module():
    print("Module '%s' setup" % __name__)

def teardown_module():
    drop_all_collections(db_session=db_session)
    drop_database(db_session)
    print("Module '%s' teardown" % __name__)

def setup_function():
    db_session.clear_all()
    print("\nTest setup")

def teardown_function():
    db_session.clear_all()
    drop_all_collections(db_session=db_session)
    print("Test teardown")

class QueryDocument(MetaModel):
    session = db_session
    __model__ = 'indexed_document'

    _id = FieldProperty(ObjectId, if_missing=ObjectId)
    name = FieldProperty(str, required=True, if_missing='')
    age = FieldProperty(int, required=True)
    height = FieldProperty(float)

db_session.register_model(QueryDocument)

@with_setup(setup_function, teardown_function)
def test_query_document_get():
    obj = QueryDocument(age=1231231)

    async def run_async():
        await obj.flush()
        query_obj = await QueryDocument.get(obj._id)

        assert obj._id == query_obj._id, '_id field should match'
        assert obj.name == query_obj.name, 'name field should match'
        assert obj.age == query_obj.age, 'age field should match'
        assert obj.height == query_obj.height, 'height field should match'

    loop_runner(run_async)

@with_setup(setup_function, teardown_function)
def test_query_find():
    obj1 = QueryDocument(age=8716868)
    obj2 = QueryDocument(name='saassasa', age=12313, height=123.)

    def check_objects(obj, query_obj):
        assert obj._id == query_obj._id, '_id field should match'
        assert obj.name == query_obj.name, 'name field should match'
        assert obj.age == query_obj.age, 'age field should match'
        assert obj.height == query_obj.height, 'height field should match'

    async def run_async():
        await obj1.flush()
        await obj2.flush()

        results = await QueryDocument.find()

        result1 = results[0]
        result2 = results[1]

        if obj1._id == result1._id:
            check_objects(obj1, result1)
            check_objects(obj2, result2)
        else:
            check_objects(obj1, result2)
            check_objects(obj2, result1)

    loop_runner(run_async)

@with_setup(setup_function, teardown_function)
def test_query_collection_find():
    obj1 = QueryDocument(age=8716868)
    obj2 = QueryDocument(name='saassasa', age=12313, height=123.)

    def check_objects(obj, query_obj):
        assert obj._id == query_obj._id, '_id field should match'
        assert obj.name == query_obj.name, 'name field should match'
        assert obj.age == query_obj.age, 'age field should match'
        assert obj.height == query_obj.height, 'height field should match'

    async def run_async():
        await obj1.flush()
        await obj2.flush()

        cursor = QueryDocument.collection.find()
        results = await QueryDocument.to_mapped_list(cursor)

        result1 = results[0]
        result2 = results[1]

        if obj1._id == result1._id:
            check_objects(obj1, result1)
            check_objects(obj2, result2)
        else:
            check_objects(obj1, result2)
            check_objects(obj2, result1)

    loop_runner(run_async)

    # query a document update it and save it in the session!