# HTTP server
from aiohttp import web
import ssl

# Discord
import discord

# Our stuff
import asyncio
import re
import datetime
import sqlite3
import logging
from os import path
from configparser import ConfigParser

LOG_FORMAT = '%(asctime)s | %(message)s'

DB_NAME = 'zmrdiscordnotify.db'

# Intents
# We need members so we can add roles.
INTENTS = discord.Intents.default()
INTENTS.members = True

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

def escape_everything(data):
    return discord.utils.escape_markdown(discord.utils.escape_mentions(data))

def get_valid_tokens():
    tokens = []
    with open(path.join(path.dirname(__file__), '.tokens.txt')) as fp:
        lines = fp.read().splitlines()
        for line in lines:
            if len(line) == 0:
                continue
            # Check for comments
            try:
                comment_index = line.index(';')
                if comment_index == 0:
                    continue
                line = line[:comment_index]
            except ValueError:
                pass
            tokens.append(line.strip())

    return tokens

class Event:
    def __init__(self, id, name, time, description='', warned=False):
        self.id = id
        self.name = name
        self.time = time
        self.description = description
        self.warned = warned

    @staticmethod
    def dateformat():
        return '%Y-%m-%d %H:%M'

    # HACK: We need timezone aware datetime object.
    @staticmethod
    def timezone_aware_time(time):
        delta = datetime.datetime.now() - datetime.datetime.utcnow()
        return time.replace(tzinfo=datetime.timezone(delta))

    @staticmethod
    def create_event(msg_content):
        data = re.findall('"(.+?)"', msg_content)

        if len(data) < 2:
            return None

        cur_time = datetime.datetime.utcnow()
        target_time = datetime.datetime.strptime(data[1], Event.dateformat())

        # We can't make an event in the past!
        if target_time < cur_time:
            return None

        delta = target_time - cur_time

        time = datetime.datetime.now() + delta
        time = Event.timezone_aware_time(time)

        logger.info('Creating event %s...' % data[0])

        return Event(0, data[0], time, '' if len(data) <= 2 else data[2])

    @staticmethod
    def create_event_sql(row):
        # id, name, description, time, warned
        # print(row)

        time = datetime.datetime.strptime(row[3], Event.dateformat())
        time = Event.timezone_aware_time(time)

        return Event(row[0], row[1], time, row[2], row[4])

    def time_to_str(self):
        return self.time.strftime(Event.dateformat())

    def time_to_str_full(self):
        return self.time.strftime(Event.dateformat() + ' (%z)')

    def get_delta_to_now(self):
        return self.time.replace(tzinfo=None) - datetime.datetime.now()

    def should_warn(self):
        if self.warned:
            return False

        timedelta = self.get_delta_to_now()
        # If the days are negative, that means it's in the past.
        # And all logic goes out the window.
        if timedelta.days < 0:
            return False
        minutes = timedelta.days * 1440 + timedelta.seconds / 60
        return minutes < 30 and minutes > 2

    def should_ping(self):
        timedelta = self.get_delta_to_now()
        # If the days are negative, that means it's in the past.
        # And all logic goes out the window.
        if timedelta.days < 0:
            return True
        minutes = timedelta.days * 1440 + timedelta.seconds / 60
        return minutes < 1

    def format_time_todelta(self):
        timedelta = self.get_delta_to_now()
        days = timedelta.days
        hours = timedelta.seconds // 3600
        minutes = timedelta.seconds // 60

        s = ''
        if days > 0:
            s = '%i days ' % days
        if hours > 0:
            s += '%i hours' % hours
        elif minutes > 0:
            s += '%i minutes' % minutes
        else:
            s += '%i seconds' % timedelta.seconds

        return s

    def db_insert(self, conn):
        c = conn.cursor()
        c.execute(
            '''INSERT INTO ntf_events (name,description,time) VALUES
            (?,?,?)''',
            (self.name, self.description, self.time_to_str())
        )
        c.execute('''SELECT LAST_INSERT_ROWID()''')
        self.id = c.fetchone()[0]
        conn.commit()

    def db_markdone(self, conn):
        c = conn.cursor()
        c.execute(
            '''UPDATE ntf_events SET done=1 WHERE id=%i''' % self.id
        )
        conn.commit()

    def db_markwarned(self, conn):
        c = conn.cursor()
        c.execute(
            '''UPDATE ntf_events SET warned=1 WHERE id=%i''' % self.id
        )
        conn.commit()

class RequestData:
    def __init__(self, data, valid_tokens):
        if 'token' not in data:
            raise Exception('No token in JSON object!')

        if not data['token'] in valid_tokens:
            raise Exception('Invalid token.')

        self.hostname = escape_everything(data['hostname'])
        self.link = 'steam://connect/' + escape_everything(data['join_ip'])
        self.num_players = int(data['num_players'])
        self.max_players = int(data['max_players'])
        self.player_name = escape_everything(data['player_name'])

class MyDiscordClient(discord.Client):
    def __init__(self, config):
        super().__init__(intents=INTENTS)

        self.my_channel = None
        self.my_guild = None
        self.my_ping_role = None

        self.events = []

        self.token = config.get('discord', 'token')

        self.ping_role = int(config.get('discord', 'ping_role'))
        self.channel_id = int(config.get('discord', 'channel'))

        self.port = int(config.get('server', 'port'))

        self.cert_path = config.get('server', 'cert')
        self.key_path = config.get('server', 'key')

        self.test_post = False
        if config.get('server', 'test_post'):
            self.test_post = True
            logger.info(
                'Testing POST requests. Notifications are not sent to Discord!'
            )

        self.valid_tokens = get_valid_tokens()

        logger.debug('Valid tokens:')
        for token in self.valid_tokens:
            logger.debug('"' + token + '"')

        self.webapp = web.Application()
        self.webapp.router.add_post('/', self.handle_webrequest)
        if config.get('server', 'test_get'):
            self.webapp.router.add_get('/', self.handle_webrequest_test_get)
            logger.info('Added test GET handler.')

        self.loop.run_until_complete(self.init_webapp())
        self.event_task = self.loop.create_task(self.check_events())

        self.init_sqlite()

    """Init SQL connection, create tables and get events"""
    def init_sqlite(self):
        self.sqlite_connection = sqlite3.connect(
            path.join(path.dirname(__file__), DB_NAME))

        c = self.sqlite_connection.cursor()

        #
        # Init tables
        #
        c.execute(
            '''CREATE TABLE IF NOT EXISTS ntf_events (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            name VARCHAR(128) NOT NULL,
            description VARCHAR(256),
            time DATETIME NOT NULL,
            done INTEGET NOT NULL DEFAULT 0,
            warned INTEGER NOT NULL DEFAULT 0)''')

        # Get events
        c.execute(
            '''SELECT id,name,description,time,warned
            FROM ntf_events WHERE done=0''')

        for row in c.fetchall():
            self.events.append(Event.create_event_sql(row))

        self.sqlite_connection.commit()

    """Init web app"""
    async def init_webapp(self):
        try:
            logger.info('Initializing HTTP server...')

            sslcontext = None
            if self.cert_path or self.key_path:
                sslcontext = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
                sslcontext.load_cert_chain(self.cert_path, self.key_path)
            else:
                logger.info('NOT USING SSL')

            runner = web.AppRunner(self.webapp)
            await runner.setup()
            site = web.TCPSite(runner, port=self.port, ssl_context=sslcontext)
            await site.start()
        except Exception as e:
            logger.error('Error initializing web server: ' + str(e))
        else:
            logger.info('Started HTTP server on port %i.' % self.port)

    """Handles the POST request from game servers."""
    async def handle_webrequest(self, request):
        if not self.is_ready():
            logger.error(
                'Received a POST request while Discord bot is not ready!')
            return

        data = None
        try:
            d = await request.json()
            data = RequestData(d, self.valid_tokens)
        except Exception as e:
            logger.error(
                'Error occurred when parsing json from request: ' + str(e))
            try:
                body = await request.text()
                logger.error('Body: ' + str(body))
            except Exception:
                pass

        if data is None:
            return web.Response(text='Failed!')

        logger.debug('Received valid JSON:')
        logger.debug(str(data))

        if self.test_post:
            logger.info('Testing POST. Not sending a mention.')
            return web.Response(text='Success!')

        try:
            logger.info('Sending mention.')

            embed = discord.Embed(
                title=data.hostname,
                description=data.link,
                color=0x13e82e)
            content = ('%s **%s** wants you to join! (*%i*/*%i*)' %
                       (self.my_ping_role.mention,
                        data.player_name,
                        data.num_players,
                        data.max_players))
            await self.my_channel.send(content=content, embed=embed)
        except Exception as e:
            logger.error('Error sending a mention: ' + str(e))

        return web.Response(text='Success!')

    async def handle_webrequest_test_get(self, request):
        logger.info('Received test GET request!')
        return web.Response(text='Hello!')

    """Task that checks for any events about to happen"""
    async def check_events(self):
        # Wait until we're ready.
        while not self.is_ready():
            await asyncio.sleep(1)

        while not self.is_closed():
            if len(self.events) > 0:
                logger.debug('Checking %i events' % len(self.events))

            for event in self.events:
                if not event.warned and event.should_warn():
                    await self.warn_event(event)
                elif event.should_ping():
                    await self.start_event(event)
                    # We will be removed from the list, so we have to break.
                    break
            await asyncio.sleep(10)

    #
    # Discord.py
    #
    async def on_ready(self):
        print('Logged on as', self.user)

        self.my_channel = self.get_channel(self.channel_id)
        if self.my_channel is None:
            raise Exception('Channel with id %i does not exist!' %
                            self.channel_id)

        self.my_guild = self.my_channel.guild
        if self.my_guild is None:
            raise Exception('Could not find guild for channel!')

        self.my_ping_role = self.my_channel.guild.get_role(self.ping_role)
        if self.my_ping_role is None:
            raise Exception('Role with id %i does not exist!' % self.ping_role)

    async def on_message(self, message):
        if not self.is_ready():
            return
        # Not command?
        if not message.content or message.content[0] != '!':
            return
        # Don't respond to ourselves
        if message.author == self.user:
            return
        # Only either in my channel or DM
        if (message.channel != self.my_channel and
                not isinstance(message.channel, discord.DMChannel)):
            return
        # Not a member of my server
        member = self.my_guild.get_member(message.author.id)
        if member is None:
            return
        #
        # Actual actions.
        #
        if message.content[1:] == 'add':
            await self.add_ping_role(member, message.channel)

        if message.content[1:] == 'remove':
            await self.remove_ping_role(member, message.channel)

        if message.content[1:] == 'events':
            await self.list_events(message.channel)

        if message.content.startswith('addevent', 1):
            await self.add_event(member, message)

        if message.content.startswith('removeevent', 1):
            await self.remove_event(member, message)

        if message.content.startswith('forceevent', 1):
            await self.force_event(member, message)

    #
    # Event stuff
    #
    async def start_event(self, event):
        logger.info('Starting event #%i (%s)' % (event.id, event.name))

        desc = ('%s **%s** is starting!' %
                (self.my_ping_role.mention, event.name))
        embed = discord.Embed(
            title=event.name,
            description=event.description,
            color=0x13e82e
        )
        try:
            await self.my_channel.send(content=desc, embed=embed)
            event.db_markdone(self.sqlite_connection)
            self.events.remove(event)
        except Exception as e:
            logger.error('Error starting an event: ' + str(e))

    async def warn_event(self, event):
        logger.info('Warning event #%i (%s)' % (event.id, event.name))

        desc = ('%s will start in %s!' %
                (event.name, event.format_time_todelta()))

        try:
            await self.my_channel.send(desc)
            event.warned = True
            event.db_markwarned()
        except Exception as e:
            logger.error('Error doing an event warning: ' + str(e))

    def init_event(self, event):
        logger.info('Adding event %s' % event.name)

        event.db_insert(self.sqlite_connection)
        self.events.append(event)

    async def quick_channel_msg(self, msg, channel=None):
        if channel is None:
            channel = self.my_channel
        try:
            await channel.send(msg)
        except Exception as e:
            logger.error('Error sending a channel message: ' + str(e))

    #
    # Command actions
    #
    #
    # Add ping role
    #
    async def add_ping_role(self, member, from_channel):
        if self.my_ping_role in member.roles:
            await self.quick_channel_msg(
                "%s You already have role %s!" %
                (member.mention,
                    self.my_ping_role.name),
                from_channel)
            return
        try:
            logger.info('Adding ping role to user %s!' % member.display_name)

            await member.add_roles(self.my_ping_role, reason='User requested.')
            await from_channel.send('%s Added role %s.' %
                                    (member.mention, self.my_ping_role.name))
        except Exception as e:
            logger.error('Error adding a ping role: ' % str(e))

    #
    # Remove ping role
    #
    async def remove_ping_role(self, member, from_channel):
        if self.my_ping_role not in member.roles:
            await self.quick_channel_msg(
                "%s You don't have role %s!" %
                (member.mention,
                    self.my_ping_role.name),
                from_channel)
            return
        try:
            logger.info('Removing role from user %s!' % member.display_name)

            await member.remove_roles(
                self.my_ping_role,
                reason='User requested.')
            await from_channel.send('%s Removed role %s.' %
                                    (member.mention,
                                        self.my_ping_role.name))
        except Exception as e:
            logger.error('Error removing a ping role: ' + str(e))

    #
    # List events
    #
    async def list_events(self, from_channel):
        if len(self.events) <= 0:
            await self.quick_channel_msg(
                "No events found! :(",
                from_channel)
            return

        embed = discord.Embed(
            title='Events',
            description='',
            color=0x13e82e)

        for event in self.events:
            embed.add_field(
                name='%s | %s' % (event.name, event.time_to_str_full()),
                value=event.format_time_todelta(),
                inline=False)

        try:
            await from_channel.send(
                content='%i event(s)' % len(self.events),
                embed=embed)
        except Exception as e:
            logger.error('Error listing events: ' + str(e))

    #
    # Add event
    #
    async def add_event(self, member, message):
        # Check if they can
        if not member.permissions_in(message.channel).manage_channels:
            return

        # Create the event
        event = Event.create_event(message.content)
        if not event:
            await self.quick_channel_msg(
                '%s Invalid syntax!' % member.mention,
                message.channel)
            return

        try:
            self.init_event(event)
            await message.channel.send(
                '%s Added event **%s** (#%i)\n%s\n**Event happens in %s**.' %
                (member.mention,
                    event.name,
                    event.id,
                    event.time_to_str_full(),
                    event.format_time_todelta())
            )
        except Exception as e:
            logger.error('Error adding an event: ' + str(e))

    #
    # Remove event
    #
    async def remove_event(self, member, message):
        # Check if they can
        if not member.permissions_in(message.channel).manage_channels:
            return

        # Find the event they want removed.
        event = None
        id = -1
        try:
            id = int(message.content.split()[1])
        except Exception as e:
            pass
        else:
            for e in self.events:
                if e.id == id:
                    event = e
                    break

        if not event:
            await self.quick_channel_msg(
                '%s Could not find event #%i!' %
                (member.mention, id),
                message.channel)
            return

        try:
            logger.info('Removing event %s!' % event.name)

            event.db_markdone(self.sqlite_connection)
            self.events.remove(event)

            await message.channel.send(
                '%s Removed event **%s** (%s).' %
                (member.mention,
                    event.name,
                    event.time_to_str_full()))
        except Exception as e:
            logger.error('Error removing an event: ' + str(e))

    #
    # Force event to start
    #
    async def force_event(self, member, message):
        # Check if they can
        if not member.permissions_in(message.channel).manage_channels:
            return

        # Find the event they want.
        event = None
        id = -1
        try:
            id = int(message.content.split()[1])
        except Exception as e:
            pass
        else:
            for e in self.events:
                if e.id == id:
                    event = e
                    break

        if not event:
            await self.quick_channel_msg(
                '%s Could not find event #%i!' %
                (member.mention, id),
                message.channel)
            return

        await self.start_event(event)

