#from elementService import ElementService
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from utilities.testCache import TestCache

import time

def _locatorSwitcher(searchBy):
    switcher ={
        "id":By.ID,
        "class_name":By.CLASS_NAME,
        "css_selector":By.CSS_SELECTOR,
        "name":By.NAME,
        "tag_name":By.TAG_NAME,
        "link_text":By.LINK_TEXT,
        "partial_link_text":By.PARTIAL_LINK_TEXT,
        "xpath":By.XPATH,
    }
    return switcher.get(searchBy,"Locator type not supported")

class BaseElement(object):
    """Base class with all Common actions.

    .. note::
        Use only when not sure about the control type. Otherwise use custom classes

    :param locators: Key-Value pair of identifier used to locate the element. Available locators- id,class_name,css_selector,name,tag_name,link_text,partial_link_text,xpath
    :type locators: kwargs

    """
    def __init__(self, **kwargs):
        self._currentElement:WebElement = None
        self.criteria = kwargs

    def __get__(self, obj, owner):
        self.testCache:TestCache = obj.testCache
        return self

    def _searchElement(self,parentElement=None,timeoutInSeconds=5):
        try:
            self._webdriver = self.testCache.driver_service.driver
            if not parentElement:
                parentElement = self._webdriver
            # if self._currentElement is None:
            if self.criteria.__len__() == 1:
                locator,val = list(self.criteria.items())[0]
                if locator == "current_element" and isinstance(val,WebElement):
                    self._currentElement = val
                else:
                    # self._currentElement = parentElement.find_element(by=_locatorSwitcher(locator),value=val)
                    wait = WebDriverWait(self._webdriver, timeoutInSeconds)
                    self._currentElement = wait.until(ec.presence_of_element_located((_locatorSwitcher(locator),val)))

        except:
            self.testCache.logger_service.logger.exception("SearchFailure:")

    def FindElement(self,elementType,**searchCriteria):
        """Returns a child element as a Wrapped element based on Element type.

        :param elementType: Type of element to be returned
        :type elementType: Type of WebElement
        :param locators: Key-Value pair of identifier used to locate the element. Available locators- id,class_name,css_selector,name,tag_name,link_text,partial_link_text,xpath
        :type locators: kwargs
        :returns: Returns element of the type specified
        """
        el = None
        self._searchElement()
        try:
            locator,val = list(searchCriteria.items())[0]
            cell:WebElement = self._currentElement.find_element(by=_locatorSwitcher(locator),value=val)
            el = elementType(current_element=cell)
            el.testCache = self.testCache
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-FindElement:")
        return el

    def FindElements(self,elementType,**searchCriteria):
        """Returns a list of child elements as a Wrapped element based on Element type.

        :param elementType: Type of element to be returned
        :type elementType: Type of WebElement
        :param locators: Key-Value pair of identifier used to locate the element. Available locators- id,class_name,css_selector,name,tag_name,link_text,partial_link_text,xpath
        :type locators: kwargs
        :returns: Returns list of elements of the type specified
        """
        els = None
        self._searchElement()
        try:
            locator,val = list(searchCriteria.items())[0]
            elements:list(WebElement) = self._currentElement.find_elements(by=_locatorSwitcher(locator),value=val)
            els = [elementType(current_element=element) for element in elements]
            for el in els:
                el.testCache = self.testCache
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-FindElements:")
        return els

    def SetFocus(self):
        """Set focus on the specified element.

        """
        self._searchElement()
        try:
            self._webdriver.execute_script("arguments[0].focus();", self._currentElement)
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-SetFocus:")

    def Click(self):
        """Click on the specified element.

        """
        self._searchElement()
        try:
            try:
                self._currentElement.click()
            except:
                self._webdriver.execute_script("arguments[0].click();", self._currentElement)
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-Click:")

    def ClickElementAtCoordinates(self, xoffset, yoffset):
        """Click the element at ``xoffset/yoffset``.
        The Cursor is moved from the center of the element and x/y coordinates are
        calculated from that point.

        :param xoffset: Distance on the x coordinate
        :type xoffset: int
        :param yoffset: Distance on the y coordinate
        :type yoffset: int

        """
        self._searchElement()
        try:
            action = ActionChains(self._webdriver)
            action.move_to_element(self._currentElement)
            action.move_by_offset(xoffset, yoffset)
            action.click()
            action.perform()
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-ClickElementAtCoordinates:")

    def DoubleClick(self):
        """Double Click on the specified element.

        """
        self._searchElement()
        try:
            action = ActionChains(self._webdriver)
            action.double_click(self._currentElement).perform()
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-DoubleClick:")

    def IsEnabled(self):
        """Check if the specified element is enabled.

        :rtype: boolean
        """
        self._searchElement()
        found = False
        try:
            found = self._currentElement.is_enabled()
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-IsEnabled:")
        return found

    def IsVisible(self):
        """Check if the specified element is visible on the screen.

        :rtype: boolean
        """
        self._searchElement()
        found = False
        try:
            found = self._currentElement.is_displayed()
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-IsVisible:")
        return found

    def ScrollIntoView(self):
        """Scroll the specified element into viewable area on the screen.

        """
        self._searchElement()
        try:
            ActionChains(self._webdriver).move_to_element(self._currentElement).perform()
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-ScrollIntoView:")

    def WaitForExists(self,timeoutInSeconds=30):
        """Wait for the specified element to be visible on the screen.

        """
        try:
            self._searchElement(timeoutInSeconds)
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-WaitForExists:")

    def WaitForVisible(self,timeoutInSeconds=30):
        """Wait for the specified element to be visible on the screen.

        """
        self._searchElement()
        wait = WebDriverWait(self._webdriver, timeoutInSeconds)
        try:
            wait.until(ec.visibility_of(self._currentElement))
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-WaitForVisible:")

    def WaitForInVisible(self,timeoutInSeconds=30):
        """Wait for the specified element to be visible on the screen.

        """
        self._searchElement()
        wait = WebDriverWait(self._webdriver, timeoutInSeconds)
        try:
            wait.until(ec.invisibility_of_element(self._currentElement))
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-WaitForInVisible:")

class TextBox(BaseElement):
    """Custom class to define a Web TextBox. It subclasses :class:`BaseElement`.

    .. note::
        Check base class for params and usage.

    """
    def EnterText(self,text):
        """Enter the given text in the specified element.

        :param text: Text to be entered
        :type text: str
        """
        self._searchElement()
        try:
            self._currentElement.send_keys(text)
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-EnterText:")

    def ClearText(self):
        """Clear all the text in the specified element.

        """
        self._searchElement()
        try:
            self._currentElement.clear()
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-ClearText:")

    def GetText(self):
        """Fetch the text from the specified element.

        :rtype: str
        """
        self._searchElement()
        text = None
        try:
            text = self._currentElement.text
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-GetText:")
        return text

    def GetValue(self):
        """Fetch the value attribute from the specified element.(For cases where GetText doesn't work)

        :rtype: str
        """
        self._searchElement()
        text = None
        try:
            text = self._currentElement.get_attribute("value")
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-GetValue:")
        return text

    def GetColorValue(self):
        from selenium.webdriver.support.color import Color
        import webcolors
        """Fetch the value attribute from the specified element.(For cases where GetText doesn't work)

        :rtype: str
        """
        self._searchElement()
        text = None
        try:
            text = self._currentElement.value_of_css_property("color")
            hexform = Color.from_string(text).hex
            if hexform=="#303f9f":
                colname = 'blue'
            else:
                colname = webcolors.hex_to_name(hexform,spec='css3')
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-GetCSSValue:")
        return colname

class Button(BaseElement):
    """Custom class to define a Web Button. It subclasses :class:`BaseElement`.

    .. note::
        Check base class for params and usage.

    """
    pass

class RadioButton(BaseElement):
    """Custom class to define a Web RadioButton. It subclasses :class:`BaseElement`.

    .. note::
        Check base class for params and usage.

    """
    pass

class CheckBox(BaseElement):
    """Custom class to define a Web CheckBox. It subclasses :class:`BaseElement`.

    .. note::
        Check base class for params and usage.

    """

    def IsSelected(self):
        """Check if the specified checkbox is selected.

        :rtype: boolean
        """
        self._searchElement()
        found = False
        try:
            found = self._currentElement.is_selected()
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-IsSelected:")
        return found

    def Check(self):
        """Check the specified checkbox if it is not already checked.

        """
        self._searchElement()
        try:
            if not self._currentElement.is_selected(): self._currentElement.click()
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-Check:")

    def UnCheck(self):
        """Un-Check the specified checkbox if it is already checked.

        """
        self._searchElement()
        try:
            if self._currentElement.is_selected(): self._currentElement.click()
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-UnCheck:")

class MatSlideToggle(BaseElement):
    """Custom class to define a Web MatSlideToggle. It subclasses :class:`BaseElement`.

    .. note::
        Check base class for params and usage.

    """

    def IsSelected(self):
        """Check if the specified Toggle is selected.

        :rtype: boolean
        """
        self._searchElement()
        found = False
        try:
            class_name = self._currentElement.get_attribute("class")
            if "mat-checked" in class_name: found = True
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-IsSelected:")
        return found

    def Toggle(self,toggleon=True):
        """Check the specified Toggle if it is not already checked.

        """
        self._searchElement()
        try:
            if toggleon:
                if not self.IsSelected(): self.Click()
            else:
                if self.IsSelected(): self.Click()
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-Toggle:")

    def GetColorValue(self):
        from selenium.webdriver.support.color import Color
        import webcolors
        """Fetch the value attribute from the specified element.(For cases where GetText doesn't work)

        :rtype: str
        """
        self._searchElement()
        text = None
        try:
            text = self._currentElement.value_of_css_property("background-color")
            hexform = Color.from_string(text).hex
            if hexform == "#303f9f":
                colname = 'blue'
            else:
                colname = webcolors.hex_to_name(hexform,spec='css3')
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-GetCSSValue:")
        return colname

