from flask_restful import fields

transaction_input_fields = {
    'prev_hash': fields.String(attribute=lambda obj: obj.prev_hash.hex()),
    'output_n': fields.Integer(attribute='output_n_int'),
    'address': fields.String,
    'compressed': fields.Boolean,
    'double_spend': fields.Boolean,
    'encoding': fields.String,
    'index_n': fields.Integer,
    'locktime_cltv': fields.Integer,
    'locktime_csv': fields.Integer,
    'public_hash': fields.String(attribute=lambda obj: obj.public_hash.hex()),
    'redeemscript': fields.String(attribute=lambda obj: obj.redeemscript.hex()),
    'script': fields.String(attribute=lambda obj: obj.unlocking_script.hex()),
    'script_code': fields.String(attribute=lambda obj: obj.script_code.hex()),
    'script_type': fields.String,
    'sequence': fields.Integer,
    'signatures': fields.String(attribute=lambda obj: ''.join([s.hex() for s in obj.signatures])),
    'sigs_required': fields.Integer,
    'valid': fields.Boolean,
    'value': fields.Integer,
    'witness': fields.String(attribute=lambda obj: b''.join(obj.witnesses).hex()),
    'witness_type': fields.String,
}

transaction_output_fields = {
    'address': fields.String,
    'output_n': fields.Integer(attribute='output_n_int'),
    'public_hash': fields.String(attribute=lambda obj: obj.public_hash.hex()),
    'script': fields.String(attribute=lambda obj: obj.lock_script.hex()),
    'script_type': fields.String,
    'spent': fields.Boolean,
    'value': fields.Integer,
}

transaction_fields = {
    'txid': fields.String,
    'date': fields.DateTime(dt_format='iso8601'),
    'block_hash': fields.String,
    'block_height': fields.Integer,
    'coinbase': fields.Boolean,
    'confirmations': fields.Integer,
    'fee': fields.Integer,
    'inputs': fields.List(fields.Nested(transaction_input_fields)),
    'input_total': fields.Integer,
    'outputs': fields.List(fields.Nested(transaction_output_fields)),
    'output_total': fields.Integer,
    'locktime': fields.Integer,
    'network': fields.String(attribute='network.name'),
    'raw_hex': fields.String(attribute=lambda obj: obj.raw_hex()),
    'size': fields.Integer,
    'status': fields.String,
    'verified': fields.Boolean,
    'version': fields.Integer(attribute='version_int'),
    'vsize': fields.Integer,
    'witness_type': fields.String,
}

transaction_fields_block = {
    'txid': fields.String,
    'coinbase': fields.Boolean,
    'fee': fields.Integer,
    'inputs': fields.List(fields.Nested(transaction_input_fields)),
    'input_total': fields.Integer,
    'outputs': fields.List(fields.Nested(transaction_output_fields)),
    'output_total': fields.Integer,
    'locktime': fields.Integer,
    'raw_hex': fields.String(attribute=lambda obj: obj.raw_hex()),
    'size': fields.Integer,
    'status': fields.String,
    'verified': fields.Boolean,
    'version': fields.Integer(attribute='version_int'),
    'vsize': fields.Integer,
    'witness_type': fields.String,
}

utxo_fields = {
    'address': fields.String,
    'tx_hash': fields.String,
    'confirmations': fields.Integer,
    'output_n': fields.Integer,
    'input_n': fields.Integer,
    'block_height': fields.Integer,
    'fee': fields.Integer,
    'size': fields.Integer,
    'value': fields.Integer,
    'script': fields.String,
    'date': fields.DateTime(dt_format='iso8601'),
}

block_fields = {
    'bits': fields.Integer,
    'depth': fields.Integer(attribute='confirmations'),
    'block_hash': fields.String,
    'height': fields.Integer,
    'merkle_root': fields.String,
    'nonce': fields.Integer,
    'prev_block': fields.String,
    'time': fields.Integer(attribute='timestamp'),
    'tx_count': fields.Integer,
    'version': fields.Integer,
}

# network_prefix_field = fields.List({'a': fields.String, 'b': fields.String, 'c': fields.String, 'd': fields.String, 'e': fields.String, 'f': fields.String})

network_field = {
    'bip44_cointype': fields.Integer,
    'currency_code': fields.String,
    'currency_name': fields.String,
    'currency_name_plural': fields.String,
    'currency_symbol': fields.String,
    'denominator': fields.Float,
    'description': fields.String,
    'dust_amount': fields.Integer,
    'fee_default': fields.Integer,
    'fee_max': fields.Integer,
    'fee_min': fields.Integer,
    'name': fields.String,
    'prefix_address':  fields.String(attribute=lambda obj: obj['prefix_address'].hex()),
    'prefix_address_p2sh': fields.String(attribute=lambda obj: obj['prefix_address_p2sh'].hex()),
    'prefix_bech32': fields.String,
    'prefix_wif': fields.String(attribute=lambda obj: obj['prefix_wif'].hex()),
}


#Application file

from openpyxl import *
from tkinter import *

wb = load_workbook('application.xlsx')

sheet = wb.active

def excel():
    sheet.column_dimensions['A'].width = 30
    sheet.column_dimensions['B'].width = 10
    sheet.column_dimensions['C'].width = 10
    sheet.column_dimensions['D'].width = 20

    sheet.cell(row=1, column=1).value = "Name"
    sheet.cell(row=1, column=2).value = "Phone Number"
    sheet.cell(row=1, column=3).value = "Place Name"
    sheet.cell(row=1, column=4).value = "Body Temperature(F)"

def focus1(event):
    PhoneNumber_field.focus_set()

def focus2(event):
    placename_field.focus_set()

def focus3(event):
    bodytemperature_field.focus_set()

def clear():
    name_field.delete(0, END)
    PhoneNumber_field.delete(0, END)
    placename_field.delete(0, END)
    bodytemperature_field.delete(0, END)

def insert():
    if (name_field.get() == "" and
            PhoneNumber_field.get() == "" and
            placename_field.get() == "" and
            bodytemperature_field.get() == ""):

        print("empty input")

    else:
        current_row = sheet.max_row
        current_column = sheet.max_column

        sheet.cell(row=current_row + 1, column=1).value = name_field.get()
        sheet.cell(row=current_row + 1, column=2).value = PhoneNumber_field.get()
        sheet.cell(row=current_row + 1, column=3).value = placename_field.get()
        sheet.cell(row=current_row + 1, column=4).value = bodytemperature_field.get()

        # save the file
        wb.save('application.xlsx')

        name_field.focus_set()

        clear()

    # Driver code

if __name__ == "__main__":
    root = Tk()

    root.configure(background='light green')

    root.title("application form")

    root.geometry("600x400")

    excel()

    heading = Label(root, text="APPLICATION",font=("times new roman",20,"bold"),fg="red" ,bg="light green")

    name = Label(root,height=4, text="NAME", bg="light green")

    PhoneNumber = Label(root,height=4, text="PHONE NUMBER", bg="light green")

    placename = Label(root,height=4,text="PLACE NAME", bg="light green")

    bodytemperature = Label(root,height=4, text="BODY TEMPERATURE (F) ", bg="light green")

    heading.grid(row=0, column=1)
    name.grid(row=1, column=0)
    PhoneNumber.grid(row=2, column=0)
    placename.grid(row=3, column=0)
    bodytemperature.grid(row=4, column=0)

    name_field = Entry(root)
    PhoneNumber_field = Entry(root)
    placename_field = Entry(root)
    bodytemperature_field = Entry(root,width=1)

    name_field.bind("<Return>", focus1)

    PhoneNumber_field.bind("<Return>", focus2)

    bodytemperature_field.bind("<Return>", focus3)

    name_field.grid(row=1, column=1, ipadx="100")
    PhoneNumber_field.grid(row=2, column=1, ipadx="100")
    placename_field.grid(row=3, column=1, ipadx="100")
    bodytemperature_field.grid(row=4, column=1, ipadx="100")

    excel()

    submit = Button(root,width=6,height=3, text="Submit", fg="Black",
                    bg="Red", command=insert)
    submit.grid(row=5, column=1)

    # start the GUI

    root.mainloop()


from engine.entity import *
from GLOBAL import *

class Paddle:
    def __init__(self, y, borderlength, host):
        self.width = 20
        self.height = 80
        self.y = y
        self.borderlength = borderlength
        self.host = host
        self.window = host.window
        self.entity = Entity(self.window.width-self.width, self.y-self.height//2, self.width, self.height, self.host)
        self.entity.give_collision()
        self.entity.sprite.fill_color(USERDATA["PADDLE_COL"])


import frappe
from frappe import _, get_doc
from frappe import publish_progress
from frappe.core.doctype.file.file import create_new_folder
from frappe.utils.file_manager import save_file

def attach_pdf(doc, attach_to_doc, event=None):
    fallback_language = frappe.db.get_single_value("System Settings", "language") or "en"
    args = {
        "attach_to_doc": attach_to_doc.doctype,
        "attach_to_name": attach_to_doc.name,
        "doctype": doc.doctype,
        "name": doc.name,
        "title": doc.get_title(),
        "lang": getattr(doc, "language", fallback_language),
        "show_progress": 0
    }
    fileurl = execute(**args)

def enqueue(args):
    """Add method execute with given args to the queue."""
    frappe.enqueue(method=execute, queue='long',
                   timeout=30, is_async=True, **args)

def execute(attach_to_doc, attach_to_name, doctype, name, title, lang=None, show_progress=True):
    """
    Queue calls this method, when it's ready.
    1. Create necessary folders
    2. Get raw PDF data
    3. Save PDF file and attach it to the document
    """
    progress = frappe._dict(title=("Creating PDF ..."), percent=0, doctype=doctype, docname=name)

    if lang:
        frappe.local.lang = lang

    if show_progress:
        publish_progress(**progress)

    doctype_folder = create_folder(_(doctype), "Home")
    title_folder = create_folder(title, doctype_folder)

    if show_progress:
        progress.percent = 33
        publish_progress(**progress)

    pdf_data = get_pdf_data(doctype, name)

    if show_progress:
        progress.percent = 66
        publish_progress(**progress)

    fileurl = save_and_attach(pdf_data, attach_to_doc, attach_to_name, name, title_folder)

    if show_progress:
        progress.percent = 100
        publish_progress(**progress)

    return fileurl

def create_folder(folder, parent):
    """Make sure the folder exists and return it's name."""
    new_folder_name = "/".join([parent, folder])

    if not frappe.db.exists("File", new_folder_name):
        create_new_folder(folder, parent)

    return new_folder_name

def get_pdf_data(doctype, name):
    """Document -> HTML -> PDF."""
    html = frappe.get_print(doctype, name)
    return frappe.utils.pdf.get_pdf(html)

def save_and_attach(content, to_doctype, to_name, pdf_name, folder):
    """
    Save content to disk and create a File document.
    File document is linked to another document.
    """
    file_name = "{}.pdf".format(pdf_name.replace(" ", "-").replace("/", "-"))
    fileName = save_file(file_name, content, to_doctype,
              to_name, folder=folder, is_private=0)
    f=frappe.get_doc("File",fileName.name)
    return f.file_url


def findObjectByName(objectList, name):
    paramUp = name.upper()
    for it in objectList:
        if it['name'].upper() == paramUp:
            return it
    return None

def defaultRoom(room):
    if room.objects is None:
        room.objects = []
    if room.directions is None:
        room.directions = []
    if room.onUse is None:
        room.onUse = []
    if room.onEnter is None:
        room.onEnter = []

def defaultObject(obj):
    if not 'takeable' in obj:
        obj['takeable'] = False
    if not 'useable' in obj:
        obj['useable'] = False

def defaultDirection(direction):
    if not 'visible' in direction:
        direction['visible'] = True
    if not 'locked' in direction:
        direction['locked'] = False


"""
This file contains the main script responsible for fetching text data from
Project Gutenberg's top Fantasy Fiction novels.
"""
import argparse
import bs4
import requests
import re
from requests.models import HTTPError
import tqdm
from functools import partial
from multiprocessing.pool import ThreadPool

base_url = 'https://www.gutenberg.org'

headers = {
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Methods': 'GET',
    'Access-Control-Allow-Headers': 'Content-Type',
    'Access-Control-Max-Age': '3600',
    'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0'
}

def get_soup_content(url: str):
    """
    Util for getting soup content from url.
    """
    try:
        request = requests.get(url, headers)
        content = request.content.decode('utf-8')
        soup = bs4.BeautifulSoup(content, features="html.parser")
        return soup
    except (UnicodeDecodeError, HTTPError, ConnectionError, TimeoutError, ConnectionResetError):
        return None

def scrape_text(read_online_url: str):
    """
    Scrape the book using the "Read Online" option
    and write all of the text blocks that make up the novel
    to a file line-by-line.
    """
    soup = get_soup_content(read_online_url)
    if not soup:
        return

    blocks = map(lambda fragment: fragment.get_text(
        strip=True).lower(), soup.find_all('p', class_=''))
    for block in blocks:
        # Split lines by ., ?, !, and any of the previous followed by a quote.
        lines = re.split(r'(?:(?<=[.!?])|(?<=[.!?]["”]))\s+', block)
        for line in lines:
            line = re.sub(r'\[[0-9]*\]', '', string=line)
            with open('novels.txt', 'a+') as file:
                file.write(f"{line}\n")

def scrape_book_link(book_url: str):
    """
    Check validity of the given book page.
    Reject if the novel isn't in English or it's not readable online.
    """
    soup = get_soup_content(book_url)
    if not soup:
        return

    bibrec = soup.find('table', class_='bibrec')
    language = bibrec.find(
        'tr', {'property': 'dcterms:language'}).find('td').text

    if language != 'English':
        return

    read_online_url = soup.find('tr', class_='even').find(
        'a', class_='link', href=True)['href']

    if not read_online_url:
        return

    scrape_text(base_url + read_online_url)

def scrape_page(idx: int, book_titles: set):
    """
    Scrape whole page of books. Each page has 25 books.
    """
    idx_url = base_url + f"/ebooks/subject/138?start_index={idx}"
    soup = get_soup_content(idx_url)

    if not soup:
        return

    book_links = soup.find_all('li', class_='booklink')

    for book_link in tqdm.tqdm(book_links):
        title = book_link.find('span', class_='title').text
        # Avoid scraping copies of the same book.
        if title in book_titles:
            continue
        book_titles.add(title)
        book_url_ending = book_link.find('a', href=True)['href']
        scrape_book_link(base_url + book_url_ending)

def driver_scrape(pages, n_threads):
    """
    Main driver function for scraping pages in parallel.
    """
    page_indices = [i for i in range(1, (pages*25), 25)]
    book_titles = set()
    partial_scrape_page = partial(scrape_page, book_titles=book_titles)

    with ThreadPool(n_threads) as pool:
        pool.map(partial_scrape_page, page_indices)

def main(args):
    driver_scrape(args.pages, args.threads)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description='Scrape from Project Gutenberg')
    parser.add_argument('--threads', type=int,
                        help='Number of threads to scrape with, must be less than or equal to #pages to scrape')
    parser.add_argument('--pages', type=int,
                        help='Number of pages of the top fantasy novels to scrape (each page has 25 novels), max 325')
    args = parser.parse_args()
    main(args)


