# Dependencies
# SSL certificate verification
# pip libraries : pandas, requests, bs4, yfinance, openpyxl

import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup

# List of S&P500 index's constituents from Wikipedia
# ---------------------------------------------------------------------------------------------------#
# data_frame = pd.read_html("https://en.wikipedia.org/wiki/List_of_S%26P_500_companies")[0]
# Get the ticker symbols from the DataFrame
# tickers = data_frame["Symbol"].tolist()

#                                            Note
# I HAVE COMMENTED OUT THE ABOVE CODE TO FETCH THE SYMBOLS OF THE ASSETS OF THE S&P500 INDEX AUTOMATICALLY
# BECAUSE THERE CAN BE POTENTIAL ISSUES WITH SSL CERTIFICATION ON DIFFERENT MACHINES AND SERVERS
# THESE ISSUES CAN BE A BIT LENGTHY TO DEBUG SO FOR NOW THE TICKERS ARE COPIED MANUALLY
# THE AUTOMATION CAN BE DONE BY GIVING IT SOME TIME
# ---------------------------------------------------------------------------------------------------#

tickers = ['MMM','AOS','ABT','ABBV','ACN','ATVI','ADM','ADBE','ADP','AAP','AES',
'AFL','A','APD','AKAM','ALK','ALB','ARE','ALGN','ALLE','LNT','ALL','GOOGL','GOOG',
'MO','AMZN','AMCR','AMD','AEE','AAL','AEP','AXP','AIG','AMT','AWK','AMP','ABC','AME',
'AMGN','APH','ADI','ANSS','AON','APA','AAPL','AMAT','APTV','ACGL','ANET','AJG','AIZ','T',
'ATO','ADSK','AZO','AVB','AVY','AXON','BKR','BALL','BAC','BBWI','BAX','BDX','WRB','BRK.B',
'BBY','BIO','TECH','BIIB','BLK','BK','BA','BKNG','BWA','BXP','BSX','BMY','AVGO','BR','BRO',
'BF.B','BG','CHRW','CDNS','CZR','CPT','CPB','COF','CAH','KMX','CCL','CARR','CTLT','CAT','CBOE',
'CBRE','CDW','CE','CNC','CNP','CDAY','CF','CRL','SCHW','CHTR','CVX','CMG','CB','CHD','CI','CINF',
'CTAS','CSCO','C','CFG','CLX','CME','CMS','KO','CTSH','CL','CMCSA','CMA','CAG','COP','ED','STZ',
'CEG','COO','CPRT','GLW','CTVA','CSGP','COST','CTRA','CCI','CSX','CMI','CVS','DHI','DHR','DRI',
'DVA','DE','DAL','XRAY','DVN','DXCM','FANG','DLR','DFS','DISH','DIS','DG','DLTR','D','DPZ','DOV',
'DOW','DTE','DUK','DD','DXC','EMN','ETN','EBAY','ECL','EIX','EW','EA','ELV','LLY','EMR','ENPH','ETR',
'EOG','EPAM','EQT','EFX','EQIX','EQR','ESS','EL','ETSY','RE','EVRG','ES','EXC','EXPE','EXPD','EXR',
'XOM','FFIV','FDS','FICO','FAST','FRT','FDX','FITB','FSLR','FE','FIS','FISV','FLT','FMC','F','FTNT',
'FTV','FOXA','FOX','BEN','FCX','GRMN','IT','GEHC','GEN','GNRC','GD','GE','GIS','GM','GPC','GILD','GL',
'GPN','GS','HAL','HIG','HAS','HCA','PEAK','HSIC','HSY','HES','HPE','HLT','HOLX','HD','HON','HRL','HST',
'HWM','HPQ','HUM','HBAN','HII','IBM','IEX','IDXX','ITW','ILMN','INCY','IR','PODD','INTC','ICE','IFF',
'IP','IPG','INTU','ISRG','IVZ','INVH','IQV','IRM','JBHT','JKHY','J','JNJ','JCI','JPM','JNPR','K','KDP',
'KEY','KEYS','KMB','KIM','KMI','KLAC','KHC','KR','LHX','LH','LRCX','LW','LVS','LDOS','LEN','LNC','LIN',
'LYV','LKQ','LMT','L','LOW','LYB','MTB','MRO','MPC','MKTX','MAR','MMC','MLM','MAS','MA','MTCH','MKC',
'MCD','MCK','MDT','MRK','META','MET','MTD','MGM','MCHP','MU','MSFT','MAA','MRNA','MHK','MOH','TAP',
'MDLZ','MPWR','MNST','MCO','MS','MOS','MSI','MSCI','NDAQ','NTAP','NFLX','NWL','NEM','NWSA','NWS','NEE',
'NKE','NI','NDSN','NSC','NTRS','NOC','NCLH','NRG','NUE','NVDA','NVR','NXPI','ORLY','OXY','ODFL','OMC',
'ON','OKE','ORCL','OGN','OTIS','PCAR','PKG','PARA','PH','PAYX','PAYC','PYPL','PNR','PEP','PFE','PCG',
'PM','PSX','PNW','PXD','PNC','POOL','PPG','PPL','PFG','PG','PGR','PLD','PRU','PEG','PTC','PSA','PHM',
'QRVO','PWR','QCOM','DGX','RL','RJF','RTX','O','REG','REGN','RF','RSG','RMD','RVTY','RHI','ROK','ROL',
'ROP','ROST','RCL','SPGI','CRM','SBAC','SLB','STX','SEE','SRE','NOW','SHW','SPG','SWKS','SJM','SNA',
'SEDG','SO','LUV','SWK','SBUX','STT','STLD','STE','SYK','SYF','SNPS','SYY','TMUS','TROW','TTWO','TPR',
'TRGP','TGT','TEL','TDY','TFX','TER','TSLA','TXN','TXT','TMO','TJX','TSCO','TT','TDG','TRV','TRMB','TFC',
'TYL','TSN','USB','UDR','ULTA','UNP','UAL','UPS','URI','UNH','UHS','VLO','VTR','VRSN','VRSK','VZ','VRTX',
'VFC','VTRS','VICI','V','VMC','WAB','WBA','WMT','WBD','WM','WAT','WEC','WFC','WELL','WST','WDC','WRK','WY',
'WHR','WMB','WTW','GWW','WYNN','XEL','XYL','YUM','ZBRA','ZBH','ZION','ZTS']

# Stocks of interest as per the problem statement - apple, amazon, google
target_tickers = ['AAPL', 'AMZN', 'GOOGL']

# Storing the total market capitalization of the entire S&P500 index (^GSPC)
marketCap_gspc = 0 # initialising, will be calculated later

# storing all the required metrics for the stocks of interests in a dictionary
# the elements in the data dictionary can be automated by dedicating a class for it.
# since the required metrics are very few, i'm going with the manual approach here
data = {
    "AAPL" : {
        'marketCap' : None,
        's&pweight' : None,
        'operationMargin' : None,
        'lastClose' : None,
        'ytdPerformance' : None,
        'revenue' : None,
        'netIncome' : None
    },
    "AMZN" : {
        'marketCap' : None,
        's&pweight' : None,
        'operationMargin' : None,
        'lastClose' : None,
        'ytdPerformance' : None,
        'revenue' : None,
        'netIncome' : None
    },
    "GOOGL" : {
        'marketCap' : None,
        's&pweight' : None,
        'operationMargin' : None,
        'lastClose' : None,
        'ytdPerformance' : None,
        'revenue' : None,
        'netIncome' : None
    }
}
# 1. starting off by calculating the net market capitalisation of s&p500 index
for ticker in tickers:
    try:
        marketCap = yf.Ticker(ticker).info['marketCap']
        # this marketCap represents the marketCap of the currently iterated stock
        marketCap_gspc += marketCap
        # updating the marketCaps of the targets to calculate the S&P weights ahead
        if(ticker in target_tickers):
            data[ticker]['marketCap'] = marketCap
    except:
        # we can also print the error received here, for now i'll just skip
        continue

# 2. Doing the Main calculation for all the metrics
for ticker in target_tickers:
    #fetching all the information of our target ticker in a variable(dicitionary) called stock
    stock = yf.Ticker(ticker).info

    # 1 S&P500 weight of the stock
    marketCap = data[ticker]['marketCap']
    weight = ((marketCap)/(marketCap_gspc))*100
    # #updating the weight of this stock
    data[ticker]['s&pweight'] = weight

    # 2 Operating Margin
    operatingMargin = stock['operatingMargins']
    data[ticker]['operatingMargin'] = operatingMargin

    # 3  Last Close Date
    lastClose = stock['previousClose']
    data[ticker]['lastClose'] = lastClose

    # 4

    # 5 Stocks' YTD performance
    previousClose = stock['previousClose']
    currentPrice = stock['currentPrice']
    ytd = ((currentPrice - previousClose)/(previousClose))*100
    data[ticker]['ytdPerformance'] = ytd

    # 6 Revenue
    totalRevenue = stock['totalRevenue']
    fiftyTwoWeekChange = stock['52WeekChange']
    # revenue = ((totalRevenue - fiftyTwoWeekChange)/(fiftyTwoWeekChange))*100
    revenue = totalRevenue
    data[ticker]['revenue'] = revenue

    # 7 Net Income
    netIncomeToCommon = stock['netIncomeToCommon']
    fiftyTwoWeekLow = stock['fiftyTwoWeekLow']
    netIncome = netIncomeToCommon
    data[ticker]['netIncome']=netIncome

    # 8

# Printing all the Stock Data on the screen
for stock in data:
    print(stock)
    print("S&P 500 weight of the stock : %f " % data[stock]['s&pweight'])
    print("Operatin Margin : %f" % data[stock]['operatingMargin'])
    print("Last Closing Price : %f " % data[stock]['lastClose'])
    print("YTD Performance : %f " % data[stock]['ytdPerformance'])
    print("Annual Revenue : %d " % data[stock]['revenue'])
    print("Net Annual Income : %d " % data[stock]['netIncome'])
    print()

# Declare the file where you want the output to be stored here
output_file = "output.xlsx"
# Convert your data to a pandas Data Frame
df = pd.DataFrame(data)
# Write the Data to the output file
df.to_excel(output_file)

print(f"Output Saved to {output_file}")


class Genome(object):
    def __init__(self, name=None, reference=None):
        self.name = name
        self.reference = reference

class GenomeReference(object):
    def __init__(self, path_ref=None):
        self.path_ref = path_ref
        self.fa_refs = Fasta(self.path_ref)
        self._cache = {}

    def lookup(self, site, chrom=None):
        chrom = chrom or site.chrom
        chrom = norm_chrom(chrom, self.fa_refs.keys())
        if chrom not in self._cache:
            self._cache[chrom] = str(self.fa_refs[chrom]).upper()
        seq = self._cache[chrom]
        var_start = site.pos - 1
        var_end = var_start + len(site.ref)
        up = seq[var_start - self.window_size:var_start]
        down = seq[var_end:var_end + self.window_size]
        assert seq[var_start - self.window_size:var_end + self.window_size] == (up + site.ref + down)
        return (up, down)

    __call__ = lookup


from matplotlib import pyplot as plt
import csv
from datetime import datetime

filename = 'sitka_weather_2014.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)
    # 得到文件第一行
    #print(header_row)
    # enumerate 获取每个元素的索引及其值
    #for index,column_header in enumerate(header_row):
    #   print(index,column_header)
    dates,highs,lows = [],[],[]
    for row in reader:
        try:
            current_date = datetime.strptime(row[0],'%Y-%m-%d')
            low = int(row[3])
        except ValueError:
            print(current_date,'missing data')
        else:
            highs.append(int(row[1]))
            dates.append(current_date)
            lows.append(low)

    #print(highs)
    fig = plt.figure(dpi=128,figsize=(10,6))
    # 折线图,scatter:点状图
    # alpha 设置透明度
    plt.plot(dates,highs,c='red',alpha=0.5)
    plt.plot(dates,lows,c='blue',alpha=0.5)
    plt.fill_between(dates,highs,lows,facecolor='blue',alpha=0.3)

    plt.title('Daily high and low temperatures, 2014',fontsize=24)
    plt.xlabel('',fontsize=16)

    # 绘制斜的日期标签，以免重叠
    fig.autofmt_xdate()

    plt.ylabel('Temperature(F)',fontsize=16)
    #plt.tick_params(axis='both',which='major',labelsize=16)

    plt.show()


