#!/usr/bin/python
#
# How to Implement a General Solution for Federated API/CLI Access Using SAML 2.0
# https://aws.amazon.com/blogs/security/how-to-implement-a-general-solution-for-federated-apicli-access-using-saml-2-0/
# Modified for ABC, using CA Siteminder for SSO into AWS.
#
# Usage:
# pipenv install
# pipenv run python samlapi_formauth.py

import sys
import boto3
import requests
import configparser
import base64
import xml.etree.ElementTree as ET
import re
# import json
import os

from pypac import PACSession
from pypac import pac_context_for_url
from bs4 import BeautifulSoup
from os.path import expanduser
from urllib.parse import urlparse
from getpass import getpass

##########################################################################
# Variables
# TODO Use argparse for stuff like region

# region: The default AWS region that this script will connect
# to for all API calls
region = 'ap-southeast-1'

# output format: The AWS CLI output format that will be configured in the
# saml profile (affects subsequent CLI calls)
outputformat = 'json'

# The file, relative to user's home directory, where this script will store the temp
# credentials under the saml profile
awsconfigfile = os.path.join('.aws', 'credentials')

# SSL certificate verification: Whether or not strict certificate
# verification is done, False should only be used for dev/test
sslverification = True

# Amount of time to wait before aborting.
request_timeout = 5

# idpentryurl: The initial url that starts the authentication process.
idpentryurl = 'https://abc.com/saml2sso?SPID=urn:amazon:webservices'

# Uncomment to enable low level debugging
# logging.basicConfig(level=logging.DEBUG)

# FIXME The requested DurationSeconds exceeds the MaxSessionDuration set for this role.
duration_seconds_long = 28800 # 8h
# duration_seconds_long = 14400 # 4h

def __load_roles_config():
    """Load the aws config profiles data for use."""
    output = {}

    config = configparser.ConfigParser()
    config.read_file(open('aws-extend-switch-roles.txt'))

    for section in config.sections():

        # handle abc-cloud-master edge case.
        profile_name = section
        role_arn = None
        if ' ' in section:
            profile_name = section.split(' ')[1]
            role_arn = config[section]['role_arn']
            account_id = role_arn.split(':')[4]
        else:
            # abc-cloud-master
            account_id = config[section]['aws_account_id']

        output[account_id] = {
            'account_id': account_id,
            'profile_name': profile_name,
            'role_arn': role_arn
        }

    return output

def __get_login():
    # Get the federated credentials from the user
    return (input('Username:'), getpass())

def __get_login_payload(formsoup, creds, wan_ip):
    # Parse the response and extract all the necessary values in order to build a dictionary of all of the form values the IdP expects
    payload = {}

    for inputtag in formsoup.find_all(re.compile('(INPUT|input)')):
        name = inputtag.get('name', '')
        value = inputtag.get('value', '')
        if 'user' in name.lower():
            # Make an educated guess that this is the right field for the username
            payload[name] = creds[0]
        elif 'email' in name.lower():
            # Some IdPs also label the username field as 'email'
            payload[name] = creds[0]
        elif 'password' == name.lower():
            # Append some silly text to the end of the password
            payload[name] = '{}|^_^|{}'.format(creds[1], wan_ip)
        elif 'passwordtemp' == name.lower():
            # Put the password in by itself
            payload[name] = creds[1]
        else:
            # Simply populate the parameter with the existing value (picks up hidden fields in the login form)
            payload[name] = value

    return payload

def __get_login_submit_url(idpentryurl, idpauthformsubmiturl, formsoup):
    # Some IdPs don't explicitly set a form action, but if one is set we should
    # build the idpauthformsubmiturl by combining the scheme and hostname
    # from the entry url with the form action target.
    # If the action tag doesn't exist, we just stick with the idpauthformsubmiturl above.
    for inputtag in formsoup.find_all(re.compile('(FORM|form)')):
        action = inputtag.get('action')
        if action:
            parsedurl = urlparse(idpentryurl)
            idpauthformsubmiturl = parsedurl.scheme + '://' + parsedurl.netloc + action
    return idpauthformsubmiturl

def __get_wan_ip():
    # Needed for CA siteminder login details
    tmpSession=PACSession()
    r = tmpSession.get('http://checkip.amazonaws.com/', verify=False)
    return r.text.rstrip()  # Chomp the trailing newline

def __extract_saml_assertion(response):
    # Decode the response and extract the SAML assertion
    soup = BeautifulSoup(response.text, 'html.parser')
    # print(soup.text)

    assertion = ''

    # Look for the SAMLResponse attribute of the input tag (determined by
    # analying the debug print lines above)
    for inputtag in soup.find_all('input'):
        if(inputtag.get('name') == 'SAMLResponse'):
            # print(inputtag.get('value'))
            assertion = inputtag.get('value')

    # Better error handling is required for production use.
    if (assertion == ''):
        # TODO: Insert valid error checking/handling
        print('Response did not contain a valid SAML assertion')
        sys.exit(0)

    return assertion

def __process_saml_assertion(assertion, roles_config):

    # Parse the returned assertion and extract the authorized roles
    awsroles = []
    root = ET.fromstring(base64.b64decode(assertion))
    for saml2attribute in root.iter('{urn:oasis:names:tc:SAML:2.0:assertion}Attribute'):
        if (saml2attribute.get('Name') == 'https://aws.amazon.com/SAML/Attributes/Role'):
            for saml2attributevalue in saml2attribute.iter('{urn:oasis:names:tc:SAML:2.0:assertion}AttributeValue'):
                # Note the reverse order, typically role_arn documented first.
                principal_arn, role_arn = saml2attributevalue.text.split(',')
                account_id = role_arn.split(':')[4]
                profile_name = roles_config[account_id]['profile_name']
                awsroles.append({
                    'role_arn': role_arn,
                    'principal_arn': principal_arn,
                    'account_id': account_id,
                    'profile_name': profile_name,
                    'saml2attributevalue': saml2attributevalue.text
                })

    # Ssome kind of logical order?
    awsroles = sorted(awsroles, key=lambda k: k['profile_name'])
    # exit('awsroles={}'.format(json.dumps(awsroles, sort_keys=True, indent=4)))

    awsrole = None  # Used for return.
    print('')

    # If I have more than one role, ask the user which one they want,
    # otherwise just proceed
    if len(awsroles) > 1:

        print('Please choose the role you would like to assume:\n')

        i = 0  # Because the data is zero-indexed
        for awsrole in awsroles:
            print('[{}]: {} {}'.format(i + 1, awsrole['profile_name'], awsrole['role_arn']))
            i += 1

        print('')

        selectedrole_valid = False
        while not selectedrole_valid:
            selectedrole_valid = False
            selectedroleindex = int(input('Selection:')) - 1  # zero-indexed
            # Basic sanity checks of input
            if selectedroleindex < 0:
                print('Select a positive number, try again.')
            elif selectedroleindex > (len(awsroles) - 1):
                print('You selected an invalid role index, please try again')
                # sys.exit(0)
            else:
                # We have a winner!
                selectedrole_valid = True

        awsrole = awsroles[int(selectedroleindex)]

    else:
        awsrole = awsroles[0]

    return awsrole

def __get_sts_token_from_login(response, roles_config, duration_seconds):
    # Use the assertion to get an AWS STS token using Assume Role with SAML

    print('Extracting data from SAML assertion...')

    assertion = __extract_saml_assertion(response)
    # print('assertion={}'.format(base64.b64decode(assertion)))

    print('Processing SAML into arns...')

    awsrole = __process_saml_assertion(assertion, roles_config)
    print('Requesting STS credentials for profile_name={}, role_arn={}'.format(awsrole['profile_name'], awsrole['role_arn']))

    # For DurationSeconds info, see http://boto3.readthedocs.io/en/latest/reference/services/sts.html#STS.Client.assume_role_with_saml
    with pac_context_for_url( 'http://checkip.amazonaws.com'):
        stsclient = boto3.client('sts', region_name=region)
    return stsclient.assume_role_with_saml(
        RoleArn=awsrole['role_arn'],
        PrincipalArn=awsrole['principal_arn'],
        SAMLAssertion=assertion,
        DurationSeconds=duration_seconds
    )

def main():
    # Converting a procedural script, piece by piece.

    print('samlapi_formauth running...')

    print('Loading account profile data')
    roles_config = __load_roles_config()
    # exit('roles_config: {}'.format(json.dumps(roles_config, sort_keys=True, indent=4)))

    # Find out the wanip, for IdP.
    wan_ip = __get_wan_ip()
    print('wan_ip {}'.format(wan_ip))

    # Initiate session handler, which takes care of cookies.
    session = PACSession()

    print('Fetching SSO page {}'.format(idpentryurl))

    # Programmatically get the SAML assertion
    # Opens the initial IdP url and follows all of the HTTP302 redirects, and gets the resulting login page.
    formresponse = session.get(idpentryurl, verify=sslverification, timeout=request_timeout)
    # print('formresponse = {}'.format(formresponse.text))

    # Capture the idpauthformsubmiturl, which is the final url after all the 302s
    # This might change below, based on what we find in html forms.
    idpauthformsubmiturl = formresponse.url
    # print('Redirected initial idpauthformsubmiturl {}'.format(idpauthformsubmiturl))

    # formsoup = BeautifulSoup(formresponse.text.decode('utf8'))
    formsoup = BeautifulSoup(formresponse.text, 'html.parser')

    # Ask User on laptop to enter their username + password.
    creds = __get_login()

    payload = __get_login_payload(formsoup, creds, wan_ip)
    # print('payload {}'.format(json.dumps(payload, sort_keys=True, indent=4)))

    idpauthformsubmiturl = __get_login_submit_url(idpentryurl, idpauthformsubmiturl, formsoup)
    print('idpauthformsubmiturl {}'.format(idpauthformsubmiturl))

    print('')  # Formatting.

    # Performs the submission of the IdP login form with the above post data
    response = session.post(idpauthformsubmiturl, data=payload, verify=sslverification, timeout=request_timeout)
    # print('POST response status {}, headers {}'.format(response.status_code, response.headers))
    # print('response.text={}'.format(response.text))

    # Delete the credential variables, just for safety
    del creds

    token = __get_sts_token_from_login(response, roles_config, duration_seconds_long)
    # print('STS token: {}'.format(json.dumps(token, sort_keys=True, indent=4)))

    # Write the AWS STS token into the AWS credential file
    filename = os.path.join(expanduser('~'), awsconfigfile)
    print('Writing temp creds to {}'.format(filename))

    # Read in the existing config file
    config = configparser.RawConfigParser()
    config.read(filename)

    # Put the credentials into a saml specific section instead of clobbering
    # the default credentials
    if not config.has_section('saml'):
        config.add_section('saml')

    config.set('saml', 'output', outputformat)
    config.set('saml', 'region', region)
    config.set('saml', 'aws_access_key_id', token['Credentials']['AccessKeyId'])
    config.set('saml', 'aws_secret_access_key', token['Credentials']['SecretAccessKey'])
    config.set('saml', 'aws_session_token', token['Credentials']['SessionToken'])

    # Write the updated config file
    with open(filename, 'w+') as configfile:
        config.write(configfile)

    # Give the user some basic info as to what has just happened
    print('\n\n----------------------------------------------------------------')
    print('Your new access key pair has been stored in the AWS configuration file {0} under the saml profile.'.format(filename))
    # TODO Format to localtime.
    print('Note that it will expire at {:%H:%M:%S %d/%b/%y %Z}.'.format(token['Credentials']['Expiration'].astimezone()))
    print('After this time, you may rerun this script to refresh your access key pair.')
    print('To use this credential, call the AWS CLI with the --profile option (e.g. aws --profile saml ec2 describe-instances).')
    print('----------------------------------------------------------------\n\n')

if __name__ == '__main__':
    main()