if __name__ == '__main__':
    # Read our config
    config = ConfigParser()
    with open(path.join(path.dirname(__file__), '.config.ini')) as fp:
        config.read_file(fp)

    # Init logger
    log_level_str = config.get(
        'server', 'logging', fallback='').upper()
    log_level = getattr(logging, log_level_str, logging.WARNING)
    ch = logging.StreamHandler()
    ch.setLevel(log_level)
    formatter = logging.Formatter(LOG_FORMAT)
    ch.setFormatter(formatter)
    logger.addHandler(ch)

    client = MyDiscordClient(config)

    try:
        client.run(config.get('discord', 'token'))
    except discord.LoginFailure:
        logger.error('Failed to log in! Make sure your token is correct!')
    except Exception as e:
        logger.error('Discord bot ended unexpectedly: ' + str(e))


#def areaOfSquare(a):
    #return a*a
#print(areaOfSquare(25))
class Car(object):
    def __init__(self, speed, steering, model, color, company, gas, wheelType):
        self.speed = speed
        self.steering = steering
        self.model = model
        self.color = color
        self.company = company
        self.gas = gas
        self.wheelType = wheelType

    def start(self):
        print("started")

    def stop(self):
        print("Car Stop!")

    def accelerate(self):
        print("Car, go faster")

Tesla = Car("60", "3", "Model X", "blue", "Tesla", "Electricity", "Michilen")
print(Tesla.gas)
Tesla.start()
Tesla.accelerate()
Tesla.stop()


from flask import Flask, render_template, request, redirect, url_for, send_from_directory
import ipaddress
import utilities.cloudUtility

app = Flask(__name__)

@app.route("/", methods=['GET'])
def route():
    return render_template('ccip.html', msg='', ipaddr='')

@app.route("/health", methods=['GET'])
def health():
    return render_template('health.html')

@app.route("/ccip", methods=['GET','POST'])
def ccip():
    if request.method == 'POST':
        if request.method == 'POST':
            if request.form['ipaddr'] is None :
                return render_template('ccip.html', msg='', ipaddr='')
            else:
                ipaddr = request.form['ipaddr']

                try:
                    ipaddress.ip_address(ipaddr)
                    print(ipaddr)
                    msg = utilities.cloudUtility.CheckIP(ipaddr)

                except Exception as e:
                    msg = str(ipaddr) + "\n"
                    msg += str(e)

                return render_template('ccip.html', msg=msg, ipaddr=ipaddr)

    else:
        return render_template('ccip.html', msg='', ipaddr='')

@app.route("/cyip", methods=['GET'])
def cyip():
    headers = request.headers
    environ = request.environ
    remoteAddr = request.remote_addr
    accessRoute = request.access_route
    environ_list = environ.items()

    return render_template('cyip.html', remoteAddr=remoteAddr, accessRoute=accessRoute, headers=headers,environ=environ_list)

@app.route("/.well-known/pki-validation/godaddy.html", methods=['GET'])
def validateCert():
    return render_template('godaddy.html')

if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0', port=80)


import pandas as pd
import sys
import math
import datetime
import re
import pkg_resources
from CdcGenerator.fields import fields

class CdCGenerator():
    def __init__(self, debug, stats=False, answers=True):
        self.debug = debug
        self.stats = stats
        self.answers = answers

        self.fields_alias = fields["2020-2021-1"]
        self.PiChart_header = """
\\\\captionsetup[subfigure]{labelformat=empty, font={bf}}
\\\\captionsetup[figure]{labelformat=empty, font={bf}}
\\\\begin{figure}[H]
\\\\begin{center}"""

        self.PiChart_footer = """
\\\\end{center}
\\\\end{figure}
"""

    def makePiChart(self, alias, percentage, echantillon):
        return """
\\\\begin{subfigure}{.4\\\\textwidth}
\\\\centering
\\\\caption{"""+alias+""" ("""+str(echantillon)+""")}
\\\\begin{tikzpicture}
\\\\pie[explode=0.1, radius=2]
 {"""+str(int(percentage))+"""/Oui,"""+str(100-int(percentage))+"""/Non}
\\\\end{tikzpicture}
\\\\end{subfigure}"""

    def makePiChart2(self, data, alias):
        total = 0
        text = ""
        for value,count in data.items():
            total += count
        reste = 0.0
        full = 100.0
        text += self.PiChart_header
        text +="""
\\\\centering
\\\\caption{"""+alias+"""}
\\\\begin{tikzpicture}
\\\\pie[explode=0.3, radius=4]{"""
        for value,count in data.items():
            pc = count/total*100+1/total
            reste += pc-int(pc)
            if reste > 1:
                pc += 1
                reste -= 1
            text +=  str(int(pc))+"""/"""+value+','
        # Removing trailing comma
        text = text[:len(text)-1]
        text += """}
\\\\end{tikzpicture}"""
        text += self.PiChart_footer

        return text

    def makeCharts(self, YesNoFields_stats, BarChart_stats, EntryCount):
        text = "\\\\section{Quelques statistiques}"
        text += self.PiChart_header
        print(YesNoFields_stats.items())
        for key, (alias, n) in YesNoFields_stats.items():
            percentage = n/EntryCount*100.0
            text += self.makePiChart(alias, percentage, n)
        text += self.PiChart_footer
        text += "\n\\\\pagebreak\n"
        for key,values in BarChart_stats.items():
            text += self.makePiChart2(values, key)
        return text

    def extract_values(self, row, YesNoFields_stats, BarChart_stats):
        data = """\\\\rule{8cm}{0.4pt}
\\\\setlength{\\\\parindent}{0cm}\n\n"""
        for field in self.fields_alias:
            alias = self.fields_alias[field]
            if not alias:
                continue
            tab = alias.split("YES-NO-")
            if len(tab) == 2:
                alias = tab[1]
                value = row[field]
                if not field in YesNoFields_stats:
                    YesNoFields_stats[field] = (alias, 1)
                elif value == "Oui":
                    alias,n = YesNoFields_stats[field]
                    n+=1
                    YesNoFields_stats[field] = (alias,n)
                continue
            tab = alias.split("CHART-")
            if len(tab) == 2:
                value = row[field]
                if isinstance(value,float) and math.isnan(value):
                    continue
                key = tab[1].split('-')[0]
                values = value.split(", ")
                if not key in BarChart_stats:
                    BarChart_stats[key] = {}
                for v in values:
                    if not v in BarChart_stats[key]:
                        BarChart_stats[key][v] = 0
                    BarChart_stats[key][v] += 1
                continue
            value = row[field]
            if isinstance(value, float) and math.isnan(value):
                continue
            data += "\\\\"+"textbf{"+alias+"} "+str(value)+" \\\\\\\\"+"\n"
        data += '\n'

        return (data, YesNoFields_stats, BarChart_stats)

    def clean_names_case(self, data):
        columns = ['PeuxtuNousDonnerTonNom_First', 'PeuxtuNousDonnerTonNom_Last']
        data[columns] = data[columns].apply(
                lambda n:
                    n.apply(
                        lambda t: t[0].upper()+t[1:].lower())
                   , axis='columns')
        return data

    def generateLatexPdf(self, input_entry_excel):

        YesNoFields_stats = {}
        BarChart_stats = {}
        EntryCount = 0

        data = pd.read_excel(input_entry_excel)
        data = self.clean_names_case(data)
        data = data.sort_values(by='PeuxtuNousDonnerTonNom_Last')
        EntryCount = len(data)
        text = ""
        final_text = ""
        for row in data.iterrows():
            extracted_text,YesNoFields_stats,BarChart_stats = self.extract_values(row[1], YesNoFields_stats, BarChart_stats)
            text += extracted_text

        if self.stats:
            final_text = self.makeCharts(YesNoFields_stats, BarChart_stats, EntryCount)+"""

\\\\pagebreak

"""
        if self.answers:
            final_text += """\\\\section{Les réponses ("""+str(EntryCount)+""")}\n"""+text

        TEMPLATE_PATH = pkg_resources.resource_filename('CdcGenerator', 'data/template_latex.tex')
        if self.debug:
            print(f"TEMPLATE_PATH : `{TEMPLATE_PATH}`")

        with open(TEMPLATE_PATH) as template:
            lines = ''.join(template.readlines())

        print(re.sub('<data>', final_text, lines))


