import logging
import logging.handlers

LOG_FILENAME = 'test.log'

logger = logging.getLogger('Fluentd-Test')
logger.setLevel(logging.DEBUG)

log_formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, maxBytes=200000, backupCount=5)
handler.setLevel(logging.DEBUG)
handler.setFormatter(log_formatter)

logger.addHandler(handler)

def main():
    logger.info('Info Test')
    logger.error('Error Test')
    logger.debug('Debug Test')

if __name__ == '__main__':
    main()


"""Explicitly create vshifts

Revision ID: d0841384d5d7
Revises: e3ef522bd3d9
Create Date: 2021-07-05 22:05:52.743905

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'd0841384d5d7'
down_revision = 'e3ef522bd3d9'
branch_labels = None
depends_on = None

def upgrade():
    op.create_table (
        "volgisticsshifts",
        sa.Column("_id", sa.Integer, primary_key=True),
        sa.Column("volg_id", sa.Integer, nullable=False),
        sa.Column("assignment", sa.String(), nullable=True),
        sa.Column("site", sa.String(), nullable=True),
        sa.Column("from_date",  sa.Date, nullable=False),
        sa.Column("hours",  sa.DECIMAL, nullable=False)
    )

    op.execute("""CREATE  INDEX vs_volg_id_idx
                    ON public.volgisticsshifts USING btree (volg_id);"""
                    )

    op.create_unique_constraint( "uq_shift", "volgisticsshifts",  ["volg_id", "assignment", "from_date", "hours"] )

def downgrade():
    op.drop_table("volgisticsshifts")


#!/usr/bin/python3

"""
Defines the model of State
"""
from sqlalchemy import MetaData, Column, Integer, String
from sqlalchemy.orm import declarative_base

mymetadata = MetaData()
Base = declarative_base(metadata=mymetadata)

class State(Base):
    """
    Class with id and name attributes of each state
    """
    __tablename__ = 'states'
    id = Column(Integer, unique=True, nullable=False, primary_key=True)
    name = Column(String(128), nullable=False)


import training
import scoring
import deployment
import diagnostics
import reporting
import json
import os
import sys
from pathlib import Path
import subprocess
import pandas as pd

with open('config.json','r') as f:
    config = json.load(f)

##################Check and read new data
#first, read ingestedfiles.txt
file_list_path = os.path.join(config["prod_deployment_path"], config['datalist_fname'])
ingested_files = []
with open(file_list_path, 'r') as f:
    for line in f:
        ingested_files.append(line)
#second, determine whether the source data folder has files that aren't listed in ingestedfiles.txt
data_files = list(Path(config["input_folder_path"]).glob('*.csv'))

##################Deciding whether to proceed, part 1
#if you found new data, you should proceed. otherwise, do end the process here

new_files = False
for file in data_files:
    for complete in ingested_files:
        if complete != file.name:
            new_files = True
            break
if not new_files:
    print("No new data found, exiting!")
    sys.exit()

print("Found new data!")
subprocess.run(args=['python', 'ingestion.py'])

##################Checking for model drift
#check whether the score from the deployed model is different from the score from the model that uses the newest ingested data
old_file_path = os.path.join(config["prod_deployment_path"], config['log_fname'])
with open(old_file_path, 'r') as f:
    score = float(f.readline())

new_data = pd.read_csv(data_files[0])
new_score = scoring.score_model(new_data, production=True)
new_score = 0.1

##################Deciding whether to proceed, part 2
#if you found model drift, you should proceed. otherwise, do end the process here
print('New Score: ' + str(new_score))
print('Old Score: ' + str(score))
if new_score < score:
    print('Model drift has occured, retraining...')
else:
    print('Model has not drifted, exiting...')
    sys.exit()

##################Re-deployment
#if you found evidence for model drift, re-run the deployment.py script
subprocess.run(args=['python', 'training.py'])
subprocess.run(args=['python', 'deployment.py'])

print('Model retrained and deployed!')

##################Diagnostics and reporting
#run diagnostics.py and reporting.py for the re-deployed model
subprocess.run(args=['python', 'apicalls.py'])
subprocess.run(args=['python', 'reporting.py'])


import os
import webapp2
import jinja2
import urllib2
from xml.dom import minidom

from google.appengine.ext import db

template_dir = os.path.join(os.path.dirname(__file__), "templates")
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),
                                autoescape = True)

IP_URL = "http://ip-api.com/xml/"

def get_coords(ip):
    ip = "4.2.2.2"
    url = IP_URL + ip
    content = None
    try:
        content = urllib2.urlopen(url).read()
    except URLError:
        return

    if content:
        d = minidom.parseString(content)
        lat_object = d.getElementsByTagName("lat")
        lon_object = d.getElementsByTagName("lon")
        if lat_object and lon_object and lat_object[0].childNodes[0].nodeValue and lon_object[0].childNodes[0].nodeValue:
            return db.GeoPt(lat_object[0].childNodes[0].nodeValue, lon_object[0].childNodes[0].nodeValue)

class Handler(webapp2.RequestHandler):
    def write(self, *a, **kw):
        self.response.out.write(*a, **kw)

    def render_str(self, template, **params):
        t = jinja_env.get_template(template)
        return t.render(params)

    def render(self, template, **kw):
        self.write(self.render_str(template, **kw))

class Art(db.Model):
    title = db.StringProperty(required = True)
    art = db.TextProperty(required = True)
    created = db.DateTimeProperty(auto_now_add = True)
    coords = db.GeoPtProperty()

class MainPage(Handler):
    def render_front(self, title="", art="", error=""):
        arts = db.GqlQuery( "SELECT * FROM Art "
                            "ORDER BY created DESC "
                            "LIMIT 10")
        self.render("front.html", title=title, art=art, error=error, arts = arts)

    def get(self):
        self.write(repr(get_coords(self.request.remote_addr)))
        self.render_front()

    def post(self):
        title = self.request.get("title")
        art = self.request.get("art")

        if title and art:
            a = Art(title = title, art = art)
            coords = get_coords(self.request.remote_addr)
            if coords:
                a.coords = coords
            a.put()

            self.redirect("/")
        else:
            error = "we need both a title and some artwork"
            self.render_front(title, art, error)

app = webapp2.WSGIApplication([ ('/', MainPage)], debug=True)


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

"""
@date: 2020/11/3 上午10:22
@file: act_helper.py
@author: zj
@description:
"""

import torch.nn as nn

def get_act(type):
    if type == 'ReLU':
        return nn.ReLU
    else:
        raise ValueError(f'{type} does not exists')


"""
AI plays Tic-Tac_toe against itself.
Adverserial Search with Tic-Tac-Toe
"""

from typing import List, Tuple
from enum import Enum
from math import inf
from copy import deepcopy
from random import randint, random, choice

class Player(Enum):
    X = 'X'   # MAX-player
    O = 'O'   # MIN-player
    EMPTY = chr(0x25a1)

class State():
    def __init__(self, *args, **kwargs):
        self._mx = list(args[0]) if args else [[Player.EMPTY,]*3 for _ in range(3)]
        self.utility_value = kwargs.get("utility")   # None by default
        self._terminal = None

        # Check state validity
        l = sum(self._mx, [])
        xx,oo = (l.count(v) for v in (Player.X, Player.O))
        if not(0 <= (xx - oo) <=1):
            raise ValueError("Invalid board state")

    def player(self) -> Player:
        """Returns the player who's turn it is"""
        l = sum(self._mx, [])
        xx,oo = (l.count(v) for v in (Player.X, Player.O))
        return Player.X if xx - oo == 0 else Player.O

    def actions(self) -> List[Tuple[int,int]]:
        """Returns valid moves from this state"""
        return [(i,j) for i in range(3) for j in range(3) if self._mx[i][j] == Player.EMPTY]

    def result(self, action) -> 'State':
        """returns the next state"""
        assert min(action) >= 0 and max(action) <= 2
        copy = deepcopy(self._mx)
        r,c = action
        if copy[r][c] != Player.EMPTY:
            raise ValueError("bad action (tuple-index)")
        player = self.player()
        copy[r][c] = player
        s = State(copy)
        r,c = [(i,j) for i in range(3) for j in range(3) if self._mx[i][j] != s._mx[i][j]][0]
        s.move = "ABC"[r] + str(c+1)
        return s

    def children(self) -> List:
        """returns next states from this state.
        This is a wrapper-function around actions() and result() methods"""
        return [self.result(action) for action in self.actions()]

    @property
    def terminal(self) -> bool:
        """is this state terminal?"""
        if self.utility():
            return True

        l = sum(self._mx, [])
        slices = [slice(i,None,3) for i in range(3)] + [slice(None,None,4), slice(2,-1,2)]

        for e in self._mx + slices:
            S = set(l[e] if isinstance(e, slice) else e)
            if not {Player.X, Player.O}.issubset(S) and Player.EMPTY in S:
                return False
        return True

    def utility(self, depth=None) -> int or float:
        """returns the utility"""
        l = sum(self._mx, [])
        slices = [slice(i,None,3) for i in range(3)] + [slice(None,None,4), slice(2,-1,2)]
        depth = abs(depth or 1)

        for e in self._mx + slices:
            t = l[e] if isinstance(e, slice) else e
            if t.count(Player.X) == 3:
                return 1 / depth
            elif t.count(Player.O) == 3:
                return -1 / depth
        return 0

    def __str__(self):
        return str.join('\n', ((str.join('', (v.value for v in row))) for row in self._mx))

    def __lt__(self, other):
        return self.utility_value < (other.utility_value if isinstance(other, State) else other)
    def __gt__(self, other):
        return self.utility_value > (other.utility_value if isinstance(other, State) else other)

def min_play(state, depth=0):
    # Base case
    if state.terminal:
        if depth==0: return state
        return state.utility(depth=depth)
    # Recursive
    best = inf
    for child in state.children():
        v = max_play(child, depth-1)
        if depth==0:
            child.utility_value = v
            v = child
        best = min(v, best)

    # Figure out what move the ai has made for printing purposes
    if depth == 0 and isinstance(best, State):
        r,c = [(i,j) for i in range(3) for j in range(3) if state._mx[i][j] != best._mx[i][j]][0]
        best.move = "ABC"[r] + str(c+1)
    return best

def max_play(state, depth=0):
    # Base case
    if state.terminal:
        if depth==0: return state
        return state.utility(depth=depth)
    # Recursive
    best = -inf
    for child in state.children():
        v = min_play(child, depth-1)
        if depth==0:
            child.utility_value = v
            v = child
        best = max(v, best)

    # Figure out what move the ai has made for printing purposes
    if depth == 0 and isinstance(best, State):
        r,c = [(i,j) for i in range(3) for j in range(3) if state._mx[i][j] != best._mx[i][j]][0]
        best.move = "ABC"[r] + str(c+1)
    return best

def decorator(func):
    """Checks whether the square chosen by the user is occupied"""
    def closure(*args, **kwargs):
        state = kwargs.get("state")
        actions = kwargs.get("actions")

        while True:
            user_input = func()
            if not(state or actions):
                return user_input

            # Get allowed actions (i.e. moves) - if not provided by the user
            actions = actions or state.actions()

            # Check validity of the move
            if user_input not in actions:
                print("The selected square is taken.")
            else:
                return user_input
    return closure

@decorator
def get_input():
    rows = {'a':0, 'b':1, 'c':2}
    cols = {str(i+1):i for i in range(3)}
    while True:
        s = input("Your move: ")
        s = s.lower().strip()
        if len(s)!=2 or s[0] not in rows.keys() or s[1] not in cols.keys():
            print("bad input - accpted format: A1")
            continue
        else:
            return (rows[s[0]], cols[s[1]])

def check_winner(state):
    if state.terminal:
        print(state)
        cost = state.utility()
        winner = {0:None, 1:Player.X, -1:Player.O}[cost]
        if not winner:
            print("\nDRAW")
        else:
            print("\nWINNER:", winner.value)
        return winner or "DRAW"
    return False

