import re
import urllib.request
import sys
import time
import datetime

#function to import strings into a log
def redirect_to_file(text):
	original = sys.stdout
	sys.stdout = open('log.txt','a')
	print(text)
	sys.stdout = original

#function to stamp time for logging
def timestamper():
    ts = time.time()
    timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
    redirect_to_file(timestamp + "\n")

try:
    #stamp the time for log
    timestamper()

    url_btc = "https://coinmarketcap.com/currencies/bitcoin" #Bitcoin
    #Open url and decode utf8
    data_btc = urllib.request.urlopen(url_btc).read()
    data1_btc = data_btc.decode("utf-8")
    m = re.search('id="quote_price">', data1_btc)
    start = m.end()
    end = start + 15

    newString = data1_btc[start:end]
    #print(newString)
        #Exclude the shit, all we care about is the price.
    for char in '':
            newString = newString.replace(char,"")
        #Simply print out price
    print("Current Bitcoin price:\n" + newString)
    redirect_to_file("BTC " + newString)
    url_eth = "https://coinmarketcap.com/currencies/ethereum" #Ethereum
    #Open url and decode utf8
    data_eth = urllib.request.urlopen(url_eth).read()
    data1_eth = data_eth.decode("utf-8")

    m = re.search('id="quote_price">', data1_eth)
    start = m.end()
    end = start + 15

    newString = data1_eth[start:end]
    #print(newString)
        #Exclude the shit, all we care about is the price.
    for char in '':
            newString = newString.replace(char,"")
        #Simply print out price
    print("Current Ethereum price:\n" + newString)
    redirect_to_file("ETH " + newString)
    url_pivx = "https://coinmarketcap.com/currencies/pivx" #Pivx
    #Open url and decode utf8
    data_pivx = urllib.request.urlopen(url_pivx).read()
    data1_pivx = data_pivx.decode("utf-8")

    m = re.search('id="quote_price">', data1_pivx)
    start = m.end()
    end = start + 15

    newString = data1_pivx[start:end]
    #print(newString)
        #Exclude the shit, all we care about is the price.
    for char in '':
            newString = newString.replace(char,"")
        #Simply print out price
    print("Current PIVX price:\n" + newString)
    redirect_to_file("PIVX " + newString)
    url_dash = "https://coinmarketcap.com/currencies/dash" #Dash
    #Open url and decode utf8
    data_dash = urllib.request.urlopen(url_dash).read()
    data1_dash = data_dash.decode("utf-8")

    m = re.search('id="quote_price">', data1_dash)
    start = m.end()
    end = start + 15

    newString = data1_dash[start:end]
    #print(newString)
        #Exclude the shit, all we care about is the price.
    for char in '':
            newString = newString.replace(char,"")
        #Simply print out price
    print("Current Dash price:\n" + newString)
    redirect_to_file("Dash " + newString)
    url_dcr = "https://coinmarketcap.com/currencies/decred" #Decred
    #Open url and decode utf8
    data_dcr = urllib.request.urlopen(url_dcr).read()
    data1_dcr = data_dcr.decode("utf-8")

    m = re.search('id="quote_price">', data1_dcr)
    start = m.end()
    end = start + 15

    newString = data1_dcr[start:end]
    #print(newString)
        #Exclude the shit, all we care about is the price.
    for char in '':
            newString = newString.replace(char,"")
        #Simply print out price
    print("Current Decred price:\n" + newString)
    redirect_to_file("Decred " + newString)
    url_nxs = "https://coinmarketcap.com/currencies/nexus" #Nexus
    #Open url and decode utf8
    data_nxs = urllib.request.urlopen(url_nxs).read()
    data1_nxs = data_nxs.decode("utf-8")

    m = re.search('id="quote_price">', data1_nxs)
    start = m.end()
    end = start + 15

    newString = data1_nxs[start:end]
    #print(newString)
        #Exclude the shit, all we care about is the price.
    for char in '':
            newString = newString.replace(char,"")
        #Simply print out price
    print("Current Nexus price:\n" + newString)
    redirect_to_file("NXS " + newString)
    url_storj = "https://coinmarketcap.com/currencies/storj" #Storj
    #Open url and decode utf8
    data_storj = urllib.request.urlopen(url_storj).read()
    data1_storj = data_storj.decode("utf-8")

    m = re.search('id="quote_price">', data1_storj)
    start = m.end()
    end = start + 15

    newString = data1_storj[start:end]
    #print(newString)
        #Exclude the shit, all we care about is the price.
    for char in '':
            newString = newString.replace(char,"")
        #Simply print out price
    print("Current Storj price:\n" + newString)
    redirect_to_file("Storj " + newString)
    url_pay = "https://coinmarketcap.com/currencies/tenx" #Tenx
    #Open url and decode utf8
    data_pay = urllib.request.urlopen(url_pay).read()
    data1_pay = data_pay.decode("utf-8")

    m = re.search('id="quote_price">', data1_pay)
    start = m.end()
    end = start + 15

    newString = data1_pay[start:end]
    #print(newString)
        #Exclude the shit, all we care about is the price.
    for char in '':
            newString = newString.replace(char,"")
        #Simply print out price
    print("Current TenX price:\n" + newString)
    redirect_to_file("TenX " + newString)
    #Finishes "neatly" the logging
    redirect_to_file("\n---End of search---")
except:
    print("Something went wrong bitch, see http://downforeveryoneorjustme/coinmarketcap.com")


# -*- coding: utf-8 -*-
"""
Created on Mon Nov 16 23:59:36 2020

@author: Mayur Vishwasrao
"""

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score,confusion_matrix
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Flatten,Dense,Embedding,LSTM,Dropout
from tensorflow.keras.preprocessing.text import one_hot
from tensorflow.keras.preprocessing.sequence import pad_sequences
import nltk
import re
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
from mlxtend.plotting import plot_confusion_matrix
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt

df = pd.read_csv("D:/fakenews/fake-news/train.csv")
df.head()

# check null values
df.isnull().sum()

# drop null values from the dataset
df = df.dropna()

x = df.drop('label',axis = 1)
y = df['label']

# Input Data for Pre-processing
X_train = x.copy()

# reset index as we have removed null values from the dataset
X_train.reset_index(inplace=True)

# Download stopwords from nltk
nltk.download("stopwords")

# Pre-processing of text data using NLP techniqies like regex, stemming, etc.
ps = PorterStemmer()
x_train_corpus = []
for i in range(0,len(X_train)):
    cl_df = re.sub('[^a-zA-Z]',' ',X_train['title'][i])
    cl_df = cl_df.lower()
    cl_df = cl_df.split()

    cl_df = [ps.stem(word) for word in cl_df if not word in stopwords.words("english")]
    cl_df = " ".join(cl_df)
    x_train_corpus.append(cl_df)

#Vocabulary Size
voc_size = 5000
onehot_train = [one_hot(words,voc_size) for words in x_train_corpus]
onehot_train

# Sentence length
sent_length = 20
embeded_x_train = pad_sequences(onehot_train,padding= 'pre',maxlen = sent_length)
embeded_x_train

embedding_vector_features = 40
model = Sequential()
model.add(Embedding(voc_size,embedding_vector_features,input_length = sent_length))
model.add(Dropout(0.2))
model.add(LSTM(100))
model.add(Dropout(0.2))
model.add(Dense(1,activation='relu'))
model.compile(optimizer = 'adam',loss ='binary_crossentropy',metrics=['accuracy'])

x_final = np.array(embeded_x_train)
y_final = np.array(y)
x_train,x_test,y_train,y_test = train_test_split(x_final,y_final,test_size=0.2,random_state=42)

model.summary()

model_df = model.fit(x_train,y_train,validation_data =(x_test,y_test),batch_size=64,epochs=10)

y_pred = model.predict_classes(x_test)
accuracy_score(y_test,y_pred)

mat = confusion_matrix(y_test,y_pred)
plot_confusion_matrix(mat,figsize=(6,6),show_normed=True)

# Testing on Test Data
Test = pd.read_csv("D:/fakenews/fake-news/test.csv")
Test_id = Test["id"]

# Drop unnecessary clumns from the dataset
X_test = Test.drop(['text','id','author'],axis=1)

# check null values from the columns
X_test.isnull().sum()

# filling missing values with Fake text as submit file is of 5200 rows, so we can't remove the missing values
X_test.fillna('Fake',inplace=True)

X_test.shape

# pre-processing of text dataset
ps = PorterStemmer()
x_test_corpus = []
for i in range(0,len(X_test)):
    cl_df_test = re.sub('[^a-zA-Z]',' ',X_test['title'][i])
    cl_df_test = cl_df_test.lower()
    cl_df_test = cl_df_test.split()

    cl_df_test = [ps.stem(word) for word in cl_df_test if not word in stopwords.words("english")]
    cl_df_test = " ".join(cl_df_test)
    x_test_corpus.append(cl_df_test)

#Vocabulary Size
voc_size = 5000
onehot_test = [one_hot(words,voc_size) for words in x_test_corpus]
onehot_test

sent_length = 20
embeded_x_test = pad_sequences(onehot_test,padding= 'pre',maxlen = sent_length)
embeded_x_test

# predict on test dataset
x_test_final = np.array(embeded_x_test)
y_test_pred = model.predict_classes(x_test_final)

# apeend the prediction into new list and create new dataframe with columns "id", and "label"
val=[]
for i in y_test_pred:
    val.append(i[0])

submission = pd.DataFrame({'id':Test_id, 'label':val})
submission.shape

# write final dataframe to csv
submission.to_csv('D:/fakenews/fake-news/submit.csv',index=False)


"""
math_object Provides helper functions for pulling the LaTeX show_template_manager from the equations database

update method: https://stackoverflow.com/questions/34774409/build-a-dynamic-update-query-in-psycopg2
https://www.psycopg.org/docs/sql.html#
Type Hinting Tuple: https://stackoverflow.com/questions/47533787/typehinting-tuples-in-python
Update borrowed from: https://stackoverflow.com/questions/34774409/build-a-dynamic-update-query-in-psycopg2
"""

__author__ = "William DeShazer"
__version__ = "0.1.0"
__license__ = "MIT"

from typing import Tuple
from collections import defaultdict
from warnings import warn
from psycopg2.sql import SQL, Identifier, Placeholder, Composed
from psycopg2 import connect, OperationalError, Binary
from psycopg2.extras import NamedTupleCursor
from latex_template import compile_pattern, template
from config import config

class RecordIDTypeError(UserWarning):
    """UserWarning for EquationGroup"""

class ImageWithoutTemplateIDError(UserWarning):
    """UserWarning for EquationGroup"""

class NoRecordIDError(UserWarning):
    """UserWarning for EquationGroup"""