import grpc
import OrderManagement_pb2 as ordermgmt_pb2
import OrderManagement_pb2_grpc as ordermgmt_pb2_grpc
from collections import namedtuple

from google.protobuf import wrappers_pb2

placed_orders = [
    ordermgmt_pb2.Order(
        id="",
        items=["Bicycle", "PC"],
        description="Birth Gift",
        price=19212,
        destination="Shanghai, China"
    ),
    ordermgmt_pb2.Order(
        id="",
        items=["Flower", "Tree"],
        description="Gardening",
        price=200,
        destination="Seatle, USA"
    ),
    ordermgmt_pb2.Order(
        id="",
        items=["Iphone", "Earphone"],
        description="Replace",
        price=1000,
        destination="Boston, USA"
    ),
    ordermgmt_pb2.Order(
        id="",
        items=["Bed", "Table"],
        description="home",
        price=19212,
        destination="Beijing, China"
    ),
    ordermgmt_pb2.Order(
        id="",
        items=["Bicycle", "Car", "Motorcycle"],
        description="Transportation",
        price=100500,
        destination="Beijing, China"
    ),
]

class MetadataInterceptor(grpc.UnaryUnaryClientInterceptor):

    class _ClientCallDetails(
            namedtuple(
                '_ClientCallDetails',
                ('method', 'timeout', 'metadata', 'credentials')),
            grpc.ClientCallDetails):
        """Wrapper class for initializing a new ClientCallDetails instance.
        """
        pass

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

    def intercept_unary_unary(self, continuation, client_call_details, request):

        if client_call_details.metadata is None:
            metadata = []
        else:
            metadata = list(client_call_details.metadata)

        metadata.append(self.injected_meta)

        client_call_details = self._ClientCallDetails(
            client_call_details.method,
            client_call_details.timeout,
            metadata,
            client_call_details.credentials
        )
        return continuation(client_call_details, request)

def main():
    with grpc.insecure_channel("localhost:50051") as in_secure_channel:
        metadata_interceptor = MetadataInterceptor(('dst_soc', '192.168.0.100'))
        with grpc.intercept_channel(in_secure_channel, metadata_interceptor) as channel:
            stub = ordermgmt_pb2_grpc.OrderManagementStub(channel)
            try:
                stub.getOrder(wrappers_pb2.StringValue(value="NOTEXISTEDORDER"))
            except Exception as e:
                print(e)
            for order in placed_orders:
                response = stub.addOrder(order)
                order_id = response.value
                order.id = order_id
                print("New Order ID: %s" % order_id)
                print(order)
            print()
            # server streaming
            match_orders = list(stub.searchOrders(wrappers_pb2.StringValue(value="Bicycle")))
            print("Search result cout: %d" % len(match_orders))
            print("Search Result : \n", "\n".join(str(_) for _ in match_orders))

            # client streaming
            def updated_orders_gen():
                for order in placed_orders:
                    yield order
            stub.updateOrders(updated_orders_gen())
            print("-" * 30)

            # bidirectional streaming
            need_process_orders = [wrappers_pb2.StringValue(value=_.id) for _ in placed_orders]

            def need_process_orders_gen():
                for order_id in need_process_orders:
                    yield order_id

            print()
            for shipment in stub.processOrders(need_process_orders_gen()):
                print("Shipment: ", shipment)

if __name__ == "__main__":
    main()


import datetime
from typing import List
import numpy as np
np.random.seed(1741)
import torch
torch.manual_seed(1741)
import random
random.seed(1741)
import spacy
from transformers import RobertaTokenizer
from utils.constant import *

tokenizer = RobertaTokenizer.from_pretrained('/vinai/hieumdt/pretrained_models/tokenizers/roberta-base', unk_token='<unk>')
nlp = spacy.load("en_core_web_sm")

# Padding function
def padding(sent, pos = False, max_sent_len = 194):
    if pos == False:
        one_list = [1] * max_sent_len # pad token id
        mask = [0] * max_sent_len
        one_list[0:len(sent)] = sent
        mask[0:len(sent)] = [1] * len(sent)
        return one_list, mask
    else:
        one_list = [0] * max_sent_len # none id
        one_list[0:len(sent)] = sent
        return one_list

def format_time(elapsed):
    '''
    Takes a time in seconds and returns a string hh:mm:ss
    '''
    # Round to the nearest second.
    elapsed_rounded = int(round((elapsed)))
    # Format as hh:mm:ss
    return str(datetime.timedelta(seconds=elapsed_rounded))

def RoBERTa_list(content, token_list = None, token_span_SENT = None):
    encoded = tokenizer.encode(content)
    roberta_subword_to_ID = encoded
    # input_ids = torch.tensor(encoded).unsqueeze(0)  # Batch size 1
    # outputs = model(input_ids)
    # last_hidden_states = outputs[0]  # The last hidden-state is the first element of the output tuple
    roberta_subwords = []
    roberta_subwords_no_space = []
    for index, i in enumerate(encoded):
        r_token = tokenizer.decode([i])
        if r_token != " ":
            roberta_subwords.append(r_token)
            if r_token[0] == " ":
                roberta_subwords_no_space.append(r_token[1:])
            else:
                roberta_subwords_no_space.append(r_token)

    roberta_subword_span = tokenized_to_origin_span(content, roberta_subwords_no_space[1:-1]) # w/o <s> and </s>
    roberta_subword_map = []
    if token_span_SENT is not None:
        roberta_subword_map.append(-1) # "<s>"
        for subword in roberta_subword_span:
            roberta_subword_map.append(token_id_lookup(token_span_SENT, subword[0], subword[1]))
        roberta_subword_map.append(-1) # "</s>"
        return roberta_subword_to_ID, roberta_subwords, roberta_subword_span, roberta_subword_map
    else:
        return roberta_subword_to_ID, roberta_subwords, roberta_subword_span, -1

def tokenized_to_origin_span(text: str, token_list: List[str]):
    token_span = []
    pointer = 0
    for token in token_list:
        start = text.find(token, pointer)
        if start != -1:
            end = start + len(token) - 1
            pointer = end + 1
            token_span.append([start, end])
            assert text[start: end+1] == token, f"token: {token} - text:{text}"
        else:
            token_span.append([-100, -100])
    return token_span

def sent_id_lookup(my_dict, start_char, end_char = None):
    for sent_dict in my_dict['sentences']:
        if end_char is None:
            if start_char >= sent_dict['sent_start_char'] and start_char <= sent_dict['sent_end_char']:
                return sent_dict['sent_id']
        else:
            if start_char >= sent_dict['sent_start_char'] and end_char <= sent_dict['sent_end_char']:
                return sent_dict['sent_id']

def token_id_lookup(token_span_SENT, start_char, end_char):
    for index, token_span in enumerate(token_span_SENT):
        if start_char >= token_span[0] and end_char <= token_span[1]:
            return index

def span_SENT_to_DOC(token_span_SENT, sent_start):
    token_span_DOC = []
    #token_count = 0
    for token_span in token_span_SENT:
        start_char = token_span[0] + sent_start
        end_char = token_span[1] + sent_start
        #assert my_dict["doc_content"][start_char] == sent_dict["tokens"][token_count][0]
        token_span_DOC.append([start_char, end_char])
        #token_count += 1
    return token_span_DOC

def id_lookup(span_SENT, start_char):
    # this function is applicable to RoBERTa subword or token from ltf/spaCy
    # id: start from 0
    token_id = -1
    for token_span in span_SENT:
        token_id += 1
        if token_span[0] <= start_char and token_span[1] >= start_char:
            return token_id
    raise ValueError("Nothing is found. \n span sentence: {} \n start_char: {}".format(span_SENT, start_char))

def pos_to_id(sent_pos):
    id_pos_sent =  [pos_dict.get(pos) if pos_dict.get(pos) != None else 0
                    for pos in sent_pos]
    return id_pos_sent

def pad_to_max_ns(ctx_augm_emb):
    max_ns = 0
    ctx_augm_emb_paded = []
    for ctx in ctx_augm_emb:
        # print(ctx.size())
        max_ns  = max(max_ns, ctx.size(0))

    for ctx in ctx_augm_emb:
        pad = torch.zeros((max_ns, 768))
        if ctx.size(0) < max_ns:
            pad[:ctx.size(0), :] = ctx
            ctx_augm_emb_paded.append(pad)
        else:
            ctx_augm_emb_paded.append(ctx)
    return ctx_augm_emb_paded

def word_dropout(seq_id, position, is_word=True, dropout_rate=0.05):
    if is_word==True:
        drop_sent = [3 if np.random.rand() < dropout_rate and i not in position else seq_id[i] for i in range(len(seq_id))]
    if is_word==False:
        drop_sent = [20 if np.random.rand() < dropout_rate and i not in position else seq_id[i] for i in range(len(seq_id))]
    # print(drop_sent)
    return drop_sent

