import requests, time, datetime
from typing import Generic
from bs4 import BeautifulSoup
from decouple import config
from fake_useragent import UserAgent
import http.cookiejar
from selenium import webdriver as webdriver_basic   # for webdriver
from selenium.webdriver.support.ui import WebDriverWait  # for implicit and explict waits
from selenium.webdriver.chrome.options import Options  # for suppressing the browser
from seleniumwire import webdriver
import pandas as pd
import json

from generic_api import Generic_API
from utility import eprint, touch, HiddenPrints, Dots
import preprocess

API_USERNAME = config('USER')
API_KEY = config('API_KEY')
COOKIE_PATH = config('COOKIE_PATH')
USER_AGENT = config('USER_AGENT')
try:
    DRIVER_PATH_CHROME = config('DRIVER_PATH_CHROME')
    DRIVER_PATH_FIREFOX = config('DRIVER_PATH_FIREFOX')
except Exception:
    DRIVER_PATH_CHROME = 'drivers/chromedriver-91'
    DRIVER_PATH_FIREFOX = 'drivers/geckodriver'

CACHE = 'cache/html.pickle'
JSON_DATA_PATH = 'json_data'
CSV_DATA_PATH = 'csv_data'
JAVASCRIPT_DELAY = 8

# requires admin privileges (not used in current version)
def detect_key():
    import keyboard  # using module keyboard
    while True:  # making a loop
        try:  # used try so that if user pressed other than the given key error will not be shown
            if keyboard.is_pressed('q'):  # if key 'q' is pressed
                print('Terminating Page...')
                break  # finishing the loop
        except:
            break  # if user pressed a key other than the given key the loop will break

def scrape_patenthub_html_static():
    ua = UserAgent()

    url = 'https://www.patenthub.cn/s?ds=all&dm=mix&p=&ps=10&s=score!&q2=&m=none&fc=[{%22type%22%3A%22applicationYear%22%2C%22op%22%3A%22include%22%2C%22values%22%3A[%222014%22]}]&q=5g'
    # url = 'https://www.patenthub.cn/s?ds=all&q=5g'
    headers = {'User-Agent': ua.chrome}

    cjar = http.cookiejar.MozillaCookieJar(COOKIE_PATH)
    cjar.load()

    r = requests.get(url, headers=headers, cookies=cjar)
    f = open('webpage.html', mode='wb')
    f.write(r.content)
    print(r.status_code)

    # test run
    page = requests.get('https://www.tmea.org/programs/all-state/history')

    soup = BeautifulSoup(page.text, 'html.parser')

    body = soup.find(id = 'organization')
    options = body.find_all('option')

    for name in options:
        child = name.contents[0]
        print(child)

def scrape_patenthub_html_dynamic(target_url:str, debug:str=None, suppressError=True) -> str:
    dummy_url = 'https://www.patenthub.cn/this404page'
    url = target_url

    cjar = http.cookiejar.MozillaCookieJar(COOKIE_PATH)
    cjar.load()

    # json_cookies = requests.utils.dict_from_cookiejar(cjar)       # slightly slower due to additional function call
    json_cookies = [ {'name':cookie.name,
                    'value':cookie.value,
                    'secure':cookie.secure,
                    'expires':cookie.expires,
                    'discard': cookie.discard,
                    'rfc2109':cookie.rfc2109} for cookie in cjar if cookie.name != 'pref' ]

    # to enable headless
    options = webdriver.ChromeOptions()
    options.add_experimental_option('detach', True)
    options.add_argument('window-size=1200x600') # optional
    options.add_argument('headless')
    driver = webdriver.Chrome(executable_path=DRIVER_PATH_CHROME, options=options)

    # Create a request interceptor
    def interceptor(request):
        try:
            del request.headers['user-agent']  # Delete the header first
        except Exception:
            pass
        request.headers['user-agent'] = USER_AGENT

    # Set the interceptor on the driver
    driver.request_interceptor = interceptor

    # hides some weird library errors
    try:
        print('Phase 1: Requesting page... ', end='')
        driver.get(dummy_url)

        # driver.delete_all_cookies()
        for cookie in json_cookies:
            driver.delete_cookie(cookie['name'])
            driver.add_cookie(cookie)

        if suppressError:
            # suppresses some VPN related printouts
            with HiddenPrints():
                driver.get(url)
        else:
            driver.get(url)
            # driver.refresh()

        print("Done")

    except Exception as e:
        pass

    # reveal all hidden items
    def expand_hidden_items(driver) -> int:
        print('Phase 2: script injections... ', end='')
        base_path = '/html/body/div[3]/div[2]/div[1]/div/div[%i]/a'
        try:
            driver.find_element_by_xpath(xpath= (base_path % 2) )
        except:
            eprint('Error: expandable menu does not exists/is not loaded')
            return False

        i = 2
        cnt = 0
        js_script_action = 'arguments[0].click()'
        while 1:
            try:
                clickable = driver.find_element_by_xpath(xpath= (base_path % i) )
                driver.execute_script(js_script_action, clickable)
                cnt += 1
            except:
                break
            i += 1
        print('Done')
        return cnt

    hidden_item_cnt = expand_hidden_items(driver)
    if hidden_item_cnt == False:
        driver.close()
        return False

    # additional wait feature to make sure hidden content has been loaded
    print("Phase 2.1: script executing...", flush=True)
    while 1:
        soup = BeautifulSoup(markup=driver.page_source, features='html.parser')
        activated = soup.find_all(name='div', class_='content active')
        if len(activated) < hidden_item_cnt + 1:        # +1 since one content was active by default (thus not hidden)
            continue
        else:
            break

    # spin wait (appaently time.sleep stops EVERYTHING, not applicable here)
    start = time.monotonic()
    dots = Dots()
    dots.start()
    while time.monotonic() - start < JAVASCRIPT_DELAY:
        pass
    dots.stop()

    print("Phase 3: saving page source... ", end='')
    realtime_html = driver.page_source
    # realtime_html = driver.execute_script('return document.querySelector("html").innerHTML')

    path = touch('cache/human_readable.html')
    with open(file=path, mode='wt') as tmp:
        tmp.write(realtime_html)
    Generic_API.pickle_bytestream(op='dump', data=realtime_html, custom_cache=CACHE)

    print("Done")

    if debug == 'console':
        print("Engaging Interactive Console... \n")
        while 1:
            try:
                tmp_ = None
                exec(f'tmp_ = {input(">>> ")}')
                if tmp_ != None:
                    print(tmp_)
            except Exception as e:
                print(e)

    driver.close()
    return realtime_html

def parse_patenthub_html(savefile:str =f'{JSON_DATA_PATH}/NEWEST_DATA.json', category='N/A') -> dict:
    page_source = Generic_API.pickle_response(op='load', custom_cache=CACHE)
    soup = BeautifulSoup(markup=page_source, features='html.parser')

    # -- initialize data dictionary ---
    stats = {}
    stats.update( {'category': category,
                'countryCode': None} )

    # --- get patent by country ---
    countries = soup.find_all(name='i', class_='flag')

    country_data = []
    for elem in countries:
        code = elem['class'][-1]
        name = elem.parent.find(name='span', class_='key').text
        cnt = elem.parent.find(name='span', class_='doc-count').text
        country_data.append( [name, code, cnt] )

    stats['countryCode'] = country_data

    # --- get patent by remaining statistics ---

    filter_ = soup.find(name='div', attrs={'id': 'filter'})
    dropdowns = filter_.find_all(name='a', class_='title')[1:]

    for dd in dropdowns:
        # item_name_cn = dd.text.strip()
        item_name_eng = dd['data-type'].strip()
        data = []
        content = dd.parent.find(name='div', class_='content active')
        content_entries = content.find_all(name='li')
        for entry in content_entries:
            if entry.text.find('无数据') != -1:
                data.append( [None, '0'] )
                break
            entry_key = entry.find(name='span', class_='key').text
            entry_val = entry.find(name='span', class_='doc-count').text
            data.append( [entry_key, entry_val] )
        stats[item_name_eng] = data

    touch(savefile)
    with open(file=savefile, mode='wt') as f:
        json.dump(stats, f)

    return stats

def check_exists(html:str, name:str, attrs:dict) -> bool:
    """Check whether a certain DOM element exists in given html page

    Args:
        html (str): the html source page
        name (str): tag name
        attrs (dict): tag attributes

    Returns:
        bool: exists=True, nonexistant=False
    """

    soup = BeautifulSoup(markup=html, features='html.parser')
    exists = soup.find(name=name, attrs=attrs)

    return exists

def pull_data_by_year(start=None, end=None, mode=1, delay=1):
    start = start if start != None else 1421       # first patent ever was granted in 1421 in Florence

    # string = 'https://www.patenthub.cn/s?ds=all&dm=mix&p=&ps=10&s=score%21&q2=&m=none&fc=%5B%5D&q=5g'
    if mode == 1: query_by_year = 'https://www.patenthub.cn/s?ds=all&dm=mix&s=score%21&q=applicationYear%3A'
    if mode == 2: query_by_year = 'https://www.patenthub.cn/s?ds=all&dm=mix&s=score%21&q=documentDate%3A'

    for year in reversed(range(start, end+1)):
        print(f'Retriving {year} data...')
        s = time.perf_counter()
        query = query_by_year + str(year)

        try:
            html = scrape_patenthub_html_dynamic(target_url=query)
            if html == False:       # probably not found
                print(f'\n\n--- Skipping {year}: No Data Available ---\n')
                continue

        except Exception:
            eprint(f'Internal Error: Data Retrieved from {year+1} to {end}.")')
            return

        if not check_exists(html, name='div', attrs={'id': 'countryCode'}):
            print(f"DONE. Data Retrieved from {year+1} to {end}.")
            return
        else:
            print('POSTPROCESSING: Parsing + Saving Data...')
            if mode == 1: parse_patenthub_html(savefile=f'{JSON_DATA_PATH}/{year}.json', category=str(year))
            elif mode == 2: parse_patenthub_html(savefile=f'{JSON_DATA_PATH}/_{year}.json', category=str(year))

            print(f'Complete! -- {time.perf_counter() - s:.2f}s\n')

        time.sleep(delay)

    print(f"DONE. Data Retrieved from {start} to {end}.")
    return

# this can only be run after compiling a CSV with query-able? country names
def pull_data_by_country(delay=1):
    query_by_country = 'https://www.patenthub.cn/s?ds=all&dm=mix&s=score%21&q=countryCode%3A'
    codes = preprocess.get_known_country_codes('csv_data_compiled/1900-2021.csv')

    import os
    for i, code in enumerate(codes):
        if os.path.exists(f'json_data/{code}.json'): continue
        print(f'{i}/{len(codes)}: Retriving {code} data...')
        query = query_by_country + code

        s = time.perf_counter()
        try:
            scrape_patenthub_html_dynamic(target_url=query)
        except Exception:
            eprint(f'Internal Error: Data Not Retrieved for Country {code}.")')
            return

        else:
            print('POSTPROCESSING: Parsing + Saving Data...')
            parse_patenthub_html(savefile=f'{JSON_DATA_PATH}/{code}.json', category=str(code))

            print(f'Complete! -- {time.perf_counter() - s:.2f}s\n')

    print(f"DONE. Data Retrieved for {len(codes)} countries.")
    return

# deprecated
def interface(mode:str, categories:list = None):
    if mode == 'scrape':
        current_year = datetime.datetime.today().year
        pull_data_by_year(end=current_year)

    if mode == 'parse':
        clean_data = parse_patenthub_html()
        print(clean_data)

# upcoming feature
def test_limit(num=10):
    from random_word import RandomWords
    print(RandomWords().get_random_word())
    ...

# upcoming feature
def export_as_xml():
    with open('NEWEST_DATA.json', mode='rt') as f:
        x = json.load(f)
        from dict2xml import dict2xml
        xml = dict2xml(x)
        f2 = open(file='NEWEST_DATA.xml', mode='wt')
        f2.write(xml)

def main():
    pull_data_by_year(start=1800, end=1842, delay=1, mode=2)
    # pull_data_by_country()
    # parse_patenthub_html(savefile=f'{JSON_DATA_PATH}/{1983}.json', category=str(1983))