import time
import os
from tqdm import tqdm
print("Loading things up")
time.sleep(2)
print("Loading sOS.py")
for i in tqdm (range (101),
			desc="Loading bOS.py",
			ascii=False, ncols=75):
	time.sleep(0.1)

print("Complete.")


def _strtobool(val):
    val = val.lower()
    if val in ("y", "yes", "t", "true", "on", "1"):
        return 1
    elif val in ("n", "no", "f", "false", "off", "0"):
        return 0
    else:
        raise valError("invalid truth val %r" % (val,))

def cast_boolean(val):
    if val == False:
        return False
    val = str(val)
    return False if val == "" else bool(_strtobool(val))


'''
Created on 3 maj 2011

@author: Yasin
'''

from PyQt5 import QtCore, QtGui, QtWidgets

from .modal_items import QColorDialog, QFontDialog

class PlotModel(QtCore.QAbstractItemModel):
    '''
    This model was created to allow support of the
    complexity of the multiCanvasItem. it allows a
    treeview approach of the
    '''
    sortRole   = QtCore.Qt.UserRole
    filterRole = QtCore.Qt.UserRole + 1

    """INPUTS: Node, QObject"""
    def __init__(self, root, parent=None, col_count = 2):
        super(PlotModel, self).__init__(parent)
        self._rootNode = root
        self._col_count = col_count
        self.color_picker = QColorDialog()
        self.font_picker = QFontDialog()
        self.referenceModel()

    def referenceModel(self):
        self._rootNode.referenceModel(self)

    def root(self):
        return self._rootNode

    """INPUTS: QModelIndex"""
    """OUTPUT: int"""
    def rowCount(self, parent):
        if not parent.isValid():
            parentNode = self._rootNode
        else:
            parentNode = parent.internalPointer()

        return parentNode.childCount()

    """INPUTS: QModelIndex"""
    """OUTPUT: int"""
    def columnCount(self, parent):
        return self._col_count

    """INPUTS: QModelIndex, int"""
    """OUTPUT: QVariant, strings are cast to QString which is a QVariant"""
    def data(self, index, role):

        if not index.isValid():
            return None

        node = index.internalPointer()

        if role == QtCore.Qt.DisplayRole or role == QtCore.Qt.EditRole:
            return node.data(index.column())

        if role == QtCore.Qt.DecorationRole:
            if index.column() == 0:
                resource = node.resource()
                return QtGui.QIcon(QtGui.QPixmap(resource))

        if role == PlotModel.sortRole:
            return node.typeInfo()

        if role == PlotModel.filterRole:
            return node.typeInfo()

        if role == QtCore.Qt.BackgroundRole:
            if type(node.data(index.column())) == QtGui.QColor:
                return QtGui.QBrush(node.data(index.column()))

        if role == QtCore.Qt.FontRole:
            if type(node.data(index.column())) == QtGui.QFont:
                return node.data(index.column())

    """INPUTS: QModelIndex, QVariant, int (flag)"""
    def setData(self, index, value, role=QtCore.Qt.EditRole):

        if index.isValid():
            node = index.internalPointer()
            if role == QtCore.Qt.EditRole:
                node.setData(index.column(), value)
                self.dataChanged.emit(index, index)
                return True

        return False

    """INPUTS: int, Qt::Orientation, int"""
    """OUTPUT: QVariant, strings are cast to QString which is a QVariant"""
    def headerData(self, section, orientation, role):
        if role == QtCore.Qt.DisplayRole:
            if section == 0:
                return "Properties"
            else:
                return ""

    """INPUTS: QModelIndex"""
    """OUTPUT: int (flag)"""
    def flags(self, index):
        item = self.getNode(index)
        return item.flags(index)

    """INPUTS: QModelIndex"""
    """OUTPUT: QModelIndex"""
    """Should return the parent of the node with the given QModelIndex"""
    def parent(self, index):
        node = self.getNode(index)
        parentNode = node.parent()

        if parentNode == self._rootNode or parentNode == None or parentNode.row() == None:
            return QtCore.QModelIndex()

        return self.createIndex(parentNode.row(), 0, parentNode)

    """INPUTS: int, int, QModelIndex"""
    """OUTPUT: QModelIndex"""
    """Should return a QModelIndex that corresponds to the given row, column and parent node"""
    def index(self, row, column, parent):
        parentNode = self.getNode(parent)
        childItem = parentNode.child(row)
        if childItem:
            return self.createIndex(row, column, childItem)
        else:
            return QtCore.QModelIndex()

    """CUSTOM"""
    """INPUTS: QModelIndex"""
    def getNode(self, index):
        node = index.internalPointer()
        if node:
            return node

        return self._rootNode

    def itemAt(self,index):
        '''
        returns the ite, associated to the model index
        '''
        if not index.parent().internalPointer() is None:
            return index.parent().internalPointer().child(index.row())
        else:
            return None

    """INPUTS: int, int, QModelIndex"""
    def insertRows(self, position, rows, items, parentNode):
        success = False
        self.beginInsertRows(
            parentNode.index(), position,
            position + rows - 1)

        for row in range(rows):
            items[row].referenceModel(self)
            success = parentNode.insertChild(position, items[row])

        self.endInsertRows()
        self.referenceModel()

        return success

    # """INPUTS: int, int, QModelIndex"""
    def removeRows(self, position, rows, parentNode):
        success = False
        self.beginRemoveRows(
            parentNode.index(), position,
            position + rows - 1)

        for row in range(rows):
            success = parentNode.removeChild(position)

        self.endRemoveRows()

        return success


class InsertionSort:

    def __init__(self, data):
        self.data = data
        self

    def sort(self):

        for j in range(1, len(self.data)):
            key = self.data[j]
            i = j - 1

            while i > -1 and self.data[i] > key:
                """
                paste value to index data[i+1] break the loop
                when our index is out of bound or data[i] is
                lower or equal to key
                """
                self.data[i + 1] = self.data[i]
                print(self.data)
                i  = i - 1
            self.data[i + 1] = key

        return self.data

    def recurse(self, val):

        if val == 0:
            return 0
        else:
            decrement = val - 1
            print("->",decrement)
            count = self.recurse(decrement)
            print("<-", count)
            return count

# data = [3,5, 1, 4, 6]
data = [6,5,4,3,2,1]
ins = InsertionSort(data)
# print(ins.sort())

digit = 8
index = 1
holder = []
while digit > 0 :
    digit = digit - 1
    holder.append((index, digit))
    index = index + 1
    print(holder)

# Simple recursion test
# ins.recurse(10)


import dbus.service
import ubuntu_sso

from ubuntu_sso.credentials import (
    HELP_TEXT_KEY,
    PING_URL_KEY,
    TC_URL_KEY,
)
from ubuntuone.platform.credentials import (
    APP_NAME,
    DESCRIPTION,
    logger,
    NO_OP,
    PING_URL,
    TC_URL,
)

TIMEOUT_INTERVAL = 10000  # 10 seconds

# constants
DBUS_BUS_NAME = "com.ubuntuone.Credentials"
DBUS_CREDENTIALS_PATH = "/credentials"
DBUS_CREDENTIALS_IFACE = "com.ubuntuone.CredentialsManagement"

class CredentialsManagement(dbus.service.Object):
    """DBus object that manages Ubuntu One credentials."""

    def __init__(self, timeout_func=lambda *a: None,
                 shutdown_func=lambda *a: None, *args, **kwargs):
        super(CredentialsManagement, self).__init__(*args, **kwargs)
        self._ref_count = 0
        self.timeout_func = timeout_func
        self.shutdown_func = shutdown_func

        self.sso_match = None
        self.sso_proxy = self._get_sso_proxy()

    def _signal_handler(self, *args, **kwargs):
        """Generic signal handler."""
        member = kwargs.get('member', None)
        app_name = args[0] if len(args) > 0 else None
        logger.debug('Handling DBus signal for member: %r, app_name: %r.',
                     member, app_name)

        if app_name != APP_NAME:
            logger.info('Received %r but app_name %r does not match %r, ' \
                        'exiting.', member, app_name, APP_NAME)
            return

        sig = getattr(self, member)

        if member in ('CredentialsFound', 'CredentialsError'):
            # this are the only signals that will forward the parameter
            logger.info('%r', member)
            arg = args[1]
            sig(arg)
        else:
            sig()

    def _get_sso_proxy(self):
        """Get the SSO dbus proxy."""
        bus = dbus.SessionBus()
        # register signal handlers for each kind of error
        self.sso_match = bus.add_signal_receiver(self._signal_handler,
                            member_keyword='member',
                            dbus_interface=ubuntu_sso.DBUS_CREDENTIALS_IFACE)
        try:
            obj = bus.get_object(ubuntu_sso.DBUS_BUS_NAME,
                                 ubuntu_sso.DBUS_CREDENTIALS_PATH,
                                 follow_name_owner_changes=True)
            proxy = dbus.Interface(obj, ubuntu_sso.DBUS_CREDENTIALS_IFACE)
        except:
            logger.exception('get_sso_proxy:')
            raise

        return proxy

    def _get_ref_count(self):
        """Get value of ref_count."""
        return self._ref_count

    def _set_ref_count(self, new_value):
        """Set a new value to ref_count."""
        logger.debug('ref_count is %r, changing value to %r.',
                     self._ref_count, new_value)
        if new_value < 0:
            self._ref_count = 0
            msg = 'Attempting to decrease ref_count to a negative value (%r).'
            logger.warning(msg, new_value)
        else:
            self._ref_count = new_value

        if self._ref_count == 0:
            logger.debug('Setting up timer with %r (%r, %r).',
                         self.timeout_func, TIMEOUT_INTERVAL, self.shutdown)
            self.timeout_func(TIMEOUT_INTERVAL, self.shutdown)

    ref_count = property(fget=_get_ref_count, fset=_set_ref_count)

    def shutdown(self):
        """If no ongoing requests, call self.shutdown_func."""
        logger.debug('shutdown!, ref_count is %r.', self._ref_count)
        if self._ref_count == 0:
            logger.info('Shutting down, calling %r.', self.shutdown_func)
            self.shutdown_func()

    # Operator not preceded by a space (fails with dbus decorators)
    # pylint: disable=C0322

    @dbus.service.signal(DBUS_CREDENTIALS_IFACE)
    def AuthorizationDenied(self):
        """Signal thrown when the user denies the authorization."""
        self.ref_count -= 1
        logger.info('%s: emitting AuthorizationDenied.',
                    self.__class__.__name__)

    @dbus.service.signal(DBUS_CREDENTIALS_IFACE, signature='a{ss}')
    def CredentialsFound(self, credentials):
        """Signal thrown when the credentials are found."""
        self.ref_count -= 1
        logger.info('%s: emitting CredentialsFound.',
                    self.__class__.__name__)

    @dbus.service.signal(DBUS_CREDENTIALS_IFACE)
    def CredentialsNotFound(self):
        """Signal thrown when the credentials are not found."""
        self.ref_count -= 1
        logger.info('%s: emitting CredentialsNotFound.',
                    self.__class__.__name__)

    @dbus.service.signal(DBUS_CREDENTIALS_IFACE)
    def CredentialsCleared(self):
        """Signal thrown when the credentials were cleared."""
        self.ref_count -= 1
        logger.info('%s: emitting CredentialsCleared.',
                    self.__class__.__name__)

    @dbus.service.signal(DBUS_CREDENTIALS_IFACE)
    def CredentialsStored(self):
        """Signal thrown when the credentials were cleared."""
        self.ref_count -= 1
        logger.info('%s: emitting CredentialsStored.',
                    self.__class__.__name__)

    @dbus.service.signal(DBUS_CREDENTIALS_IFACE, signature='a{ss}')
    def CredentialsError(self, error_dict):
        """Signal thrown when there is a problem getting the credentials."""
        self.ref_count -= 1
        logger.error('%s: emitting CredentialsError with error_dict %r.',
                     self.__class__.__name__, error_dict)

    @dbus.service.method(dbus_interface=DBUS_CREDENTIALS_IFACE,
                         async_callbacks=("reply_handler", "error_handler"))
    def find_credentials(self, reply_handler=NO_OP, error_handler=NO_OP):
        """Ask the Ubuntu One credentials."""
        self.ref_count += 1
        self.sso_proxy.find_credentials(APP_NAME, {},
            reply_handler=reply_handler, error_handler=error_handler)

    @dbus.service.method(dbus_interface=DBUS_CREDENTIALS_IFACE,
                         async_callbacks=("reply_handler", "error_handler"))
    def clear_credentials(self, reply_handler=NO_OP, error_handler=NO_OP):
        """Clear the Ubuntu One credentials."""
        self.ref_count += 1
        self.sso_proxy.clear_credentials(APP_NAME, {},
            reply_handler=reply_handler, error_handler=error_handler)

    @dbus.service.method(dbus_interface=DBUS_CREDENTIALS_IFACE,
                         in_signature='a{ss}',
                         async_callbacks=("reply_handler", "error_handler"))
    def store_credentials(self, credentials,
                          reply_handler=NO_OP, error_handler=NO_OP):
        """Store the token for Ubuntu One application."""
        self.ref_count += 1
        self.sso_proxy.store_credentials(APP_NAME, credentials,
            reply_handler=reply_handler, error_handler=error_handler)

    @dbus.service.method(dbus_interface=DBUS_CREDENTIALS_IFACE,
                         in_signature='a{ss}',
                         async_callbacks=("reply_handler", "error_handler"))
    def register(self, args, reply_handler=NO_OP, error_handler=NO_OP):
        """Get credentials if found else prompt to register to Ubuntu One."""
        self.ref_count += 1
        params = {HELP_TEXT_KEY: DESCRIPTION, TC_URL_KEY: TC_URL,
                  PING_URL_KEY: PING_URL}
        params.update(args)
        self.sso_proxy.register(APP_NAME, params,
            reply_handler=reply_handler, error_handler=error_handler)

    @dbus.service.method(dbus_interface=DBUS_CREDENTIALS_IFACE,
                         in_signature='a{ss}',
                         async_callbacks=("reply_handler", "error_handler"))
    def login(self, args, reply_handler=NO_OP, error_handler=NO_OP):
        """Get credentials if found else prompt to login to Ubuntu One."""
        self.ref_count += 1
        params = {HELP_TEXT_KEY: DESCRIPTION, TC_URL_KEY: TC_URL,
                  PING_URL_KEY: PING_URL}
        params.update(args)
        self.sso_proxy.login(APP_NAME, params,
            reply_handler=reply_handler, error_handler=error_handler)

    @dbus.service.method(dbus_interface=DBUS_CREDENTIALS_IFACE,
                         in_signature='a{ss}',
                         async_callbacks=("reply_handler", "error_handler"))
    def login_email_password(self, args, reply_handler=NO_OP, error_handler=NO_OP):
        """Get credentials if found else prompt to login to Ubuntu One."""
        self.ref_count += 1
        params = {PING_URL_KEY: PING_URL}
        params.update(args)
        self.sso_proxy.login_email_password(APP_NAME, params,
            reply_handler=reply_handler, error_handler=error_handler)

