from __future__ import absolute_import
from fluent.migrate.helpers import transforms_from

def migrate(ctx):
    """Bug 1695792 - Update copy for FxA notifications. - part {index}"""
    ctx.add_transforms(
        "browser/browser/accounts.ftl",
        "browser/browser/accounts.ftl",
        transforms_from(
            """
account-finish-account-setup = { COPY(from_path, "account.finishAccountSetup") }
""",
            from_path="browser/chrome/browser/accounts.properties",
        ),
    )


import pageviewapi
import wikipedia
import requests
import json
import csv
import time

def parseCity(line):
    output = ""
    for char in line:
        if(char == ','):
            return output[11:]
        output += char

def parsePopulation(line):
    output = ""
    for char in line:
        if (char.isdigit()):
            output += char
    return output

def parseName(line):
    output = ""
    for char in line:
        if(char == ']' or char == '|'):
            if (char == ')'):
                output += char
            return output[3:]
        output += char

def createList():
    file = open('CitiesInIowa.xml', 'r')
    count = 0
    notableArr = [[]]
    cityArr = []
    cityName = ""
    addPeople = False

    for line in file:
        count = count + 1
        if ("<title>" in line and ("Category:" not in line)):
            if (len(cityArr) > 1):
                notableArr.append(cityArr)
            cityArr = []
            addPeople = False
            cityName = parseCity(line)
            cityArr = [cityName]
        if ("population_total" in line):
            cityArr.append(parsePopulation(line))
        if ("==" in line and "Notable" in line):
            addPeople = True
        if ("==" in line and not "Notable" in line):
            addPeople = False
        if((addPeople) and ("*[[" in line) and ("Category:" not in line) and ("File:" not in line) and ("NULLIFIED" not in line) and ("END OF NOTICE" not in line)):
            cityArr.append(parseName(line))

    file.close()
    return notableArr

def getPageViews(lis):
    headers = {'User-Agent': '(wvogelschmidt@colgate.edu)'}

    try:
        article = wikipedia.page(lis).url
        article = article[30:]
        url = "https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/en.wikipedia.org/all-access/user/" + article + "/monthly/20210101/20210201"
    except wikipedia.exceptions.PageError:
        print("Page Error LIS: " + lis)
        return 0
    except wikipedia.exceptions.DisambiguationError:
        print("Disambiguation Error LIS: " + lis)
        return 0
    try:
        resp = requests.get(url, headers=headers)
        rd = resp.json()
        print("success url: " + url)
        return rd['items'][0]['views']
    except wikipedia.exceptions.DisambiguationError:
        print("Disambiguation Error URL: " + url)
        return 0

def outputList(notableArr):
    file = open('output.csv', 'w')

    for array in notableArr:
        if(len(array) > 2):
            count = 0
            pageViews = 0
            maxViews = 0
            for lis in array:
                if(count < 2):
                    file.write(lis)
                    file.write(',')
                    count += 1
                else:
                    pgVw = getPageViews(lis)
                    if (pgVw > maxViews):
                        maxViews = pgVw
                    pageViews += pgVw
            file.write(str(len(array)-2) + ',')
            file.write(str(pageViews) + ',')
            file.write(str(maxViews))
            file.write('\n')
    file.close()

#Create and Output List
notableArr = createList()
outputList(notableArr)


import logging
import time
from ffxivlib import FFXIV_LIB
import keyboard
from datetime import datetime, timedelta

def get_action(action_id, hotbars):
    '''
    '''
    return get_action_pos(action_id, hotbars)[2]

def get_action_pos(action_id, hotbars):
    for b in range(10):
        hotbar = hotbars[b]
        for c in range(12):
            action = hotbar[c]
            if action.action_id == action_id:
                return b, c, action
    return None

def get_key_from_action_pos(pos):
    keys = ['1', '2', '3' ,'4' ,'5' , '6', '7', '8', '9', '0', '-', '^']
    if pos[0] == 0:
        return keys[pos[1]]
    if pos[0] == 1:
        return 'ctrl+'+ keys[pos[1]]
    return None

def do_action(action_id, hbars):
    ap = get_action_pos(action_id, hbars)
    if ap and ap[2].enable and ap[2].percent == 0 and ap[2].in_range:
        print('action:', action_id)
        # print(action_id, ap[2].enable, ap[2].percent, ap[2].in_range)
        key = get_key_from_action_pos(ap)
        keyboard.press_and_release(key)
        return True
    return False

# hoge = get_action_pos(100)
# print(hoge)
# import pdb; pdb.set_trace()

def main():
    while True:
        try:
            ffxiv = FFXIV_LIB()
            loop(ffxiv)
        except KeyboardInterrupt:
            return
        except Exception as e:
            time.sleep(10)

def loop(ffxiv):
    print('loop start')
    rain = False
    start = True
    # log 捨て
    ffxiv.get_new_chatlogs()
    last_log_time = datetime.now()

    while True:
        time.sleep(0.2)
        for log in ffxiv.get_new_chatlogs():
            last_log_time = datetime.now()
            if log.body.startswith('/reset'):
                print('reset')
                return
            if log.body.startswith('/start'):
                print('start')
                start = True
            if log.body.startswith('/stop'):
                print('stop')
                start = False
            if log.body.startswith('/rain'):
                rain = True
            if log.body.startswith('/blood'):
                rain = False
        if last_log_time < datetime.now() - timedelta(seconds=60):
            # チャットログが更新されてない場合はリスタート
            print('no logs long time. restart')
            return

        if start and ffxiv.is_active() and ffxiv.is_battle():
            ct = ffxiv.get_current_target()
            hbars = ffxiv.get_hotbars()
            jobhud = ffxiv.get_jobhud()
            myid = ffxiv.get_myid()
            if ct is not None:
                ct_mybuffs =  [x for x in ct.buffs if x.buff_provider == myid]
                blood = get_action(110, hbars)
                menu = get_action(3559, hbars)
                bara = get_action(114, hbars)
                pion = get_action(116, hbars)
                # 詩パート
                # 旅神のメヌエット
                if do_action(3559, hbars):
                    continue
                # 賢人のバラード 114
                if menu.cost < 50:
                    if do_action(114, hbars):
                        continue
                # 軍神のパイオン 116、バラード発動後30秒後
                if menu.cost < 20 and bara.cost < 50:
                    if do_action(116, hbars):
                        continue

                # アビリティパート
                # 猛者の撃 101
                if do_action(101, hbars):
                    continue

                # バトルボイス 118
                if do_action(118, hbars):
                    continue

                # 乱れうち 107
                if do_action(107, hbars):
                    continue

                # ピッチパーフェクト
                if jobhud.hudtype==3 and (  jobhud.count == 3 or
                                            jobhud.remain <= 1000):
                    if do_action(7404, hbars):
                        continue

                # エンピリアルアロー
                if do_action(3558, hbars):
                    continue

                # サイドワインダー
                if any([x.id == 1310844 or x.id == 2622640 for x in ct_mybuffs]) and any([x.id == 1310849 or x.id == 2622641 for x in ct_mybuffs]):
                    if do_action(3562, hbars):
                        continue

                # アイアンジョー
                dot_debuffs = [x for x in ct_mybuffs if x.id in [1310844,2622640,1310849,2622641]]
                if len(dot_debuffs) > 0:
                    dot_debuffs.sort(key=lambda x: x.time_left)
                    if dot_debuffs[0].time_left < 5:
                         if do_action(3560, hbars):
                            continue

                # Weapon Skill
                # ストレートショット 98
                if do_action(98, hbars):
                    continue
                # ベノムバイト　100 debuff:1310844
                # コースティックバイト  2622640
                if not any([x.id == 1310844 or x.id == 2622640 for x in ct_mybuffs]):
                    if do_action(100, hbars):
                        continue

                # ウインドバイト　113 debuff:1310849
                # ストームバイト  113 2622641
                if not any([x.id == 1310849 or x.id == 2622641 for x in ct_mybuffs]):
                    if do_action(113, hbars):
                        continue

                if rain:
                    # レインオオブデス 117
                    if do_action(117, hbars):
                        continue
                    # クイックノック 106
                    if do_action(106, hbars):
                        continue
                else:
                    # ブラッドレッター 110
                    if do_action(110, hbars):
                        continue
                    # ヘヴィショット 97
                    if do_action(97, hbars):
                        continue

if __name__ == "__main__":
    main()


from __future__ import annotations

import datetime
from typing import TYPE_CHECKING, Literal

from public import public

if TYPE_CHECKING:
    import pandas as pd
    from ibis.expr import types as ir

import ibis.expr.operations as ops
from ibis.expr.types.core import _binop
from ibis.expr.types.generic import Column, Scalar, Value

@public
class TemporalValue(Value):
    def strftime(self, format_str: str) -> ir.StringValue:
        """Format timestamp according to `format_str`.

        Format string may depend on the backend, but we try to conform to ANSI
        `strftime`.

        Parameters
        ----------
        format_str
            `strftime` format string

        Returns
        -------
        StringValue
            Formatted version of `arg`
        """
        return ops.Strftime(self, format_str).to_expr()

@public
class TemporalScalar(Scalar, TemporalValue):
    pass  # noqa: E701,E302

@public
class TemporalColumn(Column, TemporalValue):
    pass  # noqa: E701,E302

class _DateComponentMixin:
    """Temporal expressions that have a date component.

    Currently this includes
    [`TimestampValue`][ibis.expr.types.temporal.TimestampValue]s and
    [`DateValue`][ibis.expr.types.temporal.DateValue]s
    """

    def epoch_seconds(self) -> ir.IntegerValue:
        """Extract UNIX epoch in seconds."""
        return ops.ExtractEpochSeconds(self).to_expr()

    def year(self) -> ir.IntegerValue:
        """Extract the year component."""
        return ops.ExtractYear(self).to_expr().name("year")

    def month(self) -> ir.IntegerValue:
        """Extract the month component."""
        return ops.ExtractMonth(self).to_expr().name("month")

    def day(self) -> ir.IntegerValue:
        """Extract the day component."""
        return ops.ExtractDay(self).to_expr().name("day")

    @property
    def day_of_week(self) -> DayOfWeek:
        """A namespace of methods for extracting day of week information.

        Returns
        -------
        DayOfWeek
            An namespace expression containing methods to use to extract
            information.
        """
        return DayOfWeek(self)

    def day_of_year(self) -> ir.IntegerValue:
        """Extract the day of the year component."""
        return ops.ExtractDayOfYear(self).to_expr()

    def quarter(self) -> ir.IntegerValue:
        """Extract the quarter component."""
        return ops.ExtractQuarter(self).to_expr()

    def week_of_year(self) -> ir.IntegerValue:
        """Extract the week of the year component."""
        return ops.ExtractWeekOfYear(self).to_expr()

class _TimeComponentMixin:
    """Temporal expressions that have a time component.

    Currently this includes
    [`TimestampValue`][ibis.expr.types.temporal.TimestampValue]s and
    [`TimeValue`][ibis.expr.types.temporal.TimeValue]s
    """

    def time(self) -> TimeValue:
        """Return the time component of the expression.

        Returns
        -------
        TimeValue
            The time component of `self`
        """
        return ops.Time(self).to_expr()

    def hour(self) -> ir.IntegerValue:
        """Extract the hour component."""
        return ops.ExtractHour(self).to_expr().name("hour")

    def minute(self) -> ir.IntegerValue:
        """Extract the minute component."""
        return ops.ExtractMinute(self).to_expr().name("minute")

    def second(self) -> ir.IntegerValue:
        """Extract the second component."""
        return ops.ExtractSecond(self).to_expr().name("second")

    def millisecond(self) -> ir.IntegerValue:
        """Extract the millisecond component."""
        return ops.ExtractMillisecond(self).to_expr().name("millisecond")

    def between(
        self,
        lower: str | datetime.time | TimeValue,
        upper: str | datetime.time | TimeValue,
        timezone: str | None = None,
    ) -> ir.BooleanValue:
        """Check if the expr falls between `lower` and `upper`, inclusive.

        Adjusts according to `timezone` if provided.

        Parameters
        ----------
        lower
            Lower bound
        upper
            Upper bound
        timezone
            Time zone

        Returns
        -------
        BooleanValue
            Whether `self` is between `lower` and `upper`, adjusting `timezone`
            as needed.
        """
        import ibis.expr.datatypes as dt

        op = self.op()
        if isinstance(op, ops.Time):
            # Here we pull out the first argument to the underlying Time
            # operation which is by definition (in _timestamp_value_methods) a
            # TimestampValue. We do this so that we can potentially specialize
            # the "between time" operation for
            # timestamp_value_expr.time().between(). A similar mechanism is
            # triggered when creating expressions like
            # t.column.distinct().count(), which is turned into
            # t.column.nunique().
            arg = op.arg.to_expr()
            if timezone is not None:
                arg = arg.cast(dt.Timestamp(timezone=timezone))
            op_cls = ops.BetweenTime
        else:
            arg = self
            op_cls = ops.Between

        return op_cls(arg, lower, upper).to_expr()