import time

from oslo_config import cfg

from conductor.common import db_backend
from conductor import service

CONF = cfg.CONF

def current_time_millis():
    """Current time in milliseconds."""
    return int(round(time.time() * 1000))

def main():
    """Sample usage of Music."""
    service.prepare_service()
    CONF.set_override('debug', True, 'music_api')
    CONF.set_override('mock', True, 'music_api')
    CONF.set_override('hostnames', ['music2'], 'music_api')
    music = db_backend.get_client()
    print("Music version %s" % music.version())

    # Randomize the name so that we don't step on each other.
    keyspace = 'NewVotingApp' + str(current_time_millis() / 100)
    music.keyspace_create(keyspace)

    # Create the table
    kwargs = {
        'keyspace': keyspace,
        'table': 'votecount',
        'schema': {
            'name': 'text',
            'count': 'varint',
            'PRIMARY KEY': '(name)'
        }
    }
    music.table_create(**kwargs)

    # Candidate data
    data = {
        'Joe': 5,
        'Shankar': 7,
        'Gueyoung': 8,
        'Matti': 2,
        'Kaustubh': 0
    }

    # Create an entry in the voting table for each candidate
    # and with a vote count of 0.
    kwargs = {'keyspace': keyspace, 'table': 'votecount', 'pk_name': 'name'}
    for name in data:  # We only want the keys
        kwargs['pk_value'] = name
        kwargs['values'] = {'name': name, 'count': 0}
        music.row_create(**kwargs)

    # Update each candidate's count atomically.
    kwargs = {'keyspace': keyspace, 'table': 'votecount', 'pk_name': 'name'}
    for name in data:
        count = data[name]
        kwargs['pk_value'] = name
        kwargs['values'] = {'count': count}
        kwargs['atomic'] = True
        music.row_update(**kwargs)

    # Read all rows
    kwargs = {'keyspace': keyspace, 'table': 'votecount'}
    print(music.row_read(**kwargs))  # Reads all rows

    # Delete Joe, read Matti
    kwargs = {'keyspace': keyspace, 'table': 'votecount', 'pk_name': 'name'}
    kwargs['pk_value'] = 'Joe'
    music.row_delete(**kwargs)
    kwargs['pk_value'] = 'Matti'
    print(music.row_read(**kwargs))

    # Read all rows again
    kwargs = {'keyspace': keyspace, 'table': 'votecount'}
    print(music.row_read(**kwargs))  # Reads all rows

    # Cleanup.
    music.keyspace_delete(keyspace)

if __name__ == "__main__":
    main()


"""empty message

Revision ID: 308b125d7992
Revises: 379c8d702a70
Create Date: 2019-04-27 14:51:27.810855

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
import app
from app.base import constants as APP

# revision identifiers, used by Alembic.
revision = '308b125d7992'
down_revision = '379c8d702a70'
branch_labels = None
depends_on = None
choices = app.base.model_fields.ChoiceString(APP.EMAIL_STATUS_CHOICES)

altered_tables = [
                  "corporate_access_event_host",
                  "corporate_access_event_invitee",
                  "corporate_access_event_rsvp",
                  "corporate_access_event_participant",
                  "corporate_access_event_collaborator",
                  ]

def upgrade():

    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('corporate_access_event_host', sa.Column('email_status', choices, nullable=False, server_default=APP.EMAIL_SENT))
    op.add_column('corporate_access_event_invitee', sa.Column('email_status', choices, nullable=False, server_default=APP.EMAIL_SENT))
    op.add_column('corporate_access_event_participant', sa.Column('email_status', choices, nullable=False, server_default=APP.EMAIL_SENT))
    op.add_column('corporate_access_event_rsvp', sa.Column('email_status', choices, nullable=False, server_default=APP.EMAIL_SENT))
    op.add_column('corporate_access_event_collaborator',
                  sa.Column('email_status', choices, nullable=False,
                            server_default=APP.EMAIL_SENT))

    op.create_table('unsubscribe_reason',
                    sa.Column('id', sa.BigInteger(), nullable=False),
                    sa.Column('created_date', sa.DateTime(), nullable=True),
                    sa.Column('modified_date', sa.DateTime(), nullable=True),
                    sa.Column('title',
                              app.base.model_fields.LCString(length=512),
                              nullable=False),
                    sa.Column('is_active', sa.Boolean(), nullable=True),
                    sa.PrimaryKeyConstraint('id')
                    )
    op.add_column('unsubscription',
                  sa.Column('reason_id', sa.BigInteger(), nullable=False))
    op.create_foreign_key('unsubscription_reason_id_fkey', 'unsubscription',
                          'unsubscribe_reason', ['reason_id'], ['id'],
                          ondelete='CASCADE')
    op.drop_column('unsubscription', 'reason')

    for table in altered_tables:
        update_query = "update {} set email_status=case when is_mail_sent='t' then '{}' when is_mail_sent= 'f' then '{}' end;"
        op.execute(update_query.format(table, APP.EMAIL_SENT, APP.EMAIL_NOT_SENT))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('corporate_access_event_rsvp', 'email_status')
    op.drop_column('corporate_access_event_participant', 'email_status')
    op.drop_column('corporate_access_event_invitee', 'email_status')
    op.drop_column('corporate_access_event_host', 'email_status')
    op.drop_column('corporate_access_event_collaborator', 'email_status')

    op.add_column('unsubscription', sa.Column('reason', sa.VARCHAR(length=512),
                                              autoincrement=False,
                                              nullable=True))
    op.drop_constraint('unsubscription_reason_id_fkey', 'unsubscription',
                       type_='foreignkey')
    op.drop_column('unsubscription', 'reason_id')
    op.drop_table('unsubscribe_reason')

    # ### end Alembic commands ###


import unittest

class Solution:
    def parseLine(self, line):
        size = len(line)

        result = []
        inQuote = False
        buffer = []

        i = 0
        while i < size:
            if inQuote:
                if line[i] == '"':
                    # inQuote
                    # if double "" => " else inQuote finish
                    if i + 1 < size and line[i+1] == '"':
                        buffer.append('"')
                        i += 1
                    else:
                        inQuote = False
                else:
                    buffer.append(line[i])
            else:
                if line[i] == '"':
                    inQuote = True
                elif line[i] == ',':
                    result.append(''.join(buffer))
                    buffer = []
                else:
                    buffer.append(line[i])

            i += 1

        if len(buffer) > 0:
            result.append(''.join(buffer))

        return '|'.join(result)

class TestParser(unittest.TestCase):
    parser = Solution()

    def testParseLineSimple(self):
        test = "John,Smith,john.smith@gmail.com,Los Angeles,1"
        expected = "John|Smith|john.smith@gmail.com|Los Angeles|1"
        self.assertEqual(expected, self.parser.parseLine(test))

    def testParseLineWithQuote(self):
        test = "Jane,Roberts,janer@msn.com,\"San Francisco, CA\",0"
        expected = "Jane|Roberts|janer@msn.com|San Francisco, CA|0"
        self.assertEqual(expected, self.parser.parseLine(test))

    def testParseLineWithDoubleQuotes(self):
        test = "\"Alexandra \"\"Alex\"\"\",Menendez,alex.menendez@gmail.com,Miami,1"
        expected = "Alexandra \"Alex\"|Menendez|alex.menendez@gmail.com|Miami|1"
        self.assertEqual(expected, self.parser.parseLine(test))

    def testParseLineWithStupidQuotes(self):
        test = "\"\"\"Alexandra Alex\"\"\""
        expected = "\"Alexandra Alex\""
        self.assertEqual(expected, self.parser.parseLine(test))

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


"""
Throttler limits how many requests can issue given a specific time window
Copied from https://github.com/hallazzang/asyncio-throttle
"""
import time
import asyncio
from collections import deque
from typing import Deque

# class Throttler:
#     """
#     Throttler
#     """

#     req_per_window: int
#     window: float
#     retry_interval: float

#     def __init__(
#         self, req_per_window: int, window: float = 1.0, retry_interval: float = 0.01
#     ):
#         """
#         Create a throttler.
#         """
#         self.req_per_window = req_per_window
#         self.window = window
#         self.retry_interval = retry_interval

#         self._task_logs: Deque[float] = deque()

#     def _flush(self) -> None:
#         now = time.time()
#         while self._task_logs:
#             if now - self._task_logs[0] > self.window:
#                 self._task_logs.popleft()
#             else:
#                 break

#     async def _acquire(self) -> None:
#         while True:
#             self._flush()
#             if len(self._task_logs) < self.req_per_window:
#                 break
#             await asyncio.sleep(self.retry_interval)

#         self._task_logs.append(time.time())

#     def release(self) -> None:
#         self._task_logs.pop()

#     async def __aenter__(self) -> None:
#         await self._acquire()

#     async def __aexit__(self, exc_type: Any, exc: Any, traceback: Any) -> None:
#         pass

class OrderedThrottler:
    """
    Throttler, but also keeps request in order by
    requiring them a seq number
    """

    req_per_window: int
    window: float
    retry_interval: float

    def __init__(self, req_per_window: int, window: float = 1.0, retry_interval: float = 0.01):
        """
        Create a throttler.
        """
        self.req_per_window = req_per_window
        self.window = window
        self.retry_interval = retry_interval

        self._task_logs: Deque[float] = deque()

    def _flush(self) -> None:
        now = time.time()
        while self._task_logs:
            if now - self._task_logs[0] > self.window:
                self._task_logs.popleft()
            else:
                break

    def session(self) -> "ThrottleSession":
        """returns a session"""
        return ThrottleSession(self)

class ThrottleSession:  # pylint: disable=protected-access
    """
    ThrottleSessions share a same rate throttler but
    can have independent sequence numbers
    """

    thr: OrderedThrottler
    seq: int = -1

    def __init__(self, thr: OrderedThrottler) -> None:
        self.thr = thr

    async def acquire(self, i: int) -> None:
        """
        Wait for the request being allowed to send out,
        without violating the # reqs/sec constraint and the order constraint.
        """
        if self.seq >= i:
            raise RuntimeError(f"{i} already acquired")

        while len(self.thr._task_logs) >= self.thr.req_per_window or self.seq != i - 1:
            await asyncio.sleep(self.thr.retry_interval)
            self.thr._flush()

        self.seq = i
        self.thr._task_logs.append(time.time())

    def release(self) -> None:
        """
        Cancel the last acquire, so the next acquire call will immediately return.
        Use this if no requests issued after the last acquire call.
        """
        self.thr._task_logs.pop()


import discord, os

from discord.ext import commands
from src.history import ChainedList
from src.functions import *