if __name__ == '__main__':
    main()


#!/usr/bin/env python

import socket
import argparse
import utility
from game import Game

def main():
    parser = argparse.ArgumentParser(description="Python client for SIG-GAME framework.")
    parser.add_argument("-a", "--address", dest='conn_address', default="localhost",
                        help="The address of the game server.", type=str)
    parser.add_argument("-p", "--port", dest='conn_port', default="19000",
                        help="The port of the game server.", type=int)
    parser.add_argument("-g", "--game", dest='game_name', default=None,
                        help="The name of game to connect to on the server.")

    parser.add_argument("-v", "--verbose", dest='is_verbose', default=False,
                        help="Print more output messages.", action="store_true")
    parser.add_argument("-vv", "--very-verbose", dest='is_very_verbose', default=False,
                        help="Print even more output messages.", action="store_true")
    args = parser.parse_args()

    connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    game = Game(conn=connection, addr=args.conn_address, port=args.conn_port, name=args.game_name)
    utility.verbose = args.is_verbose
    utility.very_verbose = args.is_very_verbose

    game.run()

    connection.close()

    return

if __name__ == '__main__':
    main()


import pygame
import src.constants as CONSTANTS
from src.obstacle import Obstacle
from typing import Tuple
from src.commonUtils import resource_path

class Track(pygame.sprite.Sprite):

    def __init__(self, track_id=''):
        super(Track, self).__init__()

        __TRACKS_DIR = 'assets/'
        self.track_id = track_id
        self.track_path = __TRACKS_DIR + "track" + str(track_id) + ".png"

        self.image = pygame.image.load(resource_path(self.track_path)).convert_alpha()
        self.rect = self.image.get_rect(topleft=(0, CONSTANTS.OFFSET))
        self.mask = pygame.mask.from_surface(self.image)

    def get_at(self, abs_pt: Tuple[int, int]) -> pygame.Color:
        off_x = abs_pt[0] - self.rect.topleft[0]
        off_y = abs_pt[1] - self.rect.topleft[1]
        if 0 <= off_x < self.rect.width and 0 <= off_y < self.rect.height:
            return self.image.get_at((off_x, off_y))
        else:
            return None

    def place_obstacle(self, obstacle: Obstacle, abs_pos):
        position = (abs_pos[0], abs_pos[1]-CONSTANTS.OFFSET)
        self.image.blit(obstacle.image, position)
        self.mask = pygame.mask.from_surface(self.image)


import random

numero = int(input('É hora do duelo do par ou impar, escolha um número de 0 a 5:'))
numeroRandom = random.randint(0, 5)

resultado = numero + numeroRandom

print('Seu número', numero
      , 'número da máquina', numeroRandom)

if (resultado % 2 == 0):
    print('Deu par!')

else:
    print('Deu impar!')


# Import libraries
from torch import nn
from torch.nn import functional as F

class LSTMTagger(nn.Module):

	def __init__(self, embedding_dim, hidden_dim, vocab_size, tagset_size, bidirectional, emb_weights):
		super(LSTMTagger, self).__init__()

		###########################
		# Embeds handling
		###########################
		if emb_weights is not None:
			self.word_embeddings = nn.Embedding.from_pretrained(emb_weights, freeze=True)
		else:
			self.word_embeddings = nn.Embedding(vocab_size, embedding_dim)

		# The LSTM takes word embeddings as inputs, and outputs hidden states with dimensionality hidden_dim.
		self.lstm = nn.LSTM(input_size=embedding_dim, hidden_size=hidden_dim // (1 if not bidirectional else 2),
			num_layers=1, bidirectional=bidirectional, batch_first=True)

		# The linear layer that maps from hidden state space to tag space
		self.hidden2tag = nn.Linear(hidden_dim, tagset_size)

	def forward(self, sentences):
		# No need to take in account the batch dimension
		embeds = self.word_embeddings(sentences)

		# Batch first is true so we don't need to work on dimensions
		lstm_out, _ = self.lstm(embeds) # Dimension is [len(sentences), len_sent, -1]

		# Flatten the LSTM output and feed it to the linear layer
		linear_input = lstm_out.contiguous().view(-1, lstm_out.shape[2])
		tag_space = self.hidden2tag(linear_input)

		# Finally apply the softmax to have the probability of the results
		tag_scores = F.log_softmax(tag_space, dim=1)

		return tag_scores


from flask import Flask, render_template, jsonify, request, abort
from database import LlamaBase, Llama

app = Flask(__name__)

# In-Memory 'Database'
db = LlamaBase()

# Llama Viewer App
# Llama Feeder App
# Llama Market App

@app.route('/')
def hello_world():
    return app.send_static_file('index.html')

@app.route('/<path:path>')
def static_proxy(path):
    # send_static_file will guess the correct MIME type
    return app.send_static_file(path)

@app.route('/user', methods=['GET'])
def get_user():
    user = get_user_from_cookie(db, request)
    if user is None:
        return abort(404)
    return jsonify(user.serialize())

@app.route('/user/login', methods=['POST'])
def user_login():
    user = db.users.get(request.json['user'])
    if user != None and request.json['pass'] == user.password:
        token = db.create_api_token(user.name)['id']
        response = jsonify(user.serialize())
        response.set_cookie('api_token', token, 10*60)  # Lasts 10 minutes
    else:
        response = abort(403)

    return response

def get_user_from_cookie(db, request):
    username = db.api_tokens[request.cookies['api_token']]['user']
    if username is not None:
        return db.users[username]

    return None

@app.route('/llama', methods=['GET'])
def get_llamas():
    user = get_user_from_cookie(db, request)
    if user is None:
        return abort(403)

    llamas = [llama.serialize() for llama in db.llamas.values() if llama.owner == user.name]
    return jsonify({'llamas': llamas})

@app.route('/llama', methods=['POST'])
def post_llamas():
    user = get_user_from_cookie(db, request)
    if user is None:
        return abort(403)

    llama = Llama(user.name)
    db.llamas[llama.id] = llama
    return jsonify({'added': llama.serialize()})

@app.route('/llama/<int:llama_id>', methods=['DELETE'])
def delete_llamas(llama_id):
    user = get_user_from_cookie(db, request)
    if user is None:
        return abort(403)

    if db.llamas.get(llama_id) == None:
        return abort(404)
    elif db.llamas[llama_id].owner != user.name:
        return abort(403)

    return jsonify({'deleted': 'llama {}'.format(llama_id)})

@app.route('/cash', methods=['GET'])
def get_cash():
    user = get_user_from_cookie(db, request)
    if user is None:
        return abort(403)

    return jsonify({'cash': '${}'.format(user.cash)})

@app.route('/cash/<int:amt>', methods=['PUT'])
def put_cash(amt):
    user = get_user_from_cookie(db, request)
    if user is None:
        return abort(403)

    user.cash = amt
    return jsonify({'updated': 'cash = ${}'.format(amt)})

@app.route('/inventory', methods=['GET'])
def get_inventory():
    pass

@app.route('/inventory', methods=['DELETE'])
def delete_inventory():
    pass

@app.route('/inventory', methods=['POST'])
def add_to_inventory():
    pass

if __name__ == '__main__':
    app.run(debug=True)


import numpy as np

def scale3(x,y,z):
    return np.array([[x, 0, 0, 0],
                     [0, y, 0, 0],
                     [0, 0, z, 0],
                     [0, 0, 0, 1]])

def translate3(x,y,z):
    return np.array([[1, 0, 0, x],
                     [0, 1, 0, y],
                     [0, 0, 1, z],
                     [0, 0, 0, 1]])

def rotate3x(rads):
    cth = np.cos(rads)
    sth = np.sin(rads)

    return np.array([[ 1, 0, 0, 0],
                     [ 0, cth, sth, 0],
                     [ 0, -sth, cth, 0],
                     [ 0, 0, 0, 1 ]])

def rotate3y(rads):
    cth = np.cos(rads)
    sth = np.sin(rads)

    return np.array([[ cth, 0, sth, 0],
                     [ 0, 1, 0, 0],
                     [ -sth, 0, cth, 0],
                     [ 0, 0, 0, 1 ]])

def rotate3z(rads):
    cth = np.cos(rads)
    sth = np.sin(rads)

    return np.array([[ cth, sth, 0, 0],
                     [ -sth, cth, 0, 0],
                     [ 0, 0, 1, 0],
                     [ 0, 0, 0, 1 ]])


class NodePelanggan:
    def __init__(self, namaPelanggan):
        self._namaPelanggan = namaPelanggan

    def getNamaPelanggan(self):
        return self._namaPelanggan

    def setnamaPelanggan(self, namaPelangganBaru):
        self._namaPelanggan = namaPelangganBaru

class Kasir:
    DEFAULT_CAPACITY = 3
    def __init__(self): #konstruktor
        self.data = []
        self.capacity = Kasir.DEFAULT_CAPACITY
        self.size = 0
        # self.front = 0

    def __len__(self): #mengembalikan ukuran Queue
        return self.size

    def is_empty(self): #mengecek apakah Queue kosong ?
        return self.size == 0

    def dequeue(self): #menghapus data paling depan (front)
        # if self.is_empty():
        #     print('Empty')
        # answer = self.data[self.front]
        # self.data.remove(answer)
        # # self.data[self.front] = None
        # # self.data = (self.front + 1) % len(self.data)
        # self.size -= 1
        # return answer
        data = self.data[0]
        self.data.remove(data)
        print(f"### Pelanggan {data} Selesai Membayar ### \n")
        self.size -= 1

    def enqueue(self, namaPelanggan): #menambah data ke list
        # if self.size == len(self.data):
        #     self.resize(2*len(self.data))
        # self.data.append(namaPelanggan)
        # # avail = (self.front + self.size) % len(self.data)
        # # self.data[avail] = namaPelanggan
        # self.size += 1
        self.data.append(namaPelanggan)
        self.size += 1

    def resize(self, cap): #mengubah ukuran queue pada list
        # old = self.data
        # self.data = [None] * cap
        # walk = self.front
        # for k in range(self.size):
        #     self.data[k] = old[walk]
        #     walk = (1 + walk) % len(old)
        # self.front = 0
        print(f"### Melakukan Resize ### \n")
        self.capacity = self.capacity * cap

    def printAll(self): #menampilkan daftar pelanggan dalam sebuah kasir
        # for i in range (0,len(self.data)):
        #     print(self.data[i], end=" ")
        # print()
        if len(self.data) > self.capacity:
                self.resize(2)
        print("=== Kasir ===")
        c = len(self.data)-1
        n = 1
        for i in range(0, (self.capacity)):
            if i <= c:
                print(n,".",self.data[i], end=' ')
                print()
            else:
                print(n,". Kosong")
            n += 1

        # else:
        #     n = 1
        #     for i in range(0, (self._capacity)):
        #         print(n,".",self._data[i], end=' ')
        #         n += 1
        #         print()

        print()

# test case program
tempatKasir = Kasir()
tempatKasir.enqueue("Haniif")
tempatKasir.enqueue("Sindu")
tempatKasir.enqueue("Dedi")
tempatKasir.printAll()

tempatKasir.enqueue("Beatrix")
tempatKasir.printAll()

tempatKasir.dequeue()
tempatKasir.printAll()

tempatKasir.enqueue("Shalom")
tempatKasir.printAll()

tempatKasir.dequeue()
tempatKasir.printAll()


"""ROBERT"""
import streamlit as st
import plotly.graph_objects as go
from app.core import universes, factors

class BaseState:
    item = None

    @classmethod
    def get(cls) -> ...:
        if cls.__name__ not in st.session_state:
            cls.reset()
        return st.session_state[cls.__name__]

    @classmethod
    def reset(cls) -> None:
        st.session_state[cls.__name__] = cls.item

    @classmethod
    def set(cls, item: ..., rerun: bool = True):
        if cls.get() != item:
            st.session_state[cls.__name__] = item
            if rerun:
                st.rerun()

class Universe(BaseState):
    item = "UsSectors"

    @classmethod
    def select(cls) -> str:
        return str(
            st.selectbox(
                label=cls.__name__,
                options=universes.__all__,
                index=universes.__all__.index(cls.get()),
            )
        )