from selenium import webdriver
from bs4 import BeautifulSoup
import requests
import time

def get_total(url):

    web_data = requests.get(url)
    time.sleep(2)
    soup = BeautifulSoup(web_data.text,'lxml')
    hrefs = soup.select('a.t')

    print(type(hrefs))
    href_list = []

    for href in hrefs:
        link = href.get('href')
        if 'pingbandiannao' in link:
           href_list.append(link)
    for href in href_list:
        spider(href)

def spider(href):
    driver = webdriver.Chrome()
    driver.get(href)
    driver.set_page_load_timeout(5)

    html = driver.page_source

    soup = BeautifulSoup(html, 'lxml')

    title_list = soup.select('h1.detail-title__name')
    title = title_list[0].text.strip()

    price_list = soup.select('span.infocard__container__item__main__text--price')
    price = price_list[0].text.strip()

    area_list = soup.select('div.infocard__container__item__main > a')
    area = area_list[0].text.strip()

    typename_list= soup.select('div.nav > a')
    typename= typename_list[-1].text

    view_list = soup.select('span.detail-title__info__totalcount')
    view = view_list[0].text.strip()
    data = {
        'title': title,
        'price': price,
        'area': area,
        'typename':typename,
        'view': view

    }
    print(data)

if __name__ == '__main__':
    urls = ['https://bj.58.com/pbdn/0/pn{}/'.format(str(i)) for i in range(1, 4, 1)]
    for url in urls:
        get_total(url)


# imports
from python.week_0 import christmastree
from python.week_1 import lists
from python.week_2 import imp_lcm, oop_lcm, oop_fact, palindrome

# Two styles are supported to execute abstracted logic
# 1. file names will be run by exec(open("filename.py").read())
# 2. function references will be executed directly file.function()
main_menu = []

# Submenu list of [Prompt, Action]
# Works similarly to main_menu
# fun functions submenu with crossover additions
sub_menu1 = [
    ["Swap", "python/week_0/swap.py"],
    ["Keypad", "python/week_0/keypad.py"],
    ["Christmas Tree", "python/week_0/christmas.py"],
    ["Christmas Tree With Extra Functionalities", christmastree.options],
    ["Loops", lists.tester1],
    ["Palindrome Tester", palindrome.test],
    ["Palindrome Input", palindrome.run],

]
# animations submenu
sub_menu2 = [
    ["Cat", "python/week_0/cat.py"],
    ["Ship", "python/week_0/ship.py"],

]
# math submenu
sub_menu3 = [
    ["100 Fibonacci", "python/week_1/fib.py"],
    ["Factorial", lists.tester2],
    ["OOP Factorial", oop_fact.run_factorial],
    ["OOP LCM", oop_lcm.lcm_run],
    ["Imperative LCM", imp_lcm.lcm],

]
# Menu banner is typically defined by menu owner
border = "\033[32m~\033[36m" * 25
banner = f"\033[37m\n{border}\nPlease Pick An Option!\n{border}\033[37m"
# def menu
# using main_menu list:
# 1. main menu and submenu reference are created [Prompts, Actions]
# 2. menu_list is sent as parameter to menuy.menu function that has logic for menu control
def menu():
    title = "\033[36mFunction Menu\033[36m" + banner
    menu_list = main_menu.copy()
    menu_list.append(["Fun Functions", submenu1])
    menu_list.append(["Animations", submenu2])
    menu_list.append(["Math", submenu3])
    buildMenu(title, menu_list)

# def submenu
# using sub menu list above:
# sub_menu works similarly to menu()
def submenu1():
    title = "Function Submenu" + banner
    buildMenu(title, sub_menu1)

def submenu2():
    title = "Function Submenu" + banner
    buildMenu(title, sub_menu2)

def submenu3():
    title = "Function Submenu" + banner
    buildMenu(title, sub_menu3)

def buildMenu(banner, options):
    # header for menu
    print(banner)
    # build a dictionary from options
    prompts = {0: ["Exit", None]}
    for op in options:
        index = len(prompts)
        prompts[index] = op
    # print menu or dictionary
    for key, value in prompts.items():
        print(key, '->', value[0])
    # get user choice
    choice = input("\033[36mInput:\033[36m")
    # validate choice and run
    # execute selection
    # convert to number
    try:
        choice = int(choice)
        if choice == 0:
            # stop
            print("You have exited the program!")
            return
        try:
            # try as function
            action = prompts.get(choice)[1]
            action()
        except TypeError:
            try:  # try as playground style
                exec(open(action).read())
            except FileNotFoundError:
                print(f"File not found!: {action}")
            # end function try
        # end prompts try
    except ValueError:
        # not a number error
        print(f"Not a number: {choice}")
    except UnboundLocalError:
        # traps all other errors
        print(f"Invalid choice: {choice}")
    # end validation try
    buildMenu(banner, options)  # recursion, start menu over again
if __name__ == "__main__":
    menu()


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

from django.contrib import admin
from app.models import Measurement,FabricCollection,Ratings,Designer,Styles,Country,Accessories,Fabrics,Profile,Category,Transactions,Order

class DesignerAdmin(admin.ModelAdmin):
    model = Designer
    list_display = ('name', 'user','average_rating','dp','cover_photo','city','country','address')

class RatingsAdmin(admin.ModelAdmin):
    model = Ratings
    list_display = ('comment', 'rating','designer','user')

class StylesAdmin(admin.ModelAdmin):
    model = Styles
    list_display = ('code','name','designer','custom_price','gender','pub_date')

class CountryAdmin(admin.ModelAdmin):
    model = Styles
    list_display = ('name',)

class CategoryAdmin(admin.ModelAdmin):
    model = Category
    list_display=('name',)

class AccessoriesAdmin(admin.ModelAdmin):
    model = Accessories
    list_display = ('name','price','designer','gender')

class FabricsAdmin(admin.ModelAdmin):
    model=Fabrics
    list_display = ('name','price','designer')

class ProfileAdmin(admin.ModelAdmin):
    model=Profile
    list_display = ('user','city','country','address')

class TransactionsAdmin(admin.ModelAdmin):
    model=Transactions
    list_display = ('user','reference','amount','status')

class FabricCollectionAdmin(admin.ModelAdmin):
    model=FabricCollection
    list_display = ('name','designer',)

class OrdersAdmin(admin.ModelAdmin):
    model=Transactions
    list_display = ('user','designer','style','fabric','transaction','reference')

class MeasurementAdmin(admin.ModelAdmin):
    model=Measurement

admin.site.register(Designer,DesignerAdmin)
admin.site.register(Ratings,RatingsAdmin)
admin.site.register(Styles,StylesAdmin)
admin.site.register(Country,CountryAdmin)
admin.site.register(Accessories,AccessoriesAdmin)
admin.site.register(Fabrics,FabricsAdmin)
admin.site.register(Profile,ProfileAdmin)
admin.site.register(Category,CategoryAdmin)
admin.site.register(Transactions,TransactionsAdmin)
admin.site.register(Order,OrdersAdmin)
admin.site.register(Measurement,MeasurementAdmin)
admin.site.register(FabricCollection,FabricCollectionAdmin)


"""
This file outputs random field configurations 'rbl_x.txt' and 'rbl_z.txt' as well as copies 'rbl_x0.txt' and 'rbl_z0.txt' meeting the restriction of solubility, i.e. on every spin only a field in one direction can be present.
"""

import numpy as np
import random as rd

L=4
num_ex0=1
labels=np.arange(num_ex0)
run=np.zeros(num_ex0)
beta=np.random.rand(num_ex0)*0.4+0.1
logits=np.random.rand(num_ex0,2*L*L)+1.0

#for i in xrange(num_ex0):
#   logits[i,:]=softmax(logits[i,:])

A=np.random.randint(0,2,size=(num_ex0,2*L*L))*2-1

logits=logits*A

f1=open('rbl_x.txt', "w")
f2=open('rbl_z.txt', "w")
f3=open('rbl_x0.txt', "w")
f4=open('rbl_z0.txt', "w")
for j in range(2*L*L):
   for i in range(num_ex0):
      r=np.random.rand()
      #print r
      if r>0.5:
        f1.write(str(logits[i,j])+" ")
        f2.write(str(0.0)+" ")
        f3.write(str(logits[i,j])+" ")
        f4.write(str(0.0)+" ")
      else:
        f1.write(str(0.0)+" ")
        f2.write(str(logits[i,j])+" ")
        f3.write(str(0.0)+" ")
        f4.write(str(logits[i,j])+" ")
   f1.write("\n")
   f2.write("\n")
   f3.write("\n")
   f4.write("\n")