class app:
    "class bot"
    def __init__(self) -> None:
        logPath()
        intents = discord.Intents.all()
        client = commands.Bot(command_prefix='!', intents=intents)
        now = datetime.datetime.now()
        jsonD = loadJson()
        self.history = ChainedList({"id": 0, "user": "SYSTEM", "day": now.strftime("%d/%m/%Y"), "hours": now.strftime("%H:%M:%S"), "reason": "START"})
        self.page = 1

        #Viewing last command
        @client.command()
        async def hlast(ctx):
            await ctx.send(self.generateHistory("hlast"))
            self.log("Display of the last order",ctx)

        #Viewing history
        @client.command()
        async def h(ctx):
            self.log("Viewing history",ctx)
            self.history.to_json(jsonD['path'])
            message = await ctx.send(self.generateHistory("h"))
            await message .add_reaction('⬆')
            await message .add_reaction('⬇')

        #Interact with "async def h" by emoji to scroll history
        @client.event
        async def on_reaction_add(reaction, user):
            if user.bot:
                return
            channel = reaction.message.channel

            #delete the last message of bot
            async for msg in channel.history():
                    if msg.author == client.user:
                        await msg.delete()
                        break

            if str(reaction.emoji) == '⬆':
                self.page += 1
                print(self.page)
                message = await channel.send(self.generateHistory("hp"))
                await message .add_reaction('⬆')
                if self.page > 1 : await message .add_reaction('⬇')
            elif str(reaction.emoji) == '⬇':
                self.page -= 1
                if self.page > 1 :
                    message = await channel.send(self.generateHistory("hp"))
                    await message .add_reaction('⬆')
                    await message .add_reaction('⬇')
                else : await channel.send("There is no previous page to this one")

        #Viewing by name or id
        @client.command()
        async def hname(ctx):
            await ctx.send("About who you are looking for ? (id or username) example : username#1010")

            def check(message):
                return message.author == ctx.author and message.channel == ctx.channel

            message = await client.wait_for('message', check=check)
            await ctx.send(self.generateHistory(str(message.content)))
            self.log("Viewing history of " + str(message.content),ctx)

        #Delete history
        @client.command()
        async def hdel(ctx):
            if os.path.isfile(jsonD['path']):
                os.remove(jsonD['path'])
                self.history = ChainedList({"id": str(ctx.author.id), ctx.author: "SYSTEM", "day": now.strftime("%d/%m/%Y"), "hours": now.strftime("%H:%M:%S"), "reason": "New history"})
                self.log("History deleted",ctx)
                await ctx.send("History deleted on path " + jsonD['path'])
            else:
                await ctx.send("History not found")

        #Save history
        @client.command()
        async def s(ctx):
            self.log("Save history",ctx)
            self.history.to_json(jsonD['path'])
            await ctx.send("Save history successfully.")

        #When a new user joins the server
        @client.event
        async def on_member_join(ctx):
            self.log("New user : " + ctx.author + " name : " + ctx.author.id,ctx)
            await client.get_channel(1097370667646722058).send("Welcome to the server !"+ ctx.name)

        #When he launches
        @client.event
        async def on_ready():
            await client.get_channel(1097370667646722058).send("Navy bot is running !")

        client.run(jsonD['token'])

    #Save to json on node
    def log(self, reason, ctx):
        now = datetime.datetime.now()
        self.history.append({"id": ctx.author.id, "user": str(ctx.author), "day": now.strftime("%d/%m/%Y"), "hours": now.strftime("%H:%M:%S"), "reason": reason})

    #Return history data
    def generateHistory(self, search=None):
        current_node = self.history.first_node
        result = ""
        if search == "h":
            count = 0
            while current_node != None:
                if count >= 10:
                    return result
                count += 1
                result += ("[+] - id : " + str(current_node.data['id']) + " | user : " + current_node.data['user'] + " | day : " + current_node.data['day'] + " | hours : " + current_node.data['hours'] + " | reason : " + current_node.data['reason'] + "\n")
                current_node = current_node.next_node
            return result
        elif search == "hp":
            count = self.page * 10
            countL = self.page * 10
            line = 1
            while current_node != None:
                line += 1
                if count == 0: return result
                if count <= 10:
                    print(line, " a ", countL)
                    if line >= countL: return result; self.page = 1
                    result += ("[+] - id : " + str(current_node.data['id']) + " | user : " + current_node.data['user'] + " | day : " + current_node.data['day'] + " | hours : " + current_node.data['hours'] + " | reason : " + current_node.data['reason'] + "\n")
                    current_node = current_node.next_node
                    count -= 1
                else:
                    current_node = current_node.next_node
                    count -= 1
            return result
        elif search == "hlast":
            while current_node != None:
                result = ("[+] - id : " + str(current_node.data['id']) + " | user : " + current_node.data['user'] + " | day : " + current_node.data['day'] + " | hours : " + current_node.data['hours'] + " | reason : " + current_node.data['reason'] + "\n")
                current_node = current_node.next_node
            return result
        else:
            while current_node != None:
                if current_node.data['user'] == search or str(current_node.data['id']) == search:
                    result += ("[+] - id : " + str(current_node.data['id']) + " | user : " + current_node.data['user'] + " | day : " + current_node.data['day'] + " | hours : " + current_node.data['hours'] + " | reason : " + current_node.data['reason'] + "\n")
                current_node = current_node.next_node
            return result


from common import TEST

import unittest

class test_pdf(unittest.TestCase):
    """Class containing tests for pypgm/factor/gamma/pdf.py"""

    def test_01(self):
        """Tests the following :
        1) Functionality of calculate_pdf
        """

        TEST.LOG("START - PYPGM_FACTOR_GAMMA_PDF_01", 1)
        TEST.LOG(self.test_01.__doc__, 2)

        import numpy as np

        from pypgm.factor import gamma

        eval_values = np.linspace(0,100,1e5)

        pdf = gamma.calculate_pdf(eval_values, shape = 3, scale = 1./2)

        pdf_mean = np.sum(eval_values*pdf/np.sum(pdf))

        TEST.LOG("INFO LOGGING", 3)
        TEST.LOG("PDF MEAN", 3)
        TEST.LOG(pdf_mean, 3)

        TEST.LOG("START ASSERTS", 3)
        TEST.EQUALS(
            self, pdf_mean, 1.5,
            "Error 01")

        TEST.LOG("TEST COMPLETE", 1)


import numpy as np
import scipy.stats as sstats

def KNN(train_X, train_Y, test_X, ks, verbose=False):
    """
    Compute predictions for various k
    Args:
        train_X: array of shape Ntrain x D
        train_Y: array of shape Ntrain
        test_X: array of shape Ntest x D
        ks: list of integers
    Returns:
        preds: dict k: predictions for k
    """
    # Cats data to float32
    train_X = train_X.astype(np.float32)
    test_X = test_X.astype(np.float32)

    # Alloc space for results
    preds = {}

    if verbose:
        print("Computing distances... ", end='')

    dists = -2 * np.dot(train_X, test_X.T) + np.sum(test_X**2, axis=1) + np.sum(train_X**2, axis=1)[:, np.newaxis]

    if verbose:
        print("Sorting... ", end='')

    closest = np.argsort(dists, axis=0)

    if verbose:
        print("Computing predictions...", end='')

    targets = train_Y[closest]

    for k in ks:
        predictions = sstats.mode(targets[:k])[0]
        predictions = predictions.ravel()
        preds[k] = predictions
    if verbose:
        print("Done")
    return preds


class Solution:
    def spellchecker(self, wordlist, queries):
        def replace_vowel(word):
            return "".join("*" if c in "aeiou" else c for c in word)
        perfect_match = set(wordlist)
        case_error, vowel_error = dict(), dict()
        for word in wordlist:
            word2 = word.lower()
            case_error.setdefault(word2, word)
            vowel_error.setdefault(replace_vowel(word2), word)

        def solve(query):
            if query in perfect_match:
                return query
            lower_query = query.lower()
            if lower_query in case_error:
                return case_error[lower_query]
            vowel_query = replace_vowel(lower_query)
            if vowel_query in vowel_error:
                return vowel_error[vowel_query]
            return ""
        return map(solve, queries)


import unittest
from mosaicode.GUI.fields.intfield import IntField

class TestIntField(unittest.TestCase):

    def setUp(self):
        IntField(None, None)
        self.field = IntField({"label": "test", "value": 1}, None)
        self.field = IntField({"label": "test", "value": 0}, None)
        self.field = IntField({}, self.test_value)

    def test_value(self):
        value1 = 0
        self.field.set_value(value1)
        value2 = self.field.get_value()
        self.assertEqual(value1, value2, 'incorrect value')
        value1 = 100
        self.field.set_value(value1)
        value2 = self.field.get_value()
        self.assertEqual(value1, value2, 'incorrect value')


import sys
import math
import random
import time

class Zone:
    instances = {}
    def __init__(self, ID, platinum):
        self.ID = ID
        self.platinum = platinum
        self.instances[ID] = self
        self.link = []
        self.owner = None
        self.pods = []  # pods per team
        self.continent = -1

    @property
    def is_bottleneck(self):
        return len(self.link) < 2

    def add_link(self, other_area):
        self.link.append(other_area)

    def __repr__(self):
        return "Zone {} - Platinum: {}".format(self.ID, self.platinum)

class Player:
    def __init__(self):
        self.pods = {}
        self.zones = []
        self.platinum = 200
        self.target_area = []

    @property
    def score(self):
        return len(self.zones)

    def show(self):
        print("PODS", file=sys.stderr)
        for key, value in self.pods.items():
            print(key, value, file=sys.stderr)

    def evaluate_moves(self, zone, nb_pods=1):
        score = {}
        if len(zone.link) == 0:
            print(zone.ID, file=sys.stderr)
        for neighbor in zone.link:
            if neighbor.owner == -1:
                score[neighbor] = 1
            elif neighbor.owner not in [-1, my_id]:
                other_pods = neighbor.pods
                if sum(other_pods) == 0:
                    score[neighbor] = 2
                if nb_pods > max(other_pods):
                    score[neighbor] = 5
            else:
                score[neighbor] = 0
        return score

    def pick_move(self, moves):
        if len(moves) == 0: # si on est bloqué dans un coin avec un pion a coté
            dest = -1
        elif sum(moves.values()) == 0: # on a que des moves dans la zone a nous
            dest = random.choice(list(moves.keys()))
        else:
            dest = max(moves.items(), key = lambda x: x[1])[0]
        return dest

    def expand(self):
        move_list = []
        for zone, nb_pods in self.pods.items():
            moves = self.evaluate_moves(zone, nb_pods)
            dest = self.pick_move(moves)
            if dest != -1:
                move_list += [nb_pods, zone.ID, dest.ID]

        if len(move_list) > 0:
            move_list = map(str, move_list)
            print(" ".join(move_list))
        else:
            print("WAIT")

    def get_number_of_pods_craftable(self):
        return me.platinum//20

    def check_best_positions(self):
        possible_spawn = filter(lambda x: x.owner == -1, self.target_area)
        best_areas = sorted(possible_spawn, key=lambda x: x.platinum, reverse=True)
        return best_areas

    def check_my_areas_close_to_enemies(self):
        my_areas = filter(lambda x: x.owner == my_id, self.target_area)
        best_spawn = 0
        best_score = 0
        for each_area in my_areas:
            moves = self.evaluate_moves(each_area)
            score = sum(moves.values())
            if score > best_score:
                score = best_score
                best_spawn = each_area
        return best_spawn

    def spawn(self):
        nb_pods = self.get_number_of_pods_craftable()
        result = []
        if nb_pods > 0:
            best_areas = self.check_best_positions()
            if len(best_areas) > 0:
                if len(best_areas) >= nb_pods:
                    for i in range(nb_pods):
                        area = best_areas.pop(0)
                        result += [1, area.ID]
                elif len(best_areas) < nb_pods:
                    qte_list = share(nb_pods, len(best_areas))
                    for i in range(len(best_areas)):
                        area = best_areas.pop(0)
                        qte = qte_list.pop(0)
                        result += [qte, area.ID]
            else:
                spawn = self.check_my_areas_close_to_enemies()
                if spawn != 0:
                    result += [nb_pods, spawn.ID]

        if len(result) > 0:
            result = map(str, result)
            print(" ".join(result))
        else:
            print("WAIT")

    # def first_turn(self):
    #     self.first_turn2()
    #     result = []
    #     best_areas = sorted(Zone.instances.values(), key = lambda x : x.platinum, reverse = True)
    #     for i in range(10):
    #         area = best_areas.pop(0)
    #         result += [1, area.ID]
    #     result = map(str, result)
    #     print("WAIT")
    #     print(" ".join(result))

    def first_turn(self):
        best_areas = []
        result = []
        for z in self.target_area:
            if z.is_bottleneck:
                best_areas.append(z)
        best_areas.sort(key = lambda x : x.platinum)
        qte_list = share(10, len(best_areas))
        for i in range(len(best_areas)):
            area = best_areas.pop(0)
            qte = qte_list.pop(0)
            result += [qte, area.ID]
        result = map(str, result)
        print("WAIT")
        print(" ".join(result))

    def play(self):
        self.expand()
        self.spawn()