class ComboBox(BaseElement):
    """Custom class to define a Web ComboBox. It subclasses :class:`BaseElement`.

    .. note::
        Check base class for params and usage.

    """

    def EnterText(self,text):
        """Enter the given text in the specified element.

        :param text: Text to be entered
        :type text: str
        """
        self._searchElement()
        try:
            self._currentElement.send_keys(text)
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-EnterText:")

    def SelectByIndex(self,index:int):
        """Select an item from the ComboBox using index.

        :param index: index of the item to be selected
        :type index: int
        """
        self._searchElement()
        try:
            Select(self._currentElement).select_by_index(index)
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-SelectByIndex:")

    def SelectByText(self,text:str):
        """Select an item from the ComboBox using visible text.

        :param text: text of the item to be selected
        :type text: str
        """
        self._searchElement()
        try:
            Select(self._currentElement).select_by_visible_text(text)
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-SelectByText:")

    def SelectByValue(self,text:str):
        """Select an item from the ComboBox using value attribute.

        :param text: text of the item to be selected
        :type text: str
        """
        self._searchElement()
        try:
            Select(self._currentElement).select_by_value(text)
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-SelectByValue:")

    def SelectByNgText(self,text:str):
        """Select an item from the ComboBox using visible text based on ng select model.

        :param text: text of the item to be selected
        :type text: str
        """
        self._searchElement()
        try:
            self._currentElement.click()
            xpath = "//ng-dropdown-panel//*[text()='" +text+ "']"
            el = self._webdriver.find_element_by_xpath(xpath)
            el.click()
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-SelectByNgText:")

    def GetSelectedText(self):
        """Fetch the text of the selected item from the ComboBox.

        :rtype: str
        """
        self._searchElement()
        text = None
        try:
            text = Select(self._currentElement).first_selected_option().text()
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-GetSelectedText:")
        return text

    def GetText(self):
        """Fetch the text from the specified element.

        :rtype: str
        """
        self._searchElement()
        text = None
        try:
            text = self._currentElement.text
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-GetText:")
        return text

    def GetValue(self):
        """Fetch the value attribute from the specified element.(For cases where GetText doesn't work)

        :rtype: str
        """
        self._searchElement()
        text = None
        try:
            text = self._currentElement.get_attribute("value")
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-GetValue:")
        return text

    def GetAllOptions(self):
        """Fetch the text of the all items from the ComboBox.

        :rtype: list of str
        """
        self._searchElement()
        text = list()
        try:
            options = Select(self._currentElement).options
            text = [opt.text() for opt in options]
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-GetAllOptions:")
        return text

class Table(BaseElement):
    """Custom class to define a Web Table. It subclasses :class:`BaseElement`.

    .. note::
        Check base class for params and usage.

    """

    def GetTableCellElement(self, row, column,elementType:BaseElement):
        """Returns a table cell as a Wrapped element based on Element type.
        The cell is found using ``row index`` and ``column index`` or ``row index`` and ``column header``.
        Both row and column indexes start from 1, and header and footer
        rows are not included in the count.

        :param row: Index of the required row
        :type row: int
        :param column: Index or Header of the required column
        :type column: int or str
        :param elementType: Type of element to be returned
        :type elementType: Type of WebElement
        :returns: Returns element of the type specified
        """
        el:BaseElement = None
        self._searchElement()
        try:
            row = int(row)
            if isinstance(column,str): column = self.GetColumnByHeader(column)
            column = int(column)
            if row == 0 or column == 0:
                raise ValueError('Both row and column must be non-zero, '
                                'got row %d and column %d.' % (row, column))
            cell:WebElement = self._get_cell(row, column)
            el = elementType(current_element=cell)
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-GetTableCellElement:")
        return el

    def GetTableCellText(self, row, column):
        """Returns contents of a table cell.
        The cell is found using ``row`` and ``column``.
        Both row and column indexes start from 1, and header and footer
        rows are not included in the count.

        :param row: Index of the required row
        :type row: int
        :param column: Index or Header of the required column
        :type column: int or str
        :rtype: str
        """
        text:str = None
        self._searchElement()
        try:
            row = int(row)
            if isinstance(column,str): column = self.GetColumnByHeader(column)
            column = int(column)
            if row == 0 or column == 0:
                raise ValueError('Both row and column must be non-zero, '
                                'got row %d and column %d.' % (row, column))
            cell = self._get_cell(row, column)
            text = cell.text
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-GetTableCellText:")

        return text

    def GetRowCount(self):
        """Returns number of a rows in the table.

        :rtype: int
        """
        text:int = None
        self._searchElement()
        try:
            rows = self._get_rows()
            text = len(rows)
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-GetRowCount:")
        return text

    def GetColumnCount(self):
        """Returns number of a columns in the table.

        :rtype: int
        """
        text:int = None
        self._searchElement()
        try:
            rows = self._get_rows()
            cells = rows[0].find_elements_by_xpath('./th|./td|./mat-cell')
            text = len(cells)
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-GetColumnCount:")
        return text

    def GetColumnByHeader(self,columnHeader):
        """Returns column number based on the table header.

        :rtype: int
        """
        index:int = None
        self._searchElement()
        try:
            rows = self._get_headers()
            cells = rows[0].find_elements_by_xpath('./th|./td|./mat-header-cell')
            headers = [cell.text for cell in cells]
            index = headers.index(columnHeader) #raised error
            if index:index+1
        except:
            self.testCache.logger_service.logger.exception("ActionFailure-GetColumnByHeader:")
        return index

    def GetVisibleTableData(self,getTextByValue=False):
        """Scrap the complete visible table data.

        :rtype: list
        """
        fulltable = list()
        self._searchElement()
        try:
            headerrows = self._get_headers()
            headercells = headerrows[0].find_elements_by_xpath('./th|./td|./mat-header-cell')
            headers = [cell.text for cell in headercells]

            rows = self._get_rows()

            for row in rows:
                cells = row.find_elements_by_xpath('./th|./td|./mat-cell//input')
                if getTextByValue:
                    cellsText = [cell.get_attribute("value") for cell in cells]
                else:
                    cellsText = [cell.text for cell in cells]
                fulltable.append(dict(zip(headers,cellsText)))

        except:
            self.testCache.logger_service.logger.exception("ActionFailure-GetVisibleTableData:")
        return fulltable

    def _get_cell(self, row, column):
        rows = self._get_rows()
        if len(rows) < abs(row):
            raise AssertionError("Table should have had at least %d "
                                 "rows but had only %d."
                                 % (abs(row), len(rows)))
        index = row - 1 if row > 0 else row
        cells = rows[index].find_elements_by_xpath('./th|./td|./mat-cell')
        if len(cells) < abs(column):
            raise AssertionError("Table row %d should have had at "
                                 "least %d columns but had only %d."
                                 % (row, abs(column), len(cells)))
        index = column - 1 if column > 0 else column
        return cells[index]

    def _get_rows(self):
        # Get rows in same order as browsers render them.
        table = self._currentElement
        rows = table.find_elements_by_xpath("./tbody/tr|./mat-row")
        return rows

    def _get_headers(self):
        # Get rows in same order as browsers render them.
        table = self._currentElement
        rows = table.find_elements_by_xpath("./thead/tr|./mat-header-row")
        return rows

    def _get_footers(self):
        # Get rows in same order as browsers render them.
        table = self._currentElement
        rows = table.find_elements_by_xpath("./tfoot/tr|./mat-footer-row")
        return rows


import boto3
from flask import Flask, render_template, request
import base64
import os
import json

app = Flask(__name__)

# Get AWS credentials from environment variables
aws_access_key_id = os.environ["AWS_ACCESS"]
aws_secret_access_key = os.environ["AWS_SECRET"]

# Create a new session using the credentials
session = boto3.Session(
    aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key
)

client = boto3.client("lambda", region_name="us-east-1")

# ALLOWED_EXTENSIONS = {'jpg', 'jpeg', 'png', 'gif'}

# def allowed_file(filename):
#     return '.' in filename and \
#            filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route("/", methods=["GET", "POST"])
def predict():
    if request.method == "POST":
        print("Classifying image...")
        # Read the image file from the request
        image_file = request.files["image"]

        # Check if file is a valid image
        #if not allowed_file(image_file.filename):
        #    return "Invalid file type"

        image_data = image_file.read()

        # Encode the image data in base64 format
        encoded_image = base64.b64encode(image_data).decode("utf-8")

        # Create the payload for the Lambda function
        event = {"body": encoded_image}

        # Invoke the Lambda function
        response = client.invoke(FunctionName="classify", Payload=json.dumps(event))

        # Parse the response from the Lambda function
        response_dict = json.loads(response["Payload"].read().decode("utf-8"))
        # predictions = response_dict.get('predictions')
        predictions = json.loads(response_dict.get("body"))

        # return json.loads(response_dict.get('body'))
        return render_template("preds.html", results=predictions, image_src=request.form.get('image_src'))
        # return render_template('preds.html', results=response_dict.get('body'))

    # render the home template for GET requests
    return render_template("index.html")

@app.route('/health')
def is_healthy():
    # for app runner health check error
    return True

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8080, debug=True)


import csv

print('Leitura de Arquivo CSV')
file = open('menu.csv', 'r')
file_csv = csv.reader(file, delimiter=';')
for [sabor, valor, status] in file_csv:
    print(f'O pastel {sabor}, custa {valor} e seu status está: {status}')
file.close()
print()

print('Escrita em arquivo CSV')
create_file = open('pedidos.csv', 'w', newline='', encoding='utf-8')
write = csv.writer(create_file, delimiter=';')
header = ['ID', 'CLIENTE', 'VALOR']
write.writerow(header)

pedido1 = ['01', 'Leandro', '10']
pedido2 = ['02', 'Maria', '100']
print('Pedidos Adicionados com sucesso')
write.writerow(pedido1)
write.writerow(pedido2)

create_file.close()


import sys
from collections import deque
h, w = map(int, input().split())

# 가능한 패턴
ptn1 = 'WB' * 4
ptn2 = 'BW' * 4

# 칠해야 할 칸의 수를 표시
cnt1 = deque([0 for _ in range((w-7)*(h-7))])
cnt2 = deque([0 for _ in range((w-7)*(h-7))])
# i행 j열에서 시작하는 체스판의 index n = i*(w-7) + j

for i in range(h):
    color = input()
    if (i%2):
        str1 = ptn1; str2 = ptn2
    else:
        str1 = ptn2; str2 = ptn1
    for j in range(h-7):
        for x in range(w-7):
            diff1 = 0
            diff2 = 0
            for y in range(8):
                if color[x+y] != str1[y]:
                    diff1 += 1
                if color[x+y] != str2[y]:
                    diff2 += 1
            if i in range(j,j+8):
                cnt1[(w-7)*j+x] += diff1
                cnt2[(w-7)*j+x] += diff2

print(min(min(cnt1), min(cnt2)))

백준/Silver/1018. 체스판 다시 칠하기/체스판 다시 칠하기.py

체스판 다시 칠하기.py


from PublicData.ProjectVar import ProjectPath
import os

