'''
AnimeBot.py
Acts as the "main" file and ties all the other functionality together.
'''

import asyncio
import re
import traceback
import requests
import time

import discord
import Discord
import DiscordoragiSearch
import CommentBuilder
import DatabaseHandler
import Config
import Reference

#the servers where expanded requests are disabled
disableexpanded = ['']
async_queue = asyncio.Queue(maxsize = 32)
ownerID = '164546159140929538'

@Discord.client.event
async def on_ready():
    print('Logged in as')
    print(Discord.client.user.name)
    print(Discord.client.user.id)
    print('------')

@Discord.client.event
async def on_server_join(server):
    DatabaseHandler.addServerToDatabase(server.id)
    print("Added server {} to database".format(server.id))

async def process_message(message, is_edit=False):
    #Anime/Manga requests that are found go into separate arrays
    animeArray = []
    mangaArray = []
    lnArray = []

    #Checks if bot has permissions to embed
    if message.channel.type != discord.ChannelType.private:
        canEmbed = message.channel.server.default_role.permissions.embed_links
    else:
        canEmbed = True
    if not canEmbed:
        botMember = Discord.getMemberFromID(Config.clientid, message.server)
        defaultroleperm = botMember.top_role.permissions
        canEmbed = defaultroleperm.embed_links

    isAdmin = message.author.top_role.permissions.administrator
    isServerMod = message.author.top_role.permissions.manage_server
    isOwner = message.author.id == ownerID

    if message.author.bot:
        return

    #ignores all "code" markup (i.e. anything between backticks)
    preCleanMessage = re.sub(r"\`(.*?)\`", "", message.clean_content)
    cleanMessage = re.sub(r'<:.+?:([0-9]{15,21})>', "", preCleanMessage)
    messageReply = ''

    if re.search('({!help.*?}|{{!help.*?}}|<!help.*?>|<<!help.*?>>)', cleanMessage, re.S) is not None:
        try:
            localEm = CommentBuilder.buildHelpEmbed()
            await Discord.client.send_message(message.channel, embed = localEm)
            return
        except:
            return

    if re.search('({!command.*?}|{{!command.*?}}|<!command.*?>|<<!command.*?>>)', cleanMessage, re.S) is not None:
        if 'toggleexpanded' in cleanMessage.lower() and (isAdmin or isServerMod):
            try:
                allowedStatus = DatabaseHandler.toggleAllowExpanded(message.server.id)
                print("Toggled allowExpanded for server {}".format(message.server.id))
                if allowedStatus.lower() == 'true':
                    await Discord.client.send_message(message.channel, "Expanded requests are now allowed.")
                else:
                    await Discord.client.send_message(message.channel, "Expanded requests are now disallowed.")
                return
            except Exception as e:
                print(e)
                return

        if 'addserver' in cleanMessage.lower() and (isOwner == True):
            try:
                DatabaseHandler.addServerToDatabase(message.server.id)
                await Discord.client.send_message(message.channel, "Server has been added.")
                return
            except Exception as e:
                print(e)
                return

        else:
            print("command failed, user probably has insufficient rights")
            return

    sender = re.search('[@]([A-Za-z0-9 _-]+?)(>|}|$)', cleanMessage, re.S)
    mentionArray = message.raw_mentions
    if re.search('({!stats.*?}|{{!stats.*?}}|<!stats.*?>|<<!stats.*?>>)', cleanMessage, re.S) is not None and sender is not None:
        for mention in mentionArray:
            if not canEmbed:
                messageReply = CommentBuilder.buildStatsComment(server=message.server, username=mention)
            else:
                localEm = CommentBuilder.buildStatsEmbed(server=message.server, username=mention)
                await Discord.client.send_message(message.channel, embed=localEm)
                return None
    if re.search('({!sstats}|{{!sstats}}|<!sstats>|<<!sstats>>)', cleanMessage, re.S) is not None:
        if not canEmbed:
            messageReply = CommentBuilder.buildStatsComment(server = message.server)
        else:
            localEm = CommentBuilder.buildStatsEmbed(server = message.server)
            await Discord.client.send_message(message.channel, embed=localEm)
            return None
    elif re.search('({!stats.*?}|{{!stats.*?}}|<!stats.*?>|<<!stats.*?>>)', cleanMessage, re.S) is not None:
        if not canEmbed:
            messageReply = CommentBuilder.buildStatsComment()
        else:
            localEm = CommentBuilder.buildStatsEmbed()
            await Discord.client.send_message(message.channel, embed=localEm)
            return None
    else:

        #The basic algorithm here is:
        #If it's an expanded request, build a reply using the data in the braces, clear the arrays, add the reply to the relevant array and ignore everything else.
        #If it's a normal request, build a reply using the data in the braces, add the reply to the relevant array.

        #Counts the number of expanded results vs total results. If it's not just a single expanded result, they all get turned into normal requests.
        numOfRequest = 0
        numOfExpandedRequest = 0
        forceNormal = False
        expandedAllowed = DatabaseHandler.checkServerConfig('allowexpanded', message.server.id)
        if expandedAllowed == False:
            forceNormal = True
        for match in re.finditer("\{{2}([^}]*)\}{2}|\<{2}([^>]*)\>{2}", cleanMessage, re.S):
            numOfRequest += 1
            numOfExpandedRequest += 1
            print("Request found: {}".format(match.group(0)))

        for match in re.finditer("(?<=(?<!\{)\{)([^\{\}]*)(?=\}(?!\}))|(?<=(?<!\<)\<)([^\<\>]*)(?=\>(?!\>))", cleanMessage, re.S):
            numOfRequest += 1
            print("Request found: {}".format(match.group(0)))

        if (numOfExpandedRequest >= 1) and (numOfRequest > 1):
            forceNormal = True

        #if numOfRequest != 0:
            #await Discord.client.send_typing(message.channel)
        #Expanded Anime
        for match in re.finditer("\{{2}([^}]*)\}{2}", cleanMessage, re.S):
            reply = ''
            if match.group(1) != '':
                if (forceNormal) or (str(message.channel).lower() in disableexpanded):
                    reply = await DiscordoragiSearch.buildAnimeReply(match.group(1), message, False, canEmbed)
                else:
                    reply = await DiscordoragiSearch.buildAnimeReply(match.group(1), message, True, canEmbed)

                if (reply is not None):
                    animeArray.append(reply)
            else:
                print("Empty request, ignoring")

        #Normal Anime
        for match in re.finditer("(?<=(?<!\{)\{)([^\{\}]*)(?=\}(?!\}))", cleanMessage, re.S):
            if match.group(1) != '':
                reply = await DiscordoragiSearch.buildAnimeReply(match.group(1), message, False, canEmbed)

                if (reply is not None):
                    animeArray.append(reply)
                else:
                    print('Could not find anime')
            else:
                print("Empty request, ignoring")

        #Expanded Manga
        #NORMAL EXPANDED
        for match in re.finditer("\<{2}([^>]*)\>{2}(?!(:|\>))", cleanMessage, re.S):
            if match.group(1) != '':
                reply = ''

                if (forceNormal) or (str(message.channel).lower() in disableexpanded):
                    reply = await DiscordoragiSearch.buildMangaReply(match.group(1), message, False, canEmbed)
                else:
                    reply = await DiscordoragiSearch.buildMangaReply(match.group(1), message, True, canEmbed)

                if (reply is not None):
                    mangaArray.append(reply)
            else:
                print("Empty request, ignoring")

        #AUTHOR SEARCH EXPANDED
        for match in re.finditer("\<{2}([^>]*)\>{2}:\(([^)]+)\)", cleanMessage, re.S):
            if match.group(1) != '':
                reply = ''

                if (forceNormal) or (str(message.server).lower() in disableexpanded):
                    reply = await DiscordoragiSearch.buildMangaReplyWithAuthor(match.group(1), match.group(2), message, False, canEmbed)
                else:
                    reply = await DiscordoragiSearch.buildMangaReplyWithAuthor(match.group(1), match.group(2), message, True, canEmbed)

                if (reply is not None):
                    mangaArray.append(reply)
            else:
                print("Empty request, ignoring")

        #Normal Manga
        #NORMAL
        for match in re.finditer("(?<=(?<!\<)\<)([^\<\>]+)\>(?!(:|\>))", cleanMessage, re.S):
            if match.group(1) != '':
                reply = await DiscordoragiSearch.buildMangaReply(match.group(1), message, False, canEmbed)

                if (reply is not None):
                    mangaArray.append(reply)
            else:
                print("Empty request, ignoring")

        #AUTHOR SEARCH
        for match in re.finditer("(?<=(?<!\<)\<)([^\<\>]*)\>:\(([^)]+)\)", cleanMessage, re.S):
            reply = await DiscordoragiSearch.buildMangaReplyWithAuthor(match.group(1), match.group(2), message, False, canEmbed)

            if (reply is not None):
                mangaArray.append(reply)

        #Expanded LN
        for match in re.finditer("\]{2}([^]]*)\[{2}", cleanMessage, re.S):
            if match.group(1) != '':
                reply = ''

                if (forceNormal) or (str(message.server).lower() in disableexpanded):
                    reply = await DiscordoragiSearch.buildLightNovelReply(match.group(1), False, message, canEmbed)
                else:
                    reply = await DiscordoragiSearch.buildLightNovelReply(match.group(1), True, message, canEmbed)

                if (reply is not None):
                    lnArray.append(reply)
            else:
                print("Empty request, ignoring")

        #Normal LN
        for match in re.finditer("(?<=(?<!\])\])([^\]\[]*)(?=\[(?!\[))", cleanMessage, re.S):
            if match.group(1) != '':
                reply = await DiscordoragiSearch.buildLightNovelReply(match.group(1), False, message, canEmbed)

                if (reply is not None):
                    lnArray.append(reply)
            else:
                print("Empty request, ignoring")

        #Here is where we create the final reply to be posted

        #The final message reply. We add stuff to this progressively.
        postedAnimeTitles = []
        postedMangaTitles = []
        postedLNTitles = []

        messageReply = ''
        #Basically just to keep track of people posting the same title multiple times (e.g. {Nisekoi}{Nisekoi}{Nisekoi})
        postedAnimeTitles = []
        postedMangaTitles = []
        postedLNTitles = []
        #Adding all the anime to the final message. If there's manga too we split up all the paragraphs and indent them in Reddit markup by adding a '>', then recombine them
        for i, animeReply in enumerate(animeArray):
            if not (i is 0):
                messageReply += '\n\n'
            if not (animeReply['title'] in postedAnimeTitles):
                postedAnimeTitles.append(animeReply['title'])
                if not canEmbed:
                    messageReply += animeReply['comment']
                else:
                    messageReply = 'n/a'
        if mangaArray:
            messageReply += '\n\n'
        #Adding all the manga to the final message
        for i, mangaReply in enumerate(mangaArray):
            if not (i is 0):
                messageReply += '\n\n'
            if not (mangaReply['title'] in postedMangaTitles):
                postedMangaTitles.append(mangaReply['title'])
                if not canEmbed:
                    messageReply += mangaReply['comment']
                else:
                    messageReply = 'n/a'
        if lnArray:
            messageReply += '\n\n'
        #Adding all the manga to the final comment
        for i, lnReply in enumerate(lnArray):
            if not (i is 0):
                commentReply += '\n\n'

            if not (lnReply['title'] in postedLNTitles):
                postedLNTitles.append(lnReply['title'])
                if not canEmbed:
                    messageReply += lnReply['comment']
                else:
                    messageReply = 'N/A'
        #If there are more than 10 requests, shorten them all
        if not (messageReply is '') and (len(animeArray) + len(mangaArray) >= 10):
            messageReply = re.sub(r"\^\((.*?)\)", "", messageReply, flags=re.M)
    #If there was actually something found, add the signature and post the message to Reddit. Then, add the message to the "already seen" database.
    if not (messageReply is ''):

        if is_edit:
            if not canEmbed:
                await Discord.client.send_message(message.channel, messageReply)
            else:
                for i, animeReply in enumerate(animeArray):
                    await Discord.client.send_message(message.channel, embed=animeReply['embed'])
                for i, mangaReply in enumerate(mangaArray):
                    await Discord.client.send_message(message.channel, embed=mangaReply['embed'])
                for i, lnReply in enumerate(lnArray):
                    await Discord.client.send_message(message.channel, embed=lnReply['embed'])
        else:
            try:
                print("Message created.\n")
                if not canEmbed:
                    await Discord.client.send_message(message.channel, messageReply)
                else:
                    for i, animeReply in enumerate(animeArray):
                        await Discord.client.send_message(message.channel, embed=animeReply['embed'])
                    for i, mangaReply in enumerate(mangaArray):
                        await Discord.client.send_message(message.channel, embed=mangaReply['embed'])
                    for i, lnReply in enumerate(lnArray):
                        await Discord.client.send_message(message.channel, embed=lnReply['embed'])
            except discord.errors.Forbidden:
                print('Request from banned channel: ' + str(message.channel) + '\n')
            except Exception as e:
                print(e)
                traceback.print_exc()
            except:
                traceback.print_exc()
    else:
        try:
            if is_edit:
                return None
            else:
                DatabaseHandler.addMessage(message.id, message.author.id, message.server.id, False)
        except:
            traceback.print_exc()