def share(n_elem, size):
    shared = [0] * size
    for i in range(size):
        qte = n_elem // (size-i)
        shared[i] = qte
        n_elem -= qte
    return shared

# player_count: the amount of players (2 to 4)
# my_id: my player ID (0, 1, 2 or 3)
# zone_count: the amount of zones on the map
# link_count: the amount of links between all zones
player_count, my_id, zone_count, link_count = [int(i) for i in input().split()]
for i in range(zone_count):
    # zone_id: this zone's ID (between 0 and zoneCount-1)
    # platinum_source: the amount of Platinum this zone can provide per game turn
    zone_id, platinum_source = [int(j) for j in input().split()]
    Zone(zone_id, platinum_source)

for i in range(link_count):
    zone_1, zone_2 = [int(j) for j in input().split()]
    z1 = Zone.instances[zone_1]
    z2 = Zone.instances[zone_2]
    z1.add_link(z2)
    z2.add_link(z1)
    if zone_1 in [0, 18] or zone_2 in [0, 18]:
        print(zone_1, zone_2, file=sys.stderr)

print(Zone.instances[0].link, file=sys.stderr)
print(Zone.instances[18].link, file=sys.stderr)

# Define all continents:
visited = []
continent_ID = 0
for zone in Zone.instances.values():
    if zone not in visited:
        continent_ID += 1
        visited.append(zone)
        zone.continent = continent_ID
        queue = zone.link[:]
        while len(queue) > 0:
            z = queue.pop(0)
            visited.append(z)
            z.continent = continent_ID
            queue += [x for x in z.link if x not in visited]
print(continent_ID, file=sys.stderr)

me = Player()

world = {}
for i in range(1, 6):
    continent = list(filter(lambda x:x.continent == i, Zone.instances.values()))
    size = len(continent)
    if size == 3:
        world["japon"] = continent
    elif size == 7:
        world["antarctic"] = continent
    elif size == 33:
        world["morth_america"] = continent
    elif size == 44:
        world["africa"] = continent
    elif size == 67:
        world["eurasia"] = continent

if player_count > 2:
    target_1 = world["eurasia"] + world["antarctic"] + world["japon"]
    target_2 = world["africa"] + world["morth_america"]
    s1 = sum([x.platinum for x in target_1])
    s2 = sum([x.platinum for x in target_2])
    if s1 >= s2:
        me.target_area = target_1
    else:
        me.target_area = target_2
else:
    me.target_area = Zone.instances.values()

turn = 0
while True:
    platinum = int(input())  # my available Platinum
    me.platinum = platinum
    me.zones = []
    me.pods = {}
    for i in range(zone_count):
        # z_id: this zone's ID
        # owner_id: the player who owns this zone (-1 otherwise)
        # pods_p0: player 0's PODs on this zone
        # pods_p1: player 1's PODs on this zone
        # pods_p2: player 2's PODs on this zone (always 0 for a two player game)
        # pods_p3: player 3's PODs on this zone (always 0 for a two or three player game)
        z_id, owner_id, *pods = [int(j) for j in input().split()]
        zone = Zone.instances[z_id]
        zone.owner = owner_id
        zone.pods = pods
        if owner_id == my_id:
            me.zones.append(zone)
        if pods[my_id] > 0:
            me.pods[zone] = pods[my_id]

    # print(me.pods, file=sys.stderr)

    if turn == 0:
        me.first_turn()
    else:
        me.play()

    turn += 7


import torch

from unet import UNetModel

if __name__ == "__main__":

    """
        Test the UNetModel class.  Please uncomment the print statements in the
        UNetModel class to see the output of the forward pass.
    """

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    unet_model = UNetModel(
        image_size=512,
        in_channels=4,
        model_channels=16,
        out_channels=4,
        num_res_blocks=1,
        attention_resolutions=[],
        use_bottleneck_attn=True,
        channel_mult=(1, 1, 2, 2),
        conv_resample=True,
        dims=3,
        num_classes=None,
        use_checkpoint=True,
        num_heads=1,
        num_head_channels=-1,
        use_scale_shift_norm=True,
        resblock_updown=False,
        use_new_attention_order=True,
    ).to(device)
    print("Model has been created ...")

    print(f"currently using device: {next(unet_model.parameters()).device}")

    batch_size = 6
    random_input = torch.randn(
        batch_size, 4, 128, 128, 64, dtype=torch.float32, device=device
    )
    random_timesteps = torch.randint(
        low=0, high=100, size=(batch_size,), dtype=torch.int32, device=device
    )
    output = unet_model(random_input, random_timesteps)

    # testing backward pass:
    output.sum().backward()
    print("Backward pass has been completed ...")


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import xgboost as xgb
from  sklearn.preprocessing import LabelEncoder
lbl=LabelEncoder()
import time

train_set=pd.read_csv('input/round1_ijcai_18_train_20180301/round1_ijcai_18_train_20180301.txt',sep=' ')
test_set=pd.read_csv('input/round1_ijcai_18_test_a_20180301/round1_ijcai_18_test_a_20180301.txt',sep=' ')

def output_image(train_set):

    plt.grid(True)

    for col_name in train_set.columns:
        if train_set[col_name].dtype!='float64':
            fig = plt.figure(figsize=(15, 15), dpi=100)
            trade_rate = train_set.groupby(col_name)['is_trade'].mean()
            print("----------------------------------------")
            print("column", col_name, "trade rate is:")
            print(trade_rate.describe())
            print("----------------------------------------")
            print("column", col_name, "influce var is:")
            print(trade_rate.var())
            print("#####################################################")

            trade_rate = train_set.groupby(col_name)['is_trade'].mean()

            trade_rate.plot(kind='bar', color='g')
            plt.savefig('output/' + col_name + '.jpg', dpi=100, figsize=(20, 20))
            plt.close()

train_set['source']='train'
test_set['source']='test'
train_df=pd.concat([train_set,test_set],ignore_index=True)
m,n=train_df.shape

#缺失值分析
miss_count=train_df.apply(lambda x:sum(x==-1))
miss_count.plot(kind='bar')
miss_rate=(miss_count/m)

miss_rate.plot(kind='bar')
plt.show()

#特征分析

category_var=['context_page_id','item_brand_id','item_city_id','item_price_level','item_sales_level','item_collected_level'\
              ,'item_pv_level','user_gender_id','user_age_level','user_occupation_id','user_star_level','shop_review_num_level',
              'shop_star_level']
for v in category_var:
    print (v,'不同取值出现的次数：')
    print (train_df[v].value_counts())

category_var=['context_page_id','item_brand_id','item_city_id','item_price_level','item_sales_level','item_collected_level'\
              ,'item_pv_level','user_gender_id','user_age_level','user_occupation_id','user_star_level','shop_review_num_level',
              'shop_star_level']
for v in category_var:
    print ('*************************************')
    print (v,'成交率：')
    print (train_df.groupby(v)['is_trade'].mean())
    print ('成交率的方差为：')
    print (train_df.groupby(v)['is_trade'].mean().var())

#特征处理

# 缺失值（除了性别全用众数顶上）
category_var=['context_page_id','item_brand_id','item_city_id','item_price_level','item_sales_level','item_collected_level'\
              ,'item_pv_level','user_gender_id','user_age_level','user_occupation_id','user_star_level','shop_review_num_level',
              'shop_star_level']
for v in category_var:
    top_num=train_df[v].value_counts()[:1].values[0]
    train_df[v][train_df[v]==-1]=top_num

#组合特征
def connact(x,y):

    return str(x)+'_'+str(y)

user_shop=[]
for i in range(len(train_df)):
    user_shop.append(connact(train_df.shop_id.values[i],train_df.user_id.values[i]))

train_df['user_shop']=user_shop

train_df.pivot_table()

#把数量少的类目全都归位一类low_level
category_var=['item_brand_id','item_city_id','item_price_level','item_sales_level','item_collected_level'\
              ,'item_pv_level','user_age_level','user_occupation_id','user_star_level','shop_review_num_level','shop_star_level']
for v in category_var:
        value_counts = train_df[v].value_counts()/m
        be_replace = ((value_counts[value_counts < 0.005])).index.tolist()
        if len(be_replace) > 0:
            train_df[v] = train_df[v].replace(be_replace, be_replace[0])

# 类目编码
category_var=['context_page_id','item_brand_id','item_city_id','item_price_level','item_sales_level','item_collected_level'\
              ,'item_pv_level','user_gender_id','user_age_level','user_occupation_id','user_star_level','shop_review_num_level','shop_star_level']
for v in category_var:
    train_df[v]=lbl.fit_transform(train_df[v])

# list变量处理
list_var=['item_category_list','item_property_list','predict_category_property']

#TODO 排序处理
new_list_var=[]
#取前0-9位置数据的数作为一个编号属性
for i in range(10):
    new_list_var.append('item_property_list' + str(i))
    train_df['item_property_list' + str(i)] = lbl.fit_transform(train_df['item_property_list'].map(
        lambda x: str(str(x).split(';')[i]) if len(str(x).split(';')) > i else ''))

for i in range(5):
    new_list_var.append('predict_category_property' + str(i))
    train_df['predict_category_property' + str(i)] = lbl.fit_transform(train_df['predict_category_property'].map(
        lambda x: str(str(x).split(';')[i]) if len(str(x).split(';')) > i else ''))

# 取前1-2位置数据的数作为一个编号属性
for i in range(1, 3):
    new_list_var.append('item_category_list' + str(i))
    train_df['item_category_list' + str(i)] = lbl.fit_transform(train_df['item_category_list'].map(
        lambda x: str(str(x).split(';')[i]) if len(str(x).split(';')) > i else ''))  # item_category_list的第0列全部都一样
del train_df['item_property_list']
del train_df['predict_category_property']
del train_df['item_category_list']

# 处理时间戳
print('process time ...')
format = '%Y-%m-%d %H:%M:%S'
date_time = train_df.context_timestamp.apply(lambda x: pd.to_datetime(time.strftime(format, time.localtime(x))))
train_df['day'] = date_time.dt.day
train_df['hour'] = date_time.dt.hour
#是否是工作时间
#train_df['is_work']=train_df['hour'].apply(lambda x:1 if x>18 else 0)
#处理一下
train_df['week'] = date_time.dt.dayofweek
#是否周末
#train_df['is_sunday']=train_df['week'].apply(lambda x: 1 if x>4 else 0)
del train_df['context_timestamp']
print ('time finished')

#删除id 列
train_df[train_df.source=='test'].instance_id.to_csv('output/submit_ids_csv',index=None)
del train_df['context_id']
del train_df['instance_id']
del train_df['item_id']
del train_df['user_id']
del train_df['shop_id']
del train_df['date_time']

#特征输出

train_output=train_df[train_df['source']=='train']
test_output=train_df[train_df['source']=='test']

del train_output['source']
del test_output['source']
output_image(train_output)
del test_output['is_trade']
train_output.to_csv('output/train_set.csv',index=None)
test_output.to_csv('output/test_set.csv',index=None)


def find(s):
    global visited, G
    if s == G:
        return 0

    Q = []
    Q.append(s)
    visited[s] = 1
    while Q:
        s = Q.pop(0)
        for i in range(V+1):
            if temp[s][i] == 1 and visited[i] == 0:
                Q.append(i)
                visited[i] = visited[s] + 1
    if visited[G] == 0:
        return 0
    else:
        return visited[G] - 1

import sys
sys.stdin = open('노드의_거리_input.txt', 'r')