def getUniqnum():
    with open(os.path.join(ProjectPath,"PublicData\\uniquenumber"),"r+") as fp:
        number=fp.read()
        new_number=int(number)+1
        fp.seek(0,0)
        fp.write(str(new_number))
        print("number stored")
        return number

if __name__=="__main__":
    getUniqnum()


def style(feature):
    """
    Define the style for a feature in a Folium plot.

    Args:
        feature (dict): A dictionary representing a feature with properties including 'color'.

    Returns:
        dict: A dictionary specifying the style properties, including 'fillColor', 'color', and 'weight'.
    """
    return {
        'fillColor': feature['properties']['color'],
        'color': feature['properties']['color'],
        'weight': 1
    }


import difflib
import os
import subprocess as ipc
import sys

# pylint: disable=import-error
if sys.version_info >= (3, 0):
    import configparser
else:
    import ConfigParser as configparser
# pylint: enable=import-error

import signame  # pylint: disable=wrong-import-position

here = os.path.dirname(__file__)
basedir = '{here}/..'.format(here=here)
basedir = os.path.relpath(basedir)

script = '{dir}/py{v}diatra'.format(dir=basedir, v=sys.version_info[0])

def run_pydiatra(paths, expected, expected_stderr=None, parallel=None, env=None):
    env = env or {}
    env = dict(os.environ, **env)
    env.update(PYTHONIOENCODING='UTF-8')
    if isinstance(paths, str):
        paths = [paths]
    pyflags = '-tt'
    if sys.version_info < (3,):
        pyflags += '3'
    options = []
    if parallel is True:
        options += ['-jauto']
    elif parallel is not None:
        options += ['-j{n:d}'.format(n=parallel)]
    commandline = [sys.executable, pyflags, script] + options + paths
    checker = ipc.Popen(commandline,  # pylint: disable=consider-using-with
        stdout=ipc.PIPE,
        stderr=ipc.PIPE,
        env=env,
    )
    [stdout, stderr] = (
        s.decode('UTF-8').splitlines()
        for s in checker.communicate()
    )
    rc = checker.poll()
    expected_rc = 2 if expected else 0
    message = []
    if rc < 0:
        message += [
            'command was interrupted by signal {sig}'.format(sig=signame.get_signal_name(-rc))
        ]
    elif (expected is not None) and (rc != expected_rc):
        message += [
            'command exited with status {rc}'.format(rc=rc)
        ]
    expected_stderr = list(expected_stderr or [])
    if stderr != expected_stderr:
        if message:
            message += ['', 'stderr:']
        else:
            message += ['unexpected stderr:']
        message += ['| ' + line for line in stderr]
    if (rc in (0, 2)) and (expected is not None) and (stdout != expected):
        message = ['unexpected checker output:', '']
        diff = list(
            difflib.unified_diff(expected, stdout, n=9999)
        )
        message += diff[3:]
    if message:
        raise AssertionError(str.join('\n', message))

def get_tag_names():
    path = os.path.join(basedir, 'pydiatra', 'data', 'tags')
    os.stat(path)
    cp = configparser.RawConfigParser()
    options = {}
    if str is not bytes:
        options.update(encoding='UTF-8')
    cp.read(path, **options)
    return frozenset(t for t in cp.sections())

__all__ = [
    'basedir',
    'get_tag_names',
    'run_pydiatra',
    'script',
]

# vim:ts=4 sts=4 sw=4 et


from bs4 import BeautifulSoup as bs
import pandas as pd
with open('onion_data.xls', 'r') as f:
    content = f.read()

soup = bs(content, 'html5lib')
data = []
table = soup.find('table')
for tr in table.findAll('tr'):
    row = []
    count = 0
    for td in tr.findAll('td'):
        if count != 0:
            row.append(td.span.text)
        count += 1
    data.append(row)

print(len(data))
print(data[:5])
data_dict = []
for row in data:
    row_dict = {}
    row_dict['DISTRICT'] = row[0]
    row_dict['MARKET'] = row[1]
    row_dict['COMMODITY'] = row[2]
    row_dict['VARIETY'] = row[3]
    row_dict['GRADE'] = row[4]
    row_dict['MIN'] = row[5]
    row_dict['MAX'] = row[6]
    row_dict['MODAL'] = row[7]
    row_dict['DATE'] = row[8]
    data_dict.append(row_dict)

df = pd.DataFrame(data= data_dict)
df.to_csv("full_onion_data.csv")


from DataBase import DB_movie,DB_proxy
from web import web
import time

class spider_master(object):
    def __init__(self):
        self.spider = None
        self.movie_db().clear()

    def movie_db(self):
        tmp=DB_movie()
        return tmp.operator
    def scrapy_main(self):

        self.scrapy_slave("http://www.80s.tw/movie/list/-----p",1)
        time.sleep(30)
        self.scrapy_slave("http://www.80s.tw/ju/list/----0--p",1)
        time.sleep(30)
        self.scrapy_slave("http://www.80s.tw/dm/list/----14--p",1)
        time.sleep(30)
        self.scrapy_slave("http://www.80s.tw/zy/list/----4--p",1)
        time.sleep(30)
        self.scrapy_slave("http://www.80s.tw/mv/list/-----p",2)
        time.sleep(30)
        self.scrapy_slave("http://www.80s.tw/video/list/-----p",2)
        time.sleep(30)
        self.scrapy_slave("http://www.80s.tw/course/list/-----p",2)

    def scrapy_slave(self,other_url,mon):
        self.spider = web(35)
        length=self.spider.get_page_length(other_url+str(1))
        print("len={0}".format(length))
        count = 0
        step = 5
        while True:

            self.spider.get_overall_info(other_url,count,count+step,mon)
            self.spider.get_downlink()
            self.update_data()
            self.spider.reset()
            count = count + step
            if count + step > length:
                break
            time.sleep(30)
        self.spider.get_overall_info(other_url,count,length,mon)
        self.spider.get_downlink()
        self.update_data()
        self.spider.reset()

    def update_data(self):
        res=self.spider.return_result()
        for one in res:
            #print(one)
            self.movie_db().insert(one)

if __name__ == '__main__':
    my_spider=spider_master()
    my_spider.scrapy_main()


#!/usr/bin/python
import os
import sys

"""
Initialization:
  1. Parse text file
  2. Symbol table

First Pass:
  1. Read all commands, labels and updating the symbol table

Main Loop:
  1. Get the next Assembly Language Command and parse it
  2. For A-commands: Translate symbols to binary address
  3. For C-commands: get code for each part and put together
  Output the resulting machine language command
"""
variableSpot = 16       # memory address for user variables
fileName = sys.argv[1]  # assembler file name

class SymbolTables():
    """
    class containing symbol tables
    """

    # dictionary for standard codes
    table = {
        "SP": 0,
        "LCL": 1,
        "ARG": 2,
        "THIS": 3,
        "THAT": 4,
        "SCREEN": 16384,
        "KBD": 24576,
    }

    # dictionaries to store translations of c-instructions
    comp = {
        "0": "0101010",
        "1": "0111111",
        "-1": "0111010",
        "D": "0001100",
        "A": "0110000",
        "!D": "0001101",
        "!A": "0110001",
        "-D": "0001111",
        "-A": "0110011",
        "D+1": "0011111",
        "A+1": "0110111",
        "D-1": "0001110",
        "A-1": "0110010",
        "D+A": "0000010",
        "D-A": "0010011",
        "A-D": "0000111",
        "D&A": "0000000",
        "D|A": "0010101",
        "M": "1110000",
        "!M": "1110001",
        "-M": "1110011",
        "M+1": "1110111",
        "M-1": "1110010",
        "D+M": "1000010",
        "D-M": "1010011",
        "M-D": "1000111",
        "D&M": "1000000",
        "D|M": "1010101"
    }
    dest = {
        "null": "000",
        "M": "001",
        "D": "010",
        "A": "011",
        "MD": "100",
        "AM": "101",
        "AD": "110",
        "AMD": "111",
    }
    jump = {
        "null": "000",
        "JGT": "001",
        "JEQ": "010",
        "JGE": "011",
        "JLT": "100",
        "JNE": "101",
        "JLE": "110",
        "JMP": "111",
        "R0": 0,
        "R1": 1,
        "R2": 2,
        "R3": 3,
        "R4": 4,
        "R5": 5,
        "R6": 6,
        "R7": 7,
        "R8": 8,
        "R9": 9,
        "R10": 10,
        "R11": 11,
        "R12": 12,
        "R13": 13,
        "R14": 14,
        "R15": 15,
    }

    pass

class Parser():
    """
    Parses the input assembler file
    """

    def removeWhitespace(line):
        character = line[0]
        if character == "/" or character == "\n":
            return ""
        elif character == " ":
            return Parser.removeWhitespace(line[1:])
        else:
            return character + Parser.removeWhitespace(line[1:])

    def consistency(line):
        line = line[:-1]
        if "=" not in line:
            line = "null=" + line
        if ";" not in line:
            line = line + ";null"
        return line

    pass

class CodeOps():
    """
    Different code operations
    """

    def addUserVariable(label):
        global variableSpot
        SymbolTables.table[label] = variableSpot
        variableSpot += 1
        return SymbolTables.table[label]

    def aInstruction(line):
        if line[1].isalpha():
            label = line[1:-1]
            a = SymbolTables.table.get(label, -1)
            if a == -1:
                a = CodeOps.addUserVariable(label)
        else:
            a = int(line[1:])
        binary = bin(a)[2:].zfill(16)
        return binary

    def cInstruction(line):
        line = Parser.consistency(line)
        temp = line.split("=")
        destination = SymbolTables.dest.get(temp[0], "destination failure")
        temp = temp[1].split(";")
        computation = SymbolTables.comp.get(temp[0], "computation failure")
        jumping = SymbolTables.jump.get(temp[1], "jump failure")
        return computation, destination, jumping

    def translation(line):
        if line[0] == "@":
            return CodeOps.aInstruction(line)
        else:
            bins = CodeOps.cInstruction(line)
            return "111" + bins[0] + bins[1] + bins[2]
    pass

def main():
    assembly = open(fileName + ".asm")
    temp = open(fileName + ".tmp", "w")

    lineVal = 0
    for line in assembly:
        noWhite = Parser.removeWhitespace(line)
        if noWhite != "":
            if noWhite[0] == "(":
                label = noWhite[1:-1]
                SymbolTables.table[label] = lineVal
                noWhite = ""
            else:
                lineVal += 1
                temp.write(noWhite + "\n")
    assembly.close()

    temp = open(fileName + ".tmp")
    hack = open(fileName + ".hack", "w")
    for line in temp:
        translated = CodeOps.translation(line)
        hack.write(translated + "\n")
    temp.close()
    hack.close()
    os.remove(fileName + ".tmp")

