import logging
import importlib
import copy
import pkgutil
import pkg_resources
import yaml

from pathlib import Path

import hyperspy.misc.config_dir

_logger = logging.getLogger(__name__)

_ext_f = Path(__file__).resolve().parent.joinpath("hyperspy_extension.yaml")
with open(_ext_f, 'r') as stream:
    EXTENSIONS = yaml.safe_load(stream)
EXTENSIONS["GUI"]["widgets"] = {}

# External extensions are not integrated into the API and not
# import unless needed
ALL_EXTENSIONS = copy.deepcopy(EXTENSIONS)

_external_extensions = [
    entry_point.module_name
    for entry_point in pkg_resources.iter_entry_points('hyperspy.extensions')]

for _external_extension_mod in _external_extensions:
    _logger.info("Enabling extension %s" % _external_extension_mod)
    _path = Path(
        pkgutil.get_loader(_external_extension_mod).get_filename()
    ).resolve().parent.joinpath("hyperspy_extension.yaml")

    if _path.is_file():
        with open(_path, 'r') as stream:
            _external_extension = yaml.safe_load(stream)
            if "signals" in _external_extension:
                ALL_EXTENSIONS["signals"].update(_external_extension["signals"])
            if "components1D" in _external_extension:
                ALL_EXTENSIONS["components1D"].update(
                    _external_extension["components1D"])
            if "components2D" in _external_extension:
                ALL_EXTENSIONS["components2D"].update(
                    _external_extension["components2D"])
            if "GUI" in _external_extension:
                if "toolkeys" in _external_extension["GUI"]:
                    ALL_EXTENSIONS["GUI"]["toolkeys"].extend(
                        _external_extension["GUI"]["toolkeys"])
                if "widgets" in _external_extension["GUI"]:
                    for toolkit, specs in _external_extension["GUI"]["widgets"].items():
                        if toolkit not in ALL_EXTENSIONS["GUI"]["widgets"]:
                            ALL_EXTENSIONS["GUI"]["widgets"][toolkit] = {}
                        ALL_EXTENSIONS["GUI"]["widgets"][toolkit].update(specs)

    else:
        _logger.error(
            "Failed to load hyperspy extension from {0}. Please report this issue to the {0} developers".format(_external_extension_mod))


from __future__ import absolute_import

import asyncio
import pandas as pd

from airbase import Airtable
from airbase import tools as at_tools
from airbase.utils import Logger
from gspread import authorize
from typing import Iterable, Optional

logger = Logger.start(__name__)