class MathObject:
    """Base class for Equations, Variables, and Units"""
    def __init__(self, table: str, parent_table: str, verbose: bool = True):
        """Constructor for MathObject"""
        self.table = table
        self.parent_table = parent_table  # For equations it is eqn_group. For variables it is equations
        self.records: dict = self.all_records(verbose=verbose)
        self.last_inserted = None
        self.id_name = self.table + "_id"
        self.records_for_parent = None

    def all_records(self, as_columns: bool = True, verbose: bool = False):
        """Returns all records for math_object"""
        # sql = 'SELECT * FROM {aTable}'.format(aTable=self.table)
        sql = "SELECT * FROM {tbl}"
        query = SQL(sql).format(tbl=Identifier(self.table))

        db_params = config()
        conn = connect(**db_params)

        cur = conn.cursor(cursor_factory=NamedTupleCursor)

        if verbose:
            print('Extracting records from Table: {aTable}'.format(aTable=self.table))

        try:
            cur.execute(query)
        except OperationalError as error:
            print(error)

        records = cur.fetchall()

        cur.close()
        conn.close()

        if as_columns is True:
            records = self.as_columns(records)

        return records

    def insert(self, parent_id: int = None, name: str = None,
               data=None, latex: str = None, notes: str = None,
               image: bytes = None, template_id: int = None, dimensions: int = 1,
               insertion_order: int = None, created_by: str = None, unit_id: int = 1,
               verbose: bool = None):
        """Insert New Record Into math_object"""

        if data is None:
            data = {}

        db_params = config()

        next_id: int = self.record_count_total(verbose=verbose) + 1

        if name is None:
            name = "{aTable} {ID:d}".format(ID=next_id, aTable=self.table)
        if created_by is None:
            created_by = db_params['user']

        if latex is None:
            latex = 'a^2 + b^2 = c^2'

        the_template = template(version=template_id, verbose=verbose)
        a_template = the_template.data
        template_id = the_template.id

        if image is None:
            # If a_template is None the most recent show_template_manager is used
            image = Binary(compile_pattern(pattern=latex, aTemplate=a_template, verbose=verbose))

        data.update({
            'name': name,
            'latex': latex,
            'image': image,
            'notes': notes,
            'dimensions': dimensions,
            'unit_id': unit_id,
            'template_id': template_id,
            'created_by': created_by
        })

        sql = 'INSERT INTO {table} ({fields}) VALUES ({values}) RETURNING *'

        keys = data.keys()

        query = SQL(sql).format(table=Identifier(self.table),
                                fields=SQL(', ').join(map(Identifier, keys)),
                                values=SQL(', ').join(map(Placeholder, keys)))

        conn = connect(**db_params)

        if verbose:
            print(query.as_string(conn))

        cur = conn.cursor(cursor_factory=NamedTupleCursor)

        if verbose:
            print('Adding new record to Table: {aTable}'.format(aTable=self.table))

        try:
            cur.execute(query, data)
        except OperationalError as error:
            print(error)

        new_records = cur.fetchall()

        conn.commit()

        cur.close()
        conn.close()

        if parent_id is not None:
            for record in new_records:
                self.associate_parent(parent_id=parent_id, child_id=record.id, insertion_order=insertion_order)

        self.last_inserted = self.as_columns(new_records)

        self.append(new_records)

    def update(self, an_id: id = None, where_key: str = None, name: str = None,
               data=None, latex: str = None, notes: str = None, unit_id: int = None,
               image: bytes = None, template_id: int = None, dimensions: int = None,
               modified_by: str = None, created_by: str = None,
               verbose: bool = None):
        """Insert New Record Into math_object"""

        if where_key is None:
            where_key = self.id_name

        if an_id is None:
            warn("No Record ID Specified", NoRecordIDError)
        else:
            if data is None:
                data = {}

            db_params = config()

            data.update(self._add_field('name', name))
            data.update(self._add_field('notes', notes))
            data.update(self._process_latex(latex, image, template_id, verbose))
            data.update(self._add_field('dimensions', dimensions))
            data.update(self._add_field('unit_id', unit_id))
            data.update(self._add_field('created_by', created_by))

            # If there is no data, then skip. Of course one could still change modified by:
            if len(data) > 0 or modified_by is not None:

                # Always require a modified by and because one can change data without specifying a modifer,
                # this is necessary. We don't check it before the previous if, because we don't want to create
                # a modified_by if not data was set and no modified_by was set.
                if modified_by is None:
                    modified_by = db_params['user']

                data.update(modified_by=modified_by)

                fields = data.keys()

                sql = "UPDATE {table} SET {fields} WHERE {pkey} = {a_value} RETURNING *"

                query = SQL(sql).format(table=Identifier(self.table),
                                        fields=SQL(', ').join(
                                            Composed([Identifier(k), SQL(' = '), Placeholder(k)]) for k in fields
                                        ),
                                        pkey=Identifier(where_key),
                                        a_value=Placeholder('where_key')
                                        )

                data.update(where_key=an_id)

                conn = connect(**db_params)
                cur = conn.cursor(cursor_factory=NamedTupleCursor)

                if verbose:
                    print(query.as_string(conn))
                    print(cur.mogrify(query, data))

                try:
                    cur.execute(query, data)
                except OperationalError as error:
                    print(error)

                new_record = cur.fetchall()

                conn.commit()

                cur.close()
                conn.close()

                self.last_inserted = self.as_columns(new_record)

                self.records = self.all_records()

    @staticmethod
    def _process_latex(latex, image, template_id, verbose: bool = False):
        """Helper function to process latex, image and template_id flags"""
        data = {}

        if latex is not None or image is not None or template_id is not None:

            only_template_changed = template_id is not None and latex is None and image is None
            only_latex_changed = latex is not None and image is None  # regardless of template_id state
            # always need template_id when image is manually changed
            acceptable_image_change = image is not None and template_id is not None

            recompile = only_latex_changed or only_template_changed
            if recompile:
                the_template = template(version=template_id, verbose=verbose)
                a_template = the_template.data
                template_id = the_template.id
                image = Binary(compile_pattern(pattern=latex, aTemplate=a_template, verbose=verbose))

                data.update(image=image, template_id=template_id)
                if only_latex_changed:
                    data.update(latex=latex)
            elif acceptable_image_change:
                # Note if template_id is not specified
                data.update(image=image, template_id=template_id)
                if latex is not None:
                    data.update(latex=latex)
            else:
                warn('User Input Images must also specify show_template_manager used', ImageWithoutTemplateIDError)
        return data

    @staticmethod
    def _add_field(key: str = None, value=None):
        data = {}
        if value is not None:
            data = {key: value}
        return data

    def append(self, new_records):
        """Append new records to existing records. Because the data is stored as a dictionary instead of
        NamedTuples, it requires a helper function to append new data"""
        res = self.records

        for data in new_records:
            # noinspection PyProtectedMember
            record: dict = data._asdict()
            for key, value in record.items():
                res[key].append(value)

        self.records = res

    # Simple return statement. More sophisticated ones will have to be purpose built
    def values_for_fields(self, where_key: str = None, where_values: Tuple[int, ...] = None,
                          name: bool = True, as_column: bool = True, verbose: bool = False, **kwargs):
        """where_key could be 'id', 'name', etc.
            For any other field, you just set the fieldname to True"""

        if where_key is None:
            where_key = self.id_name
        fields = [self.id_name]

        if name is True:
            fields.append('name')
        for a_key in kwargs:
            if kwargs[a_key] is True:
                fields.append(a_key)

        db_params = config()
        conn = connect(**db_params)

        if where_values is None:
            sql = "SELECT {fields} FROM {table}"
            query = SQL(sql).format(table=Identifier(self.table),
                                    fields=SQL(', ').join(map(Identifier, fields)))
            if verbose:
                print(query.as_string(conn))
        else:
            sql = "SELECT {fields} from {table} where {pkey} IN %s"
            query = SQL(sql).format(table=Identifier(self.table),
                                    fields=SQL(', ').join(map(Identifier, fields)),
                                    pkey=Identifier(where_key)
                                    )
            if verbose:
                print(query.as_string(conn))

        cur = conn.cursor(cursor_factory=NamedTupleCursor)

        if verbose:
            print('Extracting records from Table: {aTable}'.format(aTable=self.table))

        try:
            cur.execute(query, where_values)
        except OperationalError as error:
            print(error)

        records = cur.fetchall()

        cur.close()
        conn.close()

        if as_column is True:
            records = self.as_columns(records)

        return records

    def record_count_total(self, verbose: bool = False) -> int:
        """Get the total record count for {table}""".format(table=self.table)
        sql = "SELECT COUNT(*) from {table}"

        query = SQL(sql).format(table=Identifier(self.table))

        db_params = config()
        conn = connect(**db_params)

        cur = conn.cursor(cursor_factory=NamedTupleCursor)

        if verbose:
            print('Getting Count of Records in table: {table}'.format(table=self.table))

        cur.execute(query)  # self.table))

        record_count = cur.fetchone()

        cur.close()
        conn.close()

        return record_count[0]

    def record_count_for_parent(self, parent_id: Tuple[int, ...] = None,
                                verbose: bool = False):
        """Return record count for Parent Table"""
        sql = ''

        if isinstance(parent_id, tuple):
            sql = "SELECT COUNT(*) from {table} WHERE {parent_key} IN %s"
        elif isinstance(parent_id, int):
            sql = "SELECT COUNT(*) from {table} WHERE {parent_key}= %s"
        else:
            warn("Unrecognized type for parent_id", RecordIDTypeError)

        query = SQL(sql).format(table=Identifier(self.join_table()),
                                parent_key=Identifier(self.parent_key())
                                )

        db_params = config()
        conn = connect(**db_params)

        cur = conn.cursor(cursor_factory=NamedTupleCursor)

        if verbose:
            print('Getting Count of Records in table: {table} for Group ID: {gid}'.format(table=self.join_table(),
                                                                                gid=parent_id))
            print(query.as_string(conn))
            if isinstance(parent_id, int):
                cur.mogrify(query, parent_id)
            elif isinstance(parent_id, tuple):
                cur.mogrify(query, (parent_id, ))

        try:
            cur.execute(query, (parent_id,))
        except TypeError:
            cur.execute(query, parent_id)  # self.table))
        except OperationalError as error:
            print(error)

        record_count = cur.fetchone()

        cur.close()
        conn.close()

        return record_count[0]

    def associate_parent(self, parent_id: int = None, child_id: int = None,
                         insertion_order: int = None, inserted_by: str = None,
                         data: dict = None, verbose: bool = False):
        """Associate the parent and child tables using parent id. Insertion_order and inserted_by are optional"""
        parent_key = self.parent_table + '_id'
        self_key = self.table + '_id'

        join_table = self.table + '_' + self.parent_table

        db_params = config()

        if data is None:
            data = {}

        if inserted_by is None:
            inserted_by = db_params['user']

        data.update(
            {parent_key: parent_id, self_key: child_id},
            insertion_order=insertion_order,
            inserted_by=inserted_by
        )

        sql = 'INSERT INTO {table} ({fields}) VALUES ({values})'

        keys = data.keys()

        query = SQL(sql).format(table=Identifier(join_table),
                                fields=SQL(', ').join(map(Identifier, keys)),
                                values=SQL(', ').join(map(Placeholder, keys)))

        conn = connect(**db_params)

        if verbose:
            print(query.as_string(conn))

        cur = conn.cursor(cursor_factory=NamedTupleCursor)

        if verbose:
            print('Adding new record to Table: {aTable}'.format(aTable=join_table))

        try:
            cur.execute(query, data)
        except OperationalError as error:
            print(error)

        conn.commit()

        cur.close()
        conn.close()

    def get_records_for_parent(self, parent_id: Tuple[int, ...] = None, as_columns: bool = True,
                               verbose: bool = False):
        """Get the {table} records for {parent}""".format(table=self.table, parent=self.parent_table)
        db_params = config()

        sql = "SELECT * FROM {child_table} c INNER JOIN {join_table} j " \
              " USING({id_name}) WHERE j.{parent_id_name} = %s ORDER BY insertion_order;"

        query = SQL(sql).format(
            child_table=Identifier(self.table),
            join_table=Identifier(self.join_table()),
            id_name=Identifier(self.id_name),
            parent_id_name=Identifier(self.parent_key())
        )

        conn = connect(**db_params)

        cur = conn.cursor(cursor_factory=NamedTupleCursor)

        if verbose:
            print(query.as_string(conn))
            if isinstance(parent_id, int):
                cur.mogrify(query, parent_id)
            elif isinstance(parent_id, tuple):
                cur.mogrify(query, (parent_id, ))

        try:
            cur.execute(query, (parent_id, ))
        except TypeError:
            cur.execute(query, parent_id)
        except OperationalError as error:
            print(error)

        records = cur.fetchall()

        cur.close()
        conn.close()

        if as_columns is True:
            records = self.as_columns(records)

        return records

    def records_not_in_parent(self, parent_id: Tuple[int, ...], as_columns: bool = True, verbose: bool = False):
        """Get the {table} records for {parent}""".format(table=self.table, parent=self.parent_table)
        db_params = config()
        sql = ''

        sql = "SELECT * FROM {child_table} WHERE {id_name} NOT IN" \
              " (SELECT {id_name} FROM {join_table} WHERE {parent_id_name} = %s);"

        # self_dict = dict(table='equation', join_table= lambda: 'equation_eqn_group',
        #                  id_name='equation_id', parent_key = lambda : 'eqn_group_id')
        # MyTuple = namedtuple('MyTuple', self_dict)
        # self = MyTuple(**self_dict)

        query = SQL(sql).format(
            child_table=Identifier(self.table),
            join_table=Identifier(self.join_table()),
            id_name=Identifier(self.id_name),
            parent_id_name=Identifier(self.parent_key())
        )

        conn = connect(**db_params)

        cur = conn.cursor(cursor_factory=NamedTupleCursor)

        if verbose:
            print(query.as_string(conn))
            if isinstance(parent_id, int):
                cur.mogrify(query, (parent_id, ))
            elif isinstance(parent_id, tuple):
                cur.mogrify(query, parent_id)

        try:
            cur.execute(query, (parent_id, ))
        except TypeError:
            cur.execute(query, parent_id)
        except OperationalError as error:
            print(error)

        records = cur.fetchall()

        if as_columns is True:
            records = self.as_columns(records)

        cur.close()
        conn.close()
        return records

    def parent_key(self):
        """Parent ID String to be used as a Key in the table"""
        return self.parent_table + '_id'

    def self_key(self):
        """Self ID String to be used as a Key in the table"""
        return self.table + '_id'

    def join_table(self):
        """Join table which is a concatenation of the table and the parent table"""
        return self.table + '_' + self.parent_table

    # NamedTuple uses underscores to prevent name collision _make, _replace, _asdict, _fields
    # noinspection PyProtectedMember
    @staticmethod
    def as_columns(records):
        """Helper function to turn NameTuple records into column-like dictionaries"""
        res = defaultdict(list)

        for data in records:
            record = data._asdict()
            for key, value in record.items():
                res[key].append(value)
        return res


