# import pprint
# from pathlib import Path
# yaml_dumper from linkml_runtime.dumpers
# import math
import re
from typing import Dict

import click
import pandas as pd
from linkml_runtime import SchemaView
from linkml_runtime.dumpers import json_dumper
from linkml_runtime.linkml_model import EnumDefinition

import curated_schema as cs

import logging
import click_log

multivalued_delimiter = ";"

col_mappings = {"alt": "ALT"}

# todo: verbosity/logging level stuck on debug?

logger = logging.getLogger(__name__)
click_log.basic_config(logger)

@click.command()
@click_log.simple_verbosity_option(logger)
@click.option("--tsv_in_file", type=click.Path(exists=True), required=True)
@click.option("--schema_file", type=click.Path(exists=True), required=True)
@click.option("--json_out_file", type=click.Path(), required=True)
@click.option("--project_name", required=True)
def cli(tsv_in_file: str, schema_file: str, project_name: str, json_out_file: str):
    """
    Break the aggregated TSV into samples, studies and a project
    Write back out as schema-compliant JSON.
    """

    agg_data = pd.read_csv(tsv_in_file, sep="\t", low_memory=False)

    schema = SchemaView(schema_file)

    studies = agg_data["study_name"].unique().tolist()
    studies.sort()

    obj_list = []
    # "ChengpingW_2017"
    for i in studies:
        logger.warning(i)
        current_study = make_study(i, agg_data, schema)
        obj_list.append(current_study)

    logger.warning("\nPlease wait, assembling project...")

    current_project = make_project(project_name)
    current_project.studies = obj_list

    # don't want these alpha-sorted
    # when does that happen?

    json_dumper.dump(current_project, json_out_file)

def make_sample(sample_dict: Dict, schema: SchemaView) -> cs.Sample:
    """
    Make a sample from one row of data.
    """
    # todo: refactor this schema extraction out of the function and pass it TO the function
    all_slots = schema.class_induced_slots("Sample")
    all_slot_names = [slot.alias for slot in all_slots]
    all_slots_dict = dict(zip(all_slot_names, all_slots))

    inferred_requireds = [i.alias for i in all_slots if i.required]
    remaining_slots = [i.alias for i in all_slots if i.alias not in inferred_requireds]

    ir_data = {i: sample_dict[i] for i in inferred_requireds}

    curator_hotfix = ir_data["curator"]
    # curator_hotfix = curator_hotfix.split(multivalued_delimiter)
    curator_hotfix = re.split("[;,]", curator_hotfix)

    # todo generalize this
    curator_hotfix = list(
        map(lambda x: x.replace("Giacomo_Damato", "Giacomo_DAmato"), curator_hotfix)
    )
    curator_hotfix = list(
        map(
            lambda x: x.replace("Mireia_VallesColomer", "Mireia_Valles-Colomer"),
            curator_hotfix,
        )
    )
    curator_hotfix = list(set(curator_hotfix))
    curator_hotfix.sort()

    ir_data["curator"] = curator_hotfix

    gender_hotfix = ir_data["gender"]
    gender_enum = schema.get_enum("Gender")
    # gender_pvs = list(gender_enum.permissible_values.keys())
    # if gender_hotfix not in gender_pvs:
    #     # logger.warning(f"provided gender: {gender_hotfix}; expected: {gender_pvs}")
    #     gender_hotfix = "NA"
    #
    # ir_data["gender"] = gender_hotfix

    age_hotfix = ir_data["age"]

    try:
        ir_data["age"] = int(age_hotfix)
    except ValueError:
        # logger.warning(f"    age value {age_hotfix} is not an integer")
        ir_data["age"] = "NA"

    current_sample = cs.Sample(**ir_data)

    for i in remaining_slots:

        current_range = all_slots_dict[i]["range"]

        current_range_element = schema.get_element(current_range)
        current_range_type = type(current_range_element).class_name

        current_value = sample_dict[i]
        multivalued = all_slots_dict[i]["multivalued"]

        if current_range == "string":
            # todo extremely short-sighted check for any-of boolean/status_enum
            if len(all_slots_dict[i]["any_of"]) > 0:
                # print(f"{i} {current_value}")
                if current_value == "yes":
                    current_sample[i] = True
                elif current_value == "no":
                    current_sample[i] = False
                else:
                    current_sample[i] = "NA"
            elif current_value and str(current_value) == current_value:
                if multivalued:
                    # todo parameterize this
                    val_list = current_value.split(";")
                    val_list = list(set(val_list))
                    val_list.sort()
                    for current_split in val_list:
                        current_sample[i].append(current_split)
                else:
                    current_sample[i] = current_value
        elif current_range == "float":
            nan_bool = pd.isna(float(current_value))
            # todo any multivalued float slots?
            if not nan_bool:
                try:
                    current_float = float(current_value)
                    current_sample[i] = current_float
                except ValueError:
                    pass
                    # logger.warning(f"    {i} value {current_value} is not a float")
            # else:
            #     logger.warning(f"    blank {i} value")

        elif current_range == "integer":
            # todo any multivalued integer slots?
            if current_value and current_value != "":
                nan_bool = pd.isna(current_value)
                if not nan_bool:
                    try:
                        current_int = int(current_value)
                        current_sample[i] = current_int
                    except ValueError:
                        pass
                        # logger.warning(f"    {i} value {current_value} is not an integer")
            # else:
            #     logger.warning(f"    blank {i} value")

        elif current_range_type == EnumDefinition.class_name:
            if str(current_value) == current_value:
                if multivalued:
                    logger.warning(
                        f"MULTIVALUED ENUM: {i}; current_value: {current_value}; multivalued: {multivalued}"
                    )
                else:
                    if ":" in current_value:
                        logger.warning(
                            f"ERRONEOUS SINGLE-VALUED ENUM: {i}; current_value: {current_value}; multivalued: {multivalued}"
                        )
                    else:
                        current_sample[i] = current_value

        # elif current_range == "boolean":
        #     if current_value and current_value != "":
        #         current_sample[i] = current_value in ["yes"]

        # OTHER slot: premature; current_range: boolean; current range type:type_definition; current_value: nan; multivalued: None

        else:
            logger.warning(
                f"OTHER slot: {i}; current_range: {current_range}; current range type:{current_range_type}; current_value: {current_value}; multivalued: {multivalued}"
            )

    return current_sample

def make_study(
        study_name: str, agg_frame: pd.DataFrame, schema: SchemaView
) -> cs.Study:
    """
    Make a collection of all samples with a shared study name.
    """
    current_data = agg_frame[agg_frame["study_name"] == study_name]
    # logger.info(current_data.head())
    current_study = cs.Study(study_name=study_name)
    sample_list = current_data.to_dict("records")
    object_list = []
    for i in sample_list:
        current_sample = make_sample(i, schema)
        object_list.append(current_sample)

    current_study.samples = object_list

    return current_study

def make_project(project_name: str) -> cs.Project:
    """
    Make a collection of all studies in the curatedMetagenomicDataCuration directory.
    """
    current_project = cs.Project(project_name=project_name)
    return current_project

if __name__ == "__main__":
    cli()


from tensorflow.keras.layers import *
from tensorflow.keras.models import Model, load_model

def baseModel(data_shape):
    input_data = Input(shape=data_shape)

    conv2d_1 = Conv2D(64, kernel_size=(3, 3), padding='same', activation='relu')(input_data)
    mpool2d_1 = MaxPool2D(pool_size=(2, 2), strides=2)(conv2d_1)  # (49,49)

    conv2d_2 = Conv2D(96, kernel_size=(3, 3), padding='same', activation='relu')(mpool2d_1)
    mpool2d_2 = MaxPool2D(pool_size=(2, 2), strides=2)(conv2d_2)  # (24,24)

    conv2d_3 = Conv2D(128, kernel_size=(3, 3), padding='same', activation='relu')(mpool2d_2)

    conv2d_4 = Conv2D(128, kernel_size=(3, 3), padding='same', activation='relu')(conv2d_3)
    mpool2d_3 = MaxPool2D(pool_size=(2, 2), strides=2)(conv2d_4)  # (12,12)

    conv2d_5 = Conv2D(256, kernel_size=(3, 3), padding='same', activation='relu')(mpool2d_3)

    conv2d_6 = Conv2D(256, kernel_size=(3, 3), padding='same', activation='relu')(conv2d_5)

    # output
    flatten = Flatten()(conv2d_6)
    drop1 = Dropout(0.6)(flatten)
    dense1 = Dense(128, activation='relu')(drop1)
    drop2 = Dropout(0.6)(dense1)
    output = Dense(7, activation='softmax')(drop2)

    model = Model(inputs=input_data, outputs=output)
    return model


def solution(s):
    tmp = []
    for i in s:
        if i == "(":
            tmp.append(i)
        else:
            try:
                tmp.pop()
            except:
                return False
    return True if len(tmp) == 0 else False

print(solution("(()()))"))

프로그래머스/고득점 키트/02.스택_큐/stack_que_02_올바른_괄호.py


#!/usr/bin/env python
# -- coding:utf-8 --
# hyean.qin@gmail.com
from frienddata import *
import traceback

def show_list():
    for n in names:
        print(f"{n[0]} {n[1]}")

def show_person(id):
    """
    id: 用户id
    """
    show_record(id)

def find_name(name):
    """
    name: 输入的名字或者ID
    """
    # 根据输入的名字，查询是否在当前列表中，如果在，返回
    if name.isdigit():
        find_name_byId(name)
    else:
        fname = [n for n in names if n[1] == name]
        if len(fname) == 0:
            print(f"系统不存在您要查找的用户，请检查输入或与用户确认是否已注册。")
        elif len(fname) == 1:
            print(f"您要查找的用户信息以及交互记录如下：{fname[0]}")
            show_score_summary(fname[0][0])
            show_person(fname[0][0])
        else:
            for n in fname:
                print(f"{n[0]}. {n[1]}")

            conf = True
            while conf:
                num = int(input(f"请输入名单中{fname}的一个序号，查看相关用户的具体记录： "))
                fname_id = [x[0] for x in fname]
                if int(num) not in fname_id:
                    print("您输入的用户序号不在上述名称中， 为您找到该用户的信息如下：")
                    find_name_byId(num)
                else:
                    find_name_byId(num)
                conf = input(f"如果还想继续查看用户记录，请输入yes；输入其他将不再继续查看用户记录： ")
                if conf != 'yes':
                    conf = False

def find_name_byId(num):
    """根据输入数字，判断是否存在该用户，如果存在，进而查出该用户信息"""
    try:
        num = int(num)
        if num not in range(1, len(names)+1):
            print(f"系统不存在您要查找的用户，请检查输入或与用户确认是否已注册。")
        else:
            print(f"您要查找的用户信息以及交互记录如下：{(num, names_dict[num])}")
            show_score_summary(num)
            show_person(num)
    except:
        print("Value Error")

def show_record(id):
    """
    id: 用户id
    """
    personal_records = records_dict[id]
    if personal_records:
        for r in personal_records:
            print(f"{r[0][1]}（No.{r[0][0]}） {r[1]}我, 分数{r[-1]}")
    else:
        print(f"暂无此用户的行为记录。")

def show_score_summary(id):
    """
    id: 用户id
    """
    if id in score_dict:
        for k,v in score_dict[id].items():
            print(f"{names_dict[k]}:{v}", end=" ")
        print("")
    else:
        print(f"暂无此用户的行为记录。")