""" running the program """
main()


#LISTA4 - FUNCOES
# 4) Receba 4 notas e seus respectivos pesos e mostre a media

def media (notas,peso):
    soma = 0
    somaNotas = 0
    i = 0
    while i < len(notas):
        soma += (notas[i] * (peso[i]))
        somaNotas += peso[i]
        i+=1
    return (soma/somaNotas)

peso = []
notas = []
i = 0
while i < 4:
    notas.append(float(input(f'Digite a nota {i+1}: ')))
    peso.append(float(input(f'Digite o peso {i+1}: ')))
    i+=1
print(f'A média foi de {media(notas, peso)}')


import os
import sys
import time
import torch
import argparse
import numpy as np
from tqdm import tqdm

def cald(y, x, idx):
    '''calculate ay/a(x_idx)'''
    return torch.autograd.grad(y, x, grad_outputs=torch.ones_like(y), create_graph=True)[0][:,idx-1].reshape(-1, 1)

def autograd(y:torch.tensor, x:torch.tensor, order:int=10):
    '''calculate the n-order partial derivatives'''
    v = {0:[y]}
    p = x.numel()
    bar = order if p==1 else int((1-p**(order+1))/(1-p))-1
    pbar = tqdm(total=bar, desc='Calculating derivatives with Autograd', leave=True, file=sys.stdout)
    for k in range(1, order+1):
        v[k] = []
        for i in range(len(v[k-1])):    # eg. when p=3 and k=3, v[k-1] = [11, 12, 13, 21, 22, 23, ..., 33]
            for j in range(1, p+1):     # eg. when p=3 and k=3, v[k] = [111, 112, 113, 121, 122, 123, ..., 333]
                v[k].append(cald(v[k-1][i], x, j))
                pbar.set_postfix_str("order:{}, idx:{}/{}".format(k, i*p+j, len(v[k-1]*p)))
                pbar.update(1)
    return v

def main(net_path, order, point, show):
    net_dir = os.path.dirname(net_path)
    npy_dir = os.path.join(net_dir, 'npy')
    if not os.path.exists(npy_dir):
        os.mkdir(npy_dir)
    npy_path = os.path.join(npy_dir, f'Autograd_{point}_{order}').replace(" ","")

    # Forward propagation
    x = torch.tensor(point, dtype=torch.float, requires_grad=True)
    net = torch.load(net_path).eval()
    print(net)
    y = net(x)

    # Calculate all the derivatives
    time_start = time.time()
    v = autograd(y=y, x=x, order=order)
    time_auto = time.time()-time_start
    print('It takes {:.4f}s to calculate all the {}-order derivatives with Autograd.'.format(time_auto, order))
    v = {k: [float(d.detach().numpy()) for d in v[k]] for k in v.keys()}
    print(f'The number of derivatives of each order are: {str([len(v[k]) for k in v.keys()])}')
    np.save(npy_path, v)

    # Record the time cost
    time_dir = os.path.join(npy_dir, 'time')
    if not os.path.exists(time_dir):
        os.mkdir(time_dir)
    f = open(os.path.join(time_dir,f'Autograd_{point}_{order}.txt'), 'w+')
    f.write(f'Method: Autograd \nReference Point: {point} \nOrder: {order} \nTime Cost: {time_auto}s')
    f.close()

    # show results
    if show:
        for key in range(max(v.keys())+1):
            print(f'{key} order derivatives: {v[key]}')

if __name__=="__main__":
    parser = argparse.ArgumentParser(description='Taylor expansion')
    parser.add_argument('-d', type=str, default='outputs/test2d/net.pt', help='network path')
    parser.add_argument('-o', type=int, default=8, help='expansion order')
    parser.add_argument('-p', type=lambda s: [[float(item) if item[0]!='n' else -float(item[1:]) for item in s.split(',')]], default='0, 0', help='reference input')
    parser.add_argument('-s', action='store_true', help='show results')
    args = parser.parse_args()

    main(net_path=args.d, order=args.o, point=args.p, show=args.s)


class TheisWell:
    """Theis (1935) radial flow solution.

    The default TheisWell object uses the default Aq2dConf and WellGrid
    classes. The WellGrid class includes the default Well class. Methods
    include radius of influence .ri, transient drawdown at a point .dd and
    grid-contoured drawdown at specified time .dd_grid.

    Attributes:
        aq (obj) : Aq2dConf aquifer object.
        grd (obj) : WellGrid object.
        qf (float) : Fraction of pumped volume used for calculating radius of
            influence (default 0.99).

    """
    from pygaf.aquifers import Aq2dConf
    from pygaf.grids import WellGrid
    def __init__(self):
        self.aq = self.Aq2dConf()
        self.grid = self.WellGrid()
        self.well = self.grid.well
        self.qf = 0.99
        return

    @property
    def qfp(self):
        """float : Fraction of pumped volume."""
        return self.qf

    def info(self):
        """Print the solution information."""
        print('METHOD REFERENCE')
        print('----------------')
        print(
        'Theis C. V. (1935) - The relation between the lowering of the' +
        '\npiezometric surface and the rate and duration of discharge of a' +
        '\nwell using ground-water storage.'
        )
        print('\nConceptual Model:')
        print('- Infinite, confined, uniform and homogeneous aquifer.')
        print('- Radial groundwater flow.')
        print('- Steady state and fully penetrating well.')
        print('- No groundwater recharge.')
        print()

    def rinf(self, T, S, t, qf):
        """Radius of influence."""
        from numpy import sqrt, log
        return sqrt(-4.0 * T * t * log(1-qf) / S)

    def disp(self, r, S, T, t, Q):
        """Drawdown displacement."""
        from numpy import pi
        from scipy.special import expn
        u = (r**2) * S / (4.0 * T * t)
        W = expn(1, u) # Well Function
        return Q * W / (4.0 * pi * T)

    def ri(self, t=[1.0], q=-1000, plot=True, csv='', xlsx=''):
        """Calculate radius of influence at specified times.

        Radius of influence is defined as the radius from within which a
        specified faction qf of the pumped volume has been drawn. The default
        value for qf is 0.99, corresponding to the radius from which 99% of the
        pumped volume has been drawn.

        Time for calculating ri are provided in a list. A results graph is
        displayed as default and can be suppressed by setting plot=False. A
        pandas dataframe is returned with time as the row index and ri as
        a column.

        Results can be exported to csv and Excel files by setting non-blank
        filename strings for the .csv and .xlsx attributes. Filenames can be
        supplied with or without file extentions, which are added if ommitted.

        Args:
            t (float) : List of times to evaluate radius of influence
                (default [1.0]).
            plot (bool) : Display a plot of results (default True).
            csv (str) : Full filepath for export of results to csv file;
                results are exported if the string is not empty (default '').
            xlsx (str) : Full filepath for export of result to xlsx file;
                results are exported if the string is not empty (default '').

        Returns:
            Results in pandas dataframe.

        """
        import pandas
        self.well.q = q
        # Checks
        if self.qfp < 0 or self.qfp > 1:
            print('Error! The value of qf must be between 0 and 1.')
            return
        if min(t) <= 0:
            print('Error! All times must be greater than 0.')
            return
        # Radius of influence
        ri = []
        for tim in t:
            ri.append(self.rinf(self.aq.T, self.aq.S, tim, self.qfp))
        d = {'Time':t, 'ri':ri}
        df = pandas.DataFrame(data=d)
        df.set_index('Time', inplace=True)
        # Results plot
        if plot:
            import matplotlib.pyplot as plt
            df.plot(grid=True, marker='.', lw=3, alpha=0.5, ylabel='Radius')
            plt.xlim(0, None)
            plt.ylim(0, None)
            plt.title('Radius of Influence\n' +
                'T = ' + str(self.aq.T) +
                ', S = ' + str(self.aq.S) +
                ', q =' + str(self.well.q) +
                ', qf = ' + str(self.qfp)
                )
            plt.show()
        # Export result to csv
        if csv != '':
            if csv.split('.') != 'csv':
                csv = csv + '.csv'
            df.to_csv(csv)
            print('Results exported to:', csv)
        # Export result to Excel
        if xlsx != '':
            if xlsx.split('.') != 'xlsx':
                xlsx = xlsx + '.xlsx'
            df.to_excel(xlsx, sheet_name='ri')
            print('Results exported to:', xlsx)
        return df

    def dd(self, t=[1], r=[1], q=-1000.0, plot=True, csv='', xlsx=''):
        """Evaluate drawdown at specified radii and times.

        Evaluate drawdown at each radius and time specified in the lists t
        and r and for well rate q. Defaults are t=[1.0], r=[1.0] and
        q=-1000.0. A drawdown graph is displayed as default and can be
        suppressed by setting plot=False.

        A pandas dataframe of drawdown values is returned with time as the row
        index and radius values as the column headers. Results can be exported
        to csv and Excel files by setting non-blank filename strings for the
        .csv and .xlsx attributes. Filenames can be supplied with or without
        file extentions, which are added if ommitted.

        Args:
            t (float) : List of times to evaluate drawdown (default [1.0]).
            r (float) : List of radii to evaluate drawdown (default [1.0]).
            plot (bool) : Display a plot of results (default True).
            csv (str) : Full filepath for export of results to csv file;
                results are exported if the string is not empty (default '').
            xlsx (str) : Full filepath for export of result to xlsx file;
                results are exported if the string is not empty (default '').

        Returns:
            Results in a pandas dataframe.

        """
        import pandas
        self.well.q = q
        # Checks
        if min(t) <= 0 or min(r) <= 0:
            print('Error! All times and radii must be greater than 0.')
            return
        # Calculate drawdown
        d = {'Time':t}
        df = pandas.DataFrame(data=d)
        df.set_index('Time', inplace=True)
        for rad in r:
            drawdown = []
            for tim in t:
                dd = self.disp(rad, self.aq.S, self.aq.T, tim, self.well.q)
                drawdown.append(dd)
            df['r' + str(rad)] = drawdown
        # Plot results
        if plot:
            import matplotlib.pyplot as plt
            df.plot(
                grid=True, marker='.', lw=3, alpha=0.5, ylabel='Displacement'
                )
            plt.title('Drawdown\n' +
                'T = ' + str(self.aq.T) +
                ', S = ' + str(self.aq.S) +
                ', q = ' + str(self.well.q)
                )
            plt.legend(
                r, ncol=5, mode='expand', loc='upper left',
                bbox_to_anchor=(0,-0.1, 1, -0.1)
            )
            plt.show()
        # Export results
        if csv != '':
            if csv.split('.') != 'csv':
                csv = csv + '.csv'
            df.to_csv(csv)
            print('Results exported to:', csv)
        if xlsx != '':
            if xlsx.split('.') != 'xlsx':
                xlsx = xlsx + '.xlsx'
            df.to_excel(xlsx, sheet_name='ri')
            print('Results exported to:', xlsx)
        return df

    def dd_grid(self, t=1.0, q=-1000.0, plot=True, local=False, csv='',
    xlsx=''):
        """Evaluate drawdown on a regular grid.

        Evaluate drawdown on a grid of points at specified time and well rate.
        Default values are t=1.0 and q=-1000. Unless otherwise specified, a
        default WellGrid object is used; it can be accessed and adjusted via
        the .grid.gr (grid radius) and .grid.gd (grid density) attributes.

        Results are returned in a Pandas dataframe with column x-coord, y-coord
        and drawdown value. A drawdown graph is displayed as default and can
        be suppressed by setting plot=False.

        Args:
            t (float) : Time of drawdown (default 1.0).
            plot (bool) : Display a plot of results (default True).
            local (bool) : Display the results in 'local' coordinates with the
                well at coordinates 0.0, 0.0 (Default False).
            csv (str) : Full filepath for export of results to csv file;
                results are exported if the string is not empty (default '').
            xlsx (str) : Full filepath for export of result to xlsx file;
                results are exported if the string is not empty (default '').

        Returns:
            Results in a pandas dataframe.

        """
        import matplotlib.pyplot as plt
        import pandas
        self.well.q = q
        # Set coordinates
        if local:
            x, y = list(self.grid.pts.locx), list(self.grid.pts.locy)
            wx, wy = 0, 0
            plot_title = 'Drawdown at radius < ' + str(self.grid.gr) +\
            ' and t = ' + str(t) + '\n(local coordinates)'
        else:
            x, y = list(self.grid.pts.worldx), list(self.grid.pts.worldy)
            wx, wy = self.grid.well.x, self.grid.well.y
            plot_title = 'Drawdown at radius < ' + str(self.grid.gr) +\
            ' and t = ' + str(t) + '\n(world coordinates)'
        # Calculate drawdown
        radius, drawdown = [], []
        for i in range(self.grid.npts):
            r = self.grid.pts.rad[i]
            if r <= self.grid.well.r:
                r = self.grid.pts.rad[i-1]
            dd = self.disp(r, self.aq.S, self.aq.T, t, self.well.q)
            radius.append(r)
            drawdown.append(dd)
        # Plot results
        mid_row = int(self.grid.grdim/2)
        if plot:
            cm = plt.cm.get_cmap('Blues').reversed()
            fig, (ax1, ax2) = plt.subplots(
            2, 1, gridspec_kw={'height_ratios': [4, 1]}, figsize=(6, 7.6)
            )
            fig.suptitle(plot_title, fontsize=14)
            fig.suptitle(plot_title, fontsize=14)
            ax1.tricontourf(x, y, drawdown, cmap=cm)
            cs = ax1.tricontour(
                x, y, drawdown, linewidths=0.25, colors=['black']
            )
            ax1.clabel(cs, inline=1, fontsize=10)
            ax1.plot(wx, wy, '.', c='red')
            ax1.set_title('Displacement Contours')
            ax1.grid(True)
            ax1.axis('equal')
            ax2.plot(
                x[self.grid.grdim*(mid_row-1):self.grid.grdim*mid_row],
                drawdown[self.grid.grdim*(mid_row-1):self.grid.grdim*mid_row],
                '.-', lw=3, alpha=0.5
            )
            ax2.set_title('Radial Displacement')
            ax2.grid(True)
            plt.show()
            plt.close()
        # Export result
        df = pandas.DataFrame()
        df['x'] = x
        df['y'] = y
        df['radius'] = radius
        df['drawdown'] = drawdown
        if csv != '':
            if csv.split('.') != 'csv':
                csv = csv + '.csv'
            df.to_csv(csv, index=False)
            print('Results exported to:', csv)
        if xlsx != '':
            if xlsx.split('.') != 'xlsx':
                xlsx = xlsx + '.xlsx'
            df.to_excel(xlsx, sheet_name='drawdown', index=False)
            print('Results exported to:', xlsx)
        return df


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