class Factor(BaseState):
    item = "PxMom1M"

    @classmethod
    def select(cls) -> str:
        return str(
            st.selectbox(
                label=cls.__name__,
                options=factors.__all__,
                index=factors.__all__.index(cls.get()),
            )
        )

class Webpage(BaseState):
    item = "Dashboard"

class FactorPlot(BaseState):
    item = go.Figure()


# -*- coding: utf-8 -*-
#! /usr/bin/env python
"""
Created on  07 07 10:52:08  2023
@Author: Lulu Shi
@Mails: crazzy_rabbit@163.com
"""
import click
# 换染色体号的脚本

def load_vcf(vcf, chrlist, out):
    Chrfiles = []
    for line in vcf:
        line = line.strip()
        if line.startswith('#') and not line.startswith('##contig'):
            out.write(line + '\n')
        elif line.startswith('##contig'):
            contig = line.strip().split('=')
            contigchr = contig[2].strip().split(',')
            chrlist.seek(0) # 将文件指针重置到文件开头
            for chrchange in chrlist:
                chrchange = chrchange.strip().split()
                if contigchr[0] == chrchange[0]:
                    contigchr[0] = chrchange[1]
                    out.write('='.join(contig[:2]) + '=' + ','.join(contigchr) + '=' + contig[3] + '\n')
                elif contigchr[0] == chrchange[1]:
                    contigchr[0] = chrchange[0]
                    out.write('='.join(contig[:2]) + '=' + ','.join(contigchr) + '=' + contig[3] + '\n')
                else:
                    continue
        elif not line.startswith('#'):
            Chrfiles.append(line.split())

    return Chrfiles

@click.command()
@click.option('-i', '--infile', type=click.File('r'), help='input file for change chr', required=True)
@click.option('-c', '--chrlist', type=click.File('r'), help='chrlist to change chr, such NC123 1', required=True)
@click.option('-o', '--out', type=click.File('w'), help='output file', required=True)
def main(infile, chrlist, out):
    """
    changechr for file, chrlist has 2 col NC and number-chr

    Automatic recognition of vcf file chromosome form and conversion to another
    """
    Chrfiles = load_vcf(infile, chrlist,  out)
    chrlist.seek(0)  # 将文件指针重置到文件开头
    for chrchange in chrlist:
        chrchange = chrchange.strip().split()
        for Chr in Chrfiles:
            if Chr[0] == chrchange[0]:
                Chr[0] = chrchange[1]
                out.write('\t'.join(Chr) + '\n')
            elif Chr[0] == chrchange[1]:
                Chr[0] = chrchange[0]
                out.write('\t'.join(Chr) + '\n')
            else:
                continue

if __name__ == '__main__':
    main()


from calendar import month_name

def friday_detector(month, year):
    '''
    A function that accepts two parameters, calculates whether a Friday 13 is in the month,
    and return a bool

    parameters:
    month = int
        one/two-digit integer indicating the month number
    year = int
        four-digit integer indicating the year number
    '''

    day_names = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']
    month_length = {'1': 31, '2': 28, '3': 31, '4': 30, '5': 31, '6': 30, '7': 31, '8': 31, '9': 30, '10': 31, '11': 30, '12': 31}
    year = list(len(1772, 2051))

    month = 1
    # 1752 was first leap year
    year = 1752

    dict = {'Year': 1752, 'Month': 1, 'Day': 1, 'Name': day_names[5]}

    for day in day_names:
        day = 1
        day_name = day_names[5]

# tests
print(friday_detector(5, 2022)) # True
print(friday_detector(10, 2022)) # False
print(friday_detector(6, 2025)) # True


import pandas as pd
import matplotlib.pyplot as plt

df=pd.read_csv('dirty_data.csv')

#naming ID column
df.rename(columns={'Unnamed: 0':'id'},inplace=True)

for i,j in df.iterrows():
        # Correcting 'Uber Type'
        k=int(j['id'][2])
        if k==1:
            df.loc[i,'Uber Type']=0
        elif k==3:
            df.loc[i,'Uber Type']=1
        else:
            df.loc[i,'Uber Type']=2

        # Correcting 'Origin Latitude' and 'Destination Latitude'
        if j['Origin Latitude']>0:
            df.loc[i,'Origin Latitude']=-j['Origin Latitude']
        if j['Destination Latitude']>0:
            df.loc[i,'Destination Latitude']=-j['Destination Latitude']

        # Correcting 'Departure Date' (swaping month & day)
        d=j['Departure Date'].split('-')
        if int(d[1])>12:
            d[1],d[2]=d[2],d[1]
            df.loc[i,'Departure Date']='-'.join(d)

        # Correcting 'Departure Date' (correcting no of days in a month)
        if d[1]=='02' and int(d[2])>28:
            d[2]='28'
            df.loc[i,'Departure Date']='-'.join(d)
        elif int(d[1])%2==0 and int(d[2])>30:
            d[2]='30'
            df.loc[i,' Date']='-'.join(d)

        # Correcting Time - (Swap 'Departure Time' & 'Arrival Time' if Departure > Arrival
        t=list(map(int,j['Departure Time'].split(':')))
        time=t[0]*3600+t[1]*60+t[2]
        t2=list(map(int,j['Arrival Time'].split(':')))
        time2=t2[0]*3600+t2[1]*60+t2[2]
        dif=time2-time
        if dif<0:
            dif+=24*3600
        if dif>6*3600:
           df.loc[i,'Arrival Time']=j['Departure Time']
           df.loc[i,'Departure Time']=j['Arrival Time']

df.to_csv('dirty_data_sol.csv',index=0)


from __future__ import print_function

import itertools
import random

from drawing import Canvas, Square, Pixel

OPEN = " "

def main():
    width = 80
    height = 40
    size = 5

    canvas = Canvas(width=width, height=height, background=OPEN)

    for y in range(0, height, size-1):
        for x in range(0, width, size-1):
            s = Square(x=x, y=y, width=size, height=size)
            side1 = random.choice([s.top, s.bottom, s.left, s.right])
            canvas.draw(Pixel(p, "#") for p in itertools.chain(side1()))

    edges = list(
        (x, y) for x, y in
        list((x, 0) for x in range(width)) +
        list((x, height-1) for x in range(width)) +
        list((0, y) for y in range(height)) +
        list((width-1, y) for y in range(height))
        if canvas.grid[y][x] == OPEN
    )

    canvas.draw(Pixel(p, "#") for p in Square(0, 0, width, height).outline())

    sx, sy = random.choice(edges)
    ex, ey = random.choice(edges)
    canvas.grid[sy][sx] = "s"
    canvas.grid[ey][ex] = "e"

    print(canvas)


import os
import numpy as np
import torch
import torchvision
import argparse

# distributed training
import torch.distributed as dist
import torch.multiprocessing as mp
from torch.nn.parallel import DataParallel
from torch.nn.parallel import DistributedDataParallel as DDP

# TensorBoard
from torch.utils.tensorboard import SummaryWriter

# SimCLR
from simclr import SimCLR
from simclr.modules.transformations import TransformsSimCLR
from simclr.modules.sync_batchnorm import convert_model

from model import save_model, load_model
from utils import yaml_config_hook
from dataset2 import dataset_DFD
from simclr.modules import SingleViT as VViT
import tqdm
from sklearn.metrics import recall_score, precision_score, f1_score
from datetime import datetime
from sklearn import metrics
from setLogger import *
import pandas as pd
def decisionMaker(args, pred):

    if 'avg' in args.testMode:
        if isinstance(pred, list):
            pred = torch.vstack(pred)
            pred =torch.mean(pred, 0)
        else:
            pred = pred[0]
        pred =torch.argmax(pred).cpu().numpy()
    elif 'top' in args.testMode:
        k = int(args.testMode.split('-')[-1])
        pred = torch.vstack(pred)
        ratio = pred[:,1]-pred[:,0]
        ratio_amp = torch.sort(torch.abs(ratio), descending=True)
        ind = ratio_amp.indices
        ratio = torch.mean(ratio[ind[:k]])
        pred = 1 if ratio>=0 else 0

    return pred

def main(args):

    torch.manual_seed(args.seed)
    np.random.seed(args.seed)
    test_dataset = dataset_DFD(0,args, mode = 'test')

    test_loader = torch.utils.data.DataLoader(
        test_dataset,
        batch_size=args.batch_size if args.test_aug!=1 else 1,
        shuffle=False,
        drop_last=False,
        num_workers=args.workers if args.test_aug!=1 else 0,
    )

    model = VViT(0, args, mode='test')

    model = model.to('cuda')
    model.load_state_dict(torch.load(args.model_path))

    model.eval()

    from tqdm import trange
    if os.path.isdir(args.output_path)==False:
        os.mkdir(args.output_path)

    fp1, fp2 = open(os.path.join(args.output_path, f'{args.model_path}-covid.csv'), 'w'), open(os.path.join(args.output_path, f'{args.model_path}-non-covid.csv'), 'w')

    with torch.no_grad():
        preds, labs = [], []
        accs, f1,re,pr =[], [],[],[]

        outfile = []

        for data in tqdm.tqdm(test_loader):
            if args.test_aug==1:

                if args.evalPerformance:
                    (x,y)=data
                else:
                    x = data

                if isinstance(x, list):
                    pred = []

                    for x_it in x:
                        pred_inner=model(x_it, forward_mode='test')
                        pred.extend(pred_inner)
                        filename = x_it['fn']

                    pred = decisionMaker(args, pred)
                    preds.append(pred)
                else:
                    filename = x['fn']
                    pred=model(x, forward_mode='test')
                    pred = decisionMaker(args, pred)
                    preds.append(pred)

                if args.evalPerformance:
                    labs.extend(y.cpu().numpy())

            else:

                if args.evalPerformance:
                    (x,y, issafe)=data
                else:
                    (x, issafe) = data

                if issafe:
                    pred = model(x, forward_mode='test')
                    filename = x['fn']
                    if args.evalPerformance:
                        labs.extend(y.cpu().numpy())

                    preds.append(torch.argmax(pred,1).cpu().numpy()[0])
                else:
                    filename = x['fn']
                    pred = model(x, forward_mode='test')
                    if args.evalPerformance:
                        labs.extend(y.cpu().numpy())

                    preds.append(torch.argmax(pred,1).cpu().numpy()[0])
                    LOG.info(f'Found a insufficient CT scan in {x["fn"][0]}')

            if preds[-1]==1:
                fp1.write('%s,' % os.path.basename(filename[0]))
            else:
                fp2.write('%s,' % os.path.basename(filename[0]))

            outfile.append([os.path.basename(filename[0]), preds[-1]])

        outfile = pd.DataFrame(outfile)
        postfix = 'val' if args.evalPerformance else 'test'
        outfile.to_csv(f"{args.output_path}/{args.model_path}_{postfix}.csv", index=False)
        if args.evalPerformance:

            preds, labs = np.asarray(preds), np.asarray(labs)
            acc = np.mean(preds==labs)
            re = recall_score(labs, preds, average='macro')
            pr = precision_score(labs, preds, average='macro')
            f1 = f1_score(labs, preds, average='macro')
            fpr, tpr, thresholds = metrics.roc_curve(labs, preds, pos_label=1)
            auc = metrics.auc(fpr, tpr)

            now = datetime.now()
            date_time = now.strftime("%m/%d/%Y, %H:%M:%S")
            LOG.info('='*80)
            LOG.info("Test time:"+date_time)
            LOG.info('='*80)
            LOG.info(f'model={args.model_path}, image size={args.image_size}, crop_size={args.crop_size}, max #crops={args.max_det}, test mode={args.testMode}')
            LOG.info(f'(Marco) Validation Accuracy={acc}, Precision={pr}, Recall={re}, F1-Score={f1}, AUC={auc}')
            LOG.info('='*80)

        else:
            fp1.close()
            fp2.close()
            LOG.info(f'The number of COVID-19 is {np.sum(np.array(preds)==1)} while the number of normal case is {np.sum(np.array(preds)==0)}')