class Crudder:
    @staticmethod
    def _fmt_col_names(
        column: str, prefix: str, style: str, abbrs: list
    ) -> str:
        """Format Column Names"""
        style = style.lower()
        col_name = ""
        if prefix:
            col_name += prefix
        for part in column.split("_"):
            if style == "upper" or (abbrs and part.lower() in abbrs):
                part = part.upper()
            elif style in ("camel", "title"):
                part = part.title()
            if style != "camel":
                col_name += part + " "
        return col_name.strip()

    @staticmethod
    async def get_csv_data(
        filepath: str,
        prefix: str = None,
        style: str = None,
        abbrs: list = None,
    ) -> pd.DataFrame:
        """
        Get CSV file as a pandas DataFrame.

        Args:
            filepath: full path to csv file.

        Kwargs:
            prefix (default=None): desired prefix
            style (default=None): "lower", "upper", "camel" or "title"
            abbrs (default=None): list of lowercase abbrs to make upper case

        Return:
            data: If succesful, pandas DataFrame
        """  # noqa: E501
        df = pd.read_csv(filepath, encoding="utf-8")

        if style:
            df.rename(
                columns=lambda x: Crudder._fmt_col_names(
                    x, prefix, style, abbrs
                ),
                inplace=True,
            )

        logger.info(f"Fetched {df.shape[0]} rows/records from {filepath}")
        return df

    @staticmethod
    async def convert_to_df(
        source: Optional[Iterable] = None, format: Optional[str] = None
    ) -> pd.DataFrame:
        """
        Get SQL query formatted for airtable

        Args:
            df: pandas DataFrame

        Kwargs:
            output (default="records"): "records" or "rows"

        Return:
            data: If succesful, list of records or rows
        """  # noqa: E501
        assert source in (
            "records",
            "rows",
        ), "{} is not an acceptable output type".format(source)

        if isinstance(source, dict):
            pass

        elif isinstance(source, list):
            pass

    @staticmethod
    async def convert_df(df: pd.DataFrame, output: str = "records") -> list:
        """
        Get SQL query formatted for airtable

        Args:
            df: pandas DataFrame

        Kwargs:
            output (default="records"): "records" or "rows"

        Return:
            data: If succesful, list of records or rows
        """  # noqa: E501
        assert output in (
            "records",
            "rows",
        ), "{} is not an acceptable output type".format(output)

        if output == "records":
            df = df.where(pd.notnull(df), None)
            records = [{"fields": fields} for fields in df.to_dict("records")]
            return records

        elif output == "rows":
            headers = df.columns.values.tolist()
            rows = df.values.tolist()
            rows.insert(0, headers)
            return rows

    @staticmethod
    async def crud(data: list, dest: str, **kwargs) -> None:
        """
        Post Snowflake Query to Airtable or Sheets

        Args:
            data: List of records or rows to CRUD
            dest: airtable or sheets

        Kwargs:
            credentials (``oauth2client.service_account.ServiceAccountCredentials`` or ``string``): Google Credentials Object for Service Account or Airtable API Key
            type(``string``): Type of CRUD operation ("overwrite", "update", "partial" or "append")
            target(``dict``):
                table (``string``): Airtable Table Name or ID
                wks_index (``int``): Worksheet Index
            id (``string``): Airtable Base ID or Spreadheet Key
            primary_keys (``list``): List of field names to be used as primary keys
            links (``list``, optional): List of lowercase abbrs to make upper case
                table (``string``): Name of Airtable Table to link to
                primary_key (``string``): Name of field to use as a primary key in link table
                fields (``list``): List of fields to link
            arrays (``list``, optional): list of fields to turn into arrays for Multiple Select
            overrides (``list``, optional): List of dictionaries with two entries:
                override_field (``string``): Name of field that user can override
                ref_field (``string``): Name of field (checkbox) that flags a user override
            value_input_option (``string``, default="USER_ENTERED"): Sheets input style ("RAW" or "USER_ENTERED")
        """  # noqa: E501
        if dest == "airtable":
            await Crudder._at_crud(data, **kwargs)
        elif dest == "sheets":
            Crudder._sh_crud(data, **kwargs)

    @staticmethod
    async def _at_get_linked_tables(
        base: object, records: list, link: dict
    ) -> None:
        linked_table = await base.get_table(link["table"], key="name")
        link["table"] = await linked_table.get_records()
        await at_tools.link_tables(
            records, link["table"], link["fields"], link["primary_key"],
        )

    @staticmethod
    async def _at_sort_record(
        record: dict,
        primary_keys: list,
        arrays: list,
        overrides: list,
        existing_records: list,
        existing_records_indices_by_primary_key: dict,
        post_records: list,
        update_records: list,
        existing_indices: list,
    ) -> None:
        # turn array values into lists
        if arrays:
            record = await at_tools.graft_fields(record, arrays)

        # check for existing via primary keys
        hashable_keys = await at_tools.get_primary_keys_as_hashable(  # noqa: E501
            record, primary_keys
        )
        existing_index = (
            existing_records_indices_by_primary_key.get(hashable_keys)
            if hashable_keys
            else None
        )

        # if record exists
        if existing_index is not None:
            # add index to existing indices list
            existing_indices.append(existing_index)
            existing_record = existing_records[existing_index]

            # remove overriden fields
            if overrides:
                record = await at_tools.override_record(
                    record, existing_record, overrides
                )

            # filter records to only keep new data
            record = await at_tools.filter_record(record, existing_record)

            # if new data, then append to update_records
            if record["fields"]:
                update_records.append(record)

        # else append to post_records
        else:
            post_records.append(record)

    @staticmethod
    async def _at_crud(records: list, **kwargs) -> None:
        """
        Post Snowflake Query to Airtable

        Args:
            records: List of records to CRUD

        Kwargs:
            credentials (``string``): Airtable API Key
            type(``string``): Type of CRUD operation ("overwrite", "update", "partial" or "append")
            target(``dict``):
                table (``string``): Airtable Table Name or ID
            id (``string``): Airtable Base ID
            primary_keys (``list``): List of field names to be used as primary keys
            links (``list``, optional): List of lowercase abbrs to make upper case
                table (``string``): Name of Airtable Table to link to
                primary_key (``string``): Name of field to use as a primary key in link table
                fields (``list``): List of fields to link
            arrays (``list``, optional): list of fields to turn into arrays for Multiple Select
            overrides (``list``, optional): List of dictionaries with two entries:
                override_field (``string``): Name of field that user can override
                ref_field (``string``): Name of field (checkbox) that flags a user override
        """  # noqa: E501

        api_key = kwargs.get("credentials")
        mode = kwargs.get("type")
        table_key = kwargs.get("target").get("table")
        base_id = kwargs.get("id")
        primary_keys = kwargs.get("primary_keys")
        links = kwargs.get("links") or []
        arrays = kwargs.get("arrays")
        prefix = kwargs.get("prefix")
        overrides = kwargs.get("overrides")
        attachement_fields = kwargs.get("attachments")

        # Create Airtable() instance for this base & table
        async with Airtable(api_key=api_key) as at:
            base = await at.get_base(base_id, "id")
            table = await base.get_table(table_key, key="name")

            # Get records in that table
            existing_records = await table.get_records()

            if attachement_fields:
                for record in existing_records:
                    for field in attachement_fields:
                        if field in record["fields"]:
                            record["fields"][field] = [
                                {"url": item["url"]}
                                for item in record["fields"][field]
                            ]

            # Get linked tables
            await asyncio.gather(
                *[
                    Crudder._at_get_linked_tables(base, records, link)
                    for link in links
                ],
                return_exceptions=False,
            )

            # If records in table
            if existing_records:
                post_records = []  # Records to post
                update_records = []  # Records to update
                delete_records = []  # Records to delete
                existing_indices = []  # Indices of records in existing_records

                existing_records_indices_by_primary_key = {}
                for i, existing_record in enumerate(existing_records):
                    hashable_keys = await at_tools.get_primary_keys_as_hashable(  # noqa: E501
                        existing_record, primary_keys
                    )
                    if hashable_keys:
                        existing_records_indices_by_primary_key[
                            hashable_keys
                        ] = i

                await asyncio.gather(
                    *[
                        Crudder._at_sort_record(
                            record,
                            primary_keys,
                            arrays,
                            overrides,
                            existing_records,
                            existing_records_indices_by_primary_key,
                            post_records,
                            update_records,
                            existing_indices,
                        )
                        for record in records
                    ],
                    return_exceptions=False,
                )

                # create new records
                await table.post_records(post_records)
                # update existing records
                await table.update_records(update_records)

                # Get dead record indices
                all_indices = set(range(len(existing_records)))
                existing_indices = set(existing_indices)
                dead_indices = all_indices - existing_indices

                if len(dead_indices) > 0:
                    # loop through records ro delete
                    for index in dead_indices:
                        # get dead record
                        dead_record = existing_records[index]

                        if mode == "overwrite":
                            delete_records.append(dead_record)
                            continue
                        if mode == "update":
                            del_field = "Delete"
                            if prefix:
                                del_field = "AUTO_Delete"

                            if not dead_record["fields"].get(del_field):
                                record = {
                                    "id": dead_record["id"],
                                    "fields": {del_field: True},
                                }
                                delete_records.append(record)

                if delete_records:
                    # delete record
                    if mode == "overwrite":
                        await table.delete_records(delete_records)
                    # flag manual deletion via 'Delete' checkbox field
                    elif mode == "update":
                        await table.update_records(delete_records)

            # If no records in table
            else:
                if arrays:
                    # turn array values into lists
                    records = [
                        await at_tools.graft_fields(record, arrays)
                        for record in records
                    ]
                await table.post_records(records)

    @staticmethod
    def _sh_crud(rows: list, **kwargs) -> None:
        """
        Post Snowflake Query to Sheets

        Args:
            rows: List of rows to CRUD

        Kwargs:
            credentials (``oauth2client.service_account.ServiceAccountCredentials``): Google Credentials Object for Service Account
            type(``string``): Type of CRUD operation ("overwrite" or "append")
            target(``dict``):
                wks_index (``int``): Worksheet Index
            id (``string``): Spreadheet Key
            value_input_option (``string``, default="USER_ENTERED"): Sheets input style ("RAW" or "USER_ENTERED")
        """  # noqa: E501
        credentials = kwargs.get("credentials")
        sh_key = kwargs.get("id")
        wks_index = kwargs.get("target").get("wks_index")
        txn_type = kwargs.get("type")
        value_input_option = kwargs.get("value_input_option") or "USER_ENTERED"

        n_of_rows = len(rows)
        n_of_columns = len(rows[0])
        row_offset = 0

        # get the work sheet
        client = authorize(credentials)
        sh = client.open_by_key(sh_key)
        wks = sh.get_worksheet(wks_index)

        # overwrite
        if txn_type == "overwrite":
            wks.resize(rows=n_of_rows, cols=n_of_columns)
            cell_list = wks.range(1, 1, n_of_rows, n_of_columns)

        # append
        elif txn_type == "append":
            rows = rows[1:]
            n_of_rows = len(rows)
            row_count = wks.row_count
            col_count = wks.col_count
            if n_of_columns > col_count:
                col_count = n_of_columns
            wks.resize(rows=row_count + n_of_rows, cols=col_count)
            wks = sh.get_worksheet(wks_index)
            cell_list = wks.range(
                row_count + 1, 1, row_count + n_of_rows, n_of_columns
            )
            row_offset = row_count

        # TODO elif txn_type in ("update", "partial")
        else:
            return

        for i, row in enumerate(rows):
            for j, item in enumerate(row):
                cell_list[i * n_of_columns + j].value = item if item else ""

        row_chunk = 1500
        max_cells = n_of_columns * row_chunk
        for i in range(int(len(cell_list) / max_cells + 1)):
            min_index = i * max_cells
            max_index = i * max_cells + max_cells - 1
            if max_index > len(cell_list):
                max_index = len(cell_list)

            sublist = cell_list[min_index:max_index]
            try:
                wks.update_cells(
                    sublist, value_input_option=value_input_option
                )
            except TypeError:
                wks.update_cells(sublist)
            start_row = int(min_index / n_of_columns) + row_offset + 1
            end_row = start_row + row_chunk - 1
            if end_row > n_of_rows + row_offset:
                end_row = n_of_rows + row_offset
            logger.info(f"Posted rows {start_row} to {end_row} to Sheets")

            if max_index == len(cell_list) - 1:
                break

        logger.info("Completed posting to Sheets")


import argparse
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from telegram import Bot, Update, ReplyKeyboardMarkup
import os
from news_collector import start_news_collector
from telegram.error import (TelegramError, Unauthorized, BadRequest,
                            TimedOut, ChatMigrated, NetworkError)
import logging

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

collector = start_news_collector()

def parse_args():
    parser = argparse.ArgumentParser(
        description="PyNewsBot v.0.0.1\nThis bot's job is to "
                    "provide you with fresh news about Python"
                    "and he loves it!")
    parser.add_argument('-v', '--verbose', action="store_true", help='Verbose mode')
    parser.add_argument('-s', '--silent', action="store_true", help='Silent mode')
    return parser.parse_args()

def get_random_fresh_post():
    return collector.get_random_fresh_post()

def start(bot: Bot, update: Update):
    keyboard = [
        ['/get_news'],
    ]
    bot.send_message(chat_id=update.message.chat_id,
                     text='I am the PyNewsBot and I am here'
                          'to provide fresh news about Python.',
                     reply_markup=ReplyKeyboardMarkup(keyboard=keyboard))