def create_target(x_sent, y_sent, x_sent_id, y_sent_id, x_position, y_position):
    if x_sent_id < y_sent_id:
        sent = x_sent + y_sent[1:] # <s> x_sent </s> y_sent </s>
        y_position_new = y_position + len(x_sent) - 1
        x_position_new = x_position
        assert y_sent[y_position] == sent[y_position_new]
        assert x_sent[x_position] == sent[x_position_new]
    elif x_sent_id == y_sent_id:
        assert x_sent == y_sent
        sent = x_sent
        x_position_new = x_position
        y_position_new = y_position
        assert y_sent[y_position] == sent[y_position_new]
        assert x_sent[x_position] == sent[x_position_new]
    else:
        sent = y_sent + x_sent[1:]
        y_position_new = y_position
        x_position_new = x_position + len(y_sent) - 1
        assert y_sent[y_position] == sent[y_position_new]
        assert x_sent[x_position] == sent[x_position_new]
    return sent, x_position_new, y_position_new

def make_predictor_input(x_sent, y_sent, x_sent_pos, y_sent_pos, x_sent_id, y_sent_id, x_possition, y_possition, ctx, pos_ctx, ctx_id, doc_id, dropout_rate=0.05, is_test=False):
    bs = len(x_sent)
    assert len(ctx) == bs and len(x_sent) == bs and len(x_possition) == bs, 'Each element must be same batch size'
    augm_target = []
    augm_target_mask = []
    augm_pos_target = []
    _augm_target = []
    _augm_pos_target = []
    x_augm_position = []
    y_augm_position = []
    max_len = 0
    for i in range(bs):
        if ctx_id == 'all':
            selected_ctx = list(range(len(ctx[i])))
        elif ctx_id == 'warming':
            selected_ctx = []
        else:
            selected_ctx = [step[i].cpu().item() for step in ctx_id]
        augment, x_possition_new, y_possition_new = augment_target(x_sent[i], y_sent[i], x_sent_id[i], y_sent_id[i], x_possition[i], y_possition[i],
                                                                ctx[i], selected_ctx, doc_id[i])
        pos_augment, x_pos_possition_new, y_pos_possition_new = augment_target(x_sent_pos[i], y_sent_pos[i], x_sent_id[i], y_sent_id[i],
                                                                            x_possition[i], y_possition[i], pos_ctx[i], selected_ctx, doc_id[i], is_pos=True)
        # assert x_possition_new == x_pos_possition_new
        # assert y_possition_new == y_pos_possition_new
        if is_test == False:
            augment = word_dropout(augment, [x_possition_new, y_possition_new], dropout_rate=dropout_rate)
            pos_augment = word_dropout(pos_augment, [x_possition_new, y_possition_new], is_word=False, dropout_rate=dropout_rate)
        # print(len(augment))
        max_len = max(len(augment), max_len)

        x_augm_position.append(x_possition_new)
        y_augm_position.append(y_possition_new)
        _augm_target.append(augment)
        _augm_pos_target.append(pos_augment)

    for i in range(bs):
        _augment = _augm_target[i]
        _pos_augment = _augm_pos_target[i]
        pad, mask = padding(_augment, max_sent_len=max_len)
        augm_target.append(pad)
        augm_target_mask.append(mask)
        augm_pos_target.append(padding(_pos_augment, pos=True, max_sent_len=max_len))

    augm_target = torch.tensor(augm_target)
    augm_target_mask = torch.tensor(augm_target_mask)
    augm_pos_target = torch.tensor(augm_pos_target)
    x_augm_position = torch.tensor(x_augm_position)
    y_augm_position = torch.tensor(y_augm_position)
    return augm_target, augm_target_mask, augm_pos_target, x_augm_position, y_augm_position

def augment_target(x_sent, y_sent, x_sent_id, y_sent_id, x_possition, y_possition, ctx, ctx_id, doc_id, is_pos=False):
    id_left = []
    id_cent = []
    id_right = []
    if x_sent_id < y_sent_id:
        for id in ctx_id:
            assert doc_id[id] != x_sent_id
            assert doc_id[id] != y_sent_id
            if doc_id[id] < x_sent_id:
                id_left.append(id)
            elif doc_id[id] > y_sent_id:
                id_right.append(id)
            else:
                id_cent.append(id)
    elif x_sent_id == y_sent_id:
        for id in ctx_id:
            assert doc_id[id] != x_sent_id
            assert doc_id[id] != y_sent_id
            assert x_sent == y_sent
            if doc_id[id] < x_sent_id:
                id_left.append(id)
            elif doc_id[id] > x_sent_id:
                id_right.append(id)
    else:
        for id in ctx_id:
            assert doc_id[id] != x_sent_id
            assert doc_id[id] != y_sent_id
            if doc_id[id] < y_sent_id:
                id_left.append(id)
            elif doc_id[id] > x_sent_id:
                id_right.append(id)
            else:
                id_cent.append(id)

    sent_left = []
    for id in sorted(id_left):
        sent_left += ctx[id][1:-1]
    sent_cent = []
    for id in sorted(id_cent):
        sent_cent += ctx[id][1:-1]
    sent_right = []
    for id in sorted(id_right):
        sent_right += ctx[id][1:-1]

    sent = []
    if x_sent_id < y_sent_id:
        if is_pos:
            sent = [0] + sent_left + [0] + x_sent[1:] + sent_cent + [0] + y_sent[1:] + sent_right + [0]
        else:
            sent = [0] + sent_left + [2] + x_sent[1:] + sent_cent + [2] + y_sent[1:] + sent_right + [2]
        x_possition_new = 1 + x_possition + len(sent_left)
        y_possition_new = 1 + len(sent_left) + len(x_sent) + len(sent_cent) + y_possition
    elif x_sent_id == y_sent_id:
        if is_pos:
            sent = [0] + sent_left + [0] + x_sent[1:] + sent_right + [0]
        else:
            sent = [0] + sent_left + [2] + x_sent[1:] + sent_right + [2]
        x_possition_new = 1 + x_possition + len(sent_left)
        y_possition_new = 1 + y_possition + len(sent_left)
    else:
        if is_pos:
            sent = [0] + sent_left + [0] + y_sent[1:] + sent_cent + [0] + x_sent[1:] + sent_right + [0]
        else:
            sent = [0] + sent_left + [2] + y_sent[1:] + sent_cent + [2] + x_sent[1:] + sent_right + [2]
        y_possition_new = 1 + y_possition + len(sent_left)
        x_possition_new = 1 + len(sent_left) + len(y_sent) + len(sent_cent) + x_possition

    assert sent[x_possition_new] == x_sent[x_possition]
    assert sent[y_possition_new] == y_sent[y_possition]

    return sent, x_possition_new, y_possition_new

def processing_vague(logits, threshold, vague_id):
    bs = logits.size(0)
    predicts = []
    for i in range(bs):
        logit = logits[i].detach()
        logit = torch.softmax(logit, dim=0)
        entropy = - torch.sum(logit * torch.log(logit)).cpu().item()
        if entropy > threshold:
            predict = vague_id
        else:
            predict = torch.max(logit.unsqueeze(0), 1).indices.cpu().item()
        predicts.append(predict)
    # print(predicts)
    return predicts

def count_parameters(model):
    return sum(p.numel() for p in model.parameters() if p.requires_grad)

def collate_fn(batch):
    return tuple(zip(*batch))


from .graph_embedding import GraphEmbedding
from .utils import spectral_radius
import scipy.sparse as sp
import scipy.sparse.linalg as sLa
import numpy as np

class HOPE(GraphEmbedding):

    def __init__(self, file, type='directed'):
        super().__init__(file, type=type)
        X, self.Y = self.data_split(self.G)
        self.A = self.build_adj(np.array(X))

    def data_split(self, graph, ratio=0.8):
        #traning set
        X = []
        #test set
        Y = []
        for edge in graph:
            if np.random.uniform() <= ratio:
                X.append(tuple(edge))
            else:
                Y.append(tuple(edge))

        return X, Y

    def embed(self, d=100, const_b=0.8):
        s_r = spectral_radius(self.A)
        b = const_b / s_r
        # identity matrix
        I = sp.eye(self.A.shape[0]).tocsc()
        # Kats index - S = inverse(Ma) * Mb
        Ma = I - b * self.A
        Mb = b * self.A

        # similarity matrix
        S = sLa.inv(Ma) @ (Mb)

        # decomposition
        l, s, r = sLa.svds(S, k=d)

        self.U = np.dot(l, np.diag(np.sqrt(s)))
        self.V = np.dot(r.T, np.diag(np.sqrt(s)))

    def get_edge_weight(self, i, j):
        return np.dot(self.U[i, :], self.U[j, :])

    def get_testing_data(self):
        return self.Y.copy()


from keras.layers import Dense
from keras.models import Sequential
from keras.optimizers import Adam
import numpy as np

state_size = 15
action_size = 5
batch_size = 100

x_train = np.random.rand(batch_size, state_size)
y_train = np.random.rand(batch_size, action_size)

model = Sequential()
model.add(Dense(30, input_dim=state_size, activation='relu'))
model.add(Dense(30, activation='relu'))
model.add(Dense(action_size, activation='linear'))
model.compile(loss='mse', optimizer=Adam(lr=0.001))
model.summary()

model.fit(x_train, y_train, batch_size=32, epochs=1)


#-*- coding: utf-8 -*-
from __future__ import print_function, division

"""
Pytorch useful tools.
"""

import torch
import os
import errno
import numpy as np

def save_checkpoint(net, best_perf, directory, file_name, data_weights):
    print('---------- SAVING MODEL ----------')
    if not os.path.isdir(directory):
        os.makedirs(directory)
    checkpoint_file = os.path.join(directory, file_name + '_' + data_weights + '.weights')
    torch.save(net.state_dict(), checkpoint_file)
    print('Model Saved as:', checkpoint_file)
    with open (directory+'results.txt','w') as fp:
        fp.write(str(round(100*best_perf, 3))+'\n')

def load_checkpoint(model_file):
    if os.path.isfile(model_file):
        print("=> loading model '{}'".format(model_file))
        checkpoint = torch.load(model_file)
        return checkpoint
    else:
        print("=> no model found at '{}'".format(model_file))
        raise OSError(errno.ENOENT, os.strerror(errno.ENOENT), model_file)

def get_num_classes(args):
    data_path = args.data_path
    if args.dataset == 'context':
        classes_file = data_path+'/Context/classes.txt'
    else:
        classes_file = data_path+'/Drink_Bottle/classes.txt'

    num_classes = sum(1 for line in open(classes_file))
    return num_classes

def get_weight_criterion(dataset):
    if dataset == 'context':
        weights = [1, 1, 2.2, 1, 1, 1, 1, 1, 4.5, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2.2, 1.5, 1, 1, 1, 1]
    elif dataset =='bottles':
        weights = [1, 1, 1, 1, 2, 1.5, 1.5, 1, 1.5, 1, 1, 1.5, 1, 1, 1, 1.5, 1, 1, 1, 1]
    return weights

def get_embedding_size(embedding):
    if embedding == 'w2vec' or embedding == 'fasttext' or embedding == 'glove':
        embedding_size = 300
    elif embedding =='bert':
        embedding_size = 666
    elif embedding == 'phoc':
        embedding_size = 604
    elif embedding == 'fisher':
        embedding_size = 38400

    return embedding_size


import airflow
from airflow import DAG
from airflow.operators.python import PythonOperator
import pandas as pd
import great_expectations as ge
from datetime import timedelta
from datetime import datetime
from great_expectations.exceptions import GreatExpectationsValidationError

# Define the DAG
dag = DAG(
    "check_csv_dag",
    schedule = timedelta(minutes=15),
    start_date=airflow.utils.dates.days_ago(2)
)

# Define the task
def check_csv_task():
    # Load the data
    data = pd.read_csv("test (2).csv")

    sample = data.sample(1)

    print(pd.DataFrame(sample))
    df_ge = ge.dataset.PandasDataset(sample)
    # Checking whether the sample meets the constraint
    try:
        result1 = df_ge.expect_column_values_to_not_be_null("Dependents")
        print("result1:", result1)
        result2 = df_ge.expect_column_values_to_be_unique('Loan_ID')
        print("result2:", result2)
        result3 = df_ge.expect_column_values_to_not_be_null("Gender")
        print("result3:", result3)
        result4 = df_ge.expect_column_values_to_not_be_null("Credit_History")
        print("result4:", result4)
        result5 = df_ge.expect_column_values_to_be_of_type(column="Credit_History", type_="int")
        print("result5:", result5)

        if result1['success'] and result2['success'] and result3['success'] and result4['success'] or result5['success']:
           sample.to_csv(f'folderB/{datetime.now().strftime("%Y-%M-%d_%H-%M-%S")}.csv',
                                index=False)

           print("folderB notification sent")

        else:
            print("data quality failed")
            sample.to_csv(
                f'folderC/{datetime.now().strftime("%Y-%M-%d_%H-%M-%S")}.csv',
                index=False)

    except ge.exceptions.GreatExpectationsValidationError as e:

        print("entered into except")