def get_creds_proxy():
    """Get the CredentialsManagement proxy."""
    bus = dbus.SessionBus()
    try:
        obj = bus.get_object(DBUS_BUS_NAME,
                             DBUS_CREDENTIALS_PATH,
                             follow_name_owner_changes=True)
        proxy = dbus.Interface(obj, DBUS_CREDENTIALS_IFACE)
    except:
        logger.exception('get_creds_proxy:')
        raise

    return proxy


#!/usr/bin/env python3

# A tool to convert any data files using ParaView
#
# Ivan Markin, @unkaktus 2023

import paraview.simple as pv
import argparse
import os

parser = argparse.ArgumentParser('pvconvert')
parser.add_argument("-i", type=str, help="Input filename")
parser.add_argument("-o", type=str, help="Output filename")

args = parser.parse_args()

reader = pv.OpenDataFile(args.i)
writer = pv.CreateWriter(args.o, reader)
writer.WriteAllTimeSteps = 1
writer.UpdatePipeline()


import random

def is_right_price(number: int, price: int):
    if number == price:
        print("You win ! You have guessed the right number.")
        return True
    elif number > price:
        print(f"Wrong, the price is less than {number}")
        return False
    else:
        print(f"Wrong, the price is more than {number}")
        return False

def game_round(range_min: int, range_max: int, max_attempts: int):
    print('-----------------')
    attempts = 1
    price = random.randint(range_min, range_max)  # Generate a random integer between the specified range
    print(f"You have {max_attempts} attempts to try to guess the a number between {range_min} and {range_max}.")
    while True:
        try:
            number = int(input(f"({attempts}/{max_attempts}) Please input a number and press enter: "))
        except Exception as e:
            print("Wrong input. Are you sure you typed a number ?")
            continue

        if is_right_price(number, price):
            break

        attempts += 1
        if attempts > max_attempts:
            print("You lose: maximum number of attempts exceeded.")
            break
    print('-----------------')

def run_game(round_configuration: dict):
    player_want_to_play = True
    while player_want_to_play:
        game_round(**round_configuration)
        replay = input('Want to play another round ? Input "r" and then enter. Else, just press enter.')
        if replay != "r":
            player_want_to_play = False


import flask

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = '192.168.0.28'
port = 8000

s.connect((host, port))

# 서버로부터 수신
rbuff = s.recv(1024)  # 메시지 수신
received = str(rbuff, encoding='utf-8')
print('수신 : {0}'.format(received))

s.close()


