"""
Implementation of the MySQL SQL dialect.
"""

# TODO: This is currently just a copy of the T-SQL dialect, which has been modified slightly. The
#       modifications to MySQL syntax need to be completed. See http://www.rlbuckmaster.com/?p=64

from ._base import SQLDialect

from sql_dialects.enums import Nullary, Unary, Binary, Ternary, JoinTypes, LiteralTypes
from sql_dialects import ast as sql
from sql_dialects.dialects import REGISTRY

__author__ = 'Aaron Hosford'

class MySQLDialect(SQLDialect):
    """The MySQL SQL dialect."""

    def __init__(self):
        super().__init__('MySQL')

        self._join_map = {
            JoinTypes.INNER: 'INNER JOIN',
            JoinTypes.OUTER: 'OUTER JOIN',
            JoinTypes.LEFT: 'LEFT JOIN',
            JoinTypes.RIGHT: 'RIGHT JOIN',
        }

        self._operations = {
            Nullary.NOW: 'NOW()',
            Nullary.GUID: 'UUID()',
            Nullary.VERSION: 'VERSION()',

            Unary.NEG: '-{p1}',
            Unary.LENGTH: 'CHARACTER_LENGTH({p1})',
            Unary.NOT: 'NOT {p1}',
            Unary.LOWER_CASE: 'LCASE({p1})',
            Unary.UPPER_CASE: 'UCASE({p1})',

            Binary.EQ: '{p1} = {p2}',
            Binary.NE: '{p1} != {p2}',
            Binary.LT: '{p1} < {p2}',
            Binary.GT: '{p1} > {p2}',
            Binary.LE: '{p1} <= {p2}',
            Binary.GE: '{p1} >= {p2}',
            Binary.AND: '{p1} AND {p2}',
            Binary.OR: '{p1} OR {p2}',
            Binary.ROUND: 'ROUND({p1},{p2})',
            Binary.FORMAT: 'FORMAT({p1},{p2})',

            Ternary.IF_ELSE: 'IF {p1} {p2} ELSE {p3}',
            Ternary.SUBSTRING: 'SUBSTRING({p1}, {p2}, {p3})',
        }

    def build_command(self, tree):
        """
        Generate a SQL command from an AST.

        :param tree: The abstract syntax tree.
        :return: A SQL expression.
        """
        return super().build_command(tree) + ';'  # MySQL requires ; termination for commands

    def build_describe(self, tree):
        """Build a describe statement from the given AST."""
        assert isinstance(tree, sql.Describe)

        template = 'DESCRIBE {table}'
        return template.format(
            table=self.build_table(tree.table, allow_joins=False)
        )

    def build_select(self, tree):
        """Build a select statement from the given AST."""
        assert isinstance(tree, sql.Select)

        template = 'SELECT {distinct}{fields} FROM {table}{where}{group_by}{order_by}{limit}'
        return template.format(
            distinct='DISTINCT ' if tree.is_distinct else '',
            fields=self.build_fields(tree.field_list, allow_aliases=True),
            table=self.build_table(tree.table, allow_joins=True),
            where=(' ' + self.build_where(tree.where_clause)) if tree.where_clause else '',
            group_by=(' ' + self.build_group_by(tree.grouping)) if tree.grouping else '',
            order_by=(' ' + self.build_order_by(tree.grouping)) if tree.grouping else '',
            limit=' LIMIT %s' % tree.limited_to.count if tree.limited_to else ''
        )

    def build_insert(self, tree):
        """Build an insert statement from the given AST."""
        assert isinstance(tree, sql.Insert)

        template = 'INSERT INTO {table}{fields} VALUES {values}'
        return template.format(
            table=self.build_table(tree.table, allow_joins=False),
            fields=(
                (' (' + self.build_fields(tree.field_list, allow_aliases=False) + ')')
                if tree.field_list
                else ''
            ),
            values=self.build_values(tree.value_list)
        )

    def build_update(self, tree):
        """Build an update statement from the given AST."""
        assert isinstance(tree, sql.Update)

        template = 'UPDATE {tables} SET {assignments}{where}'
        return template.format(
            tables=self.build_table(tree.table, allow_joins=True),
            assignments=self.build_assignments(tree.field_list, tree.value_list),
            where=(' ' + self.build_where(tree.where_clause) if tree.where_clause else '')
        )

    def build_delete(self, tree):
        """Build a delete statement from the given AST."""
        assert isinstance(tree, sql.Delete)

        template = 'DELETE FROM {table}{where}'
        return template.format(
            table=self.build_table(tree.table, allow_joins=False),
            where=(' ' + self.build_where(tree.where_clause) if tree.where_clause else '')
        )

    def build_fields(self, tree, *, allow_aliases):
        """Build a field list from an AST."""
        assert tree is None or isinstance(tree, sql.FieldList)
        assert isinstance(allow_aliases, bool)

        if tree is None:
            return '*'

        fields = []
        for field in tree.entries:
            assert isinstance(field, sql.Alias)
            entry = self.build_value(field.expression)
            if allow_aliases:
                if field.name:
                    entry += ' AS [%s]' % field.name
            else:
                assert isinstance(field.expression, sql.Field)
                assert field.name is None
            fields.append(entry)
        return ', '.join(fields)

    def build_table(self, tree, *, allow_joins):
        """Build a table name or joined list of table names from an AST."""
        assert isinstance(tree, sql.TableExpression)
        assert isinstance(allow_joins, bool)

        if isinstance(tree, sql.Table):
            return '.'.join('[%s]' % element for element in tree.identifier.path)

        assert isinstance(tree, sql.Join)

        template = '{left} {join} {right}{on}'

        return template.format(
            left=self.build_table(tree.left, allow_joins=True),
            join=self._join_map[tree.join_type],
            right=self.build_table(tree.right, allow_joins=False),
            on=(' ' + self.build_on(tree.on_clause)) if tree.on_clause else ''
        )

    def build_where(self, tree):
        """Build a where-clause from an AST."""
        assert isinstance(tree, sql.Where)
        return 'WHERE %s' % self.build_value(tree.condition)

    def build_on(self, tree):
        """Build an ON statement (table1 JOIN table2 ON ...) from an AST."""
        assert isinstance(tree, sql.On)
        return 'ON %s' % self.build_value(tree.condition)

    def build_group_by(self, tree):
        """Build a GROUP BY statement from an AST."""
        assert isinstance(tree, sql.GroupBy)
        return 'GROUP BY ' + self.build_fields(tree.fields, allow_aliases=False)

    def build_order_by(self, tree):
        """Build an ORDER BY statement from an AST."""
        assert isinstance(tree, sql.OrderBy)
        return 'ORDER BY ' + ', '.join(self.build_order_by_entry(entry) for entry in tree.entries)

    def build_order_by_entry(self, tree):
        """Build an entry in an ORDER BY field list from an AST."""
        assert isinstance(tree, sql.OrderByEntry)
        return self.build_field(tree.field) + (' ASC' if tree.ascending else ' DESC')

    def build_field(self, tree, *, allow_aliases=False):
        """Build a field in a field list from an AST."""
        if allow_aliases and isinstance(tree, sql.Alias):
            entry = self.build_value(tree.expression)
            if tree.name:
                entry += ' AS [%s]' % tree.name
            return entry

        assert isinstance(tree, sql.Field)
        if tree.table:
            table = self.build_table(tree.table, allow_joins=False)
            return '%s.[%s]' % (table, tree.identifier.name)
        else:
            return '[%s]' % tree.identifier.name

    def build_values(self, tree):
        """Build a value list from an AST."""
        if isinstance(tree, sql.Select):
            return '(%s)' % self.build_select(tree)
        else:
            assert isinstance(tree, sql.ValueList), "No values provided."
            return '(%s)' % ', '.join(self.build_value(entry) for entry in tree.entries)

    def build_assignments(self, fields, values):
        """Build a field/value assignment list from an AST."""
        assert isinstance(fields, sql.FieldList)
        assert isinstance(values, sql.ValueList)
        assert fields.width == values.width
        return ', '.join('%s=%s' % (self.build_field(field, allow_aliases=True),
                                    self.build_value(value))
                         for field, value in zip(fields.entries, values.entries))

    def build_value(self, tree):
        """Build a value from an AST."""
        assert isinstance(tree, (sql.Value, sql.Select))

        if isinstance(tree, sql.Field):
            return self.build_field(tree)
        elif isinstance(tree, sql.Parameter):
            return '?'
        elif isinstance(tree, sql.Literal):
            return self.build_literal(tree)
        elif isinstance(tree, sql.Operation):
            return self.build_operation(tree)
        else:
            assert isinstance(tree, sql.Select)
            return '(%s)' % self.build_select(tree)

    @staticmethod
    def build_literal(tree):
        """Build a literal value from an AST."""
        assert isinstance(tree, sql.Literal)

        if tree.sql_type == LiteralTypes.STRING:
            return "'%s'" % tree.value.replace("'", "''")
        elif tree.sql_type in (LiteralTypes.INTEGER, LiteralTypes.FLOAT, LiteralTypes.BOOLEAN):
            return str(tree.value)
        elif tree.sql_type == LiteralTypes.DATE:
            return "'%s'" % tree.value.strftime('%Y%m%d')
        elif tree.sql_type == LiteralTypes.DATE_TIME:
            return "'%s'" % tree.value.strftime('%Y%m%d %I:%M:%S %p')
        else:
            assert tree.sql_type == LiteralTypes.NULL
            return 'NULL'

    def build_operation(self, tree):
        """Build an operation (e.g. addition, subtraction, or function call) from an AST."""
        assert isinstance(tree, sql.Operation)

        params = {}
        for index, operand in enumerate(tree.operands):
            value = self.build_value(operand)
            if isinstance(operand, sql.Operation):
                value = '(%s)' % value
            params['p%s' % (index + 1)] = value

        return self._operations[tree.operator].format_map(params)

# Create the dialect and register it.
MY_SQL = MySQLDialect()
REGISTRY.add(MY_SQL)


import pandas as pd
import numpy as np
table = pd.read_excel(r'I:\Python\发货商品明细 (8.18-8.27) - 副本.xlsx')
table['year'] = pd.DatetimeIndex(table['支付时间']).year
groups = table.groupby(['品牌','year'])
s = groups['成本金额'].sum()
c = groups['平台单号'].count()
pt2=pd.DataFrame({'sum':s,'count':c})
print(pt2)
pt2.to_excel(r'g:\1.xlsx')
# pt1 = table.pivot_table(index='品牌', columns='year',
#                        values=['成本金额'], aggfunc=np.sum)
# print(pt1)
# print(table.head())


import requests
from bs4 import BeautifulSoup

url = "https://www.youtube.com/@PW-Foundation/videos"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")

video_links = []
for link in soup.find_all('a'):
    href = link.get('href')
    if href.startswith('/watch?v='):
        video_links.append('https://www.youtube.com' + href)
        if len(video_links) == 5:
            break

for link in video_links:
    print(link)


import os
import shutil
from .base import GnuRecipe

class OpenWatcomRecipe(GnuRecipe):
    def __init__(self, *args, **kwargs):
        super(OpenWatcomRecipe, self).__init__(*args, **kwargs)
        self.sha256 = '10751815179199436e8b186974c20e8e' \
                      'd93925c3e94f55f2e1a4c70e3ab737d8'

        self.name = 'openwatcom'
        self.version = '2cf5825f00e10df70406a3411f22dfc2e3893690'
        self.url = 'https://github.com/open-watcom/open-watcom-v2/archive/' \
                   '$version.tar.gz'

        self.configure_args = self.shell_args + [
            './build.sh'
            ]

        self.environment['OWROOT'] = self.directory

    def patch(self):
        src = os.path.join(self.directory, 'setvars.sh')
        dst = os.path.join(self.directory, 'setvars')
        if os.path.exists(dst):
            os.remove(dst)
        shutil.copy2(src, dst)