check_csv_operator = PythonOperator(
    task_id="check_csv_task",
    python_callable=check_csv_task,
    dag=dag
)


from langchain.vectorstores import FAISS
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.document_loaders import PyPDFLoader, DirectoryLoader
from langchain.embeddings import HuggingFaceEmbeddings

# Load PDF file from data path. Convert docx to pdf if needed for word docs
loader = DirectoryLoader('add/you/doc/path/here',
                         glob="*.pdf",
                         loader_cls=PyPDFLoader)
documents = loader.load()

# chunk the pdf text - adjust chunk size and overlap as needed
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500,
                                               chunk_overlap=50)
texts = text_splitter.split_documents(documents)

# get embeddings model from huggingface
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2',
                                   model_kwargs={'device': 'cpu'})

# Build vector store
vectorstore = FAISS.from_documents(texts, embeddings)
vectorstore.save_local('vectorstore/db_faiss')


#!/usr/bin/env python
import rospy
import parameters as param
from hybrid_a_star_user_input import *
from tf.transformations import euler_from_quaternion, quaternion_from_euler
from farming_bot_msgs.msg import (
    trajectory_interface_cartesianAction,
    trajectory_interface_cartesianActionGoal
)
from std_msgs.msg import String
from geometry_msgs.msg import (
    Pose,
    PoseStamped,
    PoseArray
)
from nav_msgs.msg import (
    Path,
    Odometry,
    OccupancyGrid
)

start, goal = None, None
map_frame = "map"
def transform_vehicle(vehicle_point_object, next_state, angle_of_rotation):
  lst=[]
  # print('i came till here')
  for pt in vehicle_point_object.input_coordinates:
    new_x=(pt[0]-vehicle_point_object.center[0])*np.cos(angle_of_rotation) + (pt[1]-vehicle_point_object.center[1])*np.sin(angle_of_rotation)+next_state[0]
    new_y=-(pt[0]-vehicle_point_object.center[0])*np.sin(angle_of_rotation)+(pt[1]-vehicle_point_object.center[1])*np.cos(angle_of_rotation)+next_state[1]
    print(new_x, new_y)
    lst.append([new_x,new_y])
  vehicle_point_ret=param.vehicle_points(np.array(lst),next_state)
  # print(lst)
  return vehicle_point_ret

def row_change(msg):
  # print('Automated Row')
  get_value.row_side_data = msg.data
  # print(data)

class values:
  def __init__(self):
    self.start = None
    self.goal = None
    self.map_frame = "map"
    self.path_segment_publish = []
    self.xlist = []
    self.ylist = []
    self.yawlist = []
    self.path = []
    self.goal_reached = None
    self.directions_list = []
    self.XY_GRID_RESOLUTION = param.XY_GRID_RESOLUTION
    self.YAW_GRID_RESOLUTION = param.YAW_GRID_RESOLUTION
    self.number_of_sequences = 0
    self.MOTION_RESOLUTION = param.MOTION_RESOLUTION
    self.goal_sequences = True
    self.N_STEER = param.N_STEER
    self.pub_path = []
    self.H_COST = param.XY_GRID_RESOLUTION
    self.VR = param.VR
    self.SB_COST = param.SB_COST
    self.BACK_COST = param.BACK_COST
    self.STEER_CHANGE_COST = param.STEER_CHANGE_COST
    self.STEER_COST = param.STEER_COST
    self.H_COST = param.H_COST
    self.trajectory_pub = PoseArray()
    self.trajectory_pub.header.frame_id = 'map'
    self.total_goals_to_reached = None
    self.sequence_exist = False
    self.current_goal = None
    self.compute_time = None
    self.row_points = None
    self.row_number = None
    self.row_side_data = None
    self.threshold_distance_to_goal = param.threshold_distance_to_goal

def invert_angle(angle):
  return (angle + pi) % (2 * pi)

def set_start(msg):
  # print(msg)
  global start
  ox, oy = [], []
  orientation_q = msg.pose.pose.orientation
  orientation_list = [orientation_q.x,
                      orientation_q.y, orientation_q.z, orientation_q.w]
  (roll, pitch, yaw) = euler_from_quaternion(orientation_list)
  # roll, pitch, yaw =
  get_value.start = [msg.pose.pose.position.x, msg.pose.pose.position.y, yaw]
  #   print(np.rad2deg(start[2]))
  # pass
  if get_value.row_side_data!=None:
    vehicle_points = param.vehicle_pt_obj_actual
    get_value.row_points = transform_vehicle(vehicle_points, get_value.start,get_value.start[2])
    row_goal =  [0,0,0]
    print('get_value.start',get_value.start)
    print(get_value.row_side_data)
    if get_value.row_side_data  == 'Right':
      row_goal[0] =  get_value.row_points.input_coordinates[0][0]
      row_goal[1] =  get_value.row_points.input_coordinates[0][1]
      row_goal[2] =  invert_angle(get_value.start[2])
      print("row_goal",row_goal)
      get_value.row_side_data  = None
      get_value.path = hybrid_a_star_planning(get_value.start, row_goal, ox, oy, XY_GRID_RESOLUTION, YAW_GRID_RESOLUTION)
      x = list(get_value.path.xlist)
      y = list(get_value.path.ylist)
      yaw = list(get_value.path.yawlist)

      # print("--- %s seconds ---" % (time.time() - start_time))
      trajectory_pub = PoseArray()
      trajectory_pub.header.frame_id = 'map'
      # path_pub=Path()
      # path_pub.header.frame_id='map'
      # publish_trajectory(path)
      for i in range(len(x)):

        new_pose = Pose()
        # new_pose.header.seq=i+1
        # new_pose.header.frame_id='map'
        new_pose.position.x = x[i]
        new_pose.position.y = y[i]
        # print(new_pose.pose.position.x,new_pose.pose.position.y)
        new_pose.position.z = 0
        roll = pitch = 0
        yaw_n = yaw[i]
        quat = quaternion_from_euler(roll, pitch, yaw_n)
        new_pose.orientation.x = quat[0]
        new_pose.orientation.y = quat[1]
        new_pose.orientation.z = quat[2]
        new_pose.orientation.w = quat[3]
        # new_pose.pose.orientation=path_now[3]
        trajectory_pub.poses.append(new_pose)
        # path_pub.poses.append(new_pose)
      # print(x)
      # print(x,y,yaw)
      publish_trajectory.publish(trajectory_pub)

      # get_value.compute_time = ("--- %s seconds ---" % (time.time() - start_time))
      # print('After Computing:' , len(get_value.path.xlist))
      # x = list(path.xlist)sequence_exist
      # get_value.path_segment_publish = path_segment(get_value.path)
      # get_value.number_of_sequences  = len(get_value.path_segment_publish)
      # # if get_value.number_of_sequences == 1:
      # #   get_value.goal_sequences = False
      # if get_value.number_of_sequences>1:
      #   get_value.sequence_exist = True
      # get_value.pub_path = get_value.path_segment_publish.pop(0)
      # get_value.number_of_sequences-=1
      # get_value.total_goals_to_reached = len(get_value.pub_path)
      # publish_pose_array(get_value.pub_path)

      # print('Awesome')

      # pass

    elif get_value.row_side_data  == 'Left':
      row_goal[0] =  get_value.row_points.input_coordinates[1][0]
      row_goal[1] =  get_value.row_points.input_coordinates[1][1]
      row_goal[2] =  invert_angle(get_value.start[2])
      print("row_goal",row_goal)

      get_value.path = hybrid_a_star_planning(get_value.start, row_goal, ox, oy, XY_GRID_RESOLUTION, YAW_GRID_RESOLUTION)
      x = list(get_value.path.xlist)
      y = list(get_value.path.ylist)
      yaw = list(get_value.path.yawlist)

      # print("--- %s seconds ---" % (time.time() - start_time))
      trajectory_pub = PoseArray()
      trajectory_pub.header.frame_id = 'map'
      # path_pub=Path()
      # path_pub.header.frame_id='map'
      # publish_trajectory(path)
      for i in range(len(x)):

        new_pose = Pose()
        # new_pose.header.seq=i+1
        # new_pose.header.frame_id='map'
        new_pose.position.x = x[i]
        new_pose.position.y = y[i]
        # print(new_pose.pose.position.x,new_pose.pose.position.y)
        new_pose.position.z = 0
        roll = pitch = 0
        yaw_n = yaw[i]
        quat = quaternion_from_euler(roll, pitch, yaw_n)
        new_pose.orientation.x = quat[0]
        new_pose.orientation.y = quat[1]
        new_pose.orientation.z = quat[2]
        new_pose.orientation.w = quat[3]
        # new_pose.pose.orientation=path_now[3]
        trajectory_pub.poses.append(new_pose)
        # path_pub.poses.append(new_pose)
      # print(x)
      # print(x,y,yaw)
      publish_trajectory.publish(trajectory_pub)

      # get_value.compute_time = ("--- %s seconds ---" % (time.time() - start_time))
      # print('After Computing:' , len(get_value.path.xlist))
      # x = list(path.xlist)sequence_exist
      # get_value.path_segment_publish = path_segment(get_value.path)
      # get_value.number_of_sequences  = len(get_value.path_segment_publish)
      # # if get_value.number_of_sequences == 1:
      # #   get_value.goal_sequences = False
      # if get_value.number_of_sequences>1:
      #   get_value.sequence_exist = True
      # get_value.pub_path = get_value.path_segment_publish.pop(0)
      # get_value.number_of_sequences-=1
      # get_value.total_goals_to_reached = len(get_value.pub_path)
      # get_value.row_side_data  = None
      # publish_pose_array(get_value.pub_path)

def set_map(msg):
  # print(msg)
  pass

def set_goal(msg):
  # print(msg)
  global start, goal
  orientation_q = msg.pose.orientation
  orientation_list = [orientation_q.x,
                      orientation_q.y, orientation_q.z, orientation_q.w]
  (_roll, _pitch, _yaw) = euler_from_quaternion(orientation_list)
  goal = [msg.pose.position.x, msg.pose.position.y, _yaw]
  print(goal)
  ox, oy = [], []
  start_time = time.time()

  path = hybrid_a_star_planning(
      start, goal, ox, oy, XY_GRID_RESOLUTION, YAW_GRID_RESOLUTION)

  x = list(path.xlist)
  y = list(path.ylist)
  yaw = list(path.yawlist)

  print("--- %s seconds ---" % (time.time() - start_time))
  trajectory_pub = PoseArray()
  trajectory_pub.header.frame_id = 'map'
  # path_pub=Path()
  # path_pub.header.frame_id='map'
  # publish_trajectory(path)
  for i in range(len(x)):

    new_pose = Pose()
    # new_pose.header.seq=i+1
    # new_pose.header.frame_id='map'
    new_pose.position.x = x[i]
    new_pose.position.y = y[i]
    # print(new_pose.pose.position.x,new_pose.pose.position.y)
    new_pose.position.z = 0
    roll = pitch = 0
    yaw_n = yaw[i]
    quat = quaternion_from_euler(roll, pitch, yaw_n)
    new_pose.orientation.x = quat[0]
    new_pose.orientation.y = quat[1]
    new_pose.orientation.z = quat[2]
    new_pose.orientation.w = quat[3]
    # new_pose.pose.orientation=path_now[3]
    trajectory_pub.poses.append(new_pose)
    # path_pub.poses.append(new_pose)
  # print(x)
  # print(x,y,yaw)
  publish_trajectory.publish(trajectory_pub)

  # publish_path.publish(path_pub)
  end = list(zip(x, y, yaw))
  print(len(end))