#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
https://pytorch.org/tutorials/beginner/translation_transformer.html
"""
import argparse
from dataclasses import asdict, dataclass, field, fields
import json
import math
import os
from pathlib import Path
import pickle
import platform
import random
from typing import Any, Callable, Dict, Iterable, List, Union

import numpy as np
import torch
import torch.nn as nn
from torch.nn import Transformer
from torch.nn.utils.rnn import pad_sequence
from torch.optim.lr_scheduler import StepLR
from torch.utils.data.dataloader import DataLoader
from torchtext.data.utils import get_tokenizer
from torchtext.datasets import multi30k, Multi30k
from torchtext.vocab import build_vocab_from_iterator, Vocab
from torchtext._torchtext import Vocab as VocabPybind
from tqdm import tqdm

from toolbox.torch.utils.data.vocabulary import Vocabulary

def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--multi30k_train_url",
        default="https://raw.githubusercontent.com/neychev/small_DL_repo/master/datasets/Multi30k/training.tar.gz",
        type=str
    )
    parser.add_argument(
        "--multi30k_valid_url",
        default="https://raw.githubusercontent.com/neychev/small_DL_repo/master/datasets/Multi30k/validation.tar.gz",
        type=str
    )
    parser.add_argument("--src_language", default="de", type=str)
    parser.add_argument("--tgt_language", default="en", type=str)

    parser.add_argument("--src_tokenizer_name", default="spacy", type=str)
    parser.add_argument("--src_tokenizer_language", default="de_core_news_sm", type=str)
    parser.add_argument("--tgt_tokenizer_name", default="spacy", type=str)
    parser.add_argument("--tgt_tokenizer_language", default="en_core_web_sm", type=str)

    parser.add_argument("--vocabulary_dir", default="./vocabulary", type=str)

    parser.add_argument("--learning_rate", default=2e-5, type=float)
    parser.add_argument("--num_train_epochs", default=200, type=int)
    parser.add_argument("--batch_size", default=128, type=int)
    parser.add_argument("--keep_most_recent_by_count", default=10, type=int)
    parser.add_argument("--patience", default=-1, type=int)
    parser.add_argument("--serialization_dir", default="serialization_dir", type=str)

    parser.add_argument("--lr_scheduler_step_size", default=500000, type=int)
    parser.add_argument("--lr_scheduler_gamma", default=1.0, type=float)

    parser.add_argument("--seed", default=3407, type=str, help="https://arxiv.org/abs/2109.08203")
    parser.add_argument("--device", default="cuda" if torch.cuda.is_available() else "cpu", type=str)
    parser.add_argument("--device_id", default=0, type=int)

    parser.add_argument("--rank", default=0, type=int)
    parser.add_argument("--world_size", default=1, type=int)
    parser.add_argument("--ddp_backend", default="gloo", type=str)
    parser.add_argument("--ddp_timeout", default=1800, type=int)

    args = parser.parse_args()
    return args

class PositionalEncoding(nn.Module):
    def __init__(self,
                 emb_size: int,
                 dropout: float,
                 maxlen: int = 5000):
        super(PositionalEncoding, self).__init__()
        den = torch.exp(- torch.arange(0, emb_size, 2) * math.log(10000) / emb_size)
        pos = torch.arange(0, maxlen).reshape(maxlen, 1)
        pos_embedding = torch.zeros((maxlen, emb_size))
        pos_embedding[:, 0::2] = torch.sin(pos * den)
        pos_embedding[:, 1::2] = torch.cos(pos * den)
        pos_embedding = pos_embedding.unsqueeze(-2)

        self.dropout = nn.Dropout(dropout)
        self.register_buffer('pos_embedding', pos_embedding)

    def forward(self, token_embedding: torch.Tensor):
        return self.dropout(token_embedding + self.pos_embedding[:token_embedding.size(0), :])

class TokenEmbedding(nn.Module):
    def __init__(self, vocab_size: int, emb_size):
        super(TokenEmbedding, self).__init__()
        self.embedding = nn.Embedding(vocab_size, emb_size)
        self.emb_size = emb_size

    def forward(self, tokens: torch.Tensor):
        return self.embedding(tokens.long()) * math.sqrt(self.emb_size)

class Seq2SeqTransformer(nn.Module):
    def __init__(self,
                 num_encoder_layers: int,
                 num_decoder_layers: int,
                 emb_size: int,
                 num_head: int,
                 src_vocab_size: int,
                 tgt_vocab_size: int,
                 dim_feedforward: int = 512,
                 dropout: float = 0.1):
        super(Seq2SeqTransformer, self).__init__()
        self.transformer = Transformer(d_model=emb_size,
                                       nhead=num_head,
                                       num_encoder_layers=num_encoder_layers,
                                       num_decoder_layers=num_decoder_layers,
                                       dim_feedforward=dim_feedforward,
                                       dropout=dropout)
        self.generator = nn.Linear(emb_size, tgt_vocab_size)
        self.src_tok_emb = TokenEmbedding(src_vocab_size, emb_size)
        self.tgt_tok_emb = TokenEmbedding(tgt_vocab_size, emb_size)
        self.positional_encoding = PositionalEncoding(
            emb_size, dropout=dropout)

    def forward(self,
                src: torch.Tensor,
                trg: torch.Tensor,
                src_mask: torch.Tensor,
                tgt_mask: torch.Tensor,
                src_padding_mask: torch.Tensor,
                tgt_padding_mask: torch.Tensor,
                memory_key_padding_mask: torch.Tensor):
        """
        :param src: shape=[src_seq_len, batch_size].
        :param trg: shape=[trg_seq_len, batch_size].
        :param src_mask:
        :param tgt_mask:
        :param src_padding_mask:
        :param tgt_padding_mask:
        :param memory_key_padding_mask:
        :return: logits, shape=[batch_size, tgt_vocab_size, tgt_out_seq_len]
        """
        src_emb = self.positional_encoding(self.src_tok_emb(src))
        tgt_emb = self.positional_encoding(self.tgt_tok_emb(trg))
        outs = self.transformer(
            src_emb, tgt_emb, src_mask, tgt_mask, None,
            src_padding_mask, tgt_padding_mask, memory_key_padding_mask
        )
        logits: torch.Tensor = self.generator(outs)
        return logits

    def encode(self, src: torch.Tensor, src_mask: torch.Tensor) -> torch.Tensor:
        memory = self.transformer.encoder(
            self.positional_encoding(
                self.src_tok_emb(src)
            ),
            src_mask
        )
        return memory

    def decode(self, tgt: torch.Tensor, memory: torch.Tensor, tgt_mask: torch.Tensor) -> torch.Tensor:
        tgt_out = self.transformer.decoder(
            self.positional_encoding(
                self.tgt_tok_emb(tgt)
            ),
            memory,
            tgt_mask
        )
        return tgt_out

class CollateFunction(object):
    def __init__(self,
                 src_tokenizer,
                 tgt_tokenizer,
                 src_vocab: Vocab,
                 tgt_vocab: Vocab,
                 bos_idx: int,
                 eos_idx: int,
                 pad_idx: int,
                 ):
        self.src_tokenizer = src_tokenizer
        self.tgt_tokenizer = tgt_tokenizer
        self.src_vocab = src_vocab
        self.tgt_vocab = tgt_vocab
        self.bos_idx = bos_idx
        self.eos_idx = eos_idx
        self.pad_idx = pad_idx

    @staticmethod
    def make_mask(
        src: torch.Tensor,
        tgt_input: torch.Tensor,
        pad_idx: int,
    ):
        src_seq_len = src.shape[0]
        tgt_seq_len = tgt_input.shape[0]

        mask = torch.triu(torch.ones(size=(tgt_seq_len, tgt_seq_len)) == 1).transpose(0, 1)
        tgt_mask = mask.float().masked_fill(mask == 0, float("-inf")).masked_fill(mask == 1, float(0.0))

        src_mask = torch.zeros((src_seq_len, src_seq_len)).type(torch.bool)

        src_padding_mask = (src == pad_idx).transpose(0, 1)
        tgt_padding_mask = (tgt_input == pad_idx).transpose(0, 1)
        return src_mask, tgt_mask, src_padding_mask, tgt_padding_mask

    def __call__(self, batch: List[tuple]):
        src_batch, tgt_batch = [], []
        for src_sample, tgt_sample in batch:
            src_tokens = self.src_tokenizer(src_sample)
            src_token_ids = self.src_vocab(src_tokens)
            src_input_ids = torch.cat(
                tensors=(
                    torch.tensor([self.bos_idx]),
                    torch.tensor(src_token_ids),
                    torch.tensor([self.eos_idx])
                )
            )

            tgt_tokens = self.tgt_tokenizer(tgt_sample)
            tgt_token_ids = self.tgt_vocab(tgt_tokens)
            tgt_input_ids = torch.cat(
                tensors=(
                    torch.tensor([self.bos_idx]),
                    torch.tensor(tgt_token_ids),
                    torch.tensor([self.eos_idx])
                )
            )

            src_batch.append(src_input_ids)
            tgt_batch.append(tgt_input_ids)

        # [seq_len, batch_size]
        src_batch = pad_sequence(src_batch, padding_value=self.pad_idx)
        tgt_batch = pad_sequence(tgt_batch, padding_value=self.pad_idx)

        tgt_input = tgt_batch[:-1, :]
        tgt_out = tgt_batch[1:, :]

        # mask
        # src_mask.shape = [src_seq_len, batch_size]
        src_mask, tgt_mask, src_padding_mask, tgt_padding_mask = self.make_mask(
            src_batch, tgt_input, pad_idx=self.pad_idx
        )

        inputs = {
            "src": src_batch,
            "trg": tgt_input,
            "src_mask": src_mask,
            "tgt_mask": tgt_mask,
            "src_padding_mask": src_padding_mask,
            "tgt_padding_mask": tgt_padding_mask,
            "memory_key_padding_mask": src_padding_mask,
        }
        return inputs, tgt_out

@dataclass
class TrainingArguments(object):
    serialization_dir: str = field(
        metadata={"help": "The output directory where the model predictions and checkpoints will be written."},
    )
    num_train_epochs: int = field(default=3, metadata={"help": "Total number of training epochs to perform."})
    seed: int = field(default=42, metadata={"help": "Random seed that will be set at the beginning of training."})
    keep_most_recent_by_count: int = field(default=10, metadata={"help": "how many checkpoint to keep."})

@dataclass
class TrainerState(object):
    epoch_idx: int = 0
    global_step: int = 0

    best_epoch: int = -1
    best_validation_loss: float = float("inf")

    training_loss: float = float("inf")
    validation_loss: float = float("inf")

class Trainer(object):
    def __init__(self,
                 model: nn.Module,
                 args: TrainingArguments,
                 train_dataloader: DataLoader,
                 valid_dataloader: DataLoader,
                 loss_fn: Callable,
                 optimizer: torch.optim.Optimizer,
                 lr_scheduler: torch.optim.lr_scheduler.LRScheduler,
                 device=None
                 ):
        self.model = model
        self.args = args
        self.train_dataloader = train_dataloader
        self.valid_dataloader = valid_dataloader

        self.loss_fn = loss_fn
        self.optimizer = optimizer
        self.lr_scheduler = lr_scheduler

        # save checkpoint
        self.model_state_filename_list = list()
        self.training_state_filename_list = list()

        self.serialization_dir = Path(args.serialization_dir)
        self.serialization_dir.mkdir(exist_ok=True)

        self.device = device

        # global params
        self.state: TrainerState = None

    def _prepare_input(self, data: Union[torch.Tensor, Any]) -> Union[torch.Tensor, Any]:
        if isinstance(data, dict):
            return dict({k: self._prepare_input(v) for k, v in data.items()})
        elif isinstance(data, list):
            return list(self._prepare_input(v) for v in data)
        elif isinstance(data, tuple):
            return tuple(self._prepare_input(v) for v in data)
        elif isinstance(data, torch.Tensor):
            return data.to(self.device)
        return data

    def _prepare_inputs(self, inputs: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:
        inputs = self._prepare_input(inputs)
        return inputs

    def train(self):
        self.model.to(self.device)

        self.state = TrainerState()

        for epoch_idx in range(self.args.num_train_epochs):
            self.state.epoch_idx = epoch_idx

            self.training_epoch(epoch_idx)
            self.evaluation_epoch(epoch_idx)
            self._save_checkpoint()
            self._save_metrics()

        return

    def training_epoch(self, epoch_idx: int):
        # train
        training_total_loss: float = 0.0
        training_total_steps: int = 0

        self.model.train()
        progress_bar = tqdm(self.train_dataloader, desc='Epoch={} (train)'.format(epoch_idx), leave=True)
        for step, batch in enumerate(progress_bar):
            inputs, targets = batch
            loss, _ = self.training_step(inputs, targets)

            training_total_loss += loss.item()
            training_total_steps += 1

            # progress_bar
            progress_bar_postfix = {
                "loss": training_total_loss / training_total_steps,
            }
            progress_bar.set_postfix(**progress_bar_postfix)

        training_loss: float = training_total_loss / training_total_steps
        self.state.training_loss = training_loss

    def training_step(self,
                      inputs: Dict[str, torch.Tensor],
                      targets: torch.Tensor
                      ) -> torch.Tensor:
        inputs = self._prepare_inputs(inputs)
        targets = targets.to(self.device)

        logits = self.model.forward(**inputs)

        loss = self.loss_fn(logits.reshape(-1, logits.shape[-1]), targets.reshape(-1))

        loss.backward()

        self.optimizer.step()
        self.optimizer.zero_grad()
        self.lr_scheduler.step()

        return loss, logits

    def evaluation_epoch(self, epoch_idx: int):
        # validation
        validation_total_loss: float = 0.0
        validation_total_steps: int = 0

        self.model.eval()
        progress_bar = tqdm(self.valid_dataloader, desc='Epoch={} (valid)'.format(epoch_idx), leave=True)
        for step, batch in enumerate(progress_bar):
            self.model.train()
            inputs, targets = batch
            loss, _ = self.prediction_step(inputs, targets)

            validation_total_loss += loss.item()
            validation_total_steps += 1

            # progress_bar
            progress_bar_postfix = {
                "loss": validation_total_loss / validation_total_steps,
            }
            progress_bar.set_postfix(**progress_bar_postfix)

        validation_loss: float = validation_total_loss / validation_total_steps
        self.state.validation_loss = validation_loss

    def prediction_step(self,
                        inputs: Dict[str, torch.Tensor],
                        targets: torch.Tensor
                        ) -> torch.Tensor:
        inputs = self._prepare_inputs(inputs)
        targets = targets.to(self.device)

        with torch.no_grad():
            logits = self.model.forward(**inputs)

            loss = self.loss_fn(logits.reshape(-1, logits.shape[-1]), targets.reshape(-1))

        return loss, logits

    def _save_checkpoint(self):
        # keep most recent by count
        if len(self.model_state_filename_list) >= self.args.keep_most_recent_by_count > 0:
            model_state_filename_ = self.model_state_filename_list.pop(0)
            os.remove(model_state_filename_)
            training_state_filename_ = self.training_state_filename_list.pop(0)
            os.remove(training_state_filename_)
        model_state_filename = self.serialization_dir / "model_state_{}.th".format(self.state.epoch_idx)
        training_state_filename = self.serialization_dir / "training_state_{}.th".format(self.state.epoch_idx)
        self.model_state_filename_list.append(model_state_filename.as_posix())
        with open(model_state_filename.as_posix(), "wb") as f:
            torch.save(self.model.state_dict(), f)
        self.training_state_filename_list.append(training_state_filename.as_posix())
        with open(training_state_filename.as_posix(), "wb") as f:
            torch.save(self.optimizer.state_dict(), f)

    def _save_metrics(self):
        # metrics epoch
        metrics = {
            "best_epoch": self.state.best_epoch,
            "epoch": self.state.epoch_idx,

            "training_loss": self.state.training_loss,
            "validation_loss": self.state.validation_loss,

            "best_validation_loss": self.state.best_validation_loss,
        }
        metrics_epoch_filename = self.serialization_dir / "metrics_epoch_{}.json".format(self.state.epoch_idx)
        with open(metrics_epoch_filename, "w", encoding="utf-8") as f:
            json.dump(metrics, f, indent=4, ensure_ascii=False)

def main():
    args = get_args()

    random.seed(args.seed)
    np.random.seed(args.seed)
    torch.manual_seed(args.seed)

    device = torch.device("{}:{}".format(args.device, args.device_id))
    n_gpu = torch.cuda.device_count()

    vocabulary = Vocabulary.from_files(args.vocabulary_dir)

    unk_idx = 0
    pad_idx = 1
    bos_idx = 2
    eos_idx = 3
    special_symbols = ["<unk>", "<pad>", "<bos>", "<eos>"]

    multi30k.URL["train"] = args.multi30k_train_url
    multi30k.URL["valid"] = args.multi30k_valid_url

    src_token_to_index = vocabulary.get_token_to_index_vocabulary(namespace="src_tokens")
    src_token_to_index = list(sorted(src_token_to_index.items(), key=lambda x: x[1], reverse=False))
    src_tokens = [k for k, v in src_token_to_index]
    src_vocab = Vocab(VocabPybind(src_tokens, None))
    src_vocab.set_default_index(unk_idx)

    tgt_token_to_index = vocabulary.get_token_to_index_vocabulary(namespace="tgt_tokens")
    tgt_token_to_index = list(sorted(tgt_token_to_index.items(), key=lambda x: x[1], reverse=False))
    tgt_tokens = [k for k, v in tgt_token_to_index]
    tgt_vocab = Vocab(VocabPybind(tgt_tokens, None))
    tgt_vocab.set_default_index(unk_idx)

    src_tokenizer = get_tokenizer(args.src_tokenizer_name, language=args.src_tokenizer_language)
    tgt_tokenizer = get_tokenizer(args.tgt_tokenizer_name, language=args.tgt_tokenizer_language)

    collate_fn = CollateFunction(
        src_tokenizer=src_tokenizer,
        tgt_tokenizer=tgt_tokenizer,
        src_vocab=src_vocab,
        tgt_vocab=tgt_vocab,
        bos_idx=bos_idx,
        eos_idx=eos_idx,
        pad_idx=pad_idx,
    )

    train_dataset = Multi30k(split="train", language_pair=(args.src_language, args.tgt_language))
    train_dataloader = DataLoader(
        train_dataset,
        batch_size=args.batch_size,
        shuffle=False,
        num_workers=0 if platform.system() == "Windows" else os.cpu_count(),

        collate_fn=collate_fn,
        pin_memory=False,
        drop_last=False
    )
    valid_dataset = Multi30k(split="valid", language_pair=(args.src_language, args.tgt_language))
    valid_dataloader = DataLoader(valid_dataset, batch_size=args.batch_size, collate_fn=collate_fn)

    transformer = Seq2SeqTransformer(
        num_encoder_layers=3,
        num_decoder_layers=3,
        emb_size=512,
        num_head=8,
        src_vocab_size=len(src_vocab),
        tgt_vocab_size=len(tgt_vocab),
        dim_feedforward=512,
    )

    for p in transformer.parameters():
        if p.dim() > 1:
            nn.init.xavier_uniform_(p)

    loss_fn = torch.nn.CrossEntropyLoss(ignore_index=pad_idx)
    optimizer = torch.optim.Adam(
        transformer.parameters(),
        lr=args.learning_rate, betas=(0.9, 0.98), eps=1e-9
    )
    lr_scheduler = StepLR(
        optimizer=optimizer,
        step_size=args.lr_scheduler_step_size,
        gamma=args.lr_scheduler_gamma
    )

    training_args = TrainingArguments(
        serialization_dir=args.serialization_dir,
        num_train_epochs=args.num_train_epochs,
        seed=args.seed,
        keep_most_recent_by_count=args.keep_most_recent_by_count,
    )
    trainer = Trainer(
        model=transformer,
        args=training_args,
        train_dataloader=train_dataloader,
        valid_dataloader=valid_dataloader,
        loss_fn=loss_fn,
        optimizer=optimizer,
        lr_scheduler=lr_scheduler,
        device=device,
    )
    trainer.train()

    return

if __name__ == '__main__':
    main()


from mighty.backends import TaskBackend
from mighty.backends.schedulers.celery import start_task
from celery import uuid

class TaskBackend(TaskBackend):
    def start(self):
        task_id = uuid()
        start_task.apply_async(
            (self.ct, self.pk, self.task, task_id)+self.extra_args,
            self.extra_kwargs,
            task_id=task_id)


import os
import cherrypy
import re
import random
import locale
import pycountry
import datetime
from json import dumps as json_dumps
from cherrypy.process.plugins import SimplePlugin
from cherrypy._cptools import HandlerWrapperTool
from cherrypy._cpdispatch import PageHandler
from jinja2 import Environment, FileSystemLoader, ModuleLoader, StrictUndefined
from jinja2_webpack import Environment as WebpackEnvironment
from jinja2_webpack.filter import WebpackFilter
import opmuse
from opmuse.security import is_granted as _is_granted
from opmuse.pretty import pretty_date as _pretty_date
from opmuse.library import TrackStructureParser, Library, Album, Track, Artist

VISIBLE_WS = "\u2423"

def _max(items):
    return max(items)

def ordinal(num):
    # stolen from http://codegolf.stackexchange.com/a/4712
    k = num % 10
    return "%d%s" % (num, "tsnrhtdd"[(num / 10 % 10 != 1) * (k < 4) * k::4])

def date(value, date_format='%Y-%m-%d'):
    if type(value) is str:
        date = datetime.datetime.fromtimestamp(int(value))
    elif type(value) is int:
        date = datetime.datetime.fromtimestamp(value)
    else:
        date = value

    return date.strftime(date_format)

def get_jinja_env():
    auto_reload = cherrypy.config['opmuse'].get('jinja.auto_reload')

    if auto_reload is None:
        auto_reload = True

    template_path = os.path.join(os.path.dirname(__file__), '..', 'templates')

    if os.path.exists(template_path):
        loader = FileSystemLoader(template_path)
    else:
        loader = ModuleLoader('/usr/share/opmuse/templates/')

    env = Environment(
        loader=loader,
        extensions=['jinja2.ext.loopcontrols', 'jinja2.ext.do', 'jinja2.ext.autoescape'],
        autoescape=guess_autoescape,
        undefined=StrictUndefined,
        auto_reload=auto_reload,
        cache_size=-1
    )

    cache_path = cherrypy.config['opmuse'].get('cache.path')
    webpack_manifest_path = os.path.join(cache_path, 'webpack-manifest.json')

    if os.path.exists(webpack_manifest_path):
        webpack_env = WebpackEnvironment(manifest=webpack_manifest_path, publicRoot='')
        env.filters['webpack'] = WebpackFilter(webpack_env)
    else:
        def webpack_filter(*args):
            raise Exception("webpack-manifest.json is missing, please run `yarn build:dev` and restart opmuse")

        env.filters['webpack'] = webpack_filter

    env.filters['nl2p'] = nl2p
    env.filters['format_seconds'] = format_seconds
    env.filters['format_seconds_alt'] = format_seconds_alt
    env.filters['pretty_date'] = pretty_date
    env.filters['format_number'] = format_number
    env.filters['show_ws'] = show_ws
    env.filters['format_bytes'] = format_bytes
    env.filters['json'] = json
    env.filters['startswith'] = startswith
    env.filters['track_path'] = track_path
    env.filters['round'] = round
    env.filters['pretty_format'] = pretty_format
    env.filters['country'] = country
    env.filters['date'] = date
    env.filters['ordinal'] = ordinal
    env.filters['max'] = _max

    env.globals['pagination_pages'] = pagination_pages
    env.globals['rand_id'] = rand_id
    env.globals['is_granted'] = is_granted
    env.globals['is_track'] = is_track
    env.globals['is_artist'] = is_artist
    env.globals['is_album'] = is_album
    env.globals['render'] = render
    env.globals['opmuse_version'] = opmuse.__version__

    return env

def country(code):
    try:
        data = pycountry.countries.get(alpha_2=code)
        return data.name
    except KeyError:
        return code

# implemented with the help of http://codereview.stackexchange.com/a/15239
def pagination_pages(page, pages, size):
    if pages <= 10:
        return list(range(1, pages + 1))
    else:
        return sorted(list(set(range(1, 2)) |
                      set(range(max(1, page - size + 1), min(page + size, pages + 1))) |
                      set(range(pages, pages + 1))))

def pretty_format(format):
    return Library.pretty_format(format)

def is_track(object):
    return isinstance(object, Track)

def is_artist(object):
    return isinstance(object, Artist)

def is_album(object):
    return isinstance(object, Album)

def is_granted(role):
    return _is_granted([role])

def rand_id():
    return str(random.randrange(1, 99999))

def replace_ws(string):
    match = re.search(r'\S', string)

    if match is not None:
        index = match.start()
        return "%s%s" % (index * VISIBLE_WS, string[index:])
    else:
        return len(string) * VISIBLE_WS

def show_ws(string):
    """
    Helper for replacing trailing whitespace with the unicode visible space
    character
    """
    if string is None or isinstance(string, StrictUndefined) or len(string) == 0:
        return "[MISSING]"

    return replace_ws(replace_ws(string)[::-1])[::-1]

def format_bytes(bytes, precision=2):
    if bytes is None:
        bytes = 0

    bytes = int(bytes)

    suffixes = ['B', 'KB', 'MB', 'GB', 'TB']
    suffixIndex = 0

    while bytes > 1024:
        suffixIndex += 1
        bytes = bytes / 1024.0

    return "%.*f %s" % (precision, bytes, suffixes[suffixIndex])

def track_path(track, artist=None):
    track_structure = TrackStructureParser(track, data_override={'artist': artist})
    path = track_structure.get_path()

    if path is not None:
        return path.decode('utf8', 'replace')
    else:
        return ''

def startswith(value, start):
    return value.startswith(start)

def json(value):
    return json_dumps(value)

def format_number(number):
    if number is None:
        return None

    return locale.format('%d', number, grouping=True)

def pretty_date(date, ago="ago"):
    return _pretty_date(date, ago=ago)

def nl2p(string):
    paragraphs = []

    for line in string.split('\n'):
        if line == "":
            continue

        paragraphs.append("<p>%s</p>" % line)

    return ''.join(paragraphs)

def format_seconds_alt(seconds):
    if seconds is None:
        seconds = 0

    minutes, seconds = divmod(seconds, 60)
    hours, minutes = divmod(minutes, 60)

    if minutes > 0:
        minute_str = "%dm " % minutes
    else:
        minute_str = ""

    if hours > 0:
        hour_str = "%dh " % hours
    else:
        hour_str = ""

    if hours == 0 and minutes == 0 and seconds == 0 or seconds > 0:
        second_str = "%ds" % seconds
    else:
        second_str = ""

    return "%s%s%s" % (hour_str, minute_str, second_str)

def format_seconds(seconds):
    if seconds is None:
        seconds = 0

    minutes, seconds = divmod(seconds, 60)
    hours, minutes = divmod(minutes, 60)

    if hours > 0:
        string = "%02d:" % (hours, )
    else:
        string = ""

    return "%s%02d:%02d" % (string, minutes, seconds)

def guess_autoescape(template_name):
    if template_name is None or '.' not in template_name:
        return False

    ext = template_name.rsplit('.', 1)[1]

    return ext in ('html', )

class JinjaPlugin(SimplePlugin):

    def __init__(self, bus):
        SimplePlugin.__init__(self, bus)
        self.env = None
        self.bus.subscribe("bind_jinja", self.bind_jinja)

    def start(self):
        self.env = get_jinja_env()

    start.priority = 130

    def bind_jinja(self):
        return self.env

    def stop(self):
        self.env = None

def render(path, params=[]):
    # this one fucks with cherrypy.request and stuff, but seeing as this should
    # only be run in a template after all that jazz is done, it should be safe.
    # i guess we'll notice if it doesn't though :/
    func, vpath = cherrypy.request.dispatch.find_handler(path)
    config = cherrypy.request.config

    if func:
        handler = PageHandler(func, *params)
        filename = config['tools.jinja.filename']
        response_dict = handler()
        html = render_template(filename, response_dict)
        return html
    else:
        return ''

def render_template(filename, dictionary):
    template = cherrypy.request.jinja.get_template(filename)

    template.globals['request'] = cherrypy.request
    template.globals['xhr'] = cherrypy.request.headers.get('X-Requested-With') == 'XMLHttpRequest'
    template.globals['current_url'] = cherrypy.url()

    return template.render(dictionary)

class Jinja(HandlerWrapperTool):

    def __init__(self):
        HandlerWrapperTool.__init__(self, self.jinja)

    def callable(self, filename=None, *args, **kwargs):
        HandlerWrapperTool.callable(self)

    def jinja(self, next_handler, *args, **kwargs):
        if cherrypy.request.jinja is None:
            return

        response_dict = next_handler(*args, **kwargs)
        conf = self._merged_args()

        if 'filename' not in conf:
            raise Exception('No template filename specified!')

        html = render_template(conf['filename'], response_dict)

        if 'text/html' == cherrypy.response.headers['Content-Type']:
            cherrypy.response.headers['Content-Type'] = 'text/html; charset=utf8'

        return html.encode('utf8', 'replace')

class JinjaEnvTool(cherrypy.Tool):
    def __init__(self):
        cherrypy.Tool.__init__(self, 'on_start_resource',
                               self.bind_jinja, priority=10)

    def bind_jinja(self):
        binds = cherrypy.engine.publish('bind_jinja')
        cherrypy.request.jinja = binds[0]

class JinjaAuthenticatedTool(cherrypy.Tool):
    def __init__(self):
        cherrypy.Tool.__init__(self, 'before_handler',
                               self.start, priority=20)

    def start(self):
        if cherrypy.request.jinja is None:
            return

        cherrypy.request.jinja.globals['authenticated'] = False
        cherrypy.request.jinja.globals['user'] = None

        if hasattr(cherrypy.request, 'user') and cherrypy.request.user is not None:
            cherrypy.request.jinja.globals['user'] = cherrypy.request.user
            cherrypy.request.jinja.globals['authenticated'] = True


class Solution:
	def singleNumber(self, nums):
         singles = set()
         for num in nums:
            if num not in singles:
                singles.add(num)
            else:
                singles.remove(num)
         return sorted(list(singles))

#{
 # Driver Code Starts
#Initial Template for Python 3

if __name__ == '__main__':
	T=int(input())
	for i in range(T):
		n = int(input())
		v = list(map(int,input().split()))
		ob = Solution();
		ans = ob.singleNumber(v)
		for i in ans:
			print(i, end = " ")
		print()


from salon.salon import *
from salon.equipment import *
from itertools import groupby

class Cupboard(Equipment):
    numb_jewelry = {}

    def __init__(self, types_jewelry):
        super().__init__(types_jewelry)
        Equipment.__approach_time = 0.6
        self.numb_jewelry = self.__sort_jewelry()

    def __sort_jewelry(self):
        numb = []
        for i in range(self.count_jewelry()):
            numb.append(len(self.types_jewelry[i]))
        no_dif_numb = [el for el, _ in groupby(numb)]
        numb_jewelry = {key: [] for key in sorted(no_dif_numb)}
        for i in range(self.count_jewelry()):
            numb_jewelry[numb[i]].append(self.types_jewelry[i])
        return numb_jewelry

    def __generate_jewelry(self):
        keys_jewelry = list(self.numb_jewelry.keys())
        count_jewelry = len(keys_jewelry)
        interval_a = 1
        interval_b = count_jewelry / self.level if count_jewelry / self.level > 1 else 1
        number_random = random.randint(interval_a, interval_b) - 1
        random_list_jewelry = self.numb_jewelry.get(keys_jewelry[number_random])
        return random_list_jewelry[random.randint(1, len(random_list_jewelry)) - 1]

    def play(self, person):
        random_jewelry = self.__generate_jewelry()
        random_concat_word = sorted(list(random_jewelry), key=lambda a: random.random())
        start_play_time = time.time()
        while True:
            if (time.time() - start_play_time) >= person.max_mood_time:
                return False
            try:
                print('Составьте слово из букв:')
                for i in random_concat_word:
                    print(i, end=" ")
                print('\r')
                word = str(input()).replace(' ', '')
                if word == random_jewelry:
                    print('Верно! ', end="")
                    return True
                else:
                    print('Неверно!')
                time.sleep(0.4)
            except ValueError:
                print('Вы ввели не слово')
                time.sleep(0.4)

    def type_and_level(self):
        return 'Шкаф с одеждой ' + str(self.level) + ' уровня'


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
File to produce Gutzwiller coefficients
Needs some installation from
https://github.com/tcompa/BoseHubbardGutzwiller

Credits:
- https://github.com/tcompa/BoseHubbardGutzwiller

Author: Patrick Huembeli
"""
import math
import numpy
# import matplotlib
# matplotlib.use('Agg')
import pylab as plt
from lib_gutzwiller_simulated_annealing import Gutzwiller
from lib_gutzwiller_simulated_annealing import SA_for_gutzwiller