@public
class TimeValue(_TimeComponentMixin, TemporalValue):
    def truncate(
        self,
        unit: Literal["h", "m", "s", "ms", "us", "ns"],
    ) -> TimeValue:
        """Truncate the expression to a time expression in units of `unit`.

        Commonly used for time series resampling.

        Parameters
        ----------
        unit
            The unit to truncate to

        Returns
        -------
        TimeValue
            `self` truncated to `unit`
        """
        return ops.TimeTruncate(self, unit).to_expr()

    def __add__(
        self,
        other: datetime.timedelta | pd.Timedelta | IntervalValue,
    ) -> TimeValue | NotImplemented:
        """Add an interval to a time expression."""
        return _binop(ops.TimeAdd, self, other)

    add = radd = __radd__ = __add__

    def __sub__(
        self,
        other: TimeValue | IntervalValue,
    ) -> IntervalValue | TimeValue | NotImplemented:
        """Subtract a time or an interval from a time expression."""
        import ibis.expr.rules as rlz

        other = rlz.any(other)

        if other.output_dtype.is_time():
            op = ops.TimeDiff
        else:
            op = ops.TimeSub  # let the operation validate

        return _binop(op, self, other)

    sub = __sub__

    def __rsub__(
        self,
        other: TimeValue | IntervalValue,
    ) -> IntervalValue | TimeValue | NotImplemented:
        """Subtract a time or an interval from a time expression."""
        import ibis.expr.rules as rlz

        other = rlz.any(other)

        if other.output_dtype.is_time():
            op = ops.TimeDiff
        else:
            op = ops.TimeSub  # let the operation validate

        return _binop(op, other, self)

    rsub = __rsub__

@public
class TimeScalar(TemporalScalar, TimeValue):
    pass  # noqa: E701,E302

@public
class TimeColumn(TemporalColumn, TimeValue):
    pass  # noqa: E701,E302

@public
class DateValue(TemporalValue, _DateComponentMixin):
    def truncate(self, unit: Literal["Y", "Q", "M", "W", "D"]) -> DateValue:
        """Truncate date expression to units of `unit`.

        Parameters
        ----------
        unit
            Unit to truncate `arg` to

        Returns
        -------
        DateValue
            Truncated date value expression
        """
        return ops.DateTruncate(self, unit).to_expr()

    def __add__(
        self,
        other: datetime.timedelta | pd.Timedelta | IntervalValue,
    ) -> DateValue | NotImplemented:
        """Add an interval to a date."""
        return _binop(ops.DateAdd, self, other)

    add = radd = __radd__ = __add__

    def __sub__(
        self,
        other: datetime.date
        | DateValue
        | datetime.timedelta
        | pd.Timedelta
        | IntervalValue,
    ) -> IntervalValue | DateValue | NotImplemented:
        """Subtract a date or an interval from a date."""
        import ibis.expr.rules as rlz

        other = rlz.one_of([rlz.date, rlz.interval], other)

        if other.output_dtype.is_date():
            op = ops.DateDiff
        else:
            op = ops.DateSub  # let the operation validate

        return _binop(op, self, other)

    sub = __sub__

    def __rsub__(
        self,
        other: datetime.date
        | DateValue
        | datetime.timedelta
        | pd.Timedelta
        | IntervalValue,
    ) -> IntervalValue | DateValue | NotImplemented:
        """Subtract a date or an interval from a date."""
        import ibis.expr.rules as rlz

        other = rlz.one_of([rlz.date, rlz.interval], other)

        if other.output_dtype.is_date():
            op = ops.DateDiff
        else:
            op = ops.DateSub  # let the operation validate

        return _binop(op, other, self)

    rsub = __rsub__

@public
class DateScalar(TemporalScalar, DateValue):
    pass  # noqa: E701,E302

@public
class DateColumn(TemporalColumn, DateValue):
    pass  # noqa: E701,E302

@public
class TimestampValue(_DateComponentMixin, _TimeComponentMixin, TemporalValue):
    def truncate(
        self,
        unit: Literal["Y", "Q", "M", "W", "D", "h", "m", "s", "ms", "us", "ns"],
    ) -> TimestampValue:
        """Truncate timestamp expression to units of `unit`.

        Parameters
        ----------
        unit
            Unit to truncate to

        Returns
        -------
        TimestampValue
            Truncated timestamp expression
        """
        return ops.TimestampTruncate(self, unit).to_expr()

    def date(self) -> DateValue:
        """Return the date component of the expression.

        Returns
        -------
        DateValue
            The date component of `self`
        """
        return ops.Date(self).to_expr()

    def __add__(
        self,
        other: datetime.timedelta | pd.Timedelta | IntervalValue,
    ) -> TimestampValue | NotImplemented:
        """Add an interval to a timestamp."""
        return _binop(ops.TimestampAdd, self, other)

    add = radd = __radd__ = __add__

    def __sub__(
        self,
        other: datetime.datetime
        | pd.Timestamp
        | TimestampValue
        | datetime.timedelta
        | pd.Timedelta
        | IntervalValue,
    ) -> IntervalValue | TimestampValue | NotImplemented:
        """Subtract a timestamp or an interval from a timestamp."""
        import ibis.expr.rules as rlz

        right = rlz.any(other)

        if right.output_dtype.is_timestamp():
            op = ops.TimestampDiff
        else:
            op = ops.TimestampSub  # let the operation validate

        return _binop(op, self, other)

    sub = __sub__

    def __rsub__(
        self,
        other: datetime.datetime
        | pd.Timestamp
        | TimestampValue
        | datetime.timedelta
        | pd.Timedelta
        | IntervalValue,
    ) -> IntervalValue | TimestampValue | NotImplemented:
        """Subtract a timestamp or an interval from a timestamp."""
        import ibis.expr.rules as rlz

        right = rlz.any(other)

        if right.output_dtype.is_timestamp():
            op = ops.TimestampDiff
        else:
            op = ops.TimestampSub  # let the operation validate

        return _binop(op, other, self)

    rsub = __rsub__

@public
class TimestampScalar(TemporalScalar, TimestampValue):
    pass  # noqa: E701,E302

@public
class TimestampColumn(TemporalColumn, TimestampValue):
    pass  # noqa: E701,E302

@public
class IntervalValue(Value):
    def to_unit(self, target_unit: str) -> IntervalValue:
        """Convert this interval to units of `target_unit`."""

        # TODO(kszucs): should use a separate operation for unit conversion
        # which we can rewrite/simplify to integer multiplication/division
        return ops.ToIntervalUnit(self, unit=target_unit).to_expr()

    @property
    def years(self) -> ir.IntegerValue:
        """Extract the number of years from an interval."""
        return self.to_unit("Y")

    @property
    def quarters(self) -> ir.IntegerValue:
        """Extract the number of quarters from an interval."""
        return self.to_unit("Q")

    @property
    def months(self) -> ir.IntegerValue:
        """Extract the number of months from an interval."""
        return self.to_unit("M")

    @property
    def weeks(self) -> ir.IntegerValue:
        """Extract the number of weeks from an interval."""
        return self.to_unit("W")

    @property
    def days(self) -> ir.IntegerValue:
        """Extract the number of days from an interval."""
        return self.to_unit("D")

    @property
    def hours(self) -> ir.IntegerValue:
        """Extract the number of hours from an interval."""
        return self.to_unit("h")

    @property
    def minutes(self) -> ir.IntegerValue:
        """Extract the number of minutes from an interval."""
        return self.to_unit("m")

    @property
    def seconds(self) -> ir.IntegerValue:
        """Extract the number of seconds from an interval."""
        return self.to_unit("s")

    @property
    def milliseconds(self) -> ir.IntegerValue:
        """Extract the number of milliseconds from an interval."""
        return self.to_unit("ms")

    @property
    def microseconds(self) -> ir.IntegerValue:
        """Extract the number of microseconds from an interval."""
        return self.to_unit("us")

    @property
    def nanoseconds(self) -> ir.IntegerValue:
        """Extract the number of nanoseconds from an interval."""
        return self.to_unit("ns")

    def __add__(
        self,
        other: datetime.timedelta | pd.Timedelta | IntervalValue,
    ) -> IntervalValue | NotImplemented:
        """Add this interval to `other`."""
        return _binop(ops.IntervalAdd, self, other)

    add = radd = __radd__ = __add__

    def __sub__(
        self,
        other: datetime.timedelta | pd.Timedelta | IntervalValue,
    ) -> IntervalValue | NotImplemented:
        """Subtract `other` from this interval."""
        return _binop(ops.IntervalSubtract, self, other)

    sub = __sub__

    def __rsub__(
        self,
        other: datetime.timedelta | pd.Timedelta | IntervalValue,
    ) -> IntervalValue | NotImplemented:
        """Subtract `other` from this interval."""
        return _binop(ops.IntervalSubtract, other, self)

    rsub = __rsub__

    def __mul__(
        self,
        other: int | ir.IntegerValue,
    ) -> IntervalValue | NotImplemented:
        """Multiply this interval by `other`."""
        return _binop(ops.IntervalMultiply, self, other)

    mul = rmul = __rmul__ = __mul__

    def __floordiv__(
        self,
        other: ir.IntegerValue,
    ) -> IntervalValue | NotImplemented:
        """Floor-divide this interval by `other`."""
        return _binop(ops.IntervalFloorDivide, self, other)

    floordiv = __floordiv__

    def negate(self) -> ir.IntervalValue:
        """Negate an interval expression.

        Returns
        -------
        IntervalValue
            A negated interval value expression
        """
        op = self.op()
        if hasattr(op, "negate"):
            result = op.negate()
        else:
            result = ops.Negate(self)

        return result.to_expr()

    __neg__ = negate

    @staticmethod
    def __negate_op__():

        return ops.Negate

@public
class IntervalScalar(Scalar, IntervalValue):
    pass  # noqa: E701,E302

@public
class IntervalColumn(Column, IntervalValue):
    pass  # noqa: E701,E302

@public
class DayOfWeek:
    """A namespace of methods for extracting day of week information."""

    def __init__(self, expr):
        self._expr = expr

    def index(self):
        """Get the index of the day of the week.

        Returns
        -------
        IntegerValue
            The index of the day of the week.

            !!! note "Ibis follows pandas' conventions for day numbers: Monday = 0 and Sunday = 6."
        """  # noqa: E501
        return ops.DayOfWeekIndex(self._expr).to_expr()

    def full_name(self):
        """Get the name of the day of the week.

        Returns
        -------
        StringValue
            The name of the day of the week
        """
        return ops.DayOfWeekName(self._expr).to_expr()


import os
import numpy as np
import cv2
import torch
import torch.backends.cudnn as cudnn
from model.FaceBoxes import FaceBoxes, OriginalFaceBoxes
from model.LocConf import ConfLocFaceBoxes, ConfLoc
from model.HRFaceBoxes import HRFaceBoxes
from model.SemanticFaceBoxes import SemanticFaceBoxes
from utils.default_boxes import DefaultBox, OriginalDefaultBox, ConfLocBox, HRBoxes, SemanticBoxes
from utils.box_utils import nms, decode
from loss_and_metrics.loss_and_metrics import NewMultiBoxesLoss, MultiBoxesLoss
import config
from utils.data_load import AnnotationTransform, ToTensor

GPU = [0]