# Trader Joe's web site is designed in a way that information of a store can be find by
#first determining the state where the store is located. Thus, [list of states in which
#Trader Joe's located](https://locations.traderjoes.com/) is welcomed us. Then, one needs
#to determine [the city](https://locations.traderjoes.com/ca/) within a specific state
#where the store is located so that one can see [the list of all the stores](https://locations.traderjoes.com/ca/los-angeles/)
#within the selected city.

# In general it can be summarized as: State(e.g. Califronia)-> City(e.g. Los Angles)-> Store(e.g. Trader Joe's Hollywood). Hence, I planned my scraping strategy accordingly: define 3 main specific functions that makes the transition between each parts possible. Luckly, their site has static content which can be easily scraped by utilizing [the Beautiful Soup module.](https://www.crummy.com/software/BeautifulSoup/bs4/doc/).

#libraries that you would need
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import requests #makes the http request, necessary for geocoding
from bs4 import BeautifulSoup #module for static web site scraping
import json #reads and downloads json formats from the website
from geopy.geocoders import Nominatim #geocoding
from geopy.exc import GeocoderTimedOut
from geopy.extra.rate_limiter import RateLimiter
import re #string manipulation
import time #sleep Zzzz.
import folium #mapping
import geopandas #geospatial data
import pickle #to pickle (ssaving with very low sizes)
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler

#parses all the state links in the first page where list of all the states with Trader Joe's are listed.
def parse_statelinks(states):

    URL_state = states.find('a')['href']
    page_state = requests.get(URL_state)

    soup_state = BeautifulSoup(page_state.content, "html.parser")
    results_state = soup_state.find(id="contentbegin")
    cities = results_state.find_all("div", class_="itemlist")

    return cities

#Makes the transition from the selected state, and parses (returns) the cities with Trader Joe's within
def states2cities(soup):
    cities = []
    results = soup.find(id="contentbegin")
    states = results.find_all("div", class_="itemlist")
    cities.extend([parse_statelinks(states[i]) for i in range(len(states))])

    return cities

#Makes the transition from the selected city, and parses the stores (Trader Joe's) within,
#returns list of dictionaries (json format) that contains address information about each store.

def city2stores(city):
    store_list = []

    URL_city = city.find('a')['href']
    page_city = requests.get(URL_city)
    soup_city = BeautifulSoup(page_city.text, "html.parser")

    results_city = soup_city.find(id="contentbegin")
    city_results = results_city.find_all("script", {"type":"application/ld+json"})

    for i in range(len(city_results)):
        store_list.append(json.loads("".join(results_city.find_all("script", {"type":"application/ld+json"})[i])))

    return store_list

#Geocode the given store addresses from address string to shapely point object.

def city_geocode(store):

    PostalAddress = store['address']

    #create the full adress of the store whom you want to geocode, fix the abbrivation (e.g. change Rd. to Road)
    street_address = street_address_fix(PostalAddress['streetAddress'])
    address = street_address.split(',')[0] + ', ' + PostalAddress['addressLocality'] + ', ' + PostalAddress['addressRegion'] + ', '+ PostalAddress['addressCountry']

    #geocode using OSM Nominatim
    rnd = np.random.randint(1, 20)
    time.sleep(rnd)

    #first try to geocode the given address as it is written
    location = locator.geocode(address)

    #if doesn't work tweak the given address: remove any numbers,except the postal code, in the main part of the given address
    if location is None:
        street_address = re.sub(r'[0-9]+', '', street_address.split(',')[0])
        address = street_address + ', ' + PostalAddress['addressLocality'] + ', ' + PostalAddress['addressRegion'] + ', '+ PostalAddress['addressCountry']
        location = locator.geocode(address)

    #if doesn't work tweak the given address: get rid of address related verbs as Road, Street etc. additionally
    if location is None:
        street_address = street_abbv_remove(street_address)
        address = street_address + ', ' + PostalAddress['addressLocality'] + ', ' + PostalAddress['addressRegion'] + ', '+ PostalAddress['addressCountry']
        location = locator.geocode(address)

    #if it doesnt work just take the center of the city as the last resort.
    if location is None:
        address = PostalAddress['addressLocality'] + ', ' + PostalAddress['addressRegion'] + ', '+ PostalAddress['addressCountry'] + ', ' + PostalAddress['postalCode']
        location = locator.geocode(address)

    try:
        return [dict({'address': location.address, 'lon': location.longitude, 'lat' : location.latitude , 'point' : location.point})]
    except:
        return dict({})

#function that helps to make street related abbrivation changes.
def street_address_fix(StreetAddress):
    replacements = {'RD': 'ROAD',
                    'RD.': 'ROAD',
                    'CIR': 'CIRCLE',
                    'CIR.': 'CIRCLE',
                    'DR': 'DRIVE',
                    'DR.': 'DRIVE',
                    'LN': 'LANE',
                    'LN.': 'LANE',
                    'CT': 'COURT',
                    'CT.': 'COURT',
                    'PL': 'PLACE',
                    'PL.': 'PLACE',
                    'ST': 'STREET',
                    'ST.': 'STREET',
                    'BLVD': 'BOULEVARD',
                    'BLVD.': 'BOULEVARD'}

    StreetAddress = StreetAddress.upper().split()

    for i in range(len(StreetAddress)):
        if StreetAddress[i] in replacements.keys():
            StreetAddress[i] = replacements[StreetAddress[i]]

    StreetAddress = ' '.join(StreetAddress)
    StreetAddress = ' '.join( [s for s in StreetAddress.split() if len(s)>2] )

    return StreetAddress

def street_abbv_remove(StreetAddress):
    replacements = {'ROAD': ' ',
                    'CIRCLE': ' ',
                    'DRIVE': ' ',
                    'LANE': ' ',
                    'COURT': ' ',
                    'PLACE': ' ',
                    'STREET': ' ',
                    'BOULEVARD': ' '}

    StreetAddress = StreetAddress.upper().split()

    for i in range(len(StreetAddress)):
        if StreetAddress[i] in replacements.keys():
            StreetAddress[i] = replacements[StreetAddress[i]]

    StreetAddress = ' '.join(StreetAddress)
    StreetAddress = ' '.join( [s for s in StreetAddress.split() if len(s)>2] )

    return StreetAddress

