#!/usr/bin/env python

import psycopg2
import psycopg2.extras

import re

DANY_VERSION_STRING = "2.2.4"
DANY_FUNC_VERSION_STRING = "2_2_4"

def multiple_replace(text, adict):
    """
    From the the python cookbook. adict's keys should be the patterns
    to match and their values should be the replacements.
    """
    rx = re.compile("|".join(map(re.escape, adict)))
    def one_xlat(match):
        return adict[match.group(0)]
    return rx.sub(one_xlat, text)

class DanyException(Exception):
    pass

class DanCursor(psycopg2.extras.DictCursor):

    def execute(self, *args, **kwargs):
        try:
            super(DanCursor, self).execute(*args, **kwargs)
        except:
            raise DanyException("NOPE")

    def get_nodes(self):
        qry = """
        select no_id as node_id,
               no_active as active,
               no_comment as comment
        from sl_node
        ;
        """
        self.execute(qry)
        for row in self:
            yield row

    def get_paths(self):
        qry = """
        select pa_server as server,
               pa_client as client,
               pa_conninfo as conninfo,
               pa_connretry as connretry
        from sl_path
        ;
        """
        self.execute(qry)
        for row in self:
            yield row

    def get_listens(self):
        qry = """
        select li_origin as origin_id,
               li_provider as provider_id,
               li_receiver as receiver_id
        from sl_listen
        ;
        """
        self.execute(qry)
        for row in self:
            yield row

    def get_set(self, set_id):
        qry = """
        select set_id as id,
               set_origin as origin,
               set_comment as comment
        from sl_set
        where set_id = %(set_id)s
        ;
        """
        d = {
            "set_id": set_id,
        }
        self.execute(qry, d)
        row = self.fetchone()
        return row

    def get_sets(self):
        qry = """
        select set_id,
               set_origin as origin_id,
               set_comment as comment
        from sl_set
        ;
        """
        self.execute(qry)
        for row in self:
            yield row

    def check_subscription_exists(self, set_id, receiver_id, provider_id):
        qry = """
        select exists (select 1
                       from sl_subscribe
                       where sub_set = %(set_id)s
                         and sub_receiver = %(receiver_id)s
                         and sub_active = true
                         and sub_provider != %(provider_id)s) as sub_exists
        ;
        """
        d = {
            "set_id": set_id,
            "receiver_id": receiver_id,
            "provider_id": provider_id,
        }
        self.execute(qry, d)
        row = self.fetchone()
        return row["sub_exists"]

    def get_subscriptions(self):
        qry = """
        select sub_set as set_id,
               sub_provider as provider_id,
               sub_receiver as receiver_id,
               sub_forward as forward,
               sub_active as active
        from sl_subscribe
        ;
        """
        self.execute(qry)
        for row in self:
            yield row

    def get_max_seqno_by_origin(self):
        qry = """
        select ev_origin as origin_id,
               max(ev_seqno) as max_seqno
        from sl_event
        group by ev_origin
        ;
        """
        self.execute(qry)
        for row in self:
            yield row

    def set_seqno_value(self, name, value):
        qry = """
        select setval(%(sequence)s, %(value)s);
        """
        d = {
            "name": name,
            "value": value,
        }
        self.execute(qry, d)

    def insert_confirmation(self, origin_id, receiver_id, seqno):
        qry = """
        insert into sl_confirm
        (
          con_origin,
          con_received,
          con_seqno,
          con_timestamp
        )
        values
        (
          %(origin_id)s,
          %(received_id)s,
          %(seqno)s,
          current_timestamp
        )
        ;
        """
        d = {
            "origin": origin_id,
            "received": receiver_id,
            "seqno": seqno,
        }
        self.execute(qry, d)

    def initializeLocalNode(self, node_id, comment):
        qry = """
        select initializeLocalNode(%(node_id)s, %(comment)s);
        """
        d = {
            "node_id": node_id,
            "comment": comment,
        }
        self.execute(qry, d)

    def storeNode(self, node_id, comment):
        qry = """
        select storeNode_int(%(node_id)s, %(comment)s);
        """
        d = {
            "node_id": node_id,
            "comment": comment,
        }
        self.execute(qry, d)

    def enableNode(self, node_id):
        qry = """
        select enableNode_int(%(node_id)s);
        """
        d = {
            "node_id": node_id,
        }
        self.execute(qry, d)

    def storePath(self, server_id, client_id, conninfo, connretry):
        qry = """
        select storePath_int(%(server_id)s,
                             %(client_id)s,
                             %(conninfo)s,
                             %(connretry)s)
        ;
        """
        d = {
            "server_id": server_id,
            "client_id": client_id,
            "conninfo": conninfo,
            "connretry": connretry,
        }
        self.execute(qry, d)

    def storeListen(self, origin_id, provider_id, receiver_id):
        qry = """
        select storeListen_int(%(origin_id)s,
                               %(provider_id)s,
                               %(receiver_id)s)
        ;
        """
        d = {
            "origin_id": origin_id,
            "provider_id": provider_id,
            "receiver_id": receiver_id,
        }
        self.execute(qry, d)

    def storeSet(self, set_id, origin_id, comment):
        qry = """
        select storeSet_int(%(set_id)s,
                            %(origin_id)s,
                            %(comment)s)
        ;
        """
        d = {
            "set_id": set_id,
            "origin_id": origin_id,
            "comment": comment,
        }
        self.execute(qry, d)

    def subscribeSet(self, set_id, provider_id, receiver_id, forward, active):
        qry = """
        select subscribeSet_int(%(set_id)s,
                                %(provider_id)s,
                                %(receiver_id)s,
                                %(forward)s,
                                %(active)s)
        ;
        """
        d = {
            "set_id": set_id,
            "provider_id": provider_id,
            "receiver_id": receiver_id,
            "forward": forward,
            "active": active,
        }
        self.execute(qry, d)

    def enableSubscription(self, set_id, provider_id, receiver_id):
        qry = """
        select enableSubscription_int(%(set_id)s,
                                      %(provider_id)s,
                                      %(receiver_id)s)
        ;
        """
        d = {
            "set_id": set_id,
            "provider_id": provider_id,
            "receiver_id": receiver_id,
        }
        self.execute(qry, d)

    def reshapeSubscription(self, set_id, provider_id, receiver_id):
        qry = """
        select reshapeSubscription(%(set_id)s,
                                   %(provider_id)s,
                                   %(receiver_id)s)
        ;
        """
        d = {
            "set_id": set_id,
            "provider_id": provider_id,
            "receiver_id": receiver_id,
        }
        self.execute(qry, d)

    def determineIdxnameUnique(self, fqdn, key):
        qry = """
        select determineIdxnameUnique(%(fqdn)s, %(key)s) as idxname;
        """
        d = {
            "fqdn": fqdn,
            "key": key,
        }
        self.execute(qry, d)
        row = self.fetchone()
        return row["idxname"]

    def setAddTable(self, set_id, table_id, fqdn, idxname, comment):
        qry = """
        select setAddTable(%(set_id)s,
                           %(table_id)s,
                           %(fqdn)s,
                           %(idxname)s,
                           %(comment)s)
        ;
        """
        d = {
            "set_id": set_id,
            "table_id": table_id,
            "fqdn": fqdn,
            "idxname": idxname,
            "comment": comment,
        }
        self.execute(qry, d)

    def createEvent(self, schema, event):
        qry = """
        select createEvent(%(schema)s, %(event)s);
        """
        d = {
            "schema": schema,
            "event": event,
        }
        self.execute(qry, d)

    def acquire_event_lock(self):
        self.execute("lock table sl_event_lock;")

    def acquire_config_lock(self):
        self.execute("lock table sl_config_lock;")

class Node(object):

    def __init__(self, node_id, conninfo, comment=None):

        self.node_id = node_id
        self.conninfo = conninfo
        self.comment = comment

    def connect(self, schema):
        if isinstance(self.conninfo, basestring):
            conn = psycopg2.connect(self.conninfo, cursor_factory=DanCursor)
        elif isinstance(self.conninfo, dict):
            conn = psycopg2.connect(cursor_factory=DanCursor, **self.conninfo)
        else:
            raise DanyException("Bad conninfo: {0}".format(self.conninfo))
        level = psycopg2.extensions.ISOLATION_LEVEL_REPEATABLE_READ
        conn.set_isolation_level(level)
        cur = conn.cursor()
        cur.execute("set search_path = %s;", (schema,))
        return conn

    def __eq__(self, other):
        return self.node_id == other.node_id

class Cluster(object):

    def __init__(self, cluster_id, name, comment=None):
        """
        Define a slony cluster.
        """

        self.cluster_id = cluster_id
        self.name = name
        self.comment = comment

class Set(object):

    def __init__(self, set_id, comment="An exciting replication set"):
        self.set_id = set_id
        self.comment = comment

class Table(object):

    def __init__(self, table_id, name, schema="public", key=None, comment=None):
        self.table_id = table_id
        self.name = name
        self.schema = schema
        self.key = key
        self.comment = comment

    @property
    def fqdn(self):
        return "{0}.{1}".format(self.schema, self.name)

class AdminConnections(object):

    def __init__(self, schema):
        self.schema = schema
        self.connections = {}

    def __getitem__(self, node):

        if node not in self.connections:
            conn = node.connect(self.schema)
            self.connections[node] = conn

        return self.connections[node]

    def lookup_id(self, node_id):

        for node in self.connections:
            if node.node_id == node_id:
                return self.connections[node]

        raise DanyException("No connection info for node {0}".format(node_id))

    def __iter__(self):
        return iter(self.connections)