# -*- coding: utf-8 -*-

"""
This is a custom version of the postgresql_psycopg2 adaptor that
overrides last_insert_id so that it works with inherited tables.
Instead of using pg_get_serial_sequence, this finds the default value
of the primary key for the table and parse out the sequence name from
that.

As of May 2015 this is still required: It replaces last_insert_id, a
function that returns the last ID of a sequence. The implementation in
Django 1.6.11 and more recent versions (like 1.8.2) does the following:

  SELECT CURRVAL(pg_get_serial_sequence(tablename, pk_name))

Unfortunately, in Postgres 9.4 (and I assume in earlier versions, too)

  pg_get_serial_sequence(tablename, pk_name)

can't work with sequences defined in parent tables if inheritance is
used. CATMAID uses table inheritance and Django this Postgres function.
So it has to be worked around this problem.

Performance-wise, this should not be a big problem. this extra query is
also done in Django's version (even if in a much simpler version). The
function last_insert_id is only used for insert statements through the
ORM.
"""

from django.db.backends.base.base import NO_DB_ALIAS

# CATMAID uses PostGIS, so we need to make sure we wrap the PostGIS backend
from django.contrib.gis.db.backends.postgis.base import DatabaseWrapper as PostGISDatabaseWrapper
from django.contrib.gis.db.backends.postgis.operations import PostGISOperations

import re
import sys

class DatabaseError(Exception):
    pass

class DatabaseOperations(PostGISOperations):
    def last_insert_id(self, cursor, table_name, pk_name):
        # Get the default value for the column name:
        cursor.execute('''
SELECT adsrc
  FROM pg_attrdef pad, pg_attribute pat, pg_class pc
  WHERE pc.relname=%s AND
        pc.oid=pat.attrelid AND
        pat.attname=%s AND
        pat.attrelid=pad.adrelid AND
        pat.attnum=pad.adnum
''', (table_name, pk_name))
        # The default value should look like:
        #   nextval('concept_id_seq'::regclass)
        result_row = cursor.fetchone()
        if not result_row:
            # Then there's no column of that name, which may mean, for
            # example, that this is a managed join table with no "id"
            # column:
            return None
        default_value = result_row[0]
        m = re.search(r'nextval\(\'(.*?)\'::regclass\)', default_value)
        if not m:
            raise DatabaseError("Couldn't find the sequence for column '%s' in table '%s'" % (pk_name, table_name))
        cursor.execute("SELECT CURRVAL(%s)", (m.group(1),))
        return cursor.fetchone()[0]

class DatabaseWrapper(PostGISDatabaseWrapper):
    def __init__(self, *args, **kwargs):
        super(DatabaseWrapper, self).__init__(*args, **kwargs)
        # Don't override operations for Non-DB connections
        if kwargs.get('alias', '') != NO_DB_ALIAS:
            self.ops = DatabaseOperations(self)


"""ISBN-10 Validation 5kuy"""

# SBN-10 identifiers are ten digits long. The first nine characters are digits 0-9. The last digit can be 0-9 or X,
# to indicate a value of 10.
# An ISBN-10 number is valid if the sum of the digits multiplied by their position modulo 11 equals zero.

# Идентификаторы ISBN-10 состоят из десяти цифр. Первые девять символов-это цифры 0-9.
# Последняя цифра может быть 0-9 или X, чтобы указать значение 10.
# Число ISBN-10 допустимо, если сумма цифр, умноженная на их положение по модулю 11, равна нулю.

# Пример:
# ISBN     : 1 1 1 2 2 2 3 3 3  9
# position : 1 2 3 4 5 6 7 8 9 10
#
# Это действительный ISBN, потому что:
# (1*1 + 1*2 + 1*3 + 2*4 + 2*5 + 2*6 + 3*7 + 3*8 + 3*9 + 9*10) % 11 = 0

import re

def valid_ISBN10(isbn):
    pattern = r"[\d]|(?:X$)"
    number_list = re.findall(pattern,isbn)
    if len(number_list) == 10:
        result_sum = 0
        for index, number in enumerate(map(lambda item: int(item) if item.isdigit() else 10, number_list), 1):
            result_sum += number * index
        if not result_sum % 11:
            return True
    return False

test_list = [('1112223339', True),
             ('048665088X', True),
             ('1293000000', True),
             ('1234554321', True),
             ('1234512345', False),
             ('1293', False),
             ('X123456788', False),
             ('ABCDEFGHIJ', False),
             ('XXXXXXXXXX', False)
             ]

color = ['\033[32m', '\033[31m', '\033[38m']

for string, correct in test_list:
    result = valid_ISBN10(string)
    if result == correct:
        print(
            f"{color[0]}Тест OK: {correct}{color[2]} а вышло так: {color[0]}{result}{color[2]}")
    else:
        print(
            f"{color[1]}Тест Fail:{color[2]}{color[0]} {correct}{color[2]} а вышло так: {color[1]}{result}{color[2]}")


"""
169. Majority Element
https://leetcode.com/problems/majority-element/

Given an array nums of size n, return the majority element.

The majority element is the element that appears more than ⌊n / 2⌋ times.
You may assume that the majority element always exists in the array.

Example:
Input: nums = [3,2,3]
Output: 3
"""

# Runtime: 172ms
class Solution:
    def majorityElement(self, nums: List[int]) -> int:

        # Thought Process:
        # 1. Using a hash map (dict), to keep the count of each distinct element in the list
        # 2. We filter the dict to find the single element who is the majority
        # 3. Assuming the filtering is guaranteed one elemement being the majority,
        # 4. This means that the the filter dict will likely return only 1 value

        hash_map = {}

        for num in nums:
            hash_map[num] = hash_map.get(num, 0) + 1

        return list(dict(filter(lambda val: val[1] > (len(nums) / 2), hash_map.items())))[0]


import re
import math
from datetime import datetime
from random import randint, random
from unittest import TestCase
from enum import IntEnum
from functools import partial

__author__ = 'hisehyin'
__datetime__ = '15/12/25 上午10:50'

OPTION_SIGN = {0: '+', 1: '-', 2: '*', 3: '/'}

OPTION_PRINT = {'+': ' + ', '-': ' - ', '*': ' × ', '/': ' ÷ '}