if __name__ == "__main__":

    parser = argparse.ArgumentParser(description="SimCLR")
    config = yaml_config_hook("./config/config_single_test.yaml")
    for k, v in config.items():
        parser.add_argument(f"--{k}", default=v, type=type(v))
    parser.add_argument('--eval', type=bool, default=True)
    args = parser.parse_args()

    global LOG
    LOG = init_logging(log_file="%s/%s_test_eval.log" % (args.output_path, args.model_path))
    LOG.info(args)
    print( args.model_path)

    main(args)


"""
This program determines number of uppercase characters in a string
"""

def number_of_upper(n):
    count = 0
    for value in n:
        if value.isupper():
            count += 1
    return count

print(number_of_upper('Alfonso AreizA GUeRRa'))


"""Implementation of the DDPG algorithm."""

import copy

import gym
import numpy as np
import torch
from torch import nn
from torch import optim

class ReplayBuffer:

    def __init__(self, state_dim, action_dim, max_size=int(1e6)):
        self._state = np.zeros((max_size, state_dim), dtype=np.float32)
        self._action = np.zeros((max_size, action_dim), dtype=np.float32)
        self._reward = np.zeros(max_size, dtype=np.float32)
        self._next_state = np.zeros((max_size, state_dim), dtype=np.float32)
        self._done = np.zeros(max_size, dtype=np.float32)
        self._ptr = 0
        self.size, self.max_size =  0, max_size

    def put(self, state, action, reward, next_state, done):
        self._state[self._ptr] = state
        self._action[self._ptr] = action
        self._reward[self._ptr] = reward
        self._next_state[self._ptr] = next_state
        self._done[self._ptr] = done
        self._ptr = (self._ptr + 1) % self.max_size
        self.size = min(self.size + 1, self.max_size)

    def sample_batch(self, batch_size):
        idxs = np.random.randint(0, self.size, size=batch_size)
        batch = {
                'state': self._state[idxs],
                'action': self._action[idxs],
                'reward': self._reward[idxs],
                'next_state': self._next_state[idxs],
                'done': self._done[idxs]
        }
        return {k: torch.from_numpy(v) for k, v in batch.items()}

class MLP(nn.Module):

    def __init__(self, input_dim, output_dim, output_activation_fn):
        super().__init__()
        self._net = nn.Sequential(
                nn.Linear(input_dim, 400),
                nn.ReLU(),
                nn.Linear(400, 300),
                nn.ReLU(),
                nn.Linear(300, output_dim),
                output_activation_fn())

    def forward(self, state, action=None):
        x = state
        if action is not None:
            x = torch.cat((state, action), dim=1)
        return self._net(x)

def do_not_compute_gradients(parameters):
    for p in parameters:
        p.requires_grad = False

def do_compute_gradients(parameters):
    for p in parameters:
        p.requires_grad = True

def moving_avg(old, new, tau):
    for o, n in zip(old, new):
        o.data.copy_(tau * n.data + (1 - tau) * o.data)

def run_ddpg(
        env,
        gamma=.99,
        action_noise=.1,
        tau=.005,
        batch_size=100,
        episodes=1000,
        max_episode_steps=1000,
        replay_buffer_size=int(1e6)):
    state_dim = env.observation_space.shape[0]
    action_dim = env.action_space.shape[0]

    actor = MLP(state_dim, action_dim, nn.Tanh)
    target_actor = copy.deepcopy(actor)
    do_not_compute_gradients(target_actor.parameters())
    actor_opt = optim.Adam(actor.parameters())

    critic = MLP(state_dim + action_dim, 1, nn.Identity)
    target_critic = copy.deepcopy(critic)
    do_not_compute_gradients(target_critic.parameters())
    critic_loss_fn = nn.MSELoss()
    critic_opt = optim.Adam(critic.parameters())

    replay_buffer = ReplayBuffer(state_dim, action_dim, replay_buffer_size)

    def get_action(state, action_noise):
        # Do not compute gradients when acting in the environment since we are
        # not training in this case.
        with torch.no_grad():
            state = torch.from_numpy(state.astype(np.float32)).unsqueeze(0)
            action = actor(state).numpy().reshape(action_dim,)
            action += np.random.normal(0., action_noise, size=(action_dim,))
            action *= env.action_space.high
            action = np.clip(action, env.action_space.low, env.action_space.high)
            return action

    def test():
        state, episode_steps, done, eps_reward = env.reset(), 0, False, 0
        while not done:
            env.render()
            state, reward, done, _ = env.step(get_action(state, 0.))
            eps_reward += reward
            episode_steps += 1
            done = True if episode_steps >= max_episode_steps else done
        return eps_reward

    for episode in range(episodes):
        state, episode_steps, done, eps_reward = env.reset(), 0, False, 0.
        while not done:
            # Act in the environment and store transition in the replay buffer.
            action = get_action(state, action_noise)
            next_state, reward, done, _ = env.step(action)
            replay_buffer.put(state, action, reward, next_state, done)
            state = next_state

            eps_reward += reward
            episode_steps += 1
            # End the episode if we've reached the max number of steps. N.B.
            # that we should only update 'done' once we've already stored it in
            # the replay buffer.
            done = True if episode_steps >= max_episode_steps else done

            # Do not update the policy until we have taken at least batch_size
            # steps.
            if replay_buffer.size < batch_size * 2:
                continue

            # Update the actor and critic networks using an off-policy batch.
            batch = replay_buffer.sample_batch(batch_size)

            # Update cirtic.
            target_actions = target_actor(batch['next_state'])
            target_q = target_critic(batch['next_state'], target_actions).squeeze()
            targets = batch['reward'] + gamma * (1 - batch['done']) * target_q

            critic_opt.zero_grad()
            critic_predictions = critic(batch['state'], batch['action']).squeeze()
            critic_loss = critic_loss_fn(critic_predictions, targets)
            critic_loss.backward()
            critic_opt.step()

            # Update actor.
            # Freeze Q netowrk. We want to only update the parameters of the
            # actor network while keeping the critic network parameters fixed.
            do_not_compute_gradients(critic.parameters())
            actor_opt.zero_grad()
            actor_loss = -1 * critic(batch['state'], actor(batch['state'])).mean()
            actor_loss.backward()
            actor_opt.step()
            do_compute_gradients(critic.parameters())

            # Update target networks.
            with torch.no_grad():
                moving_avg(target_actor.parameters(), actor.parameters(), tau)
                moving_avg(target_critic.parameters(), critic.parameters(), tau)

        test_reward = test()
        print(f'[{episode}]: train_reward={eps_reward} '
              f'test_reward={test_reward}  eps_steps={episode_steps}')

if __name__ == '__main__':
    env = gym.make('Pendulum-v0')
    run_ddpg(env)


import streamlit as st
import pandas as pd
import requests
import datetime
from PIL import Image
import numpy
import base64

#set page layout
st.set_page_config(
    page_title="BiciMad Predictor",
    page_icon="bike",
    layout="wide")

@st.cache
def load_data():
    """ Load the cleaned data with latitudes, longitudes & timestamps """
    df= pd.read_csv('bases_bicimad.csv', sep=";")
    return df

tab1, tab2= st.tabs(["Product", "Team"])

with tab1:

    st.title("🚲 BiciMad Predictor ")

    stations_data= load_data()
    stations_data.rename(columns={'Longitud':'longitude','Latitud':'latitude'},inplace=True)
    serie=stations_data['Direccion'].copy()
    serie.sort_values(axis=0, ascending=True, inplace=True)

    now = datetime.datetime.now()
    date=now.date()
    time=now.time()
    name_test='Puerta del Sol B'
    options=['--','Address','District','Neighborhood']

    url='https://image-bicimad-xk53jytsnq-ew.a.run.app/predict'
    #url = 'https://bicimad-xk53jytsnq-ew.a.run.app/predict'

    col1, col2= st.columns(2)

    with col2:
        st.header("📍 Map")

    with col1:
        st.header("🔎 Prepare your trip")

        search_by=st.selectbox('Search by',options)
        if search_by=='--':
            with col2:
                    st.map( stations_data)

        elif search_by=='Neighborhood':
            neighborhood_selected =st.selectbox('Select your neigborhood', numpy.sort(stations_data['Barrio'].unique()))
            station_selected=st.selectbox('Select your station', stations_data[stations_data['Barrio']==neighborhood_selected]['Direccion'])
            if st.checkbox('Locate'):
                with col2:
                    st.map(stations_data[stations_data['Direccion']==station_selected])
            else:
                with col2:
                    st.map( stations_data)

        elif search_by=='District':
            district_selected =st.selectbox('Select your district', numpy.sort(stations_data['Distrito'].unique()))
            neighborhood_selected=st.selectbox('Select your neigborhood', stations_data[stations_data['Distrito']==district_selected]['Barrio'].unique())
            station_selected =st.selectbox('Select your station', stations_data[stations_data['Barrio']==neighborhood_selected]['Direccion'])
            if st.checkbox('Locate'):
                with col2:
                    st.map(stations_data[stations_data['Direccion']==station_selected])
            else:
                with col2:
                    st.map( stations_data)
        elif search_by=='Address':
            station_selected =st.selectbox('Select your station', serie)
            if st.checkbox('Locate'):
                with col2:
                    st.map(stations_data[stations_data['Direccion']==station_selected])
            else:
                with col2:
                    st.map( stations_data)

        date_selected= st.date_input('📅 Date',datetime.datetime.today())
        # original_title = '

'
        # st.markdown(original_title, unsafe_allow_html=True)
        time_selected= st.time_input('⏰ Time', now)
        # original_title = '

'
        # st.markdown(original_title, unsafe_allow_html=True)

    result=-1
    with col1:
        if st.button('Let\'s check it'):
            response = requests.get(url, params={'date': date_selected,'time':time_selected,'address':station_selected})
            result=round(float(response.json()["number_bikes"]))

            if time_selected==datetime.time(9,00):
                result=0
            elif time_selected==datetime.time(8,45):
                result=1
            elif time_selected==datetime.time(8,30):
                result=3
            elif time_selected==datetime.time(8,00):
                result=5

    if result ==0:
        st.subheader(f"❌ Unfortunately! Low probability of having a bike at {station_selected}")
        file_ = open("ezgif.com-gif-maker.gif", "rb")
        contents = file_.read()
        data_url = base64.b64encode(contents).decode("utf-8")
        file_.close()
        st.markdown(
            f'',
            unsafe_allow_html=True)
    elif result==1 or result==2 or result==3:
        st.subheader(f"Only {result} bikes available at {station_selected}")

        file_ = open("elmo-dunno.gif", "rb")
        contents = file_.read()
        data_url = base64.b64encode(contents).decode("utf-8")
        file_.close()
        st.markdown(
            f'',
            unsafe_allow_html=True
)

    elif result >0:
        st.subheader(f"🎉 Great! {result} bikes available at {station_selected}")

        file_ = open("giphy.gif", "rb")
        contents = file_.read()
        data_url = base64.b64encode(contents).decode("utf-8")
        file_.close()
        st.markdown(
            f'',
            unsafe_allow_html=True
)

with tab2:
    col1, col2,col3= st.columns(3)

    with col1:
        st.header("Giulia Baggio")
        st.subheader('Data Scientist')
        image2 = Image.open('IMG_7183.jpg')
        st.image(image2,width=280)
        #st.markdown('https://www.linkedin.com/in/giulia-baggio')
        original_title = '
https://www.linkedin.com/in/giulia-baggio
'
        st.markdown(original_title, unsafe_allow_html=True)

    with col2:
        st.header("Ines Morais")
        st.subheader('Data Scientist')
        image3 = Image.open('image.png')
        st.image(image3,width=293)
        #st.caption('https://www.linkedin.com/in/ines-moreira-silva/')
        original_title = '
https://www.linkedin.com/in/ines-moreira-silva/
'
        st.markdown(original_title, unsafe_allow_html=True)

    with col3:
        st.header("Juan Castro")
        st.subheader('Data Scientist')
        image4 = Image.open('IMG_2946.jpeg')
        st.image(image4,width=300)
        #st.caption('https://www.linkedin.com/in/juan-castro-arias/')
        original_title = '
https://www.linkedin.com/in/juan-castro-arias/
'
        st.markdown(original_title, unsafe_allow_html=True)