class Dan(object):

    def __init__(self, cluster, nodes):
        self.cluster = cluster
        self.schema = "_{0}".format(cluster.name)
        self.nodes = nodes
        self.connections = AdminConnections(self.schema)

    def _load_script(self, cur, fname):

        sql = None
        with open(fname) as f:
            d = {
                "@CLUSTERNAME@": self.cluster.name,
                "@MODULEVERSION@": DANY_VERSION_STRING,
                "@NAMESPACE@": self.schema,
                "@FUNCVERSION@": DANY_FUNC_VERSION_STRING,
            }
            sql = multiple_replace(f.read(), d)
        if sql is not None:
            cur.execute(sql)
        else:
            raise DanyException("Couldn't load {0}".format(fname))

    def _load_base(self, cur, node):
        # Maybe one day the schema name will be bound properly here :/
        cur.execute("create schema %s;" % (self.schema,))
        self._load_script(cur, "slony1_base.2.2.4.sql")
        self._load_script(cur, "slony1_base.v84.2.2.4.sql")
        self._load_script(cur, "slony1_funcs.2.2.4.sql")
        self._load_script(cur, "slony1_funcs.v84.2.2.4.sql")

    def _lookup_origin_helper(self, set_id, cur):

        row = cur.get_set(set_id)

        if row is None:
            return None

        return row["origin"]

    def _lookup_origin(self, set_id, cur=None):

        origin_id = None

        msg = "Cannot determine set origin for set {0}".format(set_id)

        if cur is None:

            for node in self.connections:
                cur = self.connections[node].cursor()
                origin_id = self._lookup_origin_helper(set_id, cur)
                if origin_id is not None:
                    return origin_id

            # We made it through all the connections and didn't find
            # the origin
            raise DanyException(msg)

        else:

            origin_id = self._lookup_origin_helper(set_id, cur)

            if origin_id is None:
                raise DanyException(msg)

            return origin_id

    def init_cluster(self, origin):

        conn = self.connections[origin]
        cur = conn.cursor()

        self._load_base(cur, origin)

        cur.acquire_event_lock()
        cur.acquire_config_lock()
        cur.initializeLocalNode(origin.node_id, origin.comment)
        cur.enableNode(origin.node_id)

        conn.commit()

    def store_node(self, node, event_node):

        if node == event_node:
            msg = "ev_origin for store_node cannot be the new node"
            raise DanyException(msg)

        conn_node = self.connections[node]
        cur_node = conn_node.cursor()

        self._load_base(cur_node, node)

        cur_node.acquire_event_lock()
        cur_node.acquire_config_lock()

        cur_node.initializeLocalNode(node.node_id, node.comment)
        cur_node.enableNode(node.node_id)

        conn_event_node = self.connections[event_node]
        cur_event_node = conn_event_node.cursor()

        # Copy sl_node entries from event node to the node being stored
        for row in cur_event_node.get_nodes():
            cur_node.storeNode(row["node_id"], row["comment"])
            if row["active"]:
                cur_node.enableNode(row["node_id"])

        # Copy sl_path entries from event node to the node being stored
        for row in cur_event_node.get_paths():
            cur_node.storePath(row["server_id"],
                               row["client_id"],
                               row["conninfo"],
                               row["connretry"])

        # Copy sl_listen entries from event node to the node being stored
        for row in cur_event_node.get_listens():
            cur_node.storeListen(row["origin_id"],
                                 row["provider_id"],
                                 row["receiver_id"])

        # Copy sl_set
        for row in cur_event_node.get_sets():
            cur_node.storeSet(row["set_id"], row["origin_id"], row["comment"])

        # Copy sl_subscribe
        for row in cur_event_node.get_subscriptions():

            # Every subscription here starts as inactive
            cur_node.subscribeSet(row["set_id"],
                                  row["provider_id"],
                                  row["receiver_id"],
                                  row["forward"],
                                  False)

            # Then, if the subscription is supposed to be active,
            # enable it
            if row["active"]:
                cur_node.enableSubscription(row["set_id"],
                                            row["provider_id"],
                                            row["receiver_id"])

        # For nodes that aren't this new node, mark the new node as
        # having confirmed every event up until now (to mark us as up
        # to speed). I'm not sure how we could have yet generated a
        # noticeable event that the event node would then kick back
        # for us here, but if we did then our event sequence gets
        # kicked up to that value.
        for row in cur_event_node.get_max_seqno_by_origin():
            if node.node_id == row["origin_id"]:
                cur_node.set_seqno_val("sl_event_seq", row["max_seqno"])
            else:
                cur_node.insert_confirmation(row["ev_origin"],
                                             node.node_id,
                                             row["max_ev_seqno"])

        # Now register the new node in the event node. If auto-wait is
        # enabled, this should wait for confirmation.
        cur_event_node.storeNode(node.node_id, node.comment)
        cur_event_node.enableNode(node.node_id)

        conn_node.commit()
        conn_event_node.commit()

    def store_path(self, server, client, conninfo, connretry=10):

        conn = self.connections[client]
        cur = conn.cursor()

        cur.acquire_event_lock()
        cur.acquire_config_lock()

        cur.storePath(server.node_id, client.node_id, conninfo, connretry)

        conn.commit()

    def create_set(self, set_, origin):

        conn = self.connections[origin]
        cur = conn.cursor()

        cur.acquire_event_lock()
        cur.acquire_config_lock()

        cur.storeSet(set_.set_id, origin.node_id, set_.comment)

        conn.commit()

    def set_add_table(self, set_, tables):
        """
        Add each of the provided tables to the given replication set.
        """
        origin_id = self._lookup_origin(set_.set_id)
        conn_origin = self.connections.lookup_id(origin_id)
        cur_origin = conn_origin.cursor()

        cur_origin.acquire_event_lock()
        cur_origin.createEvent(self.schema, "SYNC")

        # wait for that sync

        # Obtain a lock on sl_event_lock. It must be obtained before
        # calling setAddTable()
        cur_origin.acquire_event_lock()
        cur_origin.acquire_config_lock()

        for table in tables:
            idxname = cur_origin.determineIdxnameUnique(table.fqdn, table.key)
            cur_origin.acquire_config_lock()
            cur_origin.setAddTable(set_.set_id,
                                   table.table_id,
                                   table.fqdn,
                                   idxname,
                                   table.comment)

        conn_origin.commit()

    def subscribe_set(self, set_, provider, receiver,
                      forward=True, omitcopy=True):

        conn_provider = self.connections[provider]
        cur_provider = conn_provider.cursor()

        # Check if this node already subscribes to this set via a node
        # that's NOT the proposed provider. If so we will need to
        # reshape it later.
        reshape = cur_provider.check_subscription_exists(set_.set_id,
                                                         receiver.node_id,
                                                         provider.node_id)

        # Now look up the set's definition to find the origin node
        origin_id = self._lookup_origin(set_.set_id, cur_provider)
        conn_origin = self.connections.lookup_id(origin_id)
        cur_origin = conn_origin.cursor()

        cur_origin.acquire_event_lock()
        cur_origin.acquire_config_lock()

        cur_origin.subscribeSet(set_.set_id,
                                provider.node_id,
                                receiver.node_id,
                                forward,
                                omitcopy)

        if reshape:
            conn_receiver = self.connections[receiver]
            cur_receiver = conn_receiver.cursor()

            cur_receiver.acquire_config_lock()
            cur_receiver.reshapeSubscription(set_.set_id,
                                             provider.node_id,
                                             receiver.node_id)

            conn_receiver.commit()

        conn_provider.commit()
        conn_origin.commit()

    def sync(self, node):
        conn = self.connections[node]
        cur = conn.cursor()
        cur.acquire_event_lock()
        cur.createEvent(self.schema, "SYNC")
        conn.commit()


#!/usr/bin/env python
# coding: utf-8

from google_play_scraper import app
import pandas as pd
import pandahouse as ph
from clickhouse_driver import Client
from tqdm import tqdm

bundle_list = pd.read_csv('/home/v.roizen/scripts/parsing/rustore_bundles.csv')

bundle_list = list(set(bundle_list['bundle_id']))

def get_key(d, value):
    for k, v in d.items():
        if v == value:
            return k

colmn=['bundle_id','google_play_version']
df=pd.DataFrame(columns=colmn)

for i in tqdm(range(len(bundle_list))):
    try:
        result = app(
            bundle_list[i],
            lang='en', # defaults to 'en'
            country='us' # defaults to 'us'
        )
        google_play_version=result.get('version')
        bundle_id=bundle_list[i]
    except:
        google_play_version='App not found'
        bundle_id=bundle_list[i]

    row = [bundle_id, google_play_version]
    new_df = pd.DataFrame([row], columns=colmn)
    df = pd.concat([df, new_df], ignore_index = True)

df.to_csv('/home/v.roizen/scripts/parsing/google_play.csv')


import unittest
import numpy as np
from shapely.geometry import Polygon

from mtoolkit.workflow import Context
from mtoolkit.jobs import read_eq_catalog, read_source_model, \
create_catalog_matrix, gardner_knopoff, stepp, _check_polygon, \
processing_workflow_setup_gen
from mtoolkit.utils import get_data_path, DATA_DIR

class JobsTestCase(unittest.TestCase):

    def setUp(self):
        self.context = Context(get_data_path(
            'config.yml', DATA_DIR))
        self.eq_catalog_filename = get_data_path(
            'ISC_small_data.csv', DATA_DIR)
        self.smodel_filename = get_data_path(
            'area_source_model.xml', DATA_DIR)

    def test_read_eq_catalog(self):
        self.context.config['eq_catalog_file'] = self.eq_catalog_filename
        expected_first_eq_entry = {'eventID': 1, 'Agency': 'AAA', 'month': 1,
                'depthError': 0.5, 'second': 13.0, 'SemiMajor90': 2.43,
                'year': 2000, 'ErrorStrike': 298.0, 'timeError': 0.02,
                'sigmamb': '', 'latitude': 44.368, 'sigmaMw': 0.355,
                'sigmaMs': '', 'Mw': 1.71, 'Ms': '',
                'Identifier': 20000102034913, 'day': 2, 'minute': 49,
                'hour': 3, 'mb': '', 'SemiMinor90': 1.01, 'longitude': 7.282,
                'depth': 9.3, 'ML': 1.7, 'sigmaML': 0.1}

        read_eq_catalog(self.context)

        self.assertEqual(10, len(self.context.eq_catalog))
        self.assertEqual(expected_first_eq_entry,
                self.context.eq_catalog[0])

    def test_read_smodel(self):
        self.context.config['source_model_file'] = self.smodel_filename
        expected_first_sm_definition = \
            {'id_as': 'src_1',
             'area_boundary':
               [132.93, 42.85, 134.86,
                41.82, 129.73, 38.38,
                128.15, 40.35],
             'rupture_rate_model': [
               {'max_magnitude': 8.0,
                'a_value_cumulative':
                    3.1612436654341836,
                'name': 'truncated_guten_richter',
                'min_magnitude': 5.0,
                'b_value': 0.7318999871612379},
             {'name': 'focal_mechanism',
              'nodal_planes': [
                {'strike': 0.0,
                 'rake': 0.0,
                 'dip': 90.0,
                 'id': 0},
                {'strike': 12.0,
                 'rake': 0.0,
                 'dip': 40.0,
                 'id': 1}],
              'id': 'smi:fm1/0'}],
             'tectonic_region':
                'Active Shallow Crust',
             'id_sm': 'sm1',
             'rupture_depth_distribution': {
                'depth': [15.0],
                'magnitude': [5.0],
                'name': 'rupture_depth_distrib'},
             'hypocentral_depth': 15.0,
             'type': 'area_source',
             'name': 'Source 8.CH.3'}

        read_source_model(self.context)
        self.assertEqual(2, len(self.context.sm_definitions))
        self.assertEqual(expected_first_sm_definition,
                self.context.sm_definitions[0])

    def test_a_bad_polygon_raises_exception(self):
        polygon = Polygon([(1, 1), (1, 2), (2, 1), (2, 2)])

        self.assertRaises(RuntimeError, _check_polygon, polygon)

    def test_processing_workflow_setup(self):
        self.context.config['apply_processing_steps'] = True

        eq_internal_point = [2000, 1, 2, -0.25, 0.25]
        eq_side_point = [2000, 1, 2, -0.5, 0.25]
        eq_external_point = [2000, 1, 2, 0.5, 0.25]
        eq_events = np.array([eq_internal_point,
                eq_side_point, eq_external_point])
        self.context.vmain_shock = eq_events

        sm = {'area_boundary':
            [-0.5, 0.0, -0.5, 0.5, 0.0, 0.5, 0.0, 0.0]}
        self.context.sm_definitions = [sm]

        first_sm, filtered_eq_sm = \
            processing_workflow_setup_gen(self.context).next()

        expected_eq_events = np.array([eq_internal_point])

        self.assertTrue(np.array_equal(expected_eq_events, filtered_eq_sm))
        self.assertEqual(sm, first_sm)

    def test_gardner_knopoff(self):

        self.context.config['eq_catalog_file'] = get_data_path(
            'declustering_input_test.csv', DATA_DIR)
        self.context.config['GardnerKnopoff']['time_dist_windows'] = \
                'GardnerKnopoff'
        self.context.config['GardnerKnopoff']['foreshock_time_window'] = 0.5

        read_eq_catalog(self.context)
        create_catalog_matrix(self.context)

        expected_vmain_shock = np.delete(
            self.context.catalog_matrix, [4, 10, 19], 0)

        expected_vcl = np.array([0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0,
            0, 0, 0, 0, 6])

        expected_flag_vector = np.array([0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
            0, 0, 0, 0, 0, 0, 1])

        gardner_knopoff(self.context)

        self.assertTrue(np.array_equal(expected_vmain_shock,
                self.context.catalog_matrix))
        self.assertTrue(np.array_equal(expected_vcl, self.context.vcl))
        self.assertTrue(np.array_equal(expected_flag_vector,
                self.context.flag_vector))

    def test_parameters_gardner_knopoff(self):

        self.context.config['eq_catalog_file'] = get_data_path(
            'declustering_input_test.csv', DATA_DIR)
        self.context.config['GardnerKnopoff']['time_dist_windows'] = \
                'GardnerKnopoff'
        self.context.config['GardnerKnopoff']['foreshock_time_window'] = 0.5

        read_eq_catalog(self.context)
        create_catalog_matrix(self.context)

        def mock(data, time_dist_windows, foreshock_time_window):
            self.assertEquals("GardnerKnopoff", time_dist_windows)
            self.assertEquals(0.5, foreshock_time_window)
            return None, None, None

        self.context.map_sc['gardner_knopoff'] = mock
        gardner_knopoff(self.context)

    def test_stepp(self):
        self.context.config['eq_catalog_file'] = get_data_path(
            'completeness_input_test.csv', DATA_DIR)

        self.context.config['Stepp']['time_window'] = 5
        self.context.config['Stepp']['magnitude_windows'] = 0.1
        self.context.config['Stepp']['sensitivity'] = 0.2
        self.context.config['Stepp']['increment_lock'] = True

        read_eq_catalog(self.context)
        create_catalog_matrix(self.context)

        filtered_eq_events = np.array([
                    [4.0, 1994.], [4.1, 1994.], [4.2, 1994.],
                    [4.3, 1994.], [4.4, 1994.], [4.5, 1964.],
                    [4.6, 1964.], [4.7, 1964.], [4.8, 1964.],
                    [4.9, 1964.], [5.0, 1964.], [5.1, 1964.],
                    [5.2, 1964.], [5.3, 1964.], [5.4, 1964.],
                    [5.5, 1919.], [5.6, 1919.], [5.7, 1919.],
                    [5.8, 1919.], [5.9, 1919.], [6.0, 1919.],
                    [6.1, 1919.], [6.2, 1919.], [6.3, 1919.],
                    [6.4, 1919.], [6.5, 1919.], [6.6, 1919.],
                    [6.7, 1919.], [6.8, 1919.], [6.9, 1919.],
                    [7.0, 1919.], [7.1, 1919.], [7.2, 1919.],
                    [7.3, 1919.]])

        stepp(self.context)
        self.assertTrue(np.allclose(filtered_eq_events,
                self.context.completeness_table))

        gardner_knopoff(self.context)
        stepp(self.context)
        self.assertTrue(np.allclose(filtered_eq_events,
                self.context.completeness_table))

    def test_parameters_stepp(self):
        self.context.config['eq_catalog_file'] = get_data_path(
            'completeness_input_test.csv', DATA_DIR)

        self.context.config['Stepp']['time_window'] = 5
        self.context.config['Stepp']['magnitude_windows'] = 0.1
        self.context.config['Stepp']['sensitivity'] = 0.2
        self.context.config['Stepp']['increment_lock'] = True

        read_eq_catalog(self.context)
        create_catalog_matrix(self.context)

        def mock(year, mw, magnitude_windows, time_window, sensitivity, iloc):
            self.assertEqual(time_window, 5)
            self.assertEqual(magnitude_windows, 0.1)
            self.assertEqual(sensitivity, 0.2)
            self.assertTrue(iloc)

        self.context.map_sc['stepp'] = mock
        stepp(self.context)