def feedback_cb(msg):
  print(msg)
  pass

def trajectory_Callback(msg):
  pass

  return action_goal

get_value = values()

if __name__ == "__main__":

  rospy.init_node("Global_planner_cartesian_interface")

  # publish_path = rospy.Publisher('/gplanner/path', Path, queue_size=1)
  publish_trajectory = rospy.Publisher(
      '/navigation/cartesian_trajectory_goal', PoseArray, queue_size=1)
  start = rospy.Subscriber("odometry/filtered_map", Odometry, set_start)
  map = rospy.Subscriber(
      "/move_base/global_costmap/costmap", OccupancyGrid, set_map)
  goal = rospy.Subscriber("/gplanner/goal", PoseStamped, set_goal)
  row = rospy.Subscriber("/side", String, row_change)

  # Print result
  rospy.spin()


__all__ = [
    "parse_time",
    "parse_duration",
]

import re
from typing import Type, TypeVar

from phanas_pydantic_helpers.common.imports import DummyModule, raise_if_missing_deps

try:
    import pendulum as pen
except ImportError:
    pen = DummyModule("pendulum")

try:
    import pytimeparse
except ImportError:
    pytimeparse = DummyModule("pytimeparse")

time_pattern = re.compile(
    r"^"
    r"(?P<hour>[0-2]?\d)"
    r"(?::?(?P<minute>\d{2}))?"
    r"\s*"
    r"(?:(?P<period_am>a|am)|(?P<period_pm>p|pm))?"
    r"$",
    re.I,
)

V = TypeVar("V")

def parse_time(time_str: str, class_: Type[V] = pen.Time) -> V:
    """
    Parse a string as a time specifier of the general format "12:34 PM".

    :return: the parsed time
    """
    raise_if_missing_deps(pen)

    match = time_pattern.match(time_str)
    if not match:
        raise ValueError("No match")

    hour = int(match["hour"])
    minute = int(match["minute"] or 0)

    if (0 > hour > 23) or (0 > minute > 59):
        raise ValueError("Hour or minute is out of range")

    if match["period_pm"]:
        if hour < 12:
            # This is PM and we use 24 hour times in datetime, so add 12 hours
            hour += 12
        elif hour == 12:
            # 12 PM is 12:00
            pass
        else:
            raise ValueError("24 hour times do not use AM or PM")
    elif match["period_am"]:
        if hour < 12:
            # AM, so no change
            pass
        elif hour == 12:
            # 12 AM is 00:00
            hour = 0
        else:
            raise ValueError("24 hour times do not use AM or PM")

    return class_(hour, minute)

def parse_duration(duration_str: str, class_: Type[V] = pen.Duration) -> V:
    raise_if_missing_deps(pen, pytimeparse)

    seconds = pytimeparse.parse(duration_str)
    if seconds is None:
        raise ValueError("Invalid time duration")
    return class_(seconds=seconds)


import pandas as pd
#from fill_variables import *
import docx
import re

#Get number of questions ----------------------------------------------------------------------------------------
def get_nb_questions():
    not_Check = True
    while not_Check:
        nb_Questions = input("Entrez le nombre de questions : ")
        try:
            nb_Questions = int(nb_Questions)
            not_Check = False
        except ValueError:
            print("Veuillez entrer un nombre entier !")
    return nb_Questions
# --------------------------------------------------------------------------------------------------------------

# Get patterns  ------------------------------------------------------------------------------------------
def get_patterns(subject):
    pattern_Debut = []
    pattern_End = []
    pattern_point = [r"points\)",r"point\)",r"pts\)",r"pt\)"]
    pattern_numero = [[f"{num}\)",f"{num}/",f"Q{num}",f"QUESTION {num}",f"QUESTION{num}"] for num in range(1,10)]

    if subject.endswith(".docx"): #only for docx, pdf file scheduling
        file = docx.Document(subject)
        numero_is_inside = False
        for paragraph in file.paragraphs:
            for i in range(9):
                for j in range(len(pattern_numero[i])):
                    if re.match(pattern_numero[i][j], paragraph.text):
                        numero_is_inside = True
                        break
        for paragraph in file.paragraphs:
            if numero_is_inside:
                for i in range(9):
                        for j in range(len(pattern_numero[i])):
                            if re.match(pattern_numero[i][j], paragraph.text):
                                if j == 0 or j == 1:
                                    pattern_Debut.append(re.sub(r'^\d+\)|^\d+/', '', paragraph.text))
                                else:
                                    pattern_Debut.append(pattern_numero[i][j])
                                    #pattern_Debut.append(paragraph.text)
            else :
                for i in range(len(pattern_point)):
                    if re.search(pattern_point[i], paragraph.text):
                        pattern_Debut.append(paragraph.text)

    #pattern_Debut_clean = [re.sub(r'^\d+\)|^\d+/|^Q\d+\s*\s*', '', list) for list in pattern_Debut]
        #Remove 1) or
    #pattern_Debut.append(re.sub(r'^Q\d+\s*\s*', '', paragraph.text))

    for i in range(1,len(pattern_Debut)):
        pattern_End.append(pattern_Debut[i])
    pattern_End.append("///")

    #print("hello")

    return pattern_Debut, pattern_End
# --------------------------------------------------------------------------------------------------------------


# Day 4 info here https://adventofcode.com/2022/day/4
# In how many assignment pairs do the ranges overlap?

import re

with open('day4/input.txt') as file:
	all_lines = file.readlines()
	total_overlaps = 0

	for line in all_lines:
		range_array = [int(num) for num in re.split(r'[,-]', line)]

		num_array1 = set(range(range_array[0], range_array[1] + 1))
		num_array2 = set(range(range_array[2], range_array[3] + 1))

		if num_array1.intersection(num_array2):
			total_overlaps += 1

	print('Total overlaps are', total_overlaps)


MAX_CONSTANTS = 10
MAX_EXPAND = 30
QUANTIFIERS = ["A", "E"]
PROPOSITIONS = ["p", "q", "r", "s"]
PREDICATES = ["P", "Q", "R", "S"]
CONNECTIVES = ["^", "v", ">"]
VAR = ["x", "y", "z", "w"]
DEBUG_PARSER = False
DEBUG_SAT = True
CONSTANTS = [chr(97 + i) for i in range(MAX_CONSTANTS)]

# Parse a formula, consult parseOutputs for return values.
def parse(fmla):
    result = 0

    if len(fmla) != 0:
        if fmla[0] == "-":
            # 7 / 2 / 0 - Negatived
            first_non_neg = 1
            # clean double negations
            while first_non_neg < len(fmla) and fmla[first_non_neg] == "-":
                first_non_neg += 1
            next_parsed = parse(fmla[first_non_neg:])
            if next_parsed in [6, 8]:
                result = 7  # Neg Proposition
            elif next_parsed in [3, 4, 5, 1]:
                result = 2  # Neg first order
        elif fmla[0] in QUANTIFIERS:
            # 3 / 4 / 0 - Quantified
            if fmla[1] in VAR:
                next_parsed = parse(fmla[2:])
                if next_parsed > 0:
                    # Ax / Ex
                    result = 3 if fmla[0] == QUANTIFIERS[0] else 4

        elif fmla[0] == "(":
            # 5 / 8 / 0 - binary connectives
            if fmla[-1] == ")":
                l_parsed = parse(lhs(fmla))
                r_parsed = parse(rhs(fmla))
                if DEBUG_PARSER:
                    print('>>', lhs(fmla), con(fmla), rhs(fmla))
                if con(fmla) in CONNECTIVES and l_parsed > 0 and r_parsed > 0:
                    if l_parsed < 6 or r_parsed < 6:
                        result = 5  # binary first order
                    else:
                        result = 8  # binary proposition

        elif fmla[0] in PROPOSITIONS and len(fmla) == 1:
            # 6 / 0 - Propositions
            result = 6
        elif fmla[0] in PREDICATES:
            # 1 / 0 - Predicates
            result = 1
            if fmla[1] == "(" and fmla[-1] == ")":
                inner_fmlas = fmla[2:-1].split(',')
                for inner_fmla in inner_fmlas:
                    if (inner_fmla not in VAR and inner_fmla not in CONSTANTS) and parse(inner_fmla) == 0:
                        result = 0
                        break
    if DEBUG_PARSER:
        print(fmla, result)
    return result

def get_con_idx(fmla):
    l_count = 0
    con_idx = -1
    for idx, elem in enumerate(fmla[1:]):
        if elem == "(":
            l_count += 1
        elif elem == ")":
            l_count -= 1
        if l_count == 0:
            if elem in CONNECTIVES:
                con_idx = idx + 1
                break
        elif l_count < 0:
            break
    return con_idx

# Return the LHS of a binary connective formula
def lhs(fmla):
    return fmla[1:get_con_idx(fmla)]

# Return the connective symbol of a binary connective formula
def con(fmla):
    return fmla[get_con_idx(fmla)]

# Return the RHS symbol of a binary connective formula
def rhs(fmla):
    return fmla[get_con_idx(fmla) + 1:-1]

def _replace_in_scope(this_fmla, target, sub):
    if DEBUG_SAT:
        print(this_fmla, target, sub)
    is_inner = False
    is_quantifier = False
    num_left = 0
    rlt = this_fmla[:2]
    for i in range(len(this_fmla) - 2):

        this_char = this_fmla[i + 2]

        if not is_quantifier:
            if not is_inner:
                if this_char in QUANTIFIERS:
                    is_inner = True
                    is_quantifier = True
                elif this_char == target:
                    this_char = sub
            else:
                if this_char == '(':
                    num_left += 1
                elif this_char == ')':
                    num_left -= 1
                elif num_left == 0 and this_char in CONNECTIVES:
                    is_inner = False
        else:
            if this_char != target:
                is_inner = False
            is_quantifier = False

        rlt += this_char
    return rlt

def _no_free_var(this_fmla, target):
    if DEBUG_SAT:
        print("searching for free var... ", this_fmla, target)
    is_inner = False
    is_quantifier = False
    num_left = 0
    rlt = True
    for i in range(len(this_fmla) - 2):

        this_char = this_fmla[i + 2]

        if not is_quantifier:
            if not is_inner:
                if this_char in QUANTIFIERS:
                    is_inner = True
                    is_quantifier = True
                elif this_char == target:
                    rlt = False
                    break
            else:
                if this_char == '(':
                    num_left += 1
                elif this_char == ')':
                    num_left -= 1
                elif num_left == 0 and this_char in CONNECTIVES:
                    is_inner = False
        else:
            if this_char != target:
                is_inner = False
            is_quantifier = False

    return rlt

def _clean_double_neg(this_fmla):
    rlt = []
    for c in this_fmla:
        if c == '-' and len(rlt) > 0 and rlt[-1] == '-':
            rlt.pop()
        else:
            rlt.append(c)
    return ''.join(rlt)

def _reorder_quantifiers(this_fmla):
    this_fmla = _clean_double_neg(this_fmla)
    if parse(this_fmla) < 6:
        reordered_quantifiers = ""
        this_parsed = parse(this_fmla)
        while this_parsed in [3, 4, 2]:
            is_neg = this_parsed == 2
            if is_neg:
                this_fmla = this_fmla[1:]
                this_parsed = parse(this_fmla)
            if this_parsed in [3, 4]:
                if this_parsed == 3:  # Ax
                    if is_neg:  # -Ax
                        reordered_quantifiers = '-' + this_fmla[:2] + reordered_quantifiers
                    else:  # Ax
                        reordered_quantifiers += this_fmla[:2]
                elif this_parsed == 4:
                    if is_neg:  # -Ex
                        reordered_quantifiers += '-' + this_fmla[:2]
                    else:  # Ex
                        reordered_quantifiers = this_fmla[:2] + reordered_quantifiers
                this_fmla = this_fmla[2:]
                this_parsed = parse(this_fmla)
            elif this_parsed in [1, 5] and is_neg:
                reordered_quantifiers += "-"
        reordered_quantifiers += this_fmla
        return reordered_quantifiers
    else:
        return this_fmla