for tc in range(int(input())):
    V, E = map(int, input().split())
    data = [list(map(int, input().split())) for _ in range(E)]
    S, G = map(int, input().split())

    # 노드입력
    temp = [[0]*(V+1) for _ in range(V+1)]

    for i in data:
        temp[i[0]][i[1]] = 1
        temp[i[1]][i[0]] = 1
    # 방문
    visited = [0]*(V+1)

    print(f'#{tc+1} {find(S)}')


from django.urls import path

from .views import (ArticleView, CategoryView, ConstellationView, GroupView,
                    RegionView, SystemView, UniverseView)

urlpatterns = [
    path("article/<int:pk>/", ArticleView.as_view(), name="article"),
    path("category/<int:pk>/", CategoryView.as_view(), name="category"),
    path("group/<int:pk>/", GroupView.as_view(), name="group"),
    path("system/<int:pk>/", SystemView.as_view(), name="system"),
    path("constellation/<int:pk>/", ConstellationView.as_view(), name="constellation"),
    path("region/<int:pk>/", RegionView.as_view(), name="region"),
    path("", UniverseView.as_view(), name="universe"),
]


from urllib.request import Request, urlopen
import pandas as pd
from lxml.html import fromstring

from datetime import datetime
from dateutil.relativedelta import relativedelta, TH
import time

import os.path

def cleanstr(string):

    try:
        floatnum = float(string.replace(',', ''))
    except:
        floatnum = 0
    return floatnum

def optionchain(exdate = '30JAN2020'):
    """
    Actual scraping done here
    """
    ocurl = "http://www1.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?segmentLink=17&instrument=OPTIDX&symbol=NIFTY&date="+exdate
    headers = {'Accept' : '*/*',
               'Accept-Language' : 'en-US,en;q=0.5',
               'Host': 'www1.nseindia.com',
               'Referer': 'http://www1.nseindia.com/live_market/dynaContent/live_market.htm',
               'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1;WOW64;rv:54.0) Gecko Firefox/54',
               'X-Requested-With': 'XMLHttpRequest'}

    req = Request(ocurl, None, headers)
    response = urlopen(req)
    the_page = response.read() # works upto this; got the page in a string
    ptree = fromstring(the_page)
    tr_nodes = ptree.xpath('//table[@id="octable"]//tr')[1:]
    td_content = [[td.text_content().strip() for td in tr.xpath('td')] for tr in tr_nodes[1:]]
    td_content = [[cleanstr(string) for string in sublist] for sublist in td_content]
    return td_content[:-1]

def getLastThu(date = None):

    """
    Either keep the parameters empty which will get last thursday of current month
    else pass a date in the DDMMMYYYY format
    """
    if date is None:
        todayte = datetime.today()
    elif type(date) == str:
        todayte = datetime.strptime(date,"%d%b%Y")
    elif type(date) == datetime:
        todayte = date
    else:
        raise ValueError('badly formatted date input')

    cmon = todayte.month

    for i in range(1, 6):
        nthu = todayte + relativedelta(weekday=TH(i))
        if nthu.month != cmon:
            # since t is exceeded we need last one  which we can get by subtracting -2 since it is already a Thursday.
            nthu += relativedelta(weekday=TH(-2))
            break
    if nthu.day < todayte.day:
        timid = todayte + relativedelta(weekday=TH(i+1))
        nthu = getLastThu(timid)
        return nthu

    return nthu.strftime('%d%b%Y').upper()

def get_ocdata(exdate = '30JAN2020', start = 9*60*60, stop = (15*60+30)*60,
               scrapeGap = 5, waitflag = True, nosleep = False, logfile="opt.log"):
    """
    start: Market open time/when the data scraping starts, in seconds elapsed from 12:00 AM
    stop:  Market closing time/when the data scraping stops, in seconds elapsed from 12:00 AM
    waitflag: whether to wait for the time specified in 'start' variable to occur, sleep till then
    nosleep: whether to scrape continually or give 1 sec pause between scrapes,
             will sleep if set to True if scraped before 1 sec interval
             if scrapes take more than 1 sec has no effect
             good to use if too much data traffic/network overloaded
    """

    currtime = time.localtime()
    dct = time.strftime('%d%b%Y', currtime).upper()
    datafile = 'nseOC' + dct + '.csv'
    csvfile = 'OCdata/' + datafile

    if not os.path.exists('OCdata/'):
        os.makedirs('OCdata/')

    a = ["OI", "changeOI", "volume", "IV", "LTP", "netChange"]
    b = ["bidQ", "bidP", "askP", "askQ"]
    hdr = [s + '.call' for s in a+b] + ["strike"] + [s + '.put' for s in b + list(reversed(a))] + ["timestamp"]

    if not os.path.isfile(csvfile):
        dfcolnames = pd.DataFrame(hdr)
        dfcolnames = dfcolnames.T
        dfcolnames.to_csv(csvfile, index=False, mode='a', header=False)

    ptime = datetime.now()

    if waitflag == True:
        secs = (ptime.hour*60*60 + ptime.minute*60 + ptime.second)
        if secs < start:
            time.sleep(start - secs)

    while True:
        if (ptime.hour*60*60 + ptime.minute*60 + ptime.second) > stop:
            break
        else:
            try:
                ptime = datetime.now()
                scrapedata = pd.DataFrame(optionchain(exdate))
                scrapedata = scrapedata.iloc[:, 1:22]
                scrapedata['timestamp'] = ptime
                scrapedata.to_csv(csvfile, index=False, mode='a', header=False)

                if not nosleep:
                    end = datetime.now()
                    tdiff = end-ptime

                    tslip = scrapeGap - (tdiff.seconds + tdiff.microseconds*1.0/10**6)
                    if tslip > 0:
                        time.sleep(tslip)
            except Exception as e:
                with open("idx.log", "a") as lf:
                    lf.write("Error at {0} \t {1}\n".format(str(ptime), str(e)))
                time.sleep(scrapeGap)
            continue
    return 0

get_ocdata(exdate = getLastThu(), stop = (15*60+45)*60, scrapeGap = 5, nosleep = False, logfile="opt.log")


from __future__ import print_function

import os
import sys
import util
from theano.compat.six.moves import xrange
from pylearn2.datasets.dense_design_matrix import DenseDesignMatrix, DefaultViewConverter
from pylearn2.utils import serial
from theano import tensor as T
from theano import function
import cPickle as pkl
import numpy as np
from scipy.stats import mode

def usage():
    print("""usage: python make_submission.py model.pkl submission.csv)
Where model.pkl contains a list of trained pylearn2.models.mlp.MLP objects.
The script will make submission.csv, which you may then upload to the
kaggle site.""")

if len(sys.argv) != 3:
    usage()
    print("(You used the wrong # of arguments)")
    quit(-1)

_, model_path, out_path = sys.argv

if os.path.exists(out_path):
    usage()
    print(out_path+" already exists, and I don't want to overwrite anything just to be safe.")
    quit(-1)

models = []
try:
    for model in serial.load(model_path):
        models.append(model)
except Exception as e:
    usage()
    print(model_path + "doesn't seem to be a valid model path, I got this error when trying to load it: ")
    print(e)

# load the test set
with open('preprocessed_test_for_pylearn2.pkl') as f:
    dataset = pkl.load(f)
dataset = DenseDesignMatrix(X=dataset, view_converter=DefaultViewConverter(shape=[32, 32, 1], axes=['b', 0, 1, 'c']))

print(models)
predictions = []
print(len(models))

for model in models:

    print(model)

    model.set_batch_size(dataset.X.shape[0])

    X = model.get_input_space().make_batch_theano()
    Y = model.fprop(X) # forward prop the test data

    y = T.argmax(Y, axis=1)

    f = function([X], y)

    x_arg = dataset.get_topological_view()
    y = f(x_arg.astype(X.dtype))

    assert y.ndim == 1
    assert y.shape[0] == dataset.X.shape[0]

    # add one to the results!
    y += 1

    predictions.append(y)
    print(y)

predictions = np.array(predictions, dtype='int32')

y = mode(predictions.T, axis=1)[0]
y = np.array(y, dtype='int32')

import itertools
y = list(itertools.chain(*y))

assert len(y) == dataset.X.shape[0]
print(type(y[0]))
print(type(y))

util.write_results(y, out_path)


"""
Runtime: 36 ms, faster than 66.61% of Python3 online submissions for Reverse Linked List.
Memory Usage: 15.7 MB, less than 48.63% of Python3 online submissions for Reverse Linked List.
"""
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        on = head
        prevNode = None
        while on:
            nextNode = on.next
            on.next = prevNode
            prevNode = on
            on = nextNode
        return prevNode


import streamlit as st
import pandas as pd
import plotly.express as px

st. set_page_config(page_title="२०७९ चुनाव उम्मेदवार",page_icon=":bar_chart:", layout="wide")

@st.cache
def get_data_from_excel():
    df = pd.read_excel(io='ALL_CANDIDATE_PA_2079_06_26_19_00.xlsx', engine='openpyxl', sheet_name='Eng_Data', skiprows=0, usecols="B:I")
    return df

df= get_data_from_excel()

# st.dataframe(df) # shows the complete dataset

#------------------_SIDEBAR-----------------_#
st.sidebar.header("(Choose)छान्नुहोस्")

province = st.sidebar.multiselect(
    "(Province)प्रदेश छान्नुहोस्",
    options=df["Province"].unique(),
    default=df["Province"].unique()
)

province_sel = df.query(
    "Province == @province"
)

# st.dataframe(province_sel) # displays dataframe for selected province

district = st.sidebar.multiselect(
    "(District)जिल्ला छान्नुहोस्",
    options=province_sel["District"].unique(),
    default=province_sel["District"].unique()
)

district_sel = df.query(
    "District == @district"
)

# st.dataframe(district_sel)  # displays dataframe for selected province and districts

party = st.sidebar.multiselect(
    "(Party)पार्टी छान्नुहोस्",
    options=district_sel["Party"].unique(),
    default=district_sel["Party"].unique()
)

party_sel = df.query(
    "Party == @party & District == @district"
)

# st.dataframe(party_sel) # displays dataframe for selected province, districts and party

# gender filter
gender = st.sidebar.multiselect(
    "(Gender)लिङ्ग छान्नुहोस्",
    options=district_sel["Gender"].unique(),
    default=district_sel["Gender"].unique()
)

gender_sel = df.query(
    "Gender == @gender & District == @district & Party == @party"
)

# st.dataframe(district_sel)

#--------------------------MAINPAGE-------------------__#

st.title(":flag-np:प्रदेश सभा सदस्य निर्वाचन तर्फको उम्मेदवारीको विवरण ")
st.markdown('##')

# Analytics
total_candidates = district_sel.shape[0]
total_independent = len(district_sel[district_sel["Party"]=="स्वतन्त्र"])
# # total_female = gender_sel["महिला"].sum()

left_column, right_column = st.columns(2)

with left_column:
    st.subheader("जम्मा उम्मेदवार")
    st.subheader(total_candidates)

with right_column:
    st.subheader("जम्मा स्वतन्त्र उम्मेदवार")
    st.subheader(total_independent)

st.markdown("---")

st.markdown("## उम्मेदवारहरुको तालिका ")
st.dataframe(gender_sel)

#--------------------Analytics--------##

candidates_by_gender = (
    gender_sel.groupby(by=["Gender"]).count()[['Party']]

)

fig_gender_bar = px.bar(
    candidates_by_gender,
    x= "Party",
    y=candidates_by_gender.index,
    labels={
        "Party": "जम्मा(Total)",
        "Gender": "लिङ्ग",
        },
    orientation='h',
    title="लिङ्गको आधारमा उम्मेदवारहरु",
    template= "plotly_white",
)

fig_gender_bar.update_layout(
    plot_bgcolor= "rgba(0,0,0,0)",
    xaxis = (dict(showgrid=False))
)

st.plotly_chart(fig_gender_bar)

#-------------------candidates by party