"""
Both t-tests and ANOVA have a major drawback: the population that is being
sampled must follow a normal distribution. In many applications, this is not
too restrictive because many real-world population values follow a normal
distribution, or some rules, such as the central limit theorem, allow us to
analyze some related data. However, it is simply not true that all possible
population values follow a normal distribution in any reasonable way. For these
(thankfully, rare) cases, we need some alternative test statistics to use as
replacements for t-tests and ANOVA.

This module illustrates how to use Wilcoxon rank-sum and the Kruskal-Wallis
test for differences between two (or more, in the latter case) populations.
"""
from scipy import stats
from numpy.random import default_rng

rng = default_rng(12345)

sample_A = rng.uniform(2.5, 4.5, size=22)
sample_B = rng.uniform(3.0, 4.4, size=25)
sample_C = rng.uniform(3.0, 4.4, size=30)

significance = 0.05
statistic, p_value = stats.kruskal(sample_A, sample_B, sample_C)

print(f"Statistic: {statistic}, p-value: {p_value}")
# Statistic: 5.09365664638392, p-value: 0.07832970895845669

if p_value <= significance:
    print("Accept H0: all medians equal")
else:
    print("There are differences between population medians")
# There are differences between population medians

_, p_A_B = stats.ranksums(sample_A, sample_B)
_, p_A_C = stats.ranksums(sample_A, sample_C)
_, p_B_C = stats.ranksums(sample_B, sample_C)

if p_A_B > significance:
    print(f"Significance differences between A and B, p-value: {p_A_B}")
# Significant differences between A and B, p-value 0.08808151166219029

if p_A_C > significance:
    print(f"Significance differences between A and C, p-value: {p_A_C}")
# Significant differences between A and C, p-value 0.4257804790323789

if p_B_C > significance:
    print(f"Significance differences between B and C, p-value: {p_B_C}")
else:
    print(f"No significant differences between B and C, p-value: {p_B_C}")
# No significant differences between B and C, p-value 0.037610047044153536


def Chefs_pawn(P,N):
    Dis = {}
    for i in N:
        if(P%i == 0):
            count = 0
            Sum = 0
            while(Sum != P):
                Sum = Sum + i
                count = count + 1

            Dis[i] = count

    if(len(Dis) != 0):
        Max = min(Dis, key=Dis.get)
    else:
        Max = -1

    return (Max)

try:
    test_case = input()
    for i in range(int(test_case)):
        P = list(map(int,input().split()))
        N = list(map(int,input().split()))

        print(Chefs_pawn(P[1],N))

except EOFError as e:
    print(end="")

Code Chef/August_Challenge/Chef and Linear Chess.py


from gSettings import *
from cTCP import *

#TODO : Do not import has *
from tkinter import *