def _reorder_sigma(this_sigma):
    reordered_sigma = this_sigma.copy()
    if parse(this_sigma[0]) == 3:
        for index, this_fmla in enumerate(this_sigma):
            if this_fmla[:2] == '-A' or this_fmla[0] == 'E':
                reordered_sigma[0], reordered_sigma[index] = reordered_sigma[index], reordered_sigma[0]
    return reordered_sigma

# print(_reorder_sigma(['AxAy(P(x,y)>-P(y,x))', '-Ax-P(x,x)']))
# result = _no_free_var('Ax(P(a,a)^Ex-P(a,x))', 'x')
# result = _reorder_quantifiers('Ax-Ey-Az-EzAyExP(x,y)')
# print(result, result == 'Ex-AzAx-Ey-EzAyP(x,y)')
# result = _reorder_quantifiers('-P(x,y)')
# print(result, result == '-P(x,y)')
# result = _reorder_quantifiers('-(P(x,y)^Ax-Ey-Az-EzAyExP(x,y))')
# print(result, result == '-(P(x,y)^Ax-Ey-Az-EzAyExP(x,y))')
# result = _reorder_quantifiers('Ax-Ey-Az-EzAyEx(P(x,y)>P(y,x))')
# print(result, result == 'Ex-AzAx-Ey-EzAy(P(x,y)>P(y,x))')
# result = _reorder_quantifiers('-Ax-Ey-Az-EzAyEx(P(x,y)>P(y,x))')
# print(result, result == 'Ex-Az-Ax-Ey-EzAy(P(x,y)>P(y,x))')
# result = _reorder_quantifiers('-Ax-Ey-Az-EzAyExP(x,y)')
# print(result, result == 'Ex-Az-Ax-Ey-EzAyP(x,y)')
# result = _reorder_quantifiers('-Ax-Ey-P(x,y)')
# print(result, result == '-Ax-Ey-P(x,y)')
# result = _reorder_quantifiers('-----------q')
# print(result, result == '-Ax-Ey-P(x,y)')
# result = _reorder_quantifiers('----Ax----Ey----P(x,y)')
# print(result, result == 'EyAxP(x,y)')

# ALLOWED_CONSTS = [chr(97 + i) for i in range(15)] + [chr(97 + i) for i in range(19, 22)]
# print(ALLOWED_CONSTS)

print(_no_free_var('Ax(-P(x,x)>P(x,x))', 'x'))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Apr  8 19:38:13 2021