def unknown(bot: Bot, update: Update):
    bot.send_message(chat_id=update.message.chat_id,
                     text="Sorry, but I can't help you with that -_-")

def echo(bot: Bot, update: Update):
    bot.send_message(chat_id=update.message.chat_id,
                     reply_to_message_id=update.message.message_id,
                     text=update.message.text)

def get_news(bot: Bot, update: Update):
    post = get_random_fresh_post()
    message = '{}{}\n{}'.format(post.description[:250].replace('<br>', '\n'), '...',
                                post.url)
    bot.send_message(chat_id=update.message.chat_id, text=message)

def error_callback(bot, update, error):
    logger = logging.getLogger()
    try:
        raise error
    except Unauthorized as e:
        logger.error('Chat_id:{}\nUser message:{}\nError message:\n{}\n'.format(
            update.message.chat_id,
            update.message.text,
            e.message)
        )
    except BadRequest as e:
        logger.error('Chat_id:{}\nUser message:{}\nError message:\n{}\n'.format(
            update.message.chat_id,
            update.message.text,
            e.message)
        )
    except TimedOut as e:
        logger.error('Chat_id:{}\nUser message:{}\nError message:\n{}\n'.format(
            update.message.chat_id,
            update.message.text,
            e.message)
        )
    except NetworkError as e:
        logger.error('Chat_id:{}\nUser message:{}\nError message:\n{}\n'.format(
            update.message.chat_id,
            update.message.text,
            e.message)
        )
    except ChatMigrated as e:
        logger.error('Chat_id:{}\nUser message:{}\nError message:\n{}\n'.format(
            update.message.chat_id,
            update.message.text,
            e.message)
        )
    except TelegramError as e:
        logger.error('Chat_id:{}\nUser message:{}\nError message:\n{}\n'.format(
            update.message.chat_id,
            update.message.text,
            e.message)
        )

if __name__ == '__main__':
    args = parse_args()
    logger = logging.getLogger()
    if args.silent:
        logger.setLevel(logging.FATAL)
    else:
        if args.verbose:
            logger.setLevel(logging.DEBUG)
    token = os.environ.get('TELEGRAM_BOT_API_TOKEN', None)
    if not token:
        raise EnvironmentError(
            'Need to export your telegram bot token as TELEGRAM_BOT_API_TOKEN enviroment variable.')
    updater = Updater(token=token)
    dispatcher = updater.dispatcher
    start_handler = CommandHandler('start', start)
    get_news_handler = CommandHandler('get_news', get_news)
    echo_handler = MessageHandler(Filters.text, echo)
    unknown_handler = MessageHandler(Filters.command, unknown)
    dispatcher.add_handler(start_handler)
    dispatcher.add_handler(echo_handler)
    dispatcher.add_handler(get_news_handler)
    dispatcher.add_error_handler(error_callback)
    dispatcher.add_handler(unknown_handler)
    updater.start_polling(bootstrap_retries=5)
    logger.info('Started the bot')


from flask import Flask, render_template, request, url_for, redirect, session
from flask_paginate import Pagination
import execute

app = Flask(__name__)

def paginate(query, per_page=25):
    total = query.count()
    page = int(request.args.get('page', 1))
    items = query.offset((page -1)* per_page).limit(per_page).all()
    pagination = Pagination(page=page, total=total, record_name='items', per_page=per_page, css_framework="bootstrap5")
    return items, pagination

@app.route('/', methods=["GET", "POST"])
def index():
    auswahl = request.form.get("auswahl")
    if auswahl is None:
        return render_template('index.html')

@app.route('/popup', methods=["GET", "POST"])
def popup():
    return render_template('popup.html')

@app.route('/add', methods=["GET", "POST"])
def add():
    auswahl = request.form.get("auswahl")
    if auswahl is None:
        return render_template("add.html")
    else:
        return render_template("add.html", auswahl=auswahl)

@app.route('/nutzer', methods=["GET", "POST"])
def nutzer():
    action = request.form.get("action")
    edit = request.form.getlist("edit")
    id = request.form.get("id")
    if action == "Delete":
        execute.del_user(id)
    if edit:
        execute.update_user(edit[0], edit[1], edit[2], edit[3], edit[4])
    users, pagination = paginate(execute.get_all_users())
    return render_template("nutzer.html", users=users, pagination=pagination)

@app.route('/buch', methods=["GET", "POST"])
def buch():
    action = request.form.get("action")
    edit = request.form.getlist("edit")
    id = request.form.get("id")
    if action == "Delete":
        execute.del_buch(id)
    if edit:
        execute.update_buch(edit[0], edit[1], edit[2], edit[3])
    books, pagination = paginate(execute.get_all_books())
    return render_template("buch.html", books=books, pagination=pagination)

@app.route('/ausleih', methods=["GET", "POST"])
def ausleih():
    return render_template("ausleih.html")

@app.route('/search', methods=["GET", "POST"])
def search():
    auswahl = request.form.get("auswahl")
    search = request.form.get("search")

    if str(auswahl).lower() == "nutzer.html":
        if search:
            return render_template("search.html", auswahl=auswahl.lower(), users=execute.search_user(search))
        return render_template("search.html", auswahl=auswahl.lower())
    elif str(auswahl).lower() == "buch":
        if search:
            return render_template("search.html", auswahl=auswahl.lower(), books=execute.search_book(search))
        return render_template("search.html", auswahl=auswahl.lower())
    else:
        return render_template("search.html")

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


from datetime import timedelta
import airflow
from airflow import DAG
from airflow.operators.bash_operator import BashOperator
from airflow.contrib.operators.bigquery_operator import BigQueryOperator
# from airflow.contrib.operators import bigquery_operator

default_args={
    'owner': 'airflow',
    'start_date': airflow.utils.dates.days_ago(2),
    'depends_on_past': False,
    'email': ['airflow@example.com'],
    'email_on_failure': False,
    'email_on_retry': False,
    'retries': 1,
    'retry_delay':timedelta(seconds=20)
}

dag = DAG('dm_dag_1_v1_1_1',
        default_args=default_args,
        description='Datamart Template DAG',
        schedule_interval=timedelta(minutes=5)
        )

display_msg = BashOperator(task_id='print_msg',
                    bash_command='echo "Connecting Bigquery ..............."' ,
                    dag=dag)

delay_wait = BashOperator(task_id='Sleeping',
                    bash_command='sleep 5m' ,
                    dag=dag)

# delay_wait_2 = BashOperator(task_id='Sleeping_2',
#                     bash_command='sleep 20s' ,
#                     dag=dag)

run_bigquery = BigQueryOperator(
    task_id='get_sample_data',
    sql='bql/bigquery_sample.sql',
    use_legacy_sql=False,
    dag=dag
)
display_msg >> delay_wait  >> run_bigquery


spisok=[] #Tühi loetelu(list)
num=[1,2,3,4,5]
abc=["Abc","B","C"]
slovo="Programmeerimine"
slovo_list=list(slovo)
print(slovo) #Sõna
print(slovo_list) #Tähtede kaupa

while True:
    valik=int(input("1 - добавить букву в список\n2 - склеить списки\n3 - добавить буквы на i - позицию\n4 - удалить букву\n"))
    if valik==1:
        a=input("Введи букву\n")
        slovo_list.append(a) #Lisab loendi lõppu elemendi
        print(f"Добавили {a}, новый список", slovo_list)
    elif valik==2:
        slovo_list.extend(abc) #Laiendab nimekirja, lisades lõppu kõik nimekirja elemendid
        print(slovo_list)
    elif valik==3:
        a=input("Введи букву, которую хочешь добавить")
        i=int(input("Введи номер позиции, куда хочешь добавить буквы\n"))
        slovo_list.insert(i-1,a) #0,1,2..
        print(slovo_list)
    elif valik==4:
        a=input("Введи букву, которую хочешь удалить\n")
        n=slovo_list.remove(a)
    else:
        print("Искомой буквы нет")
    print(slovo_list)


import os
from typing import Type

import cv2
import numpy as np
from absl import app
from absl import flags
from pyrep.objects.dummy import Dummy
from pyrep.objects.vision_sensor import VisionSensor