#The main part, for loop and the functions within returns a list of dictionaries, one needs to convert them
#into dataframe in order to get something like the one that I shared in the dataset section.

URL = "https://locations.traderjoes.com/"

page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")
geopy.geocoders.options.default_timeout = 25 #set high to not get Max retries exceeded type of error
locator = Nominatim(user_agent="personal_project")

stores = []
states = states2cities(soup) #lists of all the states url where store related informations exist

for j in range(len(states)): #state -> city
    for i in range(len(states[j])):   #city -> store
        tmp = city2stores(states[j][i])
        for k in range(len(tmp)):  # ..stores..
            a = city_geocode(tmp[k])
            stores.extend(a)

#with open('stores.pickle', 'wb') as handle:
#pickle.dump(stores, handle, protocol=pickle.HIGHEST_PROTOCOL)

#Small fix with one address

stores_data = pd.DataFrame(stores)

address = stores_data[stores_data.lon==stores_data.lon.max()].address
locator = Nominatim(user_agent="personal_project")

stores[384]['address'] = locator.geocode('77 Boston Turnpike, Shrewsbury, MA,USA').address
stores[384]['lon'] =locator.geocode('77 Boston Turnpike, Shrewsbury, MA,USA').longitude
stores[384]['lat'] = locator.geocode('77 Boston Turnpike, Shrewsbury, MA,USA').latitude
stores[384]['point'] = locator.geocode('77 Boston Turnpike, Shrewsbury, MA,USA').point

#Ground Truths of Warehouse addresses
warehouse_address = ['Nazareth, PA 18064, USA', 'Suwanee, GA 30024, USA', '30 Commerce Blvd, Middleborough, MA , USA',
                    'Minooka 60447, IL, USA','5111 Bear Creek Ct, Irving, TX 75061, USA','2388 Mason Ave, Daytona Beach, FL 32117, USA',
                    '200 Phoenix Xing, Bloomfield, CT 06002, USA','10288 Calabash Ave, Fontana, CA 92335, USA','2121 Boeing Way, Stockton, CA 95206, USA',
                    '3707 Hogum Bay Rd NE, Lacey, WA 9851','4681 Edison Ave, Chino, CA 91710, USA','10401 W Van Buren St, Tolleson, AZ 85353, USA']

def real_warehouse_address(rwa):

    adrs = locator.geocode(rwa)
    return [dict({'address': adrs.address, 'lon': adrs.longitude, 'lat' : adrs.latitude , 'point' : adrs.point})]

rwa = []
for adrs in warehouse_address:
    rwa.append(real_warehouse_address(adrs))

### Analysis Part

coordinates = stores_data[['lon','lat']].values
#standardize? or Not
#scaler = StandardScaler()
#scaled_coordinates = scaler.fit_transform(coordinates)

kmeans = KMeans(
    init="k-means++",
    n_clusters=19,
    n_init=10,
    max_iter=100, random_state=17)
kmeans.fit(coordinates)

warehouse = pd.DataFrame(kmeans.cluster_centers_, columns = ['lon','lat'])
labels = pd.DataFrame(kmeans.labels_, columns= ['labels'])
stores_data = pd.concat([stores_data,labels], axis = 1 )
stores_data['labels'] = stores_data['labels'].astype(str)

#Mapping with folium part
m = folium.Map(location=[37, -102], zoom_start=4)

stores_data.apply(lambda row:folium.CircleMarker(location=[row["lat"], row["lon"]],
                                              radius=1.5, color='orange')
                                             .add_to(m), axis=1)

for w in range(len(warehouse)):

    folium.CircleMarker(
        location=[warehouse.iloc[w]['lat'],warehouse.iloc[w]['lon']],
        radius=5,
        popup="Laurelhurst Park",
        color="darkred",
        fill=True,
        fill_color="darkred",
    ).add_to(m)

for j in stores_data.labels.unique():
    temp = stores_data[stores_data.labels==j]
    for i in range(len(temp)):
        folium.PolyLine([(warehouse.loc[int(j)].lat,warehouse.loc[int(j)].lon),(temp.iloc[i].lat,temp.iloc[i].lon)],
                        color='black',
                        weight=1,
                        opacity=0.6).add_to(m)

for k in range(len(rwa)):

    folium.CircleMarker(
        location=[rwa[k][0]['lat'],rwa[k][0]['lon']],
        radius=5,
        color="green",
        fill=True,
        fill_color="green",
        opacity = 1
    ).add_to(m)

m

# In[ ]:
#stores_data.groupby(['labels']).address.count()


from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

import collections
import shlex

class CommandStringParser(object):
  """Object which builds and returns all metadata about string and flags.

  Attributes:
    command_node: calliope._CommandCommon, The command object that has access
    to argparse.
    command_parser: The argparse object for command_node.
    example_string: The most recently parsed example command string.
    example_metadata: The metadata gotten from example_string.
  """

  def __init__(self, command_node):
    self.command_node = command_node
    self.command_parser = self.command_node._parser

  def parse(self, example_command_string):
    """Gets relevant metadata from an example command string.

    Args:
      example_command_string: The example command string to be parsed.

    Returns:
      An ExampleCommandMetadata object containing the relevant metadata or None
      if there was a parsing error.
    """
    self.example_string = example_command_string
    self.example_metadata = ExampleCommandMetadata()

    parse_input = self.get_parse_args(example_command_string)
    if not parse_input:
      return

    metadata = self.command_parser.parse_args(parse_input, raise_error=True)
    filtered = metadata.GetSpecifiedArgsDict()

    for key, value in filtered.items():
      tmp_value = getattr(metadata, key, None)
      if isinstance(tmp_value, list):
        self.parse_list(tmp_value, key, value)

      elif isinstance(tmp_value, collections.OrderedDict):
        self.parse_dict(tmp_value, key, value)

      else:
        self.parse_others(tmp_value, key, value)

    return self.example_metadata

  def parse_dict(self, dict_arg, key, value, prev_stop=0, count=None):
    """Gets metadata from an example command string for a simple dict-type arg.

    It updates the already existing ExampleCommandMetadata object of the example
    string with the metadata.

    Args:
      dict_arg: The dictionary-type argument to collect metadata about.
      key: The name of the argument's attribute in the example string's
      namespace.
      value: The actual input representing the flag/argument in the example
      string (e.g --zone, --shielded-secure-boot).
      prev_stop: Optional. The index in the example string the flag was last
      seen.
      count: Optional. The number of times the flag has been seen in the example
      string.

    Returns:
      The index in the example string where parsing stopped if the argument is
      specified multiple times.
    """
    dict_list = list(dict_arg.items())
    first_pair = dict_list[0]
    last_pair = dict_list[-1]

    start_search = self.get_start_search(value, prev_stop)
    start_index = self.example_string.index(str(first_pair[0]), start_search)

    next_start = self.example_string.find('--', start_index)
    if next_start < start_search: next_start = len(self.example_string)
    last_value = str(last_pair[1])
    last_value_start = self.example_string.rfind(last_value, start_search,
                                                 next_start)
    stop_index = last_value_start + len(last_value) - 1

    arg_value = self.example_string[start_index:stop_index+1]
    # If count, dict_arg is part of a list and arg's action is append.
    # Otherwise, it's a normal dictionary arg.
    scope = '{key}_{count}'.format(key=key, count=count) if count else key
    arg_metadata = ArgumentMetadata(key, arg_value, scope,
                                    start_index, stop_index)
    self.example_metadata.add_arg_metadata(arg_metadata)

    if count:
      return arg_metadata.stop_index + 1

  def parse_list(self, list_arg, key, value, prev_stop=0, count=None):
    """Gets metadata from an example command string for a list-type argument.

    It updates the already existing ExampleCommandMetadata object of the example
    string with the metadata.

    Args:
      list_arg: The list-type argument to collect metadata about.
      key: The name of the argument's attribute in the example string's
      namespace.
      value: The actual input representing the flag/argument in the example
      string (e.g --zone, --shielded-secure-boot).
      prev_stop: Optional. The index in the example string the flag was last
      seen.
      count: Optional. The number of times the flag has been seen in the example
      string.

    Returns:
      The index in the example string where parsing stopped if the argument is
      specified multiple times.
    """
    if isinstance(list_arg[0], collections.OrderedDict):
      self.parse_nested_list(list_arg, key, value)

    elif isinstance(list_arg[0], list):
      self.parse_nested_list(list_arg, key, value)

    else:
      # Assumes list elements are either strings or numbers; excludes booleans.

      start_search = self.get_start_search(value, prev_stop)
      start_index = self.example_string.index(str(list_arg[0]), start_search)

      next_start = self.example_string.find('--', start_index)
      if next_start < start_search or next_start == -1:
        next_start = len(self.example_string)
      last_value = str(list_arg[-1])
      last_value_start = self.example_string.rfind(last_value, start_search,
                                                   next_start)
      stop_index = last_value_start + len(last_value) -1
      arg_value = self.example_string[start_index:stop_index+1]
      scope = '{key}_{count}'.format(key=key, count=count) if count else key
      # count implies list_arg is an inner list which implies action is append.
      # no count implies simple list which implies action is update.

      arg_metadata = ArgumentMetadata(key, arg_value, scope, start_index,
                                      stop_index)
      self.example_metadata.add_arg_metadata(arg_metadata)

      if count:
        return arg_metadata.stop_index + 1

  def parse_nested_list(self, list_arg, key, value):
    """Gets metadata from an example command string for nested list arguments.

    This is a helper function for parse_list().

    It updates the already existing ExampleCommandMetadata object of the example
    string with the metadata.

    Args:
      list_arg: The list-type argument to collect metadata about.
      key: The name of the argument's attribute in the example string's
      namespace.
      value: The actual input representing the flag/argument in the example
      string (e.g --zone, --shielded-secure-boot).
    """
    flag_count = self.example_string.count(value)
    if isinstance(list_arg[0], collections.OrderedDict):
      # Flag specified once in example string and occurs multiple times in
      # namespace implies flag was specified with custom delimiter.
      if flag_count == 1 and len(list_arg) > 1:
        first_dict = list(list_arg[0].items())
        last_dict = list(list_arg[-1].items())
        start = first_dict[0][0]  # first key of first dictionary
        stop = last_dict[-1][1]  # last key of last dictionary

        start_search = self.get_start_search(value)
        start_index = self.example_string.index(str(start), start_search)
        next_start = self.example_string.find('--', start_index)
        if next_start < start_search: next_start = len(self.example_string)
        stop_index = (self.example_string.rfind(str(stop), start_search,
                                                next_start) +
                      len(str(stop)) - 1)

        arg_value = self.example_string[start_index:stop_index+1]
        arg_metadata = ArgumentMetadata(key, arg_value, key, start_index,
                                        stop_index)
        self.example_metadata.add_arg_metadata(arg_metadata)

      # Flag specified multiple times in both example string namespace implies
      # flag's action is append.
      else:
        prev_stop = 0
        for i in range(flag_count):
          val = list_arg[i]
          prev_stop = self.parse_dict(val, key, value, prev_stop, i+1)

    # Nested list(list_arg[0] is a list) implies action is append.
    else:
      prev_stop = 0
      for i in range(flag_count):
        val = list_arg[i]
        prev_stop = self.parse_list(val, key, value, prev_stop, i+1)

  def parse_others(self, other_arg, key, value):
    """Gets metadata from an example string for non list-type/dict-type args.

    It updates the already existing ExampleCommandMetadata object of the example
    string with the metadata.

    Args:
      other_arg: The non list-type and non dict-type argument to collect
      metadata about.
      key: The name of the argument's attribute in the example string's
      namespace.
      value: The actual input representing the flag/argument in the example
      string (e.g --zone, --shielded-secure-boot).

    """
    # Editable range excludes quotes and booleans.

    if not isinstance(other_arg, bool):
      start_search = self.get_start_search(value)
      start_index = self.example_string.find(str(other_arg), start_search)
      if start_index == -1:
        #  Enum type
        other_arg = self.get_enum_value(other_arg, start_search)

      start_index = self.example_string.index(str(other_arg), start_search)
      arg_metadata = ArgumentMetadata(key, other_arg, key, start_index,
                                      start_index + len(str(other_arg)) - 1)

      self.example_metadata.add_arg_metadata(arg_metadata)

  def get_enum_value(self, enum_value, prev_stop):
    """Gets the input value of an enum argument in the example string.

    This is a helper function for parse_others().

    Args:
      enum_value: The namespace value of the argument in question.
      prev_stop: The index in the example string where parsing stopped.

    Returns:
     The actual input in the example string representing the argument's value.
    """
    unparsed_string = self.example_string[prev_stop:]

    if isinstance(enum_value, str):
      #  Check for common variations(underscore/hypen; lower/upper)
      possible_versions = [enum_value.lower(), enum_value.upper(),
                           enum_value.replace('-', '_'),
                           enum_value.replace('_', '-')]
      for version in possible_versions:
        if version in unparsed_string:
          enum_value = version

    if str(enum_value) not in unparsed_string:
      unparsed_string = unparsed_string.strip()
      arg_end = unparsed_string.find(' --')
      # Keep last character if we are at the end of the example.
      arg_list = (unparsed_string[:arg_end].split('=') if arg_end != -1
                  else unparsed_string.split('='))

      enum_value = (' '.join(arg_list[1:]).strip() if len(arg_list) > 2 else
                    arg_list[-1].strip())
      # We'll strip the whitespace so it won't be part of the argument value.

    return enum_value

  def get_start_search(self, namespace_name, prev_stop=0):
    """Gets the position to begin searching for an argument's value.

    This is a helper function for all the parse functions aside from parse().

    Args:
      namespace_name: The value of the argument in namespace's specified_args
      dictionary. ('INSTANCE_NAMES:1', '--zone', etc)
      prev_stop: Optional. For recurring flags, where the flag was last
      seen.

    Returns:
      The index in the example string to begin searching.
    """
    # Start searching for value from --{flag_name} if it occurs once
    # or --{flag_name} after the last place the flag was seen if it
    # occurs multiple times.
    if prev_stop:
      start_search = (self.example_string.find(namespace_name, prev_stop) +
                      len(namespace_name))
    else:
      start_search = (self.example_string.find(namespace_name) +
                      len(namespace_name))

    # For positional arguments in list form, start searching from after the
    # command name.
    if start_search == -1:
      command_name = self.command_node.name.replace('_', '-')
      command_name_start = self.example_string.find(command_name)
      start_search = command_name_start + len(command_name)+ 1

    return start_search

  def get_parse_args(self, string):
    """Gets a list of arguments in a string.

    (Note: It assumes '$' is not included in the string)

    Args:
      string: The string in question.

    Returns:
      A list of the arguments from the string.
    """
    command_name = self.command_node.name.replace('_', '-')
    command_name_start = string.find(command_name)

    # Return None if command string is under a different command node.
    if command_name_start == -1:
      return

    # calculation includes the space after the command string.
    command_name_stop = command_name_start + len(command_name)+ 1
    args_list = shlex.split(string[command_name_stop:])

    return args_list