# Physical parameters
z = 4       # number of neighbors
nmax = 20     # cutoff on the occupation number per site
U = 1.0       # on-site interaction coefficient
mu = 0.5     # chemical potential
V = 0.00     # nearest-neighbor interaction coefficient
P = 0.0      # induced-tunneling coefficient

make_2D_plot = False

# Simulated-annealing parameters
beta_min = 0.1
beta_max = 1e4
cooling_rate = 0.025  # 0.05
n_steps_per_T = 1000
quench_to_T_equal_to_0 = True
n_steps_at_T_equal_to_0 = 10000

def produce_states(run, start, end, steps, J):
    mu_list = numpy.linspace(start, end, steps)
    coeff_out = []
    labels = []
    for mu in mu_list:
        G = Gutzwiller(nmax=nmax, U=U, zJ=J, mu=mu, zV=z*V, zP=z*P)
        G = SA_for_gutzwiller(G, beta_min=beta_min, beta_max=beta_max,
                              cooling_rate=cooling_rate, n_steps_per_T=n_steps_per_T,
                              quench_to_T_equal_to_0=quench_to_T_equal_to_0,
                              n_steps_at_T_equal_to_0=n_steps_at_T_equal_to_0)
        coeff = [i for i in G.f_new]
        coeff_out.append(coeff)
        dens = G.compute_density()
        print('mu:', mu, 'density:', dens)
        if mu % 1 == 0:
            if abs(dens - mu) > 10**(-3):
                labels.append([1., 0.])
                print('SF-Phase')
            else:
                labels.append([0., 1.])
                print('Mott-Phase')
        else:
            if abs(dens - math.ceil(mu)) > 10**(-2):
                labels.append([1., 0.])
                print('SF-Phase')
            else:
                labels.append([0., 1.])
                print('Mott-Phase')
    numpy.save('J_'+str(J)+'labels_source'+str(run), labels)
    numpy.save('J_'+str(J)+'coeff_source'+str(run), coeff_out)