def show_all_records():
    for r in records:
        print(f"{r[0][1]}(No.{r[0][0]}) {r[1]} {r[2][1]}（No.{r[2][0]}）, {r[-1]}")

if __name__ == '__main__':
    # show_all_records()
    print(random.sample.__doc__)


import os
import inspect
import traceback

from openpype.lib import Logger
from openpype.lib.python_module_tools import (
    modules_from_path,
    classes_from_module,
)

log = Logger.get_logger(__name__)

class DiscoverResult:
    """Result of Plug-ins discovery of a single superclass type.

    Stores discovered, duplicated, ignored and abstract plugins and file paths
    which crashed on execution of file.
    """

    def __init__(self, superclass):
        self.superclass = superclass
        self.plugins = []
        self.crashed_file_paths = {}
        self.duplicated_plugins = []
        self.abstract_plugins = []
        self.ignored_plugins = set()
        # Store loaded modules to keep them in memory
        self._modules = set()

    def __iter__(self):
        for plugin in self.plugins:
            yield plugin

    def __getitem__(self, item):
        return self.plugins[item]

    def __setitem__(self, item, value):
        self.plugins[item] = value

    def add_module(self, module):
        """Add dynamically loaded python module to keep it in memory."""
        self._modules.add(module)

    def get_report(self, only_errors=True, exc_info=True, full_report=False):
        lines = []
        if not only_errors:
            # Successfully discovered plugins
            if self.plugins or full_report:
                lines.append(
                    "*** Discovered {} plugins".format(len(self.plugins))
                )
                for cls in self.plugins:
                    lines.append("- {}".format(cls.__class__.__name__))

            # Plugin that were defined to be ignored
            if self.ignored_plugins or full_report:
                lines.append("*** Ignored plugins {}".format(len(
                    self.ignored_plugins
                )))
                for cls in self.ignored_plugins:
                    lines.append("- {}".format(cls.__name__))

        # Abstract classes
        if self.abstract_plugins or full_report:
            lines.append("*** Discovered {} abstract plugins".format(len(
                self.abstract_plugins
            )))
            for cls in self.abstract_plugins:
                lines.append("- {}".format(cls.__name__))

        # Abstract classes
        if self.duplicated_plugins or full_report:
            lines.append("*** There were {} duplicated plugins".format(len(
                self.duplicated_plugins
            )))
            for cls in self.duplicated_plugins:
                lines.append("- {}".format(cls.__name__))

        if self.crashed_file_paths or full_report:
            lines.append("*** Failed to load {} files".format(len(
                self.crashed_file_paths
            )))
            for path, exc_info_args in self.crashed_file_paths.items():
                lines.append("- {}".format(path))
                if exc_info:
                    lines.append(10 * "*")
                    lines.extend(traceback.format_exception(*exc_info_args))
                    lines.append(10 * "*")

        return "\n".join(lines)

    def log_report(self, only_errors=True, exc_info=True):
        report = self.get_report(only_errors, exc_info)
        if report:
            log.info(report)

class PluginDiscoverContext(object):
    """Store and discover registered types nad registered paths to types.

    Keeps in memory all registered types and their paths. Paths are dynamically
    loaded on discover so different discover calls won't return the same
    class objects even if were loaded from same file.
    """

    def __init__(self):
        self._registered_plugins = {}
        self._registered_plugin_paths = {}
        self._last_discovered_plugins = {}
        # Store the last result to memory
        self._last_discovered_results = {}

    def get_last_discovered_plugins(self, superclass):
        """Access last discovered plugin by a subperclass.

        Returns:
            None: When superclass was not discovered yet.
            list: Lastly discovered plugins of the superclass.
        """

        return self._last_discovered_plugins.get(superclass)

    def discover(
        self,
        superclass,
        allow_duplicates=True,
        ignore_classes=None,
        return_report=False
    ):
        """Find and return subclasses of `superclass`

        Args:
            superclass (type): Class which determines discovered subclasses.
            allow_duplicates (bool): Validate class name duplications.
            ignore_classes (list): List of classes that will be ignored
                and not added to result.
            return_report (bool): Output will be full report if set to 'True'.

        Returns:
            Union[DiscoverResult, list[Any]]: Object holding successfully
                discovered plugins, ignored plugins, plugins with missing
                abstract implementation and duplicated plugin.
        """

        if not ignore_classes:
            ignore_classes = []

        result = DiscoverResult(superclass)
        plugin_names = set()
        registered_classes = self._registered_plugins.get(superclass) or []
        registered_paths = self._registered_plugin_paths.get(superclass) or []
        for cls in registered_classes:
            if cls is superclass or cls in ignore_classes:
                result.ignored_plugins.add(cls)
                continue

            if inspect.isabstract(cls):
                result.abstract_plugins.append(cls)
                continue

            class_name = cls.__name__
            if class_name in plugin_names:
                result.duplicated_plugins.append(cls)
                continue
            plugin_names.add(class_name)
            result.plugins.append(cls)

        # Include plug-ins from registered paths
        for path in registered_paths:
            modules, crashed = modules_from_path(path)
            for item in crashed:
                filepath, exc_info = item
                result.crashed_file_paths[filepath] = exc_info

            for item in modules:
                filepath, module = item
                result.add_module(module)
                for cls in classes_from_module(superclass, module):
                    if cls is superclass or cls in ignore_classes:
                        result.ignored_plugins.add(cls)
                        continue

                    if inspect.isabstract(cls):
                        result.abstract_plugins.append(cls)
                        continue

                    if not allow_duplicates:
                        class_name = cls.__name__
                        if class_name in plugin_names:
                            result.duplicated_plugins.append(cls)
                            continue
                        plugin_names.add(class_name)

                    result.plugins.append(cls)

        # Store in memory last result to keep in memory loaded modules
        self._last_discovered_results[superclass] = result
        self._last_discovered_plugins[superclass] = list(
            result.plugins
        )
        result.log_report()
        if return_report:
            return result
        return result.plugins

    def register_plugin(self, superclass, cls):
        """Register a directory containing plug-ins of type `superclass`

        Arguments:
            superclass (type): Superclass of plug-in
            cls (object): Subclass of `superclass`
        """

        if superclass not in self._registered_plugins:
            self._registered_plugins[superclass] = list()

        if cls not in self._registered_plugins[superclass]:
            self._registered_plugins[superclass].append(cls)

    def register_plugin_path(self, superclass, path):
        """Register a directory of one or more plug-ins

        Arguments:
            superclass (type): Superclass of plug-ins to look for during
                discovery
            path (str): Absolute path to directory in which to discover
                plug-ins
        """

        if superclass not in self._registered_plugin_paths:
            self._registered_plugin_paths[superclass] = list()

        path = os.path.normpath(path)
        if path not in self._registered_plugin_paths[superclass]:
            self._registered_plugin_paths[superclass].append(path)

    def registered_plugin_paths(self):
        """Return all currently registered plug-in paths"""
        # Return shallow copy so we the original data can't be changed
        return {
            superclass: paths[:]
            for superclass, paths in self._registered_plugin_paths.items()
        }

    def deregister_plugin(self, superclass, plugin):
        """Opposite of `register_plugin()`"""
        if superclass in self._registered_plugins:
            self._registered_plugins[superclass].remove(plugin)

    def deregister_plugin_path(self, superclass, path):
        """Opposite of `register_plugin_path()`"""
        self._registered_plugin_paths[superclass].remove(path)

class _GlobalDiscover:
    """Access to global object of PluginDiscoverContext.

    Using singleton object to register/deregister plugins and plugin paths
    and then discover them by superclass.
    """

    _context = None

    @classmethod
    def get_context(cls):
        if cls._context is None:
            cls._context = PluginDiscoverContext()
        return cls._context

def discover(
    superclass,
    allow_duplicates=True,
    ignore_classes=None,
    return_report=False
):
    """Find and return subclasses of `superclass`

    Args:
        superclass (type): Class which determines discovered subclasses.
        allow_duplicates (bool): Validate class name duplications.
        ignore_classes (list): List of classes that will be ignored
            and not added to result.
        return_report (bool): Output will be full report if set to 'True'.

    Returns:
        Union[DiscoverResult, list[Any]]: Object holding successfully
            discovered plugins, ignored plugins, plugins with missing
            abstract implementation and duplicated plugin.
    """

    context = _GlobalDiscover.get_context()
    return context.discover(
        superclass,
        allow_duplicates,
        ignore_classes,
        return_report
    )

def get_last_discovered_plugins(superclass):
    context = _GlobalDiscover.get_context()
    return context.get_last_discovered_plugins(superclass)

def register_plugin(superclass, cls):
    context = _GlobalDiscover.get_context()
    context.register_plugin(superclass, cls)

def register_plugin_path(superclass, path):
    context = _GlobalDiscover.get_context()
    context.register_plugin_path(superclass, path)

def deregister_plugin(superclass, cls):
    context = _GlobalDiscover.get_context()
    context.deregister_plugin(superclass, cls)

def deregister_plugin_path(superclass, path):
    context = _GlobalDiscover.get_context()
    context.deregister_plugin_path(superclass, path)


from flask import request
from flask_restx import Resource, fields

from app.application import api, root_ref
from app.data_handler import is_valid_sensor_data, process_sensor_data

# Define data models
sensor_data_model = api.model('SensorData', {
    'data': fields.String(description='The sensor data to update (e.g., "25.5,60.0")', required=True)
})

response_model = api.model('Response', {
    'message': fields.String(description='The status message ("Pump turned on" or "Pump turned off")')
})

@api.route('/update_data')
class UpdateData(Resource):
    @api.expect(sensor_data_model)
    @api.response(200, 'Sensor data successfully updated')
    @api.response(400, 'Invalid sensor data format')
    @api.response(500, 'An error occurred')
    def post(self):
        data = api.payload['data']
        try:
            if not is_valid_sensor_data(data):
                return "Invalid sensor data format", 400

            sensors_data = process_sensor_data(data)

            root_ref.update(sensors_data)

            return "Sensor data updated", 200
        except Exception as e:
            raise e

@api.route('/control_pump')
class ControlPump(Resource):
    @api.response(200, 'Successfully retrieved pump status', response_model)
    @api.response(400, 'An error occurred or invalid request')
    def get(self):
        try:
            pump_status = root_ref.child("sensor1").child("pump_status").get()

            if pump_status is not None:
                pump_status = pump_status == "on"  # Convert to a boolean

                # Depending on the "pump_status" value, take appropriate actions to control the pump

                response_message = ""
                if pump_status:
                    response_message = "Pump turned on"
                else:
                    response_message = "Pump turned off"
            else:
                response_message = "Pump status not found in the database"

            return {'message': response_message}, 200
        except Exception as e:
            raise e


from bookingsystem.Classes.PortalClasses.database_updater import DatabaseUpdater
from bookingsystem.models import UserRestaurantLink, Tables
from datetime import datetime

class RestaurantTablesUpdater():
    def update_tables(self, request):
        global table_id
        current_user = request.user.id
        restaurant_id = UserRestaurantLink.objects.filter(user_id=current_user).values_list('restaurant_id',
                                                                                flat=True).first()
        database_updater = DatabaseUpdater()
        for response in request.POST:
            if response != 'csrfmiddlewaretoken':
                parts = response.split('-')
                column_name = parts[0]
                column_value = request.POST[response]
                table_id = parts[1][8:]
                database_updater.update_model_instance(Tables, restaurant_id, table_id, column_name,
                                                       column_value)
        database_updater.update_model_instance(Tables, restaurant_id, table_id, 'updated_at',
                                               datetime.now())