#Overwrite on_message so we can run our stuff
@Discord.client.event
async def on_message(message):
    from DiscordoragiSearch import isValidMessage #local import here to fix attribute not found error
    print('Message recieved')
    #Is the message valid (i.e. it's not made by Discordoragi and I haven't seen it already). If no, try to add it to the "already seen pile" and skip to the next message. If yes, keep going.
    if not (isValidMessage(message)):
        try:
            if not (DatabaseHandler.messageExists(message.id)):
                DatabaseHandler.addMessage(message.id, message.author.id, message.server.id, False)
        except Exception:
            traceback.print_exc()
            pass
    else:
        await process_message(message)

# ------------------------------------#
#Here's the stuff that actually gets run

#Initialise Discord.
print('Starting Bot')
Discord.run()


from __future__ import unicode_literals
from yt_dlp import YoutubeDL
import sys
from youtubesearchpython import VideosSearch

ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192'
    }],
    'prefer_ffmpeg': True,
    'keepvideo': False,
    'outtmpl': '%(title)s.%(ext)s',
}

videosSearch = VideosSearch(sys.argv[1], limit = 10)
results = videosSearch.result()['result']

for video in results:
    print('Found "' + video['title'] + '" with duration: ' + video['duration'])
    ok = input('Y to download, N to next: ')
    while ok.lower() != 'y' and ok.lower() != 'n':
        ok = input('Y to download, N to next: ')
    ok = ok.lower()

    if ok == 'y':
        with YoutubeDL(ydl_opts) as ydl:
            ydl.download(video['link'])
        print('Done')
        sys.exit()
    else:
        continue

print('No videos found')


# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import json
import urllib.parse

import asks

from linehaul.bigquery.oauth2 import ServiceApplicationClient, TokenExpiredError
from linehaul.logging import SPEW as log_SPEW

GOOGLE_AUDIENCE = "https://www.googleapis.com/oauth2/v4/token"
GOOGLE_TOKEN_URL = "https://www.googleapis.com/oauth2/v4/token"

BIGQUERY_SCOPE = "https://www.googleapis.com/auth/bigquery"

logger = logging.getLogger(__name__)

class TokenFetchError(Exception):
    def __init__(self, *args, status_code, body, **kwargs):
        super().__init__(*args, **kwargs)

        self.status_code = status_code
        self.body = body

class BigQueryError(Exception):
    def __init__(self, *args, status_code, body, **kwargs):
        super().__init__(*args, **kwargs)

        self.status_code = status_code
        self.body = body

class _BigQueryAuthentication:
    def __init__(self, session, account, private_key):
        self._session = session
        self._client = ServiceApplicationClient(
            client_id=account,
            private_key=private_key,
            audience=GOOGLE_AUDIENCE,
            issuer=account,
        )

    async def get_token(self):
        logger.debug("Fetching OAuth2 token from %r", GOOGLE_TOKEN_URL)

        url, headers, body, = self._client.prepare_token_request(
            GOOGLE_TOKEN_URL, scope=BIGQUERY_SCOPE
        )

        resp = await self._session.post(url, headers=headers, data=body)

        if resp.status_code != 200:
            raise TokenFetchError(
                f"Invalid Response Code: {resp.status_code} with body: {resp.text!r}",
                status_code=resp.status_code,
                body=resp.text,
            )

        logger.debug("Saving fetched OAuth2 token.")
        self._client.parse_request_body_response(resp.text)

    async def authenticate(self, url, *args, **kwargs):
        logger.log(log_SPEW, "Authenticating request for %r", url)

        if not self._client.access_token:
            await self.get_token()

        try:
            return self._client.add_token(url, *args, **kwargs)
        except TokenExpiredError:
            logger.debug("OAuth2 token expired.")
            await self.get_token()
            return self._client.add_token(url, *args, **kwargs)

class BigQuery:

    _base_location = "https://www.googleapis.com"

    def __init__(self, account, private_key, *args, max_connections=None, **kwargs):
        super().__init__(*args, **kwargs)

        if max_connections is None:
            max_connections = 30

        self._session = asks.Session(connections=max_connections)
        self._auth = _BigQueryAuthentication(self._session, account, private_key)

    def _make_url(self, path):
        return urllib.parse.urljoin(self._base_location, path)

    async def get_schema(self, target):
        project_id, dataset_id, table_id = target.split(".")
        path = (
            f"/bigquery/v2/projects/{project_id}/datasets/{dataset_id}/"
            f"tables/{table_id}"
        )
        url = self._make_url(path)
        url, headers, body = await self._auth.authenticate(url, http_method="GET")

        resp = await self._session.get(url, headers=headers, data=body)

        if resp.status_code != 200:
            raise BigQueryError(
                f"Invalid Response Code: {resp.status_code} with body: {resp.text!r}",
                status_code=resp.status_code,
                body=resp.text,
            )

        return resp.json().get("schema", {}).get("fields", [])

    async def update_schema(self, target, schema):
        project_id, dataset_id, table_id = target.split(".")
        path = (
            f"/bigquery/v2/projects/{project_id}/datasets/{dataset_id}/"
            f"tables/{table_id}"
        )
        url = self._make_url(path)
        headers = {"Content-Type": "application/json"}
        body = json.dumps({"schema": {"fields": schema}})
        url, headers, body = await self._auth.authenticate(
            url, http_method="PATCH", headers=headers, body=body
        )

        resp = await self._session.request("PATCH", url, headers=headers, data=body)

        if resp.status_code != 200:
            raise BigQueryError(
                f"Invalid Response Code: {resp.status_code} with body: {resp.text!r}",
                status_code=resp.status_code,
                body=resp.text,
            )

    async def insert_all(self, target, rows, template_suffix):
        data = {
            "kind": "bigquery#tableDataInsertAllRequest",
            "skipInvalidRows": True,
            "ignoreUnknownValues": True,
            "templateSuffix": template_suffix,
            "rows": rows,
        }

        project_id, dataset_id, table_id = target.split(".")
        path = (
            f"/bigquery/v2/projects/{project_id}/datasets/{dataset_id}/"
            f"tables/{table_id}/insertAll"
        )
        url = self._make_url(path)
        headers = {"Content-Type": "application/json"}
        body = json.dumps(data)
        url, headers, body = await self._auth.authenticate(
            url, http_method="POST", headers=headers, body=body
        )

        resp = await self._session.post(url, headers=headers, data=body)

        if resp.status_code != 200:
            raise BigQueryError(
                f"Invalid Response Code: {resp.status_code} with body: {resp.text!r}",
                status_code=resp.status_code,
                body=resp.text,
            )


#importing all the libraries
import png, pyqrcode

#link for my instagram account
link = input('Enter the link here: ')

#creatng qr code
code = pyqrcode.create(link)

#saving it to desktop
code.png('QR Code.png', scale = 8)


import inspect
import functools

import icypaw.types as types
from .types import (is_icypaw_type_annotation, make_struct, Field,
                    is_icypaw_scalar_type_annotation)
from .exceptions import IcypawException
from .descriptor import get_object, iter_objects, iter_objects_from_type
from . import conventions, tahu_interface as ti