J_list = numpy.linspace(0.003, 0.3, 100)
start = 0.03
end = 3.0
steps = 100
run_list = numpy.linspace(1, 100, 100)
for run in run_list:
    for J in J_list:
        produce_states(int(run), start, end, steps, J)

if make_2D_plot:  # make 2D plot for a 50x50 grid
    J_list = numpy.linspace(0.0, 0.3, 51)
    mu_list = numpy.linspace(0.0, 3.00, 51)
    mu_list = mu_list.tolist()
    meas_J = []
    coeff_J = []
    dens_der_J = []
    for J in J_list:
        meas_mu = []
        coeff_mu = []
        density_list = []
        dens_der_mu = []
        for mu in mu_list:
            # Initialize Gutzwiller-class instance
            G = Gutzwiller(nmax=nmax, U=U, zJ=J, mu=mu, zV=z*V, zP=z*P)
            # Perform simulated-annealing optimization
            G = SA_for_gutzwiller(G, beta_min=beta_min, beta_max=beta_max,
                                  cooling_rate=cooling_rate,
                                  n_steps_per_T=n_steps_per_T,
                                  quench_to_T_equal_to_0=quench_to_T_equal_to_0,
                                  n_steps_at_T_equal_to_0=n_steps_at_T_equal_to_0)
            density = G.compute_density()
            density_list.append(density)
            if mu_list.index(mu) == 0:
                dens_derivative = 1.0
            else:
                dens_derivative = density-density_list[-2]
            coeff = [i for i in G.f_new]
            coeff_mu.append(coeff)
            dens_der_mu.append(dens_derivative)
            meas_mu.append([J, mu, G.energy, density, dens_derivative])
        meas_J.append(meas_mu)
        coeff_J.append(coeff_mu)
        dens_der_J.append(dens_der_mu)
    numpy.save('Gutzwiller_find_boundaries_params_2D_plot', meas_J)
    numpy.save('Gutzwiller_find_boundaries_coeffs_2D_plot', coeff_J)
    print('This is the screen with 2D plot')
    plt.clf()
    plt.pcolormesh(numpy.array(dens_der_J))
    plt.savefig('states_plot')


#  Posted from EduTools plugin
# a = '5 8 2 7 8 8 2 4'
c = input().split()
#c = a.split()
b= input()
d = []

for el in range(len(c)):
    if c[el] == b:
        d.append(str(el))

if len(d) > 0:
    print(' '.join(d))
else:
    print('not found')


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
import mglearn

print("""
İbrahim Halil Bayat
Department of Electronics and Communication Engineering
İstanbul Technical University
İstanbul, Turkey
\n---------- Classifying Iris Species ----------
Species: Setosa, Versicolor, Virginica.
""")

data = load_iris()
x_train, x_test, y_train, y_test = train_test_split(data['data'], data['target'], random_state=0)

print("\n--------- Converting NumPy into Pandas ---------------")
df = pd.DataFrame(x_train, columns=data['feature_names'])
grr = pd.plotting.scatter_matrix(df, c=y_train, figsize=(15, 15), marker='o', hist_kwds={'bins': 20}, s=60, alpha=0.8,
                        cmap=mglearn.cm3)
plt.show()


# os自定义包，对os做一些符合自己编程习惯的调整
from ast import Str
import os
import csv
import re
from .zhtools.langconv import Converter

#print('os自定义包，对os做一些符合自己编程习惯的调整')
def osListFilePath(path):
    FilePathList=[]
    for file in os.listdir(path):
        FilePathList.append(os.path.join(path,file))
    return FilePathList

# 获取一个没有重复的文件名
def getNoDupName(dir,name):
    path=os.path.join(dir, name)
    if os.path.exists(path):
        index=name.rfind('.')
        newname=name[:index]+'-1'+name[index:]

        return getNoDupName(dir,newname)
    else:
        return name

# 对一个字符串进行Windows文件名格式处理
def fileNameFormat(name)->Str:
    name=re.sub(r'[/*:<>?"|\\]',"#",name)
    if len(name)>100:
        name=name[:100]
    return name

# 繁体简体转换
def convertHant():
    with open('D:\\CalendarDate\\out.csv','r',encoding='utf-8') as A:
        with open('D:\\CalendarDate\\out1.csv','a',encoding='utf-8') as B:
            line = A.readline()
            while line:
                print(line)
                line=Converter('zh-hans').convert(line)
                B.write(line)
                line = A.readline()

# 写入csv文件
def write2csv(filedir, rowData):
    # write file
    # 打开文件，追加a
    with open(filedir, 'a',newline='',encoding='utf-8') as out:
        # 设定写入模式
        csv_write = csv.writer(out, dialect='excel')
        csv_write.writerow(rowData)

if __name__ == '__main__':
    convertHant()


from deeplake.core.query.autocomplete import autocomplete
import deeplake

def _test_ds():
    ds = deeplake.dataset("mem://x")
    ds.create_tensor("a")
    ds.create_tensor("b")
    ds.create_tensor("c")
    ds.create_tensor("def")
    ds.create_tensor("g/h/i")
    ds.create_tensor("j/k/l")
    return ds

def test_empty_query():
    ds = _test_ds()
    q = ""
    resp = autocomplete(q, ds)
    assert resp["tokens"] == []
    suggestions = [s["string"] for s in resp["suggestions"]]
    assert suggestions == ["a", "b", "c", "def", "g", "j"]
    assert resp["replace"] == ""

def test_tensor_name():
    ds = _test_ds()
    for q in ["a", "b", "c", "def"]:
        resp = autocomplete(q, ds)
        tokens = resp["tokens"]
        assert len(tokens) == 1
        token = tokens[0]
        assert token["string"] == q
        assert token["start"] == 0
        assert token["end"] == len(q)
        assert token["type"] == "TENSOR"
        suggestions = resp["suggestions"]
        assert resp["replace"] == ""
        assert suggestions == [
            {"string": ".contains", "type": "METHOD"},
            {"string": ".max", "type": "PROPERTY"},
            {"string": ".mean", "type": "PROPERTY"},
            {"string": ".min", "type": "PROPERTY"},
            {"string": ".shape", "type": "PROPERTY"},
            {"string": ".size", "type": "PROPERTY"},
            {"string": " ==", "type": "OP"},
            {"string": " >", "type": "OP"},
            {"string": " <", "type": "OP"},
            {"string": " >=", "type": "OP"},
            {"string": " <=", "type": "OP"},
            {"string": " !=", "type": "OP"},
        ]

def test_tensor_name_partial():
    ds = _test_ds()
    for q in ["d", "de"]:
        resp = autocomplete(q, ds)
        tokens = resp["tokens"]
        assert len(tokens) == 1
        token = tokens[0]
        assert token["string"] == q, (q, token)
        assert token["start"] == 0
        assert token["end"] == len(q)
        assert token["type"] == "UNKNOWN"
        assert resp["replace"] == q
        suggestions = resp["suggestions"]
        assert suggestions == [{"string": "def", "type": "TENSOR"}]

def test_group():
    ds = _test_ds()
    q = "g"
    resp = autocomplete(q, ds)
    suggestions = resp["suggestions"]
    assert suggestions == [
        {
            "string": ".h",
            "type": "GROUP",
        }
    ]
    q = "g.h"
    resp = autocomplete(q, ds)
    suggestions = resp["suggestions"]
    assert suggestions == [
        {
            "string": ".i",
            "type": "TENSOR",
        }
    ]

    q = "g.h.i"
    resp = autocomplete(q, ds)
    suggestions = resp["suggestions"]
    assert suggestions == [
        {"string": ".contains", "type": "METHOD"},
        {"string": ".max", "type": "PROPERTY"},
        {"string": ".mean", "type": "PROPERTY"},
        {"string": ".min", "type": "PROPERTY"},
        {"string": ".shape", "type": "PROPERTY"},
        {"string": ".size", "type": "PROPERTY"},
        {"string": " ==", "type": "OP"},
        {"string": " >", "type": "OP"},
        {"string": " <", "type": "OP"},
        {"string": " >=", "type": "OP"},
        {"string": " <=", "type": "OP"},
        {"string": " !=", "type": "OP"},
    ]

    q = "g.h.k"
    resp = autocomplete(q, ds)
    assert resp["suggestions"] == []