"""
from copy import deepcopy
import itertools
import numpy as np
import torch
from torch.optim import Adam
import torch.nn.functional as F
import torch.optim
import gym
import time
import core as core
import random
import pdb
import torch.optim as optim
from typing import List, Optional
from torch import Tensor
import math

class ReplayBuffer:
    """
    A simple FIFO experience replay buffer for SAC agents.
    """

    def __init__(self, obs_dim, act_dim, size):
        self.obs_buf = np.zeros(core.combined_shape(size, obs_dim), dtype=np.float32)
        self.obs2_buf = np.zeros(core.combined_shape(size, obs_dim), dtype=np.float32)
        self.act_buf = np.zeros(core.combined_shape(size, act_dim), dtype=np.float32)
        self.rew_buf = np.zeros(size, dtype=np.float32)
        self.done_buf = np.zeros(size, dtype=np.float32)
        self.ptr, self.size, self.max_size = 0, 0, size

    def store(self, obs, act, rew, next_obs, done):
        # pdb.set_trace()
        self.obs_buf[self.ptr] = obs
        self.obs2_buf[self.ptr] = next_obs
        self.act_buf[self.ptr] = act
        self.rew_buf[self.ptr] = rew
        self.done_buf[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=32):
        idxs = np.random.randint(0, self.size, size=batch_size)
        batch = dict(obs=self.obs_buf[idxs],
                     obs2=self.obs2_buf[idxs],
                     act=self.act_buf[idxs],
                     rew=self.rew_buf[idxs],
                     done=self.done_buf[idxs])
        return {k: torch.as_tensor(v, dtype=torch.float32) for k,v in batch.items()}

class DQN:
    def __init__(self,env,q_network=core.MLPQnet,seed=0,
            replay_size=int(1e6), gamma=0.99,
            polyak=0.995, lr=1e-3, batch_size=100,hidden_sizes=(24,48,48,48)):
        '''
        Args:
            q_network - DQN network (main and target network).
            env - Environment (GYM).
            replay_size - Replay buffer size.
            gamma - Discount factor.
            polyak - Polyak average parameter.
            lr - Learning rate.
            batch_size - Batch size of the current env.
        '''
        # Discount factor.
        self.gamma = gamma
        # Learning rate.
        self.lr = lr
        # Polay (future addition)
        self.polyak = polyak
        # Seed the random value.
        torch.manual_seed(seed)
        np.random.seed(seed)
        # Batch size
        self.batch_size = batch_size

        # Observation space.
        obs_dim = env.observation_space.shape
        act_dim = env.action_space.n

        # Create actor-critic module and target networks
        self.DQ_net = q_network(env.observation_space, env.action_space, hidden_sizes)
        # Traget network.
        self.T_net = deepcopy(self.DQ_net)

        # Freeze target networks with respect to optimizers (only update via polyak averaging)
        for p in self.T_net.q_net.parameters():
            p.requires_grad = False

        # Experience buffer
        self.replay_buffer = ReplayBuffer(obs_dim=obs_dim, act_dim=1, size=replay_size)
        print('R')
        # Count variables (Usefull while experimenting with deeper network).
        self.var_counts = tuple(core.count_vars(module) for module in [self.DQ_net.q_net])
        # Set up optimizers for policy and q-function
        self.q_optimizer = Adam(self.DQ_net.q_net.parameters(), lr=self.lr)
        # Loss function.
        self.mse_loss = torch.nn.MSELoss()

    def update(self,data):
        '''
        Args:
            data - batch data.
        '''
        o, a, r, o2, d = data['obs'],\
            data['act'], data['rew'], \
                data['obs2'], data['done']
        # for p in self.T_net.q_net.parameters():
        #     p.requires_grad = False
        # Compute the Q values for the main network.
        # pdb.set_trace()
        a = a.to(torch.int64)
        state_action_values = self.DQ_net.q_net(o).gather(1, a)
        # Create a mask for done task.
        mask = 1 - d
        # Next state Q Values.
        next_state_values = self.T_net.q_net(o2).max(1)[0].detach()
        next_state_values = torch.mul(mask, next_state_values)
        # Expected Q values.
        expected_SA_values = (next_state_values*self.gamma) + r

        # Compute the loss.
        # loss = torch.nn.MSELoss(state_action_values, expected_SA_values.unsqueeze(1))
        loss = self.mse_loss(state_action_values, expected_SA_values.unsqueeze(1))
        # pdb.set_trace()
        # Compute the gradient and optimize.
        # for p in self.DQ_net.q_net.parameters():
        #     p.requires_grad = True
        self.DQ_net.q_net.zero_grad()
        loss.backward()
        self.q_optimizer.step()

        return loss.detach().item()

class TDProp:
    def __init__(self,env,q_network=core.MLPQnet,seed=0,
            replay_size=int(1e6), gamma=0.99,
            polyak=0.995, lr=1e-3, beta1 = 0.1, beta2 = 0.1, eps = 0.1,
            batch_size=100, hidden_sizes=(24,48,48,48)):
        '''
        Args:
            q_network - DQN network (main and target network).
            env - Environment (GYM).
            replay_size - Replay buffer size.
            gamma - Discount factor.
            polyak - Polyak average parameter.
            lr - Learning rate.
            batch_size - Batch size of the current env.
            beta1 - Exponential decay rate of
        '''
        # Discount factor.
        self.gamma = gamma
        # Learning rate.
        self.lr = lr
        # Polay (future addition)
        self.polyak = polyak
        # Seed the random value.
        torch.manual_seed(seed)
        np.random.seed(seed)
        # Batch size
        self.batch_size = batch_size

        # Observation space.
        obs_dim = env.observation_space.shape
        act_dim = env.action_space.n

        # Create actor-critic module and target networks
        self.DQ_net = q_network(env.observation_space, env.action_space,hidden_sizes)
        # Traget network.
        self.T_net = deepcopy(self.DQ_net)

        # Freeze target networks with respect to optimizers (only update via polyak averaging)
        for p in self.T_net.q_net.parameters():
            p.requires_grad = False

        # Experience buffer
        self.replay_buffer = ReplayBuffer(obs_dim=obs_dim, act_dim=1, size=replay_size)
        print('R')
        # Count variables (Usefull while experimenting with deeper network).
        self.var_counts = tuple(core.count_vars(module) for module in [self.DQ_net.q_net])
        # Set up optimizers for policy and q-function
        # self.q_optimizer = AdamW(self.DQ_net.q_net.parameters(), lr=self.lr)
        self.q_optimizer = TDupdate(self.DQ_net.q_net.parameters(), lr=self.lr)
        # Loss function.
        self.mse_loss = torch.nn.MSELoss()

    def update(self,data):
        '''
        Args:
            data - batch data.
        '''
        o, a, r, o2, d = data['obs'],\
            data['act'], data['rew'], \
                data['obs2'], data['done']
        # for p in self.T_net.q_net.parameters():
        #     p.requires_grad = False
        # Compute the Q values for the main network.
        # pdb.set_trace()
        a = a.to(torch.int64)
        state_action_values = self.DQ_net.q_net(o).gather(1, a)
        # Create a mask for done task.
        mask = 1 - d
        # Next state Q Values.
        next_state_values = self.T_net.q_net(o2).max(1)[0].detach()
        next_state_values = torch.mul(mask, next_state_values)
        # Expected Q values.
        expected_SA_values = (next_state_values*self.gamma) + r

        # Compute the loss.
        # loss = torch.nn.MSELoss(state_action_values, expected_SA_values.unsqueeze(1))
        loss = self.mse_loss(state_action_values, expected_SA_values.unsqueeze(1))
        # pdb.set_trace()
        # Compute the gradient and optimize.
        # for p in self.DQ_net.q_net.parameters():
        #     p.requires_grad = True
        self.DQ_net.q_net.zero_grad()
        loss.backward()
        # TD error.
        delta = loss.detach().numpy().item()
        # Update the weights.
        self.q_optimizer.step(delta)

        return loss.detach().item()

class TDupdate(optim.Optimizer):
    r"""Implements AdamW algorithm.

    The original Adam algorithm was proposed in `Adam: A Method for Stochastic Optimization`_.
    The AdamW variant was proposed in `Decoupled Weight Decay Regularization`_.

    Args:
        params (iterable): iterable of parameters to optimize or dicts defining
            parameter groups
        lr (float, optional): learning rate (default: 1e-3)
        betas (Tuple[float, float], optional): coefficients used for computing
            running averages of gradient and its square (default: (0.9, 0.999))
        eps (float, optional): term added to the denominator to improve
            numerical stability (default: 1e-8)
        weight_decay (float, optional): weight decay coefficient (default: 1e-2)
        amsgrad (boolean, optional): whether to use the AMSGrad variant of this
            algorithm from the paper `On the Convergence of Adam and Beyond`_
            (default: False)

    .. _Adam\: A Method for Stochastic Optimization:
        https://arxiv.org/abs/1412.6980
    .. _Decoupled Weight Decay Regularization:
        https://arxiv.org/abs/1711.05101
    .. _On the Convergence of Adam and Beyond:
        https://openreview.net/forum?id=ryQu7f-RZ
    """

    def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8,
                 weight_decay=1e-2, amsgrad=False):
        if not 0.0 <= lr:
            raise ValueError("Invalid learning rate: {}".format(lr))
        if not 0.0 <= eps:
            raise ValueError("Invalid epsilon value: {}".format(eps))
        if not 0.0 <= betas[0] < 1.0:
            raise ValueError("Invalid beta parameter at index 0: {}".format(betas[0]))
        if not 0.0 <= betas[1] < 1.0:
            raise ValueError("Invalid beta parameter at index 1: {}".format(betas[1]))
        if not 0.0 <= weight_decay:
            raise ValueError("Invalid weight_decay value: {}".format(weight_decay))
        defaults = dict(lr=lr, betas=betas, eps=eps,
                        weight_decay=weight_decay, amsgrad=amsgrad)
        super(TDupdate, self).__init__(params, defaults)

    def __setstate__(self, state):
        super(TDupdate, self).__setstate__(state)
        for group in self.param_groups:
            group.setdefault('amsgrad', False)

    @torch.no_grad()
    def step(self, delta, closure=None):
        """Performs a single optimization step.

        Args:
            delta - TD error.
            closure (callable, optional): A closure that reevaluates the model
                and returns the loss.
        """
        # pdb.set_trace()
        loss = None
        if closure is not None:
            with torch.enable_grad():
                loss = closure()

        for group in self.param_groups:
            params_with_grad = []
            grads = []
            exp_avgs = []
            exp_avg_sqs = []
            state_sums = []
            max_exp_avg_sqs = []
            state_steps = []
            amsgrad = group['amsgrad']

            for p in group['params']:
                if p.grad is None:
                    continue
                params_with_grad.append(p)
                if p.grad.is_sparse:
                    raise RuntimeError('AdamW does not support sparse gradients')
                grads.append(p.grad)

                state = self.state[p]

                # State initialization
                if len(state) == 0:
                    state['step'] = 0
                    # Exponential moving average of gradient values
                    state['exp_avg'] = torch.zeros_like(p, memory_format=torch.preserve_format)
                    # Exponential moving average of squared gradient values
                    state['exp_avg_sq'] = torch.zeros_like(p, memory_format=torch.preserve_format)
                    if amsgrad:
                        # Maintains max of all exp. moving avg. of sq. grad. values
                        state['max_exp_avg_sq'] = torch.zeros_like(p, memory_format=torch.preserve_format)

                exp_avgs.append(state['exp_avg'])
                exp_avg_sqs.append(state['exp_avg_sq'])

                if amsgrad:
                    max_exp_avg_sqs.append(state['max_exp_avg_sq'])

                beta1, beta2 = group['betas']
                # update the steps for each param group update
                state['step'] += 1
                # record the step after step update
                state_steps.append(state['step'])

            alog_tdprop(params_with_grad,
                    grads,
                    delta,
                    exp_avgs,
                    exp_avg_sqs,
                    max_exp_avg_sqs,
                    state_steps,
                    amsgrad,
                    beta1,
                    beta2,
                    group['lr'],
                    group['weight_decay'],
                    group['eps'])

        return loss

def alog_tdprop(params: List[Tensor],
          grads: List[Tensor],
          delta: float,
          exp_avgs: List[Tensor],
          exp_avg_sqs: List[Tensor],
          max_exp_avg_sqs: List[Tensor],
          state_steps: List[int],
          amsgrad: bool,
          beta1: float,
          beta2: float,
          lr: float,
          weight_decay: float,
          eps: float):
    r"""Functional API that performs AdamW algorithm computation.

    See :class:`~torch.optim.AdamW` for details.
    """
    for i, param in enumerate(params):
        # pdb.set_trace()
        grad = grads[i]
        # Gradient divided by loss.
        grad_delta = grad/delta
        exp_avg = exp_avgs[i]
        exp_avg_sq = exp_avg_sqs[i]
        step = state_steps[i]

        # Perform stepweight decay
        param.mul_(1 - lr * weight_decay)

        bias_correction1 = 1 - beta1 ** step
        bias_correction2 = 1 - beta2 ** step

        # Decay the first and second moment running average coefficient
        exp_avg.mul_(beta1).add_(grad, alpha=1 - beta1)
        exp_avg_sq.mul_(beta2).addcmul_(grad_delta, grad, value=1 - beta2)
        if amsgrad:
            # Maintains the maximum of all 2nd moment running avg. till now
            torch.maximum(max_exp_avg_sqs[i], exp_avg_sq, out=max_exp_avg_sqs[i])
            # Use the max. for normalizing running avg. of gradient
            denom = (max_exp_avg_sqs[i].sqrt() / math.sqrt(bias_correction2)).add_(eps)
        else:
            denom = (exp_avg_sq.sqrt() / math.sqrt(bias_correction2)).add_(eps)

        step_size = lr / bias_correction1

        param.addcdiv_(exp_avg, denom, value=-step_size)

class DQN_CE:
    def __init__(self,env,q_network=core.MLPQnet,seed=0,
            replay_size=int(1e6), gamma=0.99,
            polyak=0.995, lr=1e-3, beta1 = 0.1, beta2 = 0.1, eps = 0.1,
            batch_size=100,hidden_sizes=(24,48,48,48)):
        '''
        Args:
            q_network - DQN network (main and target network).
            env - Environment (GYM).
            replay_size - Replay buffer size.
            gamma - Discount factor.
            polyak - Polyak average parameter.
            lr - Learning rate.
            batch_size - Batch size of the current env.
            beta1 - Exponential decay rate of
        '''
        # Discount factor.
        self.gamma = gamma
        # Learning rate.
        self.lr = lr
        # Polay (future addition)
        self.polyak = polyak
        # Seed the random value.
        torch.manual_seed(seed)
        np.random.seed(seed)
        # Batch size
        self.batch_size = batch_size

        # Observation space.
        obs_dim = env.observation_space.shape
        act_dim = env.action_space.n

        # Create actor-critic module and target networks
        self.DQ_net = q_network(env.observation_space, env.action_space,hidden_sizes)
        # Traget network.
        self.T_net = deepcopy(self.DQ_net)

        # Freeze target networks with respect to optimizers (only update via polyak averaging)
        for p in self.T_net.q_net.parameters():
            p.requires_grad = False

        # Experience buffer
        self.replay_buffer = ReplayBuffer(obs_dim=obs_dim, act_dim=1, size=replay_size)

        # Count variables (Usefull while experimenting with deeper network).
        self.var_counts = tuple(core.count_vars(module) for module in [self.DQ_net.q_net])
        # Set up optimizers for policy and q-function
        # self.q_optimizer = AdamW(self.DQ_net.q_net.parameters(), lr=self.lr)
        self.q_optimizer = Adam(self.DQ_net.q_net.parameters(), lr=self.lr)
        # Loss function.
        self.mse_loss = torch.nn.MSELoss()

    def update(self,data):
        '''
        Args:
            data - batch data.
        '''
        o, a, r, o2, d = data['obs'],\
            data['act'], data['rew'], \
                data['obs2'], data['done']
        # for p in self.T_net.q_net.parameters():
        #     p.requires_grad = False
        # Compute the Q values for the main network.
        # pdb.set_trace()
        a = a.to(torch.int64)
        q_val_s = self.DQ_net.q_net(o)
        state_action_values = q_val_s.gather(1, a)
        # Create a mask for done task.
        mask = 1 - d
        # Next state Q Values.
        q_val_next_s = self.T_net.q_net(o2)
        next_state_values = q_val_next_s.max(1)[0].detach()
        next_state_values = torch.mul(mask, next_state_values)
        # Expected Q values.
        expected_SA_values = (next_state_values*self.gamma) + r
        # Compute the loss.
        # pdb.set_trace()
        # loss = torch.nn.MSELoss(state_action_values, expected_SA_values.unsqueeze(1))
        loss = self.mse_loss(state_action_values, expected_SA_values.unsqueeze(1))
        # pdb.set_trace()
        #####################
        # Cross entropy loss.
        #####################

        # Probability of choosing action for state s (target network).
        q_val_s_target = self.T_net.q_net(o)
        # state_val_target = q_val_s_target.max(1)[0].detach()
        state_val_target = q_val_s_target.gather(1, a).detach()
        den_ns = (q_val_s_target.detach()).exp()
        sum_den_ns = den_ns.sum(1)
        num_ns = state_val_target.exp()
        num_ns = num_ns.reshape(num_ns.shape[0])
        pi_a_ns = num_ns/sum_den_ns

        # Probability of choosing action for state.
        den_s = q_val_s.exp()
        sum_den_s = den_s.sum(1)
        # sum_den_s = sum_den_s.reshape(sum_den_s.shape[0])
        num_s = state_action_values.exp()
        num_s = num_s.reshape(num_s.shape[0])
        pi_a_s = num_s/sum_den_s

        check = (den_ns == np.nan).any().item()
        if check == True:
            print('hold')
            pdb.set_trace()

        # CE loss.
        # CE_loss = pi_a_ns*pi_a_s.log()
        CE_loss = pi_a_s*pi_a_ns.log()
        CE_loss = -CE_loss
        # KL divergence.
        KL = CE_loss - (-pi_a_s*pi_a_s.log())
        # pdb.set_trace()

        # Compute the loss.
        # loss = torch.nn.MSELoss(state_action_values, expected_SA_values.unsqueeze(1))
        # loss = self.mse_loss(state_action_values + 0.0*KL.reshape((KL.shape[0],1)), expected_SA_values.unsqueeze(1))
        loss = self.mse_loss(state_action_values + 0.5*CE_loss.reshape((CE_loss.shape[0],1)), expected_SA_values.unsqueeze(1))
        loss = self.mse_loss(state_action_values, expected_SA_values.unsqueeze(1))

        # Total loss
        # total_loss = loss + 0.001*KL.mean()
        # total_loss = loss + 1*CE_loss.mean()
        total_loss = loss

        # Compute the gradient and optimize.
        # for p in self.DQ_net.q_net.parameters():
            # p.requires_grad = True
        self.DQ_net.q_net.zero_grad()
        total_loss.backward()
        # for param in self.DQ_net.q_net.parameters():
        #     param.grad.data.clamp_(-1, 1)

        # Update the weights.
        self.q_optimizer.step()

        return loss.detach().item()


# type: ignore
import sys

import fire

sys.path.append("src")

from dataset import MUCDataset  # noqa: E402

def main(
    path: str,
    lang: str,
    split: str,
    bitext: str,
    alignment: str,
    output_bitext: str,
    output_alignment: str,
):
    o_a = open(output_alignment, "w")
    o_b = open(output_bitext, "w")
    b_list = []
    a_list = []
    src = []
    # mt = MosesTokenizer(lang="en")
    file_path = MUCDataset.get_file(path, lang, split)
    with open(bitext, "r") as tfp:
        for line in tfp:
            src_text, tgt_text = line.strip().split(" ||| ")
            b_list.append((src_text, tgt_text))
            src.append(src_text.split())

    with open(alignment, "r") as tfp:
        for line in tfp:
            mapping = [
                tuple([int(i) for i in x.split("-")]) for x in line.strip().split(" ")
            ]
            a_list.append(mapping)
    for example in MUCDataset.read_file(file_path, lang, split):
        trnslt = ""
        als = ""
        start_src = 0
        start_trg = 0
        for i in range(len(example["sent"])):
            trnslt += b_list[i][1] + " "
            for j in a_list[i]:
                als += str(start_src + j[0]) + "-" + str(start_trg + j[1]) + " "
            start_src += len(src[i])
            start_trg += len(b_list[i][1].split())
        o_a.write(als + "\n")
        o_b.write(" ".join(example["doc"]) + " ||| " + trnslt + "\n")
        src = src[len(example["sent"]) :]
        a_list = a_list[len(example["sent"]) :]
        b_list = b_list[len(example["sent"]) :]

if __name__ == "__main__":
    fire.Fire(main)


# -*- coding: UTF-8 -*-

import socket

s = socket.socket()
host = socket.gethostname()
port = 12345
s.bind((host, port))

s.listen(5)

str = 'this is fujieace.com test'
str = str.encode()

while True:
    c,addr = s.accept()
    print ('连接地址：', addr)
    c.send(str)
    c.close()


class Solution:
    def spiralOrder(self, matrix: list) -> list:
        if matrix == []:
            return []
        _index_i = 0
        _index_j = 0
        m_0 = 0
        n_0 = 0
        m_1 = len(matrix)
        n_1 = len(matrix[0])
        if m_1 == 1:
            return matrix[0]
        if n_1 == 1:
            solution_list = []
            for i in range(m_1):
                solution_list.append(matrix[i][0])
            return solution_list
        i_old = 0
        j_old = 0
        solution_list = []
        solution_list.append(matrix[_index_i][_index_j])
        # print(matrix[_index_i][_index_j])
        count = 1
        count_max = m_1 * n_1
        # print(count_max)
        while count < count_max:
            m_0, n_0, m_1, n_1, i_old, j_old, _index_i, _index_j = self.next_indexex(m_0, n_0, m_1, n_1, i_old, j_old,
                                                                                _index_i, _index_j)
            # print(matrix[_index_i][_index_j])
            # print('output: ', m_0, n_0, m_1, n_1, i_old, j_old, _index_i, _index_j)
            solution_list.append(matrix[_index_i][_index_j])
            count = count + 1
            # print(count, count)
            # print(solution_list)
        return solution_list

    def next_indexex(self, m_0, n_0, m_1, n_1, i_old, j_old, i, j):
        if i == 0 and j == 0:
            return m_0, n_0, m_1, n_1, i, j, i, j + 1

        if i_old == i:
            if j_old < j:
                if j < n_1 - 1:
                    return m_0, n_0, m_1, n_1, i, j, i, j + 1
                if j == n_1 - 1:
                    return m_0, n_0, m_1, n_1, i, j, i + 1, j
            if j_old > j:
                if j > n_0:
                    return m_0, n_0, m_1, n_1, i, j, i, j - 1
                if j == n_0:
                    return m_0, n_0, m_1, n_1, i, j, i - 1, j

        if j_old == j:
            if i_old < i:
                if i < m_1 - 1:
                    return m_0, n_0, m_1, n_1, i, j, i + 1, j
                if i == m_1 - 1:
                    return m_0, n_0, m_1, n_1, i, j, i, j - 1
            if i_old > i:
                if i > (m_0 + 1):
                    return m_0, n_0, m_1, n_1, i, j, i - 1, j
                if i == (m_0 + 1):
                    return m_0 + 1, n_0 + 1, m_1 - 1, n_1 - 1, i, j, i, j + 1

test_matrix = [[ 1, 2, 3, 4, 5],
               [ 6, 7, 8, 9,10],
               [11,12,13,14,15],
               [16,17,18,19,20],
               [21,22,23,24,25]
               ]
Sol = Solution()
test_result = Sol.spiralOrder(test_matrix)
print(test_result)


from .resource import CallFilterItem, CallFilterList
from .service import build_service

class Plugin:
    def load(self, dependencies):
        api = dependencies['api']
        service = build_service()

        api.add_resource(CallFilterList, '/callfilters', resource_class_args=(service,))

        api.add_resource(
            CallFilterItem,
            '/callfilters/<int:id>',
            endpoint='callfilters',
            resource_class_args=(service,),
        )


from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect, render

# Create your views here.
from eshop_orders.forms import OrderForm
from eshop_orders.models import Order
from eshop_products.models import Product

@login_required(login_url='eshop_account:login')
def add_order(request):
    order_form = OrderForm(request.POST or None)
    if order_form.is_valid():
        order = Order.objects.filter(owner_id=request.user.id, isPaid=False).first()
        if order is None:
            order = Order.objects.create(owner_id=request.user.id, isPaid=False)
        productId = order_form.cleaned_data['productId']
        tedad = order_form.cleaned_data['tedad']
        if tedad <= 0:
            tedad = 1
        product = Product.objects.get_by_id(productId)
        order.orderdetail_set.create(product_id=product.id, gheymat=product.price, tedad=tedad)
        # felan redirecte maskhare
        title_product = product.title.replace(' ', '-')
        return redirect(f'/products/{productId}/{title_product}')
    return redirect('/')

@login_required(login_url='eshop_account:login')
def card_detail(request):
    context = {
        "order": None,
        "details": None,
    }
    order: Order = Order.objects.filter(owner_id=request.user.id, isPaid=False).first()
    if order is not None:
        context['order'] = order
        context['details'] = order.orderdetail_set.all()
    return render(request, 'orders/order_detail.html', context)


import pytest

import pyalgos.data_structures.graph as g
import pyalgos.algorithms.graph.topological_sort as t

def test_hamburger():
    peel_onions = 'peel onions'
    chop_onions = 'chop onions'
    fry_onions = 'fry onions'
    grill_patty = 'grill patty'
    put_patty_on_bun = 'put patty on bun'
    put_onion_on_patty = 'put onion on patty'
    put_top_on_bun = 'put_top_on_bun'
    serve = 'serve'

    gr = g.Graph(is_directed=True)
    gr.add_edge(put_top_on_bun, serve)
    gr.add_edge(put_onion_on_patty, put_top_on_bun)
    gr.add_edge(put_patty_on_bun, put_onion_on_patty)
    gr.add_edge(grill_patty, put_patty_on_bun)
    gr.add_edge(fry_onions, put_onion_on_patty)
    gr.add_edge(chop_onions, fry_onions)
    gr.add_edge(peel_onions, chop_onions)

    assert t.topological_sort(gr) == [
        peel_onions,
        chop_onions,
        fry_onions,
        grill_patty,
        put_patty_on_bun,
        put_onion_on_patty,
        put_top_on_bun,
        serve
    ]

def test_cycle():
    gr = g.Graph(is_directed=True)
    gr.add_edge('a', 'b')
    gr.add_edge('b', 'c')
    gr.add_edge('c', 'a')
    with pytest.raises(Exception):
        t.topological_sort(gr)


from django import forms
from .models import Attachment
from django.forms import ModelForm, FileField, ClearableFileInput

class fromAttachment(forms.ModelForm):
    class Meta:
    	model = Attachment
    	fields = ['data']
    	widgets = {
    		# hook angular listener
    		'data' : ClearableFileInput(attrs={'onchange': 'angular.element(this).scope().attach(this.files)'}),
    	}


import re

from openelexdata.us.ia.util import split_line_into_columns
from openelexdata.us.ia.parser import BaseParser, ParserState

whitespace_re = re.compile(r'\s{2,}')
number_re = re.compile(r'[0-9,]+')
clean_number_re = re.compile(r'^\d+$')

class RootState(ParserState):
    name = 'root'

    def handle_line(self, line):
        if line.startswith("GENERAL ELECTION"):
            self._context.change_state('header')

class HeaderState(ParserState):
    name ='header'

    party_cols = [
        "COUNTY",
        "CO #",
        "PRECINCT NAME",
        "DEM",
        "REP",
        "OTH",
        "SC",
        "DEM",
        "REP",
        "OTH",
        "SC",
        "DEM",
        "REP",
        "OTH",
        "SC",
        "DEM",
        "REP",
        "OTH",
        "SC",
        "DEM",
        "REP",
        "OTH",
        "SC",
        "IA HOUSE",
        "IA SENATE",
        "US REP",
    ]

    def enter(self):
        self.handle_line(self._context.current_line)

    def handle_line(self, line):
        if line.startswith("GENERAL ELECTION"):
            self._parse_offices(line)
        elif line.startswith("COUNTY"):
            self._parse_parties(line)
            self._context['breaks'] = self._detect_column_breaks(self._context.raw_line)
        else:
            # It's a result!
            self._context.change_state('results')

    def _parse_offices(self, line):
        if 'offices' in self._context:
            # We've already parsed offices, continue
            return

        cols = whitespace_re.split(line)
        # Ignore the first bit "GENERAL ELECTION" and the last
        # "IOWA DISTRICTS NUMBERS".  The rest are office names
        self._context['offices'] = cols[1:-1]

    def _parse_parties(self, line):
        if 'parties' in self._context:
            # We've already parsed parties, continue
            return

        cols = whitespace_re.split(line)
        # Skip the leading colums, which are county name, county number and
        # precinct name and the final one which are the distrct number headers
        self._context['parties'] = cols[3:7]

    @classmethod
    def _detect_column_breaks(cls, line):
        breaks = []
        last_idx = 0
        break_idx = 0

        for s in cls.party_cols:
            if s == "COUNTY":
                breaks.append(0)
            elif s == "PRECINCT NAME":
                breaks.append(breaks[break_idx - 1] + 4)
            else:
                i = line.index(s, last_idx)
                breaks.append(i)
                last_idx = i

            break_idx += 1

        return breaks

class ResultsState(ParserState):
    name = 'results'

    def enter(self):
        self.handle_line(self._context.current_line)

    def handle_line(self, line):
        if line.startswith("GENERAL ELECTION"):
            self._context.change_state('header')
            return
        elif line == "":
            return
        elif ("Douglas Melville N 1/2 Leroy Twps N 1/2 Audubon" in line or
              "Audubon Exire Greeley Hamlin Twps & Exira City" in line or
              "Jamestown Twp., Saratoga Twp., & Howard Center" in line):
            # Some lines in the PDF get split into two lines when converted to
            # text.  Save the initial columns and merge them with
            # columns from the next row later.
            self._context['previous_cols'] = whitespace_re.split(line)
            return
        elif 'previous_cols' in self._context:
            clean_line = self._context.raw_line.replace("City", "    ")
            clean_line = clean_line.replace("Twp.", "    ")
            cols = split_line_into_columns(clean_line,
                self._context['breaks'])
            cols = self._merge(self._context['previous_cols'],
                cols)
            del self._context['previous_cols']

        else:
            # Split on fixed width instead of whitespace re because some
            # cols have missing values

            # Generally, the columns line up with the headers
            breaks = self._context['breaks']

            if line.startswith("Grand Total"):
                # The last line in the file has column breaks that don't
                # quite line up with the headers
                breaks = [0, 17, 21, 54, 66, 76, 85, 93, 103, 113, 122, 130, 140, 150, 157, 165, 173, 181, 189, 197, 209, 219, 228, 236, 245, 255]

            cols = split_line_into_columns(self._context.raw_line, breaks)

        # Fourth column (index 3) should be the start of vote results
        # and therefore a number
        if not number_re.match(cols[3]):
            if cols[0].endswith("Total"):
                cols = self._split_totals(cols)
            elif ("Absentee and Special Ballots" in cols[2] or
                  "Oskaloosa Ward 2" in cols[2] or
                  "Oskaloosa Ward 3" in cols[2]):
                # Some rows just have blank values in certain columns. Onward.
                pass
            elif "Falls Plymouth Lime Creek Mason N Twps Pct" in line:
                cols[2] = "Falls Plymouth Lime Creek Mason N Twps Pct"
                cols[3] = "265"
            else:
                print(cols)
                raise AssertionError("Unexpected column alignment")

        county = cols[0]
        county_num = cols[1]
        jurisdiction = cols[2]
        votes = cols[3:-3]
        district_numbers = cols[-3:]

        for i in range(len(votes)):
            vote = votes[i].replace(',', '')

            if vote == "" or vote == "X":
                continue

            assert clean_number_re.match(vote), "Invalid vote value: {}".format(vote)

            office_idx = i / len(self._context['parties'])
            office = self._context['offices'][office_idx]
            party = self._context['parties'][i % len(self._context['parties'])]

            if office == "US REPRESENTATIVE":
                district = district_numbers[2]
            elif office == "IOWA SENATE":
                district = district_numbers[1]
            elif office == "IOWA HOUSE":
                district = district_numbers[0]
            else:
                district = ""

            result = {
                'office': office,
                'district': district,
                'candidate': '',
                'party': party,
                'reporting_level': 'precinct',
                'jurisdiction': jurisdiction,
                'county': county,
                'county_number': county_num,
                'votes': vote,
            }
            self._context.results.append(result)

    @classmethod
    def _merge(cls, l1, l2):
        if (len(l1) > len(l2)):
            longer = l1
            shorter = l2
        else:
            longer = l2
            shorter = l1

        return shorter[0:] + longer[len(shorter):]

    def _split_totals(self, cols):
        """
        Split first column into county name, county number and precinct name
        """
        cols[0] = ' '.join(re.split(r'\s+', cols[0])[1:-1])
        cols.insert(1, "Total")
        cols.insert(2, "")
        # There should be 23 columns.  Total rows don't have the legislative
        # district cols
        assert len(cols) == 23
        return cols

class ResultParser(BaseParser):
    def __init__(self, infile):
        super(ResultParser, self).__init__(infile)
        self._register_state(RootState(self))
        self._register_state(HeaderState(self))
        self._register_state(ResultsState(self))
        self._current_state = self._get_state('root')

        self['primary'] = False

fields = [
    'office',
    'district',
    'candidate',
    'party',
    'reporting_level',
    'jurisdiction',
    'county',
    'county_number',
    'votes',
]