class ExampleCommandMetadata(object):
  """Encapsulates metadata about entire example command string.

  Attributes:
    argument_metadatas: A list containing the metadata for each argument in an
    example command string.
  """

  def __init__(self):
    self._argument_metadatas = []

  def add_arg_metadata(self, arg_metadata):
    """Adds an argument's metadata to comprehensive metadata list.

    Args:
      arg_metadata: The argument metadata to be added.
    """
    self._argument_metadatas.append(arg_metadata)

  def get_argument_metadatas(self):
    """Returns the metadata for an entire example command string."""
    return sorted(self._argument_metadatas, key=lambda x: x.stop_index)

  def __eq__(self, other):
    if isinstance(other, ExampleCommandMetadata):
      # sort both first
      our_data = sorted(self._argument_metadatas, key=lambda x: x.stop_index)
      other_data = sorted(other._argument_metadatas, key=lambda x: x.stop_index)

      if len(our_data) != len(other_data):
        return False

      for i in range(len(self._argument_metadatas)):
        if our_data[i] != other_data[i]:
          return False
      return True

    return False

  def __ne__(self, other):
    return self.__eq__(other)

  def __str__(self):
    metadatas = self.get_argument_metadatas()
    result = [str(data) for data in metadatas]
    return '[{result}]'.format(result=',  '.join(result))

class ArgumentMetadata(object):
  """Encapsulates metadata about a single argument.

  Attributes:
    arg_name: The name of the argument.
    arg_value: Value of the argument.
    scope: The scope of the argument.
    start_index: The  index where the argument starts in the command string.
    stop_index: The index where the argument stops in the command string.
  """

  def __init__(self, arg_name, arg_value, scope, start_index, stop_index):
    self.arg_name = arg_name
    self.arg_value = arg_value
    self.scope = scope
    self.start_index = start_index
    self.stop_index = stop_index

  def __str__(self):
    """Returns a human-readable representation of an argument's metadata."""
    return ('ArgumentMetadata(name={name},  value={value},  scope={scope},  '
            'start_index={start},  stop_index='
            '{stop})').format(name=self.arg_name, scope=self.scope,
                              value=self.arg_value, start=self.start_index,
                              stop=self.stop_index)

  def __eq__(self, other):
    if isinstance(other, ArgumentMetadata):
      return (self.arg_name == other.arg_name and
              self.arg_value == other.arg_value and
              self.scope == other.scope and
              self.start_index == other.start_index and
              self.stop_index == other.stop_index)

    return False

  def __ne__(self, other):
    return not self.__eq__(other)


from django.shortcuts import render
from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseForbidden
from django.views.decorators.csrf import csrf_exempt
from django.conf import settings

from linebot import LineBotApi, WebhookParser
from linebot.exceptions import InvalidSignatureError, LineBotApiError
from linebot.models import (
    MessageEvent,
    TextSendMessage,
    FlexSendMessage,
    ImageSendMessage,
    TemplateSendMessage,
    ButtonComponent,
    TextComponent,
    SeparatorComponent,
    BubbleContainer,
    BoxComponent,
    MessageAction)

import re
import numpy as np
import matplotlib.pyplot as plt

#function
from .dynamic_list_generator import favorite_list_generator #最愛清單function
from .datedo import datedo_list_generator  #對象工具列
from .carousel import carousel_list
from .report import draw,text_report
from .TextTemplate import instrution_content
from .connector import selectChattingObjectNameByUserLineId,addUser,addChattingObject,addRecord
from .partition import frequency,wordanalysis
#from .test import getAmountScore,getSpeedScore

#登入linebot 跟 imgur 需要的東西(from settings)
line_bot_api = LineBotApi(settings.LINE_CHANNEL_ACCESS_TOKEN)
parser = WebhookParser(settings.LINE_CHANNEL_SECRET)
imgur_client=settings.IMGUR_CLIENT_ID

######需要從後端得到的東西######
favorite_list=[] #最愛清單(from db)

values = [60,51,70,80,60,50] #各指標本次分數(from model)
values_a= [40,50,80,70,90,60] #各指標平均分數(from DB)

#本次分數
get_point_a=80.5
#上次分數
get_point_b=77.5

#把分數型態轉變為字串
point=str(get_point_a)
difference=str(get_point_a-get_point_b)

#話題主題前三名
topic=["Travel","Sports","Fashion"]

#主題1新聞連結
topic1=["https://www.youtube.com/channel/UC0C-w0YjGpqDXGB8IHb662A",
        "https://www.youtube.com/channel/UC0C-w0YjGpqDXGB8IHb662A",
        "https://www.youtube.com/channel/UC0C-w0YjGpqDXGB8IHb662A"]
#主題2新聞連結
topic2=["https://www.youtube.com/channel/UC0C-w0YjGpqDXGB8IHb662A",
        "https://www.youtube.com/channel/UC0C-w0YjGpqDXGB8IHb662A",
        "https://www.youtube.com/channel/UC0C-w0YjGpqDXGB8IHb662A"]
#主題3新聞連結
topic3=["https://www.youtube.com/channel/UC0C-w0YjGpqDXGB8IHb662A",
        "https://www.youtube.com/channel/UC0C-w0YjGpqDXGB8IHb662A",
        "https://www.youtube.com/channel/UC0C-w0YjGpqDXGB8IHb662A"]