f1.close()
f2.close()
f3.close()
f4.close()


from flask import Flask
from flask import request
import os
import applescript
import json

app = Flask(__name__)

#Define a song to play for each team member, this could be improved.

teammember_script = """tell application "iTunes"
	play track "Dont Stop Believing" of playlist "Done"
end tell
"""

teammember2_script = """tell application "iTunes"
	play track "Yelling Into The Wind" of playlist "Done"
end tell
"""

teammember3_script = """tell application "iTunes"
	play track "Good Times" of playlist "Done"
end tell
"""

# All script is to handle any team members who don't have an individual song defined
all_script = """tell application "iTunes"
	play track "NewTown" of playlist "Done"
end tell
"""

# All script is to handle any team members who don't have an individual song defined
bug_script = """tell application "iTunes"
	play track "NewTown" of playlist "Done"
end tell
"""

@app.route('/play/', methods = ['GET', 'POST', 'PATCH'])
@app.route('/play', methods = ['GET', 'POST', 'PATCH'])
def play():
	if request.method == 'POST':

		jsondata = request.data

		data = json.loads(jsondata)

		#Load the new status of the ticket from the JSON payload
		new_status = data['changelog']['items'][0]['toString']

		if (new_status == 'Done'):

				# if user_id is passed in as URL param
				if (request.values.get('user_id', None)):
					u = request.values.get('user_id', None)
				#checks to see which team member it is. This could be improved.
				if (u == 'user.name'):
					try:
						applescript.launch_script(teammember_script)
						return "playing first teammember's song"
					except applescript.AppleScriptError:
						return "Something has gone wrong with Applescript"

				elif (u == 'user.name'):
					try:
						applescript.launch_script(teammember2_script)
						return "playing teammember2's song"
					except applescript.AppleScriptError:
						return "Something has gone wrong with Applescript"

				elif (u == 'user.name'):
					try:
						applescript.launch_script(teammember3_script)
						return "playing teammember3's song"
					except applescript.AppleScriptError:
						return "Something has gone wrong with Applescript"

				elif u in other_team_members:
					try:
						applescript.launch_script(all_script)
						return "playing the default song"
					except applescript.AppleScriptError:
						return "Something has gone wrong with Applescript"

				#check to see if user_id is empty
				elif u is None:
					return "there was no user specified when this ticket was updated"
		else:
			return "Status not Done, do nothing"

#Create a webhook that only looks at bug type tickets in your project. Then point the webhook to this route.
@app.route('/bug/', methods = ['GET', 'POST', 'PATCH'])
@app.route('/bug', methods = ['GET', 'POST', 'PATCH'])
def bug():
	try:
		applescript.launch_script(bug_script)
		return "playing the bug creation song"
	except applescript.AppleScriptError:
		return "Something has gone wrong with Applescript"

if __name__ == '__main__':
	app.debug = True
	app.run()


'''utils.py - misc useful functions'''

import requests
import hashlib
import codecs
import csv
import ckanapi

from datetime import datetime
from airflow.models import Variable

def stream_download_to_md5(url):
    '''streams input url into returned md5 hash string'''
    r = requests.get(url, stream=True)

    sig = hashlib.md5()

    # loop over lines in url response
    for line in r.iter_content():
        sig.update(line)

    return sig.hexdigest()

def download_to_md5(url):
    '''downloads input url into returned md5 hash string'''
    r = requests.get(url)

    sig = hashlib.md5(r.text.encode())

    return sig.hexdigest()

def file_to_md5(filepath):
    '''streams input string into returned md5 hash string'''
    sig = hashlib.md5()
    with open(filepath, "rb") as f:
        # loop over bytes of file until there are no bytes left
        # reads 8192 bytes at a time - md5 has 128-byte digest blocks
        # and 8192 is 128*64
        # stackoverflow.com/questions/1131220/get-the-md5-hash-of-big-files-in-python
        while True:
            buf = f.read(8192)
            if not buf:
                break
            sig.update(buf)

    return sig.hexdigest()

def csv_to_generator(filepath, fieldnames):
    '''input csv filepath and fieldnames, output stream of rows as json'''
    with codecs.open(filepath, "rbU", "latin1") as f:
        reader = csv.DictReader(f, fieldnames=fieldnames)
        next(reader)
        for row in reader:
            yield(row)

def clean_string(input):
    '''converts data to fit in CKAN datastore text column'''
    if input is None:
        return ""
    if not isinstance(input, str):
        return str(input)
    else:
        return str(input).strip()

def clean_int(input):
    '''converts data to fit in CKAN datastore int column'''
    if input:
        return int(input)
    else:
        return None

def clean_float(input):
    '''converts data to fit in CKAN datastore float column'''
    if input:
        return float(input)
    else:
        return None

def clean_date_format(input, input_format=None):
    '''converts data to fit in CKAN datastore date or timestamp column'''
    # loops through the list of formats and tries to return an input
    # string into a datetime of one of those formats

    if input is None:
        return

    if len(input) == 0:
        return

    assert isinstance(
        input, str
    ), "Utils.clean_date_format() accepts strings - it got {}".format(
        type(input)
    )

    format_dict = {
        "%Y-%m-%dT%H:%M:%S.%f": "%Y-%m-%dT%H:%M:%S",
        "%Y-%m-%d %H:%M:%S.%f": "%Y-%m-%dT%H:%M:%S",
        "%Y-%m-%dT%H:%M:%S": "%Y-%m-%dT%H:%M:%S",
        "%Y-%m-%d %H:%M:%S": "%Y-%m-%dT%H:%M:%S",
        "%Y-%m-%d": "%Y-%m-%d",
        "%d-%b-%Y": "%Y-%m-%d",
        "%d-%b-%y": "%Y-%m-%d",
        "%b-%d-%Y": "%Y-%m-%d",
        "%m-%d-%y": "%Y-%m-%d",
        "%m-%d-%Y": "%Y-%m-%d",
        "%d-%m-%y": "%Y-%m-%d",
        "%d-%m-%Y": "%Y-%m-%d",
        "%Y%m%d%H%M%S": "%Y-%m-%dT%H:%M:%S",
        "%d%m%Y": "%Y-%m-%d",
        "%d%b%Y": "%Y-%m-%d",
        "%Y-%b-%d": "%Y-%m-%d",
        "%b %d, %Y": "%Y-%m-%d",
    }

    if input_format:
        if input_format == "epoch":
            # assumes input is in milliseconds

            datetime_object = datetime.fromtimestamp(int(input)/1e3)
            try:
                return datetime_object.strftime("%Y-%m-%dT%H:%M:%S")
            except ValueError:
                return datetime_object.strftime("%Y-%m-%d")

        else:
            input = input.replace("/", "-")
            input_format = input_format.replace("/", "-")
            datetime_object = datetime.strptime(input, input_format)
            output = datetime_object.strftime(format_dict[input_format])

        return output

    else:
        for format in format_dict.keys():
            try:
                input = input.replace("/", "-")
                datetime_object = datetime.strptime(input, format)
                output = datetime_object.strftime(format_dict[format])
                return output
            except ValueError:
                pass

def connect_to_ckan():
    '''Returns RemoteCKAN object for appropriate CKAN instance'''

    # init hardcoded vars
    ACTIVE_ENV = Variable.get("active_env")
    CKAN_CREDS = Variable.get("ckan_credentials_secret", deserialize_json=True)
    CKAN = CKAN_CREDS[ACTIVE_ENV]["address"]#
    CKAN_APIKEY = CKAN_CREDS[ACTIVE_ENV]["apikey"]#

    return ckanapi.RemoteCKAN(apikey=CKAN_APIKEY, address=CKAN)


import ISAC
isac=ISAC.isac

ISAC_FUNCS={"IRTX":"HEX (32 bits/4 bytes max)"}

import web
import json

urls = (
  '/', 'index',
  '/IRTX/(.*)','IRTX'
)

class index:
    def GET(self):
        web.header('Content-Type', 'application/json')
        return json.dumps({"FUNCS":ISAC_FUNCS})

class IRTX:
    def GET(self,ircode):
        web.header('Content-Type', 'application/json')
        ircode = int(ircode,16)
        reply=isac.IRTX(ircode)
        return json.dumps(reply)

if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()


from django.conf.urls import url
from django.contrib.auth import views as djviews
from . import views as uviews

# app_name = 'user'

urlpatterns = [
    url(r'^$', uviews.index, name='index'),
    url(r'^accounts/login/$', uviews.login, name='login'),
    url(r'^accounts/logout/$', uviews.logout, {'next_page': '/'}, name='logout'),
    url(r'^user/list/$', uviews.user_list, name='user_list'),
    url(r'^user/add/$', uviews.user_manage, name='user_add'),
    url(r'^user/manage/(?P<action>[\w-]+)/(?P<aid>\d+)/$', uviews.user_manage, name='user_manage'),
    url(r'^log_audit/$', uviews.log_audit, name='log_audit'),
]