class KouSuan:
    '''
    小学1~3年级数学口算题
    '''
    @staticmethod
    def gen_float_questions(total, carry=False, percent=1):
        '''
        口算题，小数加减
        :param total: 题目数量
        :param carry: 是否允许进退位
        :param percent: 进退位问题比例
        '''
        def __check_carry(option_, num1_, num2_):
            return {
                '-': lambda *args: math.modf(args[0])[0] < math.modf(args[1])[0],
                '+': lambda *args: math.modf(args[0])[0] + math.modf(args[1])[0] > 1
            }[option_](num1_, num2_)

        questions = 0
        regex = re.compile(r'[^0-9\.]')
        while questions < total:
            num1, num2 = randint(15, 200) / 10, randint(15, 200) / 10
            if num1.is_integer() or num2.is_integer():
                continue
            option = OPTION_SIGN[randint(0, 1)]
            check_carry_ = partial(__check_carry, option_=option, num1_=num1, num2_=num2)
            if not carry:
                if check_carry_():
                    continue
            else:
                if random() < percent:
                    if not check_carry_():
                        continue
                else:
                    if check_carry_():
                        continue

            question = '{num1}{option}{num2}'.format(
                num1=num1, num2=num2, option=option)

            result = eval(question)
            if result > 0:
                questions += 1
                option_print = OPTION_PRINT[str(option)]
                question = regex.sub(option_print, question)
                yield question + ' ='

    @staticmethod
    def gen_kousuan_questions(total, max_):
        """
        口算题，加减乘除
        :param total:
        :param min_0:
        :return:
        """
        questions = 0
        regex = re.compile(r'\D')
        while questions < total:
            option = OPTION_SIGN[randint(0, 3)]
            if option == '*':
                question = '{num1}{option}{num2}'.format(num1=randint(0, 9),
                                                         option=option,
                                                         num2=randint(0, 9))
            elif option == '/':
                question = '{num1}{option}{num2}'.format(num1=randint(0, max_),
                                                         option=option,
                                                         num2=randint(1, 9))
            else:
                question = '{num1}{option}{num2}'.format(num1=randint(0, max_),
                                                         option=option,
                                                         num2=randint(0, max_))
            result = eval(question)
            if 0 <= result < max_ + 15:
                if option == '/' and not 1 < result < 10:
                    continue
                elif option == '*' and result < 10:
                    continue

                questions += 1
                option_print = OPTION_PRINT[str(option)]
                question = regex.sub(option_print, question)
                yield question + ' ='

    @staticmethod
    def gen_multiplication_division(total, max_arg1, max_arg2, min_=1, min_arg1=1, min_arg2=1):
        """
        生成乘除法计算题
        :param total: 题目总数
        :param max_arg1: 第一位最大运算数
        :param max_arg2: 第2位最大运算数
        :param min_arg1: 第一位最小运算数
        :param min_arg2: 第2位最小运算数
        :param min_: 结果下限
        """
        questions = 0
        regex = re.compile(r'\D')
        while questions < total:
            option = OPTION_SIGN[randint(2, 3)]
            if randint(1, 100) & 1 == 0:
                question = '{num1}{option}{num2}'.format(num1=randint(min_arg1, max_arg1),
                                                         option='*',
                                                         num2=randint(min_arg2, max_arg2))
            else:
                question = '{num1}{option}{num2}'.format(num1=randint(min_arg1, max_arg1),
                                                         option='/',
                                                         num2=randint(min_arg2, max_arg2))

            result = eval(question)
            if min_ <= result:
                questions += 1
                option_print = OPTION_PRINT[str(option)]
                question = regex.sub(option_print, question)
                yield question + ' ='

    @staticmethod
    def gen_add_sub_questions(total, max_, min_=0, three_element_percent=0, add_carry=False, sub_carry=False):
        """
        生成加减计算题
        :param total: 题目总数
        :param max_: 运算上限,例如20以内加减法
        :param min_: 运算下限
        :param three_element_percent: 三位运算比重
        :param add_carry: 是否允许进位加
        :param sub_carry: 是否允许借位减
        :return: generator
        """
        element_limit = 3 if three_element_percent > 0 else 2
        questions = 0
        while questions < total:
            elements = [randint(0, max_) for _ in range(element_limit)]
            question = ''
            for i, e in enumerate(elements):
                option = ' + ' if randint(0, 1) == 1 else ' - '
                question += str(e)
                if i < len(elements) - 1:
                    question += option
                    if (not sub_carry) and ('-' in option) and (e % 10 < elements[i + 1] % 10):
                        break
                    if (not add_carry) and ('+' in option) and (e % 10 + elements[i + 1] % 10 >= 10):
                        break
                else:
                    if 0 <= eval(question) <= max_:
                        if ('+' in question) and eval(question) < min_:
                            continue
                        yield question + ' = '
                        questions += 1

    @staticmethod
    def gen_3_questions(total, max_1, max_2):
        """
        乘除法计算题
        :param total:
        :param max_1:
        :param max_2:
        :return:
        """
        questions = 0
        pass

class Test(TestCase):
    def setUp(self):
        self.start = datetime.now()
        self.total = 100
        self.max = 100

    def tearDown(self):
        print(datetime.now() - self.start)

    # def test_simple_question(self):
    #     result = KouSuan.gen_add_sub_questions(self.total, 1000, 10, 0, True, True)
    #     for i, e in enumerate(result):
    #         print(e)

    # def test_kousuan(self):
    #     result = KouSuan.gen_kousuan_questions(20, 100)
    #     for i, e in enumerate(result):
    #         print(i, e)

    def test_gen_multiplication_division(self):
        result = KouSuan.gen_multiplication_division(
            total=200, max_arg1=99, max_arg2=10, min_arg1=20,  min_arg2=3, min_=10)
        for e in zip(*[result] * 5):
            print('\t'.join(e))


import sys
import json
import re
import datetime
import github
from github import Github
from common.Instance import get_github_token, get_github_repo, Instance

"""
TODO!
"""

def usage():
    print("python collect_milestones.py ")
    print("    TODO")

def do_the_thing(instance, simple=False):

    # log into GH using token
    g = Github(get_github_token())
    repo = g.get_repo(get_github_repo())
    try:
        milestone_1_14 = repo.get_milestone(number=41)
        milestone_22h1 = repo.get_milestone(number=43)
        milestone_22h2 = repo.get_milestone(number=44)

        labels = {
            "task": repo.get_label("Issue-Task"),
            "feature": repo.get_label("Issue-Feature"),
            "p0": repo.get_label("Priority-0"),
            "p1": repo.get_label("Priority-1"),
            "p2": repo.get_label("Priority-2"),
            "p3": repo.get_label("Priority-3"),
        }

        issues_1_14 = repo.get_issues(milestone=milestone_1_14, state="open")
        issues_22h1 = repo.get_issues(milestone=milestone_22h1, state="open")
        issues_22h2 = repo.get_issues(milestone=milestone_22h2, state="open")

        issues_2022 = issues_1_14.totalCount + issues_22h1.totalCount + issues_22h2.totalCount

        # all_issues = repo.get_issues(state="all").totalCount
        all_issues = repo.get_issues(state="all", sort="created", direction="desc")[0].number

        open_issues = repo.get_issues().totalCount
        open_prs = repo.get_pulls().totalCount

        total_p0s = (
            repo.get_issues(
                milestone=milestone_22h1, state="open", labels=[labels["p0"]]
            ).totalCount
            + repo.get_issues(
                milestone=milestone_22h2, state="open", labels=[labels["p0"]]
            ).totalCount
            + repo.get_issues(
                milestone=milestone_1_14, state="open", labels=[labels["p0"]]
            ).totalCount

        )
        total_p1s = (
            repo.get_issues(
                milestone=milestone_22h1, state="open", labels=[labels["p1"]]
            ).totalCount
            + repo.get_issues(
                milestone=milestone_22h2, state="open", labels=[labels["p1"]]
            ).totalCount
            + repo.get_issues(
                milestone=milestone_1_14, state="open", labels=[labels["p1"]]
            ).totalCount

        )
        total_p2s = (
            repo.get_issues(
                milestone=milestone_22h1, state="open", labels=[labels["p2"]]
            ).totalCount
            + repo.get_issues(
                milestone=milestone_22h2, state="open", labels=[labels["p2"]]
            ).totalCount
            + repo.get_issues(
                milestone=milestone_1_14, state="open", labels=[labels["p2"]]
            ).totalCount

        )
        total_p3s = (
            repo.get_issues(
                milestone=milestone_22h1, state="open", labels=[labels["p3"]]
            ).totalCount
            + repo.get_issues(
                milestone=milestone_22h2, state="open", labels=[labels["p3"]]
            ).totalCount
            + repo.get_issues(
                milestone=milestone_1_14, state="open", labels=[labels["p3"]]
            ).totalCount

        )
        total_tasks = (
            repo.get_issues(
                milestone=milestone_22h1, state="open", labels=[labels["task"]]
            ).totalCount
            + repo.get_issues(
                milestone=milestone_22h2, state="open", labels=[labels["task"]]
            ).totalCount
            + repo.get_issues(
                milestone=milestone_1_14, state="open", labels=[labels["task"]]
            ).totalCount

        )
        total_features = (
            repo.get_issues(
                milestone=milestone_22h1, state="open", labels=[labels["feature"]]
            ).totalCount
            + repo.get_issues(
                milestone=milestone_22h2, state="open", labels=[labels["feature"]]
            ).totalCount
            + repo.get_issues(
                milestone=milestone_1_14, state="open", labels=[labels["feature"]]
            ).totalCount
        )

        if not simple:
            print(f"{issues_1_14.totalCount} issues in 1.14")
            print(f"{issues_22h1.totalCount} issues in 22H1")
            print(f"{issues_22h2.totalCount} issues in 22H2")
            print(f"{open_issues-open_prs}\tissues")
            print(f"{issues_2022}\t2022 issues")
            print(f"{total_p0s}\tp0s")
            print(f"{total_p1s}\tp1s")
            print(f"{total_p2s}\tp2s")
            print(f"{total_p3s}\tp3s")
            print(f"{total_tasks}\ttasks")
            print(f"{total_features}\tfeatures")
            print(f"{open_prs}\tPRs")
            print(f"{all_issues}\tTotal Issues")
        else:
            sys.stdout.write(f"{datetime.date.today().strftime('%m/%d/%Y')},{all_issues},")

        print(
            f"{open_issues-open_prs},{issues_2022},{total_p0s},{total_p1s},{total_p2s},{total_p3s},{total_tasks},{total_features},{open_prs},{issues_22h1.totalCount},{issues_1_14.totalCount}"
        )

    except Exception as e:
        print(e)

def main(argv):

    # if len(argv) < 2:
    #     usage()
    #     return

    instance = Instance()

    if len(argv) == 2:
        command = argv[1]
        if command == "--help" or command == "-h" or command == "-?" or command == "/?":
            usage()
            return
        elif command == "csv":
            do_the_thing(instance, simple=True)
            return
    else:
        do_the_thing(instance)

if __name__ == "__main__":
    main(sys.argv)


import cv2 as cv
import os
import imutils

modelo = 'FotosElon'
ruta1 = '/Users/davidlopez/PycharmProjects/Curso_deteccion_objetos/reconocimiento_contornos/reconocimientofacial1'
rutacompleta = ruta1 + '/' + modelo
if not os.path.exists(rutacompleta):
    os.makedirs(rutacompleta)