# @Define   Graphical User Interface (GUI) to connect to a server
#
# @Author   Maxime Lagadec
# @Date     1/13/2018
#
class GUI_Client(Frame) :

    #Variables definition

    sSelected_Action = "Action_?"

    clTCP = TCP(False)

    bIsConnected = False

    # @Define   Initialisation of the class
    # @Define   Launch the GUI
    #
    def __init__(self, master = None):

        Frame.__init__(self, master)

        self.master = master
        self.pack()
        self.CreateWidgets()

        self.bIsConnected = False

    # @Define   GUI frame definition and bindings
    #
    def CreateWidgets(self):

        #Main frame setup
        self.Main = PanedWindow(self, orient = VERTICAL)

        self.Upper_Lair = PanedWindow(self, orient = HORIZONTAL)
        self.Lower_Lair = PanedWindow(self, orient = HORIZONTAL)

        #1st PanedWindow definitions
        self.IP_Menu = PanedWindow(self, orient = VERTICAL)

        self.IP_Menu_Text = Label(self)
        self.IP_Menu_Text["text"] = "\n\tSelect the IP to connect and disconnect from :\t\n"
        self.IP_Menu_Text.pack(fill = X)

        self.IP_Menu_Listbox_IPs = Listbox(self, selectmode = BROWSE)
        self.IP_Menu_Listbox_IPs.insert(END, self.clTCP.sHostIP)
        self.IP_Menu_Listbox_IPs.config(activestyle = "none")
        self.IP_Menu_Listbox_IPs.pack(fill = BOTH)

        self.IP_Menu_Button_Disconnect = Button(self)
        self.IP_Menu_Button_Disconnect["text"] = "Disconnect"
        self.IP_Menu_Button_Disconnect["fg"]   = "red"
        self.IP_Menu_Button_Disconnect.bind("<Button-1>", self.Disconnect)
        self.IP_Menu_Button_Disconnect.pack(fill = X)

        self.IP_Menu_Button_Connect = Button(self)
        self.IP_Menu_Button_Connect["text"] = "Connect"
        self.IP_Menu_Button_Connect["fg"] = "green"
        self.IP_Menu_Button_Connect.bind("<Button-1>", self.Connect)
        self.IP_Menu_Button_Connect.pack(fill = X)

        #1st Paned window end (adds and minsizes)
        self.IP_Menu.add(self.IP_Menu_Text)
        self.IP_Menu.paneconfigure(self.IP_Menu_Text, minsize = 30)
        self.IP_Menu.add(self.IP_Menu_Listbox_IPs)
        self.IP_Menu.paneconfigure(self.IP_Menu_Listbox_IPs, minsize = 200)
        self.IP_Menu.add(self.IP_Menu_Button_Connect)
        self.IP_Menu.paneconfigure(self.IP_Menu_Button_Connect, minsize = 30)
        self.IP_Menu.add(self.IP_Menu_Button_Disconnect)
        self.IP_Menu.paneconfigure(self.IP_Menu_Button_Disconnect, minsize = 30)

        #2nd PanedWindow definitions
        self.Connected_Options = PanedWindow(self, orient = VERTICAL)

        self.Connected_Options_Text = Label(self)
        self.Connected_Options_Text["text"] = "\n\tSelect the operation you wish to execute :\t\n"

        self.Connected_Options_Listbox_Actions = Listbox(self, selectmode = BROWSE)
        self.Connected_Options_Listbox_Actions.insert(END, "Send File")
        self.Connected_Options_Listbox_Actions.insert(END, "Receive File")
        self.Connected_Options_Listbox_Actions.insert(END, "Setup Logging")
        self.Connected_Options_Listbox_Actions.insert(END, "Start Logging")
        self.Connected_Options_Listbox_Actions.insert(END, "Stop Logging")

        #Add all user defined actions
        for ACTIONS in ACTIONS_LIST:
            self.Connected_Options_Listbox_Actions.insert(END, ACTIONS[0])

        self.Connected_Options_Listbox_Actions.config(activestyle = "none")
        self.Connected_Options_Listbox_Actions.bind("<<ListboxSelect>>", self.Options)
        self.Connected_Options_Listbox_Actions.pack(fill = BOTH)

        self.Connected_Options_Execute = Button(self)
        self.Connected_Options_Execute["text"] = "Execute"
        self.Connected_Options_Execute["fg"]   = "green"
        self.Connected_Options_Execute.bind("<Button-1>", self.Execute)
        self.Connected_Options_Execute.pack(fill = X)

        self.Connected_Options_Cancel = Button(self)
        self.Connected_Options_Cancel["text"] = "Cancel"
        self.Connected_Options_Cancel["fg"] = "red"
        self.Connected_Options_Cancel.bind("<Button-1>", self.Cancel)
        self.Connected_Options_Cancel.pack(fill = X)

        #2nd Paned window end (adds)
        self.Connected_Options.add(self.Connected_Options_Text)
        self.Connected_Options.paneconfigure(self.Connected_Options_Text, minsize = 30)
        self.Connected_Options.add(self.Connected_Options_Listbox_Actions)
        self.Connected_Options.paneconfigure(self.Connected_Options_Listbox_Actions, minsize = 200)
        self.Connected_Options.add(self.Connected_Options_Execute)
        self.Connected_Options.paneconfigure(self.Connected_Options_Execute, minsize = 30)
        self.Connected_Options.add(self.Connected_Options_Cancel)
        self.Connected_Options.paneconfigure(self.Connected_Options_Cancel, minsize = 30)

        #3rd PanedWindow definitions
        self.File_Argument_Options = PanedWindow(self, orient = VERTICAL)

        self.File_Arguments_Options_Text_A = Label(self)
        self.File_Arguments_Options_Text_A["text"] = "File location on local computer : "
        self.File_Arguments_Options_Text_A.pack(side = "left")
        self.File_Arguments_Options_Entry_A = Entry(self, width = 100)
        self.File_Arguments_Options_Entry_A.pack()

        self.File_Arguments_Options_Text_B = Label(self)
        self.File_Arguments_Options_Text_B["text"] = "File destination on remote computer : "
        self.File_Arguments_Options_Text_B.pack(side = "left")
        self.File_Arguments_Options_Entry_B = Entry(self, width = 100)
        self.File_Arguments_Options_Entry_B.pack()

        #3rd Paned window end (adds)
        self.File_Argument_Options.add(self.File_Arguments_Options_Text_A)
        self.File_Argument_Options.paneconfigure(self.File_Arguments_Options_Text_A, minsize = 30)
        self.File_Argument_Options.add(self.File_Arguments_Options_Entry_A)
        self.File_Argument_Options.paneconfigure(self.File_Arguments_Options_Entry_A, minsize = 30)
        self.File_Argument_Options.add(self.File_Arguments_Options_Text_B)
        self.File_Argument_Options.paneconfigure(self.File_Arguments_Options_Text_B, minsize = 30)
        self.File_Argument_Options.add(self.File_Arguments_Options_Entry_B)
        self.File_Argument_Options.paneconfigure(self.File_Arguments_Options_Entry_B, minsize = 30)

        #4th PanedWindow definitions
        self.Logging_Argument_Options = PanedWindow(self, orient = VERTICAL)

        self.Logging_Arguments_Options_Text_A = Label(self)
        self.Logging_Arguments_Options_Text_A["text"] = "File name : "
        self.Logging_Arguments_Options_Text_A.pack(side = "left")
        self.Logging_Arguments_Options_Entry_A = Entry(self, width = 100)
        self.Logging_Arguments_Options_Entry_A.pack()

        self.Logging_Arguments_Options_Text_B = Label(self)
        self.Logging_Arguments_Options_Text_B["text"] = "File location : "
        self.Logging_Arguments_Options_Text_B.pack(side = "left")
        self.Logging_Arguments_Options_Entry_B = Entry(self, width = 100)
        self.Logging_Arguments_Options_Entry_B.pack()

        #4th Paned window end (adds)
        self.Logging_Argument_Options.add(self.Logging_Arguments_Options_Text_A)
        self.Logging_Argument_Options.paneconfigure(self.Logging_Arguments_Options_Text_A, minsize = 30)
        self.Logging_Argument_Options.add(self.Logging_Arguments_Options_Entry_A)
        self.Logging_Argument_Options.paneconfigure(self.Logging_Arguments_Options_Entry_A, minsize = 30)

        self.Logging_Argument_Options.add(self.Logging_Arguments_Options_Text_B)
        self.Logging_Argument_Options.paneconfigure(self.Logging_Arguments_Options_Text_B, minsize = 30)
        self.Logging_Argument_Options.add(self.Logging_Arguments_Options_Entry_B)
        self.Logging_Argument_Options.paneconfigure(self.Logging_Arguments_Options_Entry_B, minsize = 30)

        #Main frame setup end (adds and pack)
        self.Upper_Lair.add(self.IP_Menu)
        self.Upper_Lair.paneconfigure(self.IP_Menu, minsize = 300)
        self.Main.add(self.Upper_Lair)
        self.Main.paneconfigure(self.Upper_Lair, minsize = 290)
        self.Main.add(self.Lower_Lair)
        self.Main.paneconfigure(self.Lower_Lair, minsize = 120)
        self.Main.pack()

    # @Define   Connect to the selected IP
    #
    # @Return   True : Success, False : Error
    #
    def Connect(self, event):

        #If we are not connected
        if not self.bIsConnected:

            #IP selected from the list
            try:
                self.clTCP.sSelected_IP = self.IP_Menu_Listbox_IPs.get(self.IP_Menu_Listbox_IPs.curselection())
            except:
                print("No IP selected!\n")
                return False

            self.Upper_Lair.add(self.Connected_Options)
            self.Upper_Lair.paneconfigure(self.Connected_Options, minsize = 300)
            self.Upper_Lair.pack()
            self.Lower_Lair.pack()

            self.bIsConnected = True

        #If we are connected
        else:
            print("We are already connected!\n")

        return True

    # @Define   Disconnect from the selected IP
    #
    # @Return   True : Success, False : Error
    #
    def Disconnect(self, event):

        self.clTCP.sSelected_IP = ""

        #If we are connected
        if self.bIsConnected:

            self.Lower_Lair.forget(self.File_Argument_Options)
            self.Lower_Lair.forget(self.Logging_Argument_Options)
            self.Lower_Lair.pack()
            self.Upper_Lair.forget(self.Connected_Options)
            self.Upper_Lair.pack()

            self.bIsConnected = False

        #If we are not connected
        else:
            print("We are not connected!\n")

        return True

    # @Define   Execute the selected action
    #
    def Execute(self, event):

        #If nothing is selected exception
        try:
            sAction = self.Connected_Options_Listbox_Actions.get(self.Connected_Options_Listbox_Actions.curselection())
        except:
            if self.sSelected_Action == "Action_?":
                return False
            else:
                sAction = self.sSelected_Action

        if sAction == "Send File":

            sLocation = self.File_Arguments_Options_Entry_A.get()
            sDestination = self.File_Arguments_Options_Entry_B.get()

            #Bad input
            if sLocation == "" or sDestination == "":
                self.clTCP.PrintTCP("Bad input!\n")
                return False

        if sAction == "Receive File":

            sDestination = self.File_Arguments_Options_Entry_A.get()
            sLocation = self.File_Arguments_Options_Entry_B.get()

            #Bad input
            if sLocation == "" or sDestination == "":
                print("Bad input!\n")
                return False

        if sAction == "Setup Logging":
            sLogFileName = self.Logging_Arguments_Options_Entry_A.get()
            sLogFilePosition = self.Logging_Arguments_Options_Entry_B.get()

            #Bad input
            if sLogFileName == "" or sLogFilePosition == "":
                self.clTCP.PrintTCP("Bad input!\n")
                return False

        self.clTCP.PrintTCP("{0}{1}{2}".format("Executing the following action : ", sAction, " : \n"))

        #Execute method(s) called here
        if sAction == "Send File":
            return self.clTCP.Actions(1, sLocation, sDestination)

        elif sAction == "Receive File":
            return self.clTCP.Actions(2, sLocation, sDestination)

        elif sAction == "Setup Logging":
            return self.clTCP.SetupLogging(sLogFileName, sLogFilePosition)

        elif sAction == "Start Logging":
            return self.clTCP.StartLogging()

        elif sAction == "Stop Logging":
            return self.clTCP.StopLogging()

        else:
            try:
                for ACTIONS in ACTIONS_LIST:
                    if ACTIONS[0] == sAction:
                        getattr(sys.modules[__name__], "%s" % (ACTIONS[0]))(self.clTCP)
            except:
                self.clTCP.PrintTCP("\nCould not find action %s!\n\n" % (sAction))

        return False

    # @Define   Cancel the selected action
    #
    # @TODO
    #
    def Cancel(self, event):

        #Cancel method(s) called here
        return

    # @Define   Update option(s) screen depending on action selection
    #
    def Options(self, event):

        #Get selected action
        try:
            self.sSelected_Action = self.Connected_Options_Listbox_Actions.get(self.Connected_Options_Listbox_Actions.curselection())
            self.Lower_Lair.forget(self.File_Argument_Options)
            self.Lower_Lair.forget(self.Logging_Argument_Options)
        except:
            self.Lower_Lair.pack()
            return

        #Pack GUI depending on action selected
        if self.sSelected_Action == "Send File":
            self.Lower_Lair.add(self.File_Argument_Options)
            self.Lower_Lair.pack()
        elif self.sSelected_Action == "Receive File":
            self.Lower_Lair.add(self.File_Argument_Options)
            self.Lower_Lair.pack()
        elif self.sSelected_Action == "Setup Logging":
            self.Lower_Lair.add(self.Logging_Argument_Options)
            self.Lower_Lair.pack()
        elif self.sSelected_Action == "Start Logging":
            self.Lower_Lair.pack()
        elif self.sSelected_Action == "Stop Logging":
            self.Lower_Lair.pack()
        else:
            self.Lower_Lair.pack()

# @Define   Graphical User Interface (GUI) to launch a server
#
# @Author   Maxime Lagadec
# @Date     1/13/2018
#
class GUI_Server(Frame) :

    #Variables definition
    bIsConnected = False
    iConnections = 0

    clTCP = TCP(True)

    # @Define   Initialisation of the class
    # @Define   Launch the GUI
    #
    def __init__(self, master = None):

        Frame.__init__(self, master)

        self.master = master
        self.pack()
        self.CreateWidgets()

    # @Define   GUI frame definition and bindings
    #
    def CreateWidgets(self):

        #Main frame setup
        self.Main = PanedWindow(self, orient = VERTICAL)

        #1st PanedWindow definitions
        self.Menu = PanedWindow(self, orient = VERTICAL)
        self.Menu_Button_Disconnect = Button(self, height = 1, width = 25)
        self.Menu_Button_Disconnect["text"] = "Disconnect"
        self.Menu_Button_Disconnect["fg"]   = "red"
        self.Menu_Button_Disconnect.bind("<Button-1>", self.Disconnect)
        self.Menu_Button_Disconnect.pack(fill = X)

        self.Menu_Button_Connect = Button(self, height = 1, width = 25)
        self.Menu_Button_Connect["text"] = "Connect"
        self.Menu_Button_Connect["fg"] = "green"
        self.Menu_Button_Connect.bind("<Button-1>", self.Connect)
        self.Menu_Button_Connect.pack(fill = X)

        #1st Paned window end (adds)
        self.Menu.add(self.Menu_Button_Connect)
        self.Menu.add(self.Menu_Button_Disconnect)

        #Main frame setup end (adds and pack)
        self.Main.add(self.Menu)
        self.Main.pack()

    # @Define   Connect the server
    #
    # @Return   True : Success, False : Error
    #
    def Connect(self, event):

        #If we are not connected
        if not self.bIsConnected:

            #Connection method(s) called here
            if not self.clTCP.Connect():
                print("Could not connect properly!\n")
                return False

            self.bIsConnected = True

        #If we are connected
        else:
            print("We are already connected!\n")

        return True

    # @Define   Disconnect the server and all it's connections
    #
    # @Return   True : Success, False : Error
    #
    def Disconnect(self, event):

        #If we are connected
        if self.bIsConnected:

            #Disconnection method(s) called here
            if not self.clTCP.Disconnect():
                print("Could not disconnect properly!\n")
                return False

            self.bIsConnected = False

        #If we are not connected
        else:
            print("We are not connected!\n")

        return True


from django.shortcuts import render, get_object_or_404, redirect, reverse
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ValidationError
from django.core.validators import validate_email
from django.core.mail import send_mail
from .models import NewletterSubscribers

def newsletter(request):
    """ Add user newsletter signups to the database """

    if request.method == 'POST':
        # get email address from form
        email = request.POST['newsletter']
        # if user already has already subscripbed
        if NewletterSubscribers.objects.filter(email=email).exists():
            messages.error(request, 'Already subscribed')
            return redirect('/')
        # if new email
        else:
            # saves form to database
            NewletterSubscribers.objects.create(email=email)
            # send email confirmation to user
            send_mail(
                'North Yeast Brewing',
                'Hey, You have subscriped to the North Yeast Brewing newletter.\
                 We will send you all the lastest events and offers',
                'northyeastbrewing.com',
                [email],
                fail_silently=False,
            )

        context = {
            'email': email,
        }

    return render(request, 'newsletter/newsletter_success.html', context)

@login_required
def send_newsletter(request):
    """" Gets user input then sends a email to newsletter subscriber\
        with vaild email addresss """
    if not request.user.is_superuser:
        messages.error(request, 'Sorry, you must be admin to add products.')
        return redirect(reverse('home'))
    if request.method == 'POST':
        # Get data from form
        subject = request.POST['subject']
        message = request.POST['message']

        # Get emal list from database
        email_list = []
        invalid_email = 0
        sent_emails = 0

        for subscriber in NewletterSubscribers.objects.all():
            email_list.append(subscriber.email)

        for email in email_list:
            # vaildate email and send to vaid email address
            try:
                validate_email(email)
            except ValidationError as e:
                invalid_email = invalid_email + 1
            else:
                sent_emails = sent_emails + 1
                send_mail(
                    subject,
                    message,
                    'northyeastbrewing.com',
                    [email],
                    fail_silently=False,
                )
        messages.success(request, f"Sent newsletter to {sent_emails} \
                         email address. { invalid_email} could not sent ")

    return render(request, 'newsletter/send_newsletter.html')

@login_required
def view_email_list(request):
    """ Allow admin to view email list """
    if not request.user.is_superuser:
        messages.error(request, 'Sorry, you must be admin to edit emails.')
        return redirect(reverse('home'))
    email_list = NewletterSubscribers.objects.all()

    context = {
        'email_list': email_list,
    }

    return render(request, 'newsletter/view_email_list.html', context)