import re

my_dict = {}

with open(r"lesson_5/text_6.txt", "r") as my_file:
    for i in my_file.readlines():
        subject = [v for v in re.findall(r'^\w+', i)]
        sum_hours = sum([int(v) for v in re.findall(r'\d+', i)])
        my_dict[f'{subject[0]}'] = sum_hours

print(my_dict)


import copy
import inkex
import os
import random
import sys
from lxml import etree

inkex.localize()

FILTER_ID = "mdshadow_filter%d"

def xpath(e, path):
    return e.xpath(path, namespaces=inkex.NSS)

class MaterialShadow(inkex.Effect):
    def __init__(self):
        inkex.Effect.__init__(self)
        self.OptionParser.add_option("-e", "--elevation", action="store", type="int", dest="elevation", default=1, help="Elevation of the shadow.")

    def effect(self):
        # Parse filters.svg.
        filter_path = os.path.join(os.environ["HOME"], ".config", "inkscape", "extensions", "filters.svg")
        try:
            stream = open(filter_path, "r")
        except Exception:
            inkex.errormsg(_("Unable to open specified file: %s") % filter_path)
            sys.exit()
        p = etree.XMLParser(huge_tree=True)
        filter_doc = etree.parse(stream, parser=p)
        stream.close()

        # Get elevation value.
        elevation = self.options.elevation

        filter_id = self.ensure_filter(filter_doc, elevation)
        if len(self.selected) == 0:
            pass
        else:
            for i, element in self.selected.iteritems():
                self.add_shadow(element, filter_id)

    def get_label(self, element):
        xpath_result = xpath(element, "@inkscape:label")
        assert len(xpath_result)
        return xpath_result[0]

    def ensure_new_id(self, element, exclude_ids):
        orig_id = element.get("id")
        while element.get("id") in exclude_ids:
            element.set("id", "%s_%d" % (orig_id, random.randint(1000, 9999)))

    def ensure_filter(self, filter_doc, elevation):
        root = self.document.getroot()
        filter_root = filter_doc.getroot()

        # Create defs if non existent.
        root_defs = xpath(root, "//svg:svg/svg:defs")
        if len(root_defs) == 0:
            root.insert(0, etree.Element("defs"))
        root_defs = xpath(root, "//svg:svg/svg:defs")
        assert len(root_defs)

        # Add filter.
        root_filter_defs = xpath(root, "//svg:svg/svg:defs/svg:filter")
        root_filter_ids = []
        filter_e = xpath(filter_root, "//svg:svg/svg:defs/svg:filter[@id=\"%s\"]" % FILTER_ID % elevation)
        assert len(filter_e)
        filter_e_label = self.get_label(filter_e[0])

        for e in root_filter_defs:
            root_filter_ids.append(e.get("id"))
            e_label = self.get_label(e)
            if filter_e_label == e_label:
                return e.get("id")

        self.ensure_new_id(filter_e[0], root_filter_ids)
        root_defs[0].append(filter_e[0])
        return filter_e[0].get("id")

    def add_shadow(self, element, filter_id):
        element.set("filter", "url(#%s)" % filter_id)

if __name__ == "__main__":
    ex = MaterialShadow()
    ex.affect()


import os
from transformers import DistilBertForMaskedLM
from transformers import AutoTokenizer

if __name__ == '__main__':
    # CONSTANT(S):
    MODELS = sorted([f.path for f in os.scandir('./distilbert/') if f.is_dir()])

    # variable(s):
    model_name_template = 'doubleblind'

    for m in MODELS:
        model = DistilBertForMaskedLM.from_pretrained(m, local_files_only=True)
        tokenizer = AutoTokenizer.from_pretrained(m, local_files_only=True)

        print('Pushing model {} to hub\n'.format(m))
        model.push_to_hub(model_name_template + m[-4:])
        tokenizer.push_to_hub(model_name_template + m[-4:])


class Crop():
  """A generic food crop."""
  #constructor
  def __init__(self,growth_rate, light_need, water_need):
    #set attributres
    self._growth = 0
    self._days_growing = 0
    self._growth_rate = growth_rate
    self._light_need = light_need
    self._water_need = water_need
    self._status = 'Seed'
    self._type = 'Generic'
    #prefixed with '_' to indicate private variable access only within class

new_crop = Crop(1,4,3)
print(new_crop._status)
print(new_crop._light_need)


import hamburger_music.libpoetry
import recipe.rcp

__author__ = 'elon'

class Recipe(recipe.rcp.BaseRecipe):
    def run(self, input, api_key, number_of_lines=1, max_lines_per_video=2, randomness=2, minimum_length=3,
            wordrange=range(3, 6), capitalize=True, whitelist=(), blacklist=(), languages=("en",)):

        p = hamburger_music.libpoetry.Poet(api_key, wordrange, capitalize, whitelist, blacklist,
                                           randomness=max_lines_per_video, languages=languages,
                                           minsublength=minimum_length)

        yield p.makePoem(number_of_lines)


from django.db import connection
from django.shortcuts import render, redirect
from django.views.generic import View

from community.models import Community
from registration.models import Account
from . import models
from django.utils import timezone
from django.contrib.auth.mixins import LoginRequiredMixin

from .models import Note, Like

def change_comment(request, note_pk, back_url, *args, **kwargs):
    try:
        note = Note.objects.get(pk=note_pk)
        if note.can_comment:
            note.can_comment = False
        else:
            note.can_comment = True

        note.save()
    except Note.DoesNotExist:
        pass

    return redirect(back_url, *args, **kwargs)

def like(request, note_pk, back_url, *args, **kwargs):  # Переделать Лайк#
    user = request.user
    note = Note.objects.get(pk=note_pk)
    try:
        liked = Like.objects.get(user=user, note=note)
        liked.delete()
    except Like.DoesNotExist:
        Like(user=user, note=note).save()

    return redirect(back_url, *args, **kwargs)
    # Отправить уведомление

def delete_note(request, note_pk, back_url, *args, **kwargs):
    try:
        note = Note.objects.get(pk=note_pk)
        note.delete()
    except Note.DoesNotExist:
        pass
    return redirect(back_url, *args, **kwargs)

def leave_comment(request):
    data = request.POST
    note = models.Note.objects.get(pk=int(data.get('form')))
    if data.get('text') or request.FILES.get('img'):
        comment = models.Comment(
            author=request.user,
            text=data.get('text'),
            note=note,
            image=request.FILES.get('img'),
            post_time=timezone.now()
        )
        comment.save()
    # Отправить уведомление

class NewsView(LoginRequiredMixin, View):
    login_url = 'registration'

    def get(self, request):
        context = {}
        return render(request, 'news/news.html', context)

    def post(self, request):
        pass

class AddNoteView(LoginRequiredMixin, View):
    login_url = 'registration'

    def get(self, request, name=None):
        context = {}
        if name:
            try:
                group = Community.objects.get(admin=request.user, short_name=name)
                context['author'] = group
                context['back_url'] = 'group'
                context['back_url_data'] = group.short_name
            except Community.DoesNotExist:
                return redirect('news')
        else:
            context['author'] = request.user
            context['back_url'] = 'profile'
            context['back_url_data'] = request.user.username
        return render(request, 'news/add_note.html', context)

    def post(self, request, name=None):
        files = request.FILES.getlist('note_img')
        if name:
            try:
                group = Community.objects.get(admin=request.user, short_name=name)
                try:
                    note = models.Note(group=group, post_time=timezone.now(),
                                       text=request.POST.get('note_text'),
                                       image1=files[0], image2=files[1], image3=files[2])
                except IndexError:
                    try:
                        note = models.Note(group=group, post_time=timezone.now(),
                                           text=request.POST.get('note_text'),
                                           image1=files[0], image2=files[1])
                    except IndexError:
                        try:
                            note = models.Note(group=group, post_time=timezone.now(),
                                               text=request.POST.get('note_text'),
                                               image1=files[0])
                        except IndexError:
                            if request.POST.get('note_text'):
                                note = models.Note(group=group, post_time=timezone.now(),
                                                   text=request.POST.get('note_text'),
                                                   )
                            else:
                                return redirect('group', group.short_name)

            except Community.DoesNotExist:
                return redirect('news')
        else:
            try:
                note = models.Note(author=request.user, post_time=timezone.now(), text=request.POST.get('note_text'),
                                   image1=files[0], image2=files[1], image3=files[2])
            except IndexError:
                try:
                    note = models.Note(author=request.user, post_time=timezone.now(),
                                       text=request.POST.get('note_text'),
                                       image1=files[0], image2=files[1])
                except IndexError:
                    try:
                        note = models.Note(author=request.user, post_time=timezone.now(),
                                           text=request.POST.get('note_text'),
                                           image1=files[0])
                    except IndexError:
                        if request.POST.get('note_text'):
                            note = models.Note(author=request.user, post_time=timezone.now(),
                                               text=request.POST.get('note_text'),
                                               )
                        else:
                            return redirect('profile', request.user.username)

        note.save()
        if name:
            return redirect('group', group.short_name)
        else:
            return redirect('profile', request.user.username)

class NoteView(LoginRequiredMixin, View):
    login_url = 'registration'

    # такие аргументы нужны для того чтобы не создавать отдельную функцию для лайка...
    def get(self, request, note_id, url, name=None):
        try:
            note = models.Note.objects.get(pk=note_id)
        except Note.DoesNotExist:
            if name:
                return redirect(url, name)
            else:
                return redirect(url)
        user = note.author
        context = {
            'url': 'note',
            'note': note,
            'user': user,
            'back_url': url,
            'name': name
        }
        return render(request, 'news/note.html', context)

    def post(self, request, *args, **kwargs):
        leave_comment(request)
        return redirect('note', *args, **kwargs)


# -*- coding: utf-8 -*-
"""
Created on Wed Apr 10 17:57:02 2019

@author: Administrator
"""

from pyhht.emd import EMD
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pyhht.visualization import plot_imfs

# 读取数据
#dataset = pd.read_csv('data_day.csv')
stock_dir='../dataset/AAPL.csv'
dataset = pd.read_csv(open(stock_dir),header=0)
dataset=dataset[::-1]
for col in dataset.columns:
    dataset=dataset['Open']
    data = dataset.values
    s = data.ravel()
    #emd
    decomposer = EMD(s)
    IMF = decomposer.decompose()
    print(IMF.shape)
    imf_data = pd.DataFrame(IMF.T)
    imf_data.to_csv('../dataset/emd/emd_AAPL_'+str(col)+'.csv')
    #绘制分解图
    plot_imfs(s,IMF)


import time
from urllib import parse

from flask import Blueprint, request, redirect
from flask_login import current_user

from modules.links.helpers import get_shortlink
from shared_helpers.events import enqueue_event

routes = Blueprint('routing', __name__,
                   template_folder='../../static/templates')

def queue_event(followed_at, shortlink_id, destination, accessed_via, email=None):
  enqueue_event('link_follow.created',
                'link_follow',
                {'link_id': shortlink_id,
                 'user_email': email or current_user.email,
                 'access_method': accessed_via,
                 'destination': destination},
                followed_at)

def force_to_original_url():
  return redirect(str('%s://%s?tr=ot' % (request.args.get('sc'), request.path[1:])))