from amsolver import Environment
from amsolver.action_modes import ActionMode
from amsolver.backend.observation import Observation
from amsolver.backend.task import TASKS_PATH
from amsolver.backend.task import Task
from amsolver.backend.utils import task_file_to_task_class
from amsolver.observation_config import ObservationConfig
from amsolver.sim2real.domain_randomization import RandomizeEvery, \
    VisualRandomizationConfig

FLAGS = flags.FLAGS

flags.DEFINE_string(
    'save_dir', '../amsolver/amsolver_videos',
    'Where to save the generated videos.')
flags.DEFINE_list(
    'tasks', ['wipe_table_color','wipe_table_direction', 'wipe_table_relative', 'wipe_table_shape', 'wipe_table_size'],
    'The tasks to record. If empty, all tasks are recorded.')
flags.DEFINE_boolean(
    'individual', True, 'One long clip of all the tasks, or individual videos.')
flags.DEFINE_boolean(
    'domain_randomization', False, 'If domain randomization should be applied.')
flags.DEFINE_string(
    'textures_path', '',
    'Where to locate textures if using domain randomization.')
flags.DEFINE_boolean('headless', True, 'Run in headless mode.')
flags.DEFINE_list(
    'camera_resolution', [1280, 720], 'The camera resolution')

class CameraMotion(object):
    def __init__(self, cam: VisionSensor):
        self.cam = cam

    def step(self):
        raise NotImplementedError()

    def save_pose(self):
        self._prev_pose = self.cam.get_pose()

    def restore_pose(self):
        self.cam.set_pose(self._prev_pose)

class CircleCameraMotion(CameraMotion):

    def __init__(self, cam: VisionSensor, origin: Dummy, speed: float):
        super().__init__(cam)
        self.origin = origin
        self.speed = speed  # in radians

    def step(self):
        self.origin.rotate([0, 0, self.speed])

class TaskRecorder(object):

    def __init__(self, env: Environment, cam_motion: CameraMotion, fps=30):
        self._env = env
        self._cam_motion = cam_motion
        self._fps = fps
        self._snaps = []
        self._current_snaps = []

    def take_snap(self, obs: Observation):
        self._cam_motion.step()
        self._current_snaps.append(
            (self._cam_motion.cam.capture_rgb() * 255.).astype(np.uint8))

    def record_task(self, task: Type[Task]):
        task = self._env.get_task(task)
        self._cam_motion.save_pose()
        while True:
            try:
                _, success = task.get_demos(
                    1, live_demos=True, callable_each_step=self.take_snap,
                    max_attempts=1)
                if success:
                    break
                else:
                    self._cam_motion.restore_pose()
                    self._current_snaps = []
            except RuntimeError:
                self._cam_motion.restore_pose()
                self._current_snaps = []
        self._snaps.extend(self._current_snaps)
        self._current_snaps = []
        return True

    def save(self, path):
        print('Converting to video ...')
        os.makedirs(os.path.dirname(path), exist_ok=True)
        video = cv2.VideoWriter(
                path, cv2.VideoWriter_fourcc(*'MJPG'), self._fps,
                tuple(self._cam_motion.cam.get_resolution()))
        for image in self._snaps:
            video.write(cv2.cvtColor(image, cv2.COLOR_RGB2BGR))
        video.release()
        self._snaps = []

def main(argv):

    obs_config = ObservationConfig(record_gripper_closing=True)
    obs_config.set_all(False)

    vrc = rand_every = None
    frequency = 0
    if FLAGS.domain_randomization:
        vrc = VisualRandomizationConfig(FLAGS.textures_path)
        rand_every = RandomizeEvery.TRANSITION
        frequency = 10

    env = Environment(ActionMode(), obs_config=obs_config,
                      randomize_every=rand_every, frequency=frequency,
                      visual_randomization_config=vrc, headless=FLAGS.headless)
    env.launch()

    # Add the camera to the scene
    cam_placeholder = Dummy('cam_cinematic_placeholder')
    cam = VisionSensor.create(FLAGS.camera_resolution)
    cam.set_pose(cam_placeholder.get_pose())
    cam.set_parent(cam_placeholder)

    cam_motion = CircleCameraMotion(cam, Dummy('cam_cinematic_base'), 0.005)
    tr = TaskRecorder(env, cam_motion, fps=30)

    if len(FLAGS.tasks) > 0:
        task_names = FLAGS.tasks
    else:
        tasks_path = './vlm/tasks'
        task_names = [t.replace('.py', '') for t in os.listdir(tasks_path)
                    if t != '__init__.py' and t.endswith('.py')]
    task_classes = [task_file_to_task_class(
        task_file,  parent_folder = 'vlm') for task_file in task_names]

    for i, (name, cls) in enumerate(zip(task_names, task_classes)):
        good = tr.record_task(cls)
        if FLAGS.individual and good:
            tr.save(os.path.join(FLAGS.save_dir, '%s.avi' % name))

    if not FLAGS.individual:
        tr.save(os.path.join(FLAGS.save_dir, 'recorded_tasks.avi'))
    env.shutdown()

if __name__ == '__main__':
    app.run(main)


print("Menu Takoyaki")
print("="*50)
print("Varian 1 = Rp.2000/pcs")
print("Varian 2 = Rp.2500/pcs")
stop = False
while (not stop):
	print("="*50)
	pil = int(input("Pilih Varian Takoyaki (1/2) : "))
	if (pil==1):
		beli = int(input("Berapa Banyak yang ingin di beli : "))
		if beli <= 45:
			if beli >= 10:
				harga = 2000
				potongan = int(beli * harga * 0.1)
				bayar = beli * harga - potongan
				print("Total bayar : Rp."+str(bayar))
			else :
				harga = 2000
				bayar = beli * harga
				print("Total bayar : Rp."+str(bayar))
		else :
			print("Takoyakinya Tidak Cukup")
	else :
		beli = int(input("Berapa Banyak yang ingin di beli : "))
		if beli <= 40:
			if beli >= 8:
				harga = 2500
				potongan = int(beli * harga * 0.08)
				bayar = beli * harga - potongan
				print("Total bayar : Rp."+str(bayar))
			else :
				harga = 2500
				bayar = beli * harga
				print("Total bayar : Rp."+str(bayar))
		else :
			print("Takoyakinya Tidak Cukup")
	coba = input("Mau Coba Lagi ? (ya/tidak) ")
	if coba == "tidak":
		stop = True
print("=========Terima Kasih==========")


#PyTorch lib
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.utils.data as Data
import torch.nn.functional as F
import torchvision
#Tools lib
import numpy as np
import cv2
import random
import time
import os
import argparse
from skimage.metrics import peak_signal_noise_ratio, structural_similarity

def calc_psnr(im1, im2):
    im1_y = cv2.cvtColor(im1, cv2.COLOR_BGR2YCR_CB)[:, :, 0]
    im2_y = cv2.cvtColor(im2, cv2.COLOR_BGR2YCR_CB)[:, :, 0]
    return peak_signal_noise_ratio(im1_y, im2_y)

def calc_ssim(im1, im2):
    im1_y = cv2.cvtColor(im1, cv2.COLOR_BGR2YCR_CB)[:, :, 0]
    im2_y = cv2.cvtColor(im2, cv2.COLOR_BGR2YCR_CB)[:, :, 0]
    return structural_similarity(im1_y, im2_y)

def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--input_dir", default='./test_results/deraindrop', type=str)
    parser.add_argument("--gt_dir", default='./demo_samples/deraindrop', type=str)
    args = parser.parse_args()
    return args

def align_to_four(img):
    a_row = int(img.shape[0]/4)*4
    a_col = int(img.shape[1]/4)*4
    img = img[0:a_row, 0:a_col]
    return img