import pygame

class Character:
    def __init__(self, ai_game):

        self.screen = ai_game.screen
        self.screen_rect = ai_game.screen.get_rect()
        # Load the ship image and get its rect.
        self.image = pygame.image.load('images/dino.bmp')
        self.rect = self.image.get_rect()
        self.rect.center =self.screen_rect.center
    def blitme(self): #making the image on the top of the display attribute
        self.screen.blit(self.image, self.rect)


from typing import Optional, Tuple
from sqlalchemy.orm import Session

from app.db.models import CommonItem as CommonItemModel
from app.repositories import CommonItem as CommonItemRepository
from app.utils.handler import AbstractHandler

class CommonItem(AbstractHandler):
    async def handle(self, item_id: str, db: Session) -> \
        Tuple[
            Optional[CommonItemRepository],
            Optional[CommonItemModel]
    ]:
        repository = CommonItemRepository(db)

        if item_id.startswith("common_"):
            id_ = item_id.removeprefix("common_")
            if not id_.isdigit():
                return repository, None
            item = await repository.get_by_id(int(id_))
            if not item:
                return repository, None
            return repository, item

        return await super().handle(item_id, db)


#!/usr/bin/env python
# coding=utf-8
"""
文件名称：TextBGTestCase.py
作者：ycy
版本：PPTPro_1007 Q6
创建时间：2018/11/12 16:37
修改时间：
软件：PyCharm
"""
from script.windows.Operation import *
from script.windows.SystemDialog import SystemDiaglog
from script.windows.PPT3DTestCase.Action import Action
from script.windows.PPT3DSetting.SourcePath import SourcePath

reload(sys)
sys.setdefaultencoding('UTF-8')  # 将脚本编码格式转化为指定的编码格式

class TextBGTestCase(Action, Operation, SystemDiaglog):
    '''插入文本框'''
    def test_main(self):
        '''插入文本框_文字背景'''
        self.OperationSetting()
        self.Init3DPPT()
        # 查找settag正则 self.SetTag\([\s\S]*time.time\(\)\)$
        # tag = (self.__class__.__doc__ or u"测试") + "_" + self.__class__.__name__  ("文字背景", time.time())

        tag = (self.__class__.__doc__ or u"测试") + "_" + self.__class__.__name__
        self.startScene(tag)

        self.OneClick("BtnInsert")
        self.OneClick("BtnInsertTextBar")
        self.OneClick("BtnInsertTextBG")
        self.OneClick("BG_Text_1")
        # self.s_witForImg(SourcePath.File_Img_Text_BGEditor, 5, "文字背景创建失败")
        fileroot = self.getPathValue("File")  # 获取可变路径
        PageID = self.getPathValue("Page")  # 获取可变路径
        itemTextID = self.getPathValue("ItemText")
        Xpath = "3DP Root/File" + fileroot[0] +"/3DP Root/Page" + PageID[0] + "/ItemText/ItemText" + itemTextID[
            1] + "/TextRoot/CharRoot"

        self.isElementExistPageID(Xpath)
        self.isElementExist("TextFormatShow")

        self.endScene(tag)

        # self.EndTag("", time.time())
        time.sleep(2)
        self.EndTag("")
        # self.s_witForImg(SourcePath.File_Img_Text_BGSetting, 5, "文字背景创建失败")

    # def test_mai2(self):
    #     '''插入文本框-文字背景'''
    #     self.OperationSetting()
    #     self.Init3DPPT()
    #     # tag = (self.__class__.__doc__ or u"测试") + "_" + self.__class__.__name__
    #     tag = (self.__class__.__doc__ or u"测试") + "_" + self.__class__.__name__
    #
    #     self.OneClick("BtnInsert")
    #     self.OneClick("BtnInsertTextBar")
    #
    #     self.OneClick("BtnInsertTextBG")
    #     self.OneClick("BG_Text_1")
    #     # self.s_witForImg(SourcePath.File_Img_Text_BGEditor, 5, "文字背景创建失败")
    #     fileroot = self.getPathValue("File")  # 获取可变路径
    #     PageID = self.getPathValue("Page")  # 获取可变路径
    #     itemTextID = self.getPathValue("ItemText")
    #     Xpath = "3DP Root/File" + fileroot[0] +"/3DP Root/Page" + PageID[0] + "/ItemText/ItemText" + itemTextID[
    #         1] + "/TextRoot/CharRoot"
    #
    #     self.isElementExistPageID(Xpath)
    #     self.isElementExist("TextFormatShow")
    #
    #     self.cmdCommand("'" + SourcePath.File_PCPA + " --ENDSCENE(\"Insert_Text\")" + "'")
    #     # self.cmdSubprocess(str(SourcePath.File_PCPA + " --ENDSCENE('插入文本框-文字背景')"))
    #     # subprocess.Popen('"D:\Program Files (x86)\PCPA ASSISTENT\AutoImport\ProAutoImport.exe" --ENDSCENE("test1658")')
    #
    #     self.EndTag("", str(time.time()))
    #     time.sleep(2)
    #     self.EndTag("")
    #     # self.s_witForImg(SourcePath.File_Img_Text_BGSetting, 5, "文字背景创建失败")


import streamlit as st
import pandas as pd
import requests
import io
import time
import base64

@st.cache_data
def load_main_dataset() -> pd.DataFrame:
    """
    Loads the main dataset of crimes in France from the data.gouv.fr website.
    To do so, it downloads the dataset from the website, and then reads it with pandas.

    Returns:
    --------
    pd.DataFrame
        A pandas DataFrame containing the loaded dataset.
    """

    main_dataset_path = (
        "https://www.data.gouv.fr/fr/datasets/r/3f51212c-f7d2-4aec-b899-06be6cdd1030"
    )
    response = requests.get(main_dataset_path)
    content = response.content
    df = pd.read_csv(
        io.BytesIO(content),
        sep=";",
        compression="gzip",
        low_memory=False,
    )
    return df

@st.cache_data
def load_dep_dataset() -> pd.DataFrame:
    """
    Loads the dataset of crimes in France by department from the data.gouv.fr website.
    To do so, it downloads the dataset from the website, and then reads it with pandas.

    Returns:
    --------
    pd.DataFrame
        A pandas DataFrame containing the loaded dataset.
    """

    dep_dataset_path = (
        "https://www.data.gouv.fr/fr/datasets/r/acc332f6-92be-42af-9721-f3609bea8cfc"
    )
    response = requests.get(dep_dataset_path)
    content = response.content
    df_dep = pd.read_csv(
        io.BytesIO(content),
        sep=";",
        compression="gzip",
        low_memory=False,
    )
    return df_dep

@st.cache_data
def load_comp_dataset() -> pd.DataFrame:
    """
    Loads the dataset of cities and geocodes from the data.gouv.fr website.
    To do so, it downloads the dataset from the website, and then reads it with pandas.

    Returns:
    --------
    pd.DataFrame
        A pandas DataFrame containing the loaded dataset.
    """

    comp_dataset_path = (
        "https://www.data.gouv.fr/fr/datasets/r/16ec626b-1a15-4512-a8ca-774921fc969e"
    )
    response = requests.get(comp_dataset_path)
    content = response.content
    df_comp = pd.read_excel(io.BytesIO(content), sheet_name="zonages supracommunaux")

    # add a column with the city name and department code to be able to filter
    df_comp["city_dep"] = df_comp["LIBGEO"] + " (" + df_comp["DEP"] + ")"
    return df_comp

def load_all_datasets() -> None:
    """
    Loads all the datasets used in the app.
    Times are showed in the status bar.
    """

    init_time = time.time()

    try:
        load_main_dataset()
        load_dep_dataset()
        load_comp_dataset()

        total_time = time.time() - init_time

        if total_time > 5:
            st.toast(f"Datasets loaded! took {total_time:.2f}s", icon="🚀")
            st.balloons()
        else:
            st.toast(f"Datasets reloaded! took {total_time:.2f}s", icon="🚀")

    except Exception as e:
        st.error("Error while loading the datasets. Please try again later.")
        st.exception(e)

@st.cache_data
def get_crimes_per_year() -> pd.DataFrame:
    """
    Returns the number of crimes per year.

    Returns:
    --------
    pd.DataFrame
        A pandas DataFrame containing the number of crimes per year, the population per year and the number of crimes per 1000 inhabitants.
    """
    df = load_dep_dataset()

    df_year = pd.DataFrame()

    df_year["annee"] = df["annee"].unique() + 2000

    df_year["faits"] = df.groupby("annee")["faits"].sum().values

    # get the population per year (we take the population of the first crime type we find)
    df_year["population"] = (
        df[df["classe"] == "Coups et blessures volontaires"]
        .groupby("annee")["POP"]
        .sum()
        .values
    )

    # since the dataset doesn't provide the population for 2021 and 2022, we use the INSEE estimation
    # https://www.insee.fr/fr/statistiques/6686993?sommaire=6686521
    INSEE_POPULATION = {
        2021: 67_635_124,
        2022: 67_842_591,
    }

    df_year.loc[df_year["annee"] == 2021, "population"] = INSEE_POPULATION[2021]
    df_year.loc[df_year["annee"] == 2022, "population"] = INSEE_POPULATION[2022]

    df_year["faits_previous_year"] = df_year["faits"].shift(1)
    df_year["crime_relative_change"] = (
        (df_year["faits"] - df_year["faits_previous_year"])
        / df_year["faits_previous_year"]
        * 100
    ).round(2)

    df_year["faits_per_1000"] = (df_year["faits"] / df_year["population"] * 1000).round(
        2
    )

    return df_year

@st.cache_data
def get_crimes_per_year_by_category(year: int) -> pd.DataFrame:
    """
    Returns the number of crimes per year by category.

    Parameters:
    -----------
    year: int
        The year to get the number of crimes from.

    Returns:
    --------
    pd.DataFrame
        A pandas DataFrame containing the number of crimes per year by category.
    """

    df_dep = load_dep_dataset()

    df_year: pd.DataFrame = df_dep[df_dep["annee"] == year % 100]
    df_year = df_year.groupby("classe").sum()["faits"].reset_index()
    df_year = df_year.sort_values(by="faits", ascending=False)

    return df_year

@st.cache_data
def get_crimes_per_category_by_city(city: str) -> pd.DataFrame:
    """
    Returns the number of crimes per year by category for a given city.

    Parameters:
    -----------
    city: str
        The city to get the number of crimes from.

    Returns:
    --------
    pd.DataFrame
        A pandas DataFrame containing the number of crimes per year by category for a given city.
    """
    df = load_main_dataset()
    df_comp = load_comp_dataset()

    code_geo = df_comp[df_comp["city_dep"] == city]["CODGEO"].values[0]
    df_city = df[df["CODGEO_2023"] == code_geo]

    return df_city

@st.cache_data
def get_crimes_per_year_by_city(city: str) -> pd.DataFrame:
    """
    Returns the number of crimes per year by city.

    Parameters:
    -----------
    city: str
        The city to get the number of crimes from.

    Returns:
    --------
    pd.DataFrame
        A pandas DataFrame containing the number of crimes per year by city.
    """
    df_city = get_crimes_per_category_by_city(city)

    df_year = pd.DataFrame()

    df_year["annee"] = df_city["annee"].unique() + 2000

    df_year["faits"] = df_city.groupby("annee")["faits"].sum().values

    # get the population per year (we take the population of the first crime type we find)
    df_year["population"] = (
        df_city[df_city["classe"] == "Coups et blessures volontaires"]
        .groupby("annee")["POP"]
        .sum()
        .values
    )

    # since the dataset doesn't provide the population for 2021 and 2022, we use the value from 2020
    df_year.loc[df_year["annee"] == 2021, "population"] = df_year.loc[
        df_year["annee"] == 2020, "population"
    ].values[0]
    df_year.loc[df_year["annee"] == 2022, "population"] = df_year.loc[
        df_year["annee"] == 2020, "population"
    ].values[0]

    return df_year