@routes.route('/<path:path>', methods=['GET'])
def get_go_link(path):
  requested_at = time.time()

  provided_shortpath = parse.unquote(path.strip('/'))
  shortpath_parts = provided_shortpath.split('/', 1)
  shortpath = '/'.join([shortpath_parts[0].lower()] + shortpath_parts[1:])

  if not getattr(current_user, 'email', None):
    if request.args.get('s') == 'crx' and request.args.get('sc'):
      # see: go/484356182846856
      return force_to_original_url()

    return redirect('/_/auth/login?%s' % parse.urlencode({'redirect_to': request.full_path}))

  matching_shortlink, destination = get_shortlink(current_user.organization, shortpath)

  if matching_shortlink:
    queue_event(requested_at,
                matching_shortlink.get_id(),
                destination,
                request.args.get('s') or 'other')
    return redirect(str(destination))
  elif request.args.get('s') == 'crx' and request.args.get('sc'):
    return force_to_original_url()
  else:
    return redirect(
      '%s/?%s'
      % ('http://localhost:5007' if request.host.startswith('localhost') else '',
         parse.urlencode({'sp': shortpath}))
    )


try:
    import urequests as requests
except ImportError:
    import requests
import json as j
from machine import ADC
import gc
adc = ADC(0)
value = {'value': adc.read()}

url = "https://nodered-47.herokuapp.com/post"

def post(value):
    global  url
    print(value)
    temp=url+'?value='+str(value['value'])
    r = requests.post(temp)
    del temp
    #print(r)
    #print(r.content)
    print(r.text)
    #print(r.content)
    #print(r.json())
    r.close()

while True:
    if value != adc.read():
        value = {'value': adc.read()}
        print('sendin the post request')
        post(value)
        gc.collect()


'''
Input:  {9, 6, 4, 5, 8}
Output:  2
The two inversions are {9, 6, 4} and {9, 6, 5}
'''

'''
遍历考虑中间的数字，检查前面的是否大于mid,后面的是否小于mid
'''

# Returns count of inversions
# of size 3
def getInvCount(arr, n):
    # Initialize result
    invcount = 0

    for i in range(1, n - 1):

        # Count all smaller elements
        # on right of arr[i]
        small = 0
        for j in range(i + 1, n):
            if (arr[i] > arr[j]):
                small += 1

        # Count all greater elements
        # on left of arr[i]
        great = 0;
        for j in range(i - 1, -1, -1):
            if (arr[i] < arr[j]):
                great += 1

        # Update inversion count by
        # adding all inversions that
        # have arr[i] as middle of
        # three elements
        invcount += great * small

    return invcount

# Driver program to test above function
arr = [8, 4, 2, 1]
n = len(arr)
print("Inversion Count :", getInvCount(arr, n))

interview/Count Inversions of size three in a given array---Hackerrank.py


from celery.result import AsyncResult
from django.contrib.auth import login
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import FileResponse, HttpResponseForbidden, JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse
from django.views.generic import View

from .forms import ColumnCreateForm, DataSetCreateForm, SchemaCreateForm
from .models import Column, DataSet, Schema
from .tasks import form_fake_data

def register_user(request):
    """
    Display registation form, create new user and if success redirect to index page.
    """
    if request.method == "GET":
        return render(request, 'registration/register.html')

    elif request.method == "POST":
        form = UserCreationForm(request.POST)
        if form.is_valid():
            user = form.save()
            login(request, user)
            return redirect(reverse('schemas:index'))

class IndexView(LoginRequiredMixin, View):
    """
    Responsible for index page. Display all user's schemas.
    """
    def get(self, request):
        schemas = Schema.objects.filter(confirmed=True, owner=request.user)
        return render(request, 'schemas/index.html', {'schemas': schemas})

class CreateSchemaView(LoginRequiredMixin, View):
    """
    Responsible for schema and columns creation page.
    Display forms and newly create columns.
    """
    def get(self, request):
        schema, _ = Schema.objects.get_or_create(owner=request.user, confirmed=False)
        columns = schema.column_set.all()
        form, col_form = SchemaCreateForm(instance=schema), ColumnCreateForm()
        context = {'schema': schema, 'columns': columns, 'form': form, 'col_form': col_form}
        return render(request, 'schemas/new_schema.html', context)

    def post(self, request):
        form = SchemaCreateForm(data=request.POST)

        if form.is_valid():
            schema = Schema.objects.get(owner=request.user, confirmed=False)
            schema.name = form.cleaned_data['name']
            schema.confirmed = True
            schema.save()
            return redirect(schema)

class ColumnCreateView(LoginRequiredMixin, View):
    """
    Responsible for column creation. Receive a POST AJAX request,
    create new column model in database and send json data to front end.
    """
    def post(self, request):
        if request.is_ajax():
            form = ColumnCreateForm(data=request.POST)

            if form.is_valid():
                column = form.save(commit=False)
                column.schema_id = request.POST.get('schema')
                column.save()
                response = {
                    'csrfmiddlewaretoken': request.POST.get('csrfmiddlewaretoken'),
                    'id': column.pk,
                    'name': column.name,
                    'type': column.type,
                    'start': column.start,
                    'end': column.end
                }
                return JsonResponse(response, status=202)

        else:
            return HttpResponseForbidden(status=403)

class ColumnDeleteView(LoginRequiredMixin, View):
    """
    Responsible for column removal. Receive a POST AJAX request,
    remove column from database and send json data to front end.
    """
    def post(self, request, column_id):
        if request.is_ajax():
            column = Column.objects.filter(id=column_id)
            column.delete()
            return JsonResponse({'success': column_id}, status=202)

        else:
            return HttpResponseForbidden(status=403)

class DeleteSchemaView(LoginRequiredMixin, View):
    """
    Responsible for delete schemas. Receive a POST AJAX request
    and delete schema in database and send json data to front end.
    """
    def post(self, request, schema_id):
        if request.is_ajax():
            schema = Schema.objects.filter(id=schema_id, owner=request.user)
            schema.delete()
            return JsonResponse({'succsess': schema_id}, status=200)

class SchemaView(LoginRequiredMixin, View):
    """
    Responsible for schema detail and related datasets page.
    Display datasets table and create dataset form.
    Receive a POST AJAX request, create new dataset model,
    start Celery task to form CSV file and send json data to front end.
    """
    def get(self, request, schema_id):
        schema = get_object_or_404(Schema, pk=schema_id, owner=request.user)
        data_sets = schema.dataset_set.all()
        form = DataSetCreateForm()
        context = {'schema': schema, 'data_sets': data_sets, 'form': form}
        return render(request, 'schemas/schema_detail.html', context)

    def post(self, request, schema_id):
        if request.is_ajax():
            schema = Schema.objects.get(id=schema_id, owner=request.user)
            form = DataSetCreateForm(data=request.POST)

            if form.is_valid():
                data_set = DataSet.objects.create(
                    schema_id=schema_id,
                    num_row=request.POST.get('num_row')
                )
                task = form_fake_data.delay(schema_id, data_set.id)

                response = {
                    'task_id': task.id,
                    'count': schema.dataset_set.count(),
                    'dataset_id': data_set.id,
                    'created': data_set.created,
                    'status': data_set.status
                }

                return JsonResponse(response, status=202)

        else:
            return HttpResponseForbidden(status=403)

class TaskStatusView(LoginRequiredMixin, View):
    """
    Realize Celery task status polling.
    Recieve a GET AJAX request, check task status and send json data to front end.
    """
    def get(self, request, task_id):
        if request.is_ajax():
            task_result = AsyncResult(task_id)
            result = {
                "task_id": task_id,
                "task_status": task_result.status
            }
            return JsonResponse(result, status=202)

        else:
            return HttpResponseForbidden(status=403)

class FileDownloadView(LoginRequiredMixin, View):
    """
    Response for downloading file.
    """
    def get(self, request, dataset_id):
        data_set = get_object_or_404(DataSet, id=dataset_id)
        return FileResponse(open(data_set.file.path, 'rb'))


from django.urls import path
from . import views

app_name = 'events'

urlpatterns = [
    path('create/', views.create_event, name='create_event'),
    path('<int:event_id>/edit/', views.edit_event, name='edit_event'),
    path('<int:event_id>/delete/', views.delete_event, name='delete_event'),

    # Registration management URLs
    path('events/<int:event_id>/registrations/', views.manage_registrations, name='manage_registrations'),
    path('events/registrations/<int:registration_id>/accept/', views.accept_registration, name='accept_registration'),
    path('events/registrations/<int:registration_id>/reject/', views.reject_registration, name='reject_registration'),
    path('<int:event_id>/attendee_list/', views.generate_attendee_list, name='attendee_list'),
    path('venues/', views.venue_list, name='venue_list'),
    path('venues/<int:venue_id>/', views.venue_detail, name='venue_detail'),
]


from typing import Union

import numpy as np
import paddle

from ..configuration_utils import ConfigMixin
from ..configuration_utils import register_to_config
from .scheduling_utils import SchedulerMixin