if __name__ == '__main__':
    args = get_args()

    input_list = sorted(os.listdir(args.input_dir))
    gt_list = sorted(os.listdir(args.gt_dir))
    num = len(input_list)
    cumulative_psnr = 0
    cumulative_ssim = 0
    for i in range(num):
        print('Processing image: %s'%(input_list[i]))
        img = cv2.imread(os.path.join(args.input_dir, input_list[i]))
        gt = cv2.imread(os.path.join(args.gt_dir, gt_list[i]))
        img = align_to_four(img)
        gt = align_to_four(gt)
        result = img
        # result = np.array(result, dtype = 'uint8')
        cur_psnr = calc_psnr(result, gt)
        cur_ssim = calc_ssim(result, gt)
        print('PSNR is %.4f and SSIM is %.4f'%(cur_psnr, cur_ssim))
        cumulative_psnr += cur_psnr
        cumulative_ssim += cur_ssim
    print('In testing dataset, PSNR is %.4f and SSIM is %.4f'%(cumulative_psnr/num, cumulative_ssim/num))


"""
Flask application main module.

Run in development mode:
set FLASK_ENV=development
flask run
"""

from flask import Flask, request, render_template, make_response
from io import BytesIO
from plotting import initialize_matplotlib, generate_plot, close_fig
from solar import compute_orbit

initialize_matplotlib()
app = Flask(__name__) # starts the flask application

@app.route('/')
def main():
    """
    Returns:
        Rendered index page template
    """
    return render_template('index.html')

@app.route('/ss.png')
def image():
    """
    Serves ss.png?date=<date>&n=<n>

    Get parameters:
        date: in the `yyyy-mm-dd` format
        n: limit on the number of planets to render (e.g.,., 4 to go up to Mars)

    Returns:
        Rendered image of the locations and orbits of the
        <n> planets on the given date

    Raises:
        Will raise exceptions if GET parameters are missing or incorrect.
        Warning: no error checks are implemented.
    """
    date = request.args.get('date', type=str)
    n = request.args.get('n', default = 4, type=int)

    fig = generate_plot(date, n)
    fmt = "png"

    # here the generated matplotlib plot is saved into a binary stream
    bio = BytesIO()
    fig.savefig(bio, format=fmt)
    close_fig(fig)

    #... which is then returned along with the HTTP response
    r = make_response(bio.getvalue())
    r.headers['Content-Type'] = 'image/{}'.format(fmt)
    # Note: browser caching was disabled to facilitate testing
    r.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
    r.headers['Pragma'] = 'no-cache'
    r.headers['Expires'] = '-1'
    bio.close()
    return r


from itertools import permutations

nums = sorted(list(permutations([1,2,3,4,5,6,7,8,9],3)))
n = int(input())
strings = [input() for _ in range(n)]
ans_cnt = 0
for i in nums:
    num = ""
    for j in i:
        num+=str(j)
    flag = 0
    for j in strings:
        wnum,ws,wb = map(int, j.split())
        wnum = str(wnum)
        s=0
        b=0
        for k in range(3):
            if num[k]==wnum[k]:
                s+=1
            elif num[k] in wnum:
                b+=1
        if ws != s or wb != b:
            flag = 1
    if flag==0:
        ans_cnt+=1
print(ans_cnt)


from collections import defaultdict

def ceildiv(a, b):
    return (a + b - 1) // b

INF = 10**9

def main():
    N, H = map(int, input().split())
    ds = defaultdict(lambda: -INF)  # t -> max d_t
    for n in range(N):
        t, d = map(int, input().split())
        ds[t] = max(ds[t], d)
    # Remove t and d such that t * d does not increase
    mx = -INF
    ts = []
    es = dict()
    for t in sorted(ds.keys()):
        td = t * ds[t]
        if td > mx:
            mx = td
            ts.append(t)
            es[t] = ds[t]
    # e_t --> f_t = max_t'>=t e_t'
    mx = -INF
    us = []
    for t in sorted(es.keys(), reverse=True):
        if es[t] > mx:
            us.append(t)
            mx = es[t]
    us.reverse()

    M = 0
    t = 0
    for u in us:
        f = es[u]
        s = M // f

        # sim <= s
        D = (s - t) * M
        if H <= D:
            ans = ceildiv(H, M) + t
            print(ans)
            return
        H -= D
        t = s

        # sim <= u
        def damages(x):
            return (x + t + 1) * (x - t) // 2 * f
        D = damages(u)
        if H <= D:
            left = t  # NG
            right = u  # OK
            while right - left > 1:
                mid = (left + right) // 2
                if H - damages(mid) <= 0:
                    right = mid
                else:
                    left = mid
            print(right)
            return
        H -= D
        t = u
        M = u * f
    # same as sim <= s
    ans = ceildiv(H, M) + t
    print(ans)
    return

if __name__ == "__main__":
    main()


import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

from sklearn import preprocessing
from sklearn.preprocessing import MaxAbsScaler
from sklearn.linear_model import ElasticNetCV
from sklearn.base import BaseEstimator, ClassifierMixin

class Model4(BaseEstimator, ClassifierMixin):

    def __init__(self, disable=False):
        self.name = 'ElasticNetCV Model'
        self.disable = disable

    def transform(self, data):
        # features = ['X314', 'X0_6', 'X261']
        # data = data[features]
        if not hasattr(self, 'scaler'):
            self.scaler = MaxAbsScaler().fit(data)
        data = pd.DataFrame(self.scaler.transform(data))
        # features = [ 20, 146, 147, 183, 193, 265, 306, 398, 411]
        # features = [0, 2, 3, 5, 15, 16, 21, 27, 28, 29, 33, 38, 40, 41, 43, 45, 47,
        #             56, 61, 62, 63, 67, 71, 77, 82, 88, 99, 104, 107, 113, 115, 117, 119, 121, 124, 139,
        #             143, 153, 168, 172, 178, 179, 181, 187, 189, 194, 206, 213,
        #             225, 227, 228, 236, 238, 250, 255, 262, 264, 266, 267, 270, 274, 276, 281, 288,
        #             289, 300, 302, 303, 308, 330, 343, 387, 390, 392,
        #             399, 409, 415, 416, 419, 420, 427, 432,
        #             435, 439, 442, 444, 453, 454, 458, 459]
        # data = data.drop(features, axis=1)
        # features = [
        #     20, 133, 146, 147, 183,
        #     193, 199, 265, 306, 398,
        #     410, 411
        # ]
        # features = [
        #     20, 133, 146, 147, 183,
        #     193, 199, 265, 306, 398,
        #     410, 411
        # ]
        features = [18, 20, 133, 146, 147, 159, 183, 192, 193, 199, 202, 232, 249, 265, 269, 306, 398, 410, 411, 413, 445]
        data = data[features]
        return data

    def fit(self, train, y_train):
        if self.disable:
            print(self.name, 'disabled')
            return
        print('Training', self.name)
        # self.outlier = ModelTest()
        # self.outlier.fit(train, y_train)
        train = self.transform(train.copy())
        y_train = y_train.copy()

        # train['outlier'] = m_test.predict()

        # self.model = ElasticNetCV(l1_ratio=1.0, tol=0.0001)
        # self.model.fit(train, y_train)

        # self.drop_list = []
        # print(self.model.coef_)
        # print(len(self.model.coef_))
        # features = train.columns.values
        # for i in range(len(features)):
        #     if self.model.coef_[i] < 0.00001:
        #         # print(train.columns[i])
        #         self.drop_list.append(features[i])
        # train = train.drop(self.drop_list, axis=1)
        # print(train.shape)

        self.model = ElasticNetCV(l1_ratio=1.0, tol=0.0001)
        self.model.fit(train, y_train)

        # print(self.model.coef_)
        # print(len(self.model.coef_))
        # for i in range(len(train.columns)):
        #     if self.model.coef_[i] > 0.4:
        #         print("    ", train.columns.values[i], self.model.coef_[i])

    def predict(self, test):
        if self.disable or not self.model:
            return np.zeros((test.shape[0],))
        test = self.transform(test.copy())
        return self.model.predict(test)


import influxdb_client
import datetime
import psutil
import time

from influxdb_client.client.write_api import SYNCHRONOUS

# Setup DS18B20 sensor
from w1thermsensor import W1ThermSensor
sensor = W1ThermSensor()

bucket = "influx"
org = "private"
token = "<yourtoken>"
# Store the URL of your InfluxDB instance
url="http://127.0.0.1:8086"