@login_required()
def delete_mail(request, email_id):
    """ Allow admin to delete email subscriber from list """
    if not request.user.is_superuser:
        messages.error(request, 'Sorry, you must be admin\
             to delete newsletter emails.')
        return redirect(reverse('home'))
    # find email in database
    email = get_object_or_404(NewletterSubscribers, pk=email_id)
    # delete email address from databese
    email.delete()
    # get new mailing list from database
    email_list = NewletterSubscribers.objects.all()

    context = {
        'email_list': email_list,
    }
    return render(request, 'newsletter/view_email_list.html', context)

@login_required()
def edit_mail(request, email_id):
    """ Allow admin to edit and delete from email list """

    if request.method == 'POST':
        updated_email = request.POST['updated-email']
        email = get_object_or_404(NewletterSubscribers, pk=email_id)
        email.email = updated_email
        email.save()

    email_list = NewletterSubscribers.objects.all()
    context = {
        'email_list': email_list,
    }

    return render(request, 'newsletter/view_email_list.html', context)


import torch
import torch.nn as nn
from torch.nn import functional as F

import requests

# Save the link to the data, to later be able to delete it from the directory to save storage
link_to_data = f"https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt"
filename = "tiny_shakespeare.txt"

# HYPERPARAMETERS
batch_size = 32
block_size = 8 # what is the maximum context length for predictions?
max_iters = 3000
eval_interval = 300
learning_rate = 1e-2
device = "cpu"
eval_iters = 200
n_embed = 32

torch.manual_seed(1337)

# GET DATA
def download_data(link, filename):
    r = requests.get(link_to_data)

    with open(filename, "w") as f:
        f.write(r.text)
# download_data(link=link_to_data, filename=filename)

# LOAD DATA
with open(filename, "r", encoding="utf-8") as f:
    text = f.read()

# CREATE ENCODING AND DECODING FUNCTIONS
chars = sorted(list(set(text)))
vocab_size = len(chars)
stoi = {s: i for i, s in enumerate(chars)}
itos = {i: s for i, s in enumerate(chars)}
encode = lambda s: [stoi[c] for c in s]
decode = lambda l: ''.join([itos[i] for i in l])

# CREATE TRAIN AND VAL SPLITS
data = torch.tensor(encode(text), dtype=torch.long)
n = int(0.9 * len(data))
train_data = data[:n]
val_data = data[n:]

# FUNCTION TO GET A BATCH OF DATA
def get_batch(split: str = "train"):
    data = train_data if split == "train" else val_data
    ix = torch.randint(len(data) - block_size, (batch_size, ))
    x = torch.stack([data[i: i+block_size] for i in ix])
    y = torch.stack([data[i+1: i+block_size+1] for i in ix])
    x, y = x.to(device), y.to(device)
    return x, y

# FUNCTIONS TO ESTIMATE LOSS
@torch.no_grad()
def estimate_loss():
    out = {}
    model.eval()

    for split in ["train", "val"]:
        losses = torch.zeros(eval_iters)

        for i in range(eval_iters):
            # Get a batch of data
            X, y = get_batch(split)
            # Make predictions
            logits, loss = model(X, y)
            losses[i] = loss.item()

        out[split] = losses.mean()
    model.train()
    return out

# CREATE A SIMPLE BASELINE BIGRAM MODEL
class BigramLanguageModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.token_embedding_table = nn.Embedding(vocab_size, n_embed)
        self.pos_embedding_table = nn.Embedding(block_size, n_embed)
        self.lm_head = nn.Linear(n_embed, vocab_size)

    def forward(self, idx, targets=None):
        B, T = idx.shape

        # idx and targets are both (B,T) tensor of integers
        tok_emb = self.token_embedding_table(idx) # (B,T,C)
        pos_emb = self.pos_embedding_table(torch.arange(T, device=device)) # (T, C)
        x = tok_emb + pos_emb # (B, T, C)
        logits = self.lm_head(x) # (B, T, vocab_size)

        if targets is None:
            loss = None
        else:
            B, T, C = logits.shape
            logits = logits.view(B*T, C)
            targets = targets.view(B*T)
            loss = F.cross_entropy(logits, targets)

        return logits, loss

    def generate(self, idx, max_new_tokens):
        # idx is (B, T) array of indices in the current context
        for _ in range(max_new_tokens):
            # get the predictions
            logits, loss = self(idx)
            # focus only on the last time step
            logits = logits[:, -1, :] # becomes (B, C)
            # apply softmax to get probabilities
            probs = F.softmax(logits, dim=-1) # (B, C)
            # sample from the distribution
            idx_next = torch.multinomial(probs, num_samples=1) # (B, 1)
            # append sampled index to the running sequence
            idx = torch.cat((idx, idx_next), dim=1) # (B, T+1)
        return idx

model = BigramLanguageModel().to(device)

# CREATE A PYTORCH OPTMIZER
optimizer = torch.optim.AdamW(model.parameters(), lr=learning_rate)

# TRAIN THE MODEL
for i in range(max_iters):

    if i % eval_interval == 0:
        losses = estimate_loss()
        print(f"Epoch: {i}/{max_iters}: train loss: {losses['train']:.4f} | val loss: {losses['val']:.4f}")

    # Sample a batch of data
    xb, yb = get_batch()

    # Make predictions
    logits, loss = model(xb, yb)
    optimizer.zero_grad(set_to_none=True)
    loss.backward()
    optimizer.step()

# GENERATE TEXT FROM THE MODEL
context = torch.zeros((1, 1), dtype=torch.long)
print(decode(model.generate(context, max_new_tokens=500)[0].tolist()))


import urllib
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup

import os

from newspaper import Article, Source

user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
headers={'User-Agent':user_agent,}

list_of_newspapers = ['20Minutos', 'ElMundo', 'ElPais', 'LaVanguardia']
list_of_url = ['https://www.20minutos.es/nacional/', 'https://www.elmundo.es/espana', 'https://elpais.com/espana', 'https://www.lavanguardia.com/politica']

for i,url in enumerate(list_of_url):

  newspaper_name = list_of_newspapers[i]

  # Opens up the connection and grabs the requested webpage.

  request = urllib.request.Request(url, None, headers)

  news_connection = uReq(request)

  html = news_connection.read()
  news_connection.close()

  news_soup = soup(html, "html.parser")

  # Finds the articles on the main page.
  containers = news_soup.find_all("article")

  # Creates a .csv file of the articles.

  if not os.path.isdir("newsPapers/" + newspaper_name):
    os.makedirs("newsPapers/" + newspaper_name)

  # Loops through all the articles on the technology page.

  try:
    for container in containers:
      article_link = str(container.a['href'])
      if newspaper_name == "ElPais":
        article_link = 'https://elpais.com' + article_link
      article = Article(article_link, language = 'es')
      article.download()
      article.parse()

      # Gets the data from the website.
      article_title = article.title

      article_authors = str(article.authors)

      date = article.publish_date

      year, month, day = str(date).split()[0].split("-")

      article_text = article.text.replace("\n"," ")

      article.nlp()
      article_keywords = str(article.keywords)
      artcle_summary = article.summary.replace("\n"," ")

      if not os.path.isdir("newsPapers/" +  newspaper_name + "/" + year + "-" + month + "-" + day):
        os.makedirs("newsPapers/" + newspaper_name + "/" + year + "-" + month + "-" + day)

      file_name = article_title.replace(u'\xa0', u' ').replace("|","").replace('"',"").replace("?","").replace("¿","").replace("'","").replace(":"," ").replace("/","")

      fp = open("newsPapers/" + newspaper_name + "/" + year + "-" + month + "-" + day + "/" + file_name + ".txt","w", encoding="utf-8")
      fp.write("title: " + article_title + "\n")
      fp.write("nlp_summary: " + artcle_summary + "\n")
      fp.write("text: " + article_text + "\n")
      fp.write("nlp_keywords: " + article_keywords + "\n")
      fp.write("link: " + article_link + "\n")
      fp.write("authors: " + article_authors + "\n")

      fp.close()

  except Exception as e:
    print(e)
    print(article_link)


from __future__ import unicode_literals

import logging

from mopidy import core

import pykka

from .input import InputThread, Handler

logger = logging.getLogger(__name__)

class VolumeHandler(Handler):
  def __init__(self, device_fn, event_type, event_code, actor_ref):
    super(VolumeHandler, self).__init__(device_fn, event_type, event_code)
    self.actor_proxy=actor_ref.proxy()

  def handle(self, event):
    logger.debug("Event for volume {0}".format(event.value))
    self.actor_proxy.change_volume(event.value)

class PlaylistHandler(Handler):
  def __init__(self, device_fn, event_type, event_code, actor_ref):
    super(PlaylistHandler, self).__init__(device_fn, event_type, event_code)
    self.actor_proxy=actor_ref.proxy()

  def handle(self, event):
    logger.debug("Event for playlist {0}".format(event.value))
    self.actor_proxy.change_playlist(event.value)

class MuteHandler(Handler):
  def __init__(self, device_fn, event_type, event_code, actor_ref, longpress=5):
    super(MuteHandler, self).__init__(device_fn, event_type, event_code)
    self.actor_proxy=actor_ref.proxy()
    self.longpress=longpress

  def handle(self, event):
    if (event.value==1):
      self.timestamp=event.sec
    if (event.value==0):
      diff=event.sec-self.timestamp
      if (diff>self.longpress):
        logger.debug("Longpress mute")
        self.actor_proxy.halt()
      else:
        logger.debug("Toggle mute")
        self.actor_proxy.toggle_mute()

class InputFrontend(pykka.ThreadingActor, core.CoreListener):
    def __init__(self, config, core):
        super(InputFrontend, self).__init__()
        self.config = config['headless']
        self.core=core

        self.playlists = None
        self.selected_playlist = None
        if self.config["volume_max"] is None:
            self.config["volume_max"]=100

    def on_start(self):
        self.reload_playlists()
        self.change_playlist(0)

        self.inputthread=InputThread()
        self.inputthread.registerHandler(VolumeHandler(self.config["volume_device"], "EV_REL", self.config["volume_axis"], self.actor_ref))
        self.inputthread.registerHandler(PlaylistHandler(self.config["playlist_device"], "EV_REL", self.config["playlist_axis"], self.actor_ref))
        self.inputthread.registerHandler(MuteHandler(self.config["mute_device"], "EV_KEY", self.config["mute_key"], self.actor_ref))
        self.inputthread.start()

    def on_stop(self):
        self.inputthread.stop()

    def halt(self):
        print("Goodbye")

    def change_volume(self, value):
      volume=self.core.mixer.get_volume().get()+value*self.config["volume_step"]
      if volume<0:
          volume=0
      elif volume>self.config["volume_max"]:
          volume=self.config["volume_max"]

      logger.debug("Volume changed: {0}".format(volume))
      self.core.mixer.set_volume(volume)

    def toggle_mute(self):
        mute = not self.core.mixer.get_mute().get()
        logger.debug("Muted: {0}".format(mute))
        self.core.mixer.set_mute(mute)
        if (mute):
            self.core.playback.stop()
        else:
            self.change_playlist(0)

    def change_playlist(self, value):
        self.selected_playlist= (self.selected_playlist+value) % len(self.playlists)
        logger.debug("Change playlist: {0}".format(self.selected_playlist))
        self.core.tracklist.clear()
        logger.debug("Playing {0}".format(self.playlists[self.selected_playlist].uri))
        tracks = self.core.playlists.get_items(self.playlists[self.selected_playlist].uri).get()
        logger.debug(tracks)
        track_uris = [track.uri for track in tracks]
        logger.debug("Tracks: {0}".format(track_uris))
        self.core.tracklist.add(uris=track_uris)
        self.core.playback.play()

    def reload_playlists(self):
        self.playlists = []
        for playlist in self.core.playlists.as_list().get():
            self.playlists.append(playlist)
            logger.debug(playlist)
        self.selected_playlist = 0
        logger.debug("Found {0} playlists.".format(len(self.playlists)))