"""Add check ondelete='CASCADE' to ShortUrl.owner_id FK

Revision ID: 58b794396519
Revises: d6c155422e16
Create Date: 2021-04-07 15:38:37.100772

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '58b794396519'
down_revision = 'd6c155422e16'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint('shorturl_owner_id_fkey', 'shorturl', type_='foreignkey')
    op.create_foreign_key(None, 'shorturl', 'user', ['owner_id'], ['id'], ondelete='CASCADE')
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'shorturl', type_='foreignkey')
    op.create_foreign_key('shorturl_owner_id_fkey', 'shorturl', 'user', ['owner_id'], ['id'])
    # ### end Alembic commands ###


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @File  : test_task.py
# @Author: 蒋光谱
# @Date  : 2019/3/22
# @Desc  :
import asyncio
import threading
import time
import os
import sched
from concurrent.futures.thread import ThreadPoolExecutor
from datetime import datetime

import requests

schedule = sched.scheduler(time.time, time.sleep)

class ServiceRun():
    counter = 0

    @classmethod
    def perform_command(cls, inc):
        try:
            cls.counter += 1

            print('任务%s开始时间%s' % (cls.counter, datetime.now()))
            with ThreadPoolExecutor(100) as executor:
                for i in range(3):
                    executor.submit(cls.job1)
            print('任务%s结束时间%s' % (cls.counter, datetime.now()))

            schedule.enter(inc, 0, cls.perform_command, argument=(inc,))
        except Exception as e:
            print(e)

    @staticmethod
    def job1():
        print(threading.current_thread().getName())
        try:
            requests.get('http://www.google.com')
        except Exception as e:
            print(e)

if __name__ == '__main__':
    inc = 1
    # enter用来安排某事件的发生时间，从现在起第n秒开始启动
    schedule.enter(inc, 0, ServiceRun.perform_command, (inc,))
    # schedule.enter(inc, 0, ServiceRun.perform_command, (inc,))

    # 持续运行，直到计划时间队列变成空为止
    schedule.run()


from struct import (unpack_from, calcsize, error)

def closed(data: bytes, offset: int = 0):
    FORMAT = '<HB'
    reason, connection, = unpack_from(FORMAT, data, offset=offset)
    offset += calcsize(FORMAT)

    payload = {
        'reason': reason,
        'connection': connection,
    }

    return payload, offset

def opened(data: bytes, offset: int = 0):
    ADDRESS_SIZE_BYTES = 6
    address = data[offset:offset + ADDRESS_SIZE_BYTES]
    offset += ADDRESS_SIZE_BYTES

    if len(address) < ADDRESS_SIZE_BYTES:
        raise error

    FORMAT = '<BBBBB'
    address_type, master, connection, bonding, advertiser = unpack_from(
        FORMAT, data, offset=offset)
    offset += calcsize(FORMAT)

    payload = {
        'address': address,
        'address_type': address_type,
        'master': master,
        'connection': connection,
        'bonding': bonding,
        'advertiser': advertiser,
    }

    return payload, offset

def parameters(data: bytes, offset: int = 0):
    FORMAT = '<BHHHBH'
    connection, interval, latency, timeout, security_mode, txsize = unpack_from(
        FORMAT, data, offset=offset)
    offset += calcsize(FORMAT)

    payload = {
        'connection': connection,
        'interval': interval,
        'latency': latency,
        'timeout': timeout,
        'security_mode': security_mode,
        'txsize': txsize,
    }

    return payload, offset

def phy_status(data: bytes, offset: int = 0):
    FORMAT = '<BB'
    connection, phy = unpack_from(FORMAT, data, offset=offset)
    offset += calcsize(FORMAT)

    payload = {
        'connection': connection,
        'phy': phy,
    }

    return payload, offset

def rssi(data: bytes, offset: int = 0):
    FORMAT = '<BBb'
    connection, status, rssi = unpack_from(FORMAT, data, offset=offset)
    offset += calcsize(FORMAT)

    payload = {
        'connection': connection,
        'status': status,
        'rssi': rssi,
    }

    return payload, offset


from datetime import timedelta
from http import HTTPStatus

from django.core import mail
from django.test import TestCase
from django.urls import reverse
from django.utils.timezone import now

from users.forms import EmailVerification
from users.models import User

if __name__ == '__main__':
    import unittest
    unittest.main()

class UserRegistrationViewTestCase(TestCase):

    def setUp(self):
        self.path = reverse('users:registration')
        self.data = {
            'first_name': 'Daniel',
            'last_name': 'Kotelnikov',
            'username': 'Daniel',
            'email': 'daniel@yandex.ru',
            'password1': '58523',
            'password2': '58523'
        }

    def test_user_registration_get(self):
        response = self.client.get(self.path)

        self.assertEqual(response.status_code, HTTPStatus.OK)
        self.assertEqual(response.context_data['title'], 'Store - Регистрация')
        self.assertTemplateUsed(response, 'users/registration.html')

    def test_user_registration_post(self):
        first_name = self.data['first_name']
        self.assertFalse(User.objects.filter(first_name=first_name).exists())
        response = self.client.post(self.path, self.data)

        self.assertEqual(response.status_code, HTTPStatus.FOUND)
        self.assertRedirects(response, reverse('users:login'))
        self.assertTrue(User.objects.filter(first_name=first_name).exists())

        email_verification = EmailVerification.objects.filter(first_name=first_name)
        self.assertEqual(email_verification.exists())
        self.assertEqual(
            email_verification.first().expiration.date(),
            (now() + timedelta(hours=48)).date()
        )

    def test_user_registration_error(self):
        User.objects.create(first_name=self.data['first_name'])
        response = self.client.post(self.path, self.data)

        self.assertEqual(response.status_code, HTTPStatus.OK)
        self.assertContains(response, 'User has been created', html=True)

class EmailTest(TestCase):
    def test_send_email(self):
        # Send message.
        mail.send_mail(
            'Subject here',
            'Here is the message.',
            'storeserverlogin@yandex.ru',
            ['daniel.kotelnickov@yandex.ru']
        )

        # Test that one message has been sent.
        self.assertEqual(len(mail.outbox), 1)

        # Verify that the subject of the first message is correct.
        self.assertEqual(mail.outbox[0].subject, 'Subject here')


#!/user/bin/python
# _*_ coding:utf-8 _*_
import hashlib

from settings import TOKEN

__author__ = "super.gyk"

# 验证签名 第一步创建列表 第二步进行排序 第三步拼接成字符串 第四步 验签
def validate_wx_public(form, token=TOKEN):
    timestamp = form.timestamp.data
    nonce = form.nonce.data
    echostr = form.echostr.data
    signature = form.signature.data

    array = [str(timestamp), nonce, token]
    array.sort()
    arr_to_str = ''.join(array)
    result = hashlib.sha1(arr_to_str.encode('utf8')).hexdigest()

    return result == signature, echostr


# import colorgram
#
#
# colors = colorgram.extract('hirst.jpg', 100)
# color_palette = []
# for color in range(len(colors)):
#     r = colors[color].rgb.r
#     g = colors[color].rgb.g
#     b = colors[color].rgb.b
#     new_color = (r, g, b)
#     color_palette.append(new_color)
# print(color_palette)

import random
import turtle as t

turtle = t.Turtle()
turtle.shape("circle")
turtle.speed("fastest")
turtle.pensize(20)
t.colormode(255)

# Below is the updated color list without whites
color_palette2 = [(220, 153, 106), (131, 171, 196), (221, 72, 87), (216, 131, 149), (23, 119, 153),
                  (242, 208, 98), (120, 177, 148), (37, 118, 81), (20, 164, 205), (141, 86, 62), (222, 82, 76),
                  (126, 86, 105), (173, 184, 216), (20, 166, 121), (163, 209, 167), (3, 97, 116), (175, 155, 75),
                  (55, 60, 94), (236, 162, 175), (239, 169, 155), (145, 207, 223), (85, 126, 182), (24, 99, 84),
                  (48, 64, 76)]

def new_row():
    turtle.left(90)
    turtle.forward(50)
    turtle.left(90)
    turtle.forward(500)
    turtle.left(180)

def starting_position():
    turtle.setheading(220)
    turtle.penup()
    turtle.forward(320)
    turtle.setheading(0)

starting_position()
for x in range(10):
    for y in range(10):
        turtle.pencolor(random.choice(color_palette2))
        turtle.dot(20)
        turtle.penup()
        turtle.forward(50)
    new_row()

turtle.hideturtle()

screen = t.Screen()
screen.exitonclick()


from __future__ import with_statement
from fabric.api import *
import os, datetime

# to add more features to this file, refer to https://github.com/RacingTadpole/fab-faction/blob/master/fabfile.py
# to add automatic site/app/domain-creation, refer to:
#    http://lukeplant.me.uk/blog/posts/starter-fabfile-and-scripts-for-a-django-project-on-webfaction/

# defaults--same in every environment
env.hosts = ['yourname@yourhost.com']
site_root = '/your/site/root'
local_build_dir = 'build'

def flush_prod():
	with cd(site_root):
		run('rm -rf *')

# `images` is false by default since this could take a while, and doesn't need to be done every deploy
# example usage: `fab deploy_prod:True`
def deploy_prod(images=False):

	# finalize with grunt:
	local("grunt finalize")

	# each deploy doesn't pull down from git, but we do enforce a commit at least, so each deploy is tracked
	local("git add --all")
	with settings(warn_only=True):
		# maybe there's nothing to commit? still want to proceed, so suppress this error
		local("git commit")

	# generate a tarball, minus the `images` directory:
	with lcd(local_build_dir):
		local('tar --exclude=\'./images/*\' -zcvf ../build.tar.gz .')
		put('../build.tar.gz', site_root)
		local('rm ../build.tar.gz') # clean up

	# extract dat tarball:
	with cd(site_root):
		run('tar xzf build.tar.gz')
		run('rm build.tar.gz')

	# images not tar'd up, just transferred manually.
	# don't know a way to do a diff, w/o checking them into git
	if images:
		with cd(site_root):
			put('build/images/', site_root)


from suds.client import Client

def userinfo(message):

    session_status = 'Нет активной сессии'

    login_id = message.text
    endpoint = "http://172.16.89.4:8088/billing/phpclient/admin/soap/api3.wsdl"
    service_url = 'http://172.16.89.4:34012'
    client = Client(endpoint, faults=False)
    client.set_options(location=service_url)
    manager = client.service.Login('userinfo', ')@6D2f78')

    users = client.service.getVgroups(flt={'login': login_id})
    user = users[1][0]
    account = client.service.getAccount(id=user['uid'])
    account = account[1][0]
    tarif_info = client.service.getTarif(id=user['tarid'])
    tarif_info = tarif_info[1][0]
    block_id = user['blocked']
    abonent_name = user['username']
    mobile = account['account']['mobile']
    balance = round(user['balance'], 2)
    tarif = tarif_info['tarif']['descr']
    shape = tarif_info['tarif']['shape']
    rent = tarif_info['tarif']['rent']

    sessions = client.service.getSessionsRadius(flt={'agentid': '1'}, )

    for session in sessions[1]:
        if session['vgid'] == user['vgid']:
            x = bin(session['assignedip'])[2:].zfill(32)
            x1 = int(x[0:8], base=2)
            x2 = int(x[8:16], base=2)
            x3 = int(x[16:24], base=2)
            x4 = int(x[24:], base=2)
            ip = f'{x1}.{x2}.{x3}.{x4}'
            ip_address = ip
            session_status = 'Сессия активна'
            mac = session['sessani']
            start_time = session['starttime']

    if 'address' in user:
        address_list = user['address'][0]['address'].split(',')
        address = address_list[3] + ' ' + address_list[5] + ' ' + address_list[6] + ' '
        city = address[3]
        street = address[5]
        house = address[6]
        if len(address_list[7]) > 0:
            address = address + address_list[7]
    else:
        address = "~"

    if block_id == 0:
        block = 'Активная'
    elif block_id == 4:
        block = 'Заблокирована по балансу'
    elif block_id == 10:
        block = 'Учетная запись отключена'
    else:
        block = 'Заблокирована, причина: ' + str(block_id)

    if session_status == 'Сессия активна':
        info_for_user = f'''
Абонент: {abonent_name};
Телефон: {mobile};
Адрес: {address};
Баланс: {balance} тг;
Статус у.з.: {block};
Сессия: {session_status};
Время начала: {start_time};
Ip: {ip_address}.
    '''
    else:
        info_for_user = f'''
Абонент: {abonent_name};
Телефон: {mobile};
Адрес: {address};
Баланс: {balance} тг;
Статус у.з.: {block};
Сессия: {session_status}.
            '''

    return info_for_user


import os
import sys

from .common import *
from .Benchmark import *
from .Figure import *
from .Requirement import *

class Bench:
    """A benchmarking control object"""

    def __init__(self, config, parts, figures, requirements, result_dir):
        self.parts = parts
        self.config = config
        self.figures = figures
        self.requirements = requirements
        self.result_dir = result_dir

    @classmethod
    def new(cls, config, figures, result_dir):
        parts = [ os.path.splitext(os.path.basename(figures['input_file']))[0] ]
        figures = Figure.flatten(figures['json'])
        benchmarks = Benchmark.uniq(figures)
        requirements = Requirement.uniq(benchmarks)
        config['json']['report']['high_level_setup_figure'] = os.path.abspath(config['json']['report']['high_level_setup_figure'])
        return cls(config['json'], parts, figures, requirements, result_dir)

    @classmethod
    def carry_on(cls, bench):
        bench = bench['json']
        figures = [Figure(f, bench['result_dir']) for f in bench['figures']]
        requirements = {id: Requirement(r) for id, r in bench['requirements'].items()}
        return cls(bench['config'], bench['parts'], figures, requirements, bench['result_dir'])

    def cache(self):
        """Cache the current state of execution to a file"""
        output = {
            'config': self.config,
            'parts': self.parts,
            'figures': [f.cache() for f in self.figures],
            'requirements': {id: r.cache() for id, r in self.requirements.items()},
            'result_dir': self.result_dir
        }

        output_path = os.path.join(self.result_dir, 'bench.json')
        with open(output_path, 'w') as file:
            json.dump(output, file, indent=4)

    def get_config(self):
        return self.config

    def run(self):
        # run all benchmarks one-by-one
        skip = False
        for _, req in self.requirements.items():
            if req.is_done():
                continue
            if not req.is_met(self.config):
                skip = True
                print('Skip: the requirement is not met: ' + str(req))
                continue
            req.benchmarks_run(self, self.result_dir)

        # in case of a skip, not all results are ready
        if skip:
            return

        # collect data required for all scheduled figures
        for f in self.figures:
            if f.is_done():
                continue
            f.prepare_series(self.result_dir)
            self.cache()

    def dump(self):
        for _, req in self.requirements.items():
            if req.is_done():
                status = "done"
            elif req.is_met(self.config):
                status = "met"
            else:
                status = "not met"
            print("Requirement: {} {}\n".format(req, status))
            req.benchmarks_dump(self, self.result_dir)

    def check_completed(self):
        for _, req in self.requirements.items():
            if not req.is_done():
                raise Exception('Benchmarking not completed. Please use report_bench.py.')
        for f in self.figures:
            if not f.is_done():
                raise Exception('Postprocessing not completed. Please use report_bench.py.')


import pytest

from src.data.test import Suite, Parameter, from_dict

@pytest.mark.unit
def test_deserialization():
    suite_dict = {
        'conditions': [{'id': 'c', 'variable': 'cause', 'condition': 'occurs'}],
        'expected': [{'id': 'e', 'variable': 'effect', 'condition': 'happens'}],
        'cases': [
            {'c': False, 'e': False},
            {'c': True, 'e': True}
        ]
    }
    deserialized = from_dict(suite_dict)

    cause = Parameter(id='c', variable='cause', condition='occurs')
    effect = Parameter(id='e', variable='effect', condition='happens')
    cases=[
        {'c': False, 'e': False},
        {'c': True, 'e': True}
    ]
    expected = Suite(conditions=[cause], expected=[effect], cases=cases)

    assert deserialized == expected


'''Подсчет количества объектов'''

'''
Пример подсчёта количества элементов в строке: подсчитаем сколько раз встретилась каждая буква в строке.
'''

s = input('Введите строку! Подсчитваем ее буквы: ')
d = {}
for i in s:
    if i in d:
        d[i] += 1
    else:
        d[i] = 1
print(d)
for letter, count in d.items():
    print(letter, count)

'''
В данном алгоритме если в нашем словаре есть уже такой элемент, то мы увеличиваем счётчик на 1, а если этого элемента не было,
то мы создавали его как ключ и ставим счётчик на 1 (поскольку это его первое появление)

Давайте оптимизируем:

при помощи метода isalpha() у строк оставим только буквы, тем самым исключим числа и знаки препинания
вспомним о методе get у словаря: с помощью метода get() можно получить значение, которое лежало по данному ключу, а в случае,
если ключа не было, возвращается 0 и добавляем единицу
В итоге получим такой код
'''

s = input('Введите строку! Подсчитваем ее буквы: ')
d = {}
for i in s:
     if i.isalpha():
        d[i] = d.get(i, 0) + 1
print(d)
for letter, count in d.items():
    print(letter, count)

'''
Словари помогают установить соответствие между объектами
'''

words = {}
while True:
  s = input('Введите английское слово: ')
  if s in words:
      print("Слово", s, 'переводится как', words[s])
  else:
      print("Введите перевод слова", s)
      words[s] = input()

'''
В итоге мы вводим слово, если оно есть в словаре, то мы выводим значение по заданному ключу (перевод слова), если же такого ключа нет,
то мы просим ввести перевод и добавляем в словарь пару ключ-значение
'''


import os
import asyncio
import logging
import logging.config
import argparse
import sentry_sdk
from random import randint
from sentry_sdk.integrations.logging import LoggingIntegration

MAX_DELAY_SECONDS = 5
parent_logger = logging.getLogger('parent')
children_logger = logging.getLogger('parent.children')
logging.config.fileConfig('logging.ini')

async def sleep():
    await asyncio.sleep(randint(1, MAX_DELAY_SECONDS))

async def debug(event: 'asyncio.Event'):
    while not event.set():
        parent_logger.debug('Debug message')
        children_logger.debug('Debug message')
        await sleep()

async def info(event: 'asyncio.Event'):
    while not event.set():
        parent_logger.info('Info message')
        children_logger.info('Info message')
        await sleep()

async def warning(event: 'asyncio.Event'):
    while not event.set():
        parent_logger.warning('Warning message')
        children_logger.warning('Warning message')
        await sleep()

async def error(event: 'asyncio.Event'):
    while not event.set():
        parent_logger.error('Error message')
        children_logger.warning('Error message')
        await sleep()

async def exception(event: 'asyncio.Event'):
    while not event.set():
        try:
            _ = {}['key']
        except KeyError:
            parent_logger.exception('Exception message')

        try:
            _ = {}['key']
        except KeyError:
            children_logger.exception('Exception message')
        await sleep()

def create_tasks(loop, event, exception_enable):
    sentry_dsn = os.environ.get('SENTRY_DSN', None)
    if sentry_dsn:
        sentry_logging = LoggingIntegration(
            level=logging.INFO,
            event_level=logging.ERROR
        )
        sentry_sdk.init(sentry_dsn, traces_sample_rate=1.0, integrations=[sentry_logging])
    functions = [debug, info, warning, error]
    if exception_enable:
        functions.append(exception)
    [loop.create_task(func(event)) for func in functions]

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--ex_enable', action='store_true', required=False)
    logging.info('Start application')
    current_loop = asyncio.get_event_loop()
    signal_event = asyncio.Event()
    args = parser.parse_args()
    create_tasks(current_loop, signal_event, args.ex_enable)
    try:
        current_loop.run_forever()
    except KeyboardInterrupt:
        logging.info('Wait stop application')
        signal_event.set()
        current_loop.stop()
        logging.info('Application success stop')


import json
from pprint import pprint
import numpy as np
from src.environment import TennisEnvironment
from src.maddpg import MADDPG1
from src.models import SimpleMADDPGAgent1
import datetime
import matplotlib.pyplot as plt
import torch

def train(*args, **kwargs):
    print(kwargs)

    env = TennisEnvironment(**kwargs['env'])
    env.reset(train_mode=True)

    state_dim = env.get_state_dim()
    action_dim = env.get_action_dim()
    kwargs['agent']['state_dim'] = state_dim
    kwargs['agent']['action_dim'] = action_dim
    kwargs['agent']['num_agents'] = env.get_num_agents()

    kwargs['ddpg']['device'] = 'cpu'

    agent = SimpleMADDPGAgent1(**kwargs['agent'])
    target_agent = SimpleMADDPGAgent1(**kwargs['agent'])
    alg = MADDPG1(agent=agent, target_agent=target_agent, **kwargs['ddpg'])
    scores = alg.train(env, 4000)

    dt = str(datetime.datetime.now().strftime("%m_%d_%Y_%I_%M_%p"))
    model_fname = "../models/ddpg_reacher_{}.pt".format(dt)
    torch.save(agent, model_fname)

    plt.plot(np.max(scores,axis=-1))
    plt.plot(np.convolve(np.max(scores, axis=-1), np.ones(100) / 100))
    fig_name = "../reports/ddpg_reacher_{}.png".format(dt)
    plt.savefig(fig_name)
    scores_fname = "../reports/ddpg_reacher_{}".format(dt)
    np.save(scores_fname, np.asarray(scores))

if __name__ == '__main__':
    with open('../config.json') as data_file:
        data = json.load(data_file)

    pprint(data)
    kwargs = data

    train(**kwargs)


# A minimal setup.py file to make a Python project installable.

import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

requirements = ['numpy', 'scipy', 'h5py']

setuptools.setup(
    name             = "pejzero",
    version          = "1.0.0",
    author           = "Whyjay Zheng",
    author_email     = "whyjz@berkeley.edu",
    description      = 'Supplmental materials presented in "Characteristics of marine-terminating glaciers vulnerable to lubricated bed"',
    long_description = long_description,
    long_description_content_type = "text/markdown",
    packages         = setuptools.find_packages(),
    classifiers       = [
        "Programming Language :: Python :: 3",
        "Operating System :: OS Independent",
        "License :: OSI Approved :: MIT License",
    ],
    python_requires  = '>= 3.8',
    install_requires = requirements,
)


from maestro import maestro
from conex import conex
from cursorDelPool import CursorDelPool
from logger_base import log

class maestroDao:
    _Select = "SELECT * FROM maestro ORDER BY id_maestro"
    _Insert = "INSERT INTO maestro (nombre,apellido,formacion) VALUES (%s,%s,%s)"
    _Update = "UPDATE maestro SET nombre=%s,apellido=%s,formacion=%s WHERE id_maestro=%s"
    _Delete = "DELETE FROM maestro WHERE id_maestro=%s"

    @classmethod
    def select(cls):
        with CursorDelPool() as cursor:
            cursor.execute(cls._Select)
            registros = cursor.fetchall()
            maestros = []
            for m in registros:
                maestros.append(maestro(m[0],m[1],m[2],m[3]))
            return maestros

    @classmethod
    def insert(cls, maestro):
        with CursorDelPool() as cursor:
            valores = (maestro._nombre, maestro._apellido, maestro._formacion)
            cursor.execute(cls._Insert,valores)
            return (f'{maestro._nombre}, filas afectadas: {cursor.rowcount}')

    @classmethod
    def update(cls, maestro):
        with CursorDelPool() as cursor:
            valores = (maestro._nombre, maestro._apellido, maestro._formacion, maestro.id_maestro)
            cursor.execute(cls._Update,valores)
            return (f'{maestro._nombre}, filas afectadas: {cursor.rowcount}')

    @classmethod
    def delete(cls, maestro):
        with CursorDelPool() as cursor:
            valores = (maestro.id_maestro,)
            cursor.execute(cls._Delete,valores)
            return (f'{maestro.id_maestro}, filas afectadas: {cursor.rowcount}')


import numpy as np
from operator import itemgetter

class Advertiser:
  def __init__(self, id, budget, num_creators):
    self.id = id # 0 through A
    self.budget = budget
    self.wallet = [[0 for i in range(2)] for j in range(num_creators)] # [amount, avg price paid]
    self.quality = np.random.uniform(0,1)
    self.match_vals = np.random.rand(num_creators).tolist()
    self.num_creators = num_creators

  # take in creator_id, and value and return
  # bid price
  def val_to_bid(self, value):
    return 2./3 * value

  # use function above and some logic to return
  # tuple of creator_id, bid, and amount
  def bid_strategy(self, cv_tuple):
  	#return self.random_all_in(cv_tuple)
  	return self.high_val_all_in(cv_tuple)

  def high_val_all_in(self,cv_tuple):
    bid_lst = []
    sorted_cv = sorted(cv_tuple, key=itemgetter(1), reverse=True)
    for i in range(len(sorted_cv)):
      index, value = sorted_cv[i]
      price = self.val_to_bid(value)
      if i == 0:
        quantity = self.budget/price
      else:
        quantity = 0
      full_tuple = (index, price, quantity)
      bid_lst.append(full_tuple)
    return bid_lst

  # very naive strategy where we randomly pick a creator to all-in
  def random_all_in(self, cv_tuple):
    bid_lst = []
    rand_index = np.random.randint(0,self.num_creators)
    for index, value in cv_tuple:
      price = self.val_to_bid(value)
      if index == rand_index:
        quantity = self.budget/price
      else:
        quantity = 0
      full_tuple = (index, price, quantity)
      bid_lst.append(full_tuple)
    return bid_lst

  # call the above function to return list of bids
  def bids(self, creators):
    cv_tuple = []
    for creator in creators:
      pop = creator.popularity()
      v = self.match_vals[creator.id]
      q = self.quality
      E = pop * v * q
      cv_tuple.append((creator.id,E))
    return self.bid_strategy(cv_tuple)

  def buy_ad(self, creators):
    for i in range(len(self.wallet)):
      E = creators[i].popularity() * self.quality * self.match_vals[i]
      if self.wallet[i][1] > E:
        continue
      else:
        self.budget += E*self.wallet[i][0]
        creators[i].coins += self.wallet[i][0]
        self.wallet[i] = [0,0]

  # creator_id is equivalent to coin_id
  def update_wallet(self, creator_id, price, amount):
    if amount > 0:
      self.wallet[creator_id][1] = (self.wallet[creator_id][0] * self.wallet[creator_id][1] + price * amount) / (self.wallet[creator_id][0] + amount)
    self.wallet[creator_id][0] += amount

  def asks(self):
    pass

  def approx_val(self, creators):
  	ad_dump = 0
  	for i in range(len(self.wallet)):
  		E = creators[i].popularity() * self.quality * self.match_vals[i]
  		ad_dump += E*self.wallet[i][0]
  	return self.budget + ad_dump


import os
import cv2

path = "/home/bo/桌面/LSVT/train_origin/train_full_images_0/"
datanames = os.listdir(path)
list = []
for i in datanames:
    list.append(i)
# print(list)

i=0
scale = 2.4
for img_name in list:
    img = cv2.imread(path+img_name)
    h,w,c = img.shape

    if h/w>1.2 or w/h>1.2 or h/512>scale or 512/h>scale or w/512>scale or 512/w>scale or min(h,w)/512<1:
        continue
    img_test = cv2.resize(img, (512, 512))

    cv2.imwrite('/home/bo/桌面/LSVT/train_32/'+img_name, img_test)

    print(str(i)+'/'+str(len(list))+': '+img_name)
    i+=1


from multiprocessing import cpu_count, Process
from abc import ABCMeta, abstractmethod
from functools import reduce
import pickle
import types
import time
import os

def run(infile, mapper, reducer, max_cores=None):
    master = Master(infile, mapper, reducer, max_cores)
    return master.run()

class Mapper(metaclass=ABCMeta):
    """
        mapper
    """
    def create(self, x):
        """ will be used once when func combine() exists """
        return x
    @abstractmethod
    def map(self):
        pass
    def destory(self):
        pass

class Reducer(metaclass=ABCMeta):
    """
        reducer
    """
    def create(self, x):
        """ will be used once when getting the first map result """
        return x
    @abstractmethod
    def reduce(self):
        pass
    def destory(self):
        pass

class SimpleFileReducer(Reducer):
    """
        a reducer outputs result to a file directly and return None
    """
    def __init__(self, outfile):
        self.writer = open(outfile, 'w', encoding='utf-8')
        assert self.writer != None
    def create(self, a):
        self.writer.write(a)
    def reduce(self, a, b):
        self.writer.write(b)
    def destory(self):
        self.writer.close()

class SimpleMapper(Mapper):
    """
        simple mapper
    """
    def map(self):
        pass

class SimpleReducer(Reducer):
    """
        simple reducer
    """
    def reduce(self):
        pass

class ShuffleIO(object):
    """
        data io, local file
    """
    @staticmethod
    def write_to_file(data_fp, index_fp, x):
        """
            write x to file with pickle format
        """
        data = pickle.dumps(x)
        size = len(data)
        data_fp.write(data)
        index_fp.write("%d\n" % size)

    @staticmethod
    def read_from_file(data_fp, index_fp):
        """
            iterator, read data from file with pickle format
        """
        for line in index_fp:
            size = int(line.strip())
            data = pickle.loads(data_fp.read(size))
            yield data

class Worker(object):
    """
        worker, using 1 core
    """
    def __init__(self, infile, task_name, proc_num, start, length, mapper):
        self.infile = infile
        self.task_name = task_name
        self.proc_num = proc_num
        self.start = start
        self.length = length
        self.mapper = mapper
        self.data_file_name = self.filename("data")
        self.index_file_name = self.filename("index")

    def filename(self, target):
        """
            return the filename corresponding to target
        """
        if target == "data":
            return "%s_%d.data" % (self.task_name, self.proc_num)
        elif target == "index":
            return "%s_%d.index" % (self.task_name, self.proc_num)

    def run_with_combine(self, fp, data_fp, index_fp):
        result = None
        isCreated = False
        for _ in range(self.length):
            data = self.mapper.map(fp.readline())
            if data is None: continue

            if isCreated:
                result = self.mapper.combine(result, data)
            else:
                result = self.mapper.create(data)
                isCreated = True

        ShuffleIO.write_to_file(data_fp, index_fp, result)

    def run_without_combine(self, fp, data_fp, index_fp):
        for _ in range(self.length):
            data = self.mapper.map(fp.readline())
            if data is None: continue
            ShuffleIO.write_to_file(data_fp, index_fp, data)

    def run(self):
        """
            worker runs mapper function in a working loop
        """

        with open(self.infile, encoding='utf-8') as fp, \
            open(self.data_file_name, 'wb') as data_fp, \
            open(self.index_file_name, 'w') as index_fp:

            # jump to start pos
            fp.seek(self.start)

            if hasattr(self.mapper, 'combine'):
                self.run_with_combine(fp, data_fp, index_fp)
            else:
                self.run_without_combine(fp, data_fp, index_fp)

            self.mapper.destory()

class Master(object):
    """
        master of the parallel computing framework, create it at first
    """
    def __init__(self, infile=None, mapper=None, reducer=None, max_cores=None):
        assert mapper!=None and (type(mapper) is types.FunctionType or issubclass(type(mapper), Mapper))
        assert reducer!=None and (type(reducer) is types.FunctionType or issubclass(type(reducer), Reducer))
        assert os.path.isfile(infile)

        self.infile = infile
        self.task_name = "parallel_%d" % int(time.time())
        self.cores = cpu_count()

        if max_cores != None:
            assert type(max_cores) == type(1)
            assert 0 < max_cores <= cpu_count()
            self.cores = max_cores

        if type(mapper) is types.FunctionType:
            self.mapper = SimpleMapper()
            self.mapper.map = mapper
        else:
            self.mapper = mapper

        if type(reducer) is types.FunctionType:
            self.reducer = SimpleReducer()
            self.reducer.reduce = reducer
        else:
            self.reducer = reducer

    def run(self):
        """
            running task by mapper & reducer, using all cpu cores
        """

        starts_lengths = self.get_workload_for_workers()

        workers = []
        for i, start_len in enumerate(starts_lengths):
            proc_num = i+1
            start = start_len[0]
            length = start_len[1]

            worker = Worker(self.infile, self.task_name, proc_num, start, length, self.mapper)
            worker.proc = Process(target=lambda x:x.run(), args=(worker,))
            workers.append(worker)

        # start workers
        for worker in workers: worker.proc.start()

        # block
        self.block_until_workers_done(workers)

        # reducing
        result = self.reducing(workers)

        # join
        for worker in workers: worker.proc.join()

        return result

    def get_workload_for_workers(self):
        """
            get start pos & number of lines need to be mapped, for every worker
        """
        pos = 0
        total_line = 0
        line_pos = []

        # get total line and pos of every line
        with open(self.infile, encoding='utf-8') as fp:
            for line in fp:
                line_pos.append(pos)
                total_line += 1
                pos += len(line.encode('utf-8'))

        assert total_line > 0, "total number of lines must be more than 0"

        # workload(the number of lines) of every worker
        if total_line <= self.cores:
            lengths = [1 for _ in range(total_line)]
        else:
            lengths = [int(total_line/self.cores) for _ in range(self.cores)]
            for i in range(total_line%self.cores):
                lengths[i] += 1

        # start pos of infile for every worker
        cur_line = 0
        starts = []
        for length in lengths:
            starts.append(line_pos[cur_line])
            cur_line += length

        return list(zip(starts, lengths))

    def block_until_workers_done(self, workers):
        """
            blocking
        """
        states = [False for _ in range(len(workers))]
        while not reduce(lambda a,b: a and b, states):
            need_sleep = True

            for i, worker in enumerate(workers):
                if states[i]:
                    continue
                if not worker.proc.is_alive():
                    states[i] = True
                    need_sleep = False

            if need_sleep: time.sleep(0.2)

    def reducing(self, workers):
        """
            master process running reducer function
        """
        result = None
        isCreated = False

        # read intermediate results from files written by workers, and reduce them
        for worker in workers:
            data_file = worker.data_file_name
            index_file = worker.index_file_name

            if os.path.isfile(data_file) and os.path.isfile(index_file):

                # reducer
                for data in ShuffleIO.read_from_file(open(data_file, 'rb'), open(index_file, 'r')):
                    if isCreated:
                        result = self.reducer.reduce(result, data)
                    else:
                        result = self.reducer.create(data)
                        isCreated = True

                # delete files
                os.remove(data_file)
                os.remove(index_file)

        self.reducer.destory()
        return result


import json
import urllib.parse
import boto3

print('Loading function')

def lambda_handler(event, context):
    #print("Received event: " + json.dumps(event, indent=2))

    # Extracting relevant information from the S3 event trigger
    bucket_name = event['Records'][0]['s3']['bucket']['name']
    object_key = event['Records'][0]['s3']['object']['key']

    print(f"File '{object_key}' was uploaded to bucket '{bucket_name}'")

    # Send a notification via SNS

    try:
        sns = boto3.client('sns')
        topic_arn = 'arn:aws:sns:ap-south-1:<AWS account-id>:s3-lambda-sns'
        sns.publish(
            TopicArn=topic_arn,
            Subject='S3 Object Created',
            Message='File ' +  object_key + ' was uploaded to bucket ' + bucket_name
            )

    except Exception as e:

        print(e)
        print('Error getting object_key{} from bucket_name{}. Make sure they exist and your bucket is in the same region as this function.'.format(object_key, bucket_name))
        raise e

    return {
        'statusCode': 200,
            'body': json.dumps('Lambda function executed successfully')
	    }


import requests

from elasticsearch import Elasticsearch

SETTINGS = {
    'analysis': {
        'analyzer': {
            # for the vector scoring plug-in configuration
            'payload_analyzer': {
                'type': 'custom',
                'tokenizer': 'whitespace',
                'filter': 'delimited_payload_filter'
            }
        }
    }
}

BING_MAPPINGS = {
    "bing": {
        "properties": {
            "summary": {"type": "text"},
            "url": {"type": "text"},
            "article_name": {"type": "text"},
            "news_source_name": {"type": "text"},
            "date_published": {"type": "date"},
            "all_text": {"type": "text"},
        }
    }
}

DENSITY_MAPPINGS = {
    "arson_density": {
        "properties": {
            "state": {"type": "text"},
            "count": {"type": "integer"},
            "population": {"type": "integer"},
            "year": {"type": "text"},
            "pop_density": {"type": "float"}
            }
        }
}

MAPPINGS = {
    "motives": {
        "properties": {
            "state": {"type": "text"},
            "motive": {"type": "text"},
            "year": {"type": "text"}
        }
    },
    "method": {
        "properties": {
            "state": {"type": "text"},
            "method": {"type": "text"},
            "year": {"type": "text"}
        }
    },
    "ownership": {
        "properties": {
            "state": {"type": "text"},
            "ownership": {"type": "text"},
            "year": {"type": "text"}
        }
    },
    "monthly_counts": {
        "properties": {
            "state": {"type": "text"},
            "month": {"type": "text"},
            "count": {"type": "integer"},
            "year": {"type": "text"}
        }

    },
    "arson_density": {
        "properties": {
            "state": {"type": "text"},
            "count": {"type": "integer"},
            "population": {"type": "integer"},
            "year": {"type": "text"},
            "pop_density": {"type": "float"}
        }
    }
}

class ElasticSearch(object):

    def __init__(self):
        self.host = "localhost"
        self.port = 9200
        self.es = Elasticsearch([{'host': self.host, 'port': self.port}])

    def delete_index(self, index):
        if self.es.indices.exists(index):
            print("Index '{}' exists. Deleting it now...".format(index))
            es_object.indices.delete(index=index)
            print("Deleted index...")

    def create_indices(self, settings=SETTINGS, mappings=MAPPINGS):
        for key, value in mappings.items():
            if not self.es.indices.exists(key):
                self.es.indices.create(
                    index=key,
                    body={"settings": settings, "mappings": {key: value}}
                )
                print("Index '{}' created.".format(key))
            else:
                print("Index '{}' already exists. Cannot create it.".format(key))

    def add_to_index(self, index, doc_type, body):
        self.es.index(index=index, doc_type=doc_type, body=body)

    def add_to_elastic(self, aggs, key, year, fields):
        if key == "monthly_counts":
            for row in aggs:
                self.es.index(
                    index=key,
                    doc_type=key,
                    body=dict(zip(fields[key], [row["state"], row["inc_date"], row["count"], year]))
                )

        elif key == "arson_density":
            for row in aggs:
                self.es.index(
                    index=key,
                    doc_type=key,
                    body=dict(zip(fields[key], [row[field] for field in fields[key]]))
                )

        else:
            for state, value in aggs.items():
                self.es.index(
                    index=key,
                    doc_type=key,
                    body=dict(zip(fields[key], [state, value, year]))
                )

    def query_index(self, index_name, query, field="", max_size=51):
        if len(field) > 0:
            query = field + ":" + query
        res = self.es.search(index=index_name, q=query, size=max_size)
        return res["hits"]["hits"]


import cv2
import numpy as np

img=cv2.imread('mermer.jpg')

img=cv2.resize(img,(800,800))

hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# kırmızı renk için HSV değerleri
low_red=np.array([170, 50, 70])
high_red=np.array([180, 255, 255])

mask = cv2.inRange(hsv_img, low_red, high_red)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
mask = cv2.GaussianBlur(mask, (3, 3), 0)
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
                        cv2.CHAIN_APPROX_SIMPLE)[-2]

for cnt in cnts:
    (x, y, w, h) = cv2.boundingRect(cnt)

    if w > 50: # Resimdeki küçük yerleri tespit etmesin diye. Bu değer resimden resime değişir
        cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 4)

cv2.imshow("Resim", img)
cv2.waitKey(0)
cv2.destroyAllWindows()


# Task:
# - Processed the data: On a particular date, If 70% number of confirmed case is zero, then Delete the column. i.e. whole February will be deleted and few more.
# - Plot the graph
#     - Country Wise
#     - Date Wise
#     - Continent Wise

import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv('../Datasets/time_series_2019-ncov-Confirmed.csv')
# print(data.head())
# print(data.describe())
calcuation = (len(data)*70)/100
totalZeroCasesInCol = (data == 0).sum()
dropCol = [k for k,v in totalZeroCasesInCol.iteritems() if v > calcuation]
data.drop(dropCol, axis=1, inplace=True)
# print(data.info)

# Country wise ploting.
curData = data.groupby(data['Country/Region']).sum()
curData.drop(['Lat', 'Long'],axis=1, inplace=True)
# print(curData)

xCountry = curData.index[5:20]
yCountry = curData.iloc[5:20,-1]

plt.title('Country wise ploting',fontsize=16, fontweight='bold')
plt.xlabel('Countries')
plt.xticks(rotation=90)
plt.ylabel('NO. of confirmed cases')
plt.bar(xCountry, yCountry, color='red')
plt.show()
# Date Wise Ploting.

curData.loc['total'] = curData.sum(axis=0)
print(curData)

xDate = curData.columns
yDate = curData.loc['total']

plt.title('Date wise ploting')
plt.xlabel('Date',fontsize=16)
plt.xticks(rotation=90)
plt.ylabel('NO. of confirmed cases')

plt.plot(xDate,yDate, '-or')
plt.show()


import logging

from homeassistant.components.sensor import SensorEntity, SensorDeviceClass
from homeassistant.const import UnitOfTemperature
from homeassistant.core import HomeAssistant

from .WebBoilerGenericSensor import WebBoilerGenericSensor

_LOGGER = logging.getLogger(__name__)

class WebBoilerHeatingCircuitSensor:
    """Representation of a Centrometal Boiler Sensor."""

    @staticmethod
    def create_heating_circuits_entities(
        hass: HomeAssistant, device
    ) -> list[SensorEntity]:
        """Creates heating circuits entities."""
        entities = []
        for i in range(1, 5):
            prefix = f"C{i}B"
            name = f"Circuit {i}"
            if WebBoilerHeatingCircuitSensor.device_has_prefix(device, prefix):
                entities.extend(
                    WebBoilerHeatingCircuitSensor.create_heating_circuit_entities(
                        hass, device, prefix, name
                    )
                )
        for i in range(1, 5):
            prefix = f"K{i}B"
            name = f"Circuit {i}K"
            if WebBoilerHeatingCircuitSensor.device_has_prefix(device, prefix):
                entities.extend(
                    WebBoilerHeatingCircuitSensor.create_heating_circuit_entities(
                        hass, device, prefix, name
                    )
                )
        return entities

    @staticmethod
    def device_has_prefix(device, prefix):
        """Returns if device has prefix."""
        for param in device["parameters"].keys():
            if param.startswith(prefix):
                return True
        return False

    @staticmethod
    def create_heating_circuit_entities(
        hass: HomeAssistant, device, prefix, name
    ) -> list[SensorEntity]:
        """Creates entities for heating circuit."""
        entities = []
        items = {}
        items[prefix + "_CircType"] = [
            None,
            "mdi:view-list",
            None,
            name + " Heating Type",
        ]
        items[prefix + "_dayNight"] = [
            None,
            "mdi:view-list",
            None,
            name + " Day Night Mode",
        ]
        items[prefix + "_kor"] = [
            UnitOfTemperature.CELSIUS,
            "mdi:thermometer",
            SensorDeviceClass.TEMPERATURE,
            name + " Room Target Correction",
        ]
        items[prefix + "_korType"] = [
            None,
            "mdi:view-list",
            None,
            name + " Correction Type",
        ]
        items[prefix + "_onOff"] = [None, "mdi:pump", None, name + " Pump Demand"]
        items[prefix + "_P"] = [None, "mdi:pump", None, name + " Pump"]
        items[prefix + "_Tpol"] = [
            UnitOfTemperature.CELSIUS,
            "mdi:thermometer",
            SensorDeviceClass.TEMPERATURE,
            name + " Flow Target Temperature",
        ]
        items[prefix + "_Tpol1"] = [
            UnitOfTemperature.CELSIUS,
            "mdi:thermometer",
            SensorDeviceClass.TEMPERATURE,
            name + " Flow Measured Temperature",
        ]
        items[prefix + "_Tsob"] = [
            UnitOfTemperature.CELSIUS,
            "mdi:thermometer",
            SensorDeviceClass.TEMPERATURE,
            name + " Room Target Temperature",
        ]
        items[prefix + "_Tsob1"] = [
            UnitOfTemperature.CELSIUS,
            "mdi:thermometer",
            SensorDeviceClass.TEMPERATURE,
            name + " Room Measured Temperature",
        ]
        items[prefix + "_misC"] = [
            None,
            "mdi:pipe-valve",
            None,
            name + " Valve Closing",
        ]
        items[prefix + "_misO"] = [
            None,
            "mdi:pipe-valve",
            None,
            name + " Valve Opening",
        ]
        for param_id, sensor_data in items.items():
            parameter = device.get_parameter(param_id)
            entities.append(
                WebBoilerGenericSensor(hass, device, sensor_data, parameter)
            )
        return entities