class ScoreSdeVeScheduler(SchedulerMixin, ConfigMixin):
    """
    The variance exploding stochastic differential equation (SDE) scheduler.

    :param snr: coefficient weighting the step from the model_output sample (from the network) to the random noise.
    :param sigma_min: initial noise scale for sigma sequence in sampling procedure. The minimum sigma should mirror the
            distribution of the data.
    :param sigma_max: :param sampling_eps: the end value of sampling, where timesteps decrease progessively from 1 to
    epsilon. :param correct_steps: number of correction steps performed on a produced sample. :param tensor_format:
    "np" or "pd" for the expected format of samples passed to the Scheduler.
    """

    @register_to_config
    def __init__(
        self,
        num_train_timesteps=2000,
        snr=0.15,
        sigma_min=0.01,
        sigma_max=1348,
        sampling_eps=1e-5,
        correct_steps=1,
        tensor_format="pd",
    ):
        # self.sigmas = None
        # self.discrete_sigmas = None
        #
        # # setable values
        # self.num_inference_steps = None
        self.timesteps = None

        self.set_sigmas(num_train_timesteps, sigma_min, sigma_max, sampling_eps)

        self.tensor_format = tensor_format
        self.set_format(tensor_format=tensor_format)

    def set_timesteps(self, num_inference_steps, sampling_eps=None):
        sampling_eps = sampling_eps if sampling_eps is not None else self.config.sampling_eps
        tensor_format = getattr(self, "tensor_format", "pd")
        if tensor_format == "np":
            self.timesteps = np.linspace(1, sampling_eps, num_inference_steps)
        elif tensor_format == "pd":
            self.timesteps = paddle.linspace(1, sampling_eps, num_inference_steps)
        else:
            raise ValueError(f"`self.tensor_format`: {self.tensor_format} is not valid.")

    def set_sigmas(self, num_inference_steps, sigma_min=None, sigma_max=None, sampling_eps=None):
        sigma_min = sigma_min if sigma_min is not None else self.config.sigma_min
        sigma_max = sigma_max if sigma_max is not None else self.config.sigma_max
        sampling_eps = sampling_eps if sampling_eps is not None else self.config.sampling_eps
        if self.timesteps is None:
            self.set_timesteps(num_inference_steps, sampling_eps)

        tensor_format = getattr(self, "tensor_format", "pd")
        if tensor_format == "np":
            self.discrete_sigmas = np.exp(np.linspace(np.log(sigma_min), np.log(sigma_max), num_inference_steps))
            self.sigmas = np.array([sigma_min * (sigma_max / sigma_min)**t for t in self.timesteps])
        elif tensor_format == "pd":
            self.discrete_sigmas = paddle.exp(paddle.linspace(np.log(sigma_min), np.log(sigma_max),
                                                              num_inference_steps))
            self.sigmas = paddle.to_tensor([sigma_min * (sigma_max / sigma_min)**t for t in self.timesteps])
        else:
            raise ValueError(f"`self.tensor_format`: {self.tensor_format} is not valid.")

    def get_adjacent_sigma(self, timesteps, t):
        tensor_format = getattr(self, "tensor_format", "pd")
        if tensor_format == "np":
            return np.where(timesteps == 0, np.zeros_like(t), self.discrete_sigmas[timesteps - 1])
        elif tensor_format == "pd":
            return paddle.where(timesteps == 0, paddle.zeros_like(t), self.discrete_sigmas[timesteps - 1])

        raise ValueError(f"`self.tensor_format`: {self.tensor_format} is not valid.")

    def set_seed(self, seed):
        tensor_format = getattr(self, "tensor_format", "pd")
        if tensor_format == "np":
            np.random.seed(seed)
        elif tensor_format == "pd":
            paddle.seed(seed)
        else:
            raise ValueError(f"`self.tensor_format`: {self.tensor_format} is not valid.")

    def step_pred(
        self,
        model_output: Union[paddle.Tensor, np.ndarray],
        timestep: int,
        sample: Union[paddle.Tensor, np.ndarray],
        seed=None,
    ):
        """
        Predict the sample at the previous timestep by reversing the SDE.
        """
        if seed is not None:
            self.set_seed(seed)
        # TODO(Patrick) non-Pypaddle

        timestep = timestep * paddle.ones(sample.shape[0])  # paddle.repeat_interleave(timestep, sample.shape[0])
        timesteps = (timestep * (len(self.timesteps) - 1)).astype("int64")

        sigma = self.discrete_sigmas[timesteps]
        adjacent_sigma = self.get_adjacent_sigma(timesteps, timestep)
        drift = self.zeros_like(sample)
        diffusion = (sigma**2 - adjacent_sigma**2)**0.5

        # equation 6 in the paper: the model_output modeled by the network is grad_x log pt(x)
        # also equation 47 shows the analog from SDE models to ancestral sampling methods
        drift = drift - diffusion[:, None, None, None]**2 * model_output

        #  equation 6: sample noise for the diffusion term of
        noise = self.randn_like(sample)
        prev_sample_mean = sample - drift  # subtract because `dt` is a small negative timestep
        # TODO is the variable diffusion the correct scaling term for the noise?
        prev_sample = prev_sample_mean + diffusion[:, None, None, None] * noise  # add impact of diffusion field g

        return {"prev_sample": prev_sample, "prev_sample_mean": prev_sample_mean}

    def step_correct(
        self,
        model_output: Union[paddle.Tensor, np.ndarray],
        sample: Union[paddle.Tensor, np.ndarray],
        seed=None,
    ):
        """
        Correct the predicted sample based on the output model_output of the network. This is often run repeatedly
        after making the prediction for the previous timestep.
        """
        if seed is not None:
            self.set_seed(seed)

        # For small batch sizes, the paper "suggest replacing norm(z) with sqrt(d), where d is the dim. of z"
        # sample noise for correction
        noise = self.randn_like(sample)

        # compute step size from the model_output, the noise, and the snr
        grad_norm = self.norm(model_output)
        noise_norm = self.norm(noise)
        step_size = (self.config.snr * noise_norm / grad_norm)**2 * 2
        step_size = step_size * paddle.ones(sample.shape[0])
        # self.repeat_scalar(step_size, sample.shape[0])

        # compute corrected sample: model_output term and noise term
        prev_sample_mean = sample + step_size[:, None, None, None] * model_output
        prev_sample = prev_sample_mean + ((step_size * 2)**0.5)[:, None, None, None] * noise

        return {"prev_sample": prev_sample}

    def __len__(self):
        return self.config.num_train_timesteps


from django.shortcuts import render
from django import forms
from django.http import HttpResponseRedirect
from django.contrib import messages
from django.urls import reverse

from . import util

import os
import random

def redirect(request):
    """
    Redirects links to index page, used when user attempts to acces an empty wiki or edit page

    request: HttpRequest object with information about user's request

    return: HttpResponseRedirect to index page
    """

    return HttpResponseRedirect("/")

def index(request):
    """
    Used for loading index page

    request: HttpRequest object with information about user's request

    return: render of index.html
    """

    return render(request, "encyclopedia/index.html", {
        "entries": util.list_entries() # all encyclopedia entries for web page to load
    })

def page(request, entry):
    """
    Used to load specific encyclopedia entry pages

    request: HttpRequest object with information about user's request

    return: render of entry_page.html
    """

    found = True
    information = util.get_entry(entry)

    if information == None:
        found = False
        initial_entry = entry
        entry = "Not Found"
        information = "
Entry \"" + initial_entry + "\" Does Not Exist
"
        information += "
"

    return render(request, "encyclopedia/entry_page.html", {
        "entry": entry, # title of entry
        "information": information, # entry's content
        "found": found # bool whether entry was found or not
    })

def search(request):
    """
    Used to run search function of website

    request: HttpRequest object with information about user's request

    return: render of web page
    """

    query = request.GET.get("q")

    # if search query == name of an entry, then loads that entry's page
    if util.get_entry(query) != None:
            return page(request, query)
    else:
        sub_string_entries = []
        entries = util.list_entries()

        # checks if search query is substring in every entry name
        for entry in entries:
            if entry.find(query) >= 0:
                sub_string_entries.append(entry)

        return render(request, "encyclopedia/search.html", {
            "query": query, # search query
            "entries": sub_string_entries # entries list where query is substr
        })

def create(request):
    """
    Used to load Create New Page webpage

    request: HttpRequest object with information about user's request

    return: render of create.html, or redirect to newly created web page
    """

    title = request.GET.get("title")
    content = request.GET.get("content")
    information = util.get_entry(title)

    # if user entered a title
    if title != None:
        # if entry already exists
        if information != None:
            # alers user that entry already exists
            messages.error(request, "Page already exists")

            return render(request, "encyclopedia/create.html", {})
        else:
            # creates new entry page with user submitted information
            new_file = open( os.path.join("./entries" ,(str(title) + ".md")), "w")
            new_file.write("# " + str(title) + '\n')
            content = content.replace('\n','')
            new_file.write(content)
            new_file.close()

            return HttpResponseRedirect(('/wiki/' + str(title)))

    return render(request, "encyclopedia/create.html", {})

def edit(request, entry):
    """
    Used to load edit page for entries

    entry: title of entry user is requesting to edit

    request: HttpRequest object with information about user's request

    return: redirect to entry page, render of index page
    """

    content = request.GET.get("content")

    # if user submitted content for entry
    if content != None:
        # if entry alreqady exits, delete initial file
        try:
            os.remove(os.path.join("./entries" ,(str(entry) + ".md")))
        except:
            pass

        rewrite = open(os.path.join("./entries" ,(str(entry) + ".md")), "w")
        content = content.replace('\n','')
        rewrite.write(content)
        rewrite.close()

        return HttpResponseRedirect(('/wiki/' + str(entry)))
    else:
        open_file = open(os.path.join("./entries" ,(str(entry) + ".md")), "r")
        content = open_file.read()
        open_file.close()

        if content != None:
            return render(request, "encyclopedia/edit.html", {
                "entry": entry, # title of entry
                "content": content # content of entry
            })

    return render(request, "encyclopedia/index.html", {})

def random_page(request):
    """
    Used for Random Page feature of website

    request: HttpRequest object with information about user's request

    return: redirect to random encyclopedia entry
    """

    entries = util.list_entries()

    page_index = random.randint(0, (len(entries) - 1))
    title = entries[page_index]

    return HttpResponseRedirect(('/wiki/' + str(title)))


# -*- coding: utf-8 -*-
from MySeq import MySeq
from MyMotifs import MyMotifs
from random import randint
from random import random

class MotifFinding:
    def __init__(self, size = 8, seqs = None):
        self.motifSize = size
        if (seqs != None):
            self.seqs = seqs
            self.alphabet = seqs[0].alfabeto()
        else:
            self.seqs = []

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

    def __getitem__(self, n):
        return self.seqs[n]

    def seqSize (self, i):
        return len(self.seqs[i])

    def readFile(self, fic, t):
        for s in open(fic, "r"):
            self.seqs.append(MySeq(s.strip().upper(),t))
        self.alphabet = self.seqs[0].alfabeto()

    def createMotifFromIndexes(self, indexes):
        pseqs = []
        for i,ind in enumerate(indexes):
            pseqs.append( MySeq(self.seqs[i][ind:(ind+self.motifSize)], self.seqs[i].tipo) )
        return MyMotifs(pseqs)

    # SCORES
    def score(self, s):
        score = 0
        motif = self.createMotifFromIndexes(s)
        motif.doCounts()
        mat = motif.counts
        for j in range(len(mat[0])):
            maxcol = mat[0][j]
            for  i in range(1, len(mat)):
                if mat[i][j] > maxcol:
                    maxcol = mat[i][j]
            score += maxcol
        return score

    def scoreMult(self, s):
        score = 1.0
        motif = self.createMotifFromIndexes(s)
        motif.createPWM()
        mat = motif.pwm
        for j in range(len(mat[0])):
            maxcol = mat[0][j]
            for  i in range(1, len(mat)):
                if mat[i][j] > maxcol:
                    maxcol = mat[i][j]
            score *= maxcol
        return score

    # EXHAUSTIVE SEARCH
    def nextSol (self, s):
        nextS = [0]*len(s)
        pos = len(s) - 1
        while pos >=0 and s[pos] == self.seqSize(pos) - self.motifSize:
            pos -= 1
        if (pos < 0):
            nextS = None
        else:
            for i in range(pos):
                nextS[i] = s[i]
            nextS[pos] = s[pos]+1;
            for i in range(pos+1, len(s)):
                nextS[i] = 0
        return nextS

    def exhaustiveSearch(self):
        melhorScore = -1
        res = []
        s = [0]* len(self.seqs)
        while (s!= None):
            sc = self.score(s)
            if (sc > melhorScore):
                melhorScore = sc
                res = s
            s = self.nextSol(s)
        return res

    # BRANCH AND BOUND
    def bypass (self, s):
        res =  []
        pos = len(s) -1
        while pos >=0 and s[pos] == self.seqSize(pos) - self.motifSize:
            pos -= 1
        if pos < 0: res = None
        else:
            for i in range(pos): res.append(s[i])
            res.append(s[pos]+1)
        return res

    def nextVertex (self, s):
        res =  []
        if len(s) < len(self.seqs): # internal node -> down one level
            for i in range(len(s)):
                res.append(s[i])
            res.append(0)
        else:# bypass
            pos = len(s)-1
            while pos >=0 and s[pos] == self.seqSize(pos) - self.motifSize:
                pos -= 1
            if pos < 0: res = None # last solution
            else:
                for i in range(pos): res.append(s[i])
                res.append(s[pos]+1)
        return res

    def branchAndBound (self):
        melhorScore = -1
        melhorMotif = None
        size = len(self.seqs)
        s = [0]*size
        while s != None:
            if len(s) < size:
                max_s_score = self.score(s) + (size-len(s)) * self.motifSize
                if max_s_score < melhorScore:
                    s = self.bypass(s)
                else:
                    s = self.nextVertex(s)
            else:
                sc = self.score(s)
                if sc > melhorScore:
                    melhorScore = sc
                    melhorMotif  = s
                s = self.nextVertex(s)
        return melhorMotif

   # Consensus (heuristic)

    def heuristicConsensus(self):
        res = [0]* len(self.seqs)
        maxScore = -1;
        partial = [0,0]
        for i in range(self.seqSize(0)-self.motifSize):
            for j in range(self.seqSize(1)-self.motifSize):
                partial[0] = i
                partial[1] = j
                sc = self.score(partial);
                if(sc > maxScore):
                    maxScore = sc
                    res[0] = i
                    res[1] = j
        for k in range(2, len(self.seqs)):
            partial = [0]*(k+1)
            for j in range(k):
                partial[j] = res[j]
            maxScore = -1
            for i in range(self.seqSize(k)-self.motifSize):
                partial[k] = i
                sc = self.score(partial)
                if(sc > maxScore):
                    maxScore = sc
                    res[k] = i
        return res

    # heuristic stochastic
    def heuristicStochastic (self):
        s = [0]* len(self.seqs)
        for k in range(len(s)):
            s[k] = randint(0, self.seqSize(k)- self.motifSize)
        motif = self.createMotifFromIndexes(s)
        motif.createPWM()
        sc = self.scoreMult(s)
        bestsol = s
        mel = True
        while(mel):
            for k in range(len(s)):
                s[k] = motif.mostProbableSeq(self.seqs[k])
            if self.scoreMult(s) > sc:
                sc = self.scoreMult(s)
                bestsol = s
                motif = self.createMotifFromIndexes(s)
                motif.createPWM()
            else: mel = False
        return bestsol

    # gibbs sampling
    def gibbs(self, numits):
        s = []
        for k in range(len(self.seqs)):
            s.append ( randint(0, self.seqSize(k)- self.motifSize) )
        bestS = list(s)
        bestScore = self.scoreMult(s)
        for its in range(numits):
            seqi = randint(0, len(self.seqs)-1)
            seqsel = self.seqs[seqi]
            s.pop(seqi)
            removed = self.seqs.pop(seqi)
            motif = self.createMotifFromIndexes(s)
            motif.createPWM()
            self.seqs.insert(seqi, removed)
            r = motif.probAllPositions(seqsel)
            pos = self.roulette(r)
            s.insert(seqi, pos)
            score = self.scoreMult(s)
            if score > bestScore:
                bestScore = score
                bestS = list(s)
        return bestS

    ###########################
    # END TO DO

    def roulette(self, f):
        tot = 0.0
        for x in f: tot += (0.01+x)
        val = random()* tot
        acum = 0.0
        ind = 0
        while acum < val:
            acum += (f[ind] + 0.01)
            ind += 1
        return ind-1