def _pad_to_square_and_resize(image, rgb_mean, size):
    """
    将图像pad成正方形，pad在右下方，pad的值是数据集的rgb均值
    """
    height, width, _ = image.shape
    long_side = max(width, height)
    image_t = np.empty((long_side, long_side, 3), dtype=image.dtype)
    image_t[:, :] = rgb_mean
    image_t[0:0 + height, 0:0 + width] = image
    interp_methods = [cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_NEAREST, cv2.INTER_LANCZOS4]
    interp_method = interp_methods[np.random.randint(0, 5, 1)[0]]
    image_t = cv2.resize(image_t, (size, size), interpolation=interp_method)
    return image_t

def test(name, model_path, confidence_threshold, top_k, nms_threshold):
    cfg = config
    if name == 'OriginalFaceBoxes':
        model = OriginalFaceBoxes(phase='test', num_classes=cfg.num_classes)
        default_boxes = OriginalDefaultBox(img_size=(cfg.img_dim2, cfg.img_dim2)).forward().cuda(device=GPU[0])
        img_dim = cfg.img_dim2
    elif name == 'HRFaceBoxes':
        model = HRFaceBoxes(phase='test', num_classes=cfg.num_classes)
        default_boxes = HRBoxes(img_size=(cfg.img_dim2, cfg.img_dim2)).forward().cuda(device=GPU[0])
        img_dim = cfg.img_dim2

    cudnn.benchmark = True
    if torch.cuda.is_available():
        model = model.cuda(device=GPU[0])
        model = torch.nn.DataParallel(model, device_ids=GPU)
    if model_path is not None:
        model_dict = torch.load(os.path.join(cfg.save_path, model_path))
        # print(model_dict)
        model.load_state_dict(model_dict)

    fw = open(os.path.join(cfg.save_path, 'World_Largest_Selfie.txt'), 'w')
    #
    # criterion = NewMultiBoxesLoss(num_classes=config.num_classes, threshold_for_pos=cfg.threshold_for_pos - 0.1,
    #                               threshold_for_neg=cfg.threshold_for_neg,
    #                               ratio_between_neg_and_pos=cfg.ratio_between_neg_and_pos*1)
    # criterion = MultiBoxesLoss(config.num_classes, 0.5, 0.2, 3)

    val_images = np.load(os.path.join(os.getcwd(), 'utils', 'data_list', 'val_images.npy'), allow_pickle=True)
    val_label = np.load(os.path.join(os.getcwd(), 'utils', 'data_list', 'val_label.npy'), allow_pickle=True)
    afw_images = np.load(os.path.join(os.getcwd(), 'utils', 'data_list', 'afw.npy'), allow_pickle=True)
    pascal_images = np.load(os.path.join(os.getcwd(), 'utils', 'data_list', 'pascal.npy'), allow_pickle=True)
    model.eval()
    for i in range(len(val_images) - 3000):
        image = cv2.imread(val_images[i])
        image_name = val_images[i][-val_images[i][::-1].index('/'):]
        # image = cv2.imread('World Largest Selfie.jpg')
        # image_name = 'World Largest Selfie det.jpg'
        # print(image_name)
        # cv2.imwrite(os.path.join(cfg.save_image_path, str(i) + '.jpg'), image)   # val_images[i][59:]
        # label = val_label[i]
        # image, targets = AnnotationTransform()((image, label))
        # boxes_t = targets[:, :-1].copy()
        # labels_t = targets[:, -1].copy()
        # boxes_t[:, 0::2] = boxes_t[:, 0::2] / 1024
        # boxes_t[:, 1::2] = boxes_t[:, 1::2] / 1024
        # labels_t = np.expand_dims(labels_t, 1)
        # targets_t = np.hstack([np.clip(boxes_t, a_max=1, a_min=0), labels_t])  # [num_object, 5]
        height, width, _ = image.shape
        long_side = max(width, height)
        image_t = _pad_to_square_and_resize(image, cfg.rgb_mean, img_dim)
        image_t = image_t - cfg.rgb_mean
        image_t = image_t.transpose(2, 0, 1)
        image_t = torch.from_numpy(image_t).unsqueeze(0).float()
        # targets_t = torch.from_numpy(targets_t).float()
        # image_t = torch.cat([image_t, image_t], dim=0)
        # label = []
        # label.append(targets_t)
        # label.append(targets_t)
        if torch.cuda.is_available():
            # image_t, targets_t = image_t.cuda(device=GPU[0]), targets_t.unsqueeze(0).cuda(device=GPU[0])
            # image_t, targets_t = image_t.cuda(device=GPU[0]), [anno.cuda(device=GPU[0]) for anno in label]
            image_t = image_t.cuda(device=GPU[0])
        loc_p, conf_p = model(image_t)
        # loss_loc, loss_conf = criterion((loc_p, conf_p), targets_t, default_boxes)
        # print(loss_loc, loss_conf)
        boxes = decode(loc_p.data.squeeze(0), default_boxes.data, cfg.variance)
        boxes = boxes * long_side
        boxes = boxes.cpu().numpy()
        scores = conf_p.squeeze(0).data.cpu().numpy()[:, -1]
        inds = np.where(scores > confidence_threshold)[0]
        boxes = boxes[inds]
        scores = scores[inds]

        order = scores.argsort()[::-1][:top_k]
        boxes = boxes[order]
        scores = scores[order]
        keep = nms(boxes, scores, nms_threshold)
        boxes = boxes[keep]
        scores = scores[keep]

        for det in zip(boxes, scores):
            box = det[0:-1][0]
            score = det[-1]
            if score < 0.5:
                continue
            # 记录每一个框
            xmin = box[0]
            ymin = box[1]
            xmax = box[2]
            ymax = box[3]
            fw.write('{:s} {:.3f} {:.1f} {:.1f} {:.1f} {:.1f}\n'.format(image_name, score, xmin, ymin, xmax, ymax))
            # 可视化
            text = "{:.4f}".format(score)
            box = list(map(int, box))

            cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]), (0, 0, 255), 2)
            cx = box[0]
            cy = box[1] + 12
            cv2.putText(image, text, (cx, cy),
                        cv2.FONT_HERSHEY_DUPLEX, 0.5, (255, 255, 255))
        cv2.imwrite(os.path.join(cfg.save_image_path, image_name), image)   # val_images[i][59:]
        # cv2.imwrite(image_name, image)
    fw.close()

if __name__ == '__main__':
    test('OriginalFaceBoxes', 'OriginalFaceBoxes_87.pth', 0.3, 200, 0.3)
    # test('HRFaceBoxes', 'HRFaceBoxes_147.pth', 0.3, 10000, 0.3)


from django.shortcuts import render
from .models import Product, ProductForm, Question, Option
from datetime import datetime

# Create your views here.
def form_view(request):
    pf = ProductForm(request.POST or None)
    if request.method == 'POST':
        if pf.is_valid():
            product = pf.save(commit = False)
            product.timestamp = datetime.now()
            product.save()
            return render(request,'app1/product.html',{'product':product})
    return render(request,'app1/index.html',{'pf':pf})

def products_view(request):
    products = Product.objects.all()
    return render(request,'app1/allproducts.html',{'products':products})

def ques_view(request):
    if request.method == "POST":
        question = Question.objects.all()
        optionSelected = request.POST['question']
        print(optionSelected)
        option = Option.objects.filter(value=optionSelected)[0]
        print(option)
        option.number += 1
        option.save()
        options = Option.objects.all()
        return render(request,'app1/form.html',{'options':options})
    question = Question.objects.all()
    return render(request,'app1/form.html',{'question':question[0].question, 'options':question[0].options.all()})


import json
import os
import zipfile
from tqdm import tqdm
import wget

from db import insert_company_data, create_table

# Обработка json
def handle_json(json_payload, file_path):
    try:
        # Цикл проходит по всем компаниям в json файле
        for company_info in json_payload:
            try:
                # В случае нахождения ОКВЭД 62, записывает в базу данных информацию по компании
                if company_info['data']['СвОКВЭД']['СвОКВЭДОсн']['КодОКВЭД'] == '62':
                    sv_address_legal = company_info['data']['СвАдресЮЛ']['АдресРФ']
                    # Формирования адреса в виде удобной строки для чтения
                    place_registration = f"{sv_address_legal['Индекс']}, {sv_address_legal['Регион']['НаимРегион']} " \
                                         f"{sv_address_legal['Регион']['ТипРегион']}, {sv_address_legal['Город']['ТипГород']}" \
                                         f"{sv_address_legal['Город']['НаимГород']}, {sv_address_legal['Улица']['ТипУлица']}" \
                                         f"{sv_address_legal['Улица']['НаимУлица']}, {sv_address_legal['Дом']}, " \
                                         f"{sv_address_legal['Кварт']}"
                    insert_company_data(company_info['name'], company_info['data']['СвОКВЭД']['СвОКВЭДОсн']['КодОКВЭД'],
                                        int(company_info['inn']), int(company_info['kpp']), place_registration)
            except KeyError:
                pass
    except UnicodeDecodeError:
        print(f'Не удалось десериализовать файл: {file_path}')

# Обработка всех файлов в zip архиве
def handle_zip_file(path):
    with zipfile.ZipFile(path, mode="r") as archive:
        for filename in tqdm(archive.namelist()):
            templates = archive.open(filename, 'r')
            json_payload = json.load(templates)
            handle_json(json_payload, filename)

# GitHub action test 4
if __name__ == '__main__':
    # Создает таблицу в базе, если её нет
    create_table()

    if os.path.exists('egrul.json.zip'):
        handle_zip_file('egrul.json.zip')
    else:
        print('Скачивания файла...')
        wget.download('https://ofdata.ru/open-data/download/egrul.json.zip', 'egrul.json.zip')

        handle_zip_file('egrul.json.zip')


# coding=utf-8
import requests

from jedis import jedis
from bs4 import BeautifulSoup

table_name = "China_it_top_100_company_info"
def get_it_top100():
    url = "http://www.sohu.com/a/162100864_608782"
    req = requests.Session()
    re = jedis.jedis()
    re.clear_list(table_name)
    res = req.get(url=url)
    content = res.content.decode("utf-8")
    parse_info(content, re)

def parse_info(content, re):
    soup = BeautifulSoup(content, "html5lib")
    company_list = soup.find_all('tr')
    for i in range(1, 100):
        infos = company_list[i].text.split("\n")
        company_name = infos[2].strip()
        short_name = infos[3].strip()
        product = infos[4].strip()
        re.save_dict(table_name, data=dict(company_name=company_name, short_name=[short_name], product=product))
    re.add_to_file_local(table_name)
    print("中国IT企业100强获取完成")

if __name__ == '__main__':
    get_it_top100()


import time
from functools import wraps

def fn_timer(function):
    @wraps(function)
    def function_timer(*args, **kwargs):
        t0 = time.time()
        result = function(*args, **kwargs)
        t1 = time.time()
        print("Time usage of function '{}' is {:.5f}s".format(function.__name__, t1 - t0))
        return result
    return function_timer

@fn_timer
def say_hello(name):
    print('Hello {}'.format(name))

if __name__ == '__main__':
    say_hello('Vincent')


import argparse
import os

import torch
import torch.optim as optim
from sklearn.metrics import accuracy_score
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision import transforms
from tqdm import tqdm

from models.lenet5 import lenet5

def train_one_epoch(data_loader, model, criterion, optimizer, device):
    running_loss = 0
    y_true = []
    y_predict = []
    model.train()
    for (batch_x, batch_y) in tqdm(data_loader, desc='Training'):
        batch_x = batch_x.to(device)
        batch_y = batch_y.to(device)

        optimizer.zero_grad()
        output = model(batch_x)
        loss = criterion(output, batch_y)
        loss.backward()
        optimizer.step()

        running_loss += loss.item()

        output = torch.argmax(output, dim=1)
        y_true.append(batch_y)
        y_predict.append(output)

    y_true = torch.cat(y_true, 0)
    y_predict = torch.cat(y_predict, 0)

    return running_loss / len(data_loader), accuracy_score(y_true.cpu(), y_predict.cpu())