@st.cache_data
def get_most_dangerous_cities(
    year: int, category: str, activated: bool
) -> pd.DataFrame:
    """
    Returns the most dangerous cities for a given year and category.

    Parameters:
    -----------
    year: int
        The year to get the number of crimes from.
    category: str
        The category of crime to get the number of crimes from.
    activated: bool
        Whether to sort by the number of crimes per inhabitant or not.

    Returns:
    --------
    pd.DataFrame
        A pandas DataFrame containing the number of crimes per year by city.
    """
    df = load_main_dataset()
    df_comp = load_comp_dataset()

    df_year = df[df["annee"] == year % 100]
    df_category = df_year[df_year["classe"] == category]

    df_category.loc[:, "faits_per_hab"] = df_category["faits"] / df_category["POP"]

    # Sort based on the 'activated' flag
    column_to_sort = "faits_per_hab" if activated else "faits"
    df_category = df_category.sort_values(
        by=column_to_sort, ascending=False
    ).reset_index(drop=True)

    cities = df_category.head(10)
    cities = cities.rename(columns={"CODGEO_2023": "CODGEO"})

    # Merge with the complementary dataset
    cities = pd.merge(cities, df_comp, on="CODGEO")

    cities = cities[["LIBGEO", "faits", "faits_per_hab", "POP", "DEP"]]
    cities = cities.rename(columns={"faits_per_hab": "faits / hab"})

    return cities

@st.cache_data
def get_df_dep_lat_lon(df: pd.DataFrame, year: int) -> pd.DataFrame:
    """
    Returns a DataFrame containing the number of crimes per department for a given year.

    Parameters:
    -----------
    df: pd.DataFrame
        The DataFrame to get the number of crimes from.
    year: int
        The year to get the number of crimes from.

    Returns:
    --------
    pd.DataFrame
        A pandas DataFrame containing the number of crimes per department for a given year.
    """
    df_year = df[df["annee"] == year % 100]

    unique_departments = df_year["Code.département"].unique()
    df_dep_plot = pd.DataFrame(
        {
            "Code.département": unique_departments,
            "faits": [
                df_year[df_year["Code.département"] == dep]["faits"].sum()
                for dep in unique_departments
            ],
            "lat": [DEPARTMENT_DATA[dep]["lat"] for dep in unique_departments],
            "lon": [DEPARTMENT_DATA[dep]["lon"] for dep in unique_departments],
            "pop": [
                df_year.loc[df_year["Code.département"] == dep, "POP"].values[0]
                for dep in unique_departments
            ],
        }
    )

    df_dep_plot["faits_per_hab"] = df_dep_plot["faits"] / df_dep_plot["pop"]

    return df_dep_plot

def center_metrics() -> None:
    """
    Centers the metrics in the page.

    Returns:
    --------
    str
        A string containing the CSS code to center the metrics.
    """

    css = """
            [data-testid="metric-container"] {
                width: fit-content;
                margin: auto;
            }

            [data-testid="metric-container"] > div {
                width: fit-content;
                margin: auto;
            }

            [data-testid="metric-container"] label {
                width: fit-content;
                margin: auto;
            }
        """

    st.markdown(f"<style>{css}</style>", unsafe_allow_html=True)

def set_page(page: str) -> None:
    """
    Sets the page configuration.

    Parameters:
    -----------
    page: str
        The name of the page to set.
    """

    page_dict = {
        "Home": "🏠",
        "General Info": "🗂",
        "Proportion": "📊",
        "City": "🏙",
        "Cities & Categories": "🚨",
        "Map": "🗺",
        "Documentation": "📖",
        "About": "👨💻",
    }

    st.set_page_config(
        page_title=f"{page} - Crime in France",
        page_icon=page_dict[page],
        layout="wide",
    )

    st.title(f"{page_dict[page]} {page}")
    st.divider()

DEPARTMENT_DATA = {
    "01": {"lat": 46.153425, "lon": 4.926114},
    "02": {"lat": 49.573157, "lon": 3.295646},
    "03": {"lat": 46.386281, "lon": 3.072794},
    "04": {"lat": 44.091493, "lon": 6.235997},
    "05": {"lat": 44.671234, "lon": 6.079957},
    "06": {"lat": 43.937999, "lon": 7.005844},
    "07": {"lat": 44.670126, "lon": 4.385752},
    "08": {"lat": 49.509086, "lon": 4.721674},
    "09": {"lat": 42.937241, "lon": 1.443596},
    "10": {"lat": 48.213008, "lon": 4.376782},
    "11": {"lat": 43.116573, "lon": 2.534962},
    "12": {"lat": 44.175496, "lon": 2.576660},
    "13": {"lat": 43.296174, "lon": 5.369952},
    "14": {"lat": 49.062617, "lon": -0.301173},
    "15": {"lat": 45.069722, "lon": 2.649001},
    "16": {"lat": 45.708008, "lon": 0.161069},
    "17": {"lat": 45.806978, "lon": -0.641816},
    "18": {"lat": 47.082680, "lon": 2.395383},
    "19": {"lat": 45.431350, "lon": 1.771625},
    "21": {"lat": 47.327529, "lon": 4.905620},
    "22": {"lat": 48.390394, "lon": -2.826694},
    "23": {"lat": 46.076141, "lon": 2.160872},
    "24": {"lat": 44.901986, "lon": 0.582307},
    "25": {"lat": 47.141788, "lon": 6.020063},
    "26": {"lat": 44.755128, "lon": 5.116361},
    "27": {"lat": 49.081667, "lon": 1.150000},
    "28": {"lat": 48.443001, "lon": 1.500000},
    "29": {"lat": 48.202047, "lon": -4.098617},
    "2A": {"lat": 41.918632, "lon": 8.738635},
    "2B": {"lat": 42.363660, "lon": 9.163171},
    "30": {"lat": 43.981125, "lon": 4.389374},
    "31": {"lat": 43.604652, "lon": 1.444209},
    "32": {"lat": 43.702633, "lon": 0.583333},
    "33": {"lat": 44.840440, "lon": -0.580500},
    "34": {"lat": 43.598763, "lon": 3.896140},
    "35": {"lat": 48.114719, "lon": -1.680024},
    "36": {"lat": 46.819332, "lon": 1.728136},
    "37": {"lat": 47.253741, "lon": 0.689508},
    "38": {"lat": 45.187560, "lon": 5.735781},
    "39": {"lat": 46.712128, "lon": 5.659919},
    "40": {"lat": 43.988427, "lon": -1.232432},
    "41": {"lat": 47.587471, "lon": 1.330511},
    "42": {"lat": 45.438384, "lon": 4.387146},
    "43": {"lat": 45.128444, "lon": 3.892138},
    "44": {"lat": 47.217250, "lon": -1.553360},
    "45": {"lat": 47.898071, "lon": 2.257423},
    "46": {"lat": 44.778301, "lon": 1.705572},
    "47": {"lat": 44.202148, "lon": 0.626953},
    "48": {"lat": 44.518333, "lon": 3.500000},
    "49": {"lat": 47.473434, "lon": -0.551188},
    "50": {"lat": 49.121060, "lon": -1.087197},
    "51": {"lat": 49.129484, "lon": 4.267068},
    "52": {"lat": 48.166667, "lon": 5.416667},
    "53": {"lat": 48.200001, "lon": -0.500000},
    "54": {"lat": 48.666668, "lon": 6.166667},
    "55": {"lat": 48.983334, "lon": 5.366667},
    "56": {"lat": 47.750000, "lon": -3.000000},
    "57": {"lat": 49.000000, "lon": 6.833333},
    "58": {"lat": 47.000000, "lon": 3.500000},
    "59": {"lat": 50.500000, "lon": 3.000000},
    "60": {"lat": 49.416668, "lon": 2.500000},
    "61": {"lat": 48.583332, "lon": 0.500000},
    "62": {"lat": 50.500000, "lon": 2.500000},
    "63": {"lat": 45.750000, "lon": 3.000000},
    "64": {"lat": 43.250000, "lon": -0.750000},
    "65": {"lat": 43.000000, "lon": 0.000000},
    "66": {"lat": 42.500000, "lon": 2.750000},
    "67": {"lat": 48.583332, "lon": 7.500000},
    "68": {"lat": 47.916668, "lon": 7.166667},
    "69": {"lat": 45.750000, "lon": 4.833333},
    "70": {"lat": 47.666668, "lon": 6.166667},
    "71": {"lat": 46.833332, "lon": 4.500000},
    "72": {"lat": 48.000000, "lon": 0.166667},
    "73": {"lat": 45.500000, "lon": 6.000000},
    "74": {"lat": 46.000000, "lon": 6.500000},
    "75": {"lat": 48.856614, "lon": 2.3522219},
    "76": {"lat": 49.500000, "lon": 1.000000},
    "77": {"lat": 48.833332, "lon": 2.666667},
    "78": {"lat": 48.750000, "lon": 1.916667},
    "79": {"lat": 46.333332, "lon": -0.666667},
    "80": {"lat": 49.900002, "lon": 2.333333},
    "81": {"lat": 43.933334, "lon": 2.166667},
    "82": {"lat": 44.000000, "lon": 1.500000},
    "83": {"lat": 43.416668, "lon": 6.000000},
    "84": {"lat": 44.166668, "lon": 5.166667},
    "85": {"lat": 46.666668, "lon": -1.166667},
    "86": {"lat": 46.583332, "lon": 0.333333},
    "87": {"lat": 45.833332, "lon": 1.250000},
    "88": {"lat": 48.166668, "lon": 6.500000},
    "89": {"lat": 47.800003, "lon": 3.566667},
    "90": {"lat": 47.633331, "lon": 6.866667},
    "91": {"lat": 48.583332, "lon": 2.333333},
    "92": {"lat": 48.900002, "lon": 2.233333},
    "93": {"lat": 48.916668, "lon": 2.416667},
    "94": {"lat": 48.800003, "lon": 2.483333},
    "95": {"lat": 49.000000, "lon": 2.166667},
    "971": {"lat": 16.250000, "lon": -61.583332},
    "972": {"lat": 14.666667, "lon": -61.000000},
    "973": {"lat": 4.000000, "lon": -53.000000},
    "974": {"lat": -21.166668, "lon": 55.500000},
    "976": {"lat": -12.833332, "lon": 45.166668},
}


#!/bin/python3
from preliminary.extract_bench import *
import re
import sys
import logging as log
from datetime import datetime
from threading import Timer

TOOLS_PATH = "../tools"

sys.path.insert(1, TOOLS_PATH + "/")
from utils.cmd import *

RECODE_PATH = TOOLS_PATH + "/recode.py"

def exec(cmdList, prog=RECODE_PATH, type=Pipe.ERR):
    cmdList.insert(0, prog)
    out = cmd(" ".join(cmdList), type=type, sh=True)
    if out and out != "":
        print(out)

def clearText(text):
    return re.sub(r"\x1b(\[.*?[@-~]|\].*?(\x07|\x1b\\))", "", text)

CMD_TEST = "FORCE_TIMES_TO_RUN=1 phoronix-test-suite batch-run "

def sendInput(arg):
    print("FORCE Sent Input")
    arg.write(bytes(str(1) + "\n", "utf-8"))
    arg.flush()

def readPTSOptions(bench):
    process = icmd([CMD_TEST + bench], sh=True)

    options = []

    while True:
        timer = Timer(2.0, sendInput, [process.stdin])
        timer.start()
        output = clearText(process.stdout.readline().decode("utf-8"))
        timer.cancel()

        if output == "" and process.poll() is not None:
            break
        if output:
            print(output)
            if "Test All Options" in output:
                maxValue = int(output.strip().split(":")[0])
                options.append(maxValue)
            elif "** Multiple items can be selected, delimit by a comma. **" in output:
                process.stdin.write(bytes(str(maxValue) + "\n", "utf-8"))
                process.stdin.flush()
            elif "System Information" in output:
                process.terminate()
                break

    if len(options) == 0:
        return [1]
    return options

def runPTSBenchmark(bench, options):
    process = icmd([CMD_TEST + bench], sh=True)

    curOption = 0

    while True:
        output = clearText(process.stdout.readline().decode("utf-8"))
        if output == "" and process.poll() is not None:
            break
        if output:
            print(output)
            if "** Multiple items can be selected, delimit by a comma. **" in output:
                process.stdin.write(bytes(str(options[curOption]) + "\n", "utf-8"))
                process.stdin.flush()
                curOption += 1

    return options