@csrf_exempt
def callback(request):
    if request.method == 'POST':
        signature = request.META['HTTP_X_LINE_SIGNATURE']
        body = request.body.decode('utf-8')

        try:
            events = parser.parse(body, signature)  # 傳入的事件
            print(events)

        except InvalidSignatureError:
            return HttpResponseForbidden()
        except LineBotApiError:
            return HttpResponseBadRequest()

        global topic
        global favorite_list
        for event in events:

            if isinstance(event, MessageEvent):

                if event.message.type=='file': #############分析完後要回傳到資料庫##################
                    fname1=event.message.file_name[8:]
                    fname2=fname1[:-7]
                    user_id=event.source.user_id
                    file_path = f'/tmp/{user_id}:{fname2}'
                    message_content = line_bot_api.get_message_content(event.message.id)
                    with open(file_path, 'wb') as fd:
                        for chunk in message_content.iter_content():
                            fd.write(chunk)

                    addRecord(user_id,fname2)
                    line_bot_api.reply_message(event.reply_token, TextSendMessage(text="成功傳送對話："+fname2))

                elif re.match('頻率：', event.message.text):
                    file_path = f'/tmp/{event.source.user_id}:{event.message.text[3:]}'
                    b=frequency(file_path)
                    line_bot_api.reply_message(event.reply_token, TextSendMessage(text=b))

                elif re.match('情感：', event.message.text):
                    file_path = f'/tmp/{event.source.user_id}:{event.message.text[3:]}'
                    c=wordanalysis(file_path)
                    line_bot_api.reply_message(event.reply_token, TextSendMessage(text=c))

                elif event.message.text == '報表說明':
                    line_bot_api.reply_message(event.reply_token, TextSendMessage(text = instrution_content()))

#####第一次使用需要註冊帳號######
                elif event.message.text == '我要註冊':
                    user_id = event.source.user_id
                    addUser(user_id)
                    line_bot_api.reply_message(event.reply_token, TextSendMessage(text="註冊成功！"))

#####查看分析名單######
                elif event.message.text == '分析名單':
                    user_id = event.source.user_id
                    tup=selectChattingObjectNameByUserLineId(user_id)
                    favorite_list=[]
                    for i in range(len(tup)):
                        favorite_list.append(tup[i][0])
                    favorite_list_button=favorite_list_generator(favorite_list)
                    flex_message1=FlexSendMessage(alt_text='分析名單',contents=favorite_list_button)
                    line_bot_api.reply_message(event.reply_token, flex_message1)

                elif re.match("搜尋對象:", event.message.text):
                    date=event.message.text[5:] # 對象名稱(date)
                    flex_message2=FlexSendMessage(alt_text=date,contents=datedo_list_generator(date))
                    line_bot_api.reply_message(event.reply_token, flex_message2)

#####新增對象######
                elif re.match("新增對象：", event.message.text):
                    name=event.message.text[5:]
                    user_id = event.source.user_id
                    addChattingObject(name,user_id)
                    favorite_list.append(name)
                    line_bot_api.reply_message(event.reply_token, TextSendMessage(text="成功新增對象:"+event.message.text[5:]))

#####刪除對象#####還沒完成
                elif re.match("刪除對象:", event.message.text):
                    favorite_list.remove(event.message.text[5:])
                    line_bot_api.reply_message(event.reply_token, TextSendMessage(text="成功刪除對象:"+event.message.text[5:]))

                elif re.match("尋找話題:", event.message.text):
                    flex_message3=carousel_list(topic,topic1,topic2,topic3)
                    line_bot_api.reply_message(event.reply_token, flex_message3)

######從資料庫抓1.最新分數＆次新分數陣列 2.最新指標＆次新指標陣列 3.平均指標######還沒完成
                elif re.match("目前好感度:", event.message.text):
                    reply_arr=[]
                    t_content=text_report(values,values_a)
                    txt=TextSendMessage(text=t_content)
                    reply_arr.append(txt)
                    i_content = draw(imgur_client,values,values_a,point,difference)
                    img=ImageSendMessage(original_content_url=i_content,preview_image_url=i_content)
                    reply_arr.append(img)
                    line_bot_api.reply_message(event.reply_token, reply_arr)

                elif re.match("開始新增對象，請輸入「新增對象：對象名稱」", event.message.text):
                   pass

                elif re.match("請開始上傳對話", event.message.text):
                    pass
                else:
                    line_bot_api.reply_message(event.reply_token,TextSendMessage(text="請輸入有效指令"))

        return HttpResponse()
    else:
        return HttpResponseBadRequest()


# BSD 3-Clause License
#
# Copyright (c) 2017,
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the BSD 3-Clause License  (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://opensource.org/licenses/BSD-3-Clause
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import os
import sys
import torch
from collections import OrderedDict #定义有序字典
from conformer import Conformer
import onnx
import onnxruntime

def Conformer_tiny_patch16(pretrained=False, **kwargs):
    model = Conformer(patch_size=16, channel_ratio=1, embed_dim=384, depth=12,
                      num_heads=6, mlp_ratio=4, qkv_bias=True, **kwargs)
    if pretrained:
        raise NotImplementedError
    return model

def proc_nodes_module(checkpoint):
    new_state_dict = OrderedDict()
    for k, v in checkpoint.items():
        if "module." in k:
            name = k.replace("module.", "")
        else:
            name = k
        new_state_dict[name] = v
    return new_state_dict

def pth2onnx(input_file, output_file):
    model = Conformer_tiny_patch16()

    checkpoint = torch.load(input_file, map_location='cpu')
    checkpoint = proc_nodes_module(checkpoint)
    model.load_state_dict(checkpoint)

    # 调整模型为eval mode
    model.eval()
    # 输入节点名
    input_names = ["image"]
    # 输出节点名
    output_names = ["class"]
    dynamic_axes = {'image': {0: '-1'}, 'class': {0: '-1'}}
    dummy_input = torch.randn(1, 3, 224, 224) #修改输入shape
    # verbose=True，支持打印onnx节点和对应的PyTorch代码行
    torch.onnx.export(model, dummy_input, output_file, input_names = input_names,
                      dynamic_axes = dynamic_axes, output_names = output_names, opset_version=11, verbose=True)

if __name__ == '__main__':
    # model.load_state_dict({k.replace('module.',''):v for k,v in checkpoint['state_dict'].items()})
    try:
        # pth location
        xinput_file = sys.argv[1]
        # output onnx name
        xoutput_file = sys.argv[2]
    except IndexError:
        print("Stopped!")
        exit(1)

    if not (os.path.exists(xinput_file)):
        print("Please input pth location.")

    if not (os.path.exists(xoutput_file)):
        print("Please input onnx model name.")

    pth2onnx(xinput_file, xoutput_file)
    print("job success!")


import numpy as np
import numpy.testing as npt
import pytest
from pycudadecon import affineGPU, deskewGPU, rotateGPU

def test_deskew(raw_image, deskewed_image):
    """test basic deskewing of a file aquired on a lattice light sheet"""
    deskewed = deskewGPU(raw_image, dzdata=0.3, angle=31.5, pad_val=98)
    npt.assert_allclose(deskewed, deskewed_image)

def test_affine_raises(raw_image):
    """Make sure that the affine transformation matrix must be the right size"""
    with pytest.raises(ValueError):
        affineGPU(raw_image, np.eye(5))
    with pytest.raises(ValueError):
        affineGPU(raw_image, np.eye(3))
    with pytest.raises(ValueError):
        affineGPU(raw_image, np.eye(2))

def test_affine_eye(raw_image):
    """test that an affine transform with identity matrix does not change the input"""
    result = affineGPU(raw_image, np.eye(4))
    npt.assert_allclose(result, raw_image)

def test_affine_translate(raw_image):
    """
    test affine translation leads to a bunch of empty values up to
    but not more than the requested amount of translation
    """
    xpix = -50
    ypix = -100
    zpix = -3
    T = np.array([[1, 0, 0, xpix], [0, 1, 0, ypix], [0, 0, 1, zpix], [0, 0, 0, 1]])
    result = affineGPU(raw_image, T)
    assert not np.allclose(raw_image, result, atol=10)
    assert np.all(result[:-zpix] == 0)
    assert not np.all(result[: -zpix + 1] == 0)
    assert np.all(result[:, :-xpix, :-zpix] == 0)

def test_affine_translate_RA(raw_image):
    """
    make sure the referenceing object works to accept transformation
    matrices in units of sample space, instead of world coordinates
    """
    xpix = -50
    ypix = -100
    zpix = -3
    T = np.array([[1, 0, 0, xpix], [0, 1, 0, ypix], [0, 0, 1, zpix], [0, 0, 0, 1]])
    voxsize = (0.5, 0.5, 0.5)
    result = affineGPU(raw_image, T, voxsize)
    assert not np.allclose(raw_image, result, atol=10)
    assert np.all(result[: -zpix * 2] == 0)
    assert not np.all(result[: (-zpix * 2) + 1] == 0)
    assert np.all(result[:, : -xpix * 2, : -zpix * 2] == 0)

def test_rotate(deskewed_image, rotated_image):
    """test that rotateGPU rotates the image on the Y axis by some angle"""
    result = rotateGPU(deskewed_image, 0.3, dxdata=0.1, angle=31.5)
    npt.assert_allclose(result, rotated_image)


from bs4 import BeautifulSoup
import requests

uname=input("Enter username:")
source=requests.get("https://myanimelist.net/profile/"+uname).text
soup=BeautifulSoup(source,'lxml')
#print(soup.prettify())

t1=soup.find('title').text
print(t1)

t2=soup.find('div',class_='stat-score di-t w100 pt8').text
print(t2)

print("Anime stats: ")
for t4 in soup.find_all('li',class_='clearfix mb12'):

   heading=t4.a.text+" "
   value=t4.span.text
   final=heading+value
   print(final)
   print(type(t4))

#----------------------------------------------ANIME SEARCH-------------------------------------------------------------------
@client.command()
async def ansearch(ctx):
   embed=discord.Embed(
      title='Which anime do you want to search?',
      description="Timeout in 10 seconds",
      colour=discord.Colour.green()

   )
   await ctx.send(embed=embed)
   def check(m):
      return m.author.id==ctx.author.id
   message=await client.wait_for('message',timeout=10,check=check)

   an=message.content
   search = AnimeSearch(an)

   for i in range(0,10):
      await ctx.send(str(i+1)+"."+search.results[i].title)

#----------------------------------------------MANGA SEARCH-------------------------------------------------------------------
@client.command()
async def masearch(ctx):
   embed=discord.Embed(
      title='Which manga do you want to search?',
      description="Timeout in 10 seconds",
      colour=discord.Colour.green()

   )
   await ctx.send(embed=embed)
   def check(m):
      return m.author.id==ctx.author.id
   message=await client.wait_for('message',timeout=10,check=check)

   ma=message.content
   search = MangaSearch(ma)

   for i in range(0,10):
      await ctx.send(str(i+1)+"."+search.results[i].title)