def evaluate(data_loader, model, device):
    model.eval()  # switch to eval status
    y_true = []
    y_predict = []
    with torch.no_grad():  # 在进行预测时禁用不必要的梯度计算以节省计算资源,是一种优化方法,不写也可以运行
        for (batch_x, batch_y) in tqdm(data_loader, desc='Testing'):
            batch_x = batch_x.to(device)
            batch_y = batch_y.to(device)
            batch_y_predict = model(batch_x)
            batch_y_predict = torch.argmax(batch_y_predict, dim=1)
            y_true.append(batch_y)
            y_predict.append(batch_y_predict)

        y_true = torch.cat(y_true, 0)
        y_predict = torch.cat(y_predict, 0)

    return accuracy_score(y_true.cpu(), y_predict.cpu())

def train(args):
    # 加载数据集,这里下载pytorch框架自带的数据集mnist，后续需要自己写代码加载数据
    # 注意需要把数据格式变为tensor
    transform = transforms.Compose([
        transforms.ToTensor()
    ])
    train_datas = datasets.MNIST(root=args.dataset_path, train=True, download=True, transform=transform)
    test_datas = datasets.MNIST(root=args.dataset_path, train=False, download=True, transform=transform)

    # 加载loader
    train_loader = DataLoader(train_datas, batch_size=args.batch_size, shuffle=False)
    test_loader = DataLoader(test_datas, batch_size=args.batch_size, shuffle=False)

    # 配置网络
    model = lenet5(input_channels=1, num_classes=10).to(device)

    # 配置优化器，这里用的是SGD，也可用adam等其他的优化器
    optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=0.9, weight_decay=5e-4)
    scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=50, gamma=0.1)

    # 配置损失函数
    criterion = torch.nn.CrossEntropyLoss()
    model_savepath = "./checkpoints/mnist-lenet5.pth"

    if os.path.exists(model_savepath):
        # 如果存在模型，读取一下并测试性能
        print("model exist")
        print(f"Load model from : {model_savepath}")
        model.load_state_dict(torch.load(model_savepath), strict=True)
        test_acc = evaluate(test_loader, model, device)
        print(f"Test Acc: {test_acc}")

    else:
        # 如果没有模型，则重新训练一个模型
        print("model not exist")
        print(f"Start training for {args.epochs} epochs")
        for epoch in range(args.epochs):
            train_loss, train_acc = train_one_epoch(train_loader, model, criterion, optimizer, device)
            test_acc = evaluate(test_loader, model, device)
            print(f"# EPOCH {epoch}  Train Loss: {train_loss} Train Acc: {train_acc} Test Acc: {test_acc}\n")

            scheduler.step()
            torch.save(model.state_dict(), model_savepath)

if __name__ == "__main__":
    # 命令行参数
    parser = argparse.ArgumentParser(description='Train a model ')

    parser.add_argument('--gpu', default='0', help='Which gpu to use, default:0')
    parser.add_argument('--epochs', default=50, help='Number of epochs to train backdoor model, default: 10')
    parser.add_argument('--batch_size', type=int, default=64, help='Batch size to split dataset, default: 64')
    parser.add_argument('--lr', type=float, default=0.1, help='Learning rate of the model, default: 0.1')
    parser.add_argument('--dataset_path', default='./data/', help='Place to load dataset, default: ./data/')

    args = parser.parse_args()

    # gpu若存在则用gpu，不然用cpu
    os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    train(args)


import json
import os
SETTING = None

def _load_setting(setting):
    global SETTING
    with open(setting) as file:
        SETTING = json.load(file)

_load_setting(os.path.join(os.path.dirname(os.path.abspath(__file__)), "settings.json"))


from datetime import datetime, timedelta
from airflow import DAG
from airflow.operators.dummy_operator import DummyOperator
from airflow.operators.python_operator import PythonOperator

# Define the Python function to be executed
def print_hello():
    return 'Hello from the PythonOperator!'

# Set the default arguments for the DAG
default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'email_on_failure': False,
    'email_on_retry': False,
    'retries': 1,
    'retry_delay': timedelta(minutes=1),
    'start_date': datetime(2023, 1, 1),
}

# Define the DAG
dag = DAG(
    'hello_world_dag',
    default_args=default_args,
    description='A simple Hello World DAG',
    schedule_interval=timedelta(days=1),
    catchup=False,
)

# Define the tasks
start_task = DummyOperator(
    task_id='start_task',
    dag=dag,
)

hello_task = PythonOperator(
    task_id='hello_task',
    python_callable=print_hello,
    dag=dag,
)

end_task = DummyOperator(
    task_id='end_task',
    dag=dag,
)

# Set the task dependencies
start_task >> hello_task >> end_task


"""Base Invertible Module Class"""

from typing import Tuple, Iterable
import tensorflow as tf

class InvertibleModule(tf.keras.layers.Layer):
    """
    Generic invertible layer for invertible
    neural network structures.

    Used to implement `Nice`, `Glow`, `RNVP` etc.
    """

    def __init__(self, dims_in: Tuple[int], dims_c: Iterable[Tuple[int]] = None):
        """
        Args:
            dims_in: a tuple specifying the shape of the input,
                     excluding the batch dimension, to this
                     operator: ``dims_in = (dim_0,..., channels)``
            dims_c:  a list of tuples specifying the shape
                     of the conditions to this operator,
                     excluding the batch dimension

        ** Note  to implementors:**

        - The shapes are in the standard TensorFlow 'channels_last' format.
        """
        super().__init__()
        if dims_c is None:
            dims_c = []
        self.dims_in = tuple(dims_in)
        self.dims_c = list(dims_c)

    def call(  # pylint: disable=W0221
        self,
        x_or_z: tf.Tensor,
        c: Iterable[tf.Tensor] = None,
        rev: bool = False,
        jac: bool = True,
    ):
        """
        Perform a forward (default, ``rev=False``) or
        backward pass (``rev=True``) through this layer.
        Args:
            x_or_z: input data (array-like of one or more tensors)
            c:      conditioning data (array-like of none or more tensors)
            rev:    perform backward pass
            jac:    return Jacobian associated to the direction
        """
        raise NotImplementedError(
            f"{self.__class__.__name__} does not provide call(...) method"
        )

    def get_config(self):
        "Needed within TensorFlow to serialize this layer"
        config = {"dims_in": self.dims_in, "dims_c": self.dims_c}
        base_config = super().get_config()
        return dict(list(base_config.items()) + list(config.items()))


import pandas as pd
from datetime import datetime, timedelta
import yahoo_fin.stock_info as si
import yfinance as yf
import streamlit as st
import re

#define dataframe table hover style
def hover(hover_color="blue"):
        return dict(selector="td:hover",
                    props=[("background-color", "%s" % hover_color)])

#define table style function
def set_styles(results):
    table_styles = [
                hover(),
                dict(selector="th", props=[("visibility", "show")])
                ]
    return (
        results.style.set_table_styles(table_styles)
        .set_properties(**{"background-color": "black", "color": "white"
        })

    )

#function to clean the index text
@st.cache
def format_cols(df):
    loops = len(df) + 1
    list_index = []
    for i in df.index:
        list_index.append(re.sub(r"(\w)([A-Z])", r"\1 \2", i).title())
    df.index = list_index

@st.cache
def convert_df(df):
   return df.to_csv().encode('utf-8')

#define the run function
def run():

    st.markdown("""

Financials

    """, unsafe_allow_html=True)

    ticker_list = ['-'] + si.tickers_sp500()
    # Add selection box
    global ticker
    global select_period
    default_t = ticker_list.index("AAPL")
    ticker = st.sidebar.selectbox("Select a ticker", ticker_list, index = default_t)

    if ticker != '-':
        st.write('', unsafe_allow_html=True)
        fin_type = st.radio("", ("Income Statement", "Balance Sheet", "Cash Flow"))
        pulse = st.selectbox("", ("Quarterly", "Annual"))

        if fin_type == 'Income Statement' and pulse =="Quarterly":
            inc_stm = si.get_income_statement(ticker, yearly=False)
            format_cols(inc_stm)
            st.table(set_styles(inc_stm))
        elif fin_type == 'Income Statement' and pulse =="Annual":
                inc_stm = si.get_income_statement(ticker, yearly=True)
                format_cols(inc_stm)
                st.table(set_styles(inc_stm))

        elif fin_type == 'Balance Sheet' and pulse =="Quarterly":
            bal_sht = si.get_balance_sheet(ticker, yearly=False)
            format_cols(bal_sht)
            st.table(bal_sht)

        elif fin_type == 'Balance Sheet'  and pulse =="Annual":
            bal_sht = si.get_balance_sheet(ticker, yearly=True)
            format_cols(bal_sht)
            st.table(bal_sht)

        elif fin_type == 'Cash Flow' and pulse =="Quarterly":
            cash_flw = si.get_cash_flow(ticker, yearly=False)
            format_cols(cash_flw)
            st.table(cash_flw)

        elif fin_type == 'Cash Flow' and pulse =="Annual":
            cash_flw = si.get_cash_flow(ticker, yearly=True)
            format_cols(cash_flw)
            st.table(cash_flw)

    if fin_type == 'Income Statement':
        csv = convert_df(inc_stm)
    elif fin_type == 'Balance Sheet':
        csv = convert_df(bal_sht)
    elif fin_type == 'Cash Flow':
        csv = convert_df(cash_flw)

    st.download_button("Click to Download",csv,"data.csv","text/csv",key='download-csv')


import dataclasses
import datetime as dt
# import logging
import re
import typing as tp

import common
import graphql
import queries
import staff

GITHUB_PREFIX = 'https://github.com'

# logger = logging.getLogger()

def _ago_to_string(datetime: dt.datetime) -> str:
    days = (dt.date.today() - datetime.date()) // dt.timedelta(days=1)
    days_str = 'today'
    if days == 1:
        days_str = 'yesterday'
    elif days > 1:
        days_str = f'{days} days ago on {datetime.strftime("%A")}'
    # days_str += f' at {datetime.strftime("%H:%M")}' # TODO timezone
    return days_str

@dataclasses.dataclass()
class Review:
    author: str
    commit: str
    submitted_at: tp.Optional[dt.datetime]

    def __str__(self):
        return f'last reviewed {_ago_to_string(self.submitted_at)}'

@dataclasses.dataclass(frozen=True, order=True)
class PullRequest:
    owner: str
    repo: str
    number: int
    title: tp.Optional[str] = None
    is_wip: bool = False
    merged_at: tp.Optional[dt.datetime] = None
    author: str = 'unknown'
    author_telegram: tp.Optional[str] = None
    plus: int = 0
    minus: int = 0
    last_review: tp.Optional[Review] = None  # last_review by the caller

    def to_dict(self) -> dict:
        return {'owner': self.owner, 'repo': self.repo, 'number': self.number}

    def get_path(self) -> str:
        return f'{GITHUB_PREFIX}/{self.owner}/{self.repo}/pull/{self.number}'

    def get_short_slug(self) -> str:
        return f'{self.owner}/{self.repo}#{self.number}'

    def get_slug(self) -> str:
        title: str = self.title or ''
        if len(title) > 50:
            title = title[:47]
            title = re.sub(r'\\', '', title)
            title += ' ...'

        return title if title else self.get_short_slug()

    def get_diff_link(self) -> tp.Optional[str]:
        if not self.last_review:
            return None

        return f'{self.get_path()}/files/{self.last_review.commit}..HEAD'

    def get_merged_at(self):
        return (
            f'merged {_ago_to_string(self.merged_at)}'
            if self.merged_at
            else ''
        )

    def __str__(self):
        return self.get_slug()

def _parse_time(arg: str) -> dt.datetime:
    return dt.datetime.strptime(arg, '%Y-%m-%dT%H:%M:%S%z').astimezone(
        dt.timezone.utc,
    )

async def get_timeline(
        pull_request: PullRequest, ctx,
) -> tp.Tuple[str, tp.List[dict]]:
    query = queries.get_timeline(
        pull_request.owner, pull_request.repo, pull_request.number,
    )

    response_json = await graphql.perform_request(query, ctx)

    resp = response_json['data']['repository']['pullRequest']

    author: str = resp['author']['login']
    events: tp.List[tp.Dict] = resp['timelineItems']['nodes']
    return author, events