class icpw_command:
    """A class following the Python descriptor protocol that wraps a
    command."""

    ##
    # Constructor and initialization
    #

    def __init__(self, name=None, properties=None, use_template=True):
        """Return a new descriptor that has worked out a type and name for the
        given command."""

        if callable(name):
            func = name
            name = func.__name__
        else:
            func = None
            name = name

        self._func = func
        self._name = name
        self._use_template = use_template
        self._type = self._make_type() if func else None

        # Merge default command properties with arguments
        self._properties = {
            'Writable': True,
            'ICPWCommand': True
        }
        if properties:
            self._properties.update(properties)

    def __call__(self, func):
        """Handle the case where the user provides a name in the decorator."""

        assert self._func is None
        self._func = func
        if self._name is None:
            self._name = func.__name__
        self._type = self._make_type()
        return self

    def _make_type(self):
        """Create the type object representing the arguments to this
        command. If use_template is True, this will always be a
        Template, even with just one argument. This allows the
        argument to be optional. With use_template set to False a
        scalar type will be used representing the lone argument. If
        there are zero arguments, a dummy boolean argument will be
        created.

        """

        if self._use_template:
            icypaw_type = self._make_template_type()
        else:
            icypaw_type = self._make_scalar_type()

        return icypaw_type

    def _make_template_type(self):
        """Return the type of this command, gleaned from its signature, as a
        template."""

        sig = inspect.signature(self._func)

        field_dict = {}

        for field_name, param in sig.parameters.items():
            if field_name == 'self':
                continue
            if param.annotation == inspect.Parameter.empty:
                raise IcypawException(f"Field {field_name} is unannotated in {self._func.__name__}")
            if not is_icypaw_type_annotation(param.annotation):
                raise IcypawException(f"Field {field_name}'s annotation is not an IcypawType")
            field_default = param.default if param.default != inspect.Parameter.empty else None
            field_dict[field_name] = Field(param.annotation, name=field_name, default=field_default)

        icypaw_type = make_struct(self._name, field_dict)

        return icypaw_type

    def _make_scalar_type(self):
        """Return the type of this command as a single scalar. The signature
        must have zero or one parameters.

        Note that having a single argument with a Template type would
        be indistinguishable from having multiple parameters and
        therefore is not allowed.

        """

        sig = inspect.signature(self._func)

        parameters = [param for name, param in sig.parameters.items()
                      if name != 'self']

        if len(parameters) == 0:
            icypaw_type = types.Boolean
        elif len(parameters) == 1:
            param = parameters[0]
            if param.annotation == inspect.Parameter.empty:
                raise IcypawException(f"Argument is unannotated in {self._func.__name__}")
            if not is_icypaw_type_annotation(param.annotation):
                raise IcypawException("Argument's annotation is not an IcypawType")
            if not is_icypaw_scalar_type_annotation(param.annotation):
                raise IcypawException("Argument's annotation cannot be a Struct")
            icypaw_type = param.annotation
        else:
            raise IcypawException(f"Cannot create a non-template signature with {len(parameters)} parameters")

        return icypaw_type

    ##
    # Public methods
    #

    @property
    def name(self):
        """Return the name this command is to go by on the network."""
        return self._name

    @property
    def type(self):
        """Return the argument type for this command."""
        return self._type

    def tahu_metric(self, with_properties=False):
        """Construct a Tahu Metric representation of this metric."""
        tahu_metric = ti.new_metric()
        tahu_metric.name = conventions.make_command(self.name).encode()
        self.type().set_in_metric(tahu_metric)

        if with_properties:
            self._set_properties(tahu_metric, self._properties)

        return tahu_metric

    ##
    # Descriptor protocol methods
    #

    def __get__(self, instance, _owner):
        """Simply return the wrapped function. This allows the implementor of
        an endpoint to call a command as though it were a regular
        Python function and bypass all type checking.

        """

        # I feel like there should be a way to have Python do this
        # binding for us, but this will work nonetheless.
        return functools.partial(self._func, instance)

    ##
    # Internal API methods
    #

    def run_network(self, instance, icypaw_arg):
        """Run this command using the IcypawType argument sent in a metric
        over the network."""

        assert is_icypaw_type_annotation(type(icypaw_arg)), f"{icypaw_arg} is not an Icypaw type"

        arglist = []
        argdict = {}

        if is_icypaw_scalar_type_annotation(type(icypaw_arg)):
            sig = inspect.signature(self._func)
            parameters = [param for name, param in sig.parameters.items()
                          if name != 'self']
            if isinstance(icypaw_arg, types.Boolean) and len(parameters) == 0:
                # This is idiomatic for no arguments.
                pass
            else:
                arglist.append(icypaw_arg.to_python())
        else:
            # Stuff Python values from icypaw_arg into argdict
            for name, icypaw_value in icypaw_arg.icpw_value.items():
                if not isinstance(icypaw_value, self._func.__annotations__[name]):
                    raise IcypawException(f"Argument {name} to command {self._func.__name__} has wrong type")
                argdict[name] = icypaw_value.to_python()

        self._func(instance, *arglist, **argdict)

    ##
    # Private methods
    #

    @classmethod
    def _set_properties(cls, tahu_metric, property_dict):
        """Apply properties from a python dict to a tahu metric.

        Certain command properties may have ICPW semantics which require special
        handling.
        """
        props = ti.PropertyDict(tahu_metric.properties)
        property_dict = property_dict.copy()

        # There currently aren't any ICPW semantics we want enforced for
        # commands, but I'm leaving this here for future development

        # Add all properties
        props.update(property_dict)

def get_command_object(inst, name):
    """Extract a command object from an instance, bypassing the normal
    descriptor protocol."""

    return get_object(inst, name, icpw_command)

def iter_command_objects(inst):
    """Iterate over (name, obj) tuples of commands accessible in this
    instance."""

    return iter_objects(inst, icpw_command)

def iter_command_objects_from_type(cls):
    return iter_objects_from_type(cls, icpw_command)


'''
REFIT dataset converter for the clean version avaiable at the URLs below:

"REFIT: Electrical Load Measurements (Cleaned)"
https://pureportal.strath.ac.uk/en/datasets/refit-electrical-load-measurements-cleaned
https://pureportal.strath.ac.uk/files/52873459/Processed_Data_CSV.7z
https://pureportal.strath.ac.uk/files/62090184/CLEAN_REFIT_081116.7z

The original version of the dataset include duplicated timestamps.
Check the dataset website for more information.

For citation of the dataset, use:
http://dx.doi.org/10.1038/sdata.2016.122
https://doi.org/10.15129/9ab14b0e-19ac-4279-938f-27f643078cec

'''

import pandas as pd
import numpy as np
from copy import deepcopy
from os.path import join, isdir, isfile, exists
from os import listdir
import fnmatch
import re
from sys import stdout
from nilmtk.utils import get_datastore
from nilmtk.datastore import Key
from nilmtk.timeframe import TimeFrame
from nilmtk.measurement import LEVEL_NAMES
from nilmtk.utils import get_module_directory, check_directory_exists
from nilm_metadata import convert_yaml_to_hdf5, save_yaml_to_datastore

def convert_refit(input_path, output_filename, format='HDF'):
    """
    Parameters
    ----------
    input_path : str
        The root path of the CSV files, e.g. House1.csv
    output_filename : str
        The destination filename (including path and suffix).
    format : str
        format of output. Either 'HDF' or 'CSV'. Defaults to 'HDF'
    """

    # Open DataStore
    store = get_datastore(output_filename, format, mode='w')

    # Convert raw data to DataStore
    _convert(input_path, store, 'Europe/London')

    # Add metadata
    save_yaml_to_datastore(join(get_module_directory(),
                              'dataset_converters',
                              'refit',
                              'metadata'),
                         store)
    store.close()

    print("Done converting REFIT to HDF5!")

def _convert(input_path, store, tz, sort_index=True):
    """
    Parameters
    ----------
    input_path : str
        The root path of the REFIT dataset.
    store : DataStore
        The NILMTK DataStore object.
    measurement_mapping_func : function
        Must take these parameters:
            - house_id
            - chan_id
        Function should return a list of tuples e.g. [('power', 'active')]
    tz : str
        Timezone e.g. 'US/Eastern'
    sort_index : bool
    """

    check_directory_exists(input_path)

    # Iterate though all houses and channels
    # house 14 is missing!
    houses = [1,2,3,4,5,6,7,8,9,10,11,12,13,15,16,17,18,19,20,21]
    nilmtk_house_id = 0
    prefix = ''
    suffix = '_'
    version_checked = False

    for house_id in houses:
        nilmtk_house_id += 1
        print("Loading house", house_id, end="... ")
        stdout.flush()
        csv_filename = join(input_path, prefix + 'House' + suffix + str(house_id) + '.csv')
        if not version_checked:
            version_checked = True

            if exists(csv_filename):
                print('Using original filenames (House_XX.csv)')
            else:
                prefix = 'CLEAN_'
                suffix = ''
                csv_filename = join(input_path, prefix + 'House' + suffix + str(house_id) + '.csv')
                print('Using CLEAN filenames (CLEAN_HouseXX.csv)')

        if not exists(csv_filename):
            raise RuntimeError('Could not find REFIT files. Please check the provided folder.')

        # The clean version already includes header, so we
        # just skip the text version of the timestamp
        usecols = ['Unix','Aggregate','Appliance1','Appliance2','Appliance3','Appliance4','Appliance5','Appliance6','Appliance7','Appliance8','Appliance9']

        df = _load_csv(csv_filename, usecols, tz)
        if sort_index:
            df = df.sort_index() # might not be sorted...
        chan_id = 0
        for col in df.columns:
            chan_id += 1
            print(chan_id, end=" ")
            stdout.flush()
            key = Key(building=nilmtk_house_id, meter=chan_id)

            chan_df = pd.DataFrame(df[col])
            chan_df.columns = pd.MultiIndex.from_tuples([('power', 'active')])

            # Modify the column labels to reflect the power measurements recorded.
            chan_df.columns.set_names(LEVEL_NAMES, inplace=True)

            store.put(str(key), chan_df)

        print('')

def _load_csv(filename, usecols, tz):
    """
    Parameters
    ----------
    filename : str
    usecols : list of columns to keep
    tz : str e.g. 'US/Eastern'

    Returns
    -------
    dataframe
    """
    # Load data
    df = pd.read_csv(filename, usecols=usecols)

    # Convert the integer index column to timezone-aware datetime
    df['Unix'] = pd.to_datetime(df['Unix'], unit='s', utc=True)
    df.set_index('Unix', inplace=True)
    df = df.tz_convert(tz)

    return df