candidates_by_party = (
    gender_sel.groupby(by=["Party"]).count()[['Age']]
)

fig_party_bar = px.bar(
    candidates_by_party,
    x= candidates_by_party.index,
    y= "Age",
    labels={
        "Age": "जम्मा(Total)",
        "Party": "पार्टी",
        },
    title="पार्टीको आधारमा उम्मेदवारहरु",
    template= "plotly_white",
)

fig_party_bar.update_layout(
    plot_bgcolor= "rgba(0,0,0,0)",
    xaxis = (dict(showgrid=False))
)

st.plotly_chart(fig_party_bar)

st.markdown("Made for Educational Purpose Only. Based on data provided by [Election Commission Nepal.](https://election.gov.np/np/page/candidate-list-hor)")

#--- hide streamlit style

hide_st_style= """

"""

st.markdown(hide_st_style, unsafe_allow_html=True)


from flask import Flask,render_template,request

app = Flask(__name__)

@app.route('/',methods=['GET','POST'])
def index():
    DATA = {
                "0": {
                    "question": "Which one is correct team name in NBA?",
                    "options": ["New York Bulls","Los Angeles Kings","Golden State Warriros","Huston Rocket"],
                    "answer": "Huston Rocket"},
                "1": {
                    "question": "5 + 7 = ?",
                    "options": ["10","11","12","13"],
                    "answer": "12"
                },
                "2": {
                    "question": "12 - 8 = ?",
                    "options": ["1","2","3","4"]
                    ,"answer": "4"}}

    return  render_template('index.html',quiz = DATA)

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


import streamlit as st
from datetime import datetime
import models.Colaborador as colaborador
import database as db

def pagina_cadastro_colab():
    st.title("Cadastrar Colcaborador")

    with st.form(key="include_colaborador"):
        input_nome = st.text_input(label="Insira seu nome")
        input_botao_cadastrar_colab = st.form_submit_button("Cadastrar colaborador")
        input_botao_exibe_colabs = st.form_submit_button("Exibir colaboradores")
        input_botao_deleta_colabs = st.form_submit_button("Remover colaborador")

    if input_botao_cadastrar_colab:
        colaborador.nome = input_nome
        db.cadastrarColab(input_nome)

# fazer exibir e deletar funcionar
    if(input_botao_exibe_colabs):
       db.exibirColabs()

    if(input_botao_deleta_colabs):
        db.removerColab(input_nome)

pagina_selecionada = st.sidebar.radio("Navegação", ["Cadastrar Colaborador", "Lista de Tarefas"])

if pagina_selecionada == "Cadastrar Colaborador":
    pagina_cadastro_colab()

def pagina_lista_tarefa():
    st.title("Lista de tarefas")

    with st.form(key="include_tarefa"):
        input_nome_tarefa = st.text_input(label="Descreva a terefa")
        input_hora = st.time_input("Selecione uma hora", datetime.now().time())
        input_colaborador = st.selectbox("Colaborador", ["Sarah", "Sabrina", "Samuel"])
        input_botao_tarefa = st.form_submit_button("Cadastrar Tarefa")

        if input_botao_tarefa:
            st.write(f'Descrição Tarefa: {input_nome_tarefa}')
            st.write(f'Horário: {input_hora}')
            st.write(f'Colaborador: {input_colaborador}')

if pagina_selecionada == "Lista de Tarefas":
    pagina_lista_tarefa()


'''
WE GONNA IMPLEMENT IT
               2
             /   \
           7      9
         /  \      \
        1    6      8

'''

class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None

    @staticmethod
    def create_tree():
        node_two = Node(2)
        node_seven = Node(7)
        node_nine = Node(9)
        node_six = Node(6)
        node_eight = Node(8)
        node_one = Node(1)

        #  add child to root
        node_two.add_left_child(node_seven)
        node_two.add_right_child(node_nine)

        node_seven.add_left_child(node_one)
        node_seven.add_right_child(node_six)

        node_nine.add_right_child(node_eight)

        return node_two

    @staticmethod
    def print_all_degree(node_two):
        print(f"root {node_two.data}")
        print(f"Left of root : {node_two.data} is {node_two.left.data}")
        print(f"Right of root : {node_two.data} is {node_two.right.data}")

        print(f"Left of : {node_two.left.data} is {node_two.left.left.data}")
        print(f"Right of : {node_two.left.data} is {node_two.left.right.data}")

        print(f"Right of : {node_two.right.data} is {node_two.right.right.data}")

    def add_left_child(self, child):
        self.left = child

    def add_right_child(self, child):
        self.right = child

    def pre_order(self, node):
        print(node.data)

        if node.left is not None:
            self.pre_order(node.left)

        if node.right is not None:
            self.pre_order(node.right)

    def post_order(self, node):

        if node.left is not None:
            self.post_order(node.left)

        if node.right is not None:
            self.post_order(node.right)

        print(node.data)

    def in_order(self, node):

        if node.left is not None:
            self.in_order(node.left)
        print(node.data)

        if node.right is not None:
            self.in_order(node.right)


"""
Joins graphs in one
"""
import json

files_path = json.load(open("all_files.json"))

def count_graph(adj_list):
    vertices_count = 0
    edges_count = 0
    for vertice, neighbors in adj_list.iteritems():
        vertices_count += 1
        edges_count += len(neighbors.keys())
    return (vertices_count, edges_count)

data = []
adj_list = {}
for graph_file_path in files_path:
    first_line = True
    with open(graph_file_path) as graph_file:
        for line in graph_file:
            if not first_line:
                v_i, v_j, e_w = line.split(",")
                if v_i not in adj_list:
                    adj_list[v_i] = {}
                adj_list[v_i][v_j] = e_w
        data.append(count_graph(adj_list))

with open("graph_cum.json", "w+") as graph_cum_file:
    json.dump(data, graph_cum_file)

with open("total_graph.json", "w+") as total_graph:
    json.dump(adj_list, total_graph)


def solution(s, skip, index):
    answer = ''

    english = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
               'v', 'w', 'x', 'y', 'z']
    for a in s:
        idx = english.index(a)

        shift = []
        while len(shift) < index:
            idx += 1
            idx %= 26
            if english[idx] in skip:
                continue
            else:
                shift.append(english[idx])

        answer += shift[-1]

    return answer

print(solution("aukks", "wbqd", 5))

알고리즘/프로그래머스/둘만의 암호.py


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

from django.shortcuts import render,redirect
from .models import *
from django.core.mail import send_mail
from django.contrib.auth.decorators import login_required,user_passes_test
import smtplib
import datetime

# Create your views here.

def index(request):
	response = {}
	response["components"] = Component.objects.all()
	return render(request, 'hardware/index.html', response)

def buynow(request):
	response = {}
	if request.method == "POST":
		product_id = request.POST["id"]
		component = Component.objects.get(pk=product_id)
		print(component)
		user = request.user
		message = str(component.name) + '\n'
		message+=str(component.price)
		message+=str(user.username) + " has taken the above component on "
		message+=str(datetime.datetime.now())
		sender = "chsaiteja@student.nitw.ac.in"
		receiver = 'ch1sai2teja3@gmail.com'
		rlist = []
		rlist.append(receiver)
		send_mail('IG Connect Hardware Shop',message,sender,rlist,fail_silently=False,)
		response["component"] = component

	return render(request, "hardware/buynow.html", response)

def lend(request):
	response = {}
	if request.method == "POST":
		product_id = request.POST["id"]
		component = Component.objects.get(pk=product_id)
		user = request.user
		lendComp = ComponentLending()
		lendComp.user = user
		lendComp.component = component
		lendComp.dateTaken = datetime.datetime.now()
		lendComp.save()
		response["component"] = component
	return render(request, "hardware/lend.html", response)

@login_required(login_url='/auth/login')
def lendedComponents(request):
	response = {}
	lended_comp = ComponentLending.objects.filter(user=request.user)
	dates = []
	for comp in lended_comp:
		r = {}
		days = datetime.datetime.now().date() - comp.dateTaken
		days = days.total_seconds()/60/60/24
		days = int(days)
		dates.append(r)
		r["days"] = days
	response["dates"] = dates
	response["lended_comp"] = lended_comp
	response["zipped"] = zip(dates, lended_comp)
	return render(request, "hardware/lended.html", response)

def addItem(request):
	response = {}
	if request.method == "POST":
		component = Component()
		component.name = request.POST["name"]
		component.available = True
		component.price = int(request.POST["price"])
		component.save()
		response["message"] = "Item added succesfully"
	return render(request, 'hardware/addItem.html', response)


# -*- coding: utf-8 -*-
"""
Created on Mon Oct 17 20:10:35 2016
@author: robintiman
"""
import numpy
from mpi4py import MPI
comm = MPI.COMM_WORLD

'''
The rank is the process id.

Send and Recv are blocking functions, meaning that when calling Recv the program
will be idle until a message is recieved. There is Isend and Irecv for immediate
execution.

If the sender is unknown, ANY_SOURCE can be used.

'''

rank = comm.Get_rank()
rankF = numpy.array(float(rank))
total = numpy.zeros(1)
comm.Reduce(rankF,total, op=MPI.SUM)
if rank == 0:
    print(total)
print(rank, rankF, total)


from api_v1.models import PostImage
from rest_framework import serializers
from base64 import b64decode

BASE64_STR = ';base64,'

EXPECT_IMAGE_EXTENSION_LIST = ['jpg', 'png', 'jpeg']

class ImageSerializer(serializers.ModelSerializer):
    image_content = serializers.CharField(
        write_only=True, allow_null=False)
    reg_datetime = serializers.DateTimeField(
        format="%Y-%m-%d %H:%M:%S", required=False)

    class Meta:
        model = PostImage
        fields = (
            'image_url',
            'image_content',
            'image_size',
            'image_origin_name',
            'image_extension',
            'reg_datetime',
        )
        read_only_fields = ['image_url']

    def validate(self, data: dict):
        if BASE64_STR not in data['image_content']:
            raise serializers.ValidationError('無効なデータです。')

        ext, base64_encode_str = data['image_content'].split(BASE64_STR)
        extension = ext.split('/')[-1]
        if(extension not in EXPECT_IMAGE_EXTENSION_LIST):
            raise serializers.ValidationError(
                '無効な拡張子です。')

        try:
            image_data = b64decode(s=base64_encode_str)
        except BaseException as be:
            raise serializers.ValidationError(
                '画像のデコードに失敗しました。') from be
        data['image_content'] = image_data
        data['image_extension'] = extension
        data['image_size'] = len(image_data)
        return data

    def create(self, validated_data: dict):
        image_data = validated_data.pop('image_content')

        post_image = PostImage(**validated_data)

        post_image.save(image=image_data)

        return post_image


# File: getters/deprivation_data.py
"""Loading deprivation data for differnet countries."""

# ---------------------------------------------------------------------------------

from heat_pump_adoption_modelling import PROJECT_DIR, get_yaml_config, Path
import pandas as pd

# ---------------------------------------------------------------------------------

# Load config file
config = get_yaml_config(
    Path(str(PROJECT_DIR) + "/heat_pump_adoption_modelling/config/base.yaml")
)

country_path_dict = {
    "England": "IMD_ENGLAND_PATH",
    "Wales": "IMD_WALES_PATH",
    "Scotland": "IMD_SCOTLAND_PATH",
}

def get_country_imd_data(country, usecols=None):
    """Get deprivation data for specific country.

    Parameters
    ----------
    country : {'England', 'Scotland', 'Wales'}, str
        Country for which to load IMD.

    usecols : list, default=None
        List of features to include.

    Return
    ----------
    imd_df : pandas.DataFrame
        Deprivation data."""

    file_path = str(PROJECT_DIR) + config[country_path_dict[country]]

    imd_df = pd.read_csv(file_path, usecols=usecols)
    imd_df["Country"] = country

    return imd_df