async def get_new_pr_numbers(
        owner: str,
        repo: str,
        cursor: str,
        how_many: int,
        is_merged: bool,
        ctx,
) -> tp.Tuple[tp.List[int], tp.Optional[str], tp.Optional[dt.datetime]]:
    query = queries.get_pr_numbers(owner, repo, cursor, how_many, is_merged)

    response_json = await graphql.perform_request(query, ctx)
    edges = response_json['data']['repository']['pullRequests']['edges']

    numbers: tp.List[int] = []
    for edge_dict in edges:
        numbers.append(edge_dict['node']['number'])

    return (
        numbers,
        (edges[-1]['cursor'] if is_merged and edges else None),
        (
            _parse_time(edges[-1]['node']['mergedAt'])
            if is_merged and edges
            else None
        ),
    )

async def get_new_pr_cursor(
        owner: str, repo: str, offset: int, ctx,
) -> tp.Optional[tp.Tuple[int, str, dt.datetime]]:
    query = queries.get_pr_numbers(
        owner=owner,
        repo=repo,
        cursor=None,
        how_many=min(offset + 1, 100),
        is_merged=True,
    )

    response_json = await graphql.perform_request(query, ctx)
    edges = response_json['data']['repository']['pullRequests']['edges']

    return (
        (
            edges[0]['node']['number'],
            edges[0]['cursor'],
            _parse_time(edges[0]['node']['mergedAt']),
        )
        if edges
        else None
    )

async def _get_search_results(
        query: dict,
        github_login: tp.Optional[str],
        get_author_login: bool,
        ctx,
) -> tp.List[PullRequest]:
    def _parse_time(timestring: tp.Optional[str]) -> tp.Optional[dt.datetime]:
        if not timestring:
            return None
        return dt.datetime.strptime(timestring, '%Y-%m-%dT%H:%M:%SZ')

    response_json = await graphql.perform_request(query, ctx)
    print(f'response_json = {response_json}')
    # logger.info(f'{response_json}')
    edges = response_json['data']['search']['edges']

    result: tp.List[PullRequest] = []
    for edge_dict in edges:
        node = edge_dict['node']
        owner = node['repository']['owner']['login']
        repo = node['repository']['name']

        # should filter here ?
        if owner not in common.REPOS or repo not in common.REPOS[owner]:
            continue

        if node['isDraft']:
            # TODO make a switcher in settings
            continue

        labels = {label['name'].lower() for label in node['labels']['nodes']}
        if 'not for merge' in labels:
            # TODO make a switcher in settings ?
            continue

        last_review: tp.Optional[Review] = None
        if github_login:
            for review in reversed(node['reviews']['nodes']):
                if review['author']['login'] == github_login:
                    last_review = Review(
                        author=review['author']['login'],
                        commit=review['commit']['oid'],
                        submitted_at=_parse_time(review['submittedAt']),
                    )
                break

        tg_login = None
        if get_author_login:
            tg_login = await staff.get_telegram_login_with_pg(
                node['author']['login'], ctx,
            )

        result.append(
            PullRequest(
                owner=owner,
                repo=repo,
                number=node['number'],
                title=node['title'],
                is_wip='wip' in labels,
                merged_at=_parse_time(node.get('mergedAt')),
                author=node['author']['login'],
                author_telegram=tg_login,
                plus=node['additions'],
                minus=node['deletions'],
                last_review=last_review,
            ),
        )

    result.sort(reverse=True)
    # logger.info(f'_get_search_results response size = {len(result)}')
    return result

async def get_requested_reviews(
        github_login: str, ctx,
) -> tp.List[PullRequest]:
    now = dt.datetime.now()
    timelimit = (now - dt.timedelta(days=now.weekday() + 7)).date()  # config
    query = queries.get_requested_prs(github_login, timelimit.isoformat())
    return await _get_search_results(query, github_login, True, ctx)

async def get_abandoned_prs(
        github_login: str, delta: dt.timedelta, ctx,
) -> tp.List[PullRequest]:
    date_limit = (dt.datetime.now() - delta).date()
    query = queries.get_abandoned_prs(github_login, date_limit.isoformat())
    return await _get_search_results(query, None, False, ctx)

async def get_open_prs(
        github_logins: tp.Iterable[str], asker_login: str, ctx,
) -> tp.List[PullRequest]:
    date_limit = (
        (dt.datetime.now() - dt.timedelta(days=10)).date().isoformat()
    )  # config

    result: tp.List[PullRequest] = []
    # TODO single request?
    for github_login in github_logins:
        query = queries.get_open_prs(github_login, date_limit)
        chunk = await _get_search_results(query, asker_login, True, ctx)
        result.extend(chunk)

    result.sort(reverse=True)
    return result

async def get_reviewed_prs(
        github_login: str,
        date_from: dt.datetime,
        date_to: dt.datetime,
        merged: tp.Optional[bool],
        ctx,
) -> tp.List[PullRequest]:
    query = queries.get_reviewed_prs(
        github_login,
        date_from.date().isoformat(),
        date_to.date().isoformat(),
        merged,
    )
    return await _get_search_results(query, github_login, True, ctx)


import matplotlib.pyplot as plt
import scipy
import numpy as np
import networkx as nx
import random
import cv2 as cv
import scipy.io
import scipy.spatial.distance as sd
import os

def is_connected(adj,n):
    adjn=np.zeros((n,n))
    adji=adj.copy()
    for i in range(n):
        adjn+=adji
        adji=adji.dot(adj)
    return len(np.where(adjn == 0)[0])==0

def max_span_tree(adj):
    n=adj.shape[0]
    if not(is_connected(adj,n)):
        print('This graph is not connected. No spanning tree exists')
    else:
        tr=np.zeros((n,n))
        adj[adj==0]=-np.inf
        conn_nodes = [0]
        rem_nodes = [i+1 for i in range(n-1)]
        while len(rem_nodes)>0:
            L=np.zeros(n)
            L[conn_nodes]=1
            L=L.reshape(n,1)
            C=np.zeros(n)
            C[rem_nodes]=1
            C=C.reshape(1,n)
            B=L.dot(C)
            A=B*adj
            i=np.where(A==np.max(A))[0][0]
            j=np.where(A==np.max(A))[1][0]
            tr[i,j]=1
            tr[j,i]=1
            conn_nodes+=[j]
            rem_nodes.remove(j)
    return tr.astype(int)

def build_similarity_graph(X, var=1, eps=0, k=0):
  assert eps + k != 0, "Choose either epsilon graph or k-nn graph"
  dists = sd.squareform(sd.pdist(X, "sqeuclidean"))
  similarities = np.exp(-dists / var)
  if eps:
    similarities[similarities < eps] = 0
    return similarities
  if k:
    sort = np.argsort(similarities)[:, ::-1]  # descending
    mask = sort[:, k + 1:]  # indices to mask
    for i, row in enumerate(mask): similarities[i, row] = 0
    np.fill_diagonal(similarities, 0)  # remove self similarity
    return (similarities + similarities.T) / 2  # make the graph undirected
    return similarities

def build_laplacian(W, laplacian_normalization=""):
  degree = W.sum(1)
  if not laplacian_normalization or laplacian_normalization=="unn":
    return np.diag(degree) - W
  elif laplacian_normalization == "sym":
    aux = np.diag(1 / np.sqrt(degree))
    return np.eye(*W.shape) - aux.dot(W.dot(aux))
  elif laplacian_normalization == "rw":
    return np.eye(*W.shape) - np.diag(1 / degree).dot(W)
  else: raise ValueError

def hardHFS(graph, labels, laplacian):
  classes = np.unique(labels[labels != 0]).reshape((-1, 1))
  f = (labels == classes).astype(np.float).T
  ixmask = np.where(labels == 0)[0]
  ixlabl = np.where(labels != 0)[0]
  luu = laplacian[ixmask][:, ixmask]
  wul = graph[ixmask][:, ixlabl]
  f[ixmask] = np.linalg.pinv(luu).dot(wul.dot(f[ixlabl]))
  return f

def plot_edges_and_points(X,Y,W,title=''):
    colors=['go-','ro-','co-','ko-','yo-','mo-']
    n=len(X)
    G=nx.from_numpy_matrix(W)
    nx.draw_networkx_edges(G,X)
    for i in range(n):
        plt.plot(X[i,0],X[i,1],colors[int(Y[i])])
    plt.title(title)
    plt.axis('equal')

def plot_graph_matrix(X,Y,W):
    plt.figure()
    plt.clf()
    plt.subplot(1,2,1)
    plot_edges_and_points(X,Y,W)
    plt.subplot(1,2,2)
    plt.imshow(W, extent=[0, 1, 0, 1])
    plt.show()

def plot_clustering_result(X,Y,W,spectral_labels,kmeans_labels,normalized_switch=0):
    plt.figure()
    plt.clf()
    plt.subplot(1,3,1)
    plot_edges_and_points(X,Y,W,'ground truth')
    plt.subplot(1,3,2)
    if normalized_switch:
        plot_edges_and_points(X,spectral_labels,W,'unnormalized laplacian')
    else:
        plot_edges_and_points(X,spectral_labels,W,'spectral clustering')
    plt.subplot(1,3,3)
    if normalized_switch:
        plot_edges_and_points(X,kmeans_labels,W,'normalized laplacian')
    else:
        plot_edges_and_points(X,kmeans_labels,W,'k-means')
    plt.show()

def plot_the_bend(X, Y, W, spectral_labels, eigenvalues_sorted):
    plt.figure()
    plt.clf()
    plt.subplot(1,3,1)
    plot_edges_and_points(X,Y,W,'ground truth')
    plt.subplot(1,3,2)
    plot_edges_and_points(X,spectral_labels,W,'spectral clustering')
    plt.subplot(1,3,3)
    plt.plot(np.arange(0,len(eigenvalues_sorted),1),eigenvalues_sorted,'v:')
    plt.show()

def plot_classification(X, Y,labels,  var=1, eps=0, k=0):
    plt.figure()
    W = build_similarity_graph(X, var=var, eps=eps, k=k)
    plt.subplot(1, 2, 1)
    plot_edges_and_points(X, Y, W, 'ground truth')
    plt.subplot(1, 2, 2)
    plot_edges_and_points(X, labels, W, 'HFS')
    plt.show()

def label_noise(Y, alpha):
    ind=np.arange(len(Y))
    random.shuffle(ind)
    Y[ind[:alpha]] = 3-Y[ind[:alpha]]
    return Y

def plot_classification_comparison(X, Y,hard_labels, soft_labels,var=1, eps=0, k=0):
    plt.figure()
    W = build_similarity_graph(X, var=var, eps=eps, k=k)
    plt.subplot(1,3,1)
    plot_edges_and_points(X, Y, W, 'ground truth')
    plt.subplot(1,3,2)
    plot_edges_and_points(X, hard_labels, W, 'Hard-HFS')
    plt.subplot(1,3,3)
    plot_edges_and_points(X, soft_labels, W, 'Soft-HFS')
    plt.show()

Large Scale Graph Learning/code_material_python/helper.py


import torch
import numpy as np
import math
#from scipy.special import softmax
from matplotlib import pyplot as plt

def tanh(x):
    return np.tanh(x)

def softmax(x):
    max_value = np.max(x);
    return np.exp(x-max_value)/np.sum(np.exp(x-max_value))