# PROGRAMA DE EJEMPLO PARA TRAER LAS ESTADISTICAS ACTUALES DE CASOS COVID EN EL MUNDO
# AUTOR : CESAR MAYTA
# VERSION : 1.0
# FECHA : 19/02/2021
import matplotlib.pyplot as plt
import urllib.request
import json
# peru - chile - argentina - colombia - ecuador - brazil - bolivia
datos = urllib.request.urlopen('https://api.covid19api.com/country/peru/status/confirmed?from=2020-03-01T00:00:00Z&to=2020-08-02T00:00:00Z').read().decode()
casos = json.loads(datos)

data = {}
cont = 1
for caso in casos:
    nuevoDato = {cont:caso['Cases']}
    data.update(nuevoDato)
    cont += 1
print(data)

names = list(data.keys())
values = list(data.values())

fig, axs = plt.subplots()
axs.plot(names, values)
fig.suptitle('Casos confirmados de covid en Perú')
plt.show()


import json

# Зчитуємо файли
with open("train-v2.0.json", "r") as f_train:
    train_data = json.load(f_train)

with open("dev-v2.0.json", "r") as f_dev:
    dev_data = json.load(f_dev)

# Виводимо приклад питання з тренувального набору даних
print(train_data["data"][0]["paragraphs"][0]["qas"][0]["question"])
print(dev_data["data"][0]["paragraphs"][0]["qas"][0]["question"])


import Adafruit_DHT
import board
import digitalio
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306

class Sensor:
    def __init__(self):
        self.DHT_SENSOR = Adafruit_DHT.DHT22
        self.DHT_PIN = 4
        self.i2c = board.I2C()
        self.oled = adafruit_ssd1306.SSD1306_I2C(128, 32, self.i2c)
        self.font = ImageFont.load_default()

    def get_data(self):
        humidity, temperature = Adafruit_DHT.read_retry(self.DHT_SENSOR, self.DHT_PIN)
        return temperature, humidity

    def display_on_oled(self, temperature, humidity):
        if temperature is None or humidity is None:
            return

        self.oled.fill(0)
        image = Image.new('1', (self.oled.width, self.oled.height))
        draw = ImageDraw.Draw(image)
        text = f'T: {temperature:.1f} C\nH: {humidity:.1f} %'
        draw.text((0, 0), text, font=self.font, fill=255)
        self.oled.image(image)
        self.oled.show()


#write a code to validate string starting and ending with a uppercase_letter except special characters minimum length 5 -maximum length 10

import re
n= input("enter ")

x='^[A-Z][a-zA-Z0-9]{3,8}[A-Z]$'
match = re.fullmatch(x,n)
if match is not None:
    print("valid")
else:
    print("invalid")

#AsfdhjkhjjfA


from typing import List
from topsdk.client import BaseRequest
from topsdk.util import convert_struct_list,convert_basic_list,convert_struct,convert_basic
from datetime import datetime

class TaobaoVmarketEticketManageNotifyRequest(BaseRequest):

    def __init__(
        self,
        order_id: int = None,
        notify_method: str = None,
        codemerchant_id: int = None
    ):
        """
            订单编号
        """
        self._order_id = order_id
        """
            需要调用的通知方法，目前仅支持是send（发码）或resend（重新发码）
        """
        self._notify_method = notify_method
        """
            码商ID，如果是码商，必须传，如果是信任卖家，不需要传
        """
        self._codemerchant_id = codemerchant_id

    @property
    def order_id(self):
        return self._order_id

    @order_id.setter
    def order_id(self, order_id):
        if isinstance(order_id, int):
            self._order_id = order_id
        else:
            raise TypeError("order_id must be int")

    @property
    def notify_method(self):
        return self._notify_method

    @notify_method.setter
    def notify_method(self, notify_method):
        if isinstance(notify_method, str):
            self._notify_method = notify_method
        else:
            raise TypeError("notify_method must be str")

    @property
    def codemerchant_id(self):
        return self._codemerchant_id

    @codemerchant_id.setter
    def codemerchant_id(self, codemerchant_id):
        if isinstance(codemerchant_id, int):
            self._codemerchant_id = codemerchant_id
        else:
            raise TypeError("codemerchant_id must be int")

    def get_api_name(self):
        return "taobao.vmarket.eticket.manage.notify"

    def to_dict(self):
        request_dict = {}
        if self._order_id is not None:
            request_dict["order_id"] = convert_basic(self._order_id)

        if self._notify_method is not None:
            request_dict["notify_method"] = convert_basic(self._notify_method)

        if self._codemerchant_id is not None:
            request_dict["codemerchant_id"] = convert_basic(self._codemerchant_id)

        return request_dict

    def get_file_param_dict(self):
        file_param_dict = {}
        return file_param_dict


import os

from setuptools import setup

ml_pytorch_name = "ml.pytorch"
ml_version = os.environ["ML_VERSION"]

setup(
    name=ml_pytorch_name,
    version=ml_version,
    namespace_packages=["ml"],
    packages=[ml_pytorch_name],
    install_requires=[
        f"ml.base=={ml_version}",
        "torch>=1.4",
    ],
    extras_require={
        "dev": ["pdoc3"]
    },
)


"""
+------------------------------+------------------+----------+
| Description | Published Date | Victim's Website | Post URL |
+------------------------------+------------------+----------+
|      X      |        X       |                  |     X    |
+------------------------------+------------------+----------+
Rappel : def appender(post_title, group_name, description="", website="", published="", post_url=""):
"""
import os,re
from bs4 import BeautifulSoup
from sharedutils import errlog
from parse import appender
from datetime import datetime

def main():
    for filename in os.listdir('source'):
        #try:
            if filename.startswith('cyclops-'):
                html_doc='source/'+filename
                file=open(html_doc,'r')
                soup=BeautifulSoup(file,'html.parser')
                # Find all the <div> elements with class "block-content"
                post_divs = soup.find_all('div', class_='block-content')

                # Iterate over each <div> element to extract the desired information
                for div in post_divs:
                    # Extract the title
                    title = div.find('h2').get_text(strip=True)

                    # Extract the date string
                    date_string = div.find('p', class_='fs-sm').find('span', class_='text-primary').next_sibling.strip()

                    # Convert the date string to a datetime object
                    date = datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S')

                    # Format the date in the desired format
                    formatted_date = date.strftime('%Y-%m-%d %H:%M:%S.%f')

                    # Extract the description
                    description = div.find("strong").text.strip().replace('\t', '').replace('\n', '')
                    #descriptions = re.findall(r'<strong>(.*?)</strong>', str(div), re.DOTALL)
                    #description = BeautifulSoup(descriptions[0], "html.parser").get_text().replace('\t', '').replace('\n', '')

                    appender(title, 'cyclops',description,'',formatted_date)
                file.close()
        #except:
        #    errlog('cyclops : ' + 'parsing fail')
        #    pass


from character import *

class Fight:  # pole walki - tutaj dzieje się cała gra
    def __init__(self, player, enemy):
        self.player = player
        self.enemy = enemy

    def choose_action(self, input):
        return {
            '1': 'fireball',
            '2': 'icicle',
            '3': 'default'
        }.get(input, 'default')

    def print_help(self):
        print("\n 1 - Fireball - Damage: 30, Mana Cost: 50"
              "\n 2 - Icicle - Damage: 25, Mana Cost: 40"
              "\n 3 - Normal Attack - Damage: {}, Mana Cost: 0".format(self.player.atk)
              )

    def fight(self):
        while True:
            print(f"Your hp: {self.player.hp}\nYour mana: {self.player.mp}")
            print(f"{self.enemy.name} hp: {self.enemy.hp}")
            while True:
                try:
                    self.print_help()
                    player_atk = self.player.use_spell(self.choose_action(input("Choose attack: ")))
                    break
                except NotEnoughMana:
                    pass
            self.player.attack(player_atk, self.enemy)
            if self.enemy.is_dead():
                print(f"\033[32mYou killed {self.enemy.name}\033[0m")
                return
            self.enemy.attack(self.enemy.atk, self.player)
            if self.player.is_dead():
                print(f"\033[0:31mYou are dead\033[0m")
                return
            self.player.mana_regen()

print("                  .\n"
      "                   .\n"
      "         /^\     .\n"
      "    /\   \"V\" \n"
      "   /__\   I      O  o\n"
      "  //..\\\  I     .\n"
      "  \].`[/  I\n"
      "  /l\/j\  (]    .  O\n"
      " /. ~~ ,\/I          .\n"
      " \\|L__j^\/I       o\n"
      "  \/--v}  I     o   .\n"
      "  |    |  I   _________\n"
      "  |    |  I c(`       ')o\n"
      "  |    l  I   \.     ,/\n"
      "_/j  L l\_!  _//^---^\\_ \n"
      "Welcome, Young Mage!\n"
      "So you are looking for Adventure and Glory?\n"
      "In front of you is the entrance to The Mage Arena\n"
      "To prove your courage and obtain Power you have to face bloodthirsty monster\n"
      "Shall you enter?\n")

mage = Player(input("What's your name?\n"), 100, 100, 20, 30)

goblin = Enemy("Goblin", 70, 37, 20)
game = Fight(mage, goblin)

print(mage.say_hello())

game.fight()


''' Exercicio do dia 20 '''

from turtle import Screen
from time import sleep
from cobra import Cobra
from bolinha import Bolinha
from pontuacao import Pontuação

cobra = Cobra()
bolinha = Bolinha()
jogo_esta_online = True
Pontos = Pontuação()

# Objeto da classe Screen
cenario = Screen()
cenario.setup(width=600, height=600)
cenario.bgcolor('black')
cenario.title('Ilha das Cobras')
cenario.tracer(0)
cenario.listen()
cenario.onkey(cobra.mover_para_direita, 'Right')
cenario.onkey(cobra.mover_para_esquerda, 'Left')
cenario.onkey(cobra.mover_para_cima, 'Up')
cenario.onkey(cobra.mover_para_baixo, 'Down')