def test_keyword():
    ds = _test_ds()
    q = "'x' in "
    resp = autocomplete(q, ds)
    suggestions = [s["string"] for s in resp["suggestions"]]
    assert "a" in suggestions
    assert "b" in suggestions
    assert "c" in suggestions
    assert "def" in suggestions
    assert "g" in suggestions
    assert "j" in suggestions

def test_const():
    ds = _test_ds()
    for q in ["True", "'abcd'", "123"]:
        resp = autocomplete(q, ds)
        assert resp["suggestions"] == []

def test_property():
    ds = _test_ds()
    q = "g.h.i.mean"
    resp = autocomplete(q, ds)
    suggestions = [s["string"] for s in resp["suggestions"]]
    assert " ==" in suggestions

def test_method():
    ds = _test_ds()
    q = "g.h.i.contains"
    resp = autocomplete(q, ds)
    suggestions = [s["string"] for s in resp["suggestions"]]
    assert "(" in suggestions


# Q.
# Given an [] of integer, and a integer target.
# Find the first pair of elements a and b
# that satisfy a + b = target and return their INDEX
# Assume numbers within the [] of integers will not contain duplicates
# Output "Found the numbers at position n and position m.
# (0-based index is fine)
nums = [2, 7, 11, 15]
target = 26
# brk = False
# for i in range(len(nums)):
#     for j in range(len(nums)):
#         if i != j:
#             if nums[i] + nums[j] == target:
#                 print("Found the numbers at position " + str(i) + " and position " + str(j) + ".")
#                 brk = True
#                 break
#     if brk:
#         break

# dict = {}
# for i in range(len(nums)):
#     if (target - nums[i] in dict):
#         print("Found the numbers at position " + str(dict[target - nums[i]]) + " and position " + str(i) + ".")
#     else:
#         dict[nums[i]] = i

# Counts the number of occurrences of the second most frequent element in a list
# USE dict
nums = [1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4]
dict = {}
for i in nums:
    if i in dict:
        dict[i] += 1
    else:
        dict[i] = 1

max = 0
second_max = 0
for (key, value) in dict.items():
    if value > max:
        second_max = max
        max = value
    elif value > second_max:
        second_max = value
print("The count of the second max is: " + str(second_max))


"""
给你一个 32 位的有符号整数 x ，返回将 x 中的数字部分反转后的结果。
如果反转后整数超过 32 位的有符号整数的范围[−231, 231− 1] ，就返回 0。
假设环境不允许存储 64 位整数（有符号或无符号）。

示例 1：

输入：x = 123
输出：321
示例 2：

输入：x = -123
输出：-321
示例 3：

输入：x = 120
输出：21
示例 4：

输入：x = 0
输出：0

提示：

-231 <= x <= 231 - 1

作者：力扣 (LeetCode)
链接：https://leetcode.cn/leetbook/read/top-interview-questions-easy/xnx13t/
来源：力扣（LeetCode）
著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。
"""

class Solution(object):
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        # 我的思路一开始是先把符号取下来，然后把数字放到一个数组，再给他拼回来，然后乘一下，也是受上一个反转list的代码影响，
        # 这里没有限制空间复杂度，所以可以直接做
        # 但是有一个溢出的判断需要斟酌
        flag = 0 if x >= 0 else 1
        x = abs(x)
        y = 0
        while (x > 0):
            y = y * 10 + x % 10
            x = x // 10

        if y > 2 ** 31 - 1:
            return 0

        if flag:
            y = 0 - y

        return y


from sklearn.feature_extraction.text import TfidfVectorizer
import numpy as np
from random import shuffle
import pickle as pkl
import glob
import ujson
from sklearn import svm
"""
Inspired by https://github.com/karpathy/arxiv-sanity-lite/blob/master/serve.py
"""

class Analyzer:

    def __init__(self, max_train: int = 5000) -> None:
        self.max_train = max_train
        # get the stopwords for PL
        with open("data/stopwords.txt", "r") as f:
            self.stopwords = f.read().splitlines()

    def make_corpus(self, training: bool) -> list:
        """Create a corpus of all the articles."""
        all_sources = [
            "data/tvp_articles",
            "data/wp_articles",
        ]
        all_article_paths = []
        for src in all_sources:
            all_article_paths.extend(glob.glob(f"{src}/*.json"))
        print(f"Found {len(all_article_paths)} articles.")
        if training:
            shuffle(all_article_paths)
            all_article_paths = all_article_paths[:self.max_train]

        for article in all_article_paths:
            article = ujson.load(open(article, "r"))
            # yield ' '.join([article['source'], article['content']])
            yield article["content"]

    def create_global_corpus(self,
                             max_df: float = 0.1,
                             min_df: float = 3,
                             num: int = 20000) -> None:
        """Create a global corpus of all the articles."""
        v = TfidfVectorizer(
            input="content",
            encoding="utf-8",
            decode_error="replace",
            strip_accents="unicode",
            lowercase=True,
            analyzer="word",
            stop_words=self.stopwords,
            token_pattern=r"(?u)\b[a-zA-Z_][a-zA-Z0-9_]+\b",
            ngram_range=(1, 2),
            max_features=num,
            norm="l2",
            use_idf=True,
            smooth_idf=True,
            sublinear_tf=True,
            max_df=max_df,
            min_df=min_df,
        )
        v.fit(self.make_corpus(True))
        X = v.transform(self.make_corpus(False)).astype(np.float32)
        pkl.dump(
            {
                "X": X,
                "vocab": v.vocabulary_,
                "idf": v.idf_
            },
            open("data/global_corpus.pkl", "wb"),
        )

    def load_features(self):
        """Load the features from the global corpus."""
        feats = pkl.load(open("data/global_corpus.pkl", "rb"))
        X = feats["X"]
        y = np.zeros_like(X.shape[0])
        return X, y, feats["vocab"], feats["idf"]

    def rank_news(self, C: float = 0.01):
        """Rank the news."""
        # classify
        X, y, vocab, idf = self.load_features()

        clf = svm.LinearSVC(class_weight="balanced",
                            verbose=False,
                            max_iter=10000,
                            tol=1e-6,
                            C=C)
        clf.fit(X, y)
        s = clf.decision_function(X)
        sortix = np.argsort(-s)
        scores = [100 * float(s[ix]) for ix in sortix]

        ivocab = {v: k for k, v in vocab.items()}  # index to word mapping
        weights = clf.coef_[0]  # (n_features,) weights of the trained svm
        sortix = np.argsort(-weights)
        words = []
        for ix in list(sortix[:40]) + list(sortix[-20:]):
            words.append({
                "word": ivocab[ix],
                "weight": weights[ix],
            })
        return scores, words

if __name__ == "__main__":
    a = Analyzer()
    a.create_global_corpus()


import pytest
import pandas as pd
from polyfuzz.models import TFIDF
from tests.utils import get_test_strings

from_list, to_list = get_test_strings()

@pytest.mark.parametrize("method", ["sparse", "knn", "sklearn"])
def test_distance(method):
    model = TFIDF(cosine_method=method)
    matches = model.match(from_list, to_list)

    assert isinstance(matches, pd.DataFrame)
    assert matches.Similarity.mean() > 0.0
    assert len(matches) == 6
    assert list(matches.columns) == ['From', 'To', 'Similarity']

@pytest.mark.parametrize("n_gram_low, n_gram_high", [(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)])
def test_ngrams(n_gram_low, n_gram_high):
    model = TFIDF(n_gram_range=(n_gram_low, n_gram_high))
    matches = model.match(from_list, to_list)

    assert isinstance(matches, pd.DataFrame)
    assert len(matches) == 6
    assert list(matches.columns) == ['From', 'To', 'Similarity']


from django.urls import path
from . import views
from .views import index

urlpatterns = [
    path('',views.index,name='index'),
    path('Home.html',views.Home,name='Home'),
    path('test.html',views.test,name='test'),
    path('test',views.adulttest,name='adulttest'),
    path('child-test.html',views.childtest,name='childtest'),
    path('contact-us.html',views.contactus,name='contactus'),
    path('userdetails',views.adultdetails,name='adultdetails')

]


from __future__ import print_function
import os
import re
import csv
import copy
import glob
import datetime
import json

FREESURFER_QUESTIONNAIRES = [
    "LH.APARC.FOLDIND", "LH.APARC.AREA", "RH.APARC.CURVIND",
    "LH.APARC.THICKNESSSTD", "MEASURE:VOLUME", "LH.APARC.MEANCURV",
    "RH.APARC.THICKNESSSTD", "LH.APARC.VOLUME", "RH.APARC.MEANCURV",
    "RH.APARC.VOLUME", "LH.APARC.CURVIND", "LH.APARC.THICKNESS",
    "RH.APARC.FOLDIND", "RH.APARC.GAUSCURV", "RH.APARC.AREA",
    "LH.APARC.GAUSCURV", "RH.APARC.THICKNESS"]
FREESURFER = [
    "FREESURFER"]

RQL_T1 = ("Any SC Where S is Subject, S code_in_study '{0}', "
          "S subject_scans SC, SC in_assessment A, A timepoint '{1}', "
          "SC label 'ADNI_MPRAGE'")

def freesurfer_stats(fsstatdirs, study_name, subject_age_map, savedir=None,
                     is_openanswer=False):
    """ Parse the freesurfer stats files and create a structure that
    fulfill the questionnaire importer synthax.

    Parameters
    ----------
    fsstatdirs: dict (mandatory)
        the location of the FreeSurfer stats directories with the timepoints
        as key.
    study_name: str (mandatory)
        the name of the study.
    subject_age_map: dict (mandatory)
        a map between subject ids as keys and subject ages as
        values with the time points as a fist key.
    savedir: str (optional, defafult None)
        if a valid directory is specified write the generated structure as
        a json file.
    is_openanswer: bool (optional, default False)
        if True use types open answers.

    Returns
    -------
    questionnaires: dict
        the generated structure with the stat files information.
    """
    # Go through timepoints
    questionnaires = {}
    for timepoint, statdir in fsstatdirs.items():

        # List all csv stat files
        files = glob.glob(os.path.join(statdir, "*.csv"))

        # Go through stat files
        for filepath in files:

            # Parse the stat file
            with open(filepath, "rb") as csvfile:
                dialect = csv.Sniffer().sniff(csvfile.read())
                csvfile.seek(0)
                reader = csv.reader(csvfile, dialect)
                rows = [row for row in reader]
                headers = rows[0]
                qname = headers[0]
                for row in rows[1:]:
                    subject = row[0]
                    age = subject_age_map[timepoint][subject]
                    qstruct = {
                        "Questionnaires": {
                            qname: None
                        },
                        "Assessment": {
                            "age_of_subject": float(age),
                            "identifier": u"{0}_{1}_{2}_{3}".format(
                                    timepoint, qname.upper(),
                                    study_name.upper(), subject),
                            "timepoint": unicode(timepoint)
                        }
                    }
                    if is_openanswer:
                        qstruct["Questionnaires"][qname] = {
                            "{0}: float".format(k): float(v)
                            for k, v in zip(headers[1:], row[1:])}
                    else:
                        qstruct["Questionnaires"][qname] = {
                            k: float(v) for k, v in zip(headers[1:], row[1:])}
                    questionnaires.setdefault(subject, []).append(qstruct)

    # Save the generated structure
    if savedir is not None and os.path.isdir(savedir):
        date = datetime.date.today()
        save_file = os.path.join(savedir, "freesurfer_stats_{0}.json".format(
            date.isoformat()))
        with open(save_file, "w") as openfile:
            json.dump(questionnaires, openfile, indent=4, sort_keys=True)

    return questionnaires