class MLP:
    def __init__(self,layer=[10,10, 8, 8, 4],activation='tanh',lr=0.01):
        # 初始化所需参数
        if activation == 'tanh':
            self.activation=tanh
        # 每一个layers也为行向量
        self.layers = [np.zeros((layer[0],1))]
        self.weights = []
        self.lr = lr
        self.biases = []
        self.softmax = softmax
        for i in range(1,len(layer)):
            self.weights.append(np.random.random((layer[i],layer[i-1])))
            self.layers.append(np.zeros((layer[i],1)))
            self.biases.append(np.random.random((layer[i],1)))
        #print('weights=',self.weights)

    def forward(self, x):
        # 前向传播
        # 每一层的layer均为列向量
        self.layers[0] = x.reshape(-1,1)

        for i in range(1,len(self.layers)-1):
            self.layers[i] = self.activation(np.dot(self.weights[i-1],self.layers[i-1]) + self.biases[i-1])

        k = len(self.layers)-1
        self.layers[k] = softmax(np.dot(self.weights[k-1],self.layers[k-1]) + self.biases[k-1])

    def backward(self,y): # 自行确定参数表
        # 反向传播
        y_predict = self.layers[-1]
        # pre_gradient_neurons为列向量
        pre_gradient_neurons = y_predict - y.reshape(-1,1)
        gradientWeightList = []
        gradientBiasList = []
        # 从最后一层到第一层进行遍历
        for i in range(len(self.layers)-1,0,-1):

            # gradient_bias为列向量
            gradient_bias = pre_gradient_neurons
            # (-1,1)为列向量，(1,-1)为行向量
            gradient_weight = np.dot(gradient_bias,self.layers[i-1].reshape(1,-1))
            gradientWeightList.append(gradient_weight)
            gradientBiasList.append(gradient_bias)

            if(i>1):
                s_ = 1 - np.power(self.layers[i-1],2)
                pre_gradient_neurons = np.dot(self.weights[i-1].T,pre_gradient_neurons ) * s_
        j=0
        for i in range(len(self.layers)-1,0,-1):
            self.weights[i-1] -= self.lr * gradientWeightList[j]
            self.biases[i-1] -= self.lr * gradientBiasList[j]
            j += 1

class torchNetwork(torch.nn.Module):
    def __init__(self):
        super(torchNetwork, self).__init__()
        self.linear1 = torch.nn.Linear(10, 10)
        self.tanh = torch.nn.Tanh()
        self.linear2 = torch.nn.Linear(10, 8)
        self.linear3 = torch.nn.Linear(8, 8)
        self.linear4 = torch.nn.Linear(8, 4)
        #self.softmax = torch.nn.Softmax(dim=1)
    def forward(self,x):
        x = self.linear1(x)
        x = self.tanh(x)
        x = self.linear2(x)
        x = self.tanh(x)
        x = self.linear3(x)
        x = self.tanh(x)
        x = self.linear4(x)
        #x = self.softmax(x)
        return x

def train(mlp: MLP, epochs,  inputs, labels):
    '''
        mlp: 传入实例化的MLP模型
        epochs: 训练轮数
        lr: 学习率，在mlp中已规定
        inputs: 生成的随机数据
        labels: 生成的one-hot标签
    '''

    MyMlpLoss = []
    Index = []
    for j in range(epochs):
        print('{}th training in mlp progress'.format(j+1))
        loss = 0
        for i in range(len(inputs)):
            #print('i=',i)
            mlp.forward(inputs[i])
            mlp.backward(labels[i])

        for i in range(len(inputs)):
            mlp.forward(inputs[i])
            t = labels[i].tolist().index(1)
            loss += -math.log((mlp.layers[-1].flatten())[t])
        loss /= len(inputs)
        Index.append(j+1)
        MyMlpLoss.append(loss)

    return MyMlpLoss,Index

if __name__ == '__main__':
    # 设置随机种子,保证结果的可复现性
    np.random.seed(1)
    learningRate = 0.001
    mlp = MLP(lr=learningRate)
    # 生成数据
    inputs = np.random.randn(100, 10)

    # 生成one-hot标签
    labels = np.eye(4)[np.random.randint(0, 4, size=(1, 100))].reshape(100, 4)
    label_pytorch = []
    for i in range(labels.shape[0]):
        for j in range(labels.shape[1]):
            if (labels[i][j] == 1):
                label_pytorch.append(j)
                break
    # 训练模型
    epochs=30000

    MyMlpLoss = []
    Index = []

    MyMlpLoss,Index =train(mlp, epochs,inputs, labels)

    # 打印mlp的weight和bias
    print('MLP weights and biases:')
    print('w=',mlp.weights)
    print('b=',mlp.biases)

    # 测试MLP
    correct = 0
    #print('开始预测')
    for i in range(len(inputs)):
        mlp.forward(inputs[i])
        predict_i=mlp.layers[-1].flatten()
       # print('the input is:',inputs[i])
        #print('the predict is:', predict_i)
        #print('the correct is:', labels[i])
        t = labels[i].tolist().index(1)
        if predict_i.tolist().index(np.max(predict_i))==t:
         #   print('predict is correct')
            correct += 1
        #else:
          # print('predict is wrong')
        #print('\n')
    print('MLP Accuracy:', correct/len(inputs))

    # 训练torch模型

    pytorchLoss = []
    torch_mlp = torchNetwork()

    optimizer = torch.optim.Adam(torch_mlp.parameters(), lr=learningRate)

    for i in range(epochs):
        print('{}th training in torch mlp progress'.format(i+1))
        torch_inputs = torch.from_numpy(inputs).float()
        torch_labels = torch.from_numpy(np.array(label_pytorch)).long()
        predict = torch_mlp(torch_inputs)
        optimizer.zero_grad()
        loss = torch.nn.CrossEntropyLoss()(predict, torch_labels)
        loss.backward()
        optimizer.step()
        pytorchLoss.append(loss.item())

    # 测试torch模型
    print('torch Accuracy:', torch.sum(torch.argmax(predict, dim=1) == torch_labels)/len(inputs))

    print('torch weights and biases:')
    #打印torch的weight和bias
    for name,param in torch_mlp.named_parameters():
        if name in ['linear1.weight','linear1.bias']:
            print(name,param.data.numpy())
        elif name in ['linear2.weight','linear2.bias']:
            print(name,param.data.numpy())
        elif name in ['linear3.weight','linear3.bias']:
            print(name,param.data.numpy())
        elif name in ['linear4.weight','linear4.bias']:
            print(name,param.data.numpy())

    # 绘制loss曲线
    plt.plot(Index,MyMlpLoss,color='r',label="MLP")
    plt.plot(Index,pytorchLoss,color='b',label="torch")
    plt.legend()
    #plt.plot(pytorchLoss,color='b')
    #plt.axis([0,epochs,0,2])
    plt.xlabel('epochs')
    plt.ylabel('loss')
    #plt.title('LOSS-EPOCHS')
    plt.show()


import sqlite3

class Cliente:
    def __init__(self):
        None

    #ver habitaciones en el rol cliente
    def ver_habitaciones(self):
        conn = sqlite3.connect('olympo.db')
        cursor = conn.cursor()
        consulta = "SELECT * FROM tb_habitaciones"
        cursor.execute(consulta)

        habitaciones = cursor.fetchall()
        print("Lista de habitaciones:")
        for habitacion in habitaciones:
            print("ID:", habitacion[0], "| Tipo:", habitacion[1], "| Descripción:", habitacion[2], "| Precio:", habitacion[3])

    #ver servicios del hotel
    def ver_servicios(self):
        conn = sqlite3.connect('olympo.db')
        cursor = conn.cursor()

        consulta = "SELECT * FROM tb_servicios"
        cursor.execute(consulta)
        servicios = cursor.fetchall()
        print("Lista de servicios:")
        for servicio in servicios:
            print("ID:", servicio[0], "| Nombre:", servicio[1], "| Descripción:", servicio[2], "| Precio:", servicio[3])

    #realizar una reserva como cliente
    def Reservar(self, tipo_habitacion, cantidad_personas, fecha_inicio, fecha_fin):

        conn = sqlite3.connect('Olympo.db')
        cursor = conn.cursor()

        consulta = "SELECT * FROM tb_habitaciones WHERE tipo = ? AND capacidad >= ?"
        cursor.execute(consulta, (tipo_habitacion, cantidad_personas))
        habitaciones = cursor.fetchall()

        disponible = False
        for habitacion in habitaciones:
            consulta = "SELECT * FROM tb_reservas WHERE id_habitacion = ? AND ((fecha_inicio BETWEEN ? AND ?) OR (fecha_fin BETWEEN ? AND ?))"
            cursor.execute(consulta, (habitacion[0], fecha_inicio, fecha_fin, fecha_inicio, fecha_fin))
            reservas = cursor.fetchall()
            if not reservas:
                disponible = True
                habitacion_id = habitacion[0]
                break
        if disponible:
            consulta = "INSERT INTO tb_reservas (id_habitacion, cantidad_personas, fecha_inicio, fecha_fin) VALUES (?, ?, ?, ?)"
            cursor.execute(consulta, (habitacion_id, cantidad_personas, fecha_inicio, fecha_fin))
            conn.commit()
            print("¡Reserva realizada con éxito!")
        else:
            print("Lo siento, no hay habitaciones disponibles para las fechas seleccionadas.")

#MENU
cl=Cliente
c=int(input('ver habitaciones del hotel=1 \n  consultar servicios del hotel=2 \n realizar una reserva=3 \n '))
if c==1:
    cl.ver_habitaciones()
elif c==2:
    cl.ver_habitaciones()
elif c==3:
    tp=input('ingrese el tipo de habitacion')
    can=int(input('ingrese la cantidad de personas a hospedarse'))
    ini=input('ingrese la fecha de inicio de la reserva')
    fin=input('ingrese la fecha de fin de la reserva')
    cl.Reservar(tp,can,ini,fin)


from __future__ import annotations

import math
from contextlib import suppress
from typing import Any, Callable, Union

import toolz
from typing_extensions import Annotated, get_args, get_origin

from ibis.common.dispatch import lazy_singledispatch
from ibis.common.exceptions import IbisTypeError
from ibis.common.typing import evaluate_typehint
from ibis.util import flatten_iterable, is_function, is_iterable

class Validator(Callable):
    """Abstract base class for defining argument validators."""

    __slots__ = ()

    @classmethod
    def from_annotation(cls, annot, module=None):
        # TODO(kszucs): cache the result of this function
        annot = evaluate_typehint(annot, module)
        origin_type = get_origin(annot)

        if origin_type is Union:
            inners = map(cls.from_annotation, get_args(annot))
            return any_of(tuple(inners))
        elif origin_type is list:
            (inner,) = map(cls.from_annotation, get_args(annot))
            return list_of(inner)
        elif origin_type is tuple:
            (inner,) = map(cls.from_annotation, get_args(annot))
            return tuple_of(inner)
        elif origin_type is dict:
            key_type, value_type = map(cls.from_annotation, get_args(annot))
            return dict_of(key_type, value_type)
        elif origin_type is Annotated:
            annot, *extras = get_args(annot)
            return all_of((instance_of(annot), *extras))
        elif annot is Any:
            return any_
        else:
            return instance_of(annot)

# TODO(kszucs): in order to cache valiadator instances we could subclass
# grounds.Singleton, but the imports would need to be reorganized
class Curried(toolz.curry, Validator):
    """Enable convenient validator definition by decorating plain functions."""

    def __repr__(self):
        return '{}({}{})'.format(
            self.func.__name__,
            repr(self.args)[1:-1],
            ', '.join(f'{k}={v!r}' for k, v in self.keywords.items()),
        )

validator = Curried

@validator
def ref(key, *, this):
    try:
        return this[key]
    except KeyError:
        raise IbisTypeError(f"Could not get `{key}` from {this}")

@validator
def any_(arg, **kwargs):
    return arg

@validator
def option(inner, arg, *, default=None, **kwargs):
    if arg is None:
        if default is None:
            return None
        elif is_function(default):
            arg = default()
        else:
            arg = default
    return inner(arg, **kwargs)

@validator
def instance_of(klasses, arg, **kwargs):
    """Require that a value has a particular Python type."""
    if not isinstance(arg, klasses):
        # TODO(kszucs): unify errors coming from various validators
        raise IbisTypeError(
            f"Given argument with type {type(arg)} is not an instance of {klasses}"
        )
    return arg

class lazy_instance_of(Validator):
    """A version of `instance_of` that accepts class qualnames instead of "
    "already imported classes.

    Useful for delaying imports.
    """

    def __init__(self, classes):
        classes = (classes,) if isinstance(classes, str) else tuple(classes)
        self._classes = classes
        self._check = lazy_singledispatch(lambda x: False)
        self._check.register(classes, lambda x: True)

    def __repr__(self):
        return f"lazy_instance_of(classes={self._classes!r})"

    def __call__(self, arg, **kwargs):
        if self._check(arg):
            return arg
        raise IbisTypeError(
            f"Given argument with type {type(arg)} is not an instance of "
            f"{self._classes}"
        )