def get_gb_imd_data(
    usecols=[
        "IMD Rank",
        "IMD Decile",
        "Postcode",
        "Income Score",
        "Employment Score",
        "Country",
    ]
):
    """Get deprivation data for England, Wales and Scotland.

    Parameters
    ----------
    usecols : ['IMD Rank', 'IMD Decile', 'Postcode', 'Income Score', 'Employment Score']
        Features to include.
        Above selection works for all countries.

    Return
    ----------
    imd_df : pandas.DataFrame
        Deprivation data for all countries."""

    england_imd = get_country_imd_data("England", usecols=usecols)
    wales_imd = get_country_imd_data("Wales", usecols=usecols)
    scotland_imd = get_country_imd_data("Scotland", usecols=usecols)

    imd_df = pd.concat([england_imd, wales_imd, scotland_imd], axis=0)

    return imd_df

def merge_imd_with_other_set(imd_df, other_df, postcode_label="Postcode"):
    """Merge IMD data with other data based on postcode.

    Parameters
    ----------
    imd_df : pandas.DataFrame
        Deprivation data

    other_df : pandas.DataFrame
        Other data

    Return
    ----------
    merged_df : pandas.DataFrame
        Two datasets merged on postcode."""

    if "POSTCODE" in other_df.columns:
        other_df = other_df.rename(columns={"POSTCODE": "Postcode"})

    imd_df["Postcode"] = imd_df["Postcode"].str.replace(r" ", "")
    other_df["Postcode"] = other_df["Postcode"].str.replace(r" ", "")

    merged_df = pd.merge(imd_df, other_df, on=["Postcode"])

    merged_df = merged_df.rename(columns={"Postcode": postcode_label})

    return merged_df


#!/usr/bin/python

import json
from collections import namedtuple
from datetime import datetime
from datetime import timedelta
import bme280
import pi_status_light
from threading import Thread
import time
import os
import requests
import wiringpi
from requests.exceptions import ConnectionError
from requests.exceptions import Timeout
import logging
import feedparser
import requests
from io import BytesIO
import re

''' Uses https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/bme280.py '''

home_dir = '/home/pi'
file_path = os.path.join(home_dir, '.schedule.json')
smarthings_config_path = os.path.join(home_dir, '.smartthings.json')
logger = logging.getLogger(__name__)
logger.setLevel(logging.ERROR)
logger.addHandler(pi_status_light.handler)
weather_area_code_didcot = '2651269'
weather_area_code_didcot_url = 'https://weather-broker-cdn.api.bbci.co.uk/en/forecast/rss/3day/' + weather_area_code_didcot

def load_config():
  with open(smarthings_config_path, 'r') as myfile:
    return json.loads(myfile.read().translate(None, ' \n\t\r'))

def load_data():
  with open(file_path, 'r') as myfile:
    return json.loads(myfile.read().translate(None, ' \n\t\r'))

def write_json_data(data):
  with open(file_path, 'w') as myfile:
    # print data
    myfile.write(json.dumps(data, sort_keys=True, indent=2, separators=(',', ': ')))

def write_mode(mode):
  data = load_data()
  y = json.loads(mode)
  data['state']['thermostatMode'] = y['thermostatMode']
  data['state']['setDate'] = datetime.now().strftime('%b-%d-%Y_%H:%M')
  data['state']['heatingSetpoint'] = y['heatingSetpoint']
  logger.debug("Received mode update " + data['state']['thermostatMode'])
  logger.debug("Received heatingSetpoint update " + str(data['state']['heatingSetpoint']))
  write_json_data(data)

def write_schedule(schedule):
  data = load_data()
  schedule_data = json.loads(schedule)
  data['daysOfWeek'] = schedule_data['daysOfWeek']
  data['minimun_temp'] = schedule_data['minimun_temp']
  data['offset_temp'] = schedule_data['offset_temp']
  data['temp_on_offset'] = schedule_data['temp_on_offset']
  logger.debug("Received schedule and minimun_temp update")
  write_json_data(data)

def load_state():
  data = load_data()
  # Parse JSON into an object with attributes corresponding to dict keys.
  # x = json.loads(data, object_hook=lambda d: namedtuple('X', d.keys())(*d.values()))
  thermostatOperatingState = 'heating' if pi_status_light.is_on() == 1 else 'idle'
  temperature,pressure,humidity = getDataValue(data['offset_temp'])
  return json.dumps(
      {
          'temperature': temperature,
          'pressure': pressure,
          'humidity': humidity,
          'thermostatOperatingState': thermostatOperatingState,
          'thermostatMode': data['state']['thermostatMode'],
          'setDate': data['state']['setDate'],
          'heatingSetpoint': data['state']['heatingSetpoint'],
          'weather':
          {
            'today': data['weather']['today'],
            'tomorrow': data['weather']['tomorrow'],
            'day2': data['weather']['day2']
          }
      })

def update_mode(now, currentTemp, currentWeatherData, data):
  report_required = False
  s_temp = get_temp(now, data)
  if data['state']['thermostatMode'] == 'turning_heat':
    data['state']['thermostatMode'] = 'heat'
    report_required = True
  elif  data['state']['thermostatMode'] == 'turning_auto':
    data['state']['thermostatMode'] = 'auto'
    report_required = True
  elif  data['state']['thermostatMode'] == 'turning_off':
    data['state']['thermostatMode'] = 'off'
    report_required = True
  if data['state']['thermostatMode'] == 'heat' and data['state']['heatingSetpoint'] < currentTemp - data['temp_on_offset']:
    data['state']['thermostatMode'] = 'auto'
    data['state']['setDate'] = now.strftime('%b-%d-%Y_%H:%M')
    data['state']['heatingSetpoint'] = currentTemp if currentTemp > s_temp else s_temp
    logger.debug("Switching back to auto from heat "+ str(data['state']['heatingSetpoint']))
    report_required = True
  elif data['state']['thermostatMode'] == 'auto' and pi_status_light.is_on() == 1 and data['state']['heatingSetpoint'] != s_temp:
    data['state']['heatingSetpoint'] = s_temp
    logger.debug("Updating auto temperature: " + str(data['state']['heatingSetpoint']))
    report_required = True
  if abs(data['recorded_temp'] - currentTemp) > 0.3:
    data['recorded_temp'] = currentTemp
    logger.debug("Update temperature to " + str(currentTemp))
    report_required = True
  if currentWeatherData is not None:
    data['weather']['today'] = currentWeatherData[0]
    data['weather']['tomorrow'] = currentWeatherData[1]
    data['weather']['day2'] = currentWeatherData[2]
    report_required = True
  if (report_required):
      logger.debug("Update mode to " + str(data['state']['thermostatMode']))
      logger.debug("Update heatingSetpoint to " + str(data['state']['heatingSetpoint']))
      write_json_data(data)
  return report_required

def get_temp(now, data):
  now_time = now.time()
  for time in data['daysOfWeek'][0 if datetime.today().weekday() < 5 else 1]['hour']:
      start = datetime.strptime(time['s'], '%H:%M').time()
      end = datetime.strptime(time['e'], '%H:%M').time()
      if in_between(now_time, start, end):
        return time['t']
  return data['minimun_temp']

def should_be_on(now, currentTemp, data):
  # Parse JSON into an object with attributes corresponding to dict keys.
  # x = json.loads(data, object_hook=lambda d: namedtuple('X', d.keys())(*d.values()))

  if data['state']['thermostatMode'] == 'heat':
    # check heat on
    return True

  if data['state']['thermostatMode'] == 'off':
    return False

  if data['state']['thermostatMode'] == 'auto':
    if pi_status_light.is_on():
      currentTemp = currentTemp - data['temp_on_offset']
    return True if get_temp(now, data) > currentTemp else False

  # Lower than min temp set
  return data['minimun_temp'] > currentTemp

def report_update():
  logger.debug("Pushing changes to smartthings")
  try:
    config = load_config()
    response = requests.put("https://%s%s" % (config['host'], config['endpoint_url']),
      data=load_state(),
      headers=
        {'content-type':'application/json',
        'Authorization':"Bearer %s" % config['access_token']},
      timeout=1.0)
    logger.debug("Got status " + str(response.status_code))
  except ConnectionError as e:    # This is the correct syntax
    logger.exception("Connection error")
  except Timeout as e:    # This is the correct syntax
    logger.exception("Connection timeout")

def in_between(now, start, end):
  if start <= end:
    return start <= now < end
  else: # over midnight e.g., 23:30-04:15
    return start <= now or now < end

class Control(Thread):
  def __init__(self):
    Thread.__init__(self)
    self.daemon = True

  def run(self):
    lastWeatherDownload = None
    weatherData = None
    while True:
      try:
        report_required = False
        logger.debug("reading schedule data from file")
        data = load_data()
        logger.debug("reading temp values from device")
        temperature,pressure,humidity = getDataValue(data['offset_temp'])
        now = datetime.now()
        if lastWeatherDownload is None:
          weatherData = get_weather()
          lastWeatherDownload = now
        else:
          if now > lastWeatherDownload + timedelta(minutes=1):
            lastWeatherDownload = now
            weatherData = get_weather()
          else:
            weatherData = None
        logger.debug("update mode and write data")
        report_required = update_mode(now, temperature, weatherData, data)
        if should_be_on(now, temperature, data):
          if pi_status_light.is_on() != 1:
            logger.debug("trying to turn on")
            pi_status_light.turn_on(True)
            report_required = True
        else:
          if pi_status_light.is_on() == 1:
            logger.debug("trying to turn off")
            pi_status_light.turn_on(False)
            report_required = True
        if report_required:
          logger.debug("reporting back to smartthings")
          report_update()
      except Exception as e:
        logger.exception("Error in main loop")
        pi_status_light.turn_error()
      time.sleep(5)

class HeatButton(Thread):
  def __init__(self):
    Thread.__init__(self)
    self.daemon = True

  def run(self):
    while True:
      if (wiringpi.digitalRead(pi_status_light.HEAT_BUTTON) == 1):
        time.sleep(3)
        if pi_status_light.is_on() != 1:
          data = load_data()
          data['state']['thermostatMode'] = 'turning_heat'
          data['state']['setDate'] = datetime.now().strftime('%b-%d-%Y_%H:%M')
          data['state']['heatingSetpoint'] = 21.0
          write_json_data(data)
          report_update()
      else:
        time.sleep(0.2)

def getDataValue(offset):
  temperature,pressure,humidity = bme280.readBME280All()
  temperature = temperature + offset
  return temperature,pressure,humidity

def parse_weather(data):
  temp = {}
  match = re.compile(":(.*?),").search(data)
  if match:
    temp['summary'] = match.group(1)
  match = re.compile("Minimum Temperature:\s*(-?\d+)").search(data)
  if match:
    temp['min'] = match.group(1)
  match = re.compile("Maximum Temperature:\s*(-?\d+)").search(data)
  if match:
    temp['max'] = match.group(1)
  return temp

def get_weather():
  try:
    resp = requests.get(weather_area_code_didcot_url, timeout=20.0)
    # Put it to memory stream object universal feedparser
    logger.debug("Get content from request")
    content = BytesIO(resp.content)
    logger.debug("Parsing content")
    # Parse content
    weather_data = feedparser.parse(content)
    logger.debug("Got weather data")
    if (len(weather_data['entries']) == 3):
      today = parse_weather(weather_data['entries'][0]['title'])
      tomorrow = parse_weather(weather_data['entries'][1]['title'])
      twoday = parse_weather(weather_data['entries'][2]['title'])
      return today, tomorrow, twoday
    else:
      logger.error("Cant find weather data for 3 days focus")
  except Exception as e:
    logger.exception("Error getting weather update")
    return None