ruidos = cv.CascadeClassifier('/Users/davidlopez/PycharmProjects/Curso_deteccion_objetos/reconocimiento_contornos/entrenamientosOpenCVruidos/opencv-master/data/haarcascades/haarcascade_frontalface_default.xml')
#0 es una camara externa
#1 es la camara del pc
#camara= cv.VideoCapture(1)
camara= cv.VideoCapture('ElonMusk.mp4')
id = 0

while True:
    respuesta, captura = camara.read()
    if respuesta==False:break
    captura = imutils.resize(captura, width = 640)
    grises = cv.cvtColor(captura, cv.COLOR_BGR2GRAY)
    idcaptura = captura.copy()
    # cambiando el valor de 1.3 y 5  varias las opciones para encontrar una cara
    cara = ruidos.detectMultiScale(grises, 1.3, 5)
    for(x,y,e1,e2) in cara:
        cv.rectangle(captura, (x,y), (x+e1,y+e2), (255,0,0), 2)
        rostrocapturado = idcaptura[y:y+e1, x:x+e2]
        rostrocapturado= cv.resize(rostrocapturado, (160, 160), interpolation= cv.INTER_CUBIC)
        cv.imwrite(rutacompleta+'/imagen_{}.jpg'.format(id), rostrocapturado)
        id=id+1

    cv.imshow('resultado rostro', captura)

    if id==351:
        break

camara.release()
cv.destroyAllWindows()


# -*- coding: utf-8 -*-
# @Time : 2021/12/23 11:09
# @Author : JingYang
# @File : test_SetMeterTariff.py
import datetime
import time

import requests

from common.marker import *

class Test_Set_Meter_Tariff:

    @asyncTest
    def test_SetMeterTariff(self, requestMessage, dbConnect,device, caseData):
        """使用异步方式设置测试费率到电表"""
        count = 0
        # Step1 生成异步操作读取任务，hes-api，生成running表
        data = caseData('testData/Tariff/setMeterTariff.json')
        requestData = data['test_SetMeterTariff']['request']
        # 设定三分钟异步任务，三分钟后失效
        currentTime = datetime.datetime.now().strftime('%y%m%d%H%M%S')
        endTime = (datetime.datetime.now() + datetime.timedelta(minutes=20)).strftime('%y%m%d%H%M%S')

        requestData['payload'][0]['startTime'] = currentTime
        requestData['payload'][0]['endTime'] = endTime
        requestData['payload'][0]['deviceNo'] = device['device_number']
        transactionId = str(device['device_number']) + '_' + time.strftime('%y%m%d%H%M%S',time.localtime())
        requestData['payload'][0]['transactionId'] = transactionId
        response = requests.post(url=requestMessage, json=requestData)
        assert response.status_code == 200

        # Step2 生成异步任务后，任务正常执行完毕进入his，进入his表，则认为任务结束
        # 过期时间到，也会进入his表，这里暂不考虑
        time.sleep(3)
        # 查询生成Core执行的任务的 auto_run_id
        sql_running = "select auto_run_id from H_TASK_RUNNING where NODE_NO='{}' and JOB_TYPE='SET_TARIFF_CONTENT'".format(
            device['device_number'])
        db_queue = dbConnect.fetchall_dict(sql_running)
        while len(db_queue) == 0 and count < 5:
            time.sleep(5)
            db_queue = dbConnect.fetchall_dict(sql_running)
            print(db_queue)
            print('Waiting for set tariff Tasks to Create...')
            count = count + 1

        # Step3 验证读任务执行结束，task正常移入his表
        # 查询生成Core执行结束后，his表任务状态
        sql_his = "select task_state from H_TASK_RUN_HIS where auto_run_id='{}'".format(db_queue[0]['auto_run_id'])
        db_queue = dbConnect.fetchall_dict(sql_his)
        while len(db_queue) == 0 and count < 40:
            time.sleep(10)
            db_queue = dbConnect.fetchall_dict(sql_his)
            print(db_queue)
            print('Waiting for set tariff Tasks to finish...')
            count = count + 1
        assert db_queue[0]['task_state'] == 3


from pathlib import Path

import solara
from solara.alias import rv
from solara.components.title import Title
from solara.routing import route_level_context

from ..components import Header, Hero

directory = Path(__file__).parent

# md file
md = open(directory / "README.md").read()

title = "Home"

route_order = ["/", "docs", "api", "examples"]

@solara.component
def Page():
    with solara.GridFixed(1, justify_items="center") as main:
        solara.Markdown(md)
    return main

@solara.component
def SimpleListItem(text=None, children=[], class_: str = None, icon_name=None):
    if icon_name is not None:
        children = [rv.Icon(children=[icon_name])] + children
    if text is not None:
        children = [text] + children
    return rv.Html(tag="li", children=children, attributes={"class": class_})

@solara.component
def List(children=[], class_: str = None):
    return rv.Html(tag="ul", children=children, attributes={"class": class_})

@solara.component
def Sidebar():
    route_current, all_routes = solara.use_route()
    router = solara.use_router()
    selected = router.path
    with rv.Col(tag="aside", md=4, lg=3, class_="sidebar bg-grey d-none d-md-block") as main:
        with List():
            for route in all_routes:
                if route.children and route.data is None:
                    with SimpleListItem(route.label):
                        with List():
                            for child in route.children:
                                path = solara.resolve_path(child)
                                with solara.Link(path):
                                    title = child.label or "no label"
                                    if callable(title):
                                        title = "Error: dynamic title"
                                    SimpleListItem(title, class_="active" if path == selected else None)
                else:
                    path = solara.resolve_path(route)
                    with solara.Link(path):
                        SimpleListItem(route.label, class_="active" if path == selected else None)
    return main

@solara.component
def Layout(children=[]):
    router = solara.use_router()
    route_current, all_routes = solara.use_route()
    # get child routes, and restore router level, so we can also render the route of the sidebar for the mobile view
    route_level = solara.use_context(route_level_context)
    route_sidebar_current, all_routes_sidebar = solara.use_route()
    route_level_context.provide(route_level)

    show_left_menu, set_show_left_menu = solara.use_state(False)
    show_right_menu, set_show_right_menu = solara.use_state(False)

    with solara.VBox(grow=False) as main:
        Title(title="Solara documentations")
        Header(
            on_toggle_left_menu=lambda: set_show_left_menu(not show_left_menu),
            on_toggle_right_menu=lambda: set_show_right_menu(not show_right_menu),
        )
        if route_current is not None and route_current.path == "/":
            Hero(
                title="Build large and Scalable Data Apps for Jupyter and Production",
                sub_title="Solara helps you build powerful & scalable data apps faster and easier.",
                button_text="Quickstart",
            )

        with rv.Container(tag="section", fluid=True, ma_0=True, pa_0=True, class_="fill-height mb-8"):
            if route_current is not None and route_current.path == "/":
                with rv.Row(class_="ma-2"):
                    with rv.Col(md=4, offset_md=2, sm=5, offset_sm=1):
                        solara.Markdown(
                            """
                        # What is Solara?

                        Solara lets you build data apps from in Python.

                        Grow from one-off experiments in the Jupyter notebook to highly complex production-grade data apps with confidence.

                        Access the full power of the Python ecosystem. Use your favorite libraries.
                        """
                        )
                        with solara.HBox():
                            with solara.Link("/docs"):
                                solara.Button(label="Read more", class_="ma-1", href="/docs", color="#f19f41", dark=True)
                            with solara.Link("/docs/quickstart"):
                                solara.Button(label="Quickstart", class_="ma-1", color="#f19f41", dark=True)
                    with rv.Col(md=4, sm=5):
                        rv.Img(src="/static/public/landing/what.png", style_="width:900px")

                with rv.Row(class_="ma-8"):
                    with rv.Col(md=4, offset_md=2, sm=5, offset_sm=1):
                        rv.Img(src="/static/public/landing/complexity.png", style_="width:500px")
                    with rv.Col(md=4, sm=5):
                        solara.Markdown(
                            """
                        # Build **large** apps with **low** code complexity

                        With Solara, you can build large scale apps without hitting a complexity wall.

                        With other tools you may hit a dead end due to missing features or implementing features
                        adds too much complexity to your code base.

                        Solara offers the **flexibility** to build complex apps, but keeps the **simplicity** of a small code base.
                        """
                        )
                with rv.Row(class_="ma-8"):
                    with rv.Col(md=4, sm=5, offset_sm=1, offset_md=2):
                        solara.Markdown(
                            """
                        # The trustworthiness of React

                        Using the same API as React, but ported to Python, Solara lets you build apps with the same
                        trustworthiness as React.

                        With a decade of experience, React is battle-tested and proven to be
                        a reliable and robust framework to build large scale apps.
                        """
                        )
                    with rv.Col(md=5, sm=5):
                        rv.Img(src="/static/public/landing/python-love-react.png", style_="width:300px")
                with rv.Row(class_="ma-8"):
                    with rv.Col(md=5, offset_md=2, sm=5, offset_sm=1):
                        solara.Markdown(
                            """
                        ## For any consulting, training or support needs
                        [contact@solara.dev](mailto:contact@solara.dev)
                        """
                        )
            else:
                with rv.Row(style_="gap:6rem; flex-wrap: nowrap;"):
                    if route_current is not None and hasattr(route_current.module, "Sidebar"):
                        route_current.module.Sidebar()  # type: ignore
                    else:
                        Sidebar()
                    with rv.Col(tag="main", md=True, class_="pt-12 pl-12 pr-10", style_="max-width: 1024px; overflow: auto;"):
                        if route_current is not None and route_current.path == "/":
                            with rv.Row(align="center"):
                                pass
                                # with rv.Col(md=6, class_="pa-0"):
                                #     rv.Html(tag="h1", children=["Live Demo"])
                                # with rv.Col(md=6, class_="d-flex", style_="justify-content: end"):
                                #     rv.Btn(elevation=0, large=True, children=["Running App"], color="primary", class_="btn-size--xlarge")
                            # solara.Padding(6)
                        with rv.Row(children=children):
                            pass

            # Drawer navigation for sidebar
            with rv.NavigationDrawer(
                v_model=show_left_menu,
                on_v_model=set_show_left_menu,
                absolute=True,
                hide_overlay=False,
                overlay_color="#000000",
                overlay_opacity=0.5,
            ):
                with rv.List(nav=True):
                    with rv.ListItemGroup(active_class="text--primary"):
                        for route in all_routes:
                            with solara.Link(route):
                                solara.ListItem(route.label)

            # Drawer navigation for top menu
            with rv.NavigationDrawer(
                v_model=show_right_menu,
                on_v_model=set_show_right_menu,
                right=True,
                absolute=True,
                hide_overlay=False,
                overlay_color="#000000",
                overlay_opacity=0.5,
            ):
                with rv.List(nav=True):
                    current_path = router.path
                    with rv.ListItemGroup(active_class="text--primary", v_model=current_path):
                        for route in all_routes_sidebar:
                            # this gets rid of the api/link child routes
                            if len([k for k in route.children if k.label]) == 0:
                                with solara.Link(route):
                                    solara.ListItem(route.label, value=solara.resolve_path(route))
                            else:
                                with solara.ListItem(route.label or "no-title", value=solara.resolve_path(route) + "_no_select"):
                                    for subroute in route.children:
                                        if subroute.label:
                                            with solara.Link(subroute):
                                                with solara.ListItem(subroute.label, value=solara.resolve_path(subroute)):
                                                    pass

    return main