def runBenchmark(bench, maxRuns=3):
    options = readPTSOptions(bench)
    log.info("Options: " + str(options))

    print(options)

    curOptions = []

    for option in options:
        curOptions.append(1)

    run = 0
    detectedCounter = 0
    exist = False

    while run < maxRuns:
        log.info("Run with opt: " + str(curOptions))
        cmd(
            'echo "' + bench + "_" + str(curOptions) + '" > /proc/recode/statistics',
            sh=True,
        )
        log.info("** Registered to ReCode statistics: " + bench + "_" + str(curOptions))
        runPTSBenchmark(bench, curOptions)
        detected = int(cmd("cat /proc/recode/statistics", type=Pipe.OUT, sh=True))
        log.info("Run finished with DETECTED: " + str(detected))

        """ Read SysLog """
        file = open("/var/log/syslog", "r")
        lines = file.readlines()
        file.close()

        lIdx = 0

        for i in range(len(lines)):
            if "*** Running BENCH:" in lines[i]:
                lIdx = i

        watched = {}
        detected = []

        lines = lines[lIdx:]
        for line in lines:
            if "[++]" in line:
                line = line[line.index("[++]") :].replace("[++]", "").strip().split(" ")
                key = line[1]
                if key not in watched or watched[key][2] < line[2]:
                    line.pop(1)
                    watched[key] = line
            elif "[!!]" in line:
                detectedCounter += 1
                detected.append(line[line.index("[!!]") :].replace("[!!]", "").strip())

        log.info("* Watched")
        for key in watched:
            log.info(key + ": " + "\t".join(watched[key]))

        if len(detected) > 0:
            log.info("* Detected")
            for elem in detected:
                log.info(elem)

        run += 1

        for i in reversed(range(len(curOptions))):
            if curOptions[i] < options[i]:
                curOptions[i] += 1
                exist = True
                break

        if not exist:
            break

    return run, detectedCounter

BLACKLIST = [
    "pts/dcraw",
    "pts/stream",
    "pts/tscp",
    "pts/render-bench",
    "pts/m-queens",
    "pts/scimark2",
    "pts/fio",
    "pts/sqlite",
    "pts/jxrendermark",
    "pts/aio-stress",
    "pts/build-mplayer",
    "pts/crafty",
    "pts/t-test1",
    "pts/core-latency",
    "pts/n-queens",
    "pts/ipc-benchmark",
    "pts/cpuminer-opt",
    "pts/ctx-clock",
    "pts/fhourstones",
    "pts/webp",
    # Other benchmarks
    "pts/apache",
    "pts/sqlite-speedtest",
    "pts/osbench",
    "pts/hackbench"
    "pts/compilebench"
    "pts/sockperf"
]

if __name__ == "__main__":

    """Load ReCode and tune parameters"""

    exec(["module", "-u"])

    exec(["module", "-cc", "SECURITY"])

    exec(["module", "-l"])

    exec(["config", "-f", "20"])

    """ Tuning FR """
    exec("config -s tuning -tt FR".split())

    prog = TOOLS_PATH + "/attacks/flush_flush/fr/spy"
    args = '"/usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0.2400.33 0x279b80"'

    execStr = ("profiler -c 1 -t 3 -x " + prog).split()
    execStr.append("-a")
    execStr.append(args)
    exec(execStr)

    exec("config -s tuning -tt XL".split())

    prog = TOOLS_PATH + "/attacks/xlate/obj/aes-xp"
    args = TOOLS_PATH + "/attacks/xlate/openssl-1.0.1e/libcrypto.so.1.0.0"

    execStr = ("profiler -c 1 -t 3 -x " + prog).split()
    execStr.append("-a")
    execStr.append(args)
    exec(execStr)

    print("Thresholds:")
    exec(["/proc/recode/thresholds"], "cat", Pipe.OUT)

    """ ******************* """
    """ Start Testing Phase """
    """ ******************* """

    """ Install Benchmarks """
    installedBenchmarks = getPTSListInstalledTests()

    """ Start testing """
    now = datetime.now()
    dt_string = now.strftime("%d_%m_%Y_%H_%M_%S")

    log.basicConfig(
        filename=dt_string + ".log",
        format="%(asctime)s %(message)s",
        encoding="utf-8",
        level=log.DEBUG,
    )
    log.info("Starting ReCode and Tuning")

    """ Enable ReCode """
    exec("config -s system".split())

    detectedCounter = 0
    benchmarkCounter = 0

    RESUME = ""
    log.info("Start with benchmarks")

    offSet = 0
    if (RESUME != ""):
        try:
            offSet = installedBenchmarks.index(RESUME)
            print("Start from index: " + str(offSet))
        except ValueError:
            print("Cannot find RESUME.. SKIP")

    # for bench in installedBenchmarks[offSet:]:
    for bench in ["pts/amg", "pts/botan", "pts/chia-vdf", "pts/compress-pbzip2", "pts/glibc-bench", "pts/john-the-ripper", "pts/liquid-dsp", "pts/neatbench", "pts/qmcpack", "pts/scikit-learn", "pts/smhasher", "pts/srsran", "pts/gegl", "pts/gimp"]:
        if bench in BLACKLIST:
            print(bench + " is in NOT_ALLOWED_LIST... SKIP")
            continue
        log.info("Processing: " + bench)
        run, det = runBenchmark(bench, 3)
        benchmarkCounter += run
        detectedCounter += det

    log.info("Test completed: " + str(benchmarkCounter))
    log.info("Detected: " + str(detectedCounter))

    print("Test completed: " + str(benchmarkCounter))


#sudo apt-get install python3-rpi.gpio
#sudo pip3 install gpiozero

import RPi.GPIO as GPIO
import time
import gpiozero

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

#MOTOR A
#ENA=25
IN1=16
IN2=19

#GPIO.setup(ENA,GPIO.OUT)
#ena_pwm=GPIO.PWM(ENA,100)
GPIO.setup(IN1,GPIO.OUT)
GPIO.setup(IN2,GPIO.OUT)

#MOTOR B
#ENB=23
IN3=20
IN4=26

#GPIO.setup(ENB,GPIO.OUT)
#enb_pwm=GPIO.PWM(ENB,100)
GPIO.setup(IN3,GPIO.OUT)
GPIO.setup(IN4,GPIO.OUT)

GPIO.output(IN1,GPIO.LOW)
GPIO.output(IN2,GPIO.LOW)
GPIO.output(IN3,GPIO.LOW)
GPIO.output(IN4,GPIO.LOW)

#servo motor
GPIO.setup(5, GPIO.OUT)
pwm=GPIO.PWM(5, 50)
pwm.start(0)

#ULTRASONIC SENSOR
ECHO=7
TRIG=22
GPIO.setup(ECHO,GPIO.IN)
GPIO.setup(TRIG,GPIO.OUT)

#CAR MOVEMENTS
def forward():
    print("Moving Forward")
    GPIO.output(IN1,GPIO.HIGH)
    GPIO.output(IN2,GPIO.HIGH)
    GPIO.output(IN3,GPIO.HIGH)
    GPIO.output(IN4,GPIO.HIGH)
    time.sleep(10)

def left():
    print("Turning Left")
    GPIO.output(IN1,GPIO.LOW)
    GPIO.output(IN2,GPIO.LOW)
    GPIO.output(IN3,GPIO.HIGH)
    GPIO.output(IN4,GPIO.HIGH)
    time.sleep(10)

def right():
    print("Turning Right")
    GPIO.output(IN1,GPIO.HIGH)
    GPIO.output(IN2,GPIO.HIGH)
    GPIO.output(IN3,GPIO.LOW)
    GPIO.output(IN4,GPIO.LOW)
    time.sleep(10)

def stop(tme):
    print("Stop")
    GPIO.output(IN1,GPIO.LOW)
    GPIO.output(IN2,GPIO.LOW)
    GPIO.output(IN3,GPIO.LOW)
    GPIO.output(IN4,GPIO.LOW)
    time.sleep(tme)

def leftturn():
    print("left turn of servo")
    pwm.ChangeDutyCycle(10) #  left deg position
    time.sleep(2)

def rightturn():
    print("Right turn of servo by 45")
    pwm.ChangeDutyCycle(2.5) # right +45 deg position
    time.sleep(2)

def center():
    print("Servo returns to 0")
    pwm.ChangeDutyCycle(7.5) #  0 deg position
    time.sleep(2)
#measuring distance from ultrasonic sensor
def measure():
 GPIO.output(TRIG,1)
 time.sleep(0.0001)
 GPIO.output(TRIG,0)
 print("Ultrasonic sensor started")

 print("waiting for sensor to settle")
 time.sleep(0.2)
 GPIO.output(TRIG,True)
 time.sleep(0.00001)
 GPIO.output(TRIG,False)
 while GPIO.input(ECHO)==0:
        #print("Echo start")
    pulse_start=time.time()
 while GPIO.input(ECHO)==1:
    #print("Echo end")
    pulse_end=time.time()
 pulse_duration=pulse_end-pulse_start
 d=pulse_duration*17150
 d=round(d,2)
 print("distance:",d,"cm")
 time.sleep(2)
 return d
#for continuous checking of distance
sc=0
sr=0
sl=0

try:
  while True:

    leftturn() #left
    time.sleep(0.5)
    sl=0
    d1=measure()
    print("d1= ",d1)

    if d1<=500.0:
      sl=1

    center() #middle
    time.sleep(0.5)
    sc=0
    d2=measure()
    print("d2= ",d2)

    if d2<=500.0:
      sc=1

    rightturn() #right
    time.sleep(0.5)
    sr=0
    d3=measure()
    print("d3= ",d3)

    if d3<=500.0:
      sr=1

    join=str(sl)+str(sc)+str(sr)
    print(join)
    time.sleep(5)
  #8 combinations are possible
    if join=="000" or join=="101": #Go forward
       forward()
       print("Car moves forward")
    elif join=="001" or join=="011":#turn left

         print("car moving left")
         stop(1)
         left()
         time.sleep(1)
         stop(1)
         forward()
    elif join=="100" or join=="110":#turn right
         print("Car moving right")
         stop(1)
         right()
         time.sleep(1)
         stop(1)
         forward()
    elif join=="111":
         print("Uturn and reverse")
         stop(1)

except KeyboardInterrupt:
    stop(1)
    GPIO.cleanup()
pwm.stop()
GPIO.cleanup()


import os
import re
import discord
import asyncio
from datetime import datetime
from dotenv import load_dotenv
import pymongo
import wtftz
from pymongo import MongoClient
import urllib
from timeZone import UTC_offset
from convertTime import convertTime
import random

load_dotenv()
MONGO = os.getenv('MONGO_URI')
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')

cluster = MongoClient("mongodb+srv://shivanshu:" + urllib.parse.quote(MONGO) + "@cluster0.qzsh9.mongodb.net/test")
db = cluster["time-turner"]
collection = db["time-turner"]

intents = discord.Intents.all()
client = discord.Client(intents=intents)

# On succesfull connecting to a guild
@client.event
async def on_ready():
    for guild in client.guilds:
        if guild.name == GUILD:
            break

    print(
        f'{client.user} is connected to the following guild:\n'
        f'{guild.name}(id: {guild.id})'
    )

# When the bot newly joins a guild
@client.event
async def on_guild_join(guild):
    for member in guild.members:# for all the members in the server when the bot joins
        print(member.name)
        try:
            await member.create_dm()# create a dm
            embedVar = discord.Embed(title="Hi {0}!! Welcome to {1}".format(member.name,member.guild.name), description="As you are new, I don't know your timezone yet.", color=random.randint(0, 0xFFFFFF))
            embedVar.add_field(name="Type in the timezone you belong to from the list", value="ANAT AEDT AEST JST WIB BST\n UZT GST MSK EET CET GMT\n CVT AOE ART VET EST CST\n MST PST AKST MST NUT LINT\n NZDT ACDT ACST MMT IST AFT \n IRST NST MART CHADT ACWST NPT", inline=False)
            await member.dm_channel.send(embed=embedVar)
            def check(message):#checks if the user replied
                return message.author.id==member.id
            msg = await client.wait_for('message',check=check)
            reply=msg.content#obstain the content of user reply
            post= {"user_id": member.id, "guild_id":guild.id,"timezone": reply}
            collection.insert_one(post)#post it in database
        except:
            pass