while jogo_esta_online:
    cenario.update()
    sleep(0.1)
    cobra.mover_cobra()

    # detectar colisão
    if cobra.head.distance(bolinha) < 15:
        bolinha.atualizar()
        Pontos.adicionar_ponto()
        cobra.extender_cobrinha()

    # detectar colisão com a parede
    if cobra.head.xcor() > 285 or cobra.head.xcor() < -285 or cobra.head.ycor() > 285 or cobra.head.ycor() < -285:
        Pontos.resetar()
        cobra.resetar_cobrinha()

    # detectar colisão com a própria cobra
    for parte in cobra.cobra[1:]:
        if cobra.head.distance(parte) < 10:
            Pontos.resetar()
            cobra.resetar_cobrinha()

cenario.exitonclick()

exercicios-turtle/snake - game com OOP/main.py


# -*- coding: UTF-8 -*-

# author: luoboovo
# contact: fuyu16032001@gmail.com
# datetime: 2021/1/21 19:20
# software: PyCharm
#         =    =     =
#          =   =   =
#           =  =  =
#         ===========
#         =   萝    =
#         =   卜    =
#         =   神    =
#         =   保    =
#         =   佑    =
#         =   永    =
#         =   无    =
#         =   bug  =
#          =      =
#           =    =
#              =
import socket

def receivePicture():
    while 1:
        udp_data = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        local_addrs = ('', 8009)
        udp_data.bind(local_addrs)
        reva_data = udp_data.recvfrom(50)
        print(reva_data[0])
        with open('10.jpeg', 'ab') as f:
            f.write(reva_data[0])
        # if reva_data == None:
        #     break
        # data.append(reva_data[0])

        #

        udp_data.close()