# -*- coding: utf-8 -*-
import random
from datetime import datetime
from django.core.management.base import BaseCommand

from transport_routes.models import (
    Route, Transport
)
from stilsoft_test import constants

class Command(BaseCommand):

    def handle(self, *args, **options):
        route_objects = Route.objects.count()
        if route_objects < 300:
            while route_objects < 300:
                route, status = Route.objects.get_or_create(number=random.randint(1, 9999),
                                                            route_type=random.choice(constants.TRANSPORT_TYPE_LIST),
                                                            is_active=random.choice([True, False]))
                route_objects += 1

                if status:
                    for num in range(5):
                        transport = Transport.objects.create(vehicle_id_number=random.randint(111111, 999999),
                                                             transport_type=route.route_type)
                        transport.save()
                        route.transports.add(transport)

                        print(datetime.now(), 'transport {0} / {1} for route created'.format(transport.vehicle_id_number,
                                                                                transport.transport_type))

                print(datetime.now(), 'route {0} / {1} created'.format(route.number, route.route_type))


import sqlite3
import os
import logging
import struct

#-------------------------------------------------------------------------------
#   Creating a logging with timestamp and regex
#-------------------------------------------------------------------------------

logging_str = "[%(asctime)s: %(levelname)s: %(module)s]: %(message)s"
log_dir = "logs"
os.makedirs(log_dir, exist_ok=True)
logging.basicConfig(filename=os.path.join(log_dir, 'db_operation_logs.log'), level=logging.INFO, format=logging_str, filemode="a")

def create_database(database_name: str):
    """
    Creates a new SQLite database with the given name and a table called "input_output"
    to store input and output data.

    Parameters:
    - database_name (str): The name of the database to create.

    Returns:
    - None
    """
    # Create a connection object
    conn = sqlite3.connect(database_name)
    logging.info("sql server successfully connected")
    # Create a cursor object to execute SQL queries
    cursor = conn.cursor()

    # Create a table to store input and output data
    cursor.execute('''CREATE TABLE IF NOT EXISTS predictions
                    (id INTEGER PRIMARY KEY AUTOINCREMENT,
                    visit REAL,
                    mr_delay REAL,
                    Age REAL,
                    EDUC REAL,
                    SES REAL,
                    CDR REAL,
                    eTIV REAL,
                    nWBV REAL,
                    gender REAL,
                    ASF REAL,
                    MMSE REAL,
                    prediction REAL)''')

    # Commit the changes and close the connection
    conn.commit()
    logging.info("sql table successfully created")
    conn.close()

def save_to_database(database_name: str, visit, mr_delay, Age, EDUC, SES, CDR, eTIV, nWBV, gender, ASF, MMSE, prediction: float):
    """
    Save input variables and output to a SQLite database.

    Parameters:
    visit (float): Visit number.
    mr_delay (float): Delay between visits.
    Age (float): Age of subject.
    EDUC (float): Years of education of subject.
    SES (float): Socioeconomic status of subject.
    CDR (float): Clinical dementia rating.
    eTIV (float): Estimated total intracranial volume.
    nWBV (float): Normalized whole brain volume.
    gender (float): Gender of subject (0 = Female, 1 = Male).
    ASF (float): Atlas scaling factor.
    MMSE (float): Mini-mental state examination score.
    prediction (float): Prediction generated by model.

    Returns:
    None
    """
    # Open a connection to the database
    conn = sqlite3.connect(database_name)
    c = conn.cursor()

    # Insert a new row into the table with the input variables and prediction
    c.execute('''INSERT INTO predictions (visit, mr_delay, Age, EDUC, SES, CDR, eTIV, nWBV, gender, ASF, MMSE, prediction)
                 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''',
                 (visit, mr_delay, Age, EDUC, SES, CDR, eTIV, nWBV, gender, ASF, MMSE, prediction))

    # Commit the changes and close the connection
    logging.info("new data from web-app successfully inserted to db")
    conn.commit()
    conn.close()


'''
Given a number, find sum of its digits.

Examples :

Input : n = 687
Output : 21

Input : n = 12
Output : 3
'''

# n = input()
n=687
sum = 0
# for i in n:
#     sum += int(i)  # sum = sum + int(i)
# print(sum)

while n != 0:
    sum += int(n) % 10
    n=int(n)//10
print(sum)


# -*- coding: utf-8 -*-

from rqalpha import run_file
from datetime import datetime
import os
import sys, getopt

start = "2007-01-01"
end = datetime.now().strftime("%Y-%m-%d")
stocklist_path = ""

opts, args = getopt.getopt(sys.argv[1:], "s:e:f:")
for op, value in opts:
  if op == "-s":
    start = value
  elif op == "-e":
    end = value
  elif op == "-f":
    stocklist_path = value

config = {
  "base": {
    "start_date": start,
    "end_date": end,
    "accounts": {
      "stock": 100000000
    }
  },
  "extra": {
    "log_level": "verbose",
  },
  "mod": {
    "sys_analyser": {
      "enabled": True,
      "plot": False,
      "output_file": "BTresult.pkl",
      "priority": 101
    },
    "run_csv_signals": {
      "enabled": True,
      "stocklist_path": stocklist_path
    },
    "portfolio_persist": {
      "enabled": True
    }
  }
}

strategy_file_path = "./strategy.py"

os.chdir(os.path.dirname(__file__))
run_file(strategy_file_path, config)


from urllib.parse import urlparse

from django.conf import settings
from django.contrib import admin, messages
from django.utils.translation import ngettext
from import_export import resources
from import_export.admin import ImportExportActionModelAdmin

from phonebank.models import Agent, TelnyxCredential, Room, Voter

class AgentResource(resources.ModelResource):

    class Meta:
        model = Agent
        import_id_fields = ('uuid',)

@admin.register(Agent)
class AgentAdmin(ImportExportActionModelAdmin):
    resource_class = AgentResource
    list_display = [
        'nickname', 'email_address', 'room', 'is_active', 'last_active',
        'provided_count',
    ]
    list_filter = [('room', admin.RelatedOnlyFieldListFilter), 'is_active']
    date_hierarchy = 'last_active'
    search_fields = ['uuid', 'email_address', 'nickname']
    actions = ['enable_access', 'disable_access', 'show_access_links']

    @admin.action(permissions=['change'])
    def enable_access(self, request, queryset):
        updated = queryset.filter(is_active=False).update(is_active=True)
        self.message_user(
            request,
            ngettext(
                "{} agent was enabled.",
                "{} agents were enabled.",
                updated,
            ).format(updated),
            messages.SUCCESS,
        )

    @admin.action(permissions=['change'])
    def disable_access(self, request, queryset):
        updated = queryset.filter(is_active=True).update(is_active=False)
        for telnyx_credential in TelnyxCredential.objects.exclude(
            agent__is_active=True,
        ):
            telnyx_credential.delete()
        self.message_user(
            request,
            ngettext(
                "{} agent was disabled.",
                "{} agents were disabled.",
                updated,
            ).format(updated),
            messages.SUCCESS,
        )

    @admin.action(permissions=['view'])
    def show_access_links(self, request, queryset):
        for agent in queryset:
            url = request.build_absolute_uri(agent.get_absolute_url())
            if not settings.DEBUG:
                # Alternatively, set Django setting SECURE_PROXY_SSL_HEADER.
                url = urlparse(url)._replace(scheme='https').geturl()
            self.message_user(
                request,
                "Access link for {}: {}".format(
                    agent.nickname,
                    url,
                ),
            )

@admin.register(Room)
class RoomAdmin(admin.ModelAdmin):
    pass

@admin.register(TelnyxCredential)
class TelnyxCredentialAdmin(admin.ModelAdmin):
    list_display = ['id', 'agent']
    list_filter = [('agent', admin.RelatedOnlyFieldListFilter)]
    search_fields = ['id', 'agent__nickname']

    def delete_queryset(self, request, queryset):
        # Delete selected objects individually instead of in bulk
        # in order to call the model's delete method, which deletes
        # the credential through the Telnyx API.
        for telnyx_credential in queryset:
            telnyx_credential.delete()

class VoterResource(resources.ModelResource):

    class Meta:
        model = Voter

@admin.register(Voter)
class VoterAdmin(ImportExportActionModelAdmin):
    resource_class = VoterResource
    list_display = [
        'id', 'statename', 'name_last', 'name_first', 'name_middle',
        'priority', 'provided_to', 'provided_at',
    ]
    list_filter = [
        'priority',
        ('provided_to', admin.RelatedOnlyFieldListFilter),
        ('statename', admin.AllValuesFieldListFilter),
    ]
    date_hierarchy = 'provided_at'
    search_fields = [
        'id', 'name_last', 'name_first', 'name_middle',
        'cell_phone_1', 'cell_phone_2', 'land_phone_1', 'land_phone_2',
        'notes',
    ]

# Customize admin site appearance.
admin.AdminSite.site_header = "Boran"
admin.AdminSite.site_title = "Phonebank"
admin.AdminSite.index_title = "Data management"


from lib.GImage import GImage, Anchors, Algin, ImageAnchors
from PIL import Image, ImageFont, ImageDraw

def _get_rounded_rectangle(size: tuple[int, int], radius: float, color: tuple[int, int, int, int]):
    base = Image.new(mode="RGBA", size=size)
    box = ImageDraw.Draw(base)
    box.rounded_rectangle(
        xy=((0, 0), size),
        radius=radius,
        fill=color,
        outline=None,
        width=1
    )

    return base