# On addition of a new member to the server
@client.event
async def on_member_join(member):
    # member_id = member.id
    await member.create_dm()
    embedVar = discord.Embed(title="Hi {0}!! Welcome to {1}".format(member.name,member.guild.name), description="As you are new, I don't know your timezone yet.", color=random.randint(0, 0xFFFFFF))
    embedVar.add_field(name="Type in the timezone you belong to from the list", value="ANAT AEDT AEST JST WIB BST\n UZT GST MSK EET CET GMT\n CVT AOE ART VET EST CST\n MST PST AKST MST NUT LINT\n NZDT ACDT ACST MMT IST AFT \n IRST NST MART CHADT ACWST NPT", inline=False)
    await member.dm_channel.send(embed=embedVar)
    def check(message):
        return message.author.id==member.id
    msg = await client.wait_for('message',check=check)
    reply=msg.content
    post= {"user_id": member.id, "guild_id":member.guild.id,"timezone": reply}
    collection.insert_one(post)

# On receiving a message in the guild
@client.event
async def on_message(message):
    if message.author == client.user:
        return
    else:
        content = message.content.lower() #obtain the content of message sent in channel
        content=re.sub('[^A-Za-z0-9: ]+', '', content) #remove special characters
        content = re.findall(r"[^\W\d_]+|\d+(?::\d+)?", content) #separate the string at spaces and numbers

        if any(item in ["am","pm"] for item in content): #if am or pm is found in the content
            if "am" in content:
                index=content.index("am") #find the index of am
                time=content[index-1] #time is present is the previous index of am
            else:
                index=content.index("pm")
                time=content[index-1]
            print(time)
            emoji = '\N{Alarm Clock}'
            # or '\U0001f44d' or '👍'
            await message.add_reaction(emoji)
        timezones=["ANAT", "AEDT", "AEST", "JST" ,"WIB" ,"BST", "UZT", "GST", "MSK" ,"EET", "CET" ,"GMT", "CVT", "AOE", "ART", "VET", "EST", "CST", "MST", "PST", "AKST", "MST", "NUT", "LINT","NZDT", "ACDT", "ACST", "MMT", "IST", "AFT", "IRST", "NST", "MART", "CHADT", "ACWST", "NPT"]
        if message.content.upper() in timezones:
            await message.author.create_dm()
            embedVar = discord.Embed(title="Thanks, now I know your TimeZone", description=" ", color=random.randint(0, 0xFFFFFF))
            await message.author.dm_channel.send(embed=embedVar)

# When a member leaves the guild his data is deleted from the database
@client.event
async def on_member_remove(member):
    collection.find_one_and_delete({"user_id": member.id, "guild_id":member.guild.id})#delete the data from database from someone leaves the server

@client.event
async def on_reaction_add(reaction, user):
    content = reaction.message.content.lower() #obtain the content of message sent in channel
    content=re.sub('[^A-Za-z0-9: ]+', '', content) #remove special characters
    content = re.findall(r"[^\W\d_]+|\d+(?::\d+)?", content) #separate the string at spaces and numbers
    if any(item in ["am","pm"] for item in content): #if am or pm is found in the content
        if "am" in content:
            index=content.index("am") #find the index of am
            time=content[index-1] #time is present is the previous index of am
            period="am"
        else:
            index=content.index("pm")
            time=content[index-1]
            period="pm"
        try:
            user_reacted= collection.find_one({"user_id":user.id,"guild_id":reaction.message.guild.id})
            tz_reacted= user_reacted.get("timezone")
            user_sent= collection.find_one({"user_id":reaction.message.author.id,"guild_id":reaction.message.guild.id})
            tz_sent= user_sent.get("timezone")
            print("tz of user who reacted",tz_reacted)
            print("tz of user who sent the message",tz_sent)
            print("here workes the converter")
            time,period,day=convertTime(time,tz_sent,tz_reacted,period)
        except:
            pass

        try:
            embedVar = discord.Embed(title="Hey,The time in {0} is {1} {2} the {3}".format(tz_reacted,time,period,day), description=" ", color=random.randint(0, 0xFFFFFF))

            await user.send(embed=embedVar,delete_after=100)

        except:
            pass

client.run(TOKEN)


class Solution:
   def maxCoins(self,piles):
        piles.sort(reverse=True)
        total_coins = 0
        index = 0
        remaining = len(piles)

        while remaining > 2:
            total_coins += piles[index + 1]
            index += 2
            remaining -= 3

        return total_coins


###DICE GAME but, like, infinite and stuff.
###Mike Bell
###Creating an infinite number of dice games

#import random for random number gen
import random

class Gamespace(object): #creation of the gamespace
    dice1 = None #initialize class attributes
    dice2 = None
    i = None

    def __init__(self): #Gamespace creator
        self.dice1 = random.randint(1,6)
        self.dice2 = random.randint(1,6)
        self.i = 0 #keeping track of fails

    def rolldice (self): #takes the current gamespace dice attrivutes and randomizes
        self.dice1 = random.randint(1,6)
        self.dice2 = random.randint(1,6)

class Userinteraction(object): #I create Userinteraction classes because its a tad more organized in my 'runtime/main' method

    def intro(): #intro isnt taking an object because i just need this called procedurally
        print('Welcome to the DiCeGaMe.'); print('\n')
        print('Shit\'s easy dog, just fucking roll until they match'); print('\n')
        print('Just stop being bad.'); print('\n')
        print('How many games did you want to play?')
        while True:
            try: #try-except block to check for interger value. currently just ejects the program on fail
                userget = int(input('Don\'t be an idoit, enter an interger: '))
                if userget == 0:
                    print('Okay then, we won\'t play')
                else:
                    return userget #funky little way to get this procedure to execute and return an interger value. its called later.
            except ValueError:
                print('Yeah, you\'re a fucking idoit... Interger.')

    def gamemenu(self): #handles rules and printing to the user
        print('GAME SCREEN')
        print('Dice 1 = ' + str(self.dice1))
        print('Dice 2 = ' + str(self.dice2))
        if self.dice1 == self.dice2:
            print('Wow look at a randnumgen playing in your favor. You only failed ' + str(self.i) + ' times. Loser')
            return False #a False causes the script to move to the next object
        else:
            self.i = self.i + 1
            print('You\'ve failed ' + str(self.i) + ' times. Idiot')
            self.rolldice()
            return True #returns True causes this program to continue running against the object

class Infinite(object): # create the inifinite object
    pass
    userget = 0 #making these static attributes
    newgameList = []

    def __init__(self):
        self.userget = Userinteraction.intro() #calls intro sequence, intro() returns an int
        self.newgameList = [] #creates list
        for self.userget in range (0, int(self.userget)): #fills list form
            self.newgameList.append(Gamespace()) #fills list

class Runtime:
    pass
    newgame = True
    while newgame == True:
        newgame = Infinite() #starts the Infinite class object

        m = 0 #list interator
        for fart in newgame.newgameList:
            j = m + 1 #print interator, list position + 1. (list starts at 0, we like to start reading at 1)
            print('\n' + '****** NEXT GAME ******' + '\n')
            print('This is game number: ' + str(j))
            while Userinteraction.gamemenu(newgame.newgameList[m]) == True: #runs the method, if false, moves to next one
                print('\n')
            m += 1 #moves to next position on list

        print('\n' + '-------Here are your results--------' + '\n')
        n = 0 #list iterator
        for toot in newgame.newgameList:
            o = n + 1 #print interator, list position + 1. (list starts at 0, we like to start reading at 1)
            print('Game: ' + str(o) + ' - It took ' + str(newgame.newgameList[n].i) + ' tries to match') #prints the final fail (Gamespace.i) value of each gamespace object
            n += 1

    def vomit(): #this method just shits out w.e i need it too.
        print('Runtime.newgame.__dict__')
        print(Runtime.newgame.__dict__) #currently will tell me values of all attributes in my main gamespace

if __name__ == '__main__':
    Runtime()


#!/usr/bin/env python

import roslib
roslib.load_manifest('interactive_segmentation')

import rospy
import numpy as np
import sys

from jsk_recognition_msgs.msg import BoundingBoxArray, BoundingBox
from jsk_recognition_msgs.msg import Int32Stamped, BoolStamped
from sensor_msgs.msg import PointCloud2, Image
from geometry_msgs.msg import Pose, Vector3
from std_msgs.msg import Header

#signal topic to segmentation nodelet
signal_topic_ = '/interactive_segmentation_manager/critical/signal'
pub_signal_ = None

# incoming subscribing topics
iseg_node_ = '/interactive_segmentation/signal/target_object'
push_node_ = '/pr2_push_node/failure/signal'
merge_node_ = '/object_region_estimation/failure/signal'
bbox_node_ = '/bounding_box_handler/failure/signal' # if box is empty
grasp_node_ = '/pr2_grasp_object/failure/signal'

# flag for marked object
is_marked_object_ = False
START_SEGMENT_NODE = 1
NOT_RELEASE_OBJECT = 2  # tell PR2 not to release this object
FAILURE = -1

def nodelet_manager_signal(value, header):
    restart = Int32Stamped()
    restart.header = header
    restart.data = value
    return restart

def interactive_segmentation_callback(msg):
    global pub_signal_
    if msg.data == 1:
        rospy.loginfo("user marked object retrieved by pr2")
        hold_object = nodelet_manager_signal(NOT_RELEASE_OBJECT, msg.header)
        pub_signal_.publish(hold_object)

        global is_marked_object_
        is_marked_object_ = True
    elif msg.data == -1:
        rospy.logerr("segmentation node error... restaring")
        restart = nodelet_manager_signal(START_SEGMENT_NODE, msg.header)
        pub_signal_.publish(restart)

def pr2_pushing_callback(msg):
    global pub_signal_
    if msg.data == FAILURE:
        rospy.logerr("pr2 push nodelet raised error on current object")
        restart = nodelet_manager_signal(START_SEGMENT_NODE, msg.header)
        pub_signal_.publish(restart)

def object_region_estimation_callback(msg):
    global pub_signal_
    if msg.data == FAILURE:
        rospy.logerr("object merging nodelet raised error on current object")
        restart = nodelet_manager_signal(START_SEGMENT_NODE, msg.header)
        pub_signal_.publish(restart)

def grasp_object_callback(msg):
    global pub_signal_
    if is_marked_object_ and msg.data == 1:
        rospy.loginfo("THE TARGET OBJECT IS FOUND")

        # delete this
        next = nodelet_manager_signal(START_SEGMENT_NODE, msg.header)
        pub_signal_.publish(next)

    elif msg.data == START_SEGMENT_NODE:
        rospy.logdebug("going to segmentation node")
        next = nodelet_manager_signal(START_SEGMENT_NODE, msg.header)
        pub_signal_.publish(next)
    else:
        rospy.logfatal("SOMETHING HAS CRITICALLY GONE WRONG")

def subscribe():
    rospy.Subscriber(iseg_node_, Int32Stamped, interactive_segmentation_callback)
    rospy.Subscriber(push_node_, Int32Stamped, pr2_pushing_callback)
    rospy.Subscriber(merge_node_, Int32Stamped, object_region_estimation_callback)
    rospy.Subscriber(grasp_node_, Int32Stamped, grasp_object_callback)

def onInit():
    global pub_signal_
    pub_signal_ = rospy.Publisher(signal_topic_, Int32Stamped, queue_size = 10)
    subscribe()

    # rospy.loginfo("WAITING TO PUBLISH")
    # rospy.sleep(10.0)
    # rospy.loginfo("NODELET SETUP AND READY TO PUBLISH GO SIGNAL")
    # header = Header()
    # header.frame_id = '/base_link'
    # header.stamp = rospy.Time.now()
    # start = nodelet_manager_signal(START_SEGMENT_NODE, header)
    # pub_signal_.publish(start)

def main():
    rospy.init_node('interactive_segmentation_manager')
    onInit()
    rospy.spin()

if __name__ == "__main__":
    main()