def main():
  pi_status_light.initGpio()
  try:
    t = Control()
    t.start()
    while t.isAlive():
      t.join(1)  # not sure if there is an appreciable cost to this.
  except (KeyboardInterrupt, SystemExit):
    logger.error('Received keyboard interrupt, quitting threads')

if __name__=="__main__":
  main()


import moduls
import game
from game import main_game_menu

moduls.start()  # Приветствие игрока

print("Войти = 1 Зарегистрироваться = 2")
game_mode = int(input())
if game_mode == 1:
    user_id = moduls.login()
elif game_mode == 2:
    moduls.signup()
    print("Совершите вход:")
    user_id = moduls.login()
else:
    print("Ошибка")

if user_id:
    print('GAME START')
    game.main_game_menu(user_id)
else:
    print('goodbye')


# -*-coding:Latin-1 -*
import numpy as np
import random

def sigmoide(x, deriv=False):
    if deriv:
        return ((x) * (1 - (x)))
    return (1 / (1 + np.exp(-x)))

X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0],
              [1],
              [1],
              [0]])

syn0 = 2 * np.random.random((2, 4)) - 1
syn1 = 2 * np.random.random((4, 4)) - 1
syn2 = 2 * np.random.random((4, 1)) - 1

epsi = 10
nbIte = 5000

for j in range(0, nbIte):
    l1 = sigmoide(np.dot(X, syn0))
    l2 = sigmoide(np.dot(l1, syn1))
    l3 = sigmoide(np.dot(l2, syn2))

    l3_delta = (y - l3) * sigmoide(l3, deriv=True)
    l2_delta = l3_delta.dot(syn2.T) * sigmoide(l2, deriv=True)
    l1_delta = l2_delta.dot(syn1.T) * sigmoide(l1, deriv=True)

    syn2 += l2.T.dot(l3_delta) * epsi
    syn1 += l1.T.dot(l2_delta) * epsi
    syn0 += X.T.dot(l1_delta) * epsi

print("e")
print((y - l3))
print("sortie")
print(l3)


import datetime
import json
import logging
import time

class Tracker(object):
    @classmethod
    def get_tracker(cls, backend_uri, request_parser=None, logger=None):
        if backend_uri == 'console':
            return ConsoleTracker(request_parser, logger)

        if backend_uri.startswith('mongodb://'):
            return MongoTracker(request_parser, logger, backend_uri)

    def __init__(self, request_parser, logger):
        self.request_parser = request_parser
        self.logger = logger

    def _record(self, event):
        raise NotImplementedError()

    def _log_tracking_error(self, error):
        if self.logger is not None:
            self.logger.error(error)

    def track_event(self, name, info=None):
        try:
            event_info = {}

            if info is not None:
                event_info.update(info)

            event_info['name'] = name
            event_info['time'] = time.time()
            event_info['ctime'] = datetime.datetime.now().ctime()
            self._record(event_info)
        except Exception as e:
            self._log_tracking_error('Exception in track_event: %s' % e)

    def track_view(self, request, info=None):
        try:
            if self.request_parser is None:
                self.logger.error('Can\'t track pageview events unless request_parser is set.')
                return

            event_info = {}

            event_info['request'] = self.request_parser.get_info_from_request(request)

            if info is not None:
                event_info.update(info)

            self.track_event(
                'view',
                event_info
            )
        except Exception as e:
            self._log_tracking_error('Exception in track_event: %s' % e)

class ConsoleTracker(Tracker):
    def _record(self, event):
        print(json.dumps(event))

class MongoTracker(Tracker):
    def __init__(self, request_parser, logger, mongo_uri):
        super(MongoTracker, self).__init__(request_parser, logger)
        import pymongo
        client = pymongo.MongoClient(mongo_uri)
        self.events_collection = client[mongo_uri.rsplit('/')[-1]].events

    def _record(self, event):
        self.events_collection.insert(event)

class RequestParser(object):
    def get_info_from_request(self, request):
        raise NotImplementedError()

class WerkzeugRequestParser(object):
    def get_info_from_request(self, request):
        return {
            'host': request.host,
            'secure': request.is_secure,
            'method': request.method,
            'path': request.path,
            'query_string': request.query_string,
            'remote_addr': request.remote_addr,
            'url': request.url,
            'referrer': request.referrer,
            'user_agent': {
                'full': request.user_agent.string,
                'platform': request.user_agent.platform,
                'browser': request.user_agent.browser,
                'version': request.user_agent.version,
                'language': request.user_agent.language,
            }
        }


import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
from matplotlib.ticker import MultipleLocator, AutoMinorLocator, MaxNLocator

plt.rcParams.update({
    "text.usetex": True,
    "font.family": "sans-serif",
    "font.sans-serif": ['Computer Modern Serif']})

file_name = 'tema9_zad1.txt'
xaxis_label = '$x$, cm'
yaxis_label = '$d$, mm'
xaxis_mult_loc = 50
yaxis_mult_loc = 1
save_name = 'tema9_zad1.pdf'

x_data, y_data = np.genfromtxt(file_name, unpack=True)

slope, intercept, r, p, std_err = stats.linregress(x_data, y_data)
result = stats.linregress(x_data, y_data)
print(f'intercept: {result.intercept}, std error: {result.intercept_stderr}' )
print(f'slope: {slope}')
print(f'r-value: {r} (The Pearson correlation coefficient.)')
print(f'p-value: {p} (The p-value for a hypothesis test whose null hypothesis is that the slope is zero, using Wald '
      f'Test with t-distribution of the test statistic.)')
print(f'standard error: {std_err} (Standard error of the estimated slope (gradient), under the assumption of residual '
      f'normality.)')

dx = 1
y_error = np.sqrt((std_err*x_data)**2 + (slope*dx)**2 + result.intercept_stderr**2)
print(y_error)

def lin_reg(x):
    return slope*x + intercept

slope_line = list(map(lin_reg, x_data))

fig, ax = plt.subplots(figsize=(3.48*2, 3.48*3/2))
ax.set_xlabel(xaxis_label)
ax.set_ylabel(yaxis_label)
ax.xaxis.set_major_locator(MultipleLocator(xaxis_mult_loc))
ax.xaxis.set_minor_locator(AutoMinorLocator())
ax.yaxis.set_major_locator(MultipleLocator(yaxis_mult_loc))
ax.yaxis.set_minor_locator(AutoMinorLocator())
ax.xaxis.set_major_formatter('{x:.3f}')
ax.xaxis.grid(True, which='major', color="lightgray",
              linestyle='dashed')  # which = minor|major|both
ax.xaxis.grid(True, which='minor', color="lightblue",
              linestyle='dotted')  # which = minor|major|both
ax.yaxis.grid(True, which='major', color="lightgray",
              linestyle='dashed')  # which = minor|major|both
ax.yaxis.grid(True, which='minor', color="lightblue",
              linestyle='dotted')  # which = minor|major|both
plt.plot(x_data, slope_line, label=f'{slope:.2f}$x + ${intercept:.2f}')
plt.scatter(x_data, y_data, marker=".")
plt.errorbar(x_data, y_data, xerr=dx,  yerr=y_error, fmt='.', ecolor='midnightblue', color='black', elinewidth=1.0)
plt.tight_layout(pad=0.3)
ax.legend(facecolor="white", edgecolor="white", loc="best")
plt.savefig(save_name)
plt.show()

# generates new txt file with the y_error added as last column
old_file = np.genfromtxt(file_name)
err = np.reshape(y_error, (old_file.shape[0],1))
new_text_file = np.append(old_file, err, 1)
np.savetxt('output.txt', new_text_file, delimiter=" ", fmt="%s")


class Solution:
    def rotate(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        k, start, n = k % len(nums), 0, len(nums)
        while k % n != 0 and n > 0:
            for i in range(k):
                nums[start + i], nums[len(nums) - k + i] = nums[len(nums) - k + i], nums[start + i]
            start, n = start + k, n - k
            k = k % n


from django.shortcuts import render
import plotly.io as pi
import datetime as dt
from datetime import timezone
from screener.models import *
from .helpers.db_manager import *
from .helpers.plotter import *
from .helpers.index_calculator import *

# Create your views here.
def index(request):
    print(request)
    context={

    }
    return render(request, 'screener/landing.html', context)

def company(request):
    try:
        ticker = request.GET['search_query'].upper()
        #start_date = dt.datetime.strptime(request.GET['start_date'], '%Y-%m-%d').replace(tzinfo=timezone.utc).date()
        end_date = dt.datetime.today().date()

        #start_date = request.GET['start_date']
        #end_date = request.GET['end_date']

        company = Company.objects.filter(ticker = ticker)

        if company:
            #update company objects if object is present in db
            update_object(ticker)

        else:
            #create company objects
            create_object(ticker)

    ## HANDLE ERRORS
    except Exception as e:
        context = {
            'req': request.GET['search_query'],
            'error': f"{type(e).__name__} at line {e.__traceback__.tb_lineno} of {__file__}: {e}",
        }
        return render(request, 'screener/404.html', context)

    # query db and create context
    company = Company.objects.filter(ticker = ticker)[0]

    # CALCULATE INDICES
    # get income_statement and balancesheet
    income_statement = pd.read_csv(ARCHIVE_PATH + "income_statement/" + str(company.income_statement), index_col=0)
    balancesheet = pd.read_csv(ARCHIVE_PATH + "balancesheet/" + str(company.balancesheet), index_col=0)

    indices = calculate_indices(financials = income_statement, balancesheet = balancesheet)
    mean_indices = industry_mean(company.industry)

    # CREATE PLOTS
    # get history
    history = pd.read_csv(ARCHIVE_PATH + "history/" + str(company.history), index_col='date')
    #convert dates to str
    start_date = dt.datetime.strftime(end_date-pd.DateOffset(years= 10), '%Y-%m-%d')
    end_date = dt.datetime.strftime(end_date, '%Y-%m-%d')
    candle_plot = pi.to_html(
        plot_candlestick(history.loc[start_date: end_date]),
        full_html=False,
        default_height="800px"
        )

    asset_liabilities = pi.to_html(
        plot_balancesheet(balancesheet),
        full_html=False,
        #default_height="800px"
        )

    revenue_income = pi.to_html(
        plot_income_statement(income_statement),
        full_html=False,
        #default_height="800px"
        )

    context={

        'ticker':company.ticker,
        'name':company.name,
        'sector':company.sector,
        'industry':company.industry,
        'phone':company.phone,
        'website':company.website,
        'country':company.country,
        'state':company.state,
        'city':company.city,
        'address':company.address,
        'summary':company.summary,
        'employees':company.employees,

        'balancesheet':company.balancesheet,
        'income_statement':company.income_statement,
        'cash_flow':company.cash_flow,

        'history':history,

        'indices':indices,
        'mean_indices':mean_indices,

        #plots
        'candlestick': candle_plot,
        'asset_liabilities': asset_liabilities,
        'revenue_income': revenue_income,

    }

    return render(request, 'screener/company.html', context)


import datetime
# from keys import PUBLIC_KEY,PRIVATE_KEY
import requests
import hashlib
from pprint import pprint as pp
import environ

env = environ.Env()
env.read_env()

PRIVATE_KEY = env('PRIVATE_KEY')
PUBLIC_KEY = env('PUBLIC_KEY')

timestamp = datetime.datetime.now().strftime('%Y-%m-%d%H:%M:%S')

def hash_params():
    hash_md5 = hashlib.md5()
    hash_md5.update(f'{timestamp}{PRIVATE_KEY}{PUBLIC_KEY}'.encode('utf-8'))
    hashed_params = hash_md5.hexdigest()
    return hashed_params

params = {'ts': timestamp, 'apikey': PUBLIC_KEY, 'hash': hash_params()}

response = requests.get('https://gateway.marvel.com/v1/public/characters?nameStartsWith=' + 'spi' , params=params)
data = response.json()
pp(data)