import pygame as pg

from data.colors import WHITE, COLOR_KEY
from data.config import *
from data.paths import *
import data.languages.english as eng
import data.languages.russian as rus

class StartMenuCaption:
    def __init__(self):
        self.image = pg.image.load(START_MENU_CAPTION_BG).convert_alpha()
        self.bg = pg.transform.scale(self.image, (SCR_W, int(4/15 * SCR_H)))
        self.text_surface = None
        self.set_language("English")

    def set_language(self, language):
        pg.font.init()
        font = pg.font.Font(FONT_1, int(7/60 * SCR_H))
        if language == "English":
            caption = font.render(eng.START_MENU_CAPTION, True, WHITE)
        else:
            caption = font.render(rus.START_MENU_CAPTION, True, WHITE)
        self.text_surface = pg.Surface((caption.get_size()))
        self.text_surface.fill(COLOR_KEY)
        self.text_surface.set_colorkey(COLOR_KEY)
        self.text_surface.blit(caption, (0, 0))

    def update_alpha(self, time, state):
        if state == START_MENU_HIDE:
            alpha = int(255 * (1 - time / START_MENU_ANIMATION_TIME))
        else:
            alpha = int(255 * time / START_MENU_ANIMATION_TIME)
        self.text_surface.set_alpha(alpha)
        self.bg.set_alpha(alpha)

    def reset(self):
        self.text_surface.set_alpha(255)
        self.bg.set_alpha(255)

    def draw(self, surface):
        surface.blit(self.bg, (0, int(11/30 * SCR_H)))
        surface.blit(self.text_surface, (1 / 32 * SCR_W, int(9 / 20 * SCR_H)))


import cv2
import winsound
import tkinter as tk
from tkinter import messagebox
from threading import Thread
from tkinter import ttk
from PIL import Image, ImageTk
import math
from openpyxl import Workbook
from datetime import datetime

# Global variable to track dark mode state
dark_mode = False

# Global variable for animation
angle = 0

# Global variable for Excel workbook and sheet
workbook = Workbook()
sheet = workbook.active

# Function to toggle dark mode
def toggle_dark_mode():
    global dark_mode
    dark_mode = not dark_mode
    update_theme()

# Function to update the GUI theme based on dark mode state
def update_theme():
    bg_color = "#333" if dark_mode else "white"
    fg_color = "white" if dark_mode else "black"
    button_bg = "#4CAF50" if dark_mode else "green"

    root.configure(bg=bg_color)
    title_label.configure(bg=bg_color, fg=fg_color)
    image_label.configure(bg=bg_color)
    start_button.configure(bg=button_bg, fg=fg_color)
    team_label.configure(fg=fg_color, bg=bg_color)

# Function to update the animation
def update_animation():
    global angle
    if motion_detection_running:
        angle += 2  # Rotate by 2 degrees
        if angle >= 360:
            angle = 0
        rotated_logo = original_logo.rotate(angle)
        animated_logo = ImageTk.PhotoImage(rotated_logo)
        image_label.config(image=animated_logo)
        image_label.image = animated_logo
    root.after(50, update_animation)  # Call update_animation every 50ms

# Function to start motion detection
def start_motion_detection():
    global motion_detection_running
    if not motion_detection_running:
        messagebox.showinfo("Motion Detection", "Motion detection is starting.")
        motion_detection_running = True
        Thread(target=motion_detection).start()

# Your provided motion detection code
def motion_detection():
    cam = cv2.VideoCapture(0)
    while cam.isOpened():
        ret, frame = cam.read()
        ret, frame1 = cam.read()
        diff = cv2.absdiff(frame, frame1)
        grey = cv2.cvtColor(diff, cv2.COLOR_RGB2GRAY)
        blur = cv2.GaussianBlur(grey, (5, 5), 0)
        _, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY)
        dilation = cv2.dilate(thresh, None, iterations=3)
        contour, _ = cv2.findContours(dilation, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        for c in contour:
            if cv2.contourArea(c) < 5000:
                continue
            x, y, w, h = cv2.boundingRect(c)
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 138), 2)
            winsound.Beep(1000, 200)

        if cv2.waitKey(10) == ord('a'):
            break
        cv2.imshow("Motion Detection", frame)

        # Save motion detection data to Excel
        save_motion_data()

    cam.release()
    cv2.destroyAllWindows()
    motion_detection_running = False

# Function to save motion detection data to Excel
def save_motion_data():
    global sheet
    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    sheet.append([timestamp, "Motion Detected"])
    workbook.save("motion_detection_data.xlsx")

# Create the tkinter window
root = tk.Tk()
root.title("Security Camera with Frame Detection Powered by AI")
root.configure(bg='white')  # Set default background color

# Load and display an image/logo
original_logo = Image.open("C://Users//Hp//PycharmProjects//python-security camera//sscam.jpg")  # Replace "logo.png" with your image file
original_logo = original_logo.resize((150, 150), Image.ANTIALIAS)
photo = ImageTk.PhotoImage(original_logo)
image_label = tk.Label(root, image=photo)
image_label.image = photo
image_label.pack(pady=20)

# Create a label for the project title
title_label = tk.Label(root, text="Security Camera with Frame Detection Powered by AI", font=("Arial", 16, "bold"),
                       fg="black", bg="white")
title_label.pack(pady=10)

# Create a Start button (centered)
start_button = tk.Button(root, text="Start", command=start_motion_detection, font=("Arial", 12, "bold"), bg="green",
                         fg="white", padx=20, pady=10)
start_button.pack(pady=20)

# Create a label for the team members
team_label = tk.Label(root, text="Team Members:\nAbhi Success, Pranav Mehta, Piyush Kumar, Harshit Soni, Fahim-ul-haq",
                      font=("Arial", 12), fg="black", bg="white")
team_label.pack(pady=10)

# Create a dark mode toggle button
dark_mode_button = tk.Button(root, text="Toggle Dark Mode", command=toggle_dark_mode, font=("Arial", 10, "bold"),
                             bg="gray", fg="white", padx=10, pady=5)
dark_mode_button.pack()

# Initialize motion detection state
motion_detection_running = False

# Start the tkinter main loop
root.geometry("600x500")  # Set the window size
update_theme()  # Initialize theme based on dark mode state
update_animation()  # Start the animation
root.mainloop()


from django.shortcuts import render
from github.api_client import GithubApiClient

def organization_repos(request):
    """Shows the repositories of a organization"""
    if 'organization' in request.POST:
        organization = request.POST['organization']
        api_client = GithubApiClient()
        repositories = api_client.get_repositories_by_organization(organization)
        context = {'organization': organization, 'repositories': repositories}
    else:
        context = {'organization': None}

    return render(request, 'repos/organization_repos.html', context)

def stars_repos(request):
    """Shows the selected amount of repositories with more stars in github"""
    number_of_repos = ''
    if 'number_of_repos' in request.POST:
        number_of_repos = request.POST['number_of_repos']
    if number_of_repos == '':
        number_of_repos = 10
    api_client = GithubApiClient()
    repositories = api_client.get_repositories_by_stars(number_of_repos)
    context = {
        'number_of_repos': number_of_repos,
        'repositories': repositories
    }
    return render(request, 'repos/stars_repos.html', context)


from bs4 import BeautifulSoup
import requests

html = requests.get("http://localhost:8000/index.html")
html_soup = BeautifulSoup(html.content,"html.parser")
judul = html_soup.find('p',class_='judul').text
paragraf = html_soup.find('p',class_ ='paragraf').text

print(judul)
print(paragraf)


# encoding=utf-8

from urllib import urlencode
from oauth2 import Client
from urlparse import urlunparse
import json

class GeoAPI:

  ''' field '''
  client = None
  import hosts
  api = hosts.API_HOST + "/geo/"

  ''' constractor '''
  def __init__(self, client):
    self.client = client

  ''' each API method '''
  def id(self,place_id):
    url = self.api+'id/'+place_id+'.json'
    st,res = self.client.request(url,'GET')
    return st,json.loads(res)

  def nearby_place(self, option):
    url = self.api+'nearby_place.json?'+urlencode(option)
    st,res = self.client.request(url,'GET')
    return st,json.loads(res)

  def reverse_geocode(self,lat,long,accuracy=None,granularity=None,max_results=None,callback=None):
    option = {'lat':lat,'long':long}
    if accuracy: option['accuracy'] = accuracy
    if granularity: option['granularity'] = granularity
    if max_results: option['max_results'] = max_results
    if callback: option['callback'] = callback
    url = self.api+'reverse_geocode.json?'+urlencode(option)
    st,res = self.client.request(url,'GET')
    return st,json.loads(res)

  def search(self,lat=None,long=None,query=None,ip=None,granularity=None,accuracy=None,max_results=None,contained_within=None,street_address=None,callback=None):
    option = {}
    if lat: option['lat'] = lat
    if long: option['long'] = long
    if query: option['query'] = query
    if ip: option['ip'] = ip
    if granularity: option['granularity'] = granularity
    if accuracy: option['accuracy'] = accuracy
    if max_results: option['max_results'] = max_results
    if contained_within: option['contained_within'] = contained_within
    if street_address: option['attribute:street_address'] = street_address
    if callback: option['callback'] = callback
    url = self.api + 'search.json?' + urlencode(option)
    st,res = self.client.request(url,'GET')
    return st,json.loads(res)

  def similar_place(self,lat,long,name,contained_within=None,street_address=None,callback=None):
    option = {'lat':lat,'long':long,'name':name}
    if contained_within: option['contained_within'] = contained_within
    if street_address: option['attribute:street_address'] = street_address
    if callback: option['callback'] = None
    url = self.api + 'similar_place.json?' + urlencode(option)
    st,res = self.client.request(url,'GET')
    return st,json.loads(res)

  def place(self,name,contained_within,token,lat,long,street_address=None,callback=None):
    option = {'name':name,'lat':lat,'long':long,'token':token}
    option['contained_within'] = contained_within
    if street_address: option['attribute:street_address'] = street_address
    if callback: option['callback'] = callback
    url = self.api + 'place.json?' + urlencode(option)
    st,res = self.client.request(url,'GET')
    return st,json.loads(res)


from imutils import face_utils
import numpy as np
import dlib
import cv2
import sys
import binascii
import struct
from PIL import Image, ImageDraw
import scipy
import scipy.misc
import scipy.cluster
from sys import argv
import matplotlib.pyplot as plt

def apply_lipstick(img, rgb):
    if(len(a)):
        name, (i, j)=a[0]
        points= []

        for (x, y) in shape[i:j]:
            points.append([x,y])
        points= np.reshape(points, (-1, 1, 2))
        cv2.fillPoly(img, [points], rgb, 8)

    return img, points

def get_dominant_colour(img_path):
    try:
        NUM_CLUSTERS = 5
        im = Image.open(img_path)
        #im = im.resize((150, 150))      # optional, to reduce time
        ar = np.asarray(im)
        shape = ar.shape
        ar = ar.reshape(scipy.product(shape[:2]), shape[2]).astype(float)
        codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS)
        vecs, dist = scipy.cluster.vq.vq(ar, codes)         # assign codes
        counts, bins = scipy.histogram(vecs, len(codes))    # count occurrences
        index_max = scipy.argmax(counts)                    # find most frequent
        peak = codes[index_max]
        colour = binascii.hexlify(bytearray(int(c) for c in peak)).decode('ascii')
        r = int(peak[0])
        g = int(peak[1])
        b = int(peak[2])
        bgr = (b, g, r)
        return bgr
    except:
        pass
    return (-1,-1,-1)