# tests

def test1():
    sm = MotifFinding()
    sm.readFile("exemploMotifs.txt","dna")
    sol = [25,20,2,55,59]
    sa = sm.score(sol)
    print(sa)
    scm = sm.scoreMult(sol)
    print(scm)

def test2():
    print ("Test exhaustive:")
    seq1 = MySeq("ATAGAGCTGA","dna")
    seq2 = MySeq("ACGTAGATGA","dna")
    seq3 = MySeq("AAGATAGGGG","dna")
    mf = MotifFinding(3, [seq1,seq2,seq3])
    sol = mf.exhaustiveSearch()
    print ("Solution", sol)
    print ("Score: ", mf.score(sol))
    print("Consensus:", mf.createMotifFromIndexes(sol).consensus())

    print ("Branch and Bound:")
    sol2 = mf.branchAndBound()
    print ("Solution: " , sol2)
    print ("Score:" , mf.score(sol2))
    print("Consensus:", mf.createMotifFromIndexes(sol2).consensus())

    print ("Heuristic consensus: ")
    sol3 = mf.heuristicConsensus()
    print ("Solution: " , sol3)
    print ("Score:" , mf.score(sol3))

def test3():
    mf = MotifFinding()
    mf.readFile("exemploMotifs.txt","dna")
    print ("Branch and Bound:")
    sol = mf.branchAndBound()
    print ("Solution: " , sol)
    print ("Score:" , mf.score(sol))
    print("Consensus:", mf.createMotifFromIndexes(sol).consensus())

def test4():
    mf = MotifFinding()
    mf.readFile("exemploMotifs.txt","dna")
    print ("Heuristic consensus: ")
    sol3 = mf.heuristicConsensus()
    print ("Solution: " , sol3)
    print ("Score:" , mf.score(sol3))
    print ("Score mult:" , mf.scoreMult(sol3))

    print("Heuristic stochastic")
    sol = mf.heuristicStochastic()
    print ("Solution: " , sol)
    print ("Score:" , mf.score(sol))
    print ("Score mult:" , mf.scoreMult(sol))
    print("Consensus:", mf.createMotifFromIndexes(sol).consensus())

    sol2 = mf.gibbs(1000)
    print ("Score:" , mf.score(sol2))
    print ("Score mult:" , mf.scoreMult(sol2))

if __name__ == '__main__':
    test1()
    test2()
    test3()
    test4()