@validator
def any_of(inners, arg, **kwargs):
    """At least one of the inner validators must pass."""
    for inner in inners:
        with suppress(IbisTypeError, ValueError):
            return inner(arg, **kwargs)

    raise IbisTypeError(
        "argument passes none of the following rules: "
        f"{', '.join(map(repr, inners))}"
    )

one_of = any_of

@validator
def all_of(inners, arg, **kwargs):
    """All of the inner validators must pass.

    The order of inner validators matters.

    Parameters
    ----------
    inners : List[validator]
      Functions are applied from right to left so allof([rule1, rule2], arg) is
      the same as rule1(rule2(arg)).
    arg : Any
      Value to be validated.

    Returns
    -------
    arg : Any
      Value maybe coerced by inner validators to the appropiate types
    """
    for inner in inners:
        arg = inner(arg, **kwargs)
    return arg

@validator
def isin(values, arg, **kwargs):
    if arg not in values:
        raise ValueError(f'Value with type {type(arg)} is not in {values!r}')
    if isinstance(values, dict):  # TODO check for mapping instead
        return values[arg]
    else:
        return arg

@validator
def map_to(mapping, variant, **kwargs):
    try:
        return mapping[variant]
    except KeyError:
        raise ValueError(f'Value with type {type(variant)} is not in {mapping!r}')

@validator
def container_of(inner, arg, *, type, min_length=0, flatten=False, **kwargs):
    if not is_iterable(arg):
        raise IbisTypeError('Argument must be a sequence')

    if len(arg) < min_length:
        raise IbisTypeError(f'Arg must have at least {min_length} number of elements')

    if flatten:
        arg = flatten_iterable(arg)

    return type(inner(item, **kwargs) for item in arg)

@validator
def mapping_of(key_inner, value_inner, arg, *, type, **kwargs):
    return type(
        (key_inner(k, **kwargs), value_inner(v, **kwargs)) for k, v in arg.items()
    )

@validator
def int_(arg, min=0, max=math.inf, **kwargs):
    if not isinstance(arg, int):
        raise IbisTypeError('Argument must be an integer')
    if arg < min:
        raise ValueError(f'Argument must be greater than {min}')
    if arg > max:
        raise ValueError(f'Argument must be less than {max}')
    return arg

@validator
def min_(min, arg, **kwargs):
    if arg < min:
        raise ValueError(f'Argument must be greater than {min}')
    return arg

str_ = instance_of(str)
bool_ = instance_of(bool)
none_ = instance_of(type(None))
dict_of = mapping_of(type=dict)
list_of = container_of(type=list)
tuple_of = container_of(type=tuple)


# -*- coding: utf-8 -*-
"""
Created on Tue Apr  9 13:46:06 2019

Figure 4. Interannual terrestrial and oceanic contributions to Australian precipitation in (a) relative terms
[%] and (b) absolute terms [mm].

@author: z3131380
"""

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter

#==============================================================================
# Definitions
#==============================================================================
dir_in = r'/srv/ccrc/data19/z3131380/PartB/Output/Australia/100parcels/TS10min/exp01/Processed/Rainfall_recycling/'
dir_out = r'/home/z3131380/hdrive/PhD/PartB/Reporting/Paper_1/Figures/'

region = 'Australia'
df = pd.read_csv(dir_in+'Yearly/'+region+'_annual_rainfall_recycling_1979-2013.csv')

fsize = 14

#==============================================================================
# Functions
#==============================================================================
def make_patch_spines_invisible(ax):
    ax.set_frame_on(True)
    ax.patch.set_visible(False)
    for sp in ax.spines.values():
        sp.set_visible(False)

#==============================================================================
# Figure 4. Interannual terrestrial and oceanic contributions to Australian precipitation in (a) relative terms
# [%] and (b) absolute terms [mm].
#==============================================================================

fig, ax2 = plt.subplots(figsize=(16,11.7))
ax2 = plt.subplot2grid((2, 1), (0, 0), rowspan=1)
plt.annotate('(a)',xy=(1980,0.4),xycoords='data',fontsize=fsize)
#p3, = ax2.plot(df['Year'],df['P_anom'],'-k^', label="Rainfall anomaly [%]",markersize=4,linewidth=1,alpha=0.7)
ax2.fill_between(df['Year'],df['P_anom'],0,alpha=0.5,edgecolor='None',facecolor='gray')
ax2.set_ylim(-0.5,0.5)
ax2.grid(axis='x')
ax2.tick_params(labelbottom=False,labeltop=True)
ax2.axhline(0,color='gray',linestyle=':')
ax2.set_yticklabels(ax2.get_yticks(),fontsize=fsize)
ax2.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
plt.annotate('Precipitation anomaly [%]',xy=(-0.05,0.7),xycoords='axes fraction',fontsize=fsize,rotation=90)
ax2a = ax2.twinx()
ax2b = ax2.twinx()
p2a, = ax2a.plot(df['Year'],df['outregion_ocean_pct'],'-bo', label="Ocean water vapour contribution [%]",markersize=4,markeredgecolor='b',markerfacecolor='b')#,alpha=0.5)
p2b, = ax2b.plot(df['Year'],df['RR'],'-gs', label="Land water vapour contribution [%]",markersize=4,markeredgecolor='g',markerfacecolor='g')#,alpha=0.5)
ax2a.spines["right"].set_position(("axes", 1.01))
ax2b.spines["right"].set_position(("axes", 1.07))
make_patch_spines_invisible(ax2a)
make_patch_spines_invisible(ax2b)
ax2a.spines["right"].set_visible(True)
ax2b.spines["right"].set_visible(True)
tkw = dict(size=4, width=1.5)
ax2a.tick_params(axis='y', colors=p2a.get_color(), **tkw)
ax2b.tick_params(axis='y', colors=p2b.get_color(), **tkw)
ax2a.set_yticklabels(ax2a.get_yticks(),fontsize=fsize)
ax2a.yaxis.set_major_formatter(FormatStrFormatter('%.0f'))
ax2b.set_yticklabels(ax2b.get_yticks(),fontsize=fsize)
ax2b.yaxis.set_major_formatter(FormatStrFormatter('%.0f'))
ax2a.set_ylim(70,95)
ax2b.set_ylim(5,25)
plt.annotate("Oceanic contribution [%]",xy=(1.04,0.7),xycoords = 'axes fraction',rotation=90,color='blue',fontsize=fsize)
plt.annotate("Terrestrialcontribution [%]",xy=(1.1,0.74),xycoords = 'axes fraction',rotation=90,color='green',fontsize=fsize)
ax2.set_xlim(1979,2014)
ax2.set_xticks(np.arange(min(df['Year']), max(df['Year'])+1, 1))
ax2.set_xticklabels(ax2.get_xticks(),rotation=90,fontsize=fsize)
ax2.xaxis.set_major_formatter(FormatStrFormatter('%.0f'))

ax1 = plt.subplot2grid((2, 1), (1, 0), sharex=ax2)
plt.annotate('(b)',xy=(1980,2700),xycoords='data',fontsize=fsize)
#p1, = ax1.plot(df['Year'],df['P_total']/1000,'-k^', label="Rainfall total [m]",markersize=4,linewidth=1,alpha=0.7)
ax1.fill_between(df['Year'],df['P_total']/1000,0,alpha=0.5,edgecolor='None',facecolor='gray')
#ax1.set_ylim(0,2000)
ax1.grid(axis='x')
#ax1.tick_params(labelbottom=False,labeltop=True)
ax1.set_xlim(1979,2014)
ax1.set_ylim(500,3000)
ax1.set_xticks(np.arange(min(df['Year']), max(df['Year'])+1, 1))
ax1.set_xticklabels(ax1.get_xticks(),rotation=90,fontsize=fsize)
ax1.xaxis.set_major_formatter(FormatStrFormatter('%.0f'))
ax1.set_yticklabels(ax1.get_yticks(),fontsize=fsize)
ax1.yaxis.set_major_formatter(FormatStrFormatter('%.0f'))
plt.annotate('Precipitation total [m]',xy=(-0.06,0.65),xycoords='axes fraction',fontsize=fsize,rotation=90)
ax1a = ax1.twinx()
ax1b = ax1.twinx()
p1a, = ax1a.plot(df['Year'],df['outregion_ocean_mm']/1000,'-bo', label="Ocean water vapour contribution [m]",markersize=4,markeredgecolor='b',markerfacecolor='b')
p1b, = ax1b.plot(df['Year'],df['region_land_mm']/1000,'-gs', label="Land water vapour contribution [m]",markersize=4,markeredgecolor='g',markerfacecolor='g')
ax1a.spines["right"].set_position(("axes", 1.01))
ax1b.spines["right"].set_position(("axes", 1.07))
make_patch_spines_invisible(ax1a)
make_patch_spines_invisible(ax1b)
ax1a.spines["right"].set_visible(True)
ax1b.spines["right"].set_visible(True)
tkw = dict(size=4, width=1.5)
ax1a.tick_params(axis='y', colors=p1a.get_color(), **tkw)
ax1b.tick_params(axis='y', colors=p1b.get_color(), **tkw)
ax1a.set_yticklabels(ax1a.get_yticks(),fontsize=fsize)
ax1a.yaxis.set_major_formatter(FormatStrFormatter('%.0f'))
ax1b.set_yticklabels(ax1b.get_yticks(),fontsize=fsize)
ax1b.yaxis.set_major_formatter(FormatStrFormatter('%.0f'))
ax1a.set_ylim(600,1800)
ax1b.set_ylim(100,400)
plt.annotate("Oceanic contribution [m]",xy=(1.05,0.7),xycoords = 'axes fraction',rotation=90,color='blue',fontsize=fsize)
plt.annotate("Terrestrial contribution [m]",xy=(1.105,0.75),xycoords = 'axes fraction',rotation=90,color='green',fontsize=fsize)

plt.tight_layout()
fig.savefig(dir_out+'Fig4.png',bbox_inches='tight')
plt.close()


import os
import shutil
from pathlib import Path

from src.problem import Problem

PATH_TMP_FOLDER = Path("temp")

class Generator(Problem):
    def __init__(self, pid: str):
        super().__init__(pid)

    @staticmethod
    def __ensure_and_clear_temp_folder():
        if not PATH_TMP_FOLDER.exists():
            PATH_TMP_FOLDER.mkdir(parents=True)
        for file in PATH_TMP_FOLDER.iterdir():
            os.remove(file)

    @staticmethod
    def __compile(path: Path):
        if os.system(f"g++ {path} -o {PATH_TMP_FOLDER / path.stem} -O2 -std=c++20 -Wl,--stack=268435456"):
            raise Exception(f"{path} 编译失败")

    def generate(self):
        self._logger.info("生成测试数据")

        self._logger.debug("清空临时文件夹")
        self.__ensure_and_clear_temp_folder()
        if not self._path_std.exists():
            raise Exception("标程不存在")

        self._logger.info("编译标程")
        self.__compile(self._path_std)

        self._logger.info("生成输入样例")
        if self._path_gen_cpp.exists():
            self._logger.info("编译生成器")
            self.__compile(self._path_gen_cpp)
            os.system(f"cd {PATH_TMP_FOLDER} && gen")
        elif self._path_gen_py.exists():
            shutil.copy(self._path_gen_py, PATH_TMP_FOLDER)
            os.system(f"cd {PATH_TMP_FOLDER} && python gen.py")
        else:
            raise Exception("生成器不存在")

        self._logger.info("生成输出样例")
        files_input = PATH_TMP_FOLDER.glob("*.in")
        for file in files_input:
            os.system(f"{PATH_TMP_FOLDER / 'std'} < {file} > {PATH_TMP_FOLDER / (file.stem + '.out')}")
            self._logger.info(f"输出样例 #{file.stem} 已生成")

        self._logger.info("打包测试数据")
        path_zip = self._path / "testcase.zip"
        path_zip.unlink(missing_ok=True)
        os.system(f"7z a -tzip {path_zip} {PATH_TMP_FOLDER / '*.in'} {PATH_TMP_FOLDER / '*.out'}")