# define our function for preparing mask
def prepare_mask(polygon, image):
    """Returns binary mask based on input polygon presented as list of coordinates of vertices
    Params:
        polygon (list) - coordinates of polygon's vertices. Ex: [(x1,y1),(x2,y2),...] or [x1,y1,x2,y2,...]
        image (numpy array) - original image. Will be used to create mask of the same size. Shape (H, W, C).
    Output:
        mask (numpy array) - boolean mask. Shape (H, W).
    """
    # create an "empty" pre-mask with the same size as original image
    width = image.shape[1]
    height = image.shape[0]
    mask = Image.new('L', (width, height), 0)
    # Draw your mask based on polygon
    ImageDraw.Draw(mask).polygon(polygon, outline=1, fill=1)
    # Covert to np array
    mask = np.array(mask).astype(bool)
    return mask

def compute_histogram(mask, image):
    """Returns histogram for image region defined by mask for each channel
    Params:
        image (numpy array) - original image. Shape (H, W, C).
        mask (numpy array) - boolean mask. Shape (H, W).
    Output:
        list of tuples, each tuple (each channel) contains 2 arrays: first - computed histogram, the second - bins.

    """
    # Apply binary mask to your array, you will get array with shape (N, C)
    region = image[mask]

    red = np.histogram(region[..., 0].ravel(), bins=256, range=[0, 256])
    green = np.histogram(region[..., 1].ravel(), bins=256, range=[0, 256])
    blue = np.histogram(region[..., 2].ravel(), bins=256, range=[0, 256])

    return [red, green, blue]

def plot_histogram(histograms):
    """Plots histogram computed for each channel.
    Params:
        histogram (list of tuples) - [(red_ch_hist, bins), (green_ch_hist, bins), (green_ch_hist, bins)]
    """

    colors = ['r', 'g', 'b']
    for hist, ch in zip(histograms, colors):
        plt.bar(hist[1][:256], hist[0], color=ch)

Model_PATH =Model_PATH = "C:\\Vivekanandan\\ServiceTransformation\\Web\DigitalAssurance\\Content\\ImageComparison\\shape_predictor_68_face_landmarks.dat"

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(Model_PATH)

#imgPath = r"D:\Solutions\VTO\3.jpg"
#outputFolder = r"D:\Solutions\VTO\Output"
#paletteImages = "D:\\Solutions\\ImageProcessing\\Comparison\\Test1\\LakmAbsoluteArganOilLipColor_BurntBrown.jpg,D:\\Solutions\\ImageProcessing\\Comparison\\Test1\\LakmAbsoluteArganOilLipColor_CrimsonSilk.jpg"
#outputFolder = outputFolder + '\\'

#imgPath = argv[1]
#outputFolder = argv[2]
#paletteImages = argv[3]
#outputFolder = outputFolder + '\\'

imgPath = r"D:\Solutions\VTO\2.jpg"
imgOutputPath = r"D:\Solutions\VTO\2_out.jpg"
imgOutputPath2 = r"D:\Solutions\VTO\2_out_transparency.jpg"
paletteImages = "D:\\Solutions\\ImageProcessing\\Comparison\\Test1\\LakmAbsoluteArganOilLipColor_BurntBrown.jpg,D:\\Solutions\\ImageProcessing\\Comparison\\Test1\\LakmAbsoluteArganOilLipColor_CrimsonSilk.jpg"
imgOutputPath3 = r"D:\Solutions\VTO\2_out_mask.jpg"

img = cv2.imread(imgPath)
overlay = img.copy()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 1)
# loop over the face detections
a=[]
for (i, rect) in enumerate(rects):
    shape = predictor(gray, rect)
    shape = face_utils.shape_to_np(shape)
    a=list(face_utils.FACIAL_LANDMARKS_IDXS.items())

paletteImageList = paletteImages.split(',')
index =1

height = overlay.shape[0]
width = overlay.shape[1]

empty_mask = np.zeros((height, width), dtype=np.uint8)

for paletteImage in paletteImageList:
    #imgOutPath = outputFolder + str(index) + '.jpg'
    bgr = get_dominant_colour(paletteImage)
    print(bgr)
    applied_lips, lips_polygon = apply_lipstick(img,bgr)
    cv2.imwrite(imgOutputPath,applied_lips)

    cv2.fillPoly(empty_mask, lips_polygon, (255))
    res = cv2.bitwise_and(overlay,overlay,mask = empty_mask)

    rect = cv2.boundingRect(lips_polygon) # returns (x,y,w,h) of the rect
    cropped = res[rect[1]: rect[1] + rect[3], rect[0]: rect[0] + rect[2]]

    #bg = np.ones_like(cropped, np.uint8)*255
    #cv2.bitwise_not(bg,bg, mask=empty_mask)
    #dst2 = bg+ res

    mask = prepare_mask(lips_polygon,overlay)
    cv2.imwrite(imgOutputPath3,cropped)

    histograms = compute_histogram(mask, overlay)

    # Let's plot our test results
    plt.figure(figsize=(10, 10))

    plt.subplot(221)
    plt.imshow(overlay)
    plt.title('Image')

    plt.subplot(222)
    plt.imshow(mask, cmap='gray')
    plt.title('Mask')

    plt.subplot(223)
    plot_histogram(histograms)
    plt.title('Histogram')

    plt.show()

    alpha = 0.4  # Transparency factor.
    # Following line overlays transparent rectangle over the image
    image_new = cv2.addWeighted(overlay, alpha, applied_lips, 1 - alpha, 0)
    cv2.imwrite(imgOutputPath2,image_new)
    index = index +1
    break


"""AoC 2018 Day 8"""

# Part 1

test_input = [int(i) for i in "2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2".split()]
# 2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2
# A----------------------------------
#     B----------- C-----------
#                      D-----

def sum_metadata(header):
    """
    Recursive function to sum the metadata
    """
    sum_meta = 0
    n_data, n_meta, *child = header
    while n_data:
        child, new_meta = sum_metadata(child)
        sum_meta += new_meta
        n_data -= 1
    return child[n_meta:], sum_meta + sum(child[:n_meta])

assert sum_metadata(test_input)[1] == 138

with open("day08_input.txt") as f:
    day08 = [int(i) for i in f.read().split()]

print(f'Solution for part 1: {sum_metadata(day08)[1]}')

# Part 2

def parse_dict(header):
    """
    parse the inpu as a dict, with child entries and a metadata_one
    """
    parsed = dict()
    n_data, n_meta, *child = header
    for i in range(1, n_data + 1):
        child, parsed[i] = parse_dict(child)
    parsed['metadata'] = child[:n_meta]
    return child[n_meta:], parsed

def compute_score(parsed):
    """
    If a node has no child node (len == 1), the score is the sum of the
    metadata, else the metadata are indexes of scores of child nodes. If an
    index does not exists, it is 0.
    """
    score = 0
    if len(parsed) == 1:
        # Only metadata
        score += sum(parsed['metadata'])
    else:
        indices = parsed['metadata']
        for indice in indices:
            try:
                child = parsed[indice]
            except KeyError:
                continue
            score += compute_score(child)
    return score

test_l_meta = parse_dict(test_input)[1]
assert compute_score(test_l_meta) == 66

l_meta = parse_dict(day08)[1]
print(f'Solution for part 2: {compute_score(l_meta)}')


import turtle

s = turtle.getscreen()
t = turtle.Turtle()

turtle.title("Input in turtle window")
turtle.setup(1440, 400)

# name = input("What is your name? ")
name = turtle.textinput("Personal info: Name", "What is your name?")
age = int(turtle.numinput("Personal info: Age", "Please, enter your age:"))

greeting = f"Welcome to here {name}!\n" \
           f"{name}, after 10 yers you will be {age + 10} yers old."

turtle.bgcolor("yellow")
t.pencolor('darkviolet')

t.write(greeting, font=("georgia", 30, 'bold italic'), align='center')

# ====================
turtle.ht()
t.ht()
turtle.exitonclick()


n = int(input())
s = input()
ans = 0
if len(set(s)) == 1:
    print(1)
else:
    for i in range(n-1):
        if s[i] != s[i+1]:
            ans += 1
    print(ans+1)


# variables
from db_credentials import datawarehouse_db_config, source1_db_config, source2_db_config, source3_db_config
from sql_queries import source1_db_queries, source2_db_queries, source3_db_queries
from variables import source1_name, source2_name, source3_name
import pgdb

# methods
from etl import etl_process

def main():
    print('Starting etl..')

    #establish connnection to target db
    target_conn = pgdb.connect(**datawarehouse_db_config)

    #loop through credentials for each source db
    # locations db
    for config in source1_db_config:
        try:
            print('Loading db: {}'.format(source1_name))
            etl_process(source1_db_queries, target_conn, config, 'postgres')
        except Exception as error:
            print('etl for {} has error'.format(source1_name))
            print('Error message: {}'.format(error))
            continue

   # patrons db
    for config in source2_db_config:
        try:
            print('Loading db: {}'.format(source2_name))
            etl_process(source2_db_queries, target_conn, config, 'postgres')
        except Exception as error:
            print('etl for {} has error'.format(source2_name))
            print('Error message: {}'.format(error))
            continue

    # librarymanager db
    for config in source3_db_config:
        try:
            print('Loading db: {}'.format(source3_name))
            etl_process(source3_db_queries, target_conn, config, 'postgres')
        except Exception as error:
            print('etl for {} has error'.format(source3_name))
            print('Error message: {}'.format(error))
            continue

    target_conn.close()

if __name__=="__main__":
    main()


import sys
from PyQt5 import QtWidgets, QtCore, QtGui, Qt

POINTER_TOOL = "pointer"
MOVE_TOOL = "move"
ROTATE_TOOL = "rotate"
RESET_TOOL = "reset"
ZOOM_TOOL = "zoom"

# Various cursors for tools
CURSORS = {POINTER_TOOL: Qt.Qt.ArrowCursor,
           MOVE_TOOL: Qt.Qt.SizeAllCursor,
           ROTATE_TOOL: Qt.Qt.OpenHandCursor,
          }

class EditToolBar(QtWidgets.QToolBar):

    toolClickSgl = QtCore.pyqtSignal(str)

    def __init__(self):

        super(EditToolBar, self).__init__('Edit')

        self.toolButtonGroup = QtWidgets.QButtonGroup()

        self.undoTool = QtWidgets.QAction(QtGui.QIcon(':undo'), '&Undo')

        self.resetViewTool = QtWidgets.QAction(QtGui.QIcon(':square'), '&Reset View')
        self.resetViewTool.triggered.connect(self._clickReset)

        self.pointerTool = QtWidgets.QToolButton()
        self.pointerTool.setIcon(QtGui.QIcon(':navigation-2'))
        self.pointerTool.setCheckable(True)
        self.pointerTool.setToolTip('Pointer')
        self.pointerTool.clicked.connect(self._clickPointer)
        self.toolButtonGroup.addButton(self.pointerTool)

        self.moveTool = QtWidgets.QToolButton()
        self.moveTool.setIcon(QtGui.QIcon(':move'))
        self.moveTool.setCheckable(True)
        self.moveTool.setToolTip('Move')
        self.moveTool.clicked.connect(self._clickMove)
        self.toolButtonGroup.addButton(self.moveTool)

        self.rotateTool = QtWidgets.QToolButton()
        self.rotateTool.setIcon(QtGui.QIcon(':sync'))
        self.rotateTool.setCheckable(True)
        self.rotateTool.setToolTip('Rotate')
        self.rotateTool.clicked.connect(self._clickRotate)
        self.toolButtonGroup.addButton(self.rotateTool)

        self.zoomInTool = QtWidgets.QAction(QtGui.QIcon(':maximize'), '&Zoom In')
        self.zoomOutTool = QtWidgets.QAction(QtGui.QIcon(':minimize'), '&Zoom Out')

        self.addAction(self.undoTool)
        self.addAction(self.resetViewTool)

        self.addWidget(self.pointerTool)
        self.addWidget(self.moveTool)
        self.addWidget(self.rotateTool)
        self.addAction(self.zoomInTool)
        self.addAction(self.zoomOutTool)

    def _clickMove(self, event):
        if self.moveTool.isChecked():
            self.toolClickSgl.emit(MOVE_TOOL)

    def _clickRotate(self, event):
        if self.rotateTool.isChecked():
            self.toolClickSgl.emit(ROTATE_TOOL)

    def _clickPointer(self, event):
        if self.pointerTool.isChecked():
            self.toolClickSgl.emit(POINTER_TOOL)

    def _clickReset(self, event):
        # No need to verify checked status, since this isn't a tool
        self.toolClickSgl.emit(RESET_TOOL)