class StatusTile:

    TILE_SIZE = (350, 120)
    TILE_COLOR = (0x00, 0x00, 0x00, 0x64)
    TILE_RADIUS = 15

    def __init__(self, icon_path: str, title: str):
        self.icon_path = icon_path
        self.title = title

    def get_status_tile(self, status: str):
        TILE_SIZE = StatusTile.TILE_SIZE
        X, Y = TILE_SIZE
        TILE_RADIUS = StatusTile.TILE_RADIUS
        TILE_COLOR = StatusTile.TILE_COLOR

        base = GImage(box_size=TILE_SIZE)
        base.paste(
            im=_get_rounded_rectangle(
                size=TILE_SIZE,
                radius=TILE_RADIUS,
                color=TILE_COLOR)
        )
        IMAGE_ANC = Y//2
        base.add_image(
            image_path=self.icon_path,
            box=(IMAGE_ANC, IMAGE_ANC),
            image_anchor=ImageAnchors.MIDDLE_MIDDLE
        )
        TEXT_X = X-Y
        test = GImage(box_size=(TEXT_X, Y))
        test.draw_text(
            text=self.title,
            position=(TEXT_X//2, 20),
            anchor=Anchors.MIDDLE_TOP
        )
        test.draw_text(
            text=status,
            position=(TEXT_X//2, Y-20),
            anchor=Anchors.MIDDLE_BASELINE
        )
        base.paste(im=test, box=(Y, 0))
        return base


import timm
import torch
import torch.nn as nn

from src import register
from src.typing import TensorDict

__all__ = ['PoseNet']

@register('pose')
class PoseNet(nn.Module):
    """Relative pose prediction network.
    From SfM-Learner (https://arxiv.org/abs/1704.07813)

    This network predicts the relative pose between two images, concatenated channelwise.
    It consists of a ResNet encoder (with duplicated and scaled input weights) and a simple regression decoder.
    Pose is predicted as axis-angle rotation and a translation vector.

    The objective is to predict the relative pose between two images.
    The network consists of a ResNet encoder (with duplicated weights and scaled for the input images), plus a simple
    regression decoder.
    Pose is predicted as an axis-angle rotation and a translation vector.

    NOTE: Translation is not in metric scale unless training with stereo + mono.

    :param enc_name: (str) `timm` encoder key (check `timm.list_models()`).
    :param pretrained: (bool) If `True`, returns an encoder pretrained on ImageNet.
    """
    def __init__(self, enc_name: str = 'resnet18', pretrained: bool = False):
        super().__init__()
        self.enc_name = enc_name
        self.pretrained = pretrained

        self.n_imgs = 2
        self.encoder = timm.create_model(enc_name, in_chans=3 * self.n_imgs, features_only=True, pretrained=pretrained)
        self.n_chenc = self.encoder.feature_info.channels()

        self.squeeze = self.block(self.n_chenc[-1], 256, kernel_size=1)
        self.decoder = nn.Sequential(
            self.block(256, 256, kernel_size=3, stride=1, padding=1),
            self.block(256, 256, kernel_size=3, stride=1, padding=1),
            nn.Conv2d(256, 6 * self.n_imgs, kernel_size=1),
        )

    @staticmethod
    def block(in_ch: int, out_ch: int, kernel_size: int, stride: int = 1, padding: int = 0) -> nn.Module:
        """Conv + ReLU."""
        return nn.Sequential(
            nn.Conv2d(in_ch, out_ch, kernel_size, stride, padding),
            nn.ReLU(inplace=True)
        )

    def forward(self, x: torch.Tensor) -> TensorDict:
        """Pose network forward pass.

        :param x: (Tensor) (b, 2*3, h, w) Channel-wise concatenated input images.
        :return: (dict[str, Tensor]) {
            R: (b, 2, 3) Predicted rotation in axis-angle (direction=axis, magnitude=angle).
            t: (b, 2, 3) Predicted translation.
        }
        """
        feat = self.encoder(x)
        out = self.decoder(self.squeeze(feat[-1]))
        out = 0.01 * out.mean(dim=(2, 3)).view(-1, self.n_imgs, 6)
        return {
            'R': out[..., :3],  # Axis-angle (Direction + Magnitude)
            't': out[..., 3:]
        }


import torch.nn as nn
import torch.nn.functional as F
import torch
if torch.__version__ >= "1.8":
    import torch_npu
from torch.autograd import Variable
from collections import OrderedDict

from torchvision.models.resnet import resnet18, resnet34, resnet50
import apex
import torch.npu
import os
import torch.npu
import os
NPU_CALCULATE_DEVICE = 0
if os.getenv('NPU_CALCULATE_DEVICE') and str.isdigit(os.getenv('NPU_CALCULATE_DEVICE')):
    NPU_CALCULATE_DEVICE = int(os.getenv('NPU_CALCULATE_DEVICE'))
if torch.npu.current_device() != NPU_CALCULATE_DEVICE:
    torch.npu.set_device(f'npu:{NPU_CALCULATE_DEVICE}')

def _ModifyConvStrideDilation(conv, stride=(1, 1), padding=None):
    conv.stride = stride

    if padding is not None:
        conv.padding = padding

def _ModifyBlock(block, bottleneck=False, **kwargs):
    for m in list(block.children()):
        if bottleneck:
           _ModifyConvStrideDilation(m.conv2, **kwargs)
        else:
           _ModifyConvStrideDilation(m.conv1, **kwargs)

        if m.downsample is not None:
            # need to make sure no padding for the 1x1 residual connection
            _ModifyConvStrideDilation(list(m.downsample.children())[0], **kwargs)

class ResNet18(nn.Module):
    def __init__(self):
        super().__init__()
        rn18 = resnet18(pretrained=True)

        # discard last Resnet block, avrpooling and classification FC
        # layer1 = up to and including conv3 block
        self.layer1 = nn.Sequential(*list(rn18.children())[:6])
        # layer2 = conv4 block only
        self.layer2 = nn.Sequential(*list(rn18.children())[6:7])

        # modify conv4 if necessary
        # Always deal with stride in first block
        modulelist = list(self.layer2.children())
        _ModifyBlock(modulelist[0], stride=(1,1))

    def forward(self, data):
        layer1_activation = self.layer1(data)
        x = layer1_activation
        layer2_activation = self.layer2(x)

        # Only need the output of conv4
        return [layer2_activation]

class ResNet34(nn.Module):
    def __init__(self):
        super().__init__()
        rn34 = resnet34(pretrained=True)

        # discard last Resnet block, avrpooling and classification FC
        self.layer1 = nn.Sequential(*list(rn34.children())[:6])
        self.layer2 = nn.Sequential(*list(rn34.children())[6:7])
        # modify conv4 if necessary
        # Always deal with stride in first block
        modulelist = list(self.layer2.children())
        _ModifyBlock(modulelist[0], stride=(1,1))

    def forward(self, data):
        layer1_activation = self.layer1(data)
        x = layer1_activation
        layer2_activation = self.layer2(x)

        return [layer2_activation]

class L2Norm(nn.Module):
    """
       Scale shall be learnable according to original paper
       scale: initial scale number
       chan_num: L2Norm channel number (norm over all channels)
    """
    def __init__(self, scale=20, chan_num=512):
        super(L2Norm, self).__init__()
        # Scale across channels
        self.scale = \
            nn.Parameter(torch.Tensor([scale]*chan_num).view(1, chan_num, 1, 1))

    def forward(self, data):
        # normalize accross channel
        return self.scale*data*data.pow(2).sum(dim=1, keepdim=True).clamp(min=1e-12).rsqrt()

def tailor_module(src_model, src_dir, tgt_model, tgt_dir):
    state = torch.load(src_dir)
    src_model.load_state_dict(state)
    src_state = src_model.state_dict()
    # only need features
    keys1 = src_state.keys()
    keys1 = [k for k in src_state.keys() if k.startswith("features")]
    keys2 = tgt_model.state_dict().keys()

    assert len(keys1) == len(keys2)
    state = OrderedDict()

    for k1, k2 in zip(keys1, keys2):
        # print(k1, k2)
        state[k2] = src_state[k1]
    #diff_keys = state.keys() - target_model.state_dict().keys()
    #print("Different Keys:", diff_keys)
    # Remove unecessary keys
    #for k in diff_keys:
    #    state.pop(k)
    tgt_model.load_state_dict(state)
    torch.save(tgt_model.state_dict(), tgt_dir)

# Default vgg16 in pytorch seems different from ssd
def make_layers(cfg, batch_norm=False):
    layers = []
    in_channels = 3
    for v in cfg:
        if v == 'M':
            layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
        elif v == 'C':
            # Notice ceil_mode is true
            layers += [nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True)]
        else:
            conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1)
            if batch_norm:
                layers += [conv2d, apex.parallel.SyncBatchNorm(v), nn.ReLU(inplace=True)]
                #layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)]
            else:
                layers += [conv2d, nn.ReLU(inplace=True)]
            in_channels = v
    return layers

class Loss(nn.Module):
    """
        Implements the loss as the sum of the followings:
        1. Confidence Loss: All labels, with hard negative mining
        2. Localization Loss: Only on positive labels
        Suppose input dboxes has the shape 8732x4
    """

    def __init__(self, dboxes):
        super(Loss, self).__init__()
        self.scale_xy = 1.0/dboxes.scale_xy
        self.scale_wh = 1.0/dboxes.scale_wh

        self.sl1_loss = nn.SmoothL1Loss(reduce=False)
        #self.dboxes = nn.Parameter(dboxes(order="xywh").transpose(0, 1).unsqueeze(dim = 0),
        self.dboxes = nn.Parameter((dboxes(order="xywh").to(torch.float32)).transpose(0, 1).unsqueeze(dim = 0),
            requires_grad=False)
        # Two factor are from following links
        # http://jany.st/post/2017-11-05-single-shot-detector-ssd-from-scratch-in-tensorflow.html
        self.con_loss = nn.CrossEntropyLoss(reduce=False)

    def _loc_vec(self, loc):
        """
            Generate Location Vectors
        """
        #loc=loc.npu()
        gxy = self.scale_xy*(loc[:, :2, :] - self.dboxes[:, :2, :])/self.dboxes[:, 2:, ]
        gwh = self.scale_wh*(loc[:, 2:, :]/self.dboxes[:, 2:, :]).log()

        return torch.cat((gxy, gwh), dim=1).contiguous()

    def forward(self, ploc, plabel, gloc, glabel):
        """
            ploc, plabel: Nx4x8732, Nxlabel_numx8732
                predicted location and labels

            gloc, glabel: Nx4x8732, Nx8732
                ground truth location and labels
        """

        mask = glabel > 0
        pos_num = mask.sum(dim=1)
        vec_gd = self._loc_vec(gloc).float()
        # sum on four coordinates, and mask
        sl1 = self.sl1_loss(ploc, vec_gd).sum(dim=1)
        sl1 = (mask.float()*sl1).sum(dim=1)

        # hard negative mining
        #glabel=glabel.npu()
        con = self.con_loss(plabel, glabel)

        # postive mask will never selected
        con_neg = con.clone()
        con_neg[mask] = 0
        _, con_idx = con_neg.sort(dim=1, descending=True)
        con_idx =con_idx.to(torch.float32)
        _, con_rank = con_idx.sort(dim=1)

        # number of negative three times positive
        #neg_num = torch.clamp(3*pos_num, max=mask.size(1)).unsqueeze(-1).npu()

        #reversion
        neg_num = torch.clamp(3*pos_num, max=mask.size(1)).unsqueeze(-1)
        #neg_num = neg_num.pin_memory()
        neg_num = neg_num.to(f'npu:{NPU_CALCULATE_DEVICE}', non_blocking=True)
        #reversion

        neg_mask = con_rank < neg_num

        closs = (con*(mask.float() + neg_mask.float())).sum(dim=1)

        # avoid no object detected
        total_loss = sl1 + closs
        num_mask = (pos_num > 0).float()
        pos_num = pos_num.float().clamp(min=1e-6)

        ret = (total_loss*num_mask/pos_num).mean(dim=0)
        return ret