class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
        row = collections.defaultdict(set)
        col = collections.defaultdict(set)
        grid = collections.defaultdict(set)

        for i in range(9):
            for j in range(9):
                if board[i][j] == '.':
                    continue

                if ((board[i][j] in row[i]) or
                    (board[i][j] in col[j]) or
                    (board[i][j] in grid[(i//3, j//3)])):
                    return False
                else:
                    row[i].add(board[i][j])
                    col[j].add(board[i][j])
                    grid[(i//3, j//3)].add(board[i][j])
        return True


import numpy as np
from niscv_v2.basics import utils
from niscv_v2.experiments.simulation.kernel_cost import experiment
import pickle

def run(it, dim):
    size_ests = [1000, 2000, 3000, 5000, 7000, 10000, 20000, 30000, 50000, 70000, 100000]
    results = []
    for size_est in size_ests:
        np.random.seed(19971107 + it)
        print(dim, it,  size_est)
        result1 = experiment(dim=dim, fun=utils.integrand(1), size_est=size_est, sn=False, size_kn=300, ratio=1000)
        result2 = experiment(dim=dim, fun=utils.integrand(1), size_est=size_est, sn=True, size_kn=300, ratio=1000)
        results.append(result1 + result2)

    return results

def main(dim):
    R = []
    for it in range(10):
        R.append(run(it, dim))

    with open('../../data/simulation/sample_cost_{}D'.format(dim), 'wb') as file:
        pickle.dump(R, file)

if __name__ == '__main__':
    main(4)
    main(6)
    main(8)


#!/usr/bin/python3
""" """
from tests.test_models.test_base_model import test_basemodel
from models.amenity import Amenity
import os

class test_Amenity(test_basemodel):
    """ amenity test class"""

    def __init__(self, *args, **kwargs):
        """inti the test class """
        super().__init__(*args, **kwargs)
        self.name = "Amenity"
        self.value = Amenity

    def test_name2(self):
        """testing name type """
        new = self.value()
        self.assertEqual(type(new.name), str if
                         os.getenv('HBNB_TYPE_STORAGE') != 'db' else
                         type(None))


from sqlalchemy import insert, select, update
from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.asyncio import AsyncSession

from fastapi import HTTPException, UploadFile

from src.auth.models import User

from src.profiles.schemas import ProfileCreate, ProfileUpdate
from src.profiles.models import Profile, Avatar

from src.profiles.services.s3 import upload_avatar_to_s3

from src.profiles.exceptions.http import ProfileAlreadyExist

class ProfileDB:

    @staticmethod
    async def add_profile(profile: ProfileCreate, session: AsyncSession, user: User):
        try:
            stmt = insert(Profile).values(
                first_name=profile.first_name,
                second_name=profile.second_name,
                username=profile.username,
                user_id=user.id
            )
            await session.execute(stmt)
            await session.commit()
        except IntegrityError:
            raise ProfileAlreadyExist
        return 'success'

    @staticmethod
    async def update_profile(profile: ProfileUpdate, session: AsyncSession, user: User):
        data = {key: value for key, value in profile.dict().items() if value}
        stmt = update(Profile).where(Profile.user_id == user.id).values(**data)
        await session.execute(stmt)
        await session.commit()
        return 'success'

    @staticmethod
    async def get_my_profile(session: AsyncSession, user: User):
        query = select(Profile).where(Profile.user_id == user.id)
        result = await session.execute(query)
        return result.scalar()

class AvatarDB:

    @staticmethod
    async def get_url_file(session: AsyncSession, profile: Profile) -> Avatar:
        query = select(Avatar).where(Avatar.profile_id == profile.id)
        result = await session.execute(query)
        return result.scalar()

    @staticmethod
    async def upload_file(file: UploadFile, url: str, session: AsyncSession, profile: Profile):
        stmt = insert(Avatar).values(url=url, profile_id=profile.id)
        await session.execute(stmt)
        await session.commit()
        upload_avatar_to_s3(file, url)
        return 'success'

    @staticmethod
    async def update_upload_file(file: UploadFile, url: str, session: AsyncSession, profile: Profile):
        stmt = update(Avatar).where(Avatar.profile_id == profile.id).values(url=url)
        await session.execute(stmt)
        await session.commit()
        upload_avatar_to_s3(file, url)
        return 'update'


#!/usr/bin/python3

import sys, threading

sys.setrecursionlimit(10 ** 7)  # max depth of recursion
threading.stack_size(2 ** 25)  # new thread will get stack of such size

def IsBinarySearchTree(tree):
    # set exit value
    exit_value = True
    if len(tree) > 0:

        # key
        key = [i[0] for i in tree]

        # left
        left = [i[1] for i in tree]

        # right
        right = [i[2] for i in tree]

        current = 0
        result = []
        result_idx = []

        # stack
        s = []

        # set exit value
        exit_value = True

        # subtree 0 = left, 1 = right
        # subtree = 0

        while exit_value:
            if current != -1:
                s.append(current)
                current = left[current]

            elif s:
                current = s.pop()
                result.append(key[current])
                result_idx.append(current)

                current = right[current]

            else:
                break

        # find index of the root node
        root_idx = result_idx.index(0)

        left_subtree = result[:root_idx+1]
        right_subtree = result[root_idx:]

        if sorted(left_subtree) != left_subtree:
            exit_value = False
        if sorted(right_subtree) != right_subtree:
            exit_value = False

    return exit_value

def main():
    # f = open("./tests/6")
    # nodes = int(f.readline().strip())
    nodes = int(sys.stdin.readline().strip())
    tree = []
    for i in range(nodes):
        tree.append(list(map(int, sys.stdin.readline().strip().split())))
        # tree.append(list(map(int, f.readline().strip().split())))
    if IsBinarySearchTree(tree):
        print("CORRECT")
    else:
        print("INCORRECT")

threading.Thread(target=main).start()


def get_param_groups(model):
    bias_group = []
    others_group = []
    for n, p in model.named_parameters():
        if n.find('bias') != -1 or n.find('beta') != -1:
            bias_group.append(p)
        else:
            others_group.append(p)
    return [dict(params=others_group),
            dict(params=bias_group, weight_decay=0)]


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

import pandas as pd
import numpy as np
from datetime import datetime
from collections import defaultdict
from sklearn.base import BaseEstimator, TransformerMixin

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

class TypeSelector(BaseEstimator, TransformerMixin):

    '''np.object, np.number, np.bool_'''

    def __init__(self, dtype1, dtype2=None, dtype3=None):
        self.dtype1 = dtype1
        self.dtype2 = dtype2
        self.dtype3 = dtype3

    def fit(self, X, y=None):
        return self

    def transform(self, X):

        assert isinstance(X, pd.DataFrame), "Gotta be Pandas"

        if self.dtype3 != None:

            output = (X.select_dtypes(include=[self.dtype1]),
                   X.select_dtypes(include=[self.dtype2]),
                   X.select_dtypes(include=[self.dtype3]))

        elif self.dtype2 != None:
            output = (X.select_dtypes(include=[self.dtype1]),
                   X.select_dtypes(include=[self.dtype2]))

        else:

            output = (X.select_dtypes(include=[self.dtype1]))

        return output

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

from sklearn.preprocessing import StandardScaler

class StandardScalerDf(StandardScaler):

    """
    DataFrame Wrapper around StandardScaler; Recursive override
    """

    def __init__(self, copy=True, with_mean=True, with_std=True):
        super(StandardScalerDf, self).__init__(copy=copy,
                                               with_mean=with_mean,
                                               with_std=with_std)

    def transform(self, X, y=None):
        z = super(StandardScalerDf, self).transform(X.values)
        return pd.DataFrame(z, index=X.index, columns=X.columns)

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

from fancyimpute import SoftImpute

class SoftImputeDf(SoftImpute):

    """
    DataFrame Wrapper around SoftImpute
    """

    def __init__(self, shrinkage_value=None, convergence_threshold=0.001,
                 max_iters=100,max_rank=None,n_power_iterations=1,init_fill_method="zero",
                 min_value=None,max_value=None,normalizer=None,verbose=True):

        super(SoftImputeDf, self).__init__(shrinkage_value=shrinkage_value,
                                           convergence_threshold=convergence_threshold,
                                           max_iters=max_iters,max_rank=max_rank,
                                           n_power_iterations=n_power_iterations,
                                           init_fill_method=init_fill_method,
                                           min_value=min_value,max_value=max_value,
                                           normalizer=normalizer,verbose=False)

    def fit_transform(self, X, y=None):

        assert isinstance(X, pd.DataFrame), "Must be pandas dframe"

        for col in X.columns:
            if X[col].isnull().sum() < 10:
                X[col].fillna(0.0, inplace=True)

        z = super(SoftImputeDf, self).fit_transform(X.values)
        return pd.DataFrame(z, index=X.index, columns=X.columns)

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

class SelectFeatures(BaseEstimator, TransformerMixin):

    """
    Used with Kbins to select features with sufficient cardinality. Could
    probably just join this with kbins
    """

    def __init__(self, val_count=50, categorical=False):
        self.val_count = val_count
        self.categorical = categorical

    def fit(self, X, y=None):
        return self

    def transform(self, X):
        assert isinstance(X, pd.DataFrame)
        feat = pd.DataFrame()

        if self.categorical==False:
            for col in X.columns:
                if len(X[col].value_counts()) > self.val_count:
                    X[col + '_bin'] = X[col]
                    feat = pd.concat([feat, X[col + '_bin']], axis=1)
        else:
            for col in X.columns:
                if len(X[col].value_counts()) > self.val_count:
                    feat = pd.concat([feat, X[col]], axis=1)
        return feat

#------------------------------------------------------------
from sklearn.preprocessing import KBinsDiscretizer

class KBins(KBinsDiscretizer):

    """DataFrame Wrapper around KBinsDiscretizer. Sometimes this will throw
    the monotonically increase/decrease error. You can either reduce bins
    or modify the selected features by value counts (increase)"""

    def __init__(self, n_bins=5, encode='onehot', strategy='quantile'):
        super(KBins, self).__init__(n_bins=n_bins,
                                    encode='ordinal',
                                    strategy=strategy)

    def transform(self, X, y=None):

        assert isinstance(X, pd.DataFrame), "Must be pandas dframe"

        z = super(KBins, self).transform(X)
        binned = pd.DataFrame(z, index=X.index, columns=X.columns)
        binned = binned.applymap(lambda x: 'category_' + str(x))
#         final = pd.concat([X, binned], axis=1)
        return binned

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

import re

class RegImpute(BaseEstimator, TransformerMixin):

    '''consider adding methods to check for special characters or return
    indices for nans, since nans can be different types. If bool, shut off
    regex'''

    def __init__(self, regex=True):
        self.regex = regex

    def find_nulls(self, X, y=None):
        '''this returns all dframe indices with nans. Useful to determine
        type of null'''
        return pd.isnull(X).any(1).nonzero()[0]

    def null_cols(self, X, y=None):
        '''Prints list of null cols with number of nulls'''
        null_columns=X.columns[X.isnull().any()]
        print(X[null_columns].isnull().sum())

    def fit(self, X, y=None):
        return self

    def transform(self, X):
        assert isinstance(X, pd.DataFrame)

        for col in X.columns:
            X[col].fillna(X[col].mode().iloc[0], inplace=True)

        if self.regex == True:
            X = X.applymap(lambda x: re.sub(r'\W+', '', x))

        return X

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

class FreqFeatures(BaseEstimator, TransformerMixin):

    """
    returns a dict for freqs. This can then be mapped to
    any col to create freq feature. Must be run prior to freq_group
    """

    def __init__(self, val_count=50):
        self.val_count = val_count
        self.drops = []

    def make_dict(self, col):

        df = pd.DataFrame(self.data[col].value_counts())
        df.reset_index(level=0, inplace=True)
        df.rename(columns={'index': 'key', col: 'value'}, inplace=True)
        df_dict = defaultdict(list)
        for k, v in zip(df.key, df.value):
            df_dict[k] = (int(v))
        return df_dict

    @staticmethod
    def reduce(x,y):
        if x <= 10:
            return 'rare'
        elif x <= 50:
            return 'infrequent'
        elif x <= 100:
            return 'less common'
        else:
            return y

    def fit(self, X, y=None):
        return self

    def transform(self, X):
        self.data = X

        assert isinstance(self.data, pd.DataFrame), 'pls enter dframe'

        for col in self.data.columns:
            dict_ = self.make_dict(col)
            freq_vec = self.data[col].map(dict_)

            if len(set(self.data[col])) > 2000:
                self.drops.append(col)
                self.data.drop([col], axis=1, inplace=True)
                self.data[col + '_freq'] = freq_vec

            elif len(set(self.data[col])) > 100:
                y = self.data[col]
                vectfunc = np.vectorize(self.reduce,cache=False)
                vec = np.array(vectfunc(freq_vec,y))

                self.data[col + '_freq'] = freq_vec
                self.data[col + '_reduce'] = vec
                self.data.drop([col], axis=1, inplace=True)

        return self.data

'''Notes: built-in super() function, which is a function for delegating method calls to some class in the instance’s ancestor tree. For our purposes, think of super() as a generic instance of our parent class.

http://flennerhag.com/2017-01-08-Recursive-Override/

'''

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

class MetricVec:

    '''Run this before features'''

    def __init__(self, X, y=None):
        self.data = X
        self.label = y

    def join(self, features=['churn', 'metric_renew', 'metric']):

        '''run this first'''

        assert isinstance(features, list), "as a list pls"

        features = [self.data[feat].apply(str) for feat in features]

        return pd.concat(features, axis=1).apply(lambda x: ' '.join(x), axis=1)

    def metric_merge(self, string):

        '''If the account is New, the metric is taken from the metric column - which
        has more values for New. Otherwise, it comes from metric at Renewal - which has
        more values for existing accounts'''

        churn = string.split()[0]
        m1 =string.split()[1]
        m2 = string.split()[2]
        if churn == 'New':
            metric = m2
        else:
            metric = m1
        if metric == 'nan':
            metric = metric
        else:
            metric = int(float(metric))

        return metric

    def modify(self):

        '''Only need to run this method. Everything else is linked. It works,
        but probably not set up correctly'''

        merge = self.join()

        self.data['metRic'] = np.float64(merge.apply(self.metric_merge))
        self.data.drop(['metric_renew', 'metric'], axis=1, inplace=True)

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

import calendar

class TimeFeatures:

    def __init__(self, X, y=None):
        self.data = X
        self.label = y

    def index(self, col='eff_dt', output=False):

        self.data.index = self.data[col]
        self.data.drop([col], axis=1, inplace=True)
        self.data.index.rename(col, inplace=True)
        # args
        if output:
            return self.data

    def time_feats(self, col = 'Effective YearMonth', output=False):

        self.data['eff_dt']= self.data[col].apply(lambda x: datetime.strptime(str(x), '%Y%m'))
        self.data.drop([col], axis=1, inplace=True)

        series = self.data['eff_dt']
        # financial quarter
        self.data['quarter'] = series.apply(lambda x: int(x.month // 3.25 + 1))
        # extract time features
        self.data['month_nom'] = series.apply(lambda x: calendar.month_abbr[x.month])
        self.data['month_prog'] = series.apply(lambda x: np.float(x.month))
        self.data['year'] = series.apply(lambda x: str(x.year) + '_')

        # create term for absolute quarter
        term = self.data['year'] + self.data['quarter'].astype(str)

        abs_quarter = { '2016_1': 1, '2016_2': 2, '2016_3': 3 ,'2016_4': 4,
                       '2017_1': 5, '2017_2': 6, '2017_3': 7,'2017_4': 8,'2018_1': 9,
                       '2018_2': 10}

        self.data['abs_q'] = term.map(abs_quarter)

        self.index()

        if output:
            return self.data

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


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

import sys
import re

def counter(count, message='', step=1000,):
	if count % step == 0:
		sys.stderr.write("\r\t{:,d}{}".format(count,message))

dic = {}
count = 0
for line in iter(sys.stdin.readline, ""):
	array = line.split()
	if len(array) != 3:
		continue
	if not 'unknown' in array[2] and not 'CD' in array[1]:
		m = re.match(r'^[\W0-9]+$', array[0])
		if not m:
			count += 1
			key = '\t'.join(array)
#			print(key)
			dic[key] = dic.get(key,0) + 1
			entry = key + '\t' + str(dic[key])
			counter(count,' words counted ...')
#			print(entry)
sys.stderr.write("\n\t{:,d} entries will go to stout\n".format(len(dic)))
count = 0
for k,v in dic.items():
	count += 1
	entry = k + '\t' + str(v)
	counter(count, ' entries ...')
	print(entry)
sys.stderr.write("\n\t{:,d} entries finished\n".format(count))


# 看起来可能是 topology sort 的题目，不过看了Editorial的解法1，觉得超简单，就用这个解法
# 它的想法是，如果不需要再等其他课要先修的话，那门课的max_time[i]就是精准的，可以放到queue里面
# 因为 queue里面的 max_time[i]都是正确的，便能照着这个值，去更新它的后面课程的 max_time[next]
# 如果后面课程 next 的 indegree[next]减到变成0 代表它又是不用等其他课程了，是精准的 max_time[next]
# 就再把 next 加到 queue里面
# 最后做完，找 max_time 阵列里的最大值，便是全部需要的时间
class Solution:
    def minimumTime(self, n: int, relations: List[List[int]], time: List[int]) -> int:
        next = {}
        indegree = [0]*(n+1) # 因为 relations 是 1-index 从1开始，所以要再加1个
        for a,b in relations: # 1-index从1开始，但time[i]是从0开始
            if a in next:
                next[a].append(b)
            else:
                next[a] = [b]
            indegree[b] += 1
        # 前面建好了 next[a] 及 indegree[b]
        # 现在要检查 indegree[b] 如果是0的话，加到 queue里面
        queue = deque()
        max_time = [0]*(n+1)
        time.insert(0, 0) # 为了让 0-index 的 time[i] 变成 1-index 所以在最前面插入个数字
        for b in range(1,n+1): # 因为坚持要 1-index 哈哈哈
            if indegree[b]==0: # 没有人会到 b 那 b 就是 root 源头
                queue.append(b) # 加到干净的 queue 里面
                max_time[b] = time[b] # 时间 time[b] 就是要完成 b 的时间，因为前面不用等别人

        # 最后在 queue 的循环里，逐一更新后面的人
        while len(queue)>0:
            a = queue.pop()
            if a not in next: continue
            for b in next[a]:
                max_time[b] = max(max_time[b], max_time[a] + time[b])
                indegree[b] -= 1
                if indegree[b] == 0: # 干净了、不用等别人了，便能加入 queue
                    queue.append(b)

        return max(max_time)

        return 0


from Genius import Genius
from SpotifyLyrics import SpotifyLyrics
from SECRETS import *

import tkinter as tk
import tkinter.scrolledtext as st

class SpotifyLyricsApp():
    def __init__(self, spotify):

        self._spotify = spotify

        self._rootWindow = tk.Tk()

        self._rootWindow.title("Spotify Lyrics Player")

        self._setMinSize()

        self._drawLyricsWindow()

        self._drawSongInfo()

        self._drawRefreshButton()

    def run(self):
        self._rootWindow.mainloop()

    def _setMinSize(self):
        self._rootWindow.minsize(460, 600)

    def _drawRefreshButton(self):
        refreshButton = tk.Button(
            master = self._rootWindow, text = "Refresh Lyrics",
            font = ('Times New Roman', 20),
            command = lambda: [self._updateLyricsWindow(), self._updateSongInfo()]
        )

        refreshButton.pack(side='bottom', anchor='se')

    def _drawSongInfo(self):
        self._displaySongInfo = tk.Label(
            master = self._rootWindow,
            text = f"{self._spotify.getCurrentArtist()}: {self._spotify.getCurrentSongName()}",
            font = ("Times New Roman", 20))

        self._displaySongInfo.pack(side='bottom', anchor='se')

    def _drawLyricsWindow(self):
        #IMPLEMENT: Center lyrics in window.
        self._lyricsWindow = st.ScrolledText(self._rootWindow,
                                    width = 52,
                                    height = 30,
                                    font = ("Times New Roman", 15)
                                    )

        self._lyricsWindow.pack(padx = 10, pady = 10, fill = tk.BOTH, expand = True)

        self._lyricsWindow.insert(tk.INSERT, "Start playing a song on Spotify, then click the refresh button.")

        self._lyricsWindow.configure(state='disabled')

    def _updateSongInfo(self):
        self._displaySongInfo["text"] = f"{self._spotify.getCurrentArtist()}: {self._spotify.getCurrentSongName()}"

    def _updateLyricsWindow(self):
        self._lyricsWindow.configure(state='normal')
        self._lyricsWindow.delete("1.0", "end")

        self._lyricsWindow.insert(tk.INSERT, self._spotify.getLyrics())
        self._lyricsWindow.configure(state='disabled')

if __name__ == "__main__":
    genius = Genius(GENIUS_TOKEN)

    spotifyLyrics = SpotifyLyrics(SPOTIFY_CLIENTID, SPOTIFY_CLIENTSECRET, REDIRECT_URI, SCOPES, genius)

    SpotifyLyricsApp(spotifyLyrics).run()


from os import environ

from nipype.interfaces.fsl import FLIRT, ConvertXFM, Reorient2Std
from nipype.pipeline.engine import Workflow, Node
from nipype.interfaces.utility import IdentityInterface

def make_w_coreg_7T_3T(ttype=''):
    n_in = Node(IdentityInterface(fields=[
        'T1w_7T',
        'T1w_3T',
        ]), name='input')

    n_out = Node(IdentityInterface(fields=[
        'mat_t1w3t_to_t1w7t',
        'mat_t1w7t_to_t1w3t',
        ]), name='output')

    n_7T = Node(Reorient2Std(), '7T')
    n_3T = Node(Reorient2Std(), '3T')

    n_coarse = Node(FLIRT(), 'coarse')
    n_coarse.inputs.no_search = True
    n_coarse.inputs.schedule = environ['FSLDIR'] + '/etc/flirtsch/sch3Dtrans_3dof'
    n_coarse.inputs.dof = 6

    n_fine = Node(FLIRT(), 'fine')
    n_fine.inputs.no_search = True
    # n_fine.inputs.cost = 'normcorr'
    n_fine.inputs.dof = 7

    n_3t_7t = Node(ConvertXFM(), name='t1w3t_to_t1w7t')
    n_3t_7t.inputs.concat_xfm = True
    n_3t_7t.inputs.out_file = 'mat_t1w3t_to_t1w7t.mat'

    n_7t_3t = Node(ConvertXFM(), name='t1w7t_to_t1w3t')
    n_7t_3t.inputs.invert_xfm = True
    n_7t_3t.inputs.out_file = 'mat_t1w7t_to_t1w3t.mat'

    w = Workflow('coreg_3T_7T' + ttype)
    w.connect(n_in, 'T1w_7T', n_7T, 'in_file')
    w.connect(n_in, 'T1w_3T', n_3T, 'in_file')
    w.connect(n_7T, 'out_file', n_coarse, 'reference')
    w.connect(n_3T, 'out_file', n_coarse, 'in_file')
    w.connect(n_7T, 'out_file', n_fine, 'reference')
    w.connect(n_coarse, 'out_file', n_fine, 'in_file')
    w.connect(n_coarse, 'out_matrix_file', n_3t_7t, 'in_file')
    w.connect(n_fine, 'out_matrix_file', n_3t_7t, 'in_file2')
    w.connect(n_3t_7t, 'out_file', n_7t_3t, 'in_file')
    w.connect(n_3t_7t, 'out_file', n_out, 'mat_t1w3t_to_t1w7t')
    w.connect(n_7t_3t, 'out_file', n_out, 'mat_t1w7t_to_t1w3t')

    return w


import pandas as pd
import numpy as np
import sklearn
import streamlit as st
import pickle
import tensorflow as tf

header = st.container()
description = st.container()
data = st.container()
X_test = st.container()
modeling = st.container()
btn_desc = st.sidebar.button("Description")

with header:
    st.title("This program is to predict if patient have Heart Disease or not")
    st.markdown("* First you have to enter **inputs** on the left of the window")
    st.markdown("* Then you can press **Predict** button and see the results")
    st.markdown("* If you want to see the variables's description press **Description** button")
if btn_desc :
    with description :
        st.header("Variables' Description")
        st.markdown("* **HeartDisease** - Respondents that have ever reported having coronary heart disease (CHD) or myocardial infarction          (MI)")
        st.markdown("* **BMI** - Body Mass Index (BMI)")
        st.markdown("* **Smoking** - Have you smoked at least 100 cigarettes in your entire life?")
        st.markdown("* **Alcohol Drinking** - Heavy drinkers (adult men having more than 14 drinks per week and adult women having more than         7 drinks per week")
        st.markdown("* **Storke** - (Ever told) (you had) a stroke?")
        st.markdown("* **PhysicalHealth** - Now thinking about your physical health, which includes physical illness and injury, for how             many days during the past 30")
        st.markdown("* **MentalHealth** - Thinking about your mental health, for how many days during the past 30 days was your mental               health not good?")
        st.markdown("* **DiffWalking** - Do you have serious difficulty walking or climbing stairs?")
        st.markdown("* **Sex** - Are you male or female?")
        st.markdown("* **Age Category** - Fourteen-level age category")
        st.markdown("* **Race** - Imputed race/ethnicity value")
        st.markdown("* **Diabetic** - (Ever told) (you had) diabetes?")
        st.markdown("* **PhysicalActivity** - Adults who reported doing physical activity or exercise during the past 30 days other than             their regular job")
        st.markdown("* **Gen Health** - Would you say that in general your health is..")
        st.markdown("* **SleepTime** - On average, how many hours of sleep do you get in a 24-hour period?")
        st.markdown("* **Asthma** - (Ever told) (you had) asthma?")
        st.markdown("* **KidneyDisease** - Not including kidney stones, bladder infection or incontinence, were you ever told you had kidney         disease?")
        st.markdown("* **SkinCancer** - (Ever told) (you had) skin cancer?")

with data :
    st.header("The Heart Disease Dataset")
    df = pd.read_csv("Dataset.csv")
    st.write(df.head())

st.sidebar.header("Inputs Giving")
st.sidebar.markdown("**Note :** 0 = No , 1 = Yes")
BMI = st.sidebar.slider("Please choose input for BMI variable" , min_value = 12.0 , max_value = 94.8 , step = 0.1)
Smoking = st.sidebar.radio("Please choose input for Smoking variable" , options = [0,1] , index = 0)
AlcoholDrinking = st.sidebar.radio("Please choose input for Alcohol Drinking variable" , options = [0,1] , index = 0)
Storke = st.sidebar.radio("Please choose input for Storke variable" , options = [0,1] , index = 0)
PhysicalHealth = st.sidebar.slider("Please choose input for PhysicalHealth variable" , min_value = 0 , max_value = 30 , value = 0 , step =1)
MentalHealth = st.sidebar.slider("Please choose input for MentalHealth variable" , min_value = 0 , max_value = 30 , value = 0 , step = 1)
DiffWalking = st.sidebar.radio("Please choose input for DiffWalking variable" , options = [0,1] , index = 0)
st.sidebar.markdown("0 = Female , 1 = Male")
Sex = st.sidebar.radio("Please choose input for Sex variable" , options = [0,1] , index = 0)
st.sidebar.markdown("18-24 : 0 , 25-29 : 1 , 30-34 : 2 , 35-39 : 3 , 40-44 : 4 , 45-49 : 5, 50-54 : 6 , 55-59 : 7 , 60-64 : 8 , 65 : 9 , 70-74 : 10 , 75-79 : 11 , 80 or older : 12")
AgeCategory = st.sidebar.slider("Please choose input for Age Category variable" , min_value = 0.0 , max_value = 12.0 , step = 1.0)
st.sidebar.markdown("White : 0 , Black : 1 , Asian : 2 , American Indian/Alaskan Native : 3 , Other : 4 , Hispanic : 5")
Race = st.sidebar.selectbox("Please choose input for Race variable" , options = [0,2,3,4,5] , index = 0)
st.sidebar.markdown("No : 0 , Yes : 1 , No, borderline diabetes : 2 , Yes (during pregnancy) : 3")
Diabetic = st.sidebar.selectbox("Please choose input for Diabetic variable" , options = [0,1,2,3] , index = 0)
PhysicalActivity = st.sidebar.radio("Please choose input for PhysicalActivity variable" , options = [0,1] , index = 0)
st.sidebar.markdown("Poor : 0 , Fair : 1 , Good : 2 , Very good : 3 , Excellent : 4")
GenHealth = st.sidebar.selectbox("Please choose input for Gen Health variable" , options = [0,1,2,3,4] , index = 0)
SleepTime = st.sidebar.slider("Please choose input for SleepTime variable" , min_value = 1 , max_value = 24 , step = 1)
Asthma = st.sidebar.radio("Please choose input for Asthma variable" , options = [0,1] , index = 0)
KidneyDisease = st.sidebar.radio("Please choose input for KidneyDisease variable" , options = [0,1] , index = 0)
SkinCancer = st.sidebar.radio("Please choose input for SkinCancer variable" , options = [0,1] , index = 0)

with X_test :
    st.header("You have entered these inputs")
    data = pd.DataFrame(data = {"BMI" :[BMI]  , "Smoking" : [Smoking],
                                "AlcoholDrinking" :[AlcoholDrinking], "Storke" : [Storke] , "PhysicalHealth" : [PhysicalHealth],
                                "MentalHealth" : [MentalHealth] , "DiffWalking" : [DiffWalking] , "Sex" : [Sex],
                                 "AgeCategory" : [AgeCategory] , "Race" : [Race] , "Diabetic" : [Diabetic], "PhysicalActivity" :                                                [PhysicalActivity],"GenHealth" : [GenHealth] , "SleepTime" : [SleepTime] , "Asthma" : [Asthma] ,                                            "KidneyDisease" : [KidneyDisease] , "SkinCancer" : [SkinCancer]})
    st.write(data)

with modeling :
    btn_predict = st.sidebar.button("Predict")

    if btn_predict :
        st.header("Prediction")
        scaler = pickle.load(open("scaler_deep" , "rb"))
        model = tf.keras.models.load_model("Deep_Learning.h5")
        y_pred_proba = tf.squeeze(model.predict(scaler.transform(data)))
        y_pred = tf.math.round(model.predict(scaler.transform(data)))

        if y_pred == [0] :
                st.markdown("The patient **doesn't** have Heart Disease and probablity is {:.0%}".format(y_pred_proba))

        elif y_pred == [1] :
                st.markdown("The patient **has** Heart Disease and probablity is {:.0%}".format(y_pred_proba))


# scope = region that a variable is recognized
#         A variable is only available from inside the region it is created.
#         A global and locally scoped versions of a variable can be created

name = "Name in global scope."  # Global scope (available inside & outside functions)

def out_name():
    name_function = "Name of function. In local scope."  # Local scope (available only inside this function
    print(name_function)

out_name()
print(name)


import configparser
from datetime import datetime
import os
import pyspark.sql.functions as F
from pyspark.sql import SparkSession
from pyspark.sql.types import *
from pyspark.sql.window import Window
from datetime import datetime as dt

def create_spark_session():
    '''
        creates spark session
    '''
    spark = SparkSession \
        .builder \
        .config("spark.jars.packages", "org.apache.hadoop:hadoop-aws:3.2.0") \
        .config("spark.hadoop.fs.s3a.impl","org.apache.hadoop.fs.s3a.S3AFileSystem") \
        .config("spark.hadoop.fs.s3a.access.key", os.environ['AWS_ACCESS_KEY_ID']) \
        .config("spark.hadoop.fs.s3a.secret.key", os.environ['AWS_SECRET_ACCESS_KEY']) \
        .enableHiveSupport() \
        .getOrCreate()
    return spark

def process_immigration_data(spark, input_data, output_data):
    '''
        process immigration data and write the processed data into multiple (immig_fact, visa_type_dim, travel_mode_dim, time_dim) parquet files

        arguments:
        spark -- spark session
        input_data -- input data folder path
        output_data -- output data folder path
    '''
    print("processing immigration data -->>>>")
    # get filepath to immigration data file
    immig_data = input_data + "sas_data/*.parquet"

    immig_fact_schema = StructType([
        StructField("cicid",IntegerType()),
        StructField("i94yr",ShortType()),
        StructField("i94mon",ShortType()),
        StructField("i94cit",ShortType()),
        StructField("i94res",ShortType()),
        StructField("i94port",StringType()),
        StructField("arrdate",FloatType()),
        StructField("i94mode",ShortType()),
        StructField("i94addr",StringType()),
        StructField("depdate",FloatType()),
        StructField("i94bir",ShortType()),
        StructField("i94visa",ShortType()),
        StructField("count",ShortType()),
        StructField("dtadfile",LongType()),
        StructField("visapost",StringType()),
        StructField("occup",StringType()),
        StructField("entdepa",StringType()),
        StructField("entdepd",StringType()),
        StructField("entdepu",StringType()),
        StructField("matflag",StringType()),
        StructField("biryear",ShortType()),
        StructField("dtaddto",LongType()),
        StructField("gender",StringType()),
        StructField("insnum",IntegerType()),
        StructField("airline",StringType()),
        StructField("admnum",FloatType()),
        StructField("fltno",IntegerType()),
        StructField("visatype",StringType()),
    ])

    # read immigration data in parquet format
    print(immig_data)
    #df_immig =spark.read.parquet(immig_data, schema = immig_fact_schema)

    #Schema not working in AWS EMR environment
    df_immig =spark.read.parquet(immig_data)

    df_immig.createOrReplaceTempView("immigration_data")

    # extract columns to create immigration fact table
    immig_fact = spark.sql (""" select
                    CAST(cicid as INT) as cicid,
                    CAST(i94mon as INT) as i94_month,
                    trim(CAST(i94port as String)) as partition_port,
                    trim(CAST(i94port as String)) as i94_port,
                    CAST(i94cit as INT) as i94_citizen,
                    CAST(i94res as INT) as i94_resident,
                    CAST(arrdate as INT) as i94_arrival_date,
                    CAST(i94mode as INT) as travel_mode_id,
                    CAST(i94addr as String) as state_code,
                    CAST(depdate as INT) as i94_departure_date,
                    CAST(i94bir as INT) as i94_age,
                    CAST(i94visa as INT) as visa_category_id,
                    to_date(dtadfile,'yyyyMMdd') as i94_date_added,
                    to_date(dtaddto, 'yyyyMMdd') as i94_date_admitted,
                    occup as i94_occupation,
                    entdepa as i94_arrival_flag,
                    entdepd as i94_departure_flag,
                    entdepu as i94_update_flag,
                    matflag as i94_match_flag,
                    CAST(biryear as INT) as i94_birth_year,
                    gender as i94_gender,
                    insnum as i94_insnum,
                    airline as i94_airline,
                    CAST(admnum as Double) as i94_admission_number,
                    CAST(fltno as String) as i94_flight_number,
                    CAST(visatype as String) as i94_visa_type
                    from
                    immigration_data""")

    immig_fact_updated = immig_fact.na.fill(0, subset=["travel_mode_id"])

    # write songs table to parquet files partitioned by year and artist
    immig_fact_out = output_data + "immig_fact.parquet"
    immig_fact_updated.write \
        .mode("append") \
        .partitionBy("partition_port", "i94_month") \
        .parquet(immig_fact_out)

    #Visa Type dimension table
    print("writing visa_type_dim parquet files -->>>>")
    visa_type_dim = spark.sql (""" select
                        distinct CAST(i94visa as INT) as visa_category_id,
                        CASE(CAST(i94visa as INT)) WHEN 1 then 'Business' WHEN 2 THEN 'Pleasure' ELSE 'Student' END as visa_type
                        from
                        immigration_data""")

    visa_type_dim_out = output_data + "visa_type_dim.parquet"

    visa_type_dim.write \
        .mode("append") \
        .parquet(visa_type_dim_out)

    #Travel mode dimension table
    print("writing travel_mode_dim parquet files -->>>>")
    travel_mode_dim = spark.sql (""" select
                            distinct CAST(i94mode as INT) as travel_mode_id,
                            CASE(CAST(i94mode as INT)) WHEN 1 then 'Air' WHEN 3 THEN 'Land' WHEN 2 THEN 'Sea' WHEN 9 THEN 'Not reported' ELSE 'Undefined' END as travel_mode
                            from
                            immigration_data""")

    travel_mode_dim_updated = travel_mode_dim.na.fill(0, subset=["travel_mode_id"])

    travel_mode_dim_out = output_data + "travel_mode_dim.parquet"

    travel_mode_dim_updated.write \
        .mode("append") \
        .parquet(travel_mode_dim_out)

    #Time dimension table
    time_dim = spark.sql (""" select
                            distinct CAST(arrdate as INT) as arrival_sas,
                            date_add(to_date('1960-01-01', "yyyy-MM-dd"), CAST(arrdate as INT)) as arrival_date,
                            dayofmonth(date_add(to_date('1960-01-01', "yyyy-MM-dd"), CAST(arrdate as INT))) as day,
                            month(date_add(to_date('1960-01-01', "yyyy-MM-dd"), CAST(arrdate as INT))) as month,
                            month(date_add(to_date('1960-01-01', "yyyy-MM-dd"), CAST(arrdate as INT))) as parition_month,
                            year(date_add(to_date('1960-01-01', "yyyy-MM-dd"), CAST(arrdate as INT))) as year,
                            date_format(date_add(to_date('1960-01-01', "yyyy-MM-dd"), CAST(arrdate as INT)),"E") as weekday
                            from
                            immigration_data""")

    time_dim_out = output_data + "time_dim.parquet"

    time_dim.write \
        .mode("append") \
        .partitionBy("parition_month") \
        .parquet(time_dim_out)

def main():
    '''
        main method gets invoked when the immigration_etl.py is executed
    '''
    config = configparser.ConfigParser()
    config.read('dl.cfg')

    print("printing keys --->>>>")
    print(config['AWS']['AWS_ACCESS_KEY_ID'])
    print(config['AWS']['AWS_SECRET_ACCESS_KEY'])
    os.environ["AWS_ACCESS_KEY_ID"]= config['AWS']['AWS_ACCESS_KEY_ID']
    os.environ["AWS_SECRET_ACCESS_KEY"]= config['AWS']['AWS_SECRET_ACCESS_KEY']

    spark = create_spark_session()
    input_data = "s3a://data-engineering-nd/capstone-project/"
    output_data = "s3a://data-engineering-nd/capstone-project/output/"

    #local file path
    #input_data = ""
    #output_data = ""

    process_immigration_data(spark, input_data, output_data)

if __name__ == "__main__":
    main()


# Day2 Part 2

fileContents = open("AdventOfCode2022/Day 2/input.txt")
arr = fileContents.read().split('\n')

# X = Rock Y = Paper Z = Scissors
# 1 for Rock, 2 for Paper, and 3 for Scissors
#  A for Rock, B for Paper, and C for Scissors
'''
A X = tie
A Y = Win
A Z = Lose
B X = Lose
B Y = Tie
B Z = Win
C X = Win
C Y = Lose
C Z = Tie
'''

points = {'X':1, 'Y': 2, 'Z': 3}

lose = {'A': 'Z', 'B': 'X', 'C':'Y'}
win = {'A': 'Y', 'B': 'Z', 'C': 'X'}
tie = {'A': 'X', 'B': 'Y', 'C': 'Z'}

totes = 0
for line in arr:
    strat = line.split(' ')
    opponent, you = strat[0], strat[1]
    if you == 'X':
        #lose
        totes += points[lose[opponent]]
        #no other points
    if you == 'Y':
        #draw
        totes += 3
        totes += points[tie[opponent]]
    if you == 'Z':
        #win
        totes += 6
        totes += points[win[opponent]]

print(totes)


import math

def solution():
    a, b, n = map(int, input().split())
    q = n - 1
    k = int(math.sqrt(n) + 1) % q
    r = q - k

    a1 = pow(a, r, n)
    key = b

    table_lu = {key: 0}

    for i in range(1, k):
        key = (key * a1) % n
        table_lu[key] = i

    key = 1
    for i in range(k + 1):
        if key in table_lu:
            print((k * table_lu[key] + i) % q)
            return
        key = (key * a) % n
    print(-1)

def main():
    solution()

if __name__ == '__main__':
    main()


import pytest
import os
import json
from collections import defaultdict

from turf.helpers import point, multi_line_string
from turf.utils.exceptions import InvalidInput
from turf.utils.error_codes import error_code_messages
from turf.utils.test_setup import get_fixtures
from turf.length import length

current_path = os.path.dirname(os.path.realpath(__file__))

fixtures = get_fixtures(current_path)

class TestLength:
    allowed_inpuy_types = [
        "LineString",
        "MultiLineString",
        "Polygon",
        "MultiPolygon",
    ]

    @pytest.mark.parametrize(
        "fixture",
        [
            pytest.param(fixture, id=fixture_name)
            for fixture_name, fixture in fixtures.items()
        ],
    )
    def test_length(self, fixture):
        assert round(length(fixture["in"], {"units": "feet"})) == fixture["out"]

    def test_length_with_feature_classes(self):
        feature = multi_line_string(
            [
                [
                    [-77.031669, 38.878605],
                    [-77.029609, 38.881946],
                    [-77.020339, 38.884084],
                    [-77.025661, 38.885821],
                    [-77.021884, 38.889563],
                    [-77.019824, 38.892368],
                ],
                [
                    [-77.041669, 38.885821],
                    [-77.039609, 38.881946],
                    [-77.030339, 38.884084],
                    [-77.035661, 38.878605],
                ],
            ]
        )

        assert round(length(feature, {"units": "feet"})) == 15433

    @pytest.mark.parametrize(
        "input_value, exception_value",
        [
            pytest.param(
                [0, 0],
                error_code_messages["InvalidGeometry"](allowed_inpuy_types),
                id="InvalidGeometry",
            ),
            pytest.param(
                point([0, 0]),
                error_code_messages["InvalidGeometry"](allowed_inpuy_types),
                id="InvalidGeometry",
            ),
        ],
    )
    def test_exception(self, input_value, exception_value):
        with pytest.raises(Exception) as excinfo:
            length(input_value)

        assert excinfo.type == InvalidInput
        assert str(excinfo.value) == exception_value


#!/usr/bin/env python

import re
import sys

from setuptools import setup, find_packages

def version():
    with open('pypot/_version.py') as f:
        return re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", f.read()).group(1)

extra = {}
if sys.version_info >= (3,):
    extra['use_2to3'] = True

setup(name='pypot',
      version=version(),
      packages=find_packages(),

      install_requires=['numpy', 'pyserial'],

      extras_require={
          'tools': [],  # Extras require: PyQt4 (not a PyPi packet)
          'doc': ['sphinx', 'sphinx-bootstrap-theme'],
          'server': ['bottle', 'tornado', 'zmq'],
          'remote-robot': ['zerorpc'],
      },

      entry_points={
          'gui_scripts': [
              'herborist = pypot.tools.herborist.herborist:main [tools]',
          ],
      },

      setup_requires=['setuptools_git >= 0.3', ],

      include_package_data=True,
      exclude_package_data={'': ['README', '.gitignore']},

      zip_safe=True,

      author='Pierre Rouanet, Steve N\'Guyen, Matthieu Lapeyre',
      author_email='pierre.rouanet@gmail.com',
      description='Python Library for Robot Control',
      url='https://github.com/poppy-project/pypot',
      license='GNU GENERAL PUBLIC LICENSE Version 3',

      classifiers=[
          "Programming Language :: Python :: 2",
          "Programming Language :: Python :: 3",
          "Topic :: Scientific/Engineering", ],

      **extra
      )


import heapq
from open_list import OpenList
from tspState import TSPState
from tspHelper import TSPHelper
from tsp_data import TSPData

class TSPOpenList(OpenList):
    """Priority Queue implementation of OpenList used for TSP problem. Contains:
       1. heap - for storing items as a priority queue
       2. counter - represents the no of states added to OpenList. This is equal to the number of
          nodes expanded in the search tree of the TSP problem
       3. size - represents the number of items in the open list
    """

    def __init__(self):
        self.heap = []
        self.counter = 0
        self.size = 0

    def add(self, tspState):
        self.counter += 1
        self.size += 1
        #f(state) is used as the priority. When 2 states have same priority, the state that was
        #expanded first is returned
        heapq.heappush(self.heap, (tspState.fOfState(), self.counter, tspState))

    def getItem(self):
        self.size -= 1
        return heapq.heappop(self.heap)[2]

    def getExpandedStatesCount(self):
        return self.counter

    def summary(self):
        print(15*'=' + 'OpenList Summary' + 15*'=')
        print('Open List Size = %d' % self.size)
        print('No of states added to OpenList so far = %d' % self.counter)
        print('Items:')
        for item in self.heap:
            print(item[2].getPath(), end=' ')
            print('f(state)=%d' % item[2].fOfState())
        print(42*'=')

if __name__ == '__main__':
    tspData = TSPData('/Users/apple/Documents/Projects/randTSP/7/instance_5.txt');
    tspData.summary()
    tspHelper = TSPHelper(tspData)
    tspHelper.setStartCity('A')

    startState = TSPState(tspHelper, 'A')
    aState = TSPState(tspHelper, 'A->E->B->F');
    aState = startState.moveToNextState(aState)
    successors = aState.getSuccessors()

    #Testing add function
    openList = TSPOpenList()
    for successor in successors:
        openList.add(successor)
    openList.summary()

    #Testing get function - should return the state with lowest f(state)
    nextState = openList.getItem()
    print('The next state chosen is:')
    nextState.summary()


import pickle
import os
import pandas as pd

def countingPhrase():

    dictionary_phrase = {} #make a dictionary that used key value
    f = open("D:/CSE 8th semester/PROJECT IDEAS/8thsem_project/dictionary",'rb')
    spam_dict = pickle.load(f)
    f.close()
    files = os.listdir(r"D:/CSE 8th semester/PROJECT IDEAS/8thsem_project/TR")
    for file in files:
        fp = open(r"D:/CSE 8th semester/PROJECT IDEAS/8thsem_project/TR/"+file,encoding='utf-8')
        try:
            text = fp.read()
        except:
            continue
        count = 0
        for phrase in spam_dict:
            text = text.lower()
            phrase = phrase.lower()
            if phrase in text:
                count += 1
        dictionary_phrase[file] = count
    with open(r"D:/CSE 8th semester/PROJECT IDEAS/8thsem_project/phrasecount", 'wb') as fp:
        pickle.dump(dictionary_phrase,fp)
        fp.close()

def countLines():
    dictionary_lines = {}
    files = os.listdir(r"D:/CSE 8th semester/PROJECT IDEAS/8thsem_project/TR")
    for file in files:
        fp = open(r"D:/CSE 8th semester/PROJECT IDEAS/8thsem_project/TR/"+file,encoding='utf-8')
        try:
            text = fp.read()
        except:
            continue
        count = text.count("\n")
        dictionary_lines[file] = count
    with open(r"D:/CSE 8th semester/PROJECT IDEAS/8thsem_project/linecount", 'wb') as fp:
        pickle.dump(dictionary_lines,fp)
        fp.close()

def count_attachments():
    num_of_attachments = {}
    files = os.listdir(r"D:/CSE 8th semester/PROJECT IDEAS/8thsem_project/TR")
    for file in files:
        fp = open(r"D:/CSE 8th semester/PROJECT IDEAS/8thsem_project/TR/"+file,encoding='utf-8')
        try:
            text = fp.read()
        except:
            continue
        index = text.lower().find("attachment")
        count = 0
        if index > 0:
            i = index + len("attachment")
            while text[i]!='\n':
                if text[i].isnumeric():
                    count = count*10 + int(text[i])
                i += 1
        num_of_attachments[file] = count
    with open(r"D:/CSE 8th semester/PROJECT IDEAS/8thsem_project/attachcount","wb") as fp:
        pickle.dump(num_of_attachments,fp)
        fp.close()

def character_count():
    num_of_characters = {}
    files = os.listdir(r"D:/CSE 8th semester/PROJECT IDEAS/8thsem_project/TR")
    for file in files:
        fp = open(r"D:/CSE 8th semester/PROJECT IDEAS/8thsem_project/TR/"+file,encoding='utf-8')
        try:
            text = fp.read()
        except:
            continue
        count = 0
        character_array = {"$","#","*","_","%"}
        for char in character_array:
            count += text.count(char)
        num_of_characters[file] = count

    with open("char_count",'wb') as f:
        pickle.dump(num_of_characters,f)
        f.close()

def gather_features_into_dataframe():
    with open(r"D:/CSE 8th semester/PROJECT IDEAS/8thsem_project/phrasecount", 'rb') as fp:
        phrase_count = pickle.load(fp)
    with open(r"D:/CSE 8th semester/PROJECT IDEAS/8thsem_project/charcount", 'rb') as fp:
        char_count = pickle.load(fp)
    with open(r"D:/CSE 8th semester/PROJECT IDEAS/8thsem_project/linecount", 'rb') as fp:
        line_count = pickle.load(fp)
    with open(r"D:/CSE 8th semester/PROJECT IDEAS/8thsem_project/attachcount", 'rb') as fp:
        attach_count = pickle.load(fp)

    #list_of_emails = os.listdir(r"D:/CSE 8th semester/PROJECT IDEAS/8thsem_project/TR")
    #index_of_emails = map(lambda eml: int(''.join([x for x in eml if x.isdigit()])),list_of_emails)
    records = []
    for key in char_count.keys():
        index = int(''.join([x for x in eml if x.isdigit()]))
        records.append((index, char_count[key], attach_count[key], line_count[key], phrase_count[key]))
    labels = ["index","charactercount","attachmentcount","linecount","spamphraseccount"]
    features = pd.DataFrame.from_reccords(records, columns=labels)
    with open("features","rb") as fp:
        pickle.dump(features,fp)


import json

from django.test import TestCase, Client
from django.urls import reverse

from rest_framework import status

from ..models import FieldType
from ..serializer import FieldTypeSerializer

client = Client()

def get_valid_object():
    valid_object = {
        "name": "Test Name",
        "description": "Test Description"
    }
    return valid_object

def get_invalid_object():
    invalid_object = {
        "name": "",
        "description": ""
    }
    return invalid_object

class GetAllFieldTypes(TestCase):
    """Test to GET every field type from API"""

    def setUp(self):
        FieldType.objects.create(**get_valid_object())
        FieldType.objects.create(**get_valid_object())

    def test_get_every_field_type(self):

        response = client.get(reverse("fieldtype-list"), format="json")

        # get data from database
        field_types = FieldType.objects.all()
        serializer = FieldTypeSerializer(field_types, many=True)

        self.assertEqual(response.data, serializer.data)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

class GetSingleFieldTypes(TestCase):
    """Test to GET single field type from API"""

    def setUp(self):
        self.field_type1 = FieldType.objects.create(**get_valid_object())
        self.field_type2 = FieldType.objects.create(**get_valid_object())

    def test_get_valid_single_field_type(self):

        response = client.get(reverse(
            "fieldtype-detail",
            kwargs={"pk": self.field_type1.pk}))

        # get data from database
        field_type = FieldType.objects.get(pk=self.field_type1.pk)
        serializer = FieldTypeSerializer(field_type)

        self.assertEqual(response.data, serializer.data)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_get_invalid_single_field_type(self):
        invalid_id = 450
        response = client.get(reverse(
            "fieldtype-detail",
            kwargs={"pk": invalid_id}))

        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

class CreateNewFieldTypes(TestCase):
    """Test for creaating a new field type"""

    def setUp(self):
        self.valid_field_type = get_valid_object()
        self.invalid_field_type = get_invalid_object()

    def test_create_valid_field_type(self):

        response = client.post(reverse("fieldtype-list"),
                               data=json.dumps(self.valid_field_type),
                               content_type="application/json")

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

    def test_create_invalid_field_type(self):

        response = client.post(reverse("fieldtype-list"),
                               data=json.dumps(self.invalid_field_type),
                               content_type="application/json")

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

class UpdateSingleFieldTypes(TestCase):
    """Test to Update a single field type"""

    def setUp(self):
        self.field_type = FieldType.objects.create(**get_valid_object())

        self.valid_field_type = get_valid_object()
        self.invalid_field_type = get_invalid_object()

    def test_update_valid_field_type(self):

        response = client.put(reverse(
            "fieldtype-detail",
            kwargs={"pk": self.field_type.pk}),
            data=json.dumps(self.valid_field_type),
            content_type="application/json")

        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_update_invalid_single_field_type(self):
        response = client.put(reverse(
            "fieldtype-detail",
            kwargs={"pk": self.field_type.pk}),
            data=json.dumps(self.invalid_field_type),
            content_type="application/json")

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

class DeleteSingleFieldTypes(TestCase):
    """Test to DELETE single field type from API"""

    def setUp(self):
        self.field_type = FieldType.objects.create(**get_valid_object())

    def test_delete_valid_single_field_type(self):

        response = client.delete(reverse(
            "fieldtype-detail",
            kwargs={"pk": self.field_type.pk}))

        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

    def test_delete_invalid_single_field_type(self):
        invalid_id = 450
        response = client.get(reverse(
            "fieldtype-detail",
            kwargs={"pk": invalid_id}))

        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)


import discord
from discord.ext import commands

class InfoCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

        @bot.command()
        async def info(ctx):
            '''
      Returns server stats
      '''
            guild = ctx.guild
            embed = discord.Embed(timestamp=ctx.message.created_at,
                                  color=0xd1190d)
            embed.set_thumbnail(url=guild.icon_url)
            embed.add_field(name="Members", value=guild.member_count)
            embed.add_field(name="Region", value=guild.region, inline=True)
            embed.add_field(name="Owner", value=guild.owner, inline=True)
            embed.add_field(name="Created",
                            value=guild.created_at,
                            inline=True)
            embed.add_field(name="Channels",
                            value=len(guild.channels),
                            inline=True)
            embed.add_field(name="Roles", value=len(guild.roles), inline=True)
            embed.add_field(name="Boosters",
                            value=guild.premium_subscription_count,
                            inline=True)
            embed.set_author(name=f"{guild} | ID: {guild.id}",
                             icon_url=guild.icon_url)
            embed.set_footer(
                text=
                f"Requested by {ctx.author.name}#{ctx.author.discriminator}",
                icon_url=ctx.author.avatar_url)

            await ctx.send(embed=embed)

def setup(bot):
    bot.add_cog(InfoCog(bot))


"""
Support functions for PySquaredle.
"""

import argparse
import math
import random
import sys

from pysquaredle.web import get_letters_from_web

def puzzle_letters(args: argparse.Namespace) -> str:
    """
    Use the arguments to determine the letters to use.
    """
    if args.square:
        return random_letters(args.square**2)

    if not args.letters:
        return get_letters_from_web(args.express)

    letters = str(args.letters)
    length = len(letters)
    potential_side = math.sqrt(length)

    # must be square, optionally add letters
    if potential_side % 1:
        if not args.auto_extend:
            print("Invalid puzzle: letters must form a square grid.")
            sys.exit(-1)

        letters = extend(letters, potential_side)
    return letters

def parse_args() -> argparse.Namespace:
    """
    Interpret the command-line arguments and store for later use.
    """
    parser = argparse.ArgumentParser(
        description="Solves the Squaredle puzzle, as seen on https://squaredle.app",
    )

    group = parser.add_mutually_exclusive_group()
    group.add_argument(
        "letters",
        help="""the puzzle letters. If not specified will try to download
        from https://squaredle.app. For puzzles with gaps, use underscores
        ('_') to represent the gaps. See also --express""",
        nargs="?",
    )
    group.add_argument(
        "-x",
        "--square",
        help="generate random square of x by x letters. Letter"
        " distribution matches a popular grid-based word game rhyming"
        " with scrabble",
        type=int,
    )

    output_group = parser.add_argument_group("output options")

    output_group.add_argument(
        "-c",
        "--single-column",
        action="store_true",
        help="display results as a single column. (default: %(default)s)",
    )

    output_group.add_argument(
        "-g",
        "--grid",
        action="store_true",
        help="display letters grid. (default: %(default)s)",
    )
    output_group.add_argument(
        "-H",
        "--headers",
        dest="headers",
        action="store_true",
        help="display headers for length-grouped solutions (default: %(default)s)",
    )
    output_group.add_argument(
        "-l",
        "--length",
        action="store_true",
        help="group solutions by word length (default: %(default)s)",
    )
    output_group.add_argument(
        "-m",
        "--multiple",
        help="in GUI mode, show all solutions for a given word (default: %(default)s)",
        action="store_true",
    )
    parser.add_argument(
        "-r",
        "--random",
        action="store_true",
        help="randomise letter order, for setting puzzles. Shows grid (default: %(default)s)",
    )
    output_group.add_argument(
        "-s",
        "--sort",
        action="store_true",
        help="sort solutions alphabetically (default: %(default)s)",
    )
    output_group.add_argument(
        "-u",
        "--gui",
        action="store_true",
        help="run in GUI mode. Some flags only affect text output, not GUI (default: %(default)s)",
    )

    parser.add_argument(
        "-t",
        "--auto-extend",
        help="add extra letters to the grid to make it square (default: %(default)s)",
        action="store_true",
    )

    advanced_group = parser.add_argument_group("advanced options")
    advanced_group.add_argument(
        "-d",
        "--debug",
        action="store_true",
        help="display cell neighbour list for debugging (default: %(default)s)",
    )

    advanced_group.add_argument(
        "-e",
        "--express",
        action="store_true",
        help="use express puzzle, otherwise standard. Only used when letters are downloaded (default: %(default)s)",
    )

    advanced_group.add_argument(
        "-f",
        "--file",
        help="specify word list (default: %(default)s)",
        default="./word_list.txt",
    )
    advanced_group.add_argument(
        "-z",
        "--slow-mode",
        action="store_true",
        help="show progress as it goes (default: %(default)s)",
    )

    args = parser.parse_args()
    return args

def random_letters(count: int) -> str:
    """
    Generate a string of count nicely distributed random letters
    """
    return "".join(
        random.sample(
            "EEEEEEEEEEEEEEEEAAAAAAAAAIIIIIIIIIOOOOOOOONNNNNN"
            "RRRRRRTTTTTTLLLLSSSSUUUDDDDGGGBBCCMMPPFFHHVVWWYYKJXQZ",
            count,
        )
    )

def shuffle(letters: str) -> str:
    """
    Reorder the letters
    """
    return "".join(random.sample(letters, len(letters)))

def extend(letters: str, potential_side: float) -> str:
    """
    Add enough random letters to make the grid square
    """
    diff = (int(potential_side) + 1) ** 2 - len(letters)
    print(f"Adding {diff} letters to make a square grid.")
    letters += random_letters(diff)
    return letters


import numpy as np
from scipy.sparse import *
from collections import defaultdict
import itertools
from operator import itemgetter
import sys

##          Εφαρμογή Αραιών Μητρών
##          Αναλυτικά σχόλια για τον κώδικα στην αναφορά!

def presentation():
    print("Άσκηση αραιών μητρών!")
    print("Αρχικά, θα κατασκευάσουμε τις 10.000 τριάδες που αντιστοιχούν σε αραιά μήτρα διαστάσεων 1000x1000 που περιέχει μηδενικά στο 99% και τιμές από 1.0 εώς 5.0 σε ποσοστό 1%")
    print("Επιλέξτε 1 για απάντηση του ερωτήματος Α.")
    print("Επιλέξτε 2 για απάντηση των ερωτημάτων B και Γ.")
    e = int(input("Επιλογή:" ))
    if e==1:
        question_A()
    elif e==2:
        question_BC()
    else:
        print("Δεν υπάρχει αυτή η επιλογή! Ξαναπροσπαθήστε...")
        sys.exit()

def makeMatrix():
    global row
    global col
    global val
    np.random.seed(1053711)
    row = np.random.randint(0, 1000, 10000)
    col = np.random.randint(0, 1000, 10000)

    q=1
    while q:
        i=0
        for k in range(1000):
            if row[k]==col[k]:
                col[k]=np.random.randint(0, 1000)
                i += 1
        if i==0:
            break

    global mat_csr
    global mat

    val = np.round(np.random.uniform(1.0, 5.0, 10000), 1)
    mat_coo = coo_matrix((val, (row, col)), shape=(1000, 1000))
    mat_csr = mat_coo.tocsr()

    global dnext
    dnext = defaultdict(list)
    for i,j in zip(row, col):
        dnext[i].append(j)

    return mat_csr

def question_A():

    print("Ταξίδια με 1 ή 2 πτήσεις:")
    print("Επιλέξτε τον αριθμό του αεροδρομίου σας (0 - 999) και δείτε τα διαθέσιμα ταξίδια με έως και 2 πτήσεις (0 - 999) σε αεροδρόμια:")
    e = int(input("Επιλογή:" ))
    if e<0 or e>999:
        print("Δεν υπάρχει αυτή η επιλογή! Ξαναπροσπαθήστε...")
        sys.exit()

    dest1 = sorted(dnext[e])
    dest2 = []
    print("Απευθείας ταξίδια από το αεροδρόμιό σας:", dest1)

    for i in (dest1):
        dest2.append(dnext [i])

    dest2_merged = sorted(list(itertools.chain(*dest2)))

    for i in range(len(dest1)):
        j = 0
        while j < len(dest2_merged):
            if dest1[i]==dest2_merged[j]:
                dest2_merged.pop(j)
            else:
                j += 1

    print("Ταξίδια 2 πτήσεων από το αεροδρόμιό σας:", sorted(dest2_merged))
    return

def question_BC():

    print("Κυκλικά ταξίδια:")
    print("Επιλέξτε τον αριθμό του αεροδρομίου σας (0 - 999) και δείτε τα διαθέσιμα κυκλικά ταξίδια (0 - 999) σε αεροδρόμια:")
    e = int(input("Επιλογή:" ))
    if e<0 or e>999:
        print("Δεν υπάρχει αυτή η επιλογή! Ξαναπροσπαθήστε...")
        sys.exit()

    two_flights = defaultdict(list)
    three_flights = defaultdict(list)
    four_flights = defaultdict(list)
    five_flights = defaultdict(list)
    six_flights = defaultdict(list)
    seven_flights = defaultdict(list)
    eight_flights = defaultdict(list)
    final_trip_list = defaultdict(list)
    trips = []

    print("Επιλέξτε τον μέγιστο αριθμό των πτητικών σκελών του ταξιδιού")
    print("Προειδοποίηση: Για επιλογή μεγαλύτερη των 6 πτητικών σκελών ενδέχεται να υπάρχει καθυστέρηση αρκετών λεπτών!")
    nmax = int(input("Επιλογή:" )) + 1
    if nmax<0 or nmax>10:
        print("Δεν υπάρχει αυτή η επιλογή! Ξαναπροσπαθήστε...")
        sys.exit()
    print(" ")
    print("Επιλέξατε μέχρι", nmax - 1, "πτητικά σκέλη.")

    def find_round_trips(begin, end, graph, nmax):
        trip = [(begin, [begin])]
        while len(trip):
            node, path = trip.pop(0)
            if path[-1] == end:
               yield path
            for next_node in graph[node]:
                if next_node in path[1:]:
                    continue
                else:
                    if len(path)<nmax:
                        trip.append((next_node, path + [next_node]))

    print("Επιλέξτε 1 για να δείτε όλα τα κυκλικά ταξίδια από το αεροδρόμιό σας.")
    print("Επιλέξτε 2 για να δείτε τα νόμιμα κυκλικά ταξίδια βάσει των χρονικών ορίων, μαζί με τον χρόνο που απαιτούν και τα μικρότερα/μεγαλύτερα ταξίδια, χωρίς να επιτρέπεται να γίνει ταξίδι στην ίδια ενδιάμεση πόλη 2 φορές.")
    a = int(input("Επιλογή:" ))
    print(" ")
    print("Γίνονται υπολογισμοί...")
    print(" ")
    if a==1:
        [print(x) for x in find_round_trips(e, e, dnext, nmax)]
    elif a==2:
        [trips.append(x[:]) for x in find_round_trips(e, e, dnext, nmax)]
        trips.pop(0)

        def find_legal_trips(trip):
            return len(trip) == len(set(trip))

        i = 0
        while i < len(trips):
           if find_legal_trips(trips[i][1:-1]):
               i += 1
           else:
               trips.pop(i)

        def find_time(l):
            time = 0
            value_list = []
            for i in range(len(l)-1):
                value_list.append(l[i:i+2])
            for i in range(len(value_list)):
                time += (mat_csr[value_list[i][0], value_list[i][1]])
            return time

        for i in range(len(trips)):
            time = find_time(trips[i])
            trip = tuple(trips[i])
            if len(trips[i])==3:
                two_flights[trip]=time
                if time <= 4:
                    final_trip_list[trip]=time
            elif len(trips[i])==4:
                three_flights[trip]=time
                if time <= 7.5:
                    final_trip_list[trip]=time
            elif len(trips[i])==5:
                four_flights[trip]=time
                if time <= 10.5:
                    final_trip_list[trip]=time
            elif len(trips[i])==6:
                five_flights[trip]=time
                if time <= 14:
                    final_trip_list[trip]=time
            elif len(trips[i])==7:
                six_flights[trip]=time
                if time <= 17:
                    final_trip_list[trip]=time
            elif len(trips[i])==8:
                seven_flights[trip]=time
                if time <= 20.5:
                    final_trip_list[trip]=time
            elif len(trips[i])==9:
                eight_flights[trip]=time
                if time <= 24:
                    final_trip_list[trip]=time

        print("Διαθέσιμα κυκλικά ταξίδια που πληρούν τις χρονικές προϋποθέσεις:")
        for key in final_trip_list:
            print(key, "των",len(list(key))-1,"πτητικών σκελών με συνολικό χρόνο", final_trip_list[key],"ωρών." )

        print(" ")
        print("Για το ερώτημα Γ:")
        print(" ")

        two_flights = sorted(two_flights.items(), key=itemgetter(1))
        three_flights = sorted(three_flights.items(), key=itemgetter(1))
        four_flights = sorted(four_flights.items(), key=itemgetter(1))
        five_flights = sorted(five_flights.items(), key=itemgetter(1))
        six_flights = sorted(six_flights.items(), key=itemgetter(1))
        seven_flights = sorted(seven_flights.items(), key=itemgetter(1))
        eight_flights = sorted(eight_flights.items(), key=itemgetter(1))

        if len(two_flights):
            print("Το μικρότερο ταξίδι με 2 πτητικά σκέλη είναι το", two_flights[0][0],"διάρκειας", two_flights[0][1],"ωρών." )
            print("Το μεγαλύτερο ταξίδι με 2 πτητικά σκέλη είναι το", two_flights[len(two_flights)-1][0],"διάρκειας", two_flights[len(two_flights)-1][1],"ωρών." )
        if len(three_flights):
            print("Το μικρότερο ταξίδι με 3 πτητικά σκέλη είναι το", three_flights[0][0],"διάρκειας", three_flights[0][1],"ωρών." )
            print("Το μεγαλύτερο ταξίδι με 3 πτητικά σκέλη είναι το", three_flights[len(three_flights)-1][0],"διάρκειας", three_flights[len(three_flights)-1][1],"ωρών." )
        if len(four_flights):
            print("Το μικρότερο ταξίδι με 4 πτητικά σκέλη είναι το", four_flights[0][0],"διάρκειας", four_flights[0][1],"ωρών." )
            print("Το μεγαλύτερο ταξίδι με 4 πτητικά σκέλη είναι το", four_flights[len(four_flights)-1][0],"διάρκειας", four_flights[len(four_flights)-1][1],"ωρών." )
        if len(five_flights):
            print("Το μικρότερο ταξίδι με 5 πτητικά σκέλη είναι το", five_flights[0][0],"διάρκειας", five_flights[0][1],"ωρών." )
            print("Το μεγαλύτερο ταξίδι με 5 πτητικά σκέλη είναι το", five_flights[len(five_flights)-1][0],"διάρκειας", five_flights[len(five_flights)-1][1],"ωρών." )
        if len(six_flights):
            print("Το μικρότερο ταξίδι με 6 πτητικά σκέλη είναι το", six_flights[0][0],"διάρκειας", six_flights[0][1],"ωρών." )
            print("Το μεγαλύτερο ταξίδι με 6 πτητικά σκέλη είναι το", six_flights[len(six_flights)-1][0],"διάρκειας", six_flights[len(six_flights)-1][1],"ωρών." )
        if len(seven_flights):
            print("Το μικρότερο ταξίδι με 7 πτητικά σκέλη είναι το", seven_flights[0][0],"διάρκειας", seven_flights[0][1],"ωρών." )
            print("Το μεγαλύτερο ταξίδι με 7 πτητικά σκέλη είναι το", seven_flights[len(seven_flights)-1][0],"διάρκειας", seven_flights[len(seven_flights)-1][1],"ωρών." )
        if len(eight_flights):
            print("Το μικρότερο ταξίδι με 8 πτητικά σκέλη είναι το", eight_flights[0][0],"διάρκειας", eight_flights[0][1],"ωρών." )
            print("Το μεγαλύτερο ταξίδι με 8 πτητικά σκέλη είναι το", eight_flights[len(eight_flights)-1][0],"διάρκειας", eight_flights[len(eight_flights)-1][1],"ωρών." )
    else:
        print("Δεν υπάρχει αυτή η επιλογή! Ξαναπροσπαθήστε...")
        sys.exit()

    return

if __name__ == '__main__':
    mat_csr = makeMatrix()
    presentation()


import turtle as t
import time

colors = ["red", "orange", "yellow", "green", "cyan", "blue", "purple"]
r = 20

for i in range(1, 7):
    t.penup()
    t.sety(-15*i)
    t.pendown()
    t.pencolor(colors[i-1])
    t.circle(15*i)
else:
    t.hideturtle()

time.sleep(4)


import retro

def main(game= 'StreetFighterIISpecialChampionEdition-Genesis',  state= "chunli", record= 'test'):
    env = retro.make(game= game, state= state)
    obs = env.reset()
    done = False
    while not done:
        action = env.action_space.sample()
        obs, rew, done, info = env.step(action)
        env.render()
    env.close()
    env.viewer.close()

if __name__ == "__main__":
    main()


from src.dataset import DataSet, DataLoadException
from src.cli_client import CLIClient

from typing import List
from click.exceptions import Abort

def main():
    # create DataSet objects from JSON files
    tickets_data_set = DataSet("Tickets", json_path="data/tickets.json")
    users_data_set = DataSet("Users", json_path="data/users.json")

    # relate DataSets to each other on "assignee_id"
    tickets_data_set.relate_dataset(users_data_set, "assignee_id", "_id")
    users_data_set.relate_dataset(tickets_data_set, "_id", "assignee_id")

    # initialise client for dealing with CLI interactions
    client = CLIClient([tickets_data_set, users_data_set])

    # clear the console before starting
    client.flush_screen()

    response = client.format_question(
        "Welcome to Zendesk Search!\n"
        "\tOptions:\n"
        "\t\t- 1 to search Zendesk\n"
        "\t\t- 2 to view a list of searchable fields\n"
        "\t\t (press ctrl + c to exit at any time)\n",
        options=["1", "2"],
    )

    if response == "1":
        data_set_names = [data_set.name for data_set in client.data_sets]

        # Get user to select from list of loaded datasets
        selected_data_set = client.data_set_name_mapping[
            client.format_question("Please select data set", options=data_set_names)
        ]

        # Get user to select from list of selected dataset's fields
        selected_search_field = client.format_question(
            "Please select search field", options=selected_data_set.fields
        )

        client.flush_screen()

        # Get user to select if they would like to enter a value, or search for null values
        search_for_none = (
            client.format_question(
                f"Searching {selected_data_set.name} on the {selected_search_field} field\n"
                "\tOptions:\n"
                "\t\t- 1 to enter a search term\n"
                "\t\t- 2 to search for null values\n",
                options=["1", "2"],
            )
            == "2"
        )

        if search_for_none:
            subset = selected_data_set.filter_by_value(selected_search_field, None)

        else:  # i.e., search for a value
            # get the data type of the field, and then ask the user to enter the data type
            field_type = selected_data_set.field_type_mapping[selected_search_field]

            if field_type == str:
                search_text = client.format_question(
                    "Please enter search text", response_type=str
                )
                subset = selected_data_set.filter_by_value(
                    selected_search_field, search_text
                )

            elif field_type == bool:
                search_option = client.format_question(
                    "Please enter true/false", response_type=bool
                )

                subset = selected_data_set.filter_by_value(
                    selected_search_field, search_option
                )

            elif field_type in (int, float):
                search_option = client.format_question(
                    "Please enter number", response_type=field_type
                )
                subset = selected_data_set.filter_by_value(
                    selected_search_field, search_option
                )

            elif field_type == list:
                contained_data_type = selected_data_set.list_field_type_mapping[
                    selected_search_field
                ]
                search_option = client.format_question(
                    f"Please enter {contained_data_type}",
                    response_type=contained_data_type,
                )
                subset = selected_data_set.filter_by_value(
                    selected_search_field, search_option
                )
        client.format_data(subset)

    elif response == "2":
        message = f"\n".join(
            [
                f"{data_set.name} search fields:\n\t" + "\n\t".join(data_set.fields)
                for data_set in client.data_sets
            ]
        )

        client.user_info(message)

if __name__ == "__main__":
    try:
        main()
    except Abort:
        # a KeyboardInterrupt during a click session will be reraised as an Abort
        # catch it here and throw it away, so the user can quit with a keyboard interrupt
        # and not have their stdout polluted by a bogus stacktrace
        pass


import os
from collections import Mapping
from configure import Configuration

def load_configuration(environment, config_name, config_dir):
    """

    :param environment: Environment name
    :type environment: str
    :param config_name: Config file prefix
    :type config_name: str
    :param config_dir: Config directory
    :type config_dir: str
    :return: Configuration
    """
    config_filename = '{}_{}.yaml'.format(config_name, environment)
    config_filename = os.path.join(config_dir, config_filename)

    if not os.path.isfile(config_filename):
        config_filename = '{}.yaml'.format(config_name)
        config_filename = os.path.join(config_dir, config_filename)

    config = Configuration.from_file(config_filename)
    config.configure()

    def to_dict(mapping):
        return {k: (to_dict(v) if isinstance(v, Mapping) else v) for k, v in mapping.items()}

    return to_dict(config)


import pickle
import torch
import numpy as np
import os,sys
import argparse
import math

from collections import Counter,defaultdict

def parse_args():
    parser = argparse.ArgumentParser()

    parser.add_argument('--ifile', required=True, type=str)

    parser.add_argument('--ofile', required=True, type=str, help='path to the output file ')
    parser.add_argument('--ufile', required=False,default=None, type=str, help='path to the unique file ')

    parser.add_argument('--num_samples', default=10000, type=int,
                        help='num samples', required=True)

    args = parser.parse_args(sys.argv[1:])
    return args

#args.ifile = '~/nlm/data/sudoku_data/ambiguous_data/sudoku_9_train_e_bulk.pkl'
def run(args):
    np.random.seed(42)
    ifile = args.ifile
    dat = pickle.load(open(ifile,'rb'))
    for t in dat:
        t['givens']  = (t['query'] > 0).sum()
    #
    givens2ind = defaultdict(list)
    for i,x in enumerate(dat):
        if x['count'] > 1:
            givens2ind[x['givens']].append(i)
    #
    samples = defaultdict(list)
    num_samples = int(math.ceil((1.0*args.num_samples)/len(givens2ind)))
    for k in givens2ind:
        if len(givens2ind[k]) < num_samples:
            print('{}: sampling only {} instead of {}'.format(k,len(givens2ind[k]),num_samples))
        #
        samples[k] = np.random.choice(givens2ind[k],min(len(givens2ind[k]),num_samples),replace=False)
    #
    samples_ind = []
    for k in samples:
        samples_ind.extend(samples[k])
    #
    sampled = list(np.array(dat)[samples_ind])

    if args.ufile is not None and os.path.exists(args.ufile):
        unique = pickle.load(open(args.ufile,'rb'))
        unique = [x for x in unique if x['count'] == 1]
        for t in unique:
            t['givens']  = (t['query'] > 0).sum()
        #
        sampled.extend(unique)
    elif args.ufile is not None:
        print('ufile doesnt exist')
        raise

    print('count counter:',Counter([x['count'] for x in sampled]))
    print('givens counter:',Counter([x['givens'] for x in sampled]))

    pickle.dump(sampled,open(args.ofile,'wb'))
    return sampled

if __name__ == '__main__':
    args = parse_args()
    run(args)


import cv2
import numpy as np
import os

def nothing(x):
    pass

# Get current dir
base_path = os.getcwd()

# Gives width and height of frame
width = 640
height = 480

# Window name
window_name = 'img'

# Create a window
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)

# cv2.resizeWindow(window_name, width, height)

# Create trackbars for color change
# Hue is from 0-179 for Opencv
cv2.createTrackbar('HMin', window_name, 0, 179, nothing)
cv2.createTrackbar('SMin', window_name, 0, 255, nothing)
cv2.createTrackbar('VMin', window_name, 0, 255, nothing)
cv2.createTrackbar('HMax', window_name, 0, 179, nothing)
cv2.createTrackbar('SMax', window_name, 0, 255, nothing)
cv2.createTrackbar('VMax', window_name, 0, 255, nothing)

# Set default value for Max HSV trackbars
cv2.setTrackbarPos('HMax', window_name, 179)
cv2.setTrackbarPos('SMax', window_name, 255)
cv2.setTrackbarPos('VMax', window_name, 255)

# Initialize HSV min/max values
hMin = sMin = vMin = hMax = sMax = vMax = 0
phMin = psMin = pvMin = phMax = psMax = pvMax = 0

for my_path, my_subdirs, my_files in os.walk(base_path):
    print(my_path)
    for my_file in my_files:
        if(my_file.endswith(".jpg")):

            # Concatenate strings so we can access images in subdirs
            image = cv2.imread(my_path + "/" + my_file)

            while(1):
                # Get current positions of all trackbars
                hMin = cv2.getTrackbarPos('HMin', window_name)
                sMin = cv2.getTrackbarPos('SMin', window_name)
                vMin = cv2.getTrackbarPos('VMin', window_name)
                hMax = cv2.getTrackbarPos('HMax', window_name)
                sMax = cv2.getTrackbarPos('SMax', window_name)
                vMax = cv2.getTrackbarPos('VMax', window_name)

                # Set minimum and maximum HSV values to display
                lower = np.array([hMin, sMin, vMin])
                upper = np.array([hMax, sMax, vMax])

                # Convert to HSV format and color threshold
                hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
                mask = cv2.inRange(hsv, lower, upper)
                result = cv2.bitwise_and(image, image, mask=mask)

                # Print if there is a change in HSV value
                if((phMin != hMin) | (psMin != sMin) | (pvMin != vMin) | (phMax != hMax) | (psMax != sMax) | (pvMax != vMax) ):
                    print("(hMin = %d , sMin = %d, vMin = %d), (hMax = %d , sMax = %d, vMax = %d)" % (hMin , sMin , vMin, hMax, sMax , vMax))
                    phMin = hMin
                    psMin = sMin
                    pvMin = vMin
                    phMax = hMax
                    psMax = sMax
                    pvMax = vMax

                # Display result image
                cv2.imshow(window_name, result)
                if cv2.waitKey(10) & 0xFF == ord('q'):
                    break
                elif cv2.waitKey(10) & 0xFF == ord('e'): # Right now you've got to hold this down to make it work
                    exit()

cv2.destroyAllWindows()


from functions import normal

def experiment():
    D = 0.1 ** 2
    i = 1
    S2 = 0
    X = normal(0, 1)
    Xi = [X]

    while S2 / i > D or i < 30:
        Xi.append(normal(0, 1))
        Xnew = X + (Xi[i] - X) / (i + 1)
        S2 = (1 - 1 / i) * S2 + (i + 1) * (Xnew - X) ** 2
        X = Xnew
        i += 1

    return S2, X, i

if __name__ == "__main__":
    """
    Generar n valores de una variable aleatoria normal estándar de manera tal
    que se cumplan las condiciones: n >= 30 y S/ n < 0.1,
    siendo S la desviación estándar muestral de los n datos generados.
    a) ¿Cuál es el número esperado de datos que deben generarse para cumplir
       las condiciones?
    b) ¿Cuál es el número de datos generados efectivamente?
    c) ¿Cuál es la media muestral de los datos generados?
    d) ¿Cuál es la varianza muestral de los datos generados?
    e) Comente los resultados de los items (c) y (d). ¿Son sorprendentes?
    """
    variance, X, n = experiment()
    # a)
    """
    (1 / 0.01) * E(S**2) < E(N) == 100 * E(S**2) < E(N)
    El numero esperado de datos es mayor que 100 veces la esperanza de la
    varianza muestral
    """

    # b)
    print("Numero de corridas:{}".format(n))

    # c)
    print("Media Muestral: {}".format(X))

    # d)
    print("Varianza muestral: {}".format(variance))

    # e)
    # Los datos son sorprendentes


import random
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.edge.service import Service as EdgeService
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.chrome.service import Service as BraveService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.utils import ChromeType
from selenium import webdriver
from webdriver_manager.opera import OperaDriverManager

def chrometest():
    start = time.process_time()

    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
    driver.get("https://reactle.vercel.app/")
    assert "Reactle" in driver.title
    driver.close()
    end= time.process_time()

    return end-start

def Edgetest():
    start = time.process_time()
    driver = webdriver.Edge(service=EdgeService(EdgeChromiumDriverManager().install()))
    driver.get("https://reactle.vercel.app/")
    assert "Reactle" in driver.title
    driver.close()
    end= time.process_time()

    return end-start

def Operatest():
    start = time.process_time()
    driver = webdriver.Opera(executable_path=OperaDriverManager().install())
    driver.get("https://reactle.vercel.app/")
    assert "Reactle" in driver.title
    driver.close()
    end= time.process_time()

    return end-start

def Firefoxtest():
    start = time.process_time()
    driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))
    driver.get("https://reactle.vercel.app/")
    assert "Reactle" in driver.title
    driver.close()
    end= time.process_time()

    return end-start

def bravetest():
    start = time.process_time()
    driver = webdriver.Chrome(service=BraveService(ChromeDriverManager(chrome_type=ChromeType.BRAVE).install()))
    driver.get("https://reactle.vercel.app/")
    assert "Reactle" in driver.title
    driver.close()
    end= time.process_time()

    return end-start

holder= {'Chrome':0, 'Edge':0,'Firefox':0, 'Brave':0}
holder['Chrome']+=chrometest()
holder['Edge'] +=Edgetest()
#holder['Opera'] +=Operatest()
holder['Firefox'] +=Firefoxtest()
holder['Brave']+= bravetest()
sorted_holder = sorted(holder.items(), key=lambda a:a[1])
print(sorted_holder)