LINE_COUNT=50
class Scaffold:
    def __init__(self,fastapath:str) -> None:
        self.fastapath=fastapath
    def getseq(self,start:int,end:int)->str:
        #取得的序列包括start和end，scaffold从1开始计数
        with open(self.fastapath,mode='r') as fastafile:
            fastafile.readline()
            blank_num=(start-1)//LINE_COUNT
            rest=LINE_COUNT-(start-1)%LINE_COUNT
            fastafile.read(start-1+blank_num)
            blank_num=((end-start-rest)//LINE_COUNT)+1 if end-start-rest>=0 else 0
            s=fastafile.read(end-start+1+blank_num)
            return s

if __name__ == '__main__':
    print('testing scaffold class')
    test=Scaffold(r'C:\Users\jiangzhesheng\PycharmProjects\gapfinder\hg38\chr2.fa')
    print(test.getseq(11229,46269))


import os
import threading
from datetime import datetime

from gtts import gTTS, lang
from playsound import playsound

# supported languages
languages = lang.tts_langs().keys()

def handle_entry(input, is_save, is_speak, is_file, output_name, lang):
   if not output_name:
      # if output file name is not given use current date
      output_name = datetime.now().strftime("%H_%M_%S_%d_%m_%Y")
   output_name = output_name + ".mp3"

   if is_file:
      with open(input, "r") as file:
         _text_to_speech(file.read(), output_name, lang)
   else:
      _text_to_speech(input, output_name, lang)

   if is_speak:
      threading.Thread(target=lambda: _play_sound(output_name, is_save), daemon=True).start()

def _play_sound(file_name, is_save):
   playsound(file_name)             # play mp3
   if not is_save:
      os.remove(file_name)          # delete mp3

def _text_to_speech(text, output_name, lang):
   tts = gTTS(text, lang=lang)                 # text -> speech
   tts.save(output_name)            # save speech to mp3


import requests
import urllib.request
import os
import pickle
import argparse

# file read folder
path = 'http://db.itkc.or.kr//data/imagedb/BOOK/ITKC_{0}/ITKC_{0}_{1}A/ITKC_{0}_{1}A_{2}{5}_{3}{4}.JPG'

# Manual
label = ['BT', 'MO']
middle = 1400
last = ['A', 'V']  # A ~400 V ~009
num = 10
num1 = 400

fin = ['A', 'B', 'H', 'L']

# file path, save path

# pad for number
def pad(num, width):
    return '%0{}d'.format(width) % num

def save_picture(file_name, save_dir):
    return urllib.request.urlretrieve(file_name, save_dir)

def main():

    parser = argparse.ArgumentParser()

    parser.add_argument('-l', '--label', default='BT', type=str, help='BT, MO')
    parser.add_argument('-f', '--fin', default='A', type=str, help='A,B,H,L')

    opt = parser.parse_args()

    # make directory
    if not os.path.exists('oldDB'):
        os.mkdir('oldDB')

    if opt.label == 'BT':
    	for i in range(0, middle+1):
            for k in range(num + 1):
                for j in range(num1 + 1):

                    try:
                        p = path.format(opt.label, pad(i, 4),
                                        'V', pad(j, 3), opt.fin, pad(k, 3))
                        print(p)
                        save_picture(p, './oldDB/{0}_{1}_{2}_{3}_{4}.jpg'.format(
                            opt.label, i, 'V', j, opt.fin))

                    except Exception as e:
                        print(str(e))
                        continue

    elif opt.label == 'MO':
     	for i in range(0, middle+1):
            for k in range(num1 + 1):
                for j in range(num1 + 1):

                    try:
                        p = path.format(opt.label, pad(i, 4),
                                        'A', pad(j, 3), opt.fin, pad(k, 3))
                        print(p)
                        save_picture(p, './oldDB/{0}_{1}_{2}_{3}_{4}.jpg'.format(
                            opt.label, i, 'A', j, opt.fin))

                    except Exception as e:
                        print(str(e))
                        continue

if __name__ == '__main__':
    main()


import numpy as np
import copy
from tqdm import trange
def burgers_timestep(x:np.ndarray,y:np.ndarray,u:np.ndarray,v:np.ndarray,nt:int,dt:float=0.001,nu:float=0.1):
    """Solving burgers equation using finite difference with euler time step (1st order approximation)

    Args:
        x (np.ndarray): domain x direction
        y (np.ndarray): domain y direction
        u (np.ndarray): matrix describing x-velocity field in i,j space
        v (np.ndarray): matrix describing y-velocity field in i,j space
        nt (int): number of time advances
        dt (float): time step to use for first order approximation
        nu (float, Optional): Viscosity. Defaults to 0.1

    Returns:
        (tuple): containing

            **u_history** (np.ndarray): history for u velocity
            **v_history** (np.ndarray): history for v velocity
    """

    dx = x[0,2]-x[0,1] # We can do it this way because x and y are initialized using linspace which guarantees constant spacing
    dy = y[1,0]-y[0,0]

    u_history = list()
    v_history = list()
    u_history.append(copy.deepcopy(u)) # set equal to initial value
    v_history.append(copy.deepcopy(v))
    for n in trange(nt):
        un = u.copy()   # previous value
        vn = v.copy()
        for i in range(1,x.shape[0]-1):
            for j in range(1,y.shape[1]-1):
                # Uses backward difference in space to solve first order derivative
                # Central differencing for second order derivative
                u[i,j] = (un[i, j] -(un[i, j] * dt / dx * (un[i, j] - un[i-1, j])) -vn[i, j] * dt / dy * (un[i, j] - un[i, j-1])) + (nu*dt/(dx**2))*(un[i+1,j]-2*un[i,j]+un[i-1,j])+(nu*dt/(dx**2))*(un[i,j-1]-2*un[i,j]+un[i,j+1])
                v[i,j] = (vn[i, j] -(un[i, j] * dt / dx * (vn[i, j] - vn[i-1, j]))-vn[i, j] * dt / dy * (vn[i, j] - vn[i, j-1])) + (nu*dt/(dx**2))*(vn[i+1,j]-2*vn[i,j]+vn[i-1,j])+(nu*dt/(dx**2))*(vn[i,j-1]-2*vn[i,j]+vn[i,j+1])

        u[:,0] = 1       # At all i values when j = 0
        u[:,-1] = 1      # At all i values when j = jmax
        u[0,:] = 1       # At all j values and i = 0
        u[-1,:] = 1     # At all j values and i = imax

        v[:,0] = 1
        v[:,-1] = 1
        v[0,:] = 1
        v[-1,:] = 1

        u_history.append(copy.deepcopy(u))
        v_history.append(copy.deepcopy(v))
    return u_history, v_history


#!/usr/bin/env python

import os
import sys
import math

if __name__ == "__main__":
    filepath = 'input.txt'
    data = []
    with open(filepath) as file:
        lines = file.readlines()
        for line in lines:
            data.append(int(line))
    data.sort()
    sum_1jolt_diff = 1
    sum_3jolt_diff = 1
    for i in range(len(data)-1):
        if data[i] + 1 == data[i+1]:
            sum_1jolt_diff += 1
        elif data[i] + 3 == data[i+1]:
            sum_3jolt_diff += 1
    print(sum_1jolt_diff*sum_3jolt_diff)


r"""RemoteImageLocalInstance class.

Create class that is responsible for creating a local instance AVD with a
remote image.
"""
import logging
import os
import subprocess
import sys

from acloud import errors
from acloud.create import local_image_local_instance
from acloud.internal import constants
from acloud.internal.lib import android_build_client
from acloud.internal.lib import auth
from acloud.internal.lib import utils
from acloud.setup import setup_common

logger = logging.getLogger(__name__)

# Download remote image variables.
_CUTTLEFISH_COMMON_BIN_PATH = "/usr/lib/cuttlefish-common/bin/"
_CONFIRM_DOWNLOAD_DIR = ("Download dir %(download_dir)s does not have enough "
                         "space (available space %(available_space)sGB, "
                         "require %(required_space)sGB).\nPlease enter "
                         "alternate path or 'q' to exit: ")
_HOME_FOLDER = os.path.expanduser("~")
# The downloaded image artifacts will take up ~8G:
#   $du -lh --time $ANDROID_PRODUCT_OUT/aosp_cf_x86_phone-img-eng.XXX.zip
#   422M
# And decompressed becomes 7.2G (as of 11/2018).
# Let's add an extra buffer (~2G) to make sure user has enough disk space
# for the downloaded image artifacts.
_REQUIRED_SPACE = 10

@utils.TimeExecute(function_description="Downloading Android Build image")
def DownloadAndProcessImageFiles(avd_spec):
    """Download the CF image artifacts and process them.

    To download rom images, Acloud would download the tool fetch_cvd that can
    help process mixed build images.

    Args:
        avd_spec: AVDSpec object that tells us what we're going to create.

    Returns:
        extract_path: String, path to image folder.

    Raises:
        errors.GetRemoteImageError: Fails to download rom images.
    """
    cfg = avd_spec.cfg
    build_id = avd_spec.remote_image[constants.BUILD_ID]
    build_branch = avd_spec.remote_image[constants.BUILD_BRANCH]
    build_target = avd_spec.remote_image[constants.BUILD_TARGET]

    extract_path = os.path.join(
        avd_spec.image_download_dir,
        constants.TEMP_ARTIFACTS_FOLDER,
        build_id + build_target)

    logger.debug("Extract path: %s", extract_path)
    # TODO(b/117189191): If extract folder exists, check if the files are
    # already downloaded and skip this step if they are.
    if not os.path.exists(extract_path):
        os.makedirs(extract_path)
        build_api = (
            android_build_client.AndroidBuildClient(auth.CreateCredentials(cfg)))

        # Download rom images via fetch_cvd
        fetch_cvd = os.path.join(extract_path, constants.FETCH_CVD)
        build_api.DownloadFetchcvd(fetch_cvd, cfg.fetch_cvd_version)
        fetch_cvd_build_args = build_api.GetFetchBuildArgs(
            build_id, build_branch, build_target,
            avd_spec.system_build_info.get(constants.BUILD_ID),
            avd_spec.system_build_info.get(constants.BUILD_BRANCH),
            avd_spec.system_build_info.get(constants.BUILD_TARGET),
            avd_spec.kernel_build_info.get(constants.BUILD_ID),
            avd_spec.kernel_build_info.get(constants.BUILD_BRANCH),
            avd_spec.kernel_build_info.get(constants.BUILD_TARGET),
            avd_spec.bootloader_build_info.get(constants.BUILD_ID),
            avd_spec.bootloader_build_info.get(constants.BUILD_BRANCH),
            avd_spec.bootloader_build_info.get(constants.BUILD_TARGET))
        creds_cache_file = os.path.join(_HOME_FOLDER, cfg.creds_cache_file)
        fetch_cvd_cert_arg = build_api.GetFetchCertArg(creds_cache_file)
        fetch_cvd_args = [fetch_cvd, "-directory=%s" % extract_path,
                          fetch_cvd_cert_arg]
        fetch_cvd_args.extend(fetch_cvd_build_args)
        logger.debug("Download images command: %s", fetch_cvd_args)
        try:
            subprocess.check_call(fetch_cvd_args)
        except subprocess.CalledProcessError as e:
            raise errors.GetRemoteImageError("Fails to download images: %s" % e)

    return extract_path

def ConfirmDownloadRemoteImageDir(download_dir):
    """Confirm download remote image directory.

    If available space of download_dir is less than _REQUIRED_SPACE, ask
    the user to choose a different download dir or to exit out since acloud will
    fail to download the artifacts due to insufficient disk space.

    Args:
        download_dir: String, a directory for download and decompress.

    Returns:
        String, Specific download directory when user confirm to change.
    """
    while True:
        download_dir = os.path.expanduser(download_dir)
        if not os.path.exists(download_dir):
            answer = utils.InteractWithQuestion(
                "No such directory %s.\nEnter 'y' to create it, enter "
                "anything else to exit out[y/N]: " % download_dir)
            if answer.lower() == "y":
                os.makedirs(download_dir)
            else:
                sys.exit(constants.EXIT_BY_USER)

        stat = os.statvfs(download_dir)
        available_space = stat.f_bavail*stat.f_bsize/(1024)**3
        if available_space < _REQUIRED_SPACE:
            download_dir = utils.InteractWithQuestion(
                _CONFIRM_DOWNLOAD_DIR % {"download_dir":download_dir,
                                         "available_space":available_space,
                                         "required_space":_REQUIRED_SPACE})
            if download_dir.lower() == "q":
                sys.exit(constants.EXIT_BY_USER)
        else:
            return download_dir

class RemoteImageLocalInstance(local_image_local_instance.LocalImageLocalInstance):
    """Create class for a remote image local instance AVD.

    RemoteImageLocalInstance just defines logic in downloading the remote image
    artifacts and leverages the existing logic to launch a local instance in
    LocalImageLocalInstance.
    """

    def GetImageArtifactsPath(self, avd_spec):
        """Download the image artifacts and return the paths to them.

        Args:
            avd_spec: AVDSpec object that tells us what we're going to create.

        Raises:
            errors.NoCuttlefishCommonInstalled: cuttlefish-common doesn't install.

        Returns:
            local_image_local_instance.ArtifactPaths object.
        """
        if not setup_common.PackageInstalled("cuttlefish-common"):
            raise errors.NoCuttlefishCommonInstalled(
                "Package [cuttlefish-common] is not installed!\n"
                "Please run 'acloud setup --host' to install.")

        avd_spec.image_download_dir = ConfirmDownloadRemoteImageDir(
            avd_spec.image_download_dir)

        image_dir = DownloadAndProcessImageFiles(avd_spec)
        launch_cvd_path = os.path.join(image_dir, "bin",
                                       constants.CMD_LAUNCH_CVD)
        if not os.path.exists(launch_cvd_path):
            raise errors.GetCvdLocalHostPackageError(
                "No launch_cvd found. Please check downloaded artifacts dir: %s"
                % image_dir)
        # This method does not set the optional fields because launch_cvd loads
        # the paths from the fetcher config in image_dir.
        return local_image_local_instance.ArtifactPaths(
            image_dir, image_dir, None, None, None, None)


"""EdX Branding API

Provides a way to retrieve "branded" parts of the site,
such as the site footer.

This information exposed to:
1) Templates in the LMS.
2) Consumers of the branding API.

This ensures that branded UI elements such as the footer
are consistent across the LMS and other sites (such as
the marketing site and blog).

"""
import logging
import urlparse

from django.conf import settings
from django.utils.translation import ugettext as _
from staticfiles.storage import staticfiles_storage

from microsite_configuration import microsite
from edxmako.shortcuts import marketing_link
from branding.models import BrandingApiConfig

log = logging.getLogger("edx.footer")

def is_enabled():
    """Check whether the branding API is enabled. """
    return BrandingApiConfig.current().enabled

def get_footer(is_secure=True):
    """Retrieve information used to render the footer.

    This will handle both the OpenEdX and EdX.org versions
    of the footer.  All user-facing text is internationalized.

    Currently, this does NOT support theming.

    Keyword Arguments:
        is_secure (bool): If True, use https:// in URLs.

    Returns: dict

    Example:
    >>> get_footer()
    {
        "copyright": "(c) 2015 EdX Inc",
        "logo_image": "http://www.example.com/logo.png",
        "social_links": [
            {
                "name": "facebook",
                "title": "Facebook",
                "url": "http://www.facebook.com/example",
                "icon-class": "fa-facebook-square"
            },
            ...
        ],
        "navigation_links": [
            {
                "name": "about",
                "title": "About",
                "url": "http://www.example.com/about.html"
            },
            ...
        ],
        "mobile_links": [
            {
                "name": "apple",
                "title": "Apple",
                "url": "http://store.apple.com/example_app"
                "image": "http://example.com/static/apple_logo.png"
            },
            ...
        ],
        "legal_links": [
            {
                "url": "http://example.com/terms-of-service.html",
                "name": "terms_of_service",
                "title': "Terms of Service"
            },
            # ...
        ],
        "openedx_link": {
            "url": "http://open.edx.org",
            "title": "Powered by Open edX",
            "image": "http://example.com/openedx.png"
        }
    }

    """
    return {
        "copyright": _footer_copyright(),
        "logo_image": _footer_logo_img(is_secure),
        "social_links": _footer_social_links(),
        "navigation_links": _footer_navigation_links(),
        "mobile_links": _footer_mobile_links(is_secure),
        "legal_links": _footer_legal_links(),
        "openedx_link": _footer_openedx_link(),
    }

def _footer_copyright():
    """Return the copyright to display in the footer.

    Returns: unicode

    """
    org_name = (
        "edX Inc" if settings.FEATURES.get('IS_EDX_DOMAIN', False)
        else microsite.get_value('PLATFORM_NAME', settings.PLATFORM_NAME)
    )

    # Translators: 'EdX', 'edX', and 'Open edX' are trademarks of 'edX Inc.'.
    # Please do not translate any of these trademarks and company names.
    return _(
        u"\u00A9 {org_name}.  All rights reserved except where noted.  "
        u"EdX, Open edX and the edX and Open EdX logos are registered trademarks "
        u"or trademarks of edX Inc."
    ).format(org_name=org_name)

def _footer_openedx_link():
    """Return the image link for "powered by OpenEdX".

    Args:
        is_secure (bool): Whether the request is using TLS.

    Returns: dict

    """
    # Translators: 'Open edX' is a brand, please keep this untranslated.
    # See http://openedx.org for more information.
    title = _("Powered by Open edX")
    return {
        "url": settings.FOOTER_OPENEDX_URL,
        "title": title,
        "image": settings.FOOTER_OPENEDX_LOGO_IMAGE,
    }

def _footer_social_links():
    """Return the social media links to display in the footer.

    Returns: list

    """
    links = []
    for social_name in settings.SOCIAL_MEDIA_FOOTER_NAMES:
        links.append(
            {
                "name": social_name,
                "title": unicode(settings.SOCIAL_MEDIA_FOOTER_DISPLAY.get(social_name, {}).get("title", "")),
                "url": settings.SOCIAL_MEDIA_FOOTER_URLS.get(social_name, "#"),
                "icon-class": settings.SOCIAL_MEDIA_FOOTER_DISPLAY.get(social_name, {}).get("icon", ""),
            }
        )
    return links

def _footer_navigation_links():
    """Return the navigation links to display in the footer. """
    return [
        {
            "name": link_name,
            "title": link_title,
            "url": link_url,
        }
        for link_name, link_url, link_title in [
            ("about", marketing_link("ABOUT"), _("About")),
            ("blog", marketing_link("BLOG"), _("Blog")),
            ("news", marketing_link("NEWS"), _("News")),
            ("faq", marketing_link("FAQ"), _("FAQs")),
            ("contact", marketing_link("CONTACT"), _("Contact")),
            ("jobs", marketing_link("JOBS"), _("Jobs")),
            ("donate", marketing_link("DONATE"), _("Donate")),
            ("sitemap", marketing_link("SITE_MAP"), _("Sitemap")),
        ]
        if link_url and link_url != "#"
    ]

def _footer_legal_links():
    """Return the legal footer links (e.g. terms of service). """

    links = [
        ("terms_of_service_and_honor_code", marketing_link("TOS_AND_HONOR"), _("Terms of Service & Honor Code")),
        ("privacy_policy", marketing_link("PRIVACY"), _("Privacy Policy")),
        ("accessibility_policy", marketing_link("ACCESSIBILITY"), _("Accessibility Policy")),
    ]

    # Backwards compatibility: If a combined "terms of service and honor code"
    # link isn't provided, add separate TOS and honor code links.
    tos_and_honor_link = marketing_link("TOS_AND_HONOR")
    if not (tos_and_honor_link and tos_and_honor_link != "#"):
        links.extend([
            ("terms_of_service", marketing_link("TOS"), _("Terms of Service")),
            ("honor_code", marketing_link("HONOR"), _("Honor Code")),
        ])

    return [
        {
            "name": link_name,
            "title": link_title,
            "url": link_url,
        }
        for link_name, link_url, link_title in links
        if link_url and link_url != "#"
    ]

def _footer_mobile_links(is_secure):
    """Return the mobile app store links.

    Args:
        is_secure (bool): Whether the request is using TLS.

    Returns: list

    """
    mobile_links = []
    if settings.FEATURES.get('ENABLE_FOOTER_MOBILE_APP_LINKS'):
        mobile_links = [
            {
                "name": "apple",
                "title": "Apple",
                "url": settings.MOBILE_STORE_URLS.get('apple', '#'),
                "image": _absolute_url_staticfile(is_secure, 'images/app/app_store_badge_135x40.svg')
            },
            {
                "name": "google",
                "title": "Google",
                "url": settings.MOBILE_STORE_URLS.get('google', '#'),
                "image": _absolute_url_staticfile(is_secure, 'images/app/google_play_badge_45.png')
            }
        ]
    return mobile_links

def _footer_logo_img(is_secure):
    """Return the logo used for footer about link

    Args:
        is_secure (bool): Whether the request is using TLS.

    Returns:
        Absolute url to logo
    """
    logo_name = microsite.get_value('FOOTER_ORGANIZATION_IMAGE', settings.FOOTER_ORGANIZATION_IMAGE)
    return _absolute_url_staticfile(is_secure, logo_name)

def _absolute_url(is_secure, url_path):
    """Construct an absolute URL back to the site.

    Arguments:
        is_secure (bool): If true, use HTTPS as the protocol.
        url_path (unicode): The path of the URL.

    Returns:
        unicode

    """
    site_name = microsite.get_value('SITE_NAME', settings.SITE_NAME)
    parts = ("https" if is_secure else "http", site_name, url_path, '', '', '')
    return urlparse.urlunparse(parts)

def _absolute_url_staticfile(is_secure, name):
    """Construct an absolute URL to a static resource on the site.

    Arguments:
        is_secure (bool): If true, use HTTPS as the protocol.
        name (unicode): The name of the static resource to retrieve.

    Returns:
        unicode

    """
    url_path = staticfiles_storage.url(name)

    # In production, the static files URL will be an absolute
    # URL pointing to a CDN.  If this happens, we can just
    # return the URL.
    if urlparse.urlparse(url_path).netloc:
        return url_path

    # For local development, the returned URL will be relative,
    # so we need to make it absolute.
    return _absolute_url(is_secure, url_path)


import pandas as pd
import numpy as np
from pathlib import Path

# A function to read file from a relative path
def read_file_to_df(file_name, sheet_name=None, cols_to_use=None):
    p = Path('.')

    if file_name == "ceny_mieszkan":
        file_name = file_name + ".xlsx"
        df = pd.read_excel(p / 'res' / file_name, sheet_name=sheet_name, usecols=cols_to_use, header=1,
                           skiprows=5)
        if df.columns[0].endswith(".1"):
            df.rename(columns={
                df.columns[0]: "Białystok",
                df.columns[1]: "Bydgoszcz",
                df.columns[2]: "Gdańsk",
                df.columns[3]: "Gdynia",
                df.columns[4]: "Katowice",
                df.columns[5]: "Kielce",
                df.columns[6]: "Kraków",
                df.columns[7]: "Lublin",
                df.columns[8]: "Łódź",
                df.columns[9]: "Olsztyn",
                df.columns[10]: "Opole",
                df.columns[11]: "Poznań",
                df.columns[12]: "Rzeszów",
                df.columns[13]: "Szczecin",
                df.columns[14]: "Warszawa",
                df.columns[15]: "Wrocław",
                df.columns[16]: "Zielona Góra"
            }, inplace=True)
    elif file_name == "wig20":
        file_name = file_name + "_q.csv"
        df = pd.read_csv(p / 'res' / file_name, usecols=[1])
    elif file_name == "mwig40":
        file_name = file_name + "_q.csv"
        df = pd.read_csv(p / 'res' / file_name, usecols=[1])
    elif file_name == "swig80":
        file_name = file_name + "_q.csv"
        df = pd.read_csv(p / 'res' / file_name, usecols=[1])
    elif file_name == "wig_budow":
        file_name = file_name + "_q.csv"
        df = pd.read_csv(p / 'res' / file_name, usecols=[1])
    elif file_name == "cpi_pl":
        file_name = file_name + ".csv"
        df = pd.read_csv(p / 'res' / file_name, usecols=[1])

    date_range = pd.date_range(start="2006q3", periods=len(df), freq="q")
    df.set_index(date_range, inplace=True)

    return df

def index_to_start(df_):
    df = df_ / df_.iloc[0] * 100

    return df

def multi_period_ret(period_ret):
    return np.prod(period_ret) - 1

def compute_real_prices(df):
    df_cpi = read_file_to_df('cpi_pl')
    df_cpi = df_cpi.div(100).cumprod()
    df_cpi = index_to_start(df_cpi.loc[df.index]).div(100)
    df_real = df.div(df_cpi.loc[df.index].values)

    return df_real

def compute_return(df):
    if len(df) < 5:
        df_ret = (df.iloc[-1] / df.iloc[0] - 1) * 100
    else:
        df_ret = (np.power(df.iloc[-1] / df.iloc[0], 4 / (len(df) - 1)) - 1) * 100

    return df_ret

def get_lineplot_title(mkt_type, offer_vs_trx, prx_type, chart_type, time_start, time_end):
    title_dict = {'pm': 'Primary', 'sm': 'Secondary', 'op': 'offer', 'tp': 'transaction', 'nom': 'nominal',
                  'real': 'real', 'act': '', 'ind': ' (indexed)'}
    title_template = '%s market %s %s prices%s between %s and %s'

    if len(offer_vs_trx) > 1:
        title = title_template % (
            title_dict[str(mkt_type)], title_dict[str(prx_type)], 'offer and transaction ', title_dict[str(chart_type)],
            'Q{}-{}'.format(time_start.quarter, time_start.year), 'Q{}-{}'.format(time_end.quarter, time_end.year))
    else:
        title = title_template % (
            title_dict[str(mkt_type)], title_dict[str(prx_type)], title_dict[str(offer_vs_trx[0])],
            title_dict[str(chart_type)], 'Q{}-{}'.format(time_start.quarter, time_start.year),
            'Q{}-{}'.format(time_end.quarter, time_end.year))

    return title

def get_barchart_title(time_range, mkt_type, offer_vs_trx, prx_type):
    title_dict = {'pm': 'primary', 'sm': 'secondary', 'op': 'offer', 'tp': 'transaction', 'nom': 'nominal',
                  'real': 'real'}
    title_template = 'Annualized geometric mean growth rate of %s market %s %s ' + 'prices in selected period.'
    title_template_short = 'Unannualized growth rate of %s market %s %s prices in selected period'

    if time_range[-1] - time_range[0] < 5:
        if len(offer_vs_trx) > 1:
            title = title_template_short % (title_dict[str(mkt_type)], title_dict[str(prx_type)],
                                            'offer and transaction ')
        else:
            title = title_template_short % (
                title_dict[str(mkt_type)], title_dict[str(prx_type)], title_dict[str(offer_vs_trx[0])])
    else:
        if len(offer_vs_trx) > 1:
            title = title_template % (title_dict[str(mkt_type)], title_dict[str(prx_type)], 'offer and transaction ')
        else:
            title = title_template % (
                title_dict[str(mkt_type)], title_dict[str(prx_type)], title_dict[str(offer_vs_trx[0])])

    return title

def calculate_streaks(df):
    df_ret = pd.DataFrame(df.div(df.shift(1)).sub(1) > 0)
    df_streak = pd.DataFrame(index=df.columns, columns=['up_streak', 'down_streak'])

    for i in df_streak.index:
        ups = df_ret[i].ne(df_ret[i].shift()).cumsum().loc[df_ret[df_ret[i]].index].value_counts().max()
        downs = df_ret[i].ne(df_ret[i].shift()).cumsum().loc[df_ret[~df_ret[i]].index].value_counts().max()
        df_streak['up_streak'].loc[i] = ups
        df_streak['down_streak'].loc[i] = downs

    return [df_streak.up_streak, df_streak.down_streak]

def calculate_index(df: pd.DataFrame, ind_weighting: str):

    if ind_weighting == "equal_weighted":

        const_number = df.shape[-1]

        weights = [1 / const_number] * const_number

        index_equal = df.pct_change().fillna(0).add(1).cumprod().mul(weights, axis=1).sum(axis=1).mul(1000)

        return index_equal

    elif ind_weighting == "price_weighted":

        df = df.fillna(method="ffill")

        weights = df.div(df.sum(axis=1).values, axis=0).shift().fillna(1 / df.shape[1])

        df_ret = df.pct_change()

        index_price = df_ret.fillna(0).add(1).cumprod().mul(weights, axis=1).sum(axis=1).mul(1000)

        return index_price

def combi_breakdown(combi):
    out = list()
    for item in combi:
        market = " ".join(item.split(" ")[0:2])
        prices = " ".join(item.split(" ")[2:4])
        city = item.split(" ")[-1]
        out.append([market, prices, city])

    return out