import aes
import base64
import cbc_mac
import os
import util

m = "alert('MZA who was that?');\n"
k = "YELLOW SUBMARINE"
iv = '\0'*16
tag = cbc_mac.cbc_mac_tag(m, k, iv)

assert(tag.encode('hex') == "296b8d7cb78a243dda4d0a61d33bbdd1")

target = "alert('Ayo, the Wu is back!');"

def collide_hash(target):
	# Put comment after target to hide everything after
	while True:
		base = target + '//' + os.urandom(4)
		t1 = cbc_mac.cbc_mac_tag(base, k, iv)
		preimage = aes.add_pkcs7_padding(base) + util.xor(t1, m[:16]) + m[16:]
		assert(cbc_mac.cbc_mac_tag(preimage, k, iv) == tag)
		if "\n" not in preimage[:-(len(m) - 16)]:
			return preimage
		print("newline in padding :(")

if __name__ == "__main__":
	print(base64.b64encode(collide_hash(target)))
	'''
	// example javascript
	let m = "YWxlcnQoJ0F5bywgdGhlIFd1IGlzIGJhY2shJyk7Ly+SYtRRDAwMDAwMDAwMDAwMZrPKKd6E1lIZ7AuEnoG3vGFzIHRoYXQ/Jyk7Cg=="
	eval(atob(m))
	'''


#!/usr/bin/python3
'''comment'''

class Student:
    '''comments'''

    def __init__(self, first_name, last_name, age):
        '''comments'''
        self.first_name = first_name
        self.last_name = last_name
        self.age = age

    def to_json(self, attrs=None):
        '''comment'''
        res = {}
        if type(attrs) is list:
            for key in attrs:
                if key in self.__dict__:
                    res[key] = self.__dict__[key]
            return res
        else:
            return self.__dict__

    def reload_from_json(self, json):
        '''comment'''
        sarasa = dict(json)
        for key in dict(self.__dict__):
            if key in json:
                self.__dict__[key] = sarasa[key]


import numpy as np

def pad_seq(seqs, max_length):
        seqs_pad = []

        for seq in seqs:
            seq_pad = np.pad(seq, ((0, max_length - seq.shape[0]), (0, 0)), 'constant', constant_values=0)
            assert seq_pad.shape == (max_length, seq.shape[1])
            seqs_pad.append(seq_pad)

        return seqs_pad

def segment(X, y, lens, time_steps=10):
    """
    Args:
        X: n_samples * (real_length, 310)
        y: (n_samples, )
        lens: (n_samples, )

    Returns:
        segment_X: (n_samples * time_steps, 265 // time_steps, 310)
        segment_y: (n_samples * time_steps)
        segment_lens: (n_samples * time_steps)
    """
    segment_X = []
    segment_y = []
    segment_lens = []
    num_batches = []

    n_samples = len(segment_X)

    for x, label, length in zip(X, y, lens):
        old_time_steps = x.shape[0]
        n_segments = int(np.ceil(old_time_steps / time_steps))  # 235 / 20 = 12; 265 / 20 = 14
        num_batches.append(n_segments)

        for seg_idx in range(n_segments):
            if seg_idx == n_segments - 1:
                seg_x = x[-1 * time_steps:, :]  # time_step x 310
            else:
                seg_x = x[seg_idx * time_steps: (seg_idx + 1) * time_steps, :]

            segment_X.append(seg_x)
            segment_y.append(label)
            segment_lens.append(time_steps)

    segment_X = np.stack(segment_X)
    segment_y = np.stack(segment_y)
    segment_lens = np.stack(segment_lens)

    return segment_X, segment_y, segment_lens, num_batches

class EGGDataset(object):
    def __init__(self, data_dir='data_used/'):
        self.data_dir = data_dir

    def read_data(self):
        data = []
        lengths = []
        max_lengths = []
        labels = []

        sub_labels = np.load(self.data_dir + 'label.npy')

        for subjuct_id in range(1, 4):
            zip_data = np.load(self.data_dir + '0{}.npz'.format(subjuct_id))
            film_ids = zip_data.keys()

            sub_data = []
            for film_id in film_ids:
                film = zip_data[film_id]
                film = np.reshape(film, [-1, 310])
                sub_data.append(film)

            sub_lengths = [sd.shape[0] for sd in sub_data]
            max_length = max(sub_lengths)

            lengths.append(np.asarray(sub_lengths))
            max_lengths.append(max_length)
            data.append(sub_data)
            labels.append(sub_labels)

        self.data = data
        self.lengths = lengths
        self.max_lengths = max_lengths
        self.labels = labels

    def split(self, mode='segment', time_steps=10, subject_ids=[0], train_test_point=9):
        assert mode in ['segment', 'whole']

        train_X = []
        train_y = []
        train_lens = []

        test_X = []
        test_y = []
        test_lens = []

        for subject_id in subject_ids:
            train_X.extend(self.data[subject_id][:train_test_point])
            train_y.extend(self.labels[subject_id][:train_test_point])
            train_lens.extend(self.lengths[subject_id][:train_test_point])

            test_X.extend(self.data[subject_id][train_test_point:])
            test_y.extend(self.labels[subject_id][train_test_point:])
            test_lens.extend(self.lengths[subject_id][train_test_point:])

        max_length = max(self.max_lengths)
        assert len(train_X) == 9 * len(subject_ids)
        train_batches = None
        test_batches = None

        if mode == 'segment':
            train_X, train_y, train_lens, train_batches  = segment(train_X, train_y, train_lens, time_steps)
            test_X, test_y, test_lens, test_batches = segment(test_X, test_y, test_lens, time_steps)

            print('train_X shape: {}'.format(train_X.shape))
            print('train_y shape: {}'.format(train_y.shape))
            print('train_lens shape: {}'.format(train_lens.shape))

            print('test_X shape: {}'.format(test_X.shape))
            print('test_y shape: {}'.format(test_y.shape))
            print('test_lens shape: {}'.format(test_lens.shape))

        else:
            train_X = pad_seq(train_X, max_length)
            train_X = np.stack(train_X)
            train_y = np.stack(train_y)
            train_lens = np.stack(train_lens)

            test_X = pad_seq(test_X, max_length)
            test_X = np.stack(test_X)
            test_y = np.stack(test_y)
            test_lens = np.stack(test_lens)

            print('train_X shape: {}'.format(train_X.shape))
            print('train_y shape: {}'.format(train_y.shape))
            print('train_lens shape: {}'.format(train_lens.shape))

            print('test_X shape: {}'.format(test_X.shape))
            print('test_y shape: {}'.format(test_y.shape))
            print('test_lens shape: {}'.format(test_lens.shape))
        return train_X, train_y, train_lens, train_batches, test_X, test_y, test_lens, test_batches

    def get_batch(self, sub_data, sub_labels, sub_lengths, batch_size=2):
        batch_data = None
        batch_lengths = None
        batch_labels = None

        if batch_size == -1:
            batch_data = sub_data
            batch_lengths = sub_lengths
            batch_labels = sub_labels
        else:
            n_samples = sub_data.shape[0]
            indices = np.random.choice(n_samples, batch_size, replace=False)
            batch_data = sub_data[indices]
            batch_lengths = sub_lengths[indices]
            batch_labels = sub_labels[indices]

        return batch_data, batch_labels, batch_lengths

if __name__ == '__main__':
    dataset = EGGDataset()
    dataset.read_data()
    train_X, train_y, train_lens, train_batches, test_X, test_y, test_lens, test_batches = dataset.split(mode='segment', time_steps=100, subject_ids=[0,1,2])


n, m = map(int,input().split())

first =[list(map(int,input().split())) for _ in range(n)]
second = [list(map(int,input().split())) for _ in range(n)]

row = 0
column = 0
done = False

for i in range(n):
    for j in range(m):
        first[i][j],second[i][j] = min(first[i][j],second[i][j]), max(first[i][j],second[i][j])

for i in range(n-1):
    for j in range(m):
        if first[i][j] >= first[i+1][j] or second[i][j] >= second[i+1][j]:
            done= True
    if done:
        break

for i in range(n):
    for j in range(m-1):
        if first[i][j] >= first[i][j+1] or second[i][j] >= second[i][j+1]:
            done = True
    if done:
        break
if done:
    print("Impossible")
else:
    print("Possible")


from .planner import Planner, save_model
import torch
import torch.optim as optim
import torch.utils.tensorboard as tb
import numpy as np
from .utils import load_data
from . import dense_transforms

def train(args):
    from os import path
    # Initialize the model
    model = Planner()
    optimizer = optim.Adam(model.parameters(), lr=args.lr)
    criterion = torch.nn.MSELoss()

    train_loader = load_data(batch_size=args.batch_size)

    train_logger, valid_logger = None, None
    if args.log_dir is not None:
        train_logger = tb.SummaryWriter(path.join(args.log_dir, 'train'))

    for epoch in range(args.epochs):
        model.train()
        for i, (images, labels) in enumerate(train_loader):
            optimizer.zero_grad()
            outputs = model(images)
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()

            if i % args.log_interval == 0 and train_logger:
                log(train_logger, images, labels, outputs, epoch * len(train_loader) + i)

    save_model(model)

def log(logger, img, label, pred, global_step):
    """
    logger: train_logger/valid_logger
    img: image tensor from data loader
    label: ground-truth aim point
    pred: predited aim point
    global_step: iteration
    """
    import matplotlib.pyplot as plt
    import torchvision.transforms.functional as TF
    fig, ax = plt.subplots(1, 1)
    ax.imshow(TF.to_pil_image(img[0].cpu()))
    WH2 = np.array([img.size(-1), img.size(-2)])/2
    ax.add_artist(plt.Circle(WH2*(label[0].cpu().detach().numpy()+1), 2, ec='g', fill=False, lw=1.5))
    ax.add_artist(plt.Circle(WH2*(pred[0].cpu().detach().numpy()+1), 2, ec='r', fill=False, lw=1.5))
    logger.add_figure('viz', fig, global_step)
    del ax, fig

if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser()

    parser.add_argument('--log_dir')
    parser.add_argument('--epochs', type=int, default=15)
    parser.add_argument('--batch_size', type=int, default=128)
    parser.add_argument('--lr', type=float, default=0.001)
    parser.add_argument('--log_interval', type=int, default=10)

    args = parser.parse_args()
    train(args)