file = open("Day2.txt")
lines = file.read().splitlines()
file.close()

def submarine_position(list_of_instructions):
    horizontal_position = 0
    vertical_position = 0
    aim = 0
    depth = 0

    for i in range(len(list_of_instructions)):
        instruction = list_of_instructions[i].split()[0]
        value = int(list_of_instructions[i].split()[1])

        if instruction == "forward":
            horizontal_position += value
            depth +=  aim * value
        elif instruction == "down":
            vertical_position += value
            aim += value
        elif instruction == "up":
            vertical_position -= value
            aim -= value

    print(horizontal_position * vertical_position)      # part a
    print(horizontal_position * depth)                  # part b

submarine_position(lines)


# -*- coding: utf-8 -*-
"""
Created on

"""

import pygame
from pygame.locals import *
import random
import numpy

clock = pygame.time.Clock()

# Colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)

pygame.init()

FIELDWIDTH = 10
FIELDHEIGHT = 10

field = numpy.zeros((FIELDHEIGHT, FIELDWIDTH), dtype=numpy.int16)

anaconda = [[7,4], [6,4], [5,4], [4,4], [3,4]]

def generateApple(mcintosh):
    notGenerated = True
    while notGenerated:
        newRow = random.randint(0,FIELDHEIGHT - 1)
        newColumn = random.randint(0,FIELDWIDTH - 1)

        notGenerated = False
        for segment in anaconda:
            if segment[0] == newRow and segment[1] == newColumn:
                notGenerated = True
                break

    mcintosh[0] = newRow
    mcintosh[1] = newColumn

mcintosh = [0,0]
generateApple(mcintosh)
counterApple = 0

SURFACE = pygame.display.set_mode((32 * FIELDWIDTH, 32 * FIELDHEIGHT))

font = pygame.font.SysFont("Comic Sans MS", 50, bold=5)
fontCounter = pygame.font.SysFont("Comic Sans MS", 25, bold=2)

gameOverText = font.render("Game Over", False, BLUE)
gameOverRect = gameOverText.get_rect()
gameOverRect.centerx = 16 * FIELDWIDTH
gameOverRect.centery = 16 * FIELDHEIGHT

snake = pygame.image.load("snake.png")

headNorth = snake.subsurface(3 * 64, 0, 64, 64) #1
headNorth = pygame.transform.scale(headNorth, (32, 32))
headSouth = snake.subsurface(4 * 64, 64, 64, 64) #2
headSouth = pygame.transform.scale(headSouth, (32, 32))
headWest = snake.subsurface(3 * 64, 64, 64, 64) #3
headWest = pygame.transform.scale(headWest, (32, 32))
headEast = snake.subsurface(4 * 64, 0, 64, 64) #4
headEast = pygame.transform.scale(headEast, (32, 32))

bodyVertical = snake.subsurface(2 * 64, 64, 64, 64) #5
bodyVertical = pygame.transform.scale(bodyVertical, (32, 32))
bodyHorizontal = snake.subsurface(64, 0, 64, 64) #6
bodyHorizontal = pygame.transform.scale(bodyHorizontal, (32, 32))

tailNorth = snake.subsurface(3 * 64, 2 * 64, 64, 64) #7
tailNorth = pygame.transform.scale(tailNorth, (32, 32))
tailSouth = snake.subsurface(4 * 64, 3 * 64, 64, 64) #8
tailSouth = pygame.transform.scale(tailSouth, (32, 32))
tailWest = snake.subsurface(3 * 64, 3 * 64, 64, 64) #9
tailWest = pygame.transform.scale(tailWest, (32, 32))
tailEast = snake.subsurface(4 * 64, 2 * 64, 64, 64) #10
tailEast = pygame.transform.scale(tailEast, (32, 32))

joint1 = snake.subsurface(0, 0, 64, 64) #11
joint1 = pygame.transform.scale(joint1, (32, 32))
joint2 = snake.subsurface(0, 64, 64, 64) #12
joint2 = pygame.transform.scale(joint2, (32, 32))
joint3 = snake.subsurface(2 * 64, 0, 64, 64) #13
joint3 = pygame.transform.scale(joint3, (32, 32))
joint4 = snake.subsurface(2 * 64, 2 * 64, 64, 64) #14
joint4 = pygame.transform.scale(joint4, (32, 32))

apple = snake.subsurface(0, 3 * 64, 64, 64) #15
apple = pygame.transform.scale(apple, (32, 32))

sprites = []
for i in range(0, 4):
    for j in range(0, 5):
        tempSprite = snake.subsurface(j * 64, i * 64, 64, 64)
        tempSprite = pygame.transform.scale(tempSprite, (32, 32))
        sprites.append(tempSprite)

def drawField(oldDirection):
    field[:,:] = 0

    field[mcintosh[0]][mcintosh[1]] = 15

    tailCoors = anaconda[0]
    nextTailCoors = anaconda[1]

    if tailCoors[0] == nextTailCoors[0]:
        if (nextTailCoors[1] == tailCoors[1] + 1) or ((tailCoors[1] == FIELDWIDTH - 1) and (nextTailCoors[1] == 0)):
            field[tailCoors[0]][tailCoors[1]] = 10
        if (nextTailCoors[1] == tailCoors[1] - 1) or (tailCoors[1] == 0 and (nextTailCoors[1] == FIELDWIDTH - 1)):
            field[tailCoors[0]][tailCoors[1]] = 9
    else:
        if (nextTailCoors[0] == tailCoors[0] + 1) or ((tailCoors[0] == FIELDHEIGHT - 1) and (nextTailCoors[0] == 0)):
            field[tailCoors[0]][tailCoors[1]] = 8
        if (nextTailCoors[0] == tailCoors[0] - 1) or (tailCoors[0] == 0 and (nextTailCoors[0] == FIELDHEIGHT - 1)):
            field[tailCoors[0]][tailCoors[1]] = 7

    headCoors = anaconda[-1]
    if oldDirection == "Up":
        field[headCoors[0]][headCoors[1]] = 1
    if oldDirection == "Down":
        field[headCoors[0]][headCoors[1]] = 2
    if oldDirection == "Left":
        field[headCoors[0]][headCoors[1]] = 3
    if oldDirection == "Right":
        field[headCoors[0]][headCoors[1]] = 4

    for i in range(1, len(anaconda)-1):
        prevCoors = anaconda[i-1]
        nextCoors = anaconda[i+1]
        coors = anaconda[i]
        if coors[0] == prevCoors[0] and coors[0] == nextCoors[0]:
            field[coors[0]][coors[1]] = 6
        if coors[1] == prevCoors[1] and coors[1] == nextCoors[1]:
            field[coors[0]][coors[1]] = 5
        if nextCoors[0] < prevCoors[0]:
            if coors[0] == prevCoors[0]:
                if nextCoors[1] > prevCoors[1]:
                    field[coors[0]][coors[1]] = 14
                else:
                    field[coors[0]][coors[1]] = 12
            if coors[0] == nextCoors[0]:
                if nextCoors[1] > prevCoors[1]:
                    field[coors[0]][coors[1]] = 11
                else:
                    field[coors[0]][coors[1]] = 13
        if nextCoors[0] > prevCoors[0]:
            if coors[0] == prevCoors[0]:
                if nextCoors[1] < prevCoors[1]:
                    field[coors[0]][coors[1]] = 11
                else:
                    field[coors[0]][coors[1]] = 13
            if coors[0] == nextCoors[0]:
                if nextCoors[1] < prevCoors[1]:
                    field[coors[0]][coors[1]] = 14
                else:
                    field[coors[0]][coors[1]] = 12

    for i in range(field.shape[0]):
        for j in range(field.shape[1]):
            #SURFACE.blit(sprites[field[i][j]], j*32, i*32)
            if field[i][j] == 1:
                SURFACE.blit(headNorth, (j * 32, i * 32))
            elif field[i][j] == 2:
                SURFACE.blit(headSouth, (j * 32, i * 32))
            elif field[i][j] == 3:
                SURFACE.blit(headWest, (j * 32, i * 32))
            elif field[i][j] == 4:
                SURFACE.blit(headEast, (j * 32, i * 32))
            elif field[i][j] == 5:
                SURFACE.blit(bodyVertical, (j * 32, i * 32))
            elif field[i][j] == 6:
                SURFACE.blit(bodyHorizontal, (j * 32, i* 32))
            elif field[i][j] == 7:
                SURFACE.blit(tailNorth, (j * 32, i * 32))
            elif field[i][j] == 8:
                SURFACE.blit(tailSouth, (j * 32, i * 32))
            elif field[i][j] == 9:
                SURFACE.blit(tailWest, (j * 32, i * 32))
            elif field[i][j] == 10:
                SURFACE.blit(tailEast, (j * 32, i * 32))
            elif field[i][j] == 11:
                SURFACE.blit(joint1, (j * 32, i * 32))
            elif field[i][j] == 12:
                SURFACE.blit(joint2, (j * 32, i * 32))
            elif field[i][j] == 13:
                SURFACE.blit(joint3, (j * 32, i * 32))
            elif field[i][j] == 14:
                SURFACE.blit(joint4, (j * 32, i * 32))
            elif field[i][j] == 15:
                SURFACE.blit(apple, (j * 32, i * 32))

    counterText = fontCounter.render(str(counterApple), False, RED)
    counterRect = counterText.get_rect()
    counterRect.top = 0
    counterRect.right = 32 * FIELDWIDTH
    SURFACE.blit(counterText, counterRect)

def moveSnake(direction):
    global counterApple
    headSegment = anaconda[-1]

    if direction == "Up":
        newRow = headSegment[0] - 1
        newColumn = headSegment[1]
    if direction == "Down":
        newRow = headSegment[0] + 1
        newColumn = headSegment[1]
    if direction == "Left":
        newRow = headSegment[0]
        newColumn = headSegment[1] - 1
    if direction == "Right":
        newRow = headSegment[0]
        newColumn = headSegment[1] + 1

    if newRow == -1:
        newRow = FIELDHEIGHT-1
    if newRow == FIELDHEIGHT:
        newRow = 0
    if newColumn == -1:
        newColumn = FIELDWIDTH - 1
    if newColumn == FIELDWIDTH:
        newColumn = 0

    for segment in anaconda:
        if segment[0] == newRow and segment[1] == newColumn:
            return False
    anaconda.append([newRow, newColumn])

    if newRow == mcintosh[0] and newColumn == mcintosh[1]:
        counterApple = counterApple + 1
        generateApple(mcintosh)
    else:
        anaconda.pop(0)

    return True

SPEEDSNAKE = 4
SPEED = 60

gameCounter = 0
isRunning = True
direction = "Up"
oldDirection = "Up"

gameState = "Idle"
#gameStarted = False
while isRunning:
    if gameState != "GameOver":
        SURFACE.fill(WHITE)

    for event in pygame.event.get():
        if event.type == QUIT:
            isRunning = False
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                isRunning = False
            if event.key == K_RETURN:
                gameState = "Started"

            if gameState != "GameOver":
                if event.key == K_UP and oldDirection != "Down":
                    direction = "Up"
                if event.key == K_DOWN and oldDirection != "Up":
                    direction = "Down"
                if event.key == K_LEFT and oldDirection != "Right":
                    direction = "Left"
                if event.key == K_RIGHT and oldDirection != "Left":
                    direction = "Right"

    if gameState == "Started" and gameCounter % (SPEED / SPEEDSNAKE) == 0:
        oldDirection = direction
        if not moveSnake(direction):
            gameState = "GameOver"

            #isRunning = False

    drawField(oldDirection)

    if gameState == "GameOver":
         SURFACE.blit(gameOverText, gameOverRect)

    clock.tick(SPEED)

    pygame.display.update()

    if gameState == "Started":
        gameCounter = gameCounter + 1

pygame.quit()