def freesurfer(fsdirs, study_name, subject_pattern, tool_version,
               tool_parameters=None, savedir=None, rql_template=RQL_T1):
    """ Parse the freesurfer files and create a structure that
    fulfill the processing importer synthax.

    Parameters
    ----------
    fsdirs: dict (mandatory)
        the location of the FreeSurfer directory with the
        timepoints as key.
    study_name: str (mandatory)
        the name of the study.
    subject_pattern: str (mandatory)
        a pattern used to extract the subject name from the FreeSurfer
        directories names.
    tool_version: str (mandatory)
        the FreeSurfer tool version.
    tool_parameters: object (optional, defafult None)
        a structure describing the used options.
    savedir: str (optional, defafult None)
        if a valid directory is specified write the generated structure as
        a json file.
    rql_template: str (optional, default RQL_T1)
        the rql used to retrieve the t1 scan attached to a FreeSurfer
        processing.

    Returns
    -------
    processings: dict
        the generated structure with the FreeSurfeer files information.
    """
    # Go through timepoints
    processings = {}
    for timepoint, fsdir in fsdirs.items():

        # Create an assessment
        assessment_id = u"{0}_FREESURFER_{1}".format(
            timepoint, study_name.upper())
        assessment_struct = {
            "age_of_subject": 0,
            "identifier": assessment_id,
            "timepoint": unicode(timepoint)
        }

        # Go through subjects
        for subject in os.listdir(fsdir):

            # Get the subject code
            subjectfsdir = os.path.join(fsdir, subject)
            if len(re.findall(subject_pattern, subject)) != 1:
                print("Skip '{0}' since no valid subject can be extracted "
                      "with pattern '{1}'.".format(subjectfsdir,
                                                   subject_pattern))
                continue

            # Build a RQL to get the input T1
            rql_t1 = rql_template.format(subject, timepoint)

            # Create the processingrun: multiple filsets
            processingrun_id = u"{0}_{1}".format(assessment_id, subject)
            processingrun_struct = {
                "identifier": processingrun_id,
                "label": u"FreeSurfer",
                "tool": u"FreeSurfer",
                "version": unicode(tool_version),
                "parameters": unicode(json.dumps(tool_parameters))
            }
            fileset_structs = []
            extresources_structs = []

            # Go through FreeSurfer subdirectories
            for dirname in os.listdir(subjectfsdir):
                fsetpath = os.path.join(subjectfsdir, dirname)

                # If subdirectory not empty
                if os.path.isdir(fsetpath) and len(os.listdir(fsetpath)) > 0:

                    # Create a fileset
                    fset_id = u"{0}_{1}".format(processingrun_id, dirname)
                    fileset_structs.append({
                        "identifier": fset_id,
                        "name": u"FreeSurfer {0}".format(dirname.upper())})
                    extresources_structs.append([])

                    # Create the external files
                    for root, dirs, files in os.walk(fsetpath):
                        for fname in files:
                            fpath = os.path.join(root, fname)
                            file_struct = {
                                "identifier": unicode(fpath),
                                "absolute_path": True,
                                "name": unicode(fname),
                                "filepath": unicode(fpath)
                            }
                            extresources_structs[-1].append(file_struct)

            # Create the final structure
            processing_struct = {
                "Assessment": copy.deepcopy(assessment_struct),
                "Processings": [{
                    "Inputs": [rql_t1],
                    "ExternalResources": extresources_structs,
                    "FileSets": fileset_structs,
                    "ProcessingRun": processingrun_struct
                }]
            }
            processings.setdefault(subject, []).append(processing_struct)

    # Save the generated structure
    if savedir is not None and os.path.isdir(savedir):
        date = datetime.date.today()
        save_file = os.path.join(savedir, "freesurfer_{0}.json".format(
            date.isoformat()))
        with open(save_file, "w") as openfile:
            json.dump(processings, openfile, indent=4, sort_keys=True)

    return processings


import math
from scipy import stats
import numpy as np

def AIBN(Oop, Sr):
    R = 8.31434
    t = 80
    T = t + 273.15
    theta = 4.575 * T / 1000
    beta = 3.436 * math.exp(-1200 * 4.19 /(R * T))
    HIn = 0.005
    k2 = 10**(6.6 - 9.8/theta)
    k6 = 10**(9.2 - 6.00/theta)
    ki = 10**(15 - 30.45/theta)
    RH = 7.17

    data1 = []
    with open(Oop, 'r') as f:
        dat = f.readlines()
        for line in dat:
            spl = str.split(line, '\t')
            data1.append(spl)

    data2 = []
    with open(Sr, 'r') as f:
        dat = f.readlines()
        for line in dat:
            spl = str.split(line, '\t')
            data2.append(spl)

    n = len(data1)
    W0 = []
    AIBN = []
    W = []
    Wi = []
    Wok0 = []
    Wok = []
    tau = []
    x = []
    y = []
    for i in range(n):
        W0.append(float(data1[i][1]) / (60 * 22.4))
        W.append(float(data2[i][1]) / (60 * 22.4))
        AIBN.append(float(data2[i][0]))
        Wi.append(2 * beta * ki * AIBN[i])
        Wok0.append(W0[i] + Wi[i] * (1 - beta) / (2 * beta))
        Wok.append(W[i] + Wi[i] * (1 - beta) / (2 * beta))
        tau.append(float(data2[i][2]) * 60)
        x.append(HIn / Wi[i])
        y.append(tau[i])

    a, b, r_value, p_value, std_err = stats.linregress(x,y)
    f = a
    yrs = []
    for i in range(n):
        yrs.append(a * x[i] + b)

    Z = n * sum(map(lambda xx: xx * xx, x)) - sum(x) * sum(x)
    _s = 0
    for i in range(n):
        _s += (y[i] - yrs[i]) ** 2
    Sy = math.sqrt(_s / (n - 1))
    Sa_f = math.sqrt(n * Sy * Sy / Z)
    eps = abs(100 * Sa_f / a)

    x = []
    y = []
    for i in range(n):
        x.append(math.log(Wi[i]))
        y.append(math.log(Wok[i]))

    a, b, r_value, p_value, std_err = stats.linregress(x,y)
    n_AIBN = a
    yrs = []
    for i in range(n):
        yrs.append(a * x[i] + b)

    Z = n * sum(map(lambda xx: xx * xx, x)) - sum(x) * sum(x)
    _s = 0
    for i in range(n):
        _s += (y[i] - yrs[i]) ** 2
    Sy = math.sqrt(_s / (n - 1))
    Sa_n_AIBN = math.sqrt(n * Sy * Sy / Z)
    eps = abs(100 * Sa_n_AIBN / a)

    Ft = []
    for i in range(n):
        Ft.append((Wok0[i] / Wok[i]) * abs(1 - ((Wok[i] ** 2) / (Wok0[i] ** 2))))

    x = []
    y = []
    for i in range(n):
        x.append(math.log(Wi[i]))
        y.append(math.log(Ft[i]))

    a, b, r_value, p_value, std_err = stats.linregress(x,y)
    nf_AIBN = a
    yrs = []
    for i in range(n):
        yrs.append(a * x[i] + b)

    Z = n * sum(map(lambda xx: xx * xx, x)) - sum(x) * sum(x)
    _s = 0
    for i in range(n):
        _s += (y[i] - yrs[i]) ** 2
    Sy = math.sqrt(_s / (n - 1))
    Sa_nf_AIBN = math.sqrt(n * Sy * Sy / Z)
    eps = abs(100 * Sa_nf_AIBN / a)

    x = []
    y = []
    for i in range(n):
        x.append(RH * math.sqrt(Wi[i]))
        y.append(Wok0[i])

    a, b, r_value, p_value, std_err = stats.linregress(x,y)
    k2k6 = a
    yrs = []
    for i in range(n):
        yrs.append(a * x[i] + b)

    Z = n * sum(map(lambda xx: xx * xx, x)) - sum(x) * sum(x)
    _s = 0
    for i in range(n):
        _s += (y[i] - yrs[i]) ** 2
    Sy = math.sqrt(_s / (n - 1))
    Sa_k2k6 = math.sqrt(n * Sy * Sy / Z)
    eps = abs(100 * Sa_k2k6 / a)

    x = []
    y = []

    for i in range(n):
        x.append(RH * Wi[i] / (HIn * f))
        y.append(Wok[i])

    a, b, r_value, p_value, std_err = stats.linregress(x,y)
    k2k7 = a
    yrs = []
    for i in range(n):
        yrs.append(a * x[i] + b)

    k7 = k2 / k2k7

    Z = n * sum(map(lambda xx: xx * xx, x)) - sum(x) * sum(x)
    _s = 0
    for i in range(n):
        _s += (y[i] - yrs[i]) ** 2
    Sy = math.sqrt(_s / (n - 1))
    Sa_k2k7 = math.sqrt(n * Sy * Sy / Z)
    eps = abs(100 * Sa_k2k7 / a)
    Sa_k7=k7 * eps / 100
    fk7 = f * k7
    Sa_fk7 = fk7 * math.sqrt(math.pow(Sa_f/f, 2) + math.pow(Sa_k7/k7, 2))

    return AIBN, W, Wok, tau, Wi, Ft, f, n_AIBN, nf_AIBN, fk7, k2k6, k2k7, k7, Sa_f, Sa_n_AIBN, Sa_nf_AIBN, Sa_k2k6, Sa_k2k7, Sa_k7, Sa_fk7


"""
Наиболее интересное решение задачи, так как тут сразу будем читать из файл с текстом, и даты записывать в новый файл.
Тут нужно учитывать кодировку, чтобы понимать сколько байт весит один символ.
При помощи file.seek() мы будем перемещаться по байтам, пропуская ненужное количество итераций, если дата найдена.
Но чтобы использовать file.seek() и двигаться вперед и назад без проблем, не забываем открывать все в бинаре
"""

import re

with open('text.txt', 'rb') as text_file:
    new_file = open("data.txt", "w")
    check_end_text = 0
    while True:
        symbol = text_file.read(1)
        if not symbol:
            break
        if symbol.isdigit():
            text_file.seek(-1, 1)
            symbol = text_file.read(10)
            symbol_to_write = symbol.decode('utf-8')
            if len(symbol_to_write) < 10:
                break
            if re.findall(r'\d{4}-\d\d-\d\d', symbol_to_write):
                symbol_to_write += "\n"
                new_file.write(symbol_to_write)
            else:
                text_file.seek(-9, 1)
    new_file.close()


from tkinter import *
import random
root=Tk()
root.title("3d Arrays")
root.geometry("500x500")
lbl=Label(root)
array_3d=[[["1","2","3","4","5","6"],["heads","tails"],["A","B","C","D","E","F","G","H","I","J"]]]

print(array_3d[0][0][3])

def new_word():
    r1=random.randint(0,5)
    r2=random.randint(0,1)
    r3=random.randint(0, 9)

    letter1=str(array_3d[0][0][r1])
    letter2=str(array_3d[0][1][r2])
    letter3=str(array_3d[0][2][r3])

    lbl["text"]=letter1+" "+letter2+" "+letter3+" "

btn=Button(root,text="Get letter",command=new_word)
btn.place(relx=0.5,rely=0.4,anchor=CENTER)

lbl.place(relx=0.5,rely=0.6,anchor=CENTER)

root.mainloop()


import hashlib
import re

class User:
    required_fields = ['username', 'secret']

    def __init__(self, username, secret):
        self.id = None
        self.username = username
        self.secret = secret

    def __str__(self):
        return "{}|{}".format(self.username, self.secret)

def encrypt_secret(plaintext: str) -> str:
    """
    a totally cryptographically secure method to encrypt secrets GUARANTEED unbreakable
    :param plaintext: the plaintext secret
    :return: the unbreakable encrypted version of the secret
    """
    hash = hashlib.md5()
    hash.update(plaintext.encode('utf-8'))
    return hash.hexdigest()

def validate_secret(plaintext: str) -> bool:
    result = re.match('^[a-z]{1,5}$', plaintext)
    if result is None:
        return False
    return True

def create_user(username: str, plaintext_secret: str) -> User:
    """
    validate and create a User object
    :param username: the name of the user
    :param plaintext_secret: the plaintext version of the user's secret
    :return: a User object representing that user with their secret encrypted
    """
    return User(username, encrypt_secret(plaintext_secret))

def create_user_from_dict(d: dict) -> User:
    """
    validate and create a User object from a dictionary of values
    :param d: a dict containing all the needed fields
    :return: a User object if the dict is valid
    """
    errors = []
    for field in User.required_fields:
        if field not in d.keys():
            errors.append('{} was not provided'.format(field))

    if 'secret' in d:
        if d['secret'] == '':
            errors.append('no secret provided')
        elif validate_secret(d['secret']) is False:
            errors.append('"{}" is not a valid secret'.format(d['secret']))

    if errors:
        raise ValueError(*errors)
    return create_user(d['username'], d['secret'])