measurement_name = "raspberrypi"

client = influxdb_client.InfluxDBClient(
    url=url,
    token=token,
    org=org
)

write_api = client.write_api(write_options=SYNCHRONOUS)

# take a timestamp for this measurement
timeNow = datetime.datetime.utcnow()

# collect some stats from psutil
disk = psutil.disk_usage('/')
ext = psutil.disk_usage('/srv/dev-disk-by-uuid-eb784068-eee2-4c3f-b7be-47444f65c7a9')
mem = psutil.virtual_memory()
load = psutil.getloadavg()
temp = psutil.sensors_temperatures()["cpu_thermal"]
cpu_usage_all_cores = psutil.cpu_percent(interval=1)

net_stat = psutil.net_io_counters(pernic=True, nowrap=True)["eth0"]
net_in_1 = net_stat.bytes_recv
net_out_1 = net_stat.bytes_sent
time.sleep(1)
net_stat = psutil.net_io_counters(pernic=True, nowrap=True)["eth0"]
net_in_2 = net_stat.bytes_recv
net_out_2 = net_stat.bytes_sent

net_in = round((net_in_2 - net_in_1) / 1024 / 1024, 3)
net_out = round((net_out_2 - net_out_1) / 1024 / 1024, 3)

# Get case temperature from DS18B20 sensor
case_temp = sensor.get_temperature()

#Calculate uptime
uptime_hours = (time.time() - psutil.boot_time())/3600
uptime_days = uptime_hours/24

# format the data as a single measurement for influx
body = [
    {
        "measurement": measurement_name,
        "time": timeNow,
        "fields": {
            "load_1": load[0],
            "load_5": load[1],
            "load_15": load[2],
            "cpu_usage": cpu_usage_all_cores,
            "disk_percent": disk.percent,
            "disk_free": disk.free,
            "disk_used": disk.used,
            "ext_percent": ext.percent,
            "ext_free": ext.free,
            "ext_used": ext.used,
            "mem_percent": mem.percent,
            "mem_free": mem.free,
            "mem_used": mem.used,
            "cpu_temp": temp[0].current,
            "eth0_in": net_in,
            "eth0_out": net_out,
            "case_temp": case_temp,
            "uptime_hours": uptime_hours,
            "uptime_days": uptime_days

        }
    }
]

write_api.write(bucket=bucket, org=org, record=body)


from gorgias.models import Message

class ConversationUnserializer:
    """Handle the json parsing of the contact"""

    def load(self, json_data):
        message = Message()
        message.body_html = json_data["body"]
        message.sent_datetime = json_data["created_at"]
        message.sender = json_data["support_email"]
        message.recipient = json_data["to_emails"][0]
        return message


#! /usr/bin/env python3
from setuptools import setup
import sys
if sys.version_info[0] < 3: raise RuntimeError("Sorry, you must use Python 3")
# The directory containing this file
import pathlib
HERE = pathlib.Path(__file__).parent
# The text of the README file
README = (HERE / "README.md").read_text()
setup(
    name                =   'masswappalyzer',
    description         =   "Run Wappalyzer asynchronously on a list of URLs and generate a Excel file containing all results.",
    url                 =   "https://github.com/tristanlatr/MassWappalyzer",
    maintainer          =   "tristanlatr",
    version             =   '1.7',
    entry_points        =   {'console_scripts': ['masswappalyzer = masswappalyzer:main'],},
    py_modules          =   ['masswappalyzer'],
    classifiers         =   ["Programming Language :: Python :: 3"],
    license             =   'Apache License 2.0',
    long_description    =   README,
    long_description_content_type   =   "text/markdown",
    install_requires    =   ['XlsxWriter', 'tqdm', 'pandas']
)


ls1 = []
ls2 = []
n=int(input("Enter the size of list: "))
for i in range(n):
    ls1.append(input("Enter the character: "))
for i in range(n):
    ls2.append(int(input("Enter the number: ")))
print("List 1: ",ls1)
print("List 2: ",ls2)
ls= list(set(ls2))
ls.sort()
res = []
for i in ls:
    for j in range(0, len(ls2)):
        if(ls2[j] == i):
            res.append(ls1[j])
print(res)


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures

#學習資料載入

training = np.random.randint(1, 100, size=(20, 2))

df = pd.DataFrame(training)
print(df)
train_x = training[:,0]
train_y = training[:,1]

# plt.subplot(3, 1, 1)
plt.title("random data")
# plt.subplots_adjust(bottom=0.1, top=0.9)
plt.scatter(train_x,train_y)
plt.show()

#參數初始化
theta0 = np.random.rand()
theta1 = np.random.rand()
#線性回歸函數後的預測函數
def f(x):
    return theta0 + theta1 * x

#cost function
def E(x, y):
    return 0.5 * np.sum((y - f(x)) ** 2)

#學習率
ETA = 1e-3

#誤差的差分
diff = 1

#更新回數
count = 0

#標準化
mu = train_x.mean()
sigma = train_x.std()

def standardize(x):
    return (x-mu) / sigma

train_z = standardize(train_x)
# plt.subplot(3, 1, 2)
plt.title("standardize")
# plt.subplots_adjust(bottom=0.1, top=0.9)
plt.scatter(train_z,train_y)
plt.show()

#畫出回歸線
slr = LinearRegression()
train_z = train_z.reshape(-1, 1)
slr.fit(train_z, train_y)
predicted_y1 = slr.predict(train_z)
# plt.subplot(3, 1, 3)
plt.title("GD")
# plt.subplots_adjust(bottom=0.1, top=0.9)
plt.scatter(train_z, train_y)
plt.plot(train_z, predicted_y1, color='r')
# plt.subplots_adjust(hspace=1)
plt.show()

#參數調校更新直到誤差差分小於0.01
error = E(train_z,train_y)
while diff > 1e-2  :
    #更新結果暫時儲存
    tmp_theta0 = theta0 - ETA * np.sum((f(train_z)-train_y))
    tmp_theta1 = theta1 - ETA * np.sum((f(train_z)-train_y) * train_z)
    #更新參數
    theta0 = tmp_theta0
    theta1 = tmp_theta1
    #計算與前一項誤差的差分
    current_error = E(train_z,train_y)
    diff = error - current_error
    error = current_error
    #運算過程
    count += 1
    log = '{}次: theta0 = {:.3f}, theta1 = {:.3f}, 差分 = {:.4f}'
    print(log.format(count,theta0,theta1,diff))


import requests
from parsel import Selector
from lxml import etree
import re
'''
场景：刷单 点赞 投票 爬虫（反爬虫）
1.IP电脑没有换IP
2.Cookie电脑没有换Cookie（登录信息 账号）
'''
import dailiIP
from time import sleep
ip_list=[]
#函数的定义
def getOnePageHTml(page):
    '''
    获取一页的数据 就是获取第几页的数据
    ：return：HTML网页
    :return:
    '''
    url = f'https://www.kuaidaili.com/free/inha/{page}/'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'
    }
    response = requests.get(url=url,headers=headers)
    return response.text

#解析HTML获得数据
def parseOneHTMLPage(text):
    #初始化需要从html中选择的内容
    selectors = Selector(text=text)
    selectors=selectors.xpath('//*[@id="list"]/table//tr')[1:]
    i = 1
    for selector in selectors:
        ip = selector.xpath('./td[1]/test()').get()
        port = selector.xpath('./td[2]/test()').get()

        print(ip,port,i)
        i +=1
        item=f'{ip}:{port}'
        if 'None' not in item:
            pipelinesCSV(item)

#保存数据
def pipelinesCSV(item):
    with open('kuaidaili.csv','a',encoding='utf-8') as fp:
        fp.write(item+ '\n') #item应该是一个字符串

#运行程序
def main():
    for page in range(1,100):
        text = getOnePageHTml(page)
        parseOneHTMLPage(text)
        sleep(0.5)
if __name__ == '__main__':
    main()


#Problem1: Quadratic Equation Solver
import math
import sys
#ax^2 + bx + c = 0
#This program finds the roots of this equation
a = int(sys.argv[1])
if a == 0 :
    sys.exit("'a' cannot be zero!")