def controller(randomness=True):
    """Simulates two AI's playing against each other.
    X starts.  if randomness=True then X makes sometines random moves
    which enables O to win at times.
    Otherwise each game ends in a draw"""

    # Initial state
    s = State()
    action = [randint(0,2) for _ in range(2)]
    s = s.result(action)   # X player
    print("\nX's move:", s, sep='\n', end='\n\n')

    while True:
        # O's move
        s = min_play(s)   # s = AI's move
        print(f"O's move: {s.move}")
        print(s, end='\n\n')

        winner = check_winner(s)
        if winner: break

        # X's move
        if randomness and random() < 0.35:
            a = choice(s.actions()) # random allowed action
            s = s.result(a)
            print(f"X's random move: {s.move}")
        else:
            s = max_play(s)
            print(f"X's move: {s.move}")

        print(s, end='\n\n')

        winner = check_winner(s)
        if winner: break

    return winner

winner = controller(randomness=True)
print("The winner is:", winner)


import re, json, os

def extract_question(in_file, out_file):
    cc1_all_text = open(in_file, 'r', encoding='utf8').read()

    rlt = []
    for item in re.finditer(r'
', cc1_all_text):
        rlt.append(item.start())

    print(f'find {len(rlt)}')

    def has_images(question_body):
        img_s = question_body.find('<img ')
        if img_s != -1:
            return True
        else:
            return False

    question_list = []
    need_images = []
    for pos in rlt:
        span1 = cc1_all_text.find('', pos) + 6
        span1_end = cc1_all_text.find('', span1)
        question_order = cc1_all_text[span1 : span1_end]

        question_body_s = cc1_all_text.find('', span1_end) + 5
        question_body_e = cc1_all_text.find('
', question_body_s)
        question_body = cc1_all_text[question_body_s : question_body_e].replace('\n', '').replace('\r', '').replace('
', '\n')
        question_body = re.sub(r'^\s+', '', question_body)

        last_end = question_body_e
        def find_selection():
            nonlocal last_end
            sel_s = cc1_all_text.find('
', last_end)
            sel_s = cc1_all_text.find('<input', sel_s)
            sel_s = cc1_all_text.find('>', sel_s) + 1
            sel_e = cc1_all_text.find('
', sel_s)
            sel = cc1_all_text[sel_s:sel_e].replace('\n', ' ').replace('\r', '').replace('
', '\n')
            sel = re.sub(r'^\s+', '', sel)
            last_end = sel_e + 4
            return sel

        a = find_selection()
        b = find_selection()
        c = find_selection()
        d = find_selection()

        answer_s = cc1_all_text.find('class="answer_right">', last_end) + 21
        answer_e = cc1_all_text.find('', answer_s)
        answer = cc1_all_text[answer_s: answer_e]

        my_answer_s = cc1_all_text.find('class="answer_wrong">', answer_e) + 21
        my_answer_e = cc1_all_text.find('', my_answer_s)
        my_answer = cc1_all_text[my_answer_s: my_answer_e]

        jiexi_s = cc1_all_text.find('
', my_answer_e) + 25
        jiexi_e = cc1_all_text.find('
', jiexi_s)
        jiexi = cc1_all_text[jiexi_s:jiexi_e]
        jiexi = re.sub('^\s+', '', jiexi, flags=re.M)

        question_list.append([question_order, question_body, a, b, c, d, answer, my_answer, jiexi])
        if has_images(question_body):
            need_images.append(question_order)

        print(f'{question_order} {question_body[:50]}...')

    print('write to file: ', out_file,', count: ' , len(question_list))
    print(f'need images: ' + ','.join(need_images))
    open(out_file, 'w', encoding='utf8').write(json.dumps(question_list, ensure_ascii=False, indent=2))

html_files = os.listdir('html')
json_files = list(map(lambda x:x[:-5] + '.json', html_files))

for i in range(len(html_files)):
    in_file =  'html/' + html_files[i]
    out_file = 'json/' + json_files[i]
    print('process', in_file)
    if os.path.isfile(out_file):
        print('\t', 'already exists, skip', in_file)
    else:
        extract_question(in_file, out_file)


import datetime
import os
import shutil
import re
import time
import random
import json
from copy import deepcopy
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
from scipy.cluster.hierarchy import dendrogram
from sklearn.cluster import AgglomerativeClustering
import matplotlib.pyplot as plt
import queue

from tqdm import tqdm

from multiprocessing import Pool
from multiprocessing.pool import ThreadPool
import itertools
from difflib import SequenceMatcher

if __name__ == '__main__':
    section_musks = True
    if section_musks:
        path = r"C:\school\PoliticalShapley\statistics\data\lms\mosques_raw.csv"
        root_path = os.path.dirname(path)
        mosque_path = os.path.join(root_path, f'Mosques.csv')
        msqdf = pd.read_csv(path, encoding='utf-8-sig')
        msqdf = msqdf[['town', 'mosque']]

        towncode_path = r"C:\school\PoliticalShapley\statistics\Results\votes distributions.csv"
        towndf = pd.read_csv(towncode_path, encoding='utf-8-sig')
        towndf = towndf[['town', 'uid']].sort_values(by='town', ascending=False)

        # Replace
        for from_name, to_name in [('יפה', 'תל אביב יפו'),
                                   ('באקה', 'באקה אלגרביה'),
                                   ('סאלם', 'סולם'),
                                   ('סולם  ', 'סולם'),
                                   ('גת', 'גית'),
                                   ('כפר מסר', 'כפר מצר'),
                                   ('גוש חלב', 'גש גוש חלב'),
                                   ('בועינה', 'בענה'),
                                   ('אלמגאר', 'מגאר'),
                                   ('פרדיס', 'פוריידיס'),
                                   ('גדידה', 'גדיידהמהכר'),
                                   ('תרשיחא', 'מעלותתרשיחא'),
                                   ('סידנא עלי', 'הרצליה'),
                                   ("חג'אג'רה", 'כעביהטבאשחגאגרה'),
                                   ('בועינה', 'בענה'),

                                   ]:
            idxs = msqdf['town'].eq(from_name)
            msqdf.loc[idxs, 'town'] = to_name

        # Find similarities
        def similar(a, b):
            return SequenceMatcher(None, a, b).ratio()

        col = 'town'
        msqdf['town suggest'] = None
        msqdf['town val'] = None
        msqdf['uid'] = None
        for idx in msqdf[col].index:
            a = msqdf.loc[idx, col]
            if pd.isna(a):
                continue
            bs = towndf['town'].values.tolist()
            uids = towndf['uid'].values.tolist()
            bv = [similar(a, b) for b in bs]
            opt_idx = np.argmax(bv)
            msqdf.loc[idx, ['town suggest', 'town val', 'uid']] = bs[opt_idx], bv[opt_idx], uids[opt_idx]

        msqdf['match'] = msqdf['town val'] > 0.86

        xdf = msqdf.loc[msqdf['match']]
        df = pd.DataFrame(columns=['town', 'mosque', 'real town', 'uid'], index=xdf.index)
        df.loc[xdf.index] = xdf[['town', 'mosque', 'town suggest', 'uid']].to_numpy()

        # msqdf = msqdf[~msqdf['match']]
        msqdf = msqdf.sort_values(by='town val', ascending=False)
        g = df.groupby(by='town')
        odf = pd.DataFrame(index=df['town'].unique(), columns=['uid', 'Mosques'])
        odf['Mosques'] = g['uid'].count()
        odf['uid'] = g['uid'].agg(pd.Series.mode)
        odf.to_csv(mosque_path, encoding='utf-8-sig')

    section_police = False
    if section_police:
        path = "C:\school\PoliticalShapley\statistics\data\lms\crime police.csv"
        root_path = os.path.dirname(path)
        police_path = os.path.join(root_path, f'police_info.csv')

        section_clean_police = True
        if section_clean_police:
            df = pd.read_csv(path, encoding='utf-8-sig')
            df = df.loc[:, ~df.isna().all()]
            remove_towns = ['-', 'Total', 'אחר']
            df = df[~df['יישוב'].isin(remove_towns)]

            cols = ['יישוב',
                    'תיאור עבירה',
                    'סה"כ',
                    ]
            df = df[cols]

            type_of_crime = [t for t in df['תיאור עבירה'].unique().tolist() if (~pd.isna(t))][1:]
            cols = ['town'] + type_of_crime
            towns = [t for t in df['יישוב'].unique().tolist() if t != '-' and ~pd.isna(t) and t != 'Total']
            idx = towns
            xdf = pd.DataFrame(columns=cols, index=idx)
            xdf['town'] = xdf.index.to_list()

            for c in type_of_crime:
                m = df[df['תיאור עבירה'].eq(c)]
                m = m.set_index('יישוב')
                m = m['סה"כ']
                xdf.loc[m.index, c] = m.values
            xdf = xdf.drop('town', axis=1)

            for c in xdf.columns:
                xdf[c] = xdf[c].fillna('0').str.replace(',', '').astype(int)
            xdf = xdf.drop('-', axis=1)

            xdf.to_csv(police_path, encoding='utf-8-sig')

        section_add_code = True
        if section_add_code:
            policedf = pd.read_csv(police_path, encoding='utf-8-sig', index_col=0)
            towncode_path = r"C:\school\PoliticalShapley\statistics\Results\votes distributions.csv"
            towndf = pd.read_csv(towncode_path, encoding='utf-8-sig')
            towndf = towndf[['town', 'uid']]

            policedf['town'] = policedf.index.values
            policedf['town'] = policedf['town'].replace('מועצה אזורית ', '')

            police_set = set(policedf['town'])
            print(f"Original police towns: {len(police_set)}")
            vote_set = set(towndf['town'])

            common = police_set.intersection(vote_set)
            only_in_police = police_set.difference(vote_set)
            only_in_vote = vote_set.difference(police_set)
            print(f"Already solved: {len(common)}\tTo solve: {len(only_in_police)}")

            r = pd.DataFrame(index=range(max(len(only_in_police), len(only_in_vote))))
            r.loc[range(len(only_in_police)), 'police'] = sorted(list(only_in_police))
            r.loc[range(len(only_in_vote)), 'vote'] = sorted(list(only_in_vote))

            def similar(a, b):
                return SequenceMatcher(None, a, b).ratio()

            r['Suggest'] = None
            r['val'] = None
            for idx in r['police'].index:
                a = r.loc[idx, 'police']
                if pd.isna(a):
                    continue
                bs = r['vote'].values.tolist()
                bv = [similar(a, b) for b in bs]
                opt_idx = np.argmax(bv)
                r.loc[idx, ['Suggest', 'val']] = bs[opt_idx], bv[opt_idx]
            r = r[['police', 'Suggest', 'val']]
            r = r.sort_values(by=['val'], ascending=False)
            r = r[~r['police'].isna()]

            r = r[r['police'].isin(["ע'ג'ר", 'ברנר']) | (r['val'] >= 0.8)]
            r = r[~r['police'].isin(["שער הנגב", "תמר"])]
            convertion = {r['police']: r['Suggest'] for _, r in r.iterrows()}

            for k, v in convertion.items():
                idxs = policedf['town'].eq(k)
                policedf.loc[idxs, 'town'] = v

            policedf = policedf.merge(towndf, left_on='town', right_on='town', how='outer')
            policedf = policedf[~policedf['עבירות בטחון'].isna()]
            policedf = policedf[~policedf['uid'].isna()]

            common = police_set.intersection(vote_set)
            only_in_police = police_set.difference(vote_set)
            print(f"solved after adaption: {policedf['town'].unique().shape[0]}")
            policedf.to_csv(police_path, encoding='utf-8-sig')

if __name__ == '__main__':
    print("END OF CODE.")


#!/usr/bin/env python
#  -*- coding: utf-8 -*-
__author__ = "Ringo"

'''
自动扶梯 策略 (难度：初级)
参考: https://www.shinnytech.com/blog/escalator/
注: 该示例策略仅用于功能示范, 实盘时请根据自己的策略/经验进行修改
'''

from tqsdk import TqApi, TqAuth, TargetPosTask
from tqsdk.ta import MA

# 设置合约
SYMBOL = "SHFE.rb2012"
# 设置均线长短周期
MA_SLOW, MA_FAST = 8, 40

api = TqApi(auth=TqAuth("快期账户", "账户密码"))
klines = api.get_kline_serial(SYMBOL, 60 * 60 * 24)
quote = api.get_quote(SYMBOL)
position = api.get_position(SYMBOL)
target_pos = TargetPosTask(api, SYMBOL)

# K线收盘价在这根K线波动范围函数

def kline_range(num):
    kl_range = (klines.iloc[num].close - klines.iloc[num].low) / \
               (klines.iloc[num].high - klines.iloc[num].low)
    return kl_range

# 获取长短均线值
def ma_caculate(klines):
    ma_slow = MA(klines, MA_SLOW).iloc[-1].ma
    ma_fast = MA(klines, MA_FAST).iloc[-1].ma
    return ma_slow, ma_fast

ma_slow, ma_fast = ma_caculate(klines)
print("慢速均线为%.2f,快速均线为%.2f" % (ma_slow, ma_fast))

while True:
    api.wait_update()
    # 每次k线更新，重新计算快慢均线
    if api.is_changing(klines.iloc[-1], "datetime"):
        ma_slow, ma_fast = ma_caculate(klines)
        print("慢速均线为%.2f,快速均线为%.2f" % (ma_slow, ma_fast))

    if api.is_changing(quote, "last_price"):
        # 开仓判断
        if position.pos_long == 0 and position.pos_short == 0:
            # 计算前后两根K线在当时K线范围波幅
            kl_range_cur = kline_range(-2)
            kl_range_pre = kline_range(-3)
            # 开多头判断，最近一根K线收盘价在短期均线和长期均线之上，前一根K线收盘价位于K线波动范围底部25%，最近这根K线收盘价位于K线波动范围顶部25%
            if klines.iloc[-2].close > max(ma_slow, ma_fast) and kl_range_pre <= 0.25 and kl_range_cur >= 0.75:
                print("最新价为:%.2f 开多头" % quote.last_price)
                target_pos.set_target_volume(100)

            # 开空头判断，最近一根K线收盘价在短期均线和长期均线之下，前一根K线收盘价位于K线波动范围顶部25%，最近这根K线收盘价位于K线波动范围底部25%
            elif klines.iloc[-2].close < min(ma_slow, ma_fast) and kl_range_pre >= 0.75 and kl_range_cur <= 0.25:
                print("最新价为:%.2f 开空头" % quote.last_price)
                target_pos.set_target_volume(-100)
            else:
                print("最新价位:%.2f ，未满足开仓条件" % quote.last_price)

        # 多头持仓止损策略
        elif position.pos_long > 0:
            # 在两根K线较低点减一跳，进行多头止损
            kline_low = min(klines.iloc[-2].low, klines.iloc[-3].low)
            if klines.iloc[-1].close <= kline_low - quote.price_tick:
                print("最新价为:%.2f,进行多头止损" % (quote.last_price))
                target_pos.set_target_volume(0)
            else:
                print("多头持仓，当前价格 %.2f,多头离场价格%.2f" %
                      (quote.last_price, kline_low - quote.price_tick))

        # 空头持仓止损策略
        elif position.pos_short > 0:
            # 在两根K线较高点加一跳，进行空头止损
            kline_high = max(klines.iloc[-2].high, klines.iloc[-3].high)
            if klines.iloc[-1].close >= kline_high + quote.price_tick:
                print("最新价为:%.2f 进行空头止损" % quote.last_price)
                target_pos.set_target_volume(0)
            else:
                print("空头持仓，当前价格 %.2f,空头离场价格%.2f" %
                      (quote.last_price, kline_high + quote.price_tick))


import json

file = open('weather.txt', 'r')
rows = file.readlines()

print(len(rows))
Table = []
for row in rows:
	Table.append([int(row[2:5]), int(row[6:14]), int(row[15:20]), int(row[21:26]), int(row[27:])])

print(Table[:10])
Names = ['Station', 'Date', 'Temp', 'RainTime', 'RainAmmount']

data = []
for i in range(0, len(Table)):
	month = {}
	for j in range(0, len(Names)):
		month[Names[j]] = Table[i][j]
	data.append(month)

with open('weather.json', 'w') as outfile:
    json.dump(data, outfile)


"""
Write a Python function that checks whether a passed string is palindrome or not.

Note: A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nursesrun.

Input Format:

Input_String containing String to be tested if palindrome or not

Output Format:

palindrome or not palindome as given in the description

Sample Input 0:

madam
Sample Output 0:

palindrome
Sample Input 1:

edyoda
Sample Output 1:

not palindrome
"""

Input_String = input()

def check_palindrome(Input_String):
# Write your code here
	if Input_String[:] == Input_String[::-1]:
		return "palindrome"
	else:
		return "not palindrome"

print(check_palindrome(Input_String))


# -*- coding:utf-8 -*-
'''
Created on 2017年11月14日

@author: 任胜勇

这是一个关于清晰经纬度的数据
现在有两种方法
现有的数据是每个商铺对应用户的id,经纬度
1.
    用用户的经纬度找一个二维平面的最高区间，保留边界用户，删除用户
2.
    以商铺的经纬度为中心画圆，寻找一个最合适的半径作为范围划分
3.
    计算信息与商店的距离，去掉一定数目距离的大值（认为异常）
我采用了第三种方法
'''

import os
import numpy as np
import pandas as pd
from tqdm import tqdm
from math import radians, cos, sin, asin, sqrt

def haversine(lon1, lat1, lon2, lat2):  # 经度1，纬度1，经度2，纬度2 （十进制度数）
    """
    Calculate the great circle distance between two points
    on the earth (specified in decimal degrees)
    """
    # 将十进制度数转化为弧度
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])

    # haversine公式
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
    c = 2 * asin(sqrt(a))
    r = 6371  # 地球平均半径，单位为公里
    return c * r * 1000

# 含有shop对应用户数据的pd文件路径
pd_path = r'G:\customer&fixed-position\data\shop\person2shop/'
# 保存了shop路径的文件
npy_path = r'G:\customer&fixed-position\data\shop\shop_lc/'
shop_list = os.listdir(pd_path)

shop_lc = os.listdir(npy_path)
shop_dis = dict();
dis_sum = 0;
dis_real_sum = 0;
shop_dis_real = dict()
for sp in tqdm(shop_list):

    pr_dis = dict()
    pr_dis['dis'] = [];
    pr_dis['time_stamp'] = [];
    pr_dis['user_id'] = []
    pr_dis['longitude'] = [];
    pr_dis['latitude'] = []
    shop_lc = np.load(npy_path + sp.split('.')[0] + '.npy')
    # 获取店铺的经纬度
    s_lg = shop_lc[0];
    s_lt = shop_lc[1]
    shop_data = pd.read_csv(pd_path + sp)
    # 初始化，新增distance
    shop_data['distance_sq'] = 999
    # 计算data距离

    for _, sp_data in shop_data.iterrows():
        lg = sp_data.longitude;
        lt = sp_data.latitude
        dis = haversine(lg, lt, shop_lc[0], shop_lc[1])
        pr_dis['dis'].append(dis)
        pr_dis['time_stamp'].append(sp_data.time_stamp)
        pr_dis['user_id'].append(sp_data.user_id)
        # 经纬度有小数点记录不精确，使用字符串记录
        pr_dis['longitude'].append(str(sp_data.longitude))
        pr_dis['latitude'].append(str(sp_data.latitude))

    df = pd.DataFrame({'user_id': pr_dis['user_id'],
                       'dis': pr_dis['dis'],
                       'time_stamp': pr_dis['time_stamp'],
                       'longitude': pr_dis['longitude'],
                       'latitude': pr_dis['latitude']})
    df.to_csv(r'G:\customer&fixed-position\data\shop\shop_dis/' + sp.split('.')[0] + '.csv')


import requests
import re
import os
url='http://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=detail&fr=&sf=1&fmq=1447473655189_R&pv=&ic=0&nc=1&z=&se=&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=%E9%95%BF%E8%80%85%E8%9B%A4'
html = requests.get(url).text
urls = re.findall('"objURL":"(.*?)"', html)
if not os.path.isdir('C:\\Users\\wangz\\Desktop\\dytt'):
    os.mkdir('C:\\Users\\wangz\\Desktop\\dytt')

index = 1
for url in urls:
    print("Doading:",url)
    res=requests.get(url)
    filename = os.path.join('C:\\Users\\wangz\\Desktop\\dytt', str(index) + ".jpg")
    with open(filename, 'wb') as f:
        f.write(res.content)
        index += 1


import os.path
import time
from urllib.parse import urlencode
import requests
import json
import pandas as pd
from datetime import datetime
from tqdm import tqdm
import numpy as np

def json_to_columns(row):
    # convert json to dictionary
    if row is None:
        row=[]
    if len(row)>0:
        data = row[0]
        # creating new columns for each key in JSON
        temp_row = {}
        for key in data:
            temp_row['items.' + key] = data[key]
    else:

        temp_row = {}
    return pd.Series(temp_row)

class API_regu:
    """Base class for api requests"""

    def __init__(self, url='', token='token.txt'):
        self.url = url
        with open(token, "r", encoding='utf8') as f:
            token = f.readline()
            print(token)
        self.headers = {
            'accept': '*/*',
            'Authorization': token
        }
        self.pages = None
        self.temp_frame = None

    def orders_by_page(self, page=1):
        """
            Requesting orders from api by page sorted by purchase date in descending order.

            Args:
                page (int): number of page for api request
            Returns:
                temp_frame (dataframe): result frame of requested data
        """
        self.temp_frame = pd.DataFrame()
        sort = '?sort=-purchase_date&'
        pages = 'page=' + str(page)
        url = self.url + 'order' + sort + pages
        response = requests.get(url=url, headers=self.headers, timeout=30)
        json_data = response.json()
        df = pd.json_normalize(json_data)
        if response.status_code != 200:
            print(response.json())
        # applying a function to a dataframe
        df_temp = df['items'].apply(json_to_columns)
        df_temp['items.p_id'] = df_temp['items.p_id'].fillna(0)
        df['is_business_order'] = df['is_business_order'].fillna(0)
        temp_frame = pd.concat([df, df_temp], axis=1)
        temp_frame = temp_frame.drop(['items', 'items.product'], axis=1)
        df_temp = df['shipments'].apply(json_to_columns)
        df_temp = df_temp.replace({np.nan: None})
        temp_frame['shipment_date']=df_temp['items.shipment_date']
        temp_frame['fulfillment_center']=df_temp['items.shipmentOptions'].apply(lambda x: x.get('fulfillment_center') if x is not None else None)
        temp_frame.set_index('id', inplace=True)
        return temp_frame

    def products_by_page(self, page=1):
        """
            Requesting products from api by page.

            Args:
                page (int): number of page for api request
            Returns:
                temp_frame (dataframe): result frame of requested data
        """
        self.temp_frame = pd.DataFrame()
        pages = '?page=' + str(page)
        url = self.url + 'product' + pages + '&per-page=50'
        response = requests.get(url=url, headers=self.headers)
        print(response.status_code)
        json_data = response.json()
        df = pd.json_normalize(json_data)
        self.pages = response.headers.get('X-Pagination-Page-Count')

        # applying a function to a dataframe
        temp_frame = df
        temp_frame.set_index('id', inplace=True)
        return temp_frame

    def company_by_page(self, page=1):
        """
            Requesting companies from api by page.

            Args:
                page (int): number of page for api request
            Returns:
                temp_frame (dataframe): result frame of requested data
        """
        self.temp_frame = pd.DataFrame()
        pages = '?page=' + str(page)
        url = self.url + 'company' + pages + '&per-page=50'
        response = requests.get(url=url, headers=self.headers)
        print(response.status_code)
        json_data = response.json()
        df = pd.json_normalize(json_data)
        self.pages = response.headers.get('X-Pagination-Page-Count')

        # applying a function to a dataframe
        temp_frame = df
        temp_frame.set_index('id', inplace=True)
        start_index = url.index('https://') + len('https://')
        end_index = url.index('.ysell')
        temp_frame['ysell_pack'] = self.url[start_index:end_index]
        return temp_frame

    def orders_req(self, start=1, end=10):
        """
            Requesting set of pages containing information about orders.

            Args:
                start (int): number of page to start from
                end (int): number of page to end
            Returns:
                final_frame (dataframe): result frame of requested data from all pages
        """
        final_frame = pd.DataFrame()
        error_pages = []  # List for storing error pages
        total_pages = end - start + 1
        self.temp_frame = pd.DataFrame()

        with tqdm(total=total_pages) as pbar:
            for page in range(start, end + 1):
                try:
                    final_frame = pd.concat([final_frame, self.orders_by_page(page=page)])
                except Exception as e:
                    # Error Handling
                    print(f"Ошибка на странице {page}: {e}")
                    error_pages.append(page)

                pbar.update(1)  # Increase progress bar by 1

        # Additional request for error pages
        if len(error_pages) > 0:
            time.sleep(30)
            for page in error_pages:
                try:
                    final_frame = pd.concat([final_frame, self.orders_by_page(page=page)])
                except Exception as e:
                    print(f"Ошибка при повторном опросе страницы {page}: {e}")

        # final_frame['id']=final_frame.index
        final_frame['shipments'] = final_frame['shipments'].astype(str)
        final_frame['services'] = final_frame['services'].astype(str)

        return final_frame

    def product_req(self, load_all=True, start=1, end=5):
        """
            Requesting set of pages containing information about products.

            Args:
                load_all (bool): flag for loading all pages (True)/ exact set of pages (False)
                start (int): number of page to start from
                end (int): number of page to end

            Returns:
                final_frame (dataframe): result frame of requested data from all pages
        """
        final_frame = pd.DataFrame()
        page = start
        while page <= end:
            final_frame = pd.concat([final_frame, self.products_by_page(page=page)])
            if load_all and page == start:
                end = int(self.pages)
            page += 1
        return final_frame

    def company_req(self, load_all=True, start=1, end=3):
        """
            Requesting set of pages containing information about companies.

            Args:
                load_all (bool): flag for loading all pages (True)/ exact set of pages (False)
                start (int): number of page to start from
                end (int): number of page to end

            Returns:
                final_frame (dataframe): result frame of requested data from all pages
        """
        final_frame = pd.DataFrame()
        page = start
        while page <= end:
            final_frame = pd.concat([final_frame, self.company_by_page(page=page)])
            if load_all and page == start:
                end = int(self.pages)
            page += 1
        return final_frame

# test = API_regu()
# test_data = test.orders_req(start=8300, end=8700)
# test_data.to_excel('test_data.xlsx')
# test.product_req()

class CRM_req:
    """Base class for CRM system connection"""

    def __init__(self, token='token_b.txt'):

        with open(token, "r", encoding='utf8') as f:
            link_and_token=f.readline().split(';')
            token = link_and_token[1]
            print(token)
        self.url = link_and_token[0]
        self.headers = {'Authorization': token}

    def prod_base_req(self):
        """
            Requesting information from CRM about products.

            Args:
                None
            Returns:
                df (dataframe): result frame of requested data
        """
        response = requests.get(url=self.url, timeout=30, verify=False, headers=self.headers)
        json_data = response.json()
        df = pd.json_normalize(json_data)
        df.rename(columns={'id': 'bitrix_id'}, inplace=True)
        # df['sku_fnsku'] = df['sku_fnsku'].fillna('нет / нет')
        df[['sku', 'fnsku']] = df['sku_fnsku'].str.strip().str.split(' / ', expand=True)
        df['sku_fnsku_count'] = df['sku_fnsku_count'].str.split('/', expand=True)[0]
        df['product_name'] = df['product_name'] + ' / ' + df['sku_fnsku_taste']

        # Удалить исходную колонку, если это необходимо
        df.drop(columns=['sku_fnsku'], inplace=True)

        return df

class Keepa_req:
    """ Work in progress"""

    def __init__(self):
        self.url = 'https://api.keepa.com/'
        with open('token_keepa.txt', "r", encoding='utf8') as f:
            token = f.readline()
            print(token)
        self.access_key = token
        self.pages = None
        self.temp_frame = None

    def req(self):
        url = f"{self.url}token?key={self.access_key}"
        response = requests.get(url)
        print(response.text)

    def category_req(self, category=0, filepath=None):
        url = f"{self.url}category?key={self.access_key}&domain={1}&category={category}&parents={0}"
        response = requests.get(url)
        json_data = response.json()['categories']
        df = pd.DataFrame.from_dict(json_data, orient='index')
        if filepath is not None:
            df.to_excel(filepath + '\\category_req.xlsx')
        else:
            df.to_excel('category_req.xlsx')

    def category_search(self, search="", filepath=None):
        url = f"{self.url}search?key={self.access_key}&domain={1}&type=category&term={search}"
        response = requests.get(url)
        json_data = response.json()['categories']
        df = pd.DataFrame.from_dict(json_data, orient='index')
        if filepath is not None:
            df.to_excel(filepath + '\\category_req.xlsx')
        else:
            df.to_excel('category_req.xlsx')

    def product_req(self, asin, filepath=None):
        if type(asin) is list:
            asin = ','.join(asin)
        data = {
            'asin': asin,
            'stats': 180,
        }
        encoded_data = urlencode(data)
        url = f"{self.url}product?key={self.access_key}&domain={1}&{encoded_data}"
        response = requests.get(url)
        json_data = response.json()['product']
        df = pd.DataFrame.from_dict(json_data, orient='index')
        if filepath is not None:
            df.to_excel(filepath + '\\category_req.xlsx')
        else:
            df.to_excel('product_req.xlsx')

    def product_search(self, search={}, filepath=None):
        encoded_data = urlencode(search)
        url = f"{self.url}search?key={self.access_key}&domain={1}&type=product&term={search}&stats=180"
        response = requests.get(url)
        json_data = response.json()['categories']
        df = pd.DataFrame.from_dict(json_data, orient='index')
        if filepath is not None:
            df.to_excel(filepath + '\\category_req.xlsx')
        else:
            df.to_excel('product_search.xlsx')

    def product_finder(self, search={}, filepath=None):
        encoded_data = urlencode(search)
        url = f"{self.url}query?key={self.access_key}&domain={1}&selection={search}"
        response = requests.get(url)
        json_data = response.json()['categories']
        df = pd.DataFrame.from_dict(json_data, orient='index')
        if filepath is not None:
            df.to_excel(filepath + '\\category_req.xlsx')
        else:
            df.to_excel('product_finder.xlsx')

    def bsr_req(self, category: int = 0, range: int = 30, filepath=None):
        url = f"{self.url}bestsellers?key={self.access_key}&domain={1}&category={category}&range={range}"
        response = requests.get(url)
        json_data = response.json()['categories']
        df = pd.DataFrame.from_dict(json_data, orient='index')
        if filepath is not None:
            df.to_excel(filepath + '\\category_req.xlsx')
        else:
            df.to_excel('bsr_req.xlsx')
# ['categories']
# test2=Keepa_req()
# test2.req()
# # test2.category_req()
# test2.category_search('health')


# %% [markdown] [4]:

"""
Cribbed from key_and_query_projection.py
"""

import ast
from transformer_lens.cautils.notebook import *
from transformer_lens.rs.callum2.utils import project, get_effective_embedding
from transformer_lens.rs.arthurs_notebooks.arthurs_utils import *
import argparse

#%%

model = HookedTransformer.from_pretrained(
    "gpt2-small",
    center_unembed=True,
    center_writing_weights=True,
    fold_ln=True,
    refactor_factored_attn_matrices=False,
)
model.set_use_attn_result(True)

# %%

MAX_SEQ_LEN = 512 # half of 1024 as
BATCH_SIZE = 30
batched_tokens, targets = get_filtered_webtext(model, batch_size=BATCH_SIZE, seed=1727, device="cuda", max_seq_len=MAX_SEQ_LEN)
effective_embeddings = get_effective_embedding(model, use_codys_without_attention_changes=False)

# %%

# Find the top 5% of things by importance
# Do this crap
# See change in loss

# NEGATIVE_LAYER_IDX, NEGATIVE_HEAD_IDX = NEG_HEADS[model.cfg.model_name]
# NEGATIVE_HEAD_IDX, NEGATIVE_LAYER_IDX = 9, 9
for NEGATIVE_LAYER_IDX, NEGATIVE_HEAD_IDX in [(10, 0), (11, 10)] + list(itertools.product(range(11, -1, -1), range(12))):

    END_STATE_HOOK = f"blocks.{model.cfg.n_layers-1}.hook_resid_post"
    names_filter1 = (
        lambda name: name == END_STATE_HOOK
        or name==f"blocks.{NEGATIVE_LAYER_IDX}.hook_resid_pre"
        or name==f"blocks.{NEGATIVE_LAYER_IDX}.attn.hook_result"
        or name==f"blocks.{NEGATIVE_LAYER_IDX}.attn.hook_attn_scores"
    )
    model = model.to("cuda:1")
    logits, cache = model.run_with_cache(
        batched_tokens.to("cuda:1"),
        names_filter=names_filter1,
        device="cpu",
    )
    model = model.to("cuda:0")
    cache.to("cuda:0")
    print("Done")
    cpu_logits = logits.cpu()
    del logits
    gc.collect()
    torch.cuda.empty_cache()

    # %%

    original_end_state = cache[get_act_name("resid_post", model.cfg.n_layers-1)]

    batched_tokens_loss = get_metric_from_end_state(
        model=model,
        end_state=original_end_state,
        targets=targets,
    )

    #%%

    head_output = cache[get_act_name("result", NEGATIVE_LAYER_IDX)][:, :, NEGATIVE_HEAD_IDX]
    assert head_output.shape == (BATCH_SIZE, MAX_SEQ_LEN, model.cfg.d_model)

    #%%

    if ipython is not None:
        unembed = einops.einsum(
            head_output,
            model.W_U,
            "b s d_model, d_model d_vocab -> b s d_vocab",
        )

    #%%

    if ipython is not None:
        the_topk = torch.topk(
            -unembed,
            k=10,
            dim=-1,
        ).indices

    #%%

    mean_head_output = einops.reduce(head_output, "b s d -> d", reduction="mean")

    #%%

    mean_ablated_end_states = cache[get_act_name("resid_post", model.cfg.n_layers-1)] - head_output + einops.repeat(mean_head_output, "d -> b s d", b=BATCH_SIZE, s=MAX_SEQ_LEN)
    mean_ablated_loss = get_metric_from_end_state(
        model=model,
        end_state=mean_ablated_end_states,
        targets=targets,
    )

    # %%

    max_importance_examples = sorted(
        [
            (
                batch_idx,
                seq_idx,
                (mean_ablated_loss-batched_tokens_loss)[batch_idx, seq_idx].item(),
            )
            for batch_idx, seq_idx in itertools.product(
                range(BATCH_SIZE), range(MAX_SEQ_LEN)
            )
        ],
        key=lambda x: x[2],
        reverse=True,
    )

    # %%

    # Get the top 5% of things by importance

    TOP5P_BATCH_SIZE = len(max_importance_examples) // 20
    all_top_5_percent = max_importance_examples[:TOP5P_BATCH_SIZE]

    np.random.seed(1827)
    np.random.shuffle(all_top_5_percent)

    top5p_batch_indices = [x[0] for x in all_top_5_percent]
    top5p_seq_indices = [x[1] for x in all_top_5_percent]

    #%%

    top5p_tokens = batched_tokens[top5p_batch_indices]
    top5p_targets = torch.LongTensor([targets[top5p_batch_idx, top5p_seq_idx] for top5p_batch_idx, top5p_seq_idx in zip(top5p_batch_indices, top5p_seq_indices)])

    #%%

    top5p_losses = batched_tokens_loss[top5p_batch_indices, top5p_seq_indices]

    # %%

    # Do the key-side thing where we project onto W_U

    keyside_projections = t.zeros((BATCH_SIZE, MAX_SEQ_LEN, model.cfg.d_model))
    keyside_orthogonals = t.zeros((BATCH_SIZE, MAX_SEQ_LEN, model.cfg.d_model))

    for batch_idx, seq_idx in tqdm(list(itertools.product(range(BATCH_SIZE), range(MAX_SEQ_LEN)))):
        keyside_vector, keyside_orthogonal = project(
            cache[get_act_name("resid_pre", NEGATIVE_LAYER_IDX)][batch_idx, seq_idx],
            effective_embeddings["W_E (including MLPs)"][batched_tokens[batch_idx, seq_idx]],
        )
        keyside_projections[batch_idx, seq_idx] = keyside_vector
        keyside_orthogonals[batch_idx, seq_idx] = keyside_orthogonal

    #%%

    queryside_vectors = t.ones((TOP5P_BATCH_SIZE, model.cfg.d_model)).cuda() * (-420)
    all_queryside_components = [] # t.ones((TOP5P_BATCH_SIZE, MAX_SEQ_LEN+1)) * (-420)
    NORMY=False
    for batch_batch_idx, (top5p_batch_idx, top5p_seq_idx) in tqdm(list(enumerate(list(zip(top5p_batch_indices, top5p_seq_indices))))):
        t.cuda.empty_cache()

        my_direction_indices = list([batched_tokens[top5p_batch_idx, earlier_seq_idx].item() for earlier_seq_idx in range(top5p_seq_idx+1)])
        assert len(my_direction_indices) == top5p_seq_idx+1
        my_directions = torch.stack([model.W_U.T[my_direction_idx] for my_direction_idx in my_direction_indices], dim=0)

        all_queryside_components.append(einops.einsum(
            cache[get_act_name("resid_pre", NEGATIVE_LAYER_IDX)][top5p_batch_idx, top5p_seq_idx],
            my_directions,
            "d_model, s d_model -> s",
        ))

        # queryside_vector, queryside_orthogonal, queryside_component = project(
        #     cache[get_act_name("resid_pre", NEGATIVE_LAYER_IDX)][top5p_batch_idx, top5p_seq_idx],
        #     dir=my_directions,
        #     return_component=True,
        # )
        # queryside_vectors[batch_batch_idx] = queryside_vector
        # assert len(queryside_component) == len(my_direction_indices) # number of distinct tokens

        # if NORMY:
        #     queryside_norms = [model.W_U.T[batched_tokens[top5p_batch_idx, earlier_seq_idx]].norm(dim=0).item() for earlier_seq_idx in range(top5p_seq_idx+1)]
        #     queryside_norms = torch.tensor(queryside_norms)
        #     assert queryside_component.shape == queryside_norms.shape
        #     queryside_components.append(queryside_component * queryside_norms.cuda())
        # else:
        #     queryside_components.append([queryside_component[my_directions_lookup[idx]] for idx in range(top5p_seq_idx+1)])

        # # warnings.warn("Another lock on")
        # # queryside_vectors[batch_batch_idx] = model.W_U.T[top5p_tokens[batch_idx, seq_idx]]

    #%%

    # all_queryside_norms = []
    # for batch_batch_idx, (top5p_batch_idx, top5p_seq_idx) in tqdm(list(enumerate(list(zip(top5p_batch_indices, top5p_seq_indices))))):
    #     queryside_norms = [model.W_U.T[batched_tokens[top5p_batch_idx, earlier_seq_idx]].norm(dim=0).item() for earlier_seq_idx in range(top5p_seq_idx+1)]
    #     queryside_components[batch_batch_idx] = torch.tensor(queryside_components[batch_batch_idx])
    #     if NORMY:
    #         queryside_components[batch_batch_idx] /= torch.tensor(queryside_norms)

    #     # queryside_norms = torch.tensor(queryside_norms)
    #     # assert queryside_component.shape == queryside_norms.shape
    #     # queryside_components.append(queryside_component * queryside_norms.cuda())

    #%%

    fig = go.Figure()
    colors = px.colors.qualitative.Plotly
    colors = colors * 1000

    def topn(x, y, n=1):
        """Returns whether the top x is in the top n y points"""
        y = sorted(list(enumerate(y)), reverse=True, key=lambda x: x[1])
        x = sorted(list(enumerate(x)), reverse=True, key=lambda x: x[1])
        yinds = [yind for yind, _ in y]
        return x[0][0] in yinds[:n]

    cnt = 0
    for i in range(100):
        x = (cache[get_act_name("attn_scores", NEGATIVE_LAYER_IDX)][top5p_batch_indices[i], NEGATIVE_HEAD_IDX, top5p_seq_indices[i], :top5p_seq_indices[i]+1]).cpu()
        y = torch.tensor(all_queryside_components[i]).cpu()
        if topn(y, x, n=5):
            cnt+=1

        if False:
            fig.add_trace(
                go.Scatter(
                    x=x,
                    y=y,
                    mode="markers",
                    text=[(j, model.to_str_tokens(batched_tokens[top5p_batch_indices[i], max(0,j-1):j+2])) for j in range(top5p_seq_indices[i]+1)],
                    marker=dict(
                        color=colors[i],
                    ),
                )
            )

    # # read the existing data
    MY_FNAME = "../arthur/json_data/more_corr.json"
    with open(MY_FNAME, "r") as f:
        cur_json = json.load(f)

    mean_ablation_bad =  (mean_ablated_loss[top5p_batch_indices, top5p_seq_indices] - top5p_losses).tolist()

    # update the data
    cur_json[str((NEGATIVE_LAYER_IDX, NEGATIVE_HEAD_IDX))] = {
        "layer_idx": NEGATIVE_LAYER_IDX,
        "head_idx": NEGATIVE_HEAD_IDX,
        "cnt": cnt,
    }

    # write the data
    # write the updated data (don't overwrite!)
    with open(MY_FNAME, "w") as f:
        f.write(json.dumps(cur_json, indent=4))

    print(cnt/100)
    if False:
        fig.show()

# %%

# # read the existing data
MY_FNAME = "../arthur/json_data/more_corr.json"
with open(MY_FNAME, "r") as f:
    cur_json = json.load(f)

# %%

percents = t.zeros(12, 12)

# %%

data=[]

for k in cur_json.keys():
    layer_idx, head_idx = ast.literal_eval(k)
    if layer_idx>6:
        data.append(((layer_idx, head_idx), cur_json[k]["cnt"] / 100))

# %%

data = sorted(data, key=lambda x: x[1], reverse=True)
# %%

px.bar(
    x=[f"{layer_idx}, {head_idx}" for (layer_idx, head_idx), _ in data],
    y=[cnt for _, cnt in data],
    title="Percentage of the important prompts where the top in-context unembedding is one of the top 5 attention scores",
    text=[f"{cnt*100:.2f}%" for _, cnt in data],
    color = ["red" if i<2 else "blue" for i, cnt in enumerate(data)],
    labels={
        "x": "Layer, Head",
        "y": "Percentage",
    },
    height=500,
    # width=1000,
).update_layout(
    # font=dict(
    #     size=20,
    # ),
    xaxis=dict(
        tickangle=45,
    ),
    # yaxis=dict(
    #     tickformat="%",
    # ),
).show()

# %%


from django.conf.urls import url
from .views import PersonaDetailView, PersonaListView, ResponsableListView
from .views import InstitucionListView
from .views import ParticipanteListView

urlpatterns = [
    url(r'^responsables/$', ResponsableListView.as_view(),
        name='responsable-list'),
    url(r'^personas/$', PersonaListView.as_view(), name='personas-list'),
    url(r'^personas/(?P<id>\d+)/$', PersonaDetailView.as_view(),
        name='personas-detail'),
    url(r'^instituciones/$', InstitucionListView.as_view(),
        name='instituciones-list'),
    url(r'^participantes/$', ParticipanteListView.as_view(),
        name='participante-list'),
]


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

import os
import argparse
import multiprocessing

def filter_data(input_file, output_file):
    print('Process %s' % input_file)
    with open(input_file, 'r') as inf:
        with open(output_file, 'w') as of:
            for line in inf:
                try:
                    line.decode('utf-8')
                    of.write(line)
                except:
                    continue

def filter_data_list(input_list, output_folder, process_count=2):
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
    pool = multiprocessing.Pool(process_count)
    with open(input_list) as inf:
        for line in inf:
            line = line.strip()
            if line.startswith('#'):
                continue
            output_file_name = os.path.basename(line)
            output_file_path = os.path.join(output_folder, output_file_name)
            pool.apply_async(filter_data, (line, output_file_path))
    pool.close()
    pool.join()

def filter_data_folder(input_folder, output_folder, process_count=2):
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
    pool = multiprocessing.Pool(process_count)
    for root, _, files in os.walk(input_folder):
        for f in files:
            input_path = os.path.join(root, f)
            output_path = os.path.join(output_folder, f)
            pool.apply_async(filter_data, (input_path, output_path))
    pool.close()
    pool.join()

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--input-file', '-i', nargs='+', default=[])
    parser.add_argument('--output-file', '-o', default=[])
    parser.add_argument('--input-list', '-il', nargs='+', default=[])
    parser.add_argument('--input-folder', '-if', nargs='+', default=[])
    parser.add_argument('--output-folder', '-of', default='')
    parser.add_argument('--process-count', default=0)
    args = parser.parse_args()

    if not args.input_file and not args.input_list and not args.input_folder:
        parser.error('No input data.')
    if not args.output_file and not args.output_folder:
        parser.error('No output path.')
    if (args.input_list or args.input_folder) and not args.output_folder:
        parser.error('"--input-list" or "--input-folder" is set, '
                     'but missing "--output-folder".')
    if len(args.input_file) > 1 and not args.output_folder:
        parser.error('input files are more than one, '
                     'but missing "--output-folder".')

    cpu_core = multiprocessing.cpu_count() if not args.process_count else args.process_count

    if args.input_list:
        for input_list in args.input_list:
            filter_data_list(input_list, args.output_folder, cpu_core)
    if args.input_folder:
        for folder in args.input_folder:
            filter_data_folder(folder, args.output_folder, cpu_core)
    if len(args.input_file) == 1:
        if not os.path.exists(args.output_folder):
            os.makedirs(args.output_folder)
        filter_data(args.input_file[0], args.output_file)
    elif len(args.input_file) > 1:
        if not os.path.exists(args.output_folder):
            os.makedirs(args.output_folder)
        for f in args.input_file:
            file_name = os.path.basename(f)
            output_file_path = os.path.join(args.output_folder, file_name)
            filter_data(f, output_file_path)


#https://open.kattis.com/problems/sumkindofproblem

#input for iterations
n= int(input())
for i in range (0,n):
  #input for test number and amount of digits
  test,digits = input().split(' ')
  test = int(test)
  digits = int(digits)
  #formulas
  s1 = (1+digits)*digits/2
  s2 = (2+digits*2)*digits/2
  s3 = (1+digits*2-1)*digits/2
  #outputs
  print("%i %i %i %i" %(test,s1,s3,s2))


import re
import pytz
import datetime
import requests
from bs4 import BeautifulSoup

headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
        'Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.53'
    }

weekday = {
    'Monday': 0,
    'Tuesday': 1,
    'Wednesday': 2,
    'Thursday': 3,
    'Friday': 4,
    'Saturday': 5,
    'Sunday': 6,

    '一': 0,
    '二': 1,
    '三': 2,
    '四': 3,
    '五': 4,
    '六': 5,
    '日': 6
}

# Get current month and year
now = datetime.datetime.now().replace(second=0, microsecond=0)
curr_year = str(now.year)
curr_month = now.month

# Spring, summer, fall, winter season
def get_curr_season(eng = False):
    if curr_month >= 1 and curr_month <= 3:
        curr_season = (curr_year + '/winter') if eng else (curr_year + '01')
    elif curr_month >= 4 and curr_month <= 6:
        curr_season = (curr_year + '/spring') if eng else (curr_year + '04')
    elif curr_month >= 7 and curr_month <= 9:
        curr_season = (curr_year + '/summer') if eng else (curr_year + '07')
    elif curr_month >= 10 and curr_month <= 12:
        curr_season = (curr_year + '/fall') if eng else (curr_year + '10')

    return curr_season

# Convert anime start day & time from China Standard Time to local time zone
def to_local_time(day, time, zone = 'cst'):
    tz = pytz.timezone('Asia/Shanghai') if zone == 'cst' else pytz.timezone('Asia/Tokyo')
    web_time = datetime.datetime.now(tz).replace(second=0, microsecond=0, tzinfo=None)
    time_diff = int((now - web_time).total_seconds() / 3600)
    hour = int(time[:2]) + time_diff

    if hour < 0:
        hour = 24 + hour
        time = '0' + str(hour) + time[2:] if hour < 10 else str(hour) + time[2:]
        if day != 0:
            day -= 1
        else:
            day = 6
    else:
        time = '0' + str(hour) + time[2:] if hour < 10 else str(hour) + time[2:]

    return day, time

# Source: yuc.wiki, language: Chinese Simplified
def anime_chs():
    anime_list = []
    url = 'https://yuc.wiki/{}/'.format(get_curr_season())
    page_text = requests.get(url=url, headers=headers).content
    soup = BeautifulSoup(page_text, 'html.parser')
    animes = soup.find_all('div', {'style':'float:left'})

    for anime in animes:
        try:
            name = anime.find('td', class_=re.compile('^date_title.*')).text
            date = anime.find('p', {'class':'imgtext'}).text.split('~')[0] + '/' + curr_year
            time = anime.find('p', {'class':'imgep'}).text.split('~')[0]
            img = anime.find('img')['src']

            # Change date to weekday
            date = datetime.datetime.strptime(date, "%m/%d/%Y")
            day = weekday[date.strftime("%A")]

            # Move time >= 24:00 to the next day
            if int(time[:2]) >= 24:
                time = '0' + str(int(time[:2]) - 24) + time[2:]
                if day != 6:
                    day += 1
                else:
                    day = 0

            day, time = to_local_time(day, time)
            anime_list.append([name, day, time, img])
        except:
            pass

    return anime_list

# Source: acgsecrets.hk, language: Chinese Traditional
def anime_cht():
    anime_list = []
    url = 'https://acgsecrets.hk/bangumi/{}/'.format(get_curr_season())
    page_text = requests.get(url=url, headers=headers).content
    soup = BeautifulSoup(page_text, 'html.parser')
    content = soup.find('div', {'id':'acgs-anime-icons'})
    animes = content.find_all('div', recursive=False)

    for anime in animes:
        name = anime.find('div', {'class':'anime_name'}).text
        day = weekday[anime.find('div', {'class':'day'}).text]
        time = anime.find('div', {'class':'time'}).text
        img = anime.find('img', {'class':'img-fit-cover'})['src']

        day, time = to_local_time(day, time)
        anime_list.append([name, day, time, img])

    return anime_list

# Helper function for anime_eng()
# Get weekday and time for specific anime on myanimelist.net
def get_date_time(url):
    page_text = requests.get(url=url, headers=headers).content
    soup = BeautifulSoup(page_text, 'html.parser')
    content = soup.find('span', string=re.compile('Broadcast:')).next_sibling.strip().split()

    day = weekday[content[0][:-1]]
    time = content[2]
    zone = content[3][1:-1]

    return to_local_time(day, time, zone)

def anime_eng():
    anime_list = []
    url = 'https://myanimelist.net/anime/season/{}'.format(get_curr_season(True))
    page_text = requests.get(url=url, headers=headers).content
    soup = BeautifulSoup(page_text, 'html.parser')
    content = soup.find('div', {'class':'seasonal-anime-list js-seasonal-anime-list js-seasonal-anime-list-key-1'})
    animes = content.find_all('div', {'class':'js-anime-category-producer seasonal-anime js-seasonal-anime js-anime-type-all js-anime-type-1'})

    for anime in animes:
        name = anime.find('span', {'class':'js-title'}).text
        link = anime.find('a')['href']
        day, time = get_date_time(link)
        img_tag = anime.find('img')
        img = img_tag.get('src') or img_tag.get('data-src')

        anime_list.append([name, day, time, img])

    return anime_list

# Sort anime list based on weekday and time
def sort_key(anime_list):
    time = datetime.datetime.strptime(anime_list[2], '%H:%M')

    return (anime_list[1], time)

# Get anime list based on desired language
def get_anime(lang = 'chs'):
    if lang == 'chs':
        anime_list = anime_chs()
    elif lang == 'cht':
        anime_list = anime_cht()
    elif lang == 'eng':
        anime_list = anime_eng()

    anime_list = sorted(anime_list, key=sort_key)
    # for anime in anime_list:
    #     print(anime)
    return anime_list


n = int(input())
for _ in range(n):
    data = input().split()
    name= data[0]
    temp= data[1]
    postSecondStudieYear = temp[0:4]
    temp2= data[2]
    bithYear= temp2[0:4]
    course= data[3]

    if int(postSecondStudieYear) >= 2010 or int(bithYear) >= 1991:
        print(name+" "+"eligible")
    else:
        if int(course) > 40:
            print(name+" "+"ineligible")
        else:
            print(name+" "+"coach petitions")


def linear_search(array, target):
    index = 0
    found = False

    while index < len(array) and not found:
        if target == array[index]:
            found = True
            print(array[index], 'found at position', index+1)
        else:
            index += 1

states = ['Johor', 'Selangor', 'Perak', 'Penang', 'Kedah', 'Terengganu', 'Kelantan']
state = 'Penang'
linear_search(states, state)


from dataclasses import dataclass
from typing import List

from .hparams import HyperParams

@dataclass
class ROMEHyperParams(HyperParams):
    # Method
    layers: List[int]
    fact_token: str
    v_num_grad_steps: int
    v_lr: float
    v_weight_decay: float
    clamp_norm_factor: float
    kl_factor: float

    # Module templates
    rewrite_module_tmp: str
    mlp_module_tmp: str

    @classmethod
    def from_name(cls, name: str):
        data = dict(
            fact_token='subject_last',
            v_num_grad_steps=20,
            v_lr=1e-1,
            v_weight_decay=1e-2,
            clamp_norm_factor=4,
            kl_factor=0.0625,
        )
        if name == 'llama-7b':
            data.update(
                dict(
                    layers=[5],
                    rewrite_module_tmp='model.layers.{}.mlp.down_proj',
                    mlp_module_tmp='model.layers.{}.mlp',
                ))
        elif name == 'llama-13b':
            data.update(
                dict(
                    layers=[10],
                    rewrite_module_tmp='model.layers.{}.mlp.down_proj',
                    mlp_module_tmp='model.layers.{}.mlp',
                ))
        elif name == 'chatglm-6b':
            data.update(
                dict(
                    layers=[5],
                    rewrite_module_tmp=
                    'transformer.encoder.layers.{}.mlp.dense_4h_to_h',
                    mlp_module_tmp='transformer.encoder.layers.{}.mlp',
                ))
        else:
            raise NotImplementedError(f'{name} not supported.')

        return cls(**data)


import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    maxDiff = None
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_Sample_Input_1(self):
        input = """3 7"""
        output = """2"""

    def test_Sample_Input_2(self):
        input = """4 10"""
        output = """12"""

    def test_Sample_Input_3(self):
        input = """1 1000000"""
        output = """392047955148"""

import sys
sys.setrecursionlimit(500*500)

from math import gcd
from functools import reduce
from itertools import product
from itertools import combinations
from itertools import accumulate # 累積和作るやつ
import numpy as np
from collections import deque
from collections import defaultdict
from heapq import heappop, heappush

alpha2num = lambda c: ord(c) - ord('a')
num2alpha = lambda c: chr(c+97)
popcnt = lambda x: bin(x).count("1")

dx = [-1, 0, 1, 0]
dy = [0, -1, 0, 1]

def resolve():
  # https://atcoder.jp/contests/abc206/editorial/2129
  L, R = map(int, input().split(" "))
  L = max(2, L)
  def f1(l, r):
    return ((r-l+1)*(r-l))//2

  from collections import defaultdict
  # f2 => g
  memo = [defaultdict(int) for _ in range(R+1)]
  def f2(l, r):
    if l == r: return 0
    # print(l, r)
    if memo[r][l] != 0: return memo[r][l]
    temp = 0
    for i in range(2, r):
      temp += f2((l-1)//i+1, r//i)
    # print(l, r, f1(l, r) - temp)
    memo[r][l] = f1(l, r) - temp
    return memo[r][l]

  f3 = sum([R//i-1 for i in range(L, R+1)])

  ans = 2*(f1(L, R) - f2(L, R) - f3)
  print(ans)

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


import yfinance as yf
import streamlit as st
import pandas as pd
import numpy as np
from datetime import date
from datetime import timedelta
import matplotlib

st.write("""
# Simple Stock Price App
Shown are the stock closing price and volume of Google!
""")

today1 = date.today()
today0= today1 - timedelta(days = 2) #yesterday
now= today0.strftime("%Y-%m-%d")
# https://towardsdatascience.com/how-to-get-stock-data-using-python-c0de1df17e75
#define the ticker symbol
tickerSymbols = ["MSFT", "GOOGL", "AAPL"]
#get data on this ticker
#tickerData = yf.tickerSymbols
data = yf.download(tickerSymbols, start='2010-01-20', end=now)
#print(tickerData)
#get the historical prices for this ticker
# tickers =['FNF', 'ASML', 'GOOGL', 'CVS']
# data = yf.download(tickers, group_by="ticker", period='1y')
#tickerDf = yf.download(tickerData, period='1y') #, start='2020-5-31', end='2020-5-31')
# Open	High	Low	Close	Volume	Dividends	Stock Splits
#print(tickerDf)

st.write("""
## Closing Price
""")
st.line_chart(data.Close)
st.write("""
## Volume Price
""")
st.line_chart(data.Volume)

# chart_data = pd.DataFrame(
#      np.random.randn(20, 3),
#      columns=['a', 'b', 'c'])

# st.line_chart(chart_data)


from letmedoit import config
from pathlib import Path
import platform, os, sys, ctypes, subprocess, re
import shutil

def createShortcuts():
    thisOS = platform.system()
    appName = config.letMeDoItName.split()[0]
    # Windows icon
    if thisOS == "Windows":
        myappid = "letmedoit.ai"
        ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
        windowsIconPath = os.path.abspath(os.path.join(sys.path[0], "icons", f"{appName}.ico"))
        if not os.path.isfile(windowsIconPath):
            windowsIconPath = os.path.abspath(os.path.join(sys.path[0], "icons", "LetMeDoIt.ico"))
        ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(windowsIconPath)

    # Desktop shortcut
    # on Windows
    if thisOS == "Windows":
        shortcutBat1 = os.path.join(config.letMeDoItAIFolder, f"{appName}.bat")
        desktopShortcut1 = os.path.join(os.path.expanduser('~'), 'Desktop', f"{appName}.bat")
        desktopShortcut2 = os.path.join(os.path.expanduser('~'), 'OneDrive', 'Desktop', f"{appName}.bat")
        sendToShortcut = os.path.join(os.path.expanduser('~'), os.environ["APPDATA"], 'Microsoft\Windows\SendTo', f"{appName}.bat")
        shortcutCommand1 = f'''powershell.exe -NoExit -Command "{sys.executable} '{config.letMeDoItFile}' \"%1\""'''
        # Create .bat for application shortcuts
        if not os.path.exists(shortcutBat1):
            for i in (shortcutBat1, desktopShortcut1, desktopShortcut2, sendToShortcut):
                try:
                    print("creating shortcuts ...")
                    with open(i, "w") as fileObj:
                        fileObj.write(shortcutCommand1)
                    print(f"Created: {shortcutBat1}")
                except:
                    pass
    # on macOS
    # on iOS a-Shell app, ~/Desktop/ is invalid
    elif thisOS == "Darwin":
        desktopPath = os.path.expanduser("~/Desktop")
        if os.path.isdir(desktopPath):
            shortcut_file = os.path.join(config.letMeDoItAIFolder, f"{appName}.command")
            if not os.path.isfile(shortcut_file):
                print("creating shortcut ...")
                with open(shortcut_file, "w") as f:
                    f.write("#!/bin/bash\n")
                    f.write(f"cd {config.letMeDoItAIFolder}\n")
                    f.write(f"{sys.executable} {config.letMeDoItFile}\n")
                os.chmod(shortcut_file, 0o755)
                print(f"Created: {shortcut_file}")
                shutil.copy(shortcut_file, desktopPath) # overwrites older version
                print("Copied to Desktop!")

    # additional shortcuts on Linux
    elif thisOS == "Linux":
        def desktopFileContent():
            iconPath = os.path.join(config.letMeDoItAIFolder, "icons", f"{appName}.png")
            if not os.path.isfile(iconPath):
                iconPath = os.path.join(config.letMeDoItAIFolder, "icons", "LetMeDoIt.png")
            return """#!/usr/bin/env xdg-open

[Desktop Entry]
Version=1.0
Type=Application
Terminal=true
Path={0}
Exec={1} {2}
Icon={3}
Name={4}
""".format(config.letMeDoItAIFolder, sys.executable, config.letMeDoItFile, iconPath, config.letMeDoItName)

        linuxDesktopFile = os.path.join(config.letMeDoItAIFolder, f"{appName}.desktop")
        if not os.path.exists(linuxDesktopFile):
            print("creating shortcut ...")
            # Create .desktop shortcut
            with open(linuxDesktopFile, "w") as fileObj:
                fileObj.write(desktopFileContent())
            print(f"Created: {linuxDesktopFile}")
            try:
                # Try to copy the newly created .desktop file to:
                from pathlib import Path
                # ~/.local/share/applications
                userAppDir = os.path.join(str(Path.home()), ".local", "share", "applications")
                Path(userAppDir).mkdir(parents=True, exist_ok=True)
                shutil.copy(linuxDesktopFile, userAppDir) # overwrites older version
                print(f"Copied to '{userAppDir}'!")
                # ~/Desktop
                desktopPath = os.path.expanduser("~/Desktop")
                if os.path.isdir(desktopPath):
                    shutil.copy(linuxDesktopFile, desktopPath) # overwrites older version
                    print("Copied to Desktop!")
            except:
                pass
    createUtilities()
    #createAppAlias()

def createUtilities():
    first_name = config.letMeDoItName.split()[0]
    storage = os.path.join(os.path.expanduser('~'), first_name.lower())
    try:
        Path(storage).mkdir(parents=True, exist_ok=True)
    except:
        pass

    thisOS = platform.system()
    if thisOS == "Windows":
        work_with_text_script = f'''param (
    [string]$selected_text
)
Start-Process "{sys.executable} {config.letMeDoItFile} $selected_text"'''
        work_with_text_script_path = os.path.join(storage, f"{first_name}.ps1")
        with open(work_with_text_script_path, "w", encoding="utf-8") as fileObj:
            fileObj.write(work_with_text_script)
    elif thisOS == "Linux":
        # work with text selection
        # Install xsel
        # > apt install xsel
        # To use this script, users need to:
        # 1. Launch "Settings"
        # 2. Go to "Keyboard" > "Keyboard Shortcuts" > "View and Customise Shortcuts" > "Custom Shortcuts"
        # 3. Select "+" to add a custom shortcut and enter the following information, e.g.:
        # Name: LetMeDoIt AI
        # Command: /usr/bin/gnome-terminal --command ~/letmedoit/letmedoit.sh
        # Shortcut: Ctrl + Alt + L
        # get selected text
        work_with_text_script = f'''#!/usr/bin/env bash
selected_text=$(echo "$(xsel -o)" | sed 's/"/\"/g')
{sys.executable} {config.letMeDoItFile} -u false -n true -i false "$selected_text"'''
        work_with_text_script_path = os.path.join(storage, first_name)
        with open(work_with_text_script_path, "w", encoding="utf-8") as fileObj:
            fileObj.write(work_with_text_script)
        # summarise selected text
        work_with_text_script = f'''#!/usr/bin/env bash
selected_text=$(echo "$(xsel -o)" | sed 's/"/\"/g')
{sys.executable} {config.letMeDoItFile} -u false -n true -i false -c "Let me Summarize" -r "$selected_text"'''
        work_with_summary_script_path = os.path.join(storage, f"{first_name}_Summary")
        with open(work_with_summary_script_path, "w", encoding="utf-8") as fileObj:
            fileObj.write(work_with_text_script)
        # download selected url
        work_with_text_script = f'''#!/usr/bin/env bash
selected_text=$(echo "$(xsel -o)" | sed 's/"/\"/g')
{sys.executable} {config.letMeDoItFile} -u false -n true -i false -c "Let me Download" -r "$selected_text"'''
        work_with_download_script_path = os.path.join(storage, f"{first_name}_Download")
        with open(work_with_download_script_path, "w", encoding="utf-8") as fileObj:
            fileObj.write(work_with_text_script)
        # download selected Youtube url into mp3
        work_with_text_script = f'''#!/usr/bin/env bash
selected_text=$(echo "$(xsel -o)" | sed 's/"/\"/g')
{sys.executable} {config.letMeDoItFile} -u false -n true -i false -c "Let me Download Youtube MP3" -r "$selected_text"'''
        work_with_downloadmp3_script_path = os.path.join(storage, f"{first_name}_YoutubeMP3")
        with open(work_with_downloadmp3_script_path, "w", encoding="utf-8") as fileObj:
            fileObj.write(work_with_text_script)
        # pronounce selected text
        work_with_text_script = f'''#!/usr/bin/env bash
selected_text=$(echo "$(xsel -o)" | sed 's/"/\"/g')
{sys.executable} {config.letMeDoItFile} -u false -n true -i false -c "Let me Pronounce" -r "$selected_text"'''
        work_with_pronunciation_script_path = os.path.join(storage, f"{first_name}_Pronunciation")
        with open(work_with_pronunciation_script_path, "w", encoding="utf-8") as fileObj:
            fileObj.write(work_with_text_script)
        # explain selected text
        work_with_text_script = f'''#!/usr/bin/env bash
selected_text=$(echo "$(xsel -o)" | sed 's/"/\"/g')
{sys.executable} {config.letMeDoItFile} -u false -n true -i false -c "Let me Explain" -r "$selected_text"'''
        work_with_explanation_script_path = os.path.join(storage, f"{first_name}_Explanation")
        with open(work_with_explanation_script_path, "w", encoding="utf-8") as fileObj:
            fileObj.write(work_with_text_script)
        # translate selected text
        work_with_text_script = f'''#!/usr/bin/env bash
selected_text=$(echo "$(xsel -o)" | sed 's/"/\"/g')
{sys.executable} {config.letMeDoItFile} -u false -n true -i false -c "Let me Translate" -r "$selected_text"'''
        work_with_translation_script_path = os.path.join(storage, f"{first_name}_Translation")
        with open(work_with_translation_script_path, "w", encoding="utf-8") as fileObj:
            fileObj.write(work_with_text_script)
        # work with files or folders selection via NAUTILUS; right-click > scripts > LetMeDoIt
        if not config.isTermux:
            work_with_files_script = f'''#!/usr/bin/env bash
mkdir -p {storage}
# Get the selected file or folder path
path="$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"
echo "$path" > {storage}/selected_files.txt
/usr/bin/gnome-terminal --command "{sys.executable} {config.letMeDoItFile} -u false -n true -i false -f {storage}/selected_files.txt"'''
            work_with_files_script_path = os.path.expanduser(f"~/.local/share/nautilus/scripts/{first_name}")
            with open(work_with_files_script_path, "w", encoding="utf-8") as fileObj:
                fileObj.write(work_with_files_script)
        # make script files executable
        scriptFiles = [
            work_with_translation_script_path,
            work_with_explanation_script_path,
            work_with_summary_script_path,
            work_with_text_script_path,
            work_with_pronunciation_script_path,
            work_with_downloadmp3_script_path,
            work_with_download_script_path,
        ]
        if config.isTermux:
            # added a command shortcut
            try:
                termux_shortcut_script = f'''#!/usr/bin/env bash\n{sys.executable} {config.letMeDoItFile}'''
                termux_shortcut_path = "/data/data/com.termux/files/usr/bin/letmedoit"
                with open(termux_shortcut_path, "w", encoding="utf-8") as fileObj:
                    fileObj.write(termux_shortcut_script)
                scriptFiles.append(termux_shortcut_path)
            except:
                pass
        else:
            scriptFiles.append(work_with_files_script_path)
        for i in scriptFiles:
            os.chmod(i, 0o755)
    elif thisOS == "Darwin":
        file1 = os.path.join(config.letMeDoItAIFolder, "macOS_service/LetMeDoIt_Text_workflow/Contents/document.wflow")
        file2 = os.path.join(config.letMeDoItAIFolder, "macOS_service/LetMeDoIt_Files_workflow/Contents/document.wflow")
        file3 = os.path.join(config.letMeDoItAIFolder, "macOS_service/LetMeDoIt_Explanation_workflow/Contents/document.wflow")
        file4 = os.path.join(config.letMeDoItAIFolder, "macOS_service/LetMeDoIt_Translation_workflow/Contents/document.wflow")
        file5 = os.path.join(config.letMeDoItAIFolder, "macOS_service/LetMeDoIt_Summary_workflow/Contents/document.wflow")
        file6 = os.path.join(config.letMeDoItAIFolder, "macOS_service/LetMeDoIt_Pronounce_workflow/Contents/document.wflow")
        file7 = os.path.join(config.letMeDoItAIFolder, "macOS_service/LetMeDoIt_YoutubeMP3_workflow/Contents/document.wflow")
        file8 = os.path.join(config.letMeDoItAIFolder, "macOS_service/LetMeDoIt_Download_workflow/Contents/document.wflow")
        for i in (file1, file2, file3, file4, file5, file6, file7, file8):
            with open(i, "r", encoding="utf-8") as fileObj:
                content = fileObj.read()
            search_replace = (
                ("~/letmedoit", storage),
                ("\[LETMEDOIT_PATH\]", f"{sys.executable} {config.letMeDoItFile}"),
            )
            for search, replace in search_replace:
                content = re.sub(search, replace, content)
            with open(i, "w", encoding="utf-8") as fileObj:
                fileObj.write(content)
        file1 = os.path.join(config.letMeDoItAIFolder, "macOS_service/LetMeDoIt_Files_workflow/Contents/Info.plist")
        file2 = os.path.join(config.letMeDoItAIFolder, "macOS_service/LetMeDoIt_Text_workflow/Contents/Info.plist")
        file3 = os.path.join(config.letMeDoItAIFolder, "macOS_service/LetMeDoIt_Explanation_workflow/Contents/Info.plist")
        file4 = os.path.join(config.letMeDoItAIFolder, "macOS_service/LetMeDoIt_Translation_workflow/Contents/Info.plist")
        file5 = os.path.join(config.letMeDoItAIFolder, "macOS_service/LetMeDoIt_Summary_workflow/Contents/Info.plist")
        file6 = os.path.join(config.letMeDoItAIFolder, "macOS_service/LetMeDoIt_Pronounce_workflow/Contents/Info.plist")
        file7 = os.path.join(config.letMeDoItAIFolder, "macOS_service/LetMeDoIt_YoutubeMP3_workflow/Contents/Info.plist")
        file8 = os.path.join(config.letMeDoItAIFolder, "macOS_service/LetMeDoIt_Download_workflow/Contents/Info.plist")
        for i in (file1, file2, file3, file4, file5, file6, file7, file8):
            with open(i, "r", encoding="utf-8") as fileObj:
                content = fileObj.read()
            content = re.sub("LetMeDoIt", first_name, content)
            with open(i, "w", encoding="utf-8") as fileObj:
                fileObj.write(content)
        folder1 = os.path.join(config.letMeDoItAIFolder, "macOS_service", "LetMeDoIt_Files_workflow")
        folder1_dest = os.path.join(os.path.expanduser("~/Library/Services"), f"{first_name} Files.workflow")
        folder2 = os.path.join(config.letMeDoItAIFolder, "macOS_service", "LetMeDoIt_Text_workflow")
        folder2_dest = os.path.join(os.path.expanduser("~/Library/Services"), f"{first_name} Text.workflow")
        folder3 = os.path.join(config.letMeDoItAIFolder, "macOS_service", "LetMeDoIt_Explanation_workflow")
        folder3_dest = os.path.join(os.path.expanduser("~/Library/Services"), f"{first_name} Explanation.workflow")
        folder4 = os.path.join(config.letMeDoItAIFolder, "macOS_service", "LetMeDoIt_Summary_workflow")
        folder4_dest = os.path.join(os.path.expanduser("~/Library/Services"), f"{first_name} Summary.workflow")
        folder5 = os.path.join(config.letMeDoItAIFolder, "macOS_service", "LetMeDoIt_Translation_workflow")
        folder5_dest = os.path.join(os.path.expanduser("~/Library/Services"), f"{first_name} Translation.workflow")
        folder6 = os.path.join(config.letMeDoItAIFolder, "macOS_service", "LetMeDoIt_Pronounce_workflow")
        folder6_dest = os.path.join(os.path.expanduser("~/Library/Services"), f"{first_name} Pronunciation.workflow")
        folder7 = os.path.join(config.letMeDoItAIFolder, "macOS_service", "LetMeDoIt_YoutubeMP3_workflow")
        folder7_dest = os.path.join(os.path.expanduser("~/Library/Services"), f"{first_name} Youtube MP3.workflow")
        folder8 = os.path.join(config.letMeDoItAIFolder, "macOS_service", "LetMeDoIt_Download_workflow")
        folder8_dest = os.path.join(os.path.expanduser("~/Library/Services"), f"{first_name} Download.workflow")
        folders = (
            (folder1, folder1_dest),
            (folder2, folder2_dest),
            (folder3, folder3_dest),
            (folder4, folder4_dest),
            (folder5, folder5_dest),
            (folder6, folder6_dest),
            (folder7, folder7_dest),
            (folder8, folder8_dest),
        )
        for folder, folder_dest in folders:
            if os.path.isdir(folder_dest):
                shutil.rmtree(folder_dest, ignore_errors=True)
            shutil.copytree(folder, folder_dest)
        iconFolder = os.path.join(config.letMeDoItAIFolder, "icons")
        iconFolder_dest = os.path.join(storage, "icons")
        if not os.path.isdir(iconFolder_dest):
            shutil.copytree(iconFolder, iconFolder_dest)

def createAppAlias():
    with open(os.path.join(config.letMeDoItAIFolder, "package_name.txt"), "r", encoding="utf-8") as fileObj:
        package = fileObj.read()
    alias = package
    target = f"{sys.executable} {config.letMeDoItFile}"

    findAlias = "/bin/bash -ic 'alias letmedoit'" # -c alone does not work
    aliasOutput, *_ = subprocess.Popen(findAlias, shell=True, stdout=subprocess.PIPE, text=True).communicate()

    if not aliasOutput.strip() == f"""alias letmedoit='{target}'""":
        print("creating alias ...")
        def addAliasToLoginProfile(profile, content):
            if os.path.isfile(profile):
                content = f"\r\n{content}" if config.thisPlatform == "Windows" else f"\n{content}"
            try:
                with open(profile, "a", encoding="utf-8") as fileObj:
                    fileObj.write(content)
            except:
                pass
        home = os.path.expanduser("~")
        if config.thisPlatform == "Windows":
            """# command prompt
            profile = os.path.join(home, "AutoRun.bat")
            content = f'''doskey {alias}="{target}"'''
            addAliasToLoginProfile(profile, content)
            # powershell
            profile = os.path.join(home, "Documents", "WindowsPowerShell", "Microsoft.PowerShell_profile.ps1")
            content = f'''Set-Alias -Name {alias} -Value "{target}"'''
            addAliasToLoginProfile(profile, content)"""
            pass
        else:
            content = f"""alias {alias}='{target}'"""
            try:
                for profile in (".bash_profile", ".zprofile", ".bashrc", ".zshrc"):
                    addAliasToLoginProfile(os.path.join(home, profile), content)
                print(content)
            except:
                pass


#
#
import numpy as np
import matplotlib.pyplot as plt
from .params import *

prop_cycle = plt.rcParams['axes.prop_cycle']
colors = prop_cycle.by_key()['color']

class D2nuTable(object):
    """ Class to represent the second frequency differences D2nu.

    Attributes:
        l (np.array): Harmonic degrees.
        n (np.array): Radial orders.
        freq (np.array): Mode frequencies.
        d2nu (np.array): Second differences.
        err (np.array): Errors on second differences.
        matcov (np.array): Covariance matrix.
        cut_table (D2nuTable):
            The initial dataset of d2nu but data points
            with large uncertainties are excluded.
        cut_range_table (D2nuTable):
            The initial dataset of d2nu but only data points
            in a given frequency range are kept.
        cut_l_table (D2nuTable):
            The initial dataset of d2nu but data points which
            angular degree is l are excluded (usually l=2 because they have the
            largest uncertainties).
        cond (float): Condition number of the covariance matrix.
        inv_matcov (np.array): Inverse covariance matrix.
    """

    def __init__(self):
        """ Initialises an instance of D2nuTable. """

    def create(self, l, n, freq, d2nu, err, matcov):
        """ Creates an instance of D2nuTable by directly passing the data.

        Args:
            l (np.array): Angular degrees.
            n (np.array): Radial orders.
            freq (np.array): Mode frequencies.
            d2nu (np.array): Second differences.
            err (np.array): Errors on second differences.
            matcov (np.array): Covariance matrix.
        """
        self.l = l
        self.n = n
        self.freq = freq
        self.d2nu = d2nu
        self.err = err
        self.matcov = matcov

    def load(self):
        """ Loads the second differences D2nu from a file.

        Not implemented yet.
        """

    # Methods for data handling

    def cut(self, sigma):
        """ Removes observational points for which the D2nu uncertainty is
        above sigma * median(err).

        The resulting D2nuTable object is stored in a new attribute called
        'cut_table'.

        Args:
            sigma (float):
                Defines the maximum tolerated d2nu uncertainty in cut_table
                (i.e. sigma * median(err)).
        """
        median_sigma = np.median(self.err)
        i_kept = np.argwhere(self.err <= sigma*median_sigma)[:,0]
        l_kept, n_kept, freq_kept, d2nu_kept, err_kept = \
                self.l[i_kept], self.n[i_kept], self.freq[i_kept],\
                self.d2nu[i_kept],self.err[i_kept]
        matcov_kept = self.matcov[i_kept,:][:,i_kept]
        i_sort = np.argsort(freq_kept)
        l_kept, n_kept, freq_kept, rr010_kept, err_kept = \
                l_kept[i_sort], n_kept[i_sort], freq_kept[i_sort],\
                d2nu_kept[i_sort], err_kept[i_sort]
        matcov_kept = matcov_kept[i_sort,:][:,i_sort]
        self.cut_table = D2nuTable()
        self.cut_table.create(l_kept,n_kept,freq_kept,d2nu_kept,err_kept,matcov_kept)

    def cut_range(self, freqmin, freqmax):
        """ Keeps data points between freqmin and freqmax.

        The resulting D2nuTable object is stored in a new attribute called
        'cut_range_table'.

        Args:
            freqmin (float): Minimum frequency.
            freqmax (float): Maximum frequency.
        """
        i_kept = np.argwhere((self.freq >= freqmin) & (self.freq <= freqmax))[:,0]
        l_kept, n_kept, freq_kept, d2nu_kept, err_kept = \
                self.l[i_kept], self.n[i_kept], self.freq[i_kept], \
                self.d2nu[i_kept],self.err[i_kept]
        matcov_kept = self.matcov[i_kept,:][:,i_kept]
        # sort data points by increasing mode frequency
        i_sort = np.argsort(freq_kept)
        l_kept, n_kept, freq_kept, d2nu_kept, err_kept = \
                l_kept[i_sort], n_kept[i_sort], freq_kept[i_sort], \
                d2nu_kept[i_sort], err_kept[i_sort]
        matcov_kept = matcov_kept[i_sort,:][:,i_sort]
        self.cut_range_table = D2nuTable()
        self.cut_range_table.create(l_kept,n_kept,freq_kept,d2nu_kept,err_kept,matcov_kept)

    def cut_l(self, l):
        """ Keeps data points that have their angular degree != l.

        The resulting D2nuTable object is stored in a new attribute called
        'cut_l_table'.

        Args:
            l (int): Angular degree of the data points to exclude.
        """
        i_kept = np.argwhere(self.l != l)[:,0]
        l_kept, n_kept, freq_kept, d2nu_kept, err_kept = \
                self.l[i_kept], self.n[i_kept], self.freq[i_kept], \
                self.d2nu[i_kept],self.err[i_kept]
        matcov_kept = self.matcov[i_kept,:][:,i_kept]
        # sort data points by increasing mode frequency
        i_sort = np.argsort(freq_kept)
        l_kept, n_kept, freq_kept, d2nu_kept, err_kept = \
                l_kept[i_sort], n_kept[i_sort], freq_kept[i_sort], \
                d2nu_kept[i_sort], err_kept[i_sort]
        matcov_kept = matcov_kept[i_sort,:][:,i_sort]
        self.cut_l_table = D2nuTable()
        self.cut_l_table.create(l_kept,n_kept,freq_kept,d2nu_kept,err_kept,matcov_kept)

    # Methods related to the covariance matrix

    def matcov_condnumber(self):
        """ Gets the condition number of the covariance matrix. """
        self.cond = np.linalg.cond(self.matcov)
        print(f"Condition number is {self.cond}")

    def matcov_invert(self):
        """ Inverts the covariance matrix.

        If fit_params["nsvd"] is different from -1, the inversion of the
        covariance matrix is regularised using the truncated singular value
        decomposition (SVD) method, fit_params["nsvd"] being the number of
        singular values to keep.
        """
        if fit_params["nsvd"] != -1:
            u,s,v = np.linalg.svd(self.matcov)
            self.inv_matcov = np.linalg.pinv(self.matcov,
                    rcond=s[fit_params["nsvd"]-1]/s.max())
        else:
            self.inv_matcov = np.linalg.inv(self.matcov)

    # Plotting methods

    def plot(self):
        """ Plots the second differences D2nu. """
        plt.figure()
        plt.xlabel(r'Frequency ($\mu$Hz)')
        plt.ylabel(r'$\Delta_2 \nu$ $(\mu Hz)$')
        markers = ['o','^','s']
        for l in np.arange(3):
            i_l = self.l == l
            plt.errorbar(self.freq[i_l],self.d2nu[i_l],yerr=self.err[i_l],
                    fmt=markers[l],mfc=colors[l],mec='none',ecolor='#c4c4c4',
                    label=r'$\ell = {}$'.format(l))
        plt.legend()
        plt.show()

    def plot_matcov_svd(self):
        """ Plots the singular values of the covariance matrix. """
        u, s, v = np.linalg.svd(self.matcov)
        plt.figure()
        plt.plot(np.arange(1,len(s)+1),s,'o-')
        plt.show()

    def plot_matcov(self):
        """ Plots the covariance matrix. """
        plt.figure()
        plt.imshow(self.matcov)
        cb = plt.colorbar()
        plt.show()


from tkinter import *
from tkinter import messagebox

def click():
    # messagebox.showinfo(title='message box',message='you are an idiot')
    # messagebox.showwarning(title='warning',message='you have a VIRUS!!!')
    # messagebox.showerror(title='error',message='something went wrong')
    # if messagebox.askokcancel(title='ask ok cancel',message='do you want to do the thing'):
    #     print("you did a thing")
    # else:
    #     print("you didnt do anything")
    # if messagebox.askretrycancel(title='ask retry or no',message='do you want to retry the thing'):
    #     print("you retry a thing")
    # else:
    #     print("you didnt retry anything")
    # if messagebox.askyesno(title='ask yes or no',message='do you like me'):
    #     print("i like you")
    # else:
    #     print('why dont you like me')
    # print(messagebox.askquestion(title="question",message="do you like me? "))
    print(messagebox.askyesnocancel(title="yes no cancel",message="do you like to code"))

window = Tk()

button = Button(window,command=click,text='click me')
button.pack()

window.mainloop()


# You are given a histogram consisting of rectangles of different heights. These heights are represented in an input list, such that [1, 3, 2, 5] corresponds to the following diagram:

#       x
#       x
#   x   x
#   x x x
# x x x x
# Determine the area of the largest rectangle that can be formed only from the bars of the histogram. For the diagram above, for example, this would be six, representing the 2 x 3 area at the bottom right.

class histogram():

    def calculate_largest(l):

        ordered_list = []
        max = 0
        i = 0
        while (i < len(l)):

            #adds the height to the stack if it is greater than the current greatest height
            if (len(ordered_list) == 0) or (l[ordered_list[-1]] <= ordered_list[i]):
                ordered_list.append(i)
                i+=1
            #if it is not greater than the current greatest height, we use this element as the right index and the previous lowest as the left index to find area
            else:
                temp_height = ordered_list.pop()
                temp_width = i - ordered_list[-1] - 1
                temp_area = temp_width * l[temp_height]
                if (temp_area > max):
                    max = temp_area

        while (len(ordered_list) > 0):

            temp_height = ordered_list.pop()
            temp_width = i - ordered_list[-1] - 1
            temp_area = l[ordered_list] * temp_width
            if (temp_area > max):
                max = temp_area

        return max