from ui.gameplay import Gameplay
from ui.results import Results
from services.functions import Functions
from repository.repository import Repository
import pygame
import sys
import os
from pygame.locals import *
pygame.font.init()

dirname = os.path.dirname(__file__)

class MainMenu:

    def __init__(self):
        pygame.init()
        self.g = Gameplay()
        self.r = Results()
        self.f = Functions()
        self.repo = Repository()
        self.screen = pygame.display.set_mode((600, 600), 0, 32)
        self.mainClock = pygame.time.Clock()
        self.menufont = pygame.font.SysFont("cambria", 100)
        self.menufont2 = pygame.font.SysFont("cambria", 102)
        self.menufont3 = pygame.font.SysFont("cambria", 40)
        self.menufont4 = pygame.font.SysFont("cambria", 15)
        self.to_game = pygame.Rect(200, 300, 200, 50)
        self.to_results = pygame.Rect(200, 400, 200, 50)
        self.erase_results = pygame.Rect(5, 5, 135, 30)
        self.erase_confirmation = pygame.Rect(200, 50, 200, 100)
        self.click = False
        self.mx, self.my = pygame.mouse.get_pos()
        self.easy = pygame.Rect(200, 210, 200, 50)
        self.normal = pygame.Rect(200, 270, 200, 50)
        self.hard = pygame.Rect(200, 330, 200, 50)

    def run_main_menu(self):
        self.click = False
        confirm_box = False
        self.repo.create_or_maintain_result_files()
        while True:
            self.screen.fill((255, 255, 255))

            self.mx, self.my = pygame.mouse.get_pos()

            self.f.draw_text("Tyhjennä tulostaulu", self.menufont4, (0, 0, 0), self.screen, 10, 10)
            self.f.draw_text("Sudoku", self.menufont, (0, 0, 0), self.screen, 135, 150)
            self.f.draw_text('Peliin', self.menufont3, (0, 0, 0), self.screen, 248, 298)
            self.f.draw_text('Tulokset', self.menufont3, (0, 0, 0), self.screen, 225, 400)

            if self.to_game.collidepoint((self.mx, self.my)):
                if self.click:
                    self.difficulty()
            elif self.to_results.collidepoint((self.mx, self.my)):
                if self.click:
                    self.r.show_results()
            elif self.erase_results.collidepoint((self.mx, self.my)):
                if self.click and confirm_box:
                    confirm_box = False
                elif self.click:
                    confirm_box = True

            pygame.draw.rect(self.screen, (0, 0, 0), self.to_game, 1)
            pygame.draw.rect(self.screen, (0, 0, 0), self.to_results, 1)
            pygame.draw.rect(self.screen, (0, 0, 0), self.erase_results, 1)

            if confirm_box:
                pygame.draw.rect(self.screen, (0, 0, 0), self.erase_confirmation, 1)
                self.f.draw_text("Haluatko varmasti", self.menufont4, (0, 0, 0), self.screen, 240, 60)
                self.f.draw_text("tyhjentää tulostaulun?", self.menufont4, (0, 0, 0), self.screen, 230, 80)
                self.f.draw_text("ENTER = Tyhjennä", self.menufont4, (255, 0, 0), self.screen, 240, 100)
                self.f.draw_text("ESC = Peruuta", self.menufont4, (255, 0, 0), self.screen, 252, 120)

            self.click = False
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == KEYDOWN:
                    if event.key == K_ESCAPE and not confirm_box:
                        pygame.quit()
                        sys.exit()
                    if confirm_box:
                        if event.key == K_RETURN:
                            self.repo.clear_results()
                            confirm_box = False
                        elif event.key == K_ESCAPE:
                            confirm_box = False
                if event.type == MOUSEBUTTONDOWN:
                    if event.button == 1:
                        self.click = True

            pygame.display.update()
            self.mainClock.tick(60)

    def difficulty(self):

        while True:

            self.screen.fill((255, 255, 255))

            self.f.draw_text('Helppo', self.menufont3, (0, 0, 0), self.screen, 236, 208)
            self.f.draw_text('Normaali', self.menufont3, (0, 0, 0), self.screen, 218, 268)
            self.f.draw_text('Vaikea', self.menufont3, (0, 0, 0), self.screen, 240, 328)

            self.mx, self.my = pygame.mouse.get_pos()

            pygame.draw.rect(self.screen, (0, 0, 0), self.easy, 1)
            pygame.draw.rect(self.screen, (0, 0, 0), self.normal, 1)
            pygame.draw.rect(self.screen, (0, 0, 0), self.hard, 1)

            self.click = False
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        self.run_main_menu()
                if event.type == MOUSEBUTTONDOWN:
                    if event.button == 1:
                        self.click = True
                if self.easy.collidepoint((self.mx, self.my)):
                    if self.click:
                        self.g.game_loop(0)
                elif self.normal.collidepoint((self.mx, self.my)):
                    if self.click:
                        self.g.game_loop(1)
                elif self.hard.collidepoint((self.mx, self.my)):
                    if self.click:
                        self.g.game_loop(2)

            pygame.display.update()
            self.mainClock.tick(60)


from sklearn.cluster import KMeans
from sklearn.preprocessing import MinMaxScaler
import pandas as pd

def knn_transform(data, column, k):
    """返回数据和质心"""
    data_copy = data.copy()
    data = MinMaxScaler().fit_transform(data)
    estimator = KMeans(n_clusters=k)
    estimator.fit(data)
    data_copy[f"{column}_category"] = pd.DataFrame(estimator.labels_)
    return data_copy

def kd_range(data):
    record = {}
    temp = None
    for i in range(len(data)):
        if temp is None:
            temp = data[i]
            record[temp] = []
            start = i
        elif temp != data[i]:
            # 结束上一次记录
            end = i - 1
            record[temp].append([start, end])
            # 开始下一次记录
            start = i
            temp = data[i]
            # 若没有记录则添加类别记录
            try:
                record[temp]
            except:
                record[temp] = []
            # 如果是最后一个，则直接加入记录
        elif i == len(data) - 1:
            record[temp].append([start, i])
    return {'name': 'record_discretization', 'value': record}


from bs4 import BeautifulSoup
from celery import Celery
from minio import Minio
import re
import requests
import io
import json
import logging
from conf import BUCKET_NAME, MINIO_URL, MINIO_ACCESS_KEY, MINIO_SECRET_KEY, RABBITMQ_URI, REDIS_URI
import dask

logging.basicConfig()
logging.getLogger().setLevel(logging.INFO)

app = Celery('tasks',
    backend=REDIS_URI,
    broker=RABBITMQ_URI
    )

client = Minio(
    MINIO_URL,
    access_key=MINIO_ACCESS_KEY,
    secret_key=MINIO_SECRET_KEY,
    secure=False,
    )

bucket_name = BUCKET_NAME

def getCrewData(url):
    crew_data = {
        "crew": []
    }
    r = requests.get(url=url)

    # Create a BeautifulSoup object
    soup = BeautifulSoup(r.text, 'html.parser')

    #page title
    title = soup.find('title')
    crew_data["title"] = title.string
    cast_list = soup.find("table", {"class" : "cast_list"})

    trows = cast_list.find_all('tr')

    for tr in trows:
        td = tr.find_all('td')
        if len(td)==4:
            row = [i.text for i in td]
            crew_data["crew"].append({
                "name":re.sub("[^a-zA-Z' ]+", '', row[1]).strip(),
                "character":re.sub("[^a-zA-Z' ]+", '', row[3]).strip()
            })
    return crew_data

def getAllGenres():
    genres = []
    url = f"https://www.imdb.com/search/title/"
    r = requests.get(url=url, stream=True)
    soup = BeautifulSoup(r.text, 'html.parser')
    res = soup.find_all("tbody")
    for r in res:
        r_td = r.find_all("td")
        for td in r_td:
            genre = td.find("input", {"name": "genres"})
            if genre:
                genres.append(genre.get("value"))
    return genres

def getMovieDetails(url, scrape_ts):
    logging.info(f"Getting details for {url}")
    data = {}
    r = requests.get(url=url)
    # Create a BeautifulSoup object
    soup = BeautifulSoup(r.text, 'html.parser')

    # scrape timestamp
    data["scrape_ts"] = scrape_ts

    # page title
    title = soup.title.string
    data["title"] = title
    # data["title"] = title.split(' - IMDb')[0]

    # title url
    data["url"] = url

    try:
        data["poster_url"] = soup.find("div", {"class": "poster"}).find('img')['src']
    except:
        data["poster_url"] = None
        logging.debug(f"Poster URL for {url} is None")

    # rating
    try:
        ratingValue = soup.find("span", {"itemprop" : "ratingValue"})
        data["ratingValue"] = ratingValue.string
    except:
        data["ratingValue"] = None
        logging.debug(f"Rating value is None for url {url}")

    # no of rating given
    try:
        ratingCount = soup.find("span", {"itemprop" : "ratingCount"})
        data["ratingCount"] = ratingCount.string
    except:
        data["ratingCount"] = None
        logging.debug(f"Rating count is None for url {url}")

    # name
    titleName = soup.find("div",{'class':'titleBar'}).find("h1")
    data["name"] = titleName.contents[0].strip()
    # data["name"] = titleName.contents[0].replace(u'\xa0', u'')

    # is_series
    # check if the string "Episodes" is present
    try:
        isSeries = soup.find("div", {"class": "article"}).find("h2")
        data["isSeries"] = True
    except:
        data["isSeries"] = False

    # duration
    try:
        data["duration"] = soup.find("time").string.strip()
    except:
        data["duration"] = None
        logging.debug(f"Duration is None for url {url}")

    # genre
    try:
        data["genres"] = []
        genre = soup.find("div", {"class": "subtext"}).find_all("a", href=True)[:-1]
        for g in genre:
            data["genres"].append(re.search(r">.*<\/a>", str(g)).group()[1:-4])
    except:
        data["genres"] = None
        logging.debug(f"Genres is None for url {url}")

    # release date
    try:
        release_date = soup.find("div", {"class": "subtext"}).find_all("a", href=True)[-1]
        data["release_date"] = release_date.string.strip()
    except:
        data["release_date"] = None
        logging.debug(f"Release date is None for url {url}")

    # movie rated
    # subtext = soup.find("div", {"class": "subtext"})
    # print(f"subtext: {subtext}")
    # print(f"subtext text: {subtext.text}")
    # movie_rated = subtext.text.split()[0]

    # print(movie_rated)
    # # print(movie_rated.text.split())

    # data["movie_rated"] = movie_rated

    # summary
    try:
        summary_text = soup.find("div",{'class':'summary_text'})
        data["summary_text"] = summary_text.text.strip()
    except:
        data["summary_text"] = None
        logging.debug(f"Summary text is None for url {url}")

    try:
        credit_summary_item = soup.find_all("div",{'class':'credit_summary_item'})
        data["credits"] = {}
        for i in credit_summary_item:
            item = i.find("h4")
            names = i.find_all("a")
            data["credits"][item.string] = []
            for i in names:
                data["credits"][item.string].append({
                    "link": i["href"],
                    "name": i.string
                })
    except:
        data["credits"] = None
        logging.debug(f"Credits is None for url {url}")

    return data

def put_json(bucket_name, object_name, d):
    """
    jsonify a dict and write it as object to the bucket
    """
    # prepare data and corresponding data stream
    data = json.dumps(d).encode("utf-8")
    data_stream = io.BytesIO(data)
    data_stream.seek(0)

    # put data as object into the bucket
    client.put_object(
        bucket_name=bucket_name,
        object_name=object_name,
        data=data_stream,
        length=len(data),
        content_type="application/json"
    )

@app.task
def writeToFile(chunk, object_name, scrape_ts):
    '''
    for each chunk (metadata of 5 titles in json format),
    dump them into one .json file and store that .json file
    in the folder which has the name of today's date
    '''
    title_list = []
    for link in chunk:  # loop thru all the 5 json data
        url = f"https://www.imdb.com{link}"
        title = getMovieDetails(url, scrape_ts)
        title_list.append(title)

    put_json(bucket_name, object_name, title_list)