b = int(sys.argv[2])
c = int(sys.argv[3])
# Finding the roots with quadratic formula
disc = b**2 - 4*a*c
if disc < 0 :
    sys.exit("The solution is not real.")
root1 = round((-b + math.sqrt(disc)) / (2*a),2)
root2 = round((-b - math.sqrt(disc)) / (2*a),2)
if disc > 0:
    print("There are two solutions.\nSolutions:", root1, root2)
else:
    print("There is one solution.\nSolution:", root1)

First Semester/Basic Python Calculations/1.py


from django.urls import path
from .views import ListLocationView, DetailLocationView, DetailRegionView, DetailLeisureView, ListPostView, \
    CreatePostView, Profile
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
    path('posts/', ListPostView.as_view(), name='posts'),
    path('create/', CreatePostView.as_view(template_name='location/add_location.html'), name='create_location'),
    path('', ListLocationView.as_view(), name='locations'),
    path('locations/<int:pk>', DetailLocationView.as_view(), name='location'),
    path('regions/<str:slug>', DetailRegionView.as_view(), name='region'),
    path('leisure/<str:slug>', DetailLeisureView.as_view(), name='leisure'),
    path('profile/', Profile.as_view(), name='profile'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


# Leetcode  ->  Medium
# Queries on Number of Points Inside a Circle
# Input: points = [[1,3],[3,3],[5,3],[2,2]], queries = [[2,3,1],[4,3,1],[1,1,2]]
# Output: [3,2,2]
# Explanation: The points and circles are shown above.
# queries[0] is the green circle, queries[1] is the red circle, and queries[2] is the blue circle.

# Better implementation: Runtime =  2444 ms ; Memory =  14.6 MB
class Solution:
    def countPoints(self, points: list[list[int]], queries: list[list[int]]) -> list[int]:
        return [sum(1 if (p_x-c_x)**2 + (p_y-c_y)**2 <= r**2 else 0 for p_x, p_y in points) for c_x, c_y, r in queries]

# Original implementation: Runtime =  2784 ms ; Memory =  14.5 MB
# class Solution:
#     def countPoints(self, points: list[list[int]], queries: list[list[int]]) -> list[int]:
#         count = [0] * len(queries)
#         for q in range(len(queries)):
#             c_x = queries[q][0]
#             c_y = queries[q][1]
#             for p in range(len(points)):
#                 length = (points[p][0]-c_x) ** 2 + (points[p][1]-c_y) ** 2
#                 if length <= queries[q][2] ** 2:
#                     count[q] += 1
#         return count

#Paramater
points = [[1,1],[2,2],[3,3],[4,4],[5,5]]
queries = [[1,2,2],[2,2,2],[4,3,2],[4,3,3]]
# points = [[1,3],[3,3],[5,3],[2,2]]
# queries = [[2,3,1],[4,3,1],[1,1,2]]

#Main Function
def main():
    output = Solution()
    print(output.countPoints(points, queries))

if __name__ =='__main__':
    main()


# -*- coding:utf-8 -*-
__author__ = 'zhengjiali'
__date__ = '2018/4/16 下午5:08'
from django.conf.urls import url

from .views import *
from api.test_views import CaseTestView

urlpatterns = [
    url(r'^query$', PlanQueryView.as_view(),name="query"),#plan列表搜索
    url(r'^(?P<plan_id>\w+)$', get_plan),#获取plan信息接口
    url(r'^add/$',new_plan),#新建计划step1
    url(r'^(?P<plan_id>\w+)/addCase$',plan_addCase), #新建计划step2
    url(r'^delete/$',plan_delete),#删除计划
    url(r'^(?P<plan_id>\w+)/exec$',ExecTask.as_view(),name='execTask'),#异步执行
    url(r'^(?P<plan_id>\w+)/task$',TaskView.as_view()),#同步调用
    url(r'^check/(?P<plan_id>\w+)/$',plan_check),#plan查看url
    url(r'^$',plan_list),
    url(r'^addinfo/$',plan_addinfo),
    url(r'^(?P<plan_id>\w+)/addcase/$', plan_addcase),
    url(r'^(?P<plan_id>\w+)/history/$',get_tasks),
]


import hashlib
import random
from contextlib import suppress
from collections import defaultdict
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from twilio.rest import Client
from twilio.twiml.messaging_response import Message, MessagingResponse

from matching.models import Owner, Messages
from register.constants import MessageStates
from register.models import VerificationAttempt, PhoneNumber

def spread_event_kwargs(f):
    def _inner(self, event, *args, **kwargs):
        kwargs.update(event.kwargs)
        return f(self, event, *args, **kwargs)

    return _inner

class ClaimNumberService:
    def __init__(self, phonenumber):
        self.phonenumber = phonenumber
        self.hash_code = 0

    @classmethod
    def create(cls, model):
        return cls(model)

    @staticmethod
    def hash(val):
        return hashlib.sha256(str(val).encode("utf-8")).hexdigest()

    @classmethod
    def verifiy(cls, verification, entered_code):
        if cls.hash(entered_code) == verification.hash:
            owner = Owner.objects.create(number=verification.number)
            return True
        else:
            return False

    @property
    def client(self):
        return Client(settings.TWILIO_KEYS.sid, settings.TWILIO_KEYS.token)

    def send_verification(self):
        try:
            self.phonenumber.owner
        except ObjectDoesNotExist:
            # Cool, we still need to set an owner
            code = random.randint(100_000, 999_999)
            self.hash_code = self.hash(code)
            message = self.client.messages.create(
                to=self.phonenumber.parsed_number,
                from_=settings.TWILIO_KEYS.number,
                body=f"This is your secret santa agent. What was that code we gave you?",
            )
            VerificationAttempt.objects.create(
                number=self.phonenumber, twilio_sid=message.sid, hash=self.hash_code
            )
            return code

class MessageLifecycle:

    PROMPTS = defaultdict(lambda: "Something went wrong, text Michael 🤯")

    def __init__(self, owner):
        self.owner = owner
        self.PROMPTS.update(
            {
                MessageStates.INITIAL: ["Tell me what your name is."],
                MessageStates.NAME: [
                    "What foundation would you like to have donated to? Just give enough context that someone could figure it out."
                ],
                MessageStates.FOUNDATION: [
                    "Thanks!",
                    "Anything you reply here will be fowarded to your secret santa.",
                    "I'll hold onto the messages you send before you get paired and send them off as well."
                ],
                MessageStates.FORWARDING: ["✉", "Sent!"],
            }
        )

        self.recievers = {
            MessageStates.INITIAL: self.set_name,
            MessageStates.NAME: self.set_foundation,
            MessageStates.FOUNDATION: self.fowarding_messages,
            MessageStates.FORWARDING: self.fowarding_messages,
        }

    @classmethod
    def load_from_number(cls, number):
        number = PhoneNumber.objects.get(parsed_number=number)
        try:
            return cls(number.owner)
        except ObjectDoesNotExist:
            return None

    @property
    def state(self):
        return MessageStates(self.owner.state)

    def set_name(self, name):
        self.owner.owner_name = name
        self.owner.state = MessageStates.NAME.value
        self.owner.save()

    def set_foundation(self, foundation):
        self.owner.foundation_message = foundation
        self.owner.state = MessageStates.FOUNDATION.value
        self.owner.save()

    def fowarding_messages(self, message):
        forward = MessageForwarder(self.owner)
        forward.send_message(message)

    def recieved_message(self, message):
        self.recievers[self.state](message)

    def get_prompt(self):
        messages = self.PROMPTS[self.state]
        response = MessagingResponse()
        list(map(response.message, messages))
        if self.state is MessageStates.FOUNDATION:
            self.owner.state = MessageStates.FORWARDING.value
            self.owner.save()
        return response

class MessageForwarder:
    def __init__(self, owner):
        self.owner = owner

    @property
    def active(self):
        with suppress(ObjectDoesNotExist):
            if self.owner.secret_santa_group is not None:
                return self.owner.secret_santa_group.active
            else:
                return False

    @property
    def send_to(self):
        return self.owner.your_secret.number.parsed_number

    @property
    def client(self):
        return Client(settings.TWILIO_KEYS.sid, settings.TWILIO_KEYS.token)

    def send_message(self, message):
        m = Messages.objects.create(sender=self.owner, message=message)
        self.send_message_attemp(m)

    def send_all_unsent_messages(self):
        if self.active:
            for message_not_sent in Messages.objects.filter(
                sender=self.owner, sent=False
            ):
                self._send_message(message_not_sent)

    def send_message_attemp(self, m):
        if self.active:
            self._send_message(m)

    def trigger_secret_santa_message(self):
        recipiant = self.owner.your_secret.owner_name
        foundation = self.owner.your_secret.foundation_message
        you = self.owner.owner_name
        self.client.messages.create(
            to=self.owner.number.parsed_number,
            from_=settings.TWILIO_KEYS.number,
            body=f"It's time for secret santa {you}!! 🎅🏽🤫 You got {recipiant}! They wanted you to donate to '{foundation}'."
        )

    def _send_message(self, m):
        self.client.messages.create(
            to=self.send_to, from_=settings.TWILIO_KEYS.number, body=m.message
        )
        m.sent = True
        m.save()


import pandas as pd
import numpy as np
import gdown

class Dataloader:
    def __init__(
            self,
            series: pd.Series,
            window_size: int,
            step_size: int,
            horizon: int,
            first_pred: int
    ):
        self.series = series
        self.window_size = window_size
        self.step_size = step_size
        self.horizon = horizon
        self.first_pred = first_pred
        assert self.first_pred > self.window_size
        feat_idx = []
        target_idx = []
        for i in range(self.first_pred, self.series.shape[0], self.step_size):
            feat_idx.append(range(i-self.horizon-self.window_size+1, i-self.horizon+1))
            target_idx.append(i)
        self.feat_idx = feat_idx
        self.target_idx = target_idx

    def __len__(self):
        return len(self.feat_idx)

    def __iter__(self):
        self.iter = 0
        return self

    def __next__(self):
        if self.iter < len(self.feat_idx):
            feat = self.series.iloc[self.feat_idx[self.iter]]
            target = self.series.iloc[self.target_idx[self.iter]]
            self.iter += 1
            return feat, target
        else:
            raise StopIteration

def _get_returns(data, assets, weights, from_date, to_date):
    data.index = pd.to_datetime(data.index)
    portfolio = (data[assets] * weights).sum(axis=1)
    from_mask = portfolio.index >= pd.to_datetime(from_date)
    to_mask = portfolio.index <= pd.to_datetime(to_date)
    return (portfolio / portfolio.shift() - 1)[from_mask & to_mask]

def stocks_returns(assets, weights, from_date, to_date):
    url = 'https://drive.google.com/file/d/1lLQV4oc30mo1_m39p4JXlpd1gV6pLw6A/view?usp=sharing'
    gdown.download(url, 'stocks.csv', fuzzy=True)
    data = pd.read_csv('stocks.csv', index_col=0)
    return _get_returns(data, assets, weights, from_date, to_date)

def commodities_returns(assets, weights, from_date, to_date):
    url = 'https://drive.google.com/file/d/1GFq1jcV00BjFEa7hmZSO1xD7K4j4gv3O/view?usp=sharing'
    gdown.download(url, 'commodities.csv', fuzzy=True)
    data = pd.read_csv('commodities.csv', index_col=0)
    return _get_returns(data, assets, weights, from_date, to_date)

def cryptocurrencies_returns(assets, weights, from_date, to_date):
    url = 'https://drive.google.com/file/d/1mPP5Vb57Jc2mYPeLYZPgAJM8ogjiguSO/view?usp=sharing'
    gdown.download(url, 'cryptocurrencies.csv', fuzzy=True)
    data = pd.read_csv('cryptocurrencies.csv', index_col=0)
    return _get_returns(data, assets, weights, from_date, to_date)


from selenium import webdriver as web
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import time ,asyncio
from  article_model import Article
import csv

URL = "http://e-pao.net/"

driver = web.Chrome(service= Service(ChromeDriverManager().install()))

async def extractDetail(link=None):
  try:
   if link is not None:
     await asyncio.sleep(1)
     print("Loading....")
     driver.get(link)
     xpath ='//*[@id="gen_panel"]'
     div = driver.find_element(By.XPATH,xpath)
     span =  div.find_element(By.XPATH,"./div[2]/p[1]/span[1]")
     p = div.find_element(By.XPATH,'./div[2]/p[@class="hlmheadlines"]')
     return Article(heading=span.text,article=p.text)
   else:
     print('Link is none')
     return None
  except Exception as e:
   print("On detail extract "+str(e))
   return None

#Using Selenium (active)
async def sel():

 try:

   driver.get(URL)
   driver.implicitly_wait(10)
   xpath ='//div[@id="mayek_con"]'
   div = driver.find_element(By.XPATH,value=xpath)
   anchor_list =  div.find_elements(By.TAG_NAME,'a')
   links = [
     {
         "title":anchor.find_element(By.CLASS_NAME,"hlmheadline").text.strip(),
         "link":anchor.get_attribute("href").strip()
      }
   for anchor in anchor_list]
   with open('articles.csv', mode='a', newline='', encoding='utf-8') as csv_file:
    # Define the CSV writer
    csv_writer = csv.DictWriter(csv_file, fieldnames=['heading', 'article'])

    # Write the header row
    csv_writer.writeheader()
    for link in links:
      articleObj = await extractDetail(link=link.get("link"))
      print(f"Headline {articleObj.heading}")
      print(f"Body {articleObj.article}")
      csv_writer.writerow({'heading': articleObj.heading, 'article': articleObj.article})

   driver.quit()

 except Exception as e:
  print(e)

#main call
if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(sel())
    loop.close()


from wsgiref.simple_server import make_server
import json

def show_index():
    return '欢迎访问'
def show_demo():
    return json.dumps({'name':'zhangsan'})
def show_register():
    with open('test.txt','r',encoding='utf8') as file:
        return file.read()

url={'/':show_index,
     '/demo':show_demo,
     '/register':show_register}

def demo_app(environ,start_response):
    #第0个参数，请求路径相关的环境
    #第1个参数，函数，返回响应头
    path=environ['PATH_INFO']
    status_code='200 OK'#默认状态码：200
    method=url.get(path)
    print(method)
    if method:
        response=method()
    else:
        status_code='404 Not Found'
        response='对不起，页面不存在'

    start_response(status_code,[('content-type','text/html;charset=utf8')])
    return [response.encode('utf8')]#列表返回元素:给浏览器的数据

if __name__=='__main__':
    httpd=make_server('',8080,demo_app)
    sa=httpd.socket.getsockname()
    print('Server HTTP on',sa[0],'port',sa[1],'...')
    #代码作用：打开电脑浏览器，并在浏览器输入
    #import webbrowser
    #webbrowser.open()

    httpd.handle_request()#处理一次请求


import os

import cv2
import numpy as np
from torch.utils.data import Dataset

from dataset_pytorch.data_utils import ToTensor

import tqdm

class RealTextureDataset(Dataset):

    def __getitem__(self, index):
        texture_img_path = self.data[index]
        texture_img = cv2.imread(texture_img_path)
        texture_img = cv2.cvtColor(texture_img, cv2.COLOR_BGR2RGB)

        texture_img = cv2.resize(texture_img, dsize=(self.img_size, self.img_size))

        texture_img = self.to_tensor(texture_img)

        return texture_img

    def __len__(self):
        return len(self.data)

    def __init__(self, data_path, img_size=64, normalize=True):
        self.data_path = data_path
        self.img_size = img_size
        self.normalize = normalize
        self.to_tensor = ToTensor(normalize=self.normalize)
        self.data = []
        self.generate_index()

    def generate_index(self):
        print('generating index')
        for root, dirs, files in os.walk(self.data_path):
            for name in tqdm.tqdm(files):
                if name.endswith('.jpg') and 'nongrey' in name:
                    self.data.append(os.path.join(root, name))

        print('finish generating index, found texture image: {}'.format(len(self.data)))