if __name__ == '__main__':
    # receivePicture()
    # 4，    对压缩文件进行加密操作和解密操作
    # （提示，以字节形式读取指定压缩文件，交换两个
    # 固定位置的字节编码，再写到新位置，删除源文件，
    # 解密即为重复加密过程）
    import os
    def encryption():
        with open('test.txt','rb') as f:
            a = f.read()
        with open('test_encryption.txt','wb') as f :
            b = a[len(a)//2:] + a[:len(a)//2]
            f.write(b)
        os.remove('test.txt')
    def decrypt():
        with open('test_encryption.txt','rb') as f:
            a = f.read()
        with open('test_decrypt.txt', 'wb') as f:
            b = a[len(a) // 2:] + a[:len(a) // 2]
            f.write(b)
    # # encryption()
    # decrypt()
    # 5，    定义类A，A中具有属性，id
    # name
    # age
    # sex
    # 定义类B，B中具有方法add_excel(a), 其中参数a是类A的对象
    # 方法具有像excel表格中插入a数据的功能
    import pandas as pd

    class A:
        def __init__(self, id, name, age, sex):
            self.id = id
            self.name = name
            self.age = age
            self.sex = sex

    class B(A):
        def add_excel(self):
            data = pd.DataFrame([self.id, self.name, self.age, self.sex])
            data.to_excel('test.xls')
    b = B(1,'name',18,1)
    b.add_excel()


from BackTestBase import *

class BackTestLongOnly(BackTestBase):
    def run_sma_strategy(self, SMA1: int, SMA2: int):
        ''' Back-testing a SMA-based strategy.

        Parameters
        ==========
        SMA1, SMA2: int
            shorter and longer term simple moving average (in days)
        '''
        msg = f'\n\nRunning SMA strategy | SMA1={SMA1} & SMA2={SMA2}'
        msg += f'\nfixed costs {self.ftc} | '
        msg += f'proportional costs {self.ptc}'
        print(msg)
        print('=' * 55)

        self.reset_strategy()
        raw = self.data.copy()

        raw['SMA1'] = raw['price'].rolling(SMA1).mean()
        raw['SMA2'] = raw['price'].rolling(SMA2).mean()
        raw['position'] = 0

        bar = 0
        for bar in range(SMA2, len(raw)):
            if self.position == 0:
                if raw['SMA1'].iloc[bar] > raw['SMA2'].iloc[bar]:
                    self.place_buy_order(bar, amount=self.amount)
                    self.position = 1  # long position

            elif self.position == 1:
                if raw['SMA1'].iloc[bar] < raw['SMA2'].iloc[bar]:
                    self.place_sell_order(bar, units=self.units)
                    self.position = 0  # market neutral

            # add position and balance to the DataFrame
            price = self.get_date_price(bar)[1]
            valuation = self.units * price + self.amount
            raw.at[raw.index[bar], 'valuation'] = valuation
            raw.at[raw.index[bar], 'position'] = self.position

        self.result = raw

        self.close_out(bar)
        self.calculate_statistics()
        self.print_strategy_resume()
        self.plot_data(['price', 'SMA1', 'SMA2'], raw)
        self.plot_strategy()

    def run_momentum_strategy(self, momentum=1):
        ''' Back-testing a momentum-based strategy.

        Parameters
        ==========
        momentum: int
            number of days for mean return calculation
        '''
        msg = f'\n\nRunning momentum strategy | {momentum} candle(s)'
        msg += f'\nfixed costs {self.ftc} | '
        msg += f'proportional costs {self.ptc}'
        print(msg)
        print('=' * 55)

        self.reset_strategy()
        raw = self.data.copy()

        raw['momentum'] = raw['return'].rolling(momentum).mean()
        raw['position'] = 0

        bar = 0
        for bar in range(momentum, len(raw)):
            if self.position == 0:
                if raw['momentum'].iloc[bar] > 0:
                    self.place_buy_order(bar, amount=self.amount)
                    self.position = 1  # long position
            elif self.position == 1:
                if raw['momentum'].iloc[bar] < 0:
                    self.place_sell_order(bar, units=self.units)
                    self.position = 0  # market neutral

            # add position and balance to the DataFrame
            price = self.get_date_price(bar)[1]
            valuation = self.units * price + self.amount
            raw.at[raw.index[bar], 'valuation'] = valuation
            raw.at[raw.index[bar], 'position'] = self.position

        self.result = raw

        self.close_out(bar)
        self.calculate_statistics()
        self.print_strategy_resume()
        self.plot_strategy()

if __name__ == '__main__':
    start = dt.datetime(2022, 6, 1)
    end = dt.datetime(2022, 6, 3)

    lobt = BackTestLongOnly(start, end, 100000, verbose=False)

    lobt.run_sma_strategy(90, 194)
    lobt.run_momentum_strategy(1)


# Author: Braedy Kuzma

from django.core.paginator import Paginator, InvalidPage
from rest_framework.views import APIView

from dash.models import Comment, Author, RemoteCommentAuthor
from .serializers import CommentSerializer
from .verifyUtils import addCommentValidators, InvalidField, ResourceConflict, \
                         DependencyError, NotVisible
from .dataUtils import validateData, getCommentData, getPost
from .httpUtils import JSONResponse

class CommentView(APIView):
    """
    This view gets
    """
    def get(self, request, pid):
        # Try to pull page number out of GET
        try:
            pageNum = int(request.GET.get('page', 0))
        except ValueError:
            raise InvalidField('page', request.GET.get('page'))

        # Paginator one-indexes for some reason...
        pageNum += 1

        # Try to pull size out of GET
        try:
            size = int(request.GET.get('size', 50))
        except ValueError:
            raise InvalidField('size', request.GET.get('size'))

        # Only serve max 100 comments per page
        if size > 100:
            size = 100

        # Get comments and the count
        post = getPost(request, pid)
        comments = Comment.objects.filter(post=post)
        count = comments.count()

        # Set up the Paginator
        pager = Paginator(comments, size)

        # Get our page
        try:
            page = pager.page(pageNum)
        except InvalidPage:
            data = {'query': 'comments',
                    'count': count,
                    'comments': [],
                    'size': 0}

            if pageNum > pager.num_pages:
                # Last page is num_pages - 1 because zero indexed for external
                uri = request.build_absolute_uri('?size={}&page={}'\
                                                 .format(size,
                                                         pager.num_pages - 1))
                data['last'] = uri
            elif pageNum < 1:
                # First page is 0 because zero indexed for external
                uri = request.build_absolute_uri('?size={}&page={}'\
                                                 .format(size, 0))
                data['first'] = uri
            else:
                # Just reraise the error..
                raise

            return JSONResponse(data)

        # Start our query response
        respData = {}
        respData['query'] = 'comments'
        respData['count'] = count
        respData['size'] = size if size < len(page) else len(page)

        # Now get our data
        comSer = CommentSerializer(page, many=True)
        respData['comments'] = comSer.data

        # Build and our next/previous uris
        # Next if one-indexed pageNum isn't already the page count
        if pageNum != pager.num_pages:
            # Just use page num, we've already incremented and the external
            # inteface is zero indexed
            uri = request.build_absolute_uri('?size={}&page={}'\
                                             .format(size, pageNum))
            respData['next'] = uri

        # Previous if one-indexed pageNum isn't the first page
        if pageNum != 1:
            # Use pageNum - 2 because we're using one indexed pageNum and the
            # external interface is zero indexed
            uri = request.build_absolute_uri('?size={}&page={}'\
                                             .format(size, pageNum - 2))
            respData['previous'] = uri

        return JSONResponse(respData, status=200)

    def post(self, request, pid):
        """
        This creates comments on posts.
        """
        # Get and validate the sent data
        data = getCommentData(request)
        validateData(data, addCommentValidators)

        # See if a comment exists already
        try:
            comment = Comment.objects.get(id=data['comment']['id'])
        # We WANT it to be not found
        except Comment.DoesNotExist:
            pass
        # No error was raised which means it already exists
        else:
            raise ResourceConflict('comment', data['comment']['id'])

        # Get the post this should be attached to
        post = getPost(request, pid)

        # Check if post is SERVERONLY, they can't post comments to a SERVERONLY
        # post (how did they even SEE it?)
        if post.visibility == 'SERVERONLY':
            raise NotVisible('Access denied: post has SERVERONLY visibility')

        # Ensure that the url they POST'd to was the URL they said they were
        # posting to
        if post.id != data['post']:
            data = {'post.id': post.id,
                    'query.post': data['post']}
            raise DependencyError(data)

        # Pull out comment specific data
        commentData = data['comment']

        # Build comment
        comment = Comment()
        comment.author = commentData['author']['id']
        comment.post = post
        comment.comment = commentData['comment']
        comment.contentType = commentData['contentType']
        comment.published = commentData['published']
        comment.id = commentData['id']

        comment.save()

        authorData = commentData['author']
        authorId = authorData['id']
        # Try to get as a local author
        try:
            author = Author.objects.get(id=authorId)
        # Not a local author, we don't care, just make them remote
        except Author.DoesNotExist:
            # Try and get remote author, if we find, then update
            try:
                author = RemoteCommentAuthor.objects.get(authorId=authorId)
                author.displayName = authorData['displayName']
                author.host = authorData['host']
                author.github = authorData.get('github', '')
                author.save()
            # Didn't exist, so make!
            except RemoteCommentAuthor.DoesNotExist:
                author = RemoteCommentAuthor()
                author.authorId = authorId
                author.displayName = authorData['displayName']
                author.host = authorData['host']
                author.save()

        # TODO check if author has visibility,403 if they don't

        data = {
            "query": "addComment",
            "success": True,
            "message": "Comment Added"
        }

        return JSONResponse(data)


import requests
from bs4 import BeautifulSoup

url = "https://www.baidu.com/"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")

links = soup.find_all("a")
nodes = []
edges = []
for link in links:
    url = link["href"]
    nodes.append({"label": url})
    edges.append({"source": nodes.index(nodes[-2]), "target": nodes.index(nodes[-1])})

# 导入nodes和edges到Gephi,进行可视化和分析


from datetime import datetime
from typing import Optional, Any, Dict, List

import attr
from toolz import pipe

@attr.s(auto_attribs=True, frozen=True)
class ScheduleLog:
    id: str
    batch_result: str
    project_name: str
    project_id: int
    datetime: datetime
    data: Optional[Dict[str, Any]]
    duration: float

    @staticmethod
    def from_dict(raw_dic: Dict[str, Any]) -> "ScheduleLog":
        dic: Dict[str, Any] = {
            **{k: v for k, v in raw_dic.items() if k != "_id"},
            "id": str(raw_dic["_id"]),
        }

        return ScheduleLog(
            id=dic["id"],
            project_id=dic["project_id"],
            project_name=dic["project_name"],
            datetime=dic["datetime"],
            batch_result=dic["batch_result"],
            data=dic["data"],
            duration=dic["duration"]
        )

@attr.s(auto_attribs=True, frozen=True)
class ScheduleProject:
    project_id: int
    project_name: str
    credential_script: str
    credential_url: str
    datetime: datetime

    @staticmethod
    def from_dict(raw_dic: Dict[str, Any]) -> "ScheduleProject":
        keys = set(attr.fields_dict(ScheduleProject).keys())
        command = raw_dic.get("command", dict())
        dic: Dict[str, Any] = {
            **{k: v for k, v in command.items() if k in keys},
            **{k: v for k, v in raw_dic.items() if k in keys},
        }
        return ScheduleProject(
            project_id=dic["project_id"],
            project_name=raw_dic["_id"],
            credential_script=command.get("credentials", dict()).get(
                "script_name"
            ),
            credential_url=command.get("credentials", dict()).get(
                "shotgrid_url"
            ),
            datetime=dic["datetime"],
        )

@attr.s(auto_attribs=True, frozen=True)
class EnhancedScheduleProject(ScheduleProject):
    latest_logs: List[ScheduleLog]

    @staticmethod
    def from_entities(
        project: ScheduleProject, logs: List[ScheduleLog]
    ) -> "EnhancedScheduleProject":
        params = {
            **attr.asdict(project),
            "latest_logs": logs,
        }
        return pipe(params, lambda x: EnhancedScheduleProject(**x))

@attr.s(auto_attribs=True, frozen=True)
class ScheduleQueueItem:
    id: str
    project_name: str
    project_id: int
    datetime: datetime

    @staticmethod
    def from_dict(raw_dic: Dict[str, Any]) -> "ScheduleQueueItem":
        keys = set(attr.fields_dict(ScheduleQueueItem).keys())
        upper = {"id" if k == "_id" else k: v for k, v in raw_dic.items()}
        commands = raw_dic.get("command", dict()).items()
        dic: Dict[str, Any] = {
            **{k: v for k, v in commands if k in keys},
            **{k: v for k, v in upper.items() if k in keys},
            "id": str(upper["id"]),
        }
        return ScheduleQueueItem(
            id=dic["id"],
            project_name=dic["project_name"],
            project_id=dic["project_id"],
            datetime=dic["datetime"],
        )

@attr.s(auto_attribs=True, frozen=True)
class ShotgridCredentials:
    shotgrid_url: str
    script_name: str
    script_key: str

    def to_mongo(self) -> Dict[str, Any]:
        return {
            "_id": self.shotgrid_url,
            "script_name": self.script_name,
            "script_key": self.script_key,
        }

    @staticmethod
    def from_struct(struct: Any) -> "ShotgridCredentials":
        if not attr.has(type(struct)):
            raise Exception(f"Unsupported type {type(struct)} of {struct}")
        return ShotgridCredentials.from_dict(attr.asdict(struct))

    @staticmethod
    def from_mongo(dic: Dict[str, Any]) -> "ShotgridCredentials":
        return ShotgridCredentials(
            shotgrid_url=dic["_id"],
            script_name=dic["script_name"],
            script_key=dic["script_key"],
        )

    @staticmethod
    def from_dict(dic: Dict[str, Any]) -> "ShotgridCredentials":
        return ShotgridCredentials(
            shotgrid_url=dic["shotgrid_url"],
            script_name=dic["script_name"],
            script_key=dic["script_key"],
        )


import sqlite3
import datetime
import time
import pandas as pd
class DBparser:
    def __init__(self):
        self.oil_types_names = ['АИ-95']
        connection = sqlite3.connect('azs.db')
        cursor = connection.cursor()

        cursor.execute('''SELECT * FROM type_oil''')
        oil_types = cursor.fetchall()

        self.oil_types_id = [el[0] for el in oil_types]
        self.oil_types_names = [el[1] for el in oil_types]

        self.oil_type_id_from_name = dict(zip(self.oil_types_names, self.oil_types_id))
        self.name_oil_from_oil_type_id = dict(zip(self.oil_types_id, self.oil_types_names))

        cursor.execute('''SELECT * FROM oil''')
        oil = cursor.fetchall()

        oil_ids = [el[0] for el in oil]
        oil_names = [el[1] for el in oil]

        self.oil_id_from_oil_name = dict(zip(oil_names, oil_ids))
        self.oil_name_from_oil_id = dict(zip(oil_ids,oil_names))

        cursor.execute('''SELECT * FROM station''')
        station = cursor.fetchall()

        id_stations = [el[0] for el in station]
        links_station = [el[1] for el in station]

        self.station_links_from_id = dict(zip(id_stations,links_station))
        self.station_id_from_link = dict(zip(links_station, id_stations))

        cursor.execute('''SELECT * FROM station_company''')
        station_company = cursor.fetchall()
        id_stations = [el[0] for el in station_company]
        id_company = [el[1] for el in station_company]

        cursor.execute('''SELECT * FROM company''')
        company = cursor.fetchall()
        id_company_company = [el[0] for el in company]
        name_company = [el[1] for el in company]
        self.id_company_to_name_company = dict(zip(id_company_company, name_company))
        self.name_company_to_id_company = dict(zip(name_company, id_company_company))

        self.id_company_from_id_station = dict(zip(id_stations, id_company))

        connection.commit()
        connection.close()
    def GetSingleDayDf(self, date, name_oil_type):
        id_oil_type = self.oil_type_id_from_name[name_oil_type]

        connection = sqlite3.connect('azs.db')
        cursor = connection.cursor()

        cursor.execute(f''' SELECT id_oil FROM oil_id_type_oil_id WHERE type_oil_id={id_oil_type}''')
        id_oil_to_df = [el[0] for el in cursor.fetchall()]
        name_oil_to_df = [self.oil_name_from_oil_id[el] for el in id_oil_to_df]
        print(f"Oil id to df: {id_oil_to_df}")

        cursor.execute(f'''SELECT * FROM price WHERE date > {time.mktime(date.timetuple())} and date < {time.mktime(date.timetuple())+24*60*60}''')
        price_table = cursor.fetchall()
        station_links = [self.station_links_from_id[el[0]] for el in price_table]
        oil_names = [self.oil_name_from_oil_id[el[1]] for el in price_table]
        dates = [datetime.datetime.fromtimestamp(el[2]).strftime('%d-%m-%Y') for el in price_table]
        id_company_of_station = [self.id_company_from_id_station[el[0]] for el in price_table]
        name_station = [self.id_company_to_name_company[el] for el in id_company_of_station]
        price_values = [el[3] for el in price_table]

        connection.commit()
        connection.close()
        df = pd.DataFrame(
            data={"Название": name_station,
                  "Ссылка": station_links,
                  "Тип топлива": oil_names,
                  "Цена": price_values}
        )

        return df[df['Тип топлива'].isin(name_oil_to_df)]


"""
"""

import sys
sys.path.insert(0, '../')

from random import randrange
from droiddepot.connection import discover_droid, DroidCommandId
from droiddepot.script import DroidScripts
from time import sleep
from bleak import BleakError
import asyncio

async def main() -> None:
    """
    Main entry point into the example application
    """

    d = await discover_droid(retry=True)
    try:
        await d.connect(silent=True)
        await d.audio_controller.set_volume(20)
        d.script_engine.start_beacon_reactions()

        while d.droid.is_connected:
            sleep(1)

    except OSError as err:
        print(f"Discovery failed due to operating system: {err}")
    except BleakError as err:
        print(f"Discovery failed due to Bleak: {err}")
    except KeyboardInterrupt as err:
        pass
    finally:
        print("Shutting down.")
        await d.disconnect()

# Main entry point into the example application
if __name__ == "__main__":
    asyncio.run(main())


# -*- coding: utf-8 -*-
"""
Created on Wed May 10 19:11:51 2023

@author: Chunhui TU
"""
import numpy as np
import math
import random
import time
import utils # make sure that MCTS.py, experiment.py, utils.py and main.py are under the same directory and set a correct work dir

def generate_Map(mapSize, nPoint):
    """
    Create a map of a specified size,
    generate a specified number of random bonus/punishment points,
    and randomly generate start point
    """

    # create an empty 1D array
    emptySpace = [0 for i in range(mapSize[0]*mapSize[1])]

    # Select n random elements from the emptySpace
    randomIndices = random.sample(range(len(emptySpace)), nPoint)
    # Random start point
    randomStart   = random.sample(range(len(emptySpace)), 1)[0]

    # avoid start point is at the same position of random b/p point
    while randomStart in randomIndices:
        randomStart = random.sample(range(len(emptySpace)), 1)

    # convert their 1D position to 2D position
    randomPoints = []
    for index in randomIndices:
        i = index // mapSize[0]
        j = index % mapSize[0]
        randomPoints.append((i,j))

    # birth point
    i_b = randomStart // mapSize[0]
    j_b = randomStart % mapSize[0]
    birthPoint = (i_b, j_b)

    return randomPoints, birthPoint

# generate random bonus/penalty marks
def generate_Random_Marks(nList, ratio):

    # Generate a complete list of rewards and penalties
    penaltyList = np.arange(-0.1, -1.1, -0.02).tolist()
    bonusList   = np.arange(0.1, 1, 0.02).tolist()

    # confirm the actual random rewards list
    nBonus   = math.ceil(nList * ratio)
    nPenalty = nList - nBonus
    rBonus   = random.sample(bonusList, nBonus)
    rPenalty = random.sample(penaltyList, nPenalty)

    # final list
    randomList = rBonus + rPenalty

    return randomList

class GameState:
    def __init__(self, mapSize, randomPoints, randomMarkList, rbirthPoint, birthPoint=None):
        self.board          = [[0 for _ in range(mapSize[0])] for _ in range(mapSize[1])]
        self.mapSize        = mapSize
        self.randomPoints   = randomPoints
        self.randomMarkList = randomMarkList
        self.rbirthPoint    = rbirthPoint
        self.currentPlayer  = 1
        self.score          = 0

        # Initialize birth point
        # if birthPoint is not specified manually, use the randomly generated birth point
        if birthPoint is None:
            self.currentPoint = rbirthPoint
        else:
            self.currentPoint = birthPoint

    def get_Possible_Actions(self):

        x, y = self.currentPoint
        possibleActions = []

        # populate the list of actions based on the current game state
        # make sure to exclude actions that move beyond the boundary of the game map

        # check north
        if y > 0:
            possibleActions.append(['N', (x, y-1)])
        # check south
        if y < 9:
            possibleActions.append(['S', (x, y+1)])
        # check west
        if x > 0 :
            possibleActions.append(['W', (x-1, y)])
        # check east
        if x < 9:
            possibleActions.append(['E', (x+1, y)])

        return possibleActions

    def get_Next_State(self, action):
        # new position
        x = action[1][0]
        y = action[1][1]

        nextState = GameState(mapSize        = self.mapSize,
                              randomPoints   = self.randomPoints,
                              randomMarkList = self.randomMarkList,
                              rbirthPoint    = self.rbirthPoint,
                              birthPoint     = (x,y))

        nextState.board = [row[:] for row in self.board]
        nextState.board[x][y] = self.currentPlayer
        nextState.currentPlayer = -self.currentPlayer

        if nextState.currentPoint in self.randomPoints:
            # random score
            score = random.sample(self.randomMarkList, 1)[0]
            self.score += score

        return nextState

    def get_Winner(self):

        win = False

        if self.score >= 0.5:
            win = True

        return win

    def is_Game_Over(self):

        gameOver = False

        if self.score <= -0.5:
            gameOver = True
        elif self.get_Winner():
            gameOver = True
        elif all(all(row) for row in self.board):
            gameOver = True

        return gameOver

class Node:
    def __init__(self, state, parent=None):
        self.state    = state
        self.parent   = parent
        self.children = []
        self.visits   = 0
        self.wins     = 0

    def add_child(self, childState):
        childNode = Node(state=childState, parent=self)
        self.children.append(childNode)
        return childNode

    def expand(self):
        untriedActions = self.state.get_Possible_Actions()
        if untriedActions:
            action    = random.choice(untriedActions)
            nextState = self.state.get_Next_State(action)
            childNode = self.add_child(childState=nextState)

            return childNode
        return None

    def simulate(self):
        currentState = self.state
        while not currentState.is_Game_Over():
            action = random.choice(currentState.get_Possible_Actions())
            currentState = currentState.get_Next_State(action)
            #print(currentState.currentPoint)
        return currentState.get_Winner()

    def backpropagate(self, result):
        node = self
        while node:
            node.visits += 1
            node.wins += result
            node = node.parent

    def get_Best_Child(self):
        bestChild = max(self.children, key=lambda c: c.wins / c.visits)
        return bestChild.state

def monte_Carlo_Tree_Search(initial_State, num_Iterations=100, select_method='UCB1', C_p=0.9):

    # monitor the resources consumption
    # include CPU, MEM and time
    # record start time of total
    startTime_Total = time.time()
    monitor_thread, stop_event, cpu_percent_list, memory_usage_list = utils.monitor_Resource_Usage()

    # start MCTS
    rootNode = Node(initial_State)

    for _ in range(num_Iterations):
        node = rootNode

        # Selection
        while node.children:
            # UCB1
            if select_method == 'UCB1':
                node = max(node.children, key=lambda c: c.wins / c.visits + math.sqrt(2 * math.log(node.visits) / c.visits))
            else:
            # UCT
                node = max(node.children, key=lambda c: c.wins / c.visits + 2 * C_p * math.sqrt(2 * math.log(node.visits) / c.visits))

        # Expansion
        childNode = node.expand()
        #print('childNode:', childNode.state.currentPoint)
        if childNode:
            result = childNode.simulate()
            childNode.backpropagate(result)
            bestChild_State = rootNode.get_Best_Child()

    # calculate resources consumption
    endTime_Total = time.time()
    # Record the resource usage after 1 second as a benchmark
    time.sleep(1)
    # stop monitoring
    stop_event.set()
    # join the monitoring thread to wait for it to finish
    monitor_thread.join()

    # calculate the overall resources consumption
    timeSpent_Total = endTime_Total - startTime_Total
    timeSpent_Loop  = timeSpent_Total/num_Iterations
    cpu_avg, mem_avg = utils.calculate_Resource_Usage(cpu_percent_list, memory_usage_list, timeSpent_Total)
    resources = [round(timeSpent_Total, 3), round(timeSpent_Loop, 3),  cpu_avg, mem_avg]

    return bestChild_State, resources

################################ END ##########################################


from PyQt5.QtGui import QFont
from PyQt5 import QtCore, QtGui, QtWidgets

import var
from dni import Dni
import datetime
from datetime import *

class Clientes:

    def validarDNI():
        try:
            dni=var.ui.leDNI.text()
            var.ui.leDNI.setText(dni.upper())

            if (len(dni) == 9):
                numero = ""
                i = 0
                while (i < 8):
                    numero = numero + dni[i]
                    i += 1
                letra = dni[8]
                letra=letra.upper()
                dniCorrecto = Dni(numero)
                if (dniCorrecto.letra == letra):
                    print("DNI CORRECTO")
                    var.ui.lblValidarDni.setStyleSheet('QLabel {color:blue;font-size:14pt;font-weight:bold}')
                    var.ui.lblValidarDni.setFont(QFont("Forte"))
                    var.ui.lblValidarDni.setText('V')

                else:
                    print("DNI INCORRECTO")
                    var.ui.lblValidarDni.setStyleSheet('QLabel {color:red;font-size:14pt;font-weight:bold}')
                    var.ui.lblValidarDni.setFont(QFont("Forte"))
                    var.ui.lblValidarDni.setText('X')

            else:
                print("DNI INCORRECTO")
                var.ui.lblValidarDni.setStyleSheet('QLabel {color:red;font-size:14pt;font-weight:bold}')
                var.ui.lblValidarDni.setFont(QFont("Forte"))
                var.ui.lblValidarDni.setText('X')
        except Exception as error:
            print("Error %s: " % str(error))

    def selTransaccion(self):
        try:
            global transaccion
            if var.ui.rbIngresar.isChecked():
                print('marcado ingresar')
                transaccion=True
            if var.ui.rbRetirar.isChecked():
                print('marcado retirar')
                transaccion=False
        except Exception as error:
            print('Error en módulo seleccionar transacción:',error)

    def operacion():
        try:
            var.cantidad = int(var.ui.leCantidad.text())
            if(transaccion==True):
                var.saldo=var.saldo+var.cantidad
                var.ui.textEditSaldo.setText(str(var.saldo))
            elif(transaccion==False):
                var.saldo=var.saldo-var.cantidad
                var.ui.textEditSaldo.setText(str(var.saldo))
            else:
                var.saldo=var.saldo
                var.ui.textEditSaldo.setText(str(var.saldo))
        except Exception as error:
            print('Error en el modulo operación: ',error)

    def abrirCalendario(self):
        try:
            var.dlgCalendario.show()
        except Exception as error:
            print('Error: %s' % str(error))

    def edad(qDate):
        try:
            #global age
            today = datetime.now()
            var.age = today.year - qDate.year() - ((today.month, today.day) < (qDate.month(), qDate.day()))
            print(var.age)
        except Exception as error:
            print('Error en el modulo de edad: ',error)

    def cargarFecha(qDate):
        try:
            data=('{0}/{1}/{2}'.format(qDate.day(),qDate.month(),qDate.year()))
            if var.age <= 18:
                var.dlgEdad.show()
                var.ui.leFecha.setText("")
                var.ui.leDNI.setText("")
                var.ui.leApellido1.setText("")
                var.ui.leApellido2.setText("")
                var.ui.leNombre.setText("")
                var.ui.leCantidad.setText("")
            else:
                var.ui.leFecha.setText(str(data))
            var.dlgCalendario.hide()
        except Exception as error:
            print('Error: %' % str(error))

    def comprobacion():
        if (var.ui.lblValidarDni.text()=="" or var.ui.lblValidarDni.text()=="X") or var.ui.leFecha.text()=="":
            var.info=False
            print('FALTAN EL DNI O AL FECHA DE NACIMIENTO')
        else:
            var.info=True


# Complete the solve function below.
def solve(s):
    ret = s[0].upper()
    i =1
    while i<len(s):
        ret+=s[i]
        if s[i] == " ":
            if s[i+1] != " ":
                ret+=s[i+1].upper()
                i+=1
        i+=1
    return ret


import csv
import random
import statistics
import pandas as pd
import plotly.figure_factory as ff
import plotly.graph_objects as go

df = pd.read_csv("sample_2.csv")
data = df["reading_time"].tolist()

#fig = ff.create_distplot([data], ["Math_score"],show_hist=False)
#fig.show()
pmean = statistics.mean(data)
pstd_dev = statistics.stdev(data)
print("The z score is =", pmean)
print("std dev of sampling distrubition", pstd_dev)

def random_set_of_mean(counter):
    dataset=[]
    for i in range(0,counter):
        random_index = random.randint(0,len(data)-1)
        value= data[random_index]
        dataset.append(value)
    mean = statistics.mean(dataset)
    return mean

mean_list=[]
for i in range (0,1000):
    set_of_mean = random_set_of_mean(100)
    mean_list.append(set_of_mean)

std_dev =statistics.stdev(mean_list)
mean =  statistics.mean(mean_list)
print("mean of sampling distribbution ", mean)

std_1s,std_1e=mean-std_dev,mean+std_dev
std_2s,std_2e=mean-(2*std_dev),mean+(2*std_dev)
std_3s,std_3e=mean-(3*std_dev),mean+(3*std_dev)
fig = ff.create_distplot([mean_list], ["studentMark"], show_hist=False)
fig.add_trace(go.Scatter(x=[mean,mean],y=[0,0.20],mode="lines", name="mean"))
fig.add_trace(go.Scatter(x=[mean,mean],y=[0,0.17],mode="lines",name="MEAN"))
fig.add_trace(go.Scatter(x=[std_1s,std_1s],y=[0,0.17],mode="lines",name="std_dev"))
fig.add_trace(go.Scatter(x=[std_1e,std_1e],y=[0,0.17],mode="lines",name="std_dev"))
fig.add_trace(go.Scatter(x=[std_2s,std_2s],y=[0,0.17],mode="lines",name="std_dev2"))
fig.add_trace(go.Scatter(x=[std_2e,std_2e],y=[0,0.17],mode="lines",name="std_dev2"))
fig.show()

df = pd.read_csv("sample_2.csv")
data = df ["reading_time"].tolist()

mean_of_sample1 = statistics.mean(data)
print("mean_of_sample1", mean_of_sample1)
fig = ff.create_distplot([mean_list],["studentmarks"], show_hist=False)
fig.add_trace(go.Scatter(x=[mean,mean],y=[0,0.17],mode="lines",name="mean"))
fig.add_trace(go.Scatter(x=[mean_of_sample1,mean_of_sample1],y=[0,0.17],mode="lines",name="mean_of_sample1"))
fig.add_trace(go.Scatter(x=[std_1e,std_1e],y=[0,0.17],mode="lines",name="std1_end"))
fig.add_trace(go.Scatter(x=[std_2e,std_2e],y=[0,0.17],mode="lines",name="std2_end"))
fig.show()


import os
import shutil
from glob import glob
from tqdm import tqdm
import numpy as np
import cv2
from PIL import Image

def main(root_dir, out_dir, rel_dir=None, min_ratio=0.1):
    in_dir = root_dir if rel_dir is None else os.path.join(root_dir, rel_dir)
    seg_files = []

    # For each sub-directory in the input directory
    subdirs = [os.path.join(in_dir, d) for d in os.listdir(in_dir) if os.path.isdir(os.path.join(in_dir, d))]
    for curr_dir in tqdm(sorted(subdirs)):
        curr_seg_files = [os.path.basename(curr_dir) + '/' + os.path.basename(f) for f in glob(curr_dir + '/*.png')]
        curr_seg_files = sorted(curr_seg_files)
        seg_files += curr_seg_files

    # Append relative directory to segmentation paths
    for i in range(len(seg_files)):
        seg_files[i] = os.path.join(rel_dir, seg_files[i])

    # Filter out bad segmentations
    valid_seg_files = []
    for seg_file in seg_files:
        seg_path = os.path.join(root_dir, seg_file)
        seg = np.array(Image.open(seg_path))
        mask_ratio = np.count_nonzero(seg) / seg.size
        if mask_ratio > min_ratio:
            valid_seg_files.append(seg_file)

    # Write landmarks and bounding boxes to file
    np.savetxt(os.path.join(out_dir, 'seg_list.txt'), valid_seg_files, fmt='%s')

if __name__ == "__main__":
    # Parse program arguments
    import argparse
    parser = argparse.ArgumentParser('produce_masks')
    parser.add_argument('input', metavar='DIR', help='input root directory')
    parser.add_argument('-o', '--output', metavar='DIR', help='output directory')
    parser.add_argument('-r', '--rel_dir', metavar='DIR', help='relative directory')
    parser.add_argument('-mr', '--min_ratio', metavar='F', type=float, default=0.1,
                        help='minimum ratio of mask non zero pixels to image pixels')
    args = parser.parse_args()
    main(args.input, args.output, args.rel_dir, args.min_ratio)


#--------------------------------------------------------------------
#Administration Details
#--------------------------------------------------------------------
__author__ = "Mats Larsen"
__copyright__ = "Mats Larsen 2014"
__credits__ = ["Mats Larsen"]
__license__ = "GPLv3"
__maintainer__ = "Mats Larsen"
__email__ = "larsen.mats.87@gmail.com"
__status__ = "Development"
__description__ = "Module is for displaying messeges. The Class should contain all the desired display messages. The idea is that it is typically the same messages there will be displayed. It should be understand as a fast phototyping of a appluication to display common messages."
__file__ = "test_msg.py"
__class__ ="DisplayMsg"
__dependencies__ = ["DisplayMsg"]
#--------------------------------------------------------------------
#Version
#--------------------------------------------------------------------
__version_stage__ = "Pre_alpha"
__version_number__ = "0.1"
__version_date__ = "20140917"
__version_risk__ = "This current version is in Pre-alpha version, which meaning that the program can crash or perform other unrespected behavoiurs."
__version_modification__ = "The development project has just been created."
__version_next_update__ = "Implementation of more messeages."
#--------------------------------------------------------------------
#Hardware
#--------------------------------------------------------------------
"""
This project is not releated to any kind of hardware, like GPIOs.
"""
#-----------------------------------------------------
#Import
#-----------------------------------------------------
import traceback
from msg import DisplayMsg as DM
import numpy as np
#--------------------------------------------------------------------
#CONSTANTS
#--------------------------------------------------------------------
LOG_LEVEL = 2 # Information level
LOG_ALWAYS = 3 # Always log data
#--------------------------------------------------------------------
#METHODS
#-------------------------------------------------------------------
def log(msg, log_level=LOG_LEVEL):
    """
    Print a message, and track, where the log is invoked
    Input:
    -msg: message to be printed, ''
    -log_level: informationlevel, i
    """
    global LOG_LEVEL
    if log_level <= LOG_LEVEL:
        print(str(log_level) + ' : ' + FILE + '.py::' + traceback.extract_stack()[-2][2] + ' : ' + msg)
log_level = 2
file1 = 'common.txt'
#msg = DM(file1)#,name='Test of Display messages', log_level=log_level)

count = 0
field = []
field_data = []
for line in open(file1):
    if line.startswith('#'):
        pass#print('sdsd')
    else:
        count +=1
        #print('sssssssssssssss')
        #print(line)
        field.append(line.split(':')[0].strip())
        field_data.append(line.split(':')[1].strip())
print(field)
print(field_data)
msg = [[0 for x in range(2)] for x in range(count)]
print(msg)

for x in range(0,len(field)):
    print(x)
    print(field[x])
    print(field_data[x])
    msg[x][0] = field[x]
    msg[x][1] = field_data[x]
print(msg)
print(msg[0][0])
print(msg[0][1])


import os
import shutil

with open("/home/webthethird/Ethereum/uschunt-slither/study/data/ground_truth_files.csv", "r") as file:
    path_list = file.readlines()
not_found = []
count = 0
root_path = "/home/webthethird/Ethereum/uschunt-slither/study/data/proxies_with_uschunt_results/ethereum/mainnet/proxies/"
for path in path_list:
    path = path.replace("\n", "").strip()
    if path.endswith("AndImpl.sol"):
        file_path = root_path.replace("/proxies/", "/proxies_and_implementations/") + path.replace("AndImpl.sol", ".sol")
        if not os.path.exists(file_path):
            file_path = root_path + path.replace("AndImpl.sol", ".sol")
    else:
        file_path = root_path + path
    if not os.path.exists(file_path):
        not_found.append(path)
    else:
        count += 1
        output_path = file_path.replace("/proxies_and_implementations/", "/proxies/").replace("proxies_with_uschunt_results/ethereum/mainnet/proxies", "ground_truth").replace("AndImpl.sol", ".sol")
        print(f"Copying {path} to {output_path}")
        try:
            shutil.copytree(file_path, output_path)
        except FileExistsError:
            continue

dirs_not_found = [path.rsplit("/", maxsplit=1)[1].replace("AndImpl.sol", ".sol") for path in not_found]
for root, d_names, f_names in os.walk(root_path):
    for dir_name in d_names:
        if dir_name in dirs_not_found:
            count += 1
            dir_path = os.path.join(root, dir_name)
            if os.path.exists(dir_path.replace("/proxies/", "/proxies_and_implementations/")):
                dir_path = dir_path.replace("/proxies/", "/proxies_and_implementations/")
            output_path = dir_path.replace("/proxies_and_implementations/", "/proxies/").replace(
                "proxies_with_uschunt_results/ethereum/mainnet/proxies", "ground_truth").replace("AndImpl.sol", ".sol")
            dirs_not_found.remove(dir_name)
            try:
                shutil.copytree(dir_path, output_path)
            except FileExistsError:
                continue
print(f"Found {count} files")
print("Not found:")
for path in dirs_not_found:
    print(path)


# The basic outline of this problem is to read the file, look for integers using the re.findall(),
# looking for a regular expression of '[0-9]+' and then converting the extracted strings to integers and summing up the integers.

import re

fname = input("Enter file: ")
if len(fname) < 1 : fname = "regex_sum_197754.txt"
fh = open(fname)

fileLine = fh.read() # read the whole file into a single string
int_string = re.findall('[0-9]+', fileLine)
print(int_string)

# list comprehension to convert list of strings to list of ints
intList = [int(i) for i in int_string]
print(sum(intList))