# This file is part of django-ca (https://github.com/mathiasertl/django-ca).
#
# django-ca is free software: you can redistribute it and/or modify it under the terms of the GNU General
# Public License as published by the Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# django-ca is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along with django-ca. If not, see
# <http://www.gnu.org/licenses/>.
"""Form widgets for django-ca admin interface."""

import logging
import typing
from typing import Any, Dict, Iterable, List, Optional, Tuple, Type, Union

from cryptography import x509
from cryptography.x509.oid import AuthorityInformationAccessOID, ExtensionOID

from django import forms
from django.forms import widgets
from django.utils.translation import gettext as _

from django_ca import ca_settings
from django_ca.constants import (
    EXTENDED_KEY_USAGE_NAMES,
    EXTENSION_DEFAULT_CRITICAL,
    KEY_USAGE_NAMES,
    REVOCATION_REASONS,
)
from django_ca.extensions.utils import certificate_policies_is_simple
from django_ca.utils import ADMIN_SUBJECT_OIDS, format_general_name

log = logging.getLogger(__name__)

ExtensionWidgetsType = Tuple[Union[Type[forms.Widget], forms.Widget], ...]

class DjangoCaWidgetMixin:
    """Widget mixin with some generic functionality.

    This class is *not* intended for MultiWidget instances.

    Classes using this mixin will have a ``django-ca-widget`` CSS class and can define further classes using
    the ``css_classes`` attribute.
    """

    css_classes: Iterable[str] = ("django-ca-widget",)

    def get_css_classes(self) -> typing.Set[str]:
        """Get set of configured CSS classes."""
        css_classes = set()
        for cls in reversed(self.__class__.__mro__):
            css_classes |= set(getattr(cls, "css_classes", set()))
        return css_classes

    def add_css_classes(self, attrs: Dict[str, str]) -> None:
        """Add CSS classes to the passed attributes."""
        css_classes = " ".join(sorted(self.get_css_classes()))

        if "class" in attrs:
            attrs["class"] += f" {css_classes}"
        else:
            attrs["class"] = css_classes

    def get_context(self, *args: Any, **kwargs: Any) -> Dict[str, Any]:
        """Get the context."""
        # TYPEHINT NOTE: This is a mixin, not worth creating a protocol just for this
        ctx: Dict[str, Any] = super().get_context(*args, **kwargs)  # type: ignore[misc]
        self.add_css_classes(ctx["widget"]["attrs"])
        return ctx

class CheckboxInput(DjangoCaWidgetMixin, widgets.CheckboxInput):
    """CheckboxInput that uses the DjangoCaWidgetMixin."""

class MultiWidget(DjangoCaWidgetMixin, widgets.MultiWidget):  # pylint: disable=abstract-method
    """MultiWidget that uses the DjangoCaWidgetMixin."""

    css_classes = ("django-ca-multiwidget",)
    template_name = "django_ca/forms/widgets/multiwidget.html"
    labels: Tuple[Optional[str], ...] = ()
    help_texts: Tuple[Optional[str], ...] = ()

    class Media:
        css = {
            "all": ("django_ca/admin/css/multiwidget.css",),
        }

    def get_context(self, *args: Any, **kwargs: Any) -> Dict[str, Any]:
        """Get the context."""
        # TYPEHINT NOTE: This is a mixin, not worth creating a protocol just for this
        ctx: Dict[str, Any] = super().get_context(*args, **kwargs)
        for widget, label in zip(ctx["widget"]["subwidgets"], self.labels):
            widget["label"] = label
        for widget, help_text in zip(ctx["widget"]["subwidgets"], self.help_texts):
            widget["help_text"] = help_text
        return ctx

class SelectMultiple(DjangoCaWidgetMixin, widgets.SelectMultiple):
    """SelectMultiple field that uses the DjangoCaWidgetMixin."""

class Textarea(DjangoCaWidgetMixin, widgets.Textarea):
    """Textarea field that uses the DjangoCaWidgetMixin."""

class TextInput(DjangoCaWidgetMixin, widgets.TextInput):
    """TextInput field that uses the DjangoCaWidgetMixin."""

class LabeledCheckboxInput(CheckboxInput):
    """CheckboxInput widget that adds a label and wraps everything in a <span />.

    This is necessary because widgets in MultiValueFields don't render with a label."""

    template_name = "django_ca/forms/widgets/labeledcheckboxinput.html"

    def __init__(self, label: str, wrapper_classes: Iterable[str] = tuple()) -> None:
        self.wrapper_classes = tuple(wrapper_classes) + ("labeled-checkbox",)
        self.label = label
        super().__init__()

    def get_context(self, *args: Any, **kwargs: Any) -> Dict[str, Any]:
        ctx = super().get_context(*args, **kwargs)
        ctx["widget"]["wrapper_classes"] = " ".join(self.wrapper_classes)
        ctx["widget"]["label"] = self.label

        # Tell any wrapping widget (like a MultiWidget) that this widget displays its own label.
        ctx["widget"]["handles_label"] = True
        return ctx

    class Media:
        css = {
            "all": ("django_ca/admin/css/labeledcheckboxinput.css",),
        }

class CriticalInput(LabeledCheckboxInput):
    """Widget for setting the `critical` value of an extension."""

    css_classes = ("critical",)
    template_name = "django_ca/forms/widgets/critical.html"

    def __init__(self, *args: Any, **kwargs: Any) -> None:
        self.oid = kwargs.pop("oid")
        super().__init__(label=_("critical"), wrapper_classes=("critical",))

    def get_context(self, *args: Any, **kwargs: Any) -> Dict[str, Any]:
        ctx = super().get_context(*args, **kwargs)
        ctx["widget"]["oid"] = self.oid.dotted_string
        return ctx

class LabeledTextInput(widgets.TextInput):
    """CheckboxInput widget that adds a label and wraps everything in a <span />.

    This is necessary because widgets in MultiValueFields don't render with a label."""

    template_name = "django_ca/forms/widgets/labeledtextinput.html"

    def __init__(self, label: str, *args: Any, **kwargs: Any):
        self.label = label
        super().__init__(*args, **kwargs)

    def get_context(self, *args: Any, **kwargs: Any) -> Dict[str, Any]:
        ctx = super().get_context(*args, **kwargs)
        ctx["widget"]["label"] = self.label
        ctx["widget"]["cssid"] = self.label.lower().replace(" ", "-")
        return ctx

    class Media:
        css = {
            "all": ("django_ca/admin/css/labeledtextinput.css",),
        }

class SubjectTextInput(LabeledTextInput):
    """Widget used in :py:class:`~django_ca.widgets.SubjectWidget`."""

    template_name = "django_ca/forms/widgets/subjecttextinput.html"

class ProfileWidget(widgets.Select):
    """Widget for profile selection."""

    template_name = "django_ca/forms/widgets/profile.html"

    def get_context(self, *args: Any, **kwargs: Any) -> Dict[str, Any]:
        ctx = super().get_context(*args, **kwargs)
        ctx["desc"] = ca_settings.CA_PROFILES[ca_settings.CA_DEFAULT_PROFILE].get(
            "description", ca_settings.CA_PROFILES[ca_settings.CA_DEFAULT_PROFILE].get("desc", "")
        )
        return ctx

    class Media:
        js = (
            "admin/js/jquery.init.js",
            "django_ca/admin/js/extensions.js",
            "django_ca/admin/js/profilewidget.js",
        )

class CustomMultiWidget(widgets.MultiWidget):  # pylint: disable=abstract-method; decompress() in subclasses
    """Wraps the multi widget into a <p> element (base class for other widgets)."""

    template_name = "django_ca/forms/widgets/custommultiwidget.html"

class SubjectWidget(CustomMultiWidget):
    """Widget for a :py:class:`~django_ca.subject.Subject`."""

    def __init__(self, attrs: Optional[Dict[str, str]] = None) -> None:
        _widgets = (
            SubjectTextInput(label=_("Country"), attrs={"placeholder": "2 character country code"}),
            SubjectTextInput(label=_("State")),
            SubjectTextInput(label=_("Location")),
            SubjectTextInput(label=_("Organization")),
            SubjectTextInput(label=_("Organizational Unit")),
            SubjectTextInput(label=_("CommonName"), attrs={"required": True}),
            SubjectTextInput(label=_("E-Mail")),
        )
        super().__init__(_widgets, attrs)

    def decompress(self, value: Optional[x509.Name]) -> List[str]:
        if not value:
            return ["" for attr in ADMIN_SUBJECT_OIDS]

        attr_mapping = {attr.oid: attr.value for attr in value}
        return [attr_mapping.get(oid, "") for oid in ADMIN_SUBJECT_OIDS]  # type: ignore[misc]

class GeneralNamesWidget(Textarea):
    """Widget for a list of :py:class:`~cg:cryptography.x509.GeneralName` instances."""

    def format_value(self, value: Optional[Union[str, Iterable[x509.GeneralName]]]) -> str:
        if isinstance(value, str):  # Received during form rendering for a bound form with errors
            return value
        if not value:
            return ""
        return "\n".join([format_general_name(name) for name in value])

class ExtensionWidget(MultiWidget):  # pylint: disable=abstract-method  # is an abstract class
    """Base class for widgets that display a :py:class:`~cg:cryptography.Extension`.

    Subclasses of this class are expected to set the `extension_widgets` attribute or implement `get_widgets`.
    """

    extension_widgets: Optional[ExtensionWidgetsType]
    oid: x509.ObjectIdentifier
    css_classes = ("extension",)

    def __init__(self, attrs: Optional[Dict[str, str]] = None, **kwargs: Any) -> None:
        sub_widgets = self.get_widgets(**kwargs) + (CriticalInput(oid=self.oid),)
        super().__init__(widgets=sub_widgets, attrs=attrs)

    def get_widgets(self, **kwargs: Any) -> ExtensionWidgetsType:
        """Get sub-widgets used by this widget."""
        if self.extension_widgets is not None:  # pragma: no branch
            return self.extension_widgets
        raise ValueError(  # pragma: no cover
            "ExtensionWidget is expected to either set widgets or implement get_widgets()."
        )

class DistributionPointWidget(ExtensionWidget):
    """Widgets for extensions that use a DistributionPoint."""

    extension_widgets = (
        GeneralNamesWidget(attrs={"class": "full-name", "rows": 3}),
        TextInput(attrs={"class": "relative-name"}),
        GeneralNamesWidget(attrs={"class": "crl-issuer", "rows": 3}),
        SelectMultiple(choices=REVOCATION_REASONS, attrs={"class": "reasons"}),
    )
    labels = (
        _("Full name"),
        _("Relative name"),
        _("CRL issuer"),
        _("Reasons"),
    )

    def decompress(
        self, value: Optional[x509.Extension[x509.CRLDistributionPoints]]
    ) -> Tuple[str, str, str, List[str], bool]:
        full_name = relative_name = crl_issuer = ""
        reasons: List[str] = []

        if value is None:
            return full_name, relative_name, crl_issuer, reasons, EXTENSION_DEFAULT_CRITICAL[self.oid]
        if len(value.value) > 1:
            log.warning(
                "Received multiple DistributionPoints, only the first can be changed in the web interface."
            )

        dpoint = value.value[0]
        if dpoint.relative_name:
            relative_name = dpoint.relative_name.rfc4514_string()
        if dpoint.reasons:
            reasons = [reason.name for reason in dpoint.reasons]

        return dpoint.full_name, relative_name, dpoint.crl_issuer, reasons, value.critical

class MultipleChoiceExtensionWidget(  # pylint: disable=abstract-method  # is an abstract class
    ExtensionWidget
):
    """Base class for widgets that can be displayed with a simple SelectMultiple widget."""

    def get_widgets(  # type: ignore[override]  # we are more specific here
        self, choices: typing.Sequence[Tuple[str, str]]
    ) -> Tuple[widgets.SelectMultiple]:
        return (widgets.SelectMultiple(choices=choices),)

class AuthorityInformationAccessWidget(ExtensionWidget):
    """Widget for a :py:class:`~cg:cryptography.x509.AuthorityInformationAccess` extension."""

    extension_widgets = (
        GeneralNamesWidget(attrs={"class": "ca-issuers", "rows": 3}),
        GeneralNamesWidget(attrs={"class": "ocsp", "rows": 3}),
    )
    help_texts = (
        _("Location(s) of the CA certificate."),
        _("Location(s) of the OCSP responder."),
    )
    labels = (
        _("CA issuers"),
        _("OCSP"),
    )
    oid = ExtensionOID.AUTHORITY_INFORMATION_ACCESS

    def decompress(
        self, value: Optional[x509.Extension[x509.AuthorityInformationAccess]]
    ) -> Tuple[List[x509.GeneralName], List[x509.GeneralName], bool]:
        if value is None:
            return [], [], EXTENSION_DEFAULT_CRITICAL[self.oid]

        ocsp = [
            ad.access_location for ad in value.value if ad.access_method == AuthorityInformationAccessOID.OCSP
        ]
        ca_issuers = [
            ad.access_location
            for ad in value.value
            if ad.access_method == AuthorityInformationAccessOID.CA_ISSUERS
        ]

        return ca_issuers, ocsp, value.critical

class CertificatePoliciesWidget(ExtensionWidget):
    """Widget for a :py:class:`~cg:cryptography.x509.CertificatePolicies` extension."""

    oid = ExtensionOID.CERTIFICATE_POLICIES
    extension_widgets = (
        forms.TextInput(),  # policy identifier
        forms.Textarea(attrs={"rows": 3}),  # practice statement
        forms.Textarea(attrs={"rows": 3}),  # explicit text
    )
    help_texts = (
        "",
        _(
            "A pointers (e.g. URLs) to a certification practice statement (CPS). Separate multiple pointers "
            "with a newline."
        ),
        _("A textual statement that can be displayed to the user"),
    )
    labels = (
        _("Policy Identifier"),
        _("Certificate Practice Statement(s)"),
        _("Explicit Text"),
    )

    def decompress(
        self, value: Optional[x509.Extension[x509.CertificatePolicies]]
    ) -> Tuple[str, str, str, bool]:
        if value is None:
            return "", "", "", EXTENSION_DEFAULT_CRITICAL[ExtensionOID.CERTIFICATE_POLICIES]

        ext_value = value.value

        # COVERAGE NOTE: ruled out by the admin interface
        if certificate_policies_is_simple(ext_value) is False:  # pragma: no cover
            raise ValueError("This widget only supports a simple certificate policy values.")

        policy_information = ext_value[0]
        practice_statement: List[str] = []
        explicit_text = ""
        for policy_qualifier in policy_information.policy_qualifiers:
            if isinstance(policy_qualifier, str):
                practice_statement.append(policy_qualifier)
            else:  # UserNotice object
                explicit_text = policy_qualifier.explicit_text

        return (
            policy_information.policy_identifier.dotted_string,
            "\n".join(practice_statement),
            explicit_text,
            value.critical,
        )

class CRLDistributionPointsWidget(DistributionPointWidget):
    """Widget for a :py:class:`~cg:cryptography.x509.CRLDistributionPoints` extension."""

    help_texts = (
        _("Location(s) where to retrieve the CRL."),
        _(
            "X.500 Relative name to retrieve the CRL. RFC 5280 does not recommend setting this field. Cannot "
            "be set together with Full name."
        ),
        _("Distinguished name of the issuer of the CRL."),
        _("Revocation reasons that are included in this CRL, leave empty for all reasons (recommended)."),
    )
    oid = ExtensionOID.CRL_DISTRIBUTION_POINTS

class ExtendedKeyUsageWidget(MultipleChoiceExtensionWidget):
    """Widget for a :py:class:`~cg:cryptography.x509.ExtendedKeyUsage` extension."""

    oid = ExtensionOID.EXTENDED_KEY_USAGE

    def decompress(self, value: Optional[x509.Extension[x509.ExtendedKeyUsage]]) -> Tuple[List[str], bool]:
        if value is None:
            return [], EXTENSION_DEFAULT_CRITICAL[self.oid]
        choices = [EXTENDED_KEY_USAGE_NAMES[usage] for usage in value.value]
        return choices, value.critical

class FreshestCRLWidget(DistributionPointWidget):
    """Widget for a :py:class:`~cg:cryptography.x509.FreshestCRL` extension."""

    oid = ExtensionOID.FRESHEST_CRL

class KeyUsageWidget(MultipleChoiceExtensionWidget):
    """Widget for a :py:class:`~cg:cryptography.x509.KeyUsage` extension."""

    oid = ExtensionOID.KEY_USAGE

    def decompress(self, value: Optional[x509.Extension[x509.KeyUsage]]) -> Tuple[List[str], bool]:
        if value is None:
            return [], EXTENSION_DEFAULT_CRITICAL[self.oid]
        choices = []

        # Cannot use a list comprehension here, because cryptography raises ValueError for some attributes
        for choice in KEY_USAGE_NAMES:
            try:
                chosen = getattr(value.value, choice)
            except ValueError:
                # cryptography raises ValueError for decipher/encipher_only if key_agreement is not set
                chosen = False

            if chosen:
                choices.append(choice)

        return choices, value.critical

class IssuerAlternativeNameWidget(ExtensionWidget):
    """Widget for a :py:class:`~cg:cryptography.x509.IssuerAlternativeName` extension."""

    extension_widgets = (GeneralNamesWidget(attrs={"rows": 3}),)
    oid = ExtensionOID.ISSUER_ALTERNATIVE_NAME

    def decompress(
        self, value: Optional[x509.Extension[x509.IssuerAlternativeName]]
    ) -> Tuple[List[x509.GeneralName], bool]:
        if value is None:
            return [], EXTENSION_DEFAULT_CRITICAL[self.oid]
        return list(value.value), value.critical

class OCSPNoCheckWidget(ExtensionWidget):
    """Widget for a :py:class:`~cg:cryptography.x509.OCSPNoCheck` extension."""

    extension_widgets = (LabeledCheckboxInput(label=_("included"), wrapper_classes=["include"]),)
    oid = ExtensionOID.OCSP_NO_CHECK

    def decompress(self, value: Optional[x509.Extension[x509.OCSPNoCheck]]) -> Tuple[bool, bool]:
        if value is None:
            return False, EXTENSION_DEFAULT_CRITICAL[self.oid]
        return True, value.critical

class SubjectAlternativeNameWidget(ExtensionWidget):
    """Widget for a :py:class:`~cg:cryptography.x509.IssuerAlternativeName` extension."""

    extension_widgets = (
        GeneralNamesWidget(attrs={"rows": 3}),
        LabeledCheckboxInput(label="Include CommonName"),
    )
    oid = ExtensionOID.SUBJECT_ALTERNATIVE_NAME

    # COVERAGE NOTE: In Django 4.1, decompress is not called if compress() returns a tuple
    #       https://github.com/django/django/commit/37602e49484a88867f40e9498f86c49c2d1c5d7c
    def decompress(
        self,
        value: Optional[
            Union[
                Tuple[List[x509.GeneralName], bool, bool],
                Tuple[x509.Extension[x509.SubjectAlternativeName], bool],
            ]
        ],
    ) -> Tuple[List[x509.GeneralName], bool, bool]:  # pragma: no cover
        if value is None:
            default_cn_in_san = ca_settings.CA_PROFILES[ca_settings.CA_DEFAULT_PROFILE]["cn_in_san"]
            return [], default_cn_in_san, EXTENSION_DEFAULT_CRITICAL[self.oid]

        if len(value) == 3:
            # TYPE NOTE: mypy does not eliminate two-tuple from union in length check
            return typing.cast(Tuple[List[x509.GeneralName], bool, bool], value)

        ext, cn_in_san = value  # type: ignore[misc]
        if ext is None:
            return [], cn_in_san, EXTENSION_DEFAULT_CRITICAL[self.oid]

        return list(ext.value), cn_in_san, ext.critical

class TLSFeatureWidget(MultipleChoiceExtensionWidget):
    """Widget for a :py:class:`~cg:cryptography.x509.TLSFeature` extension."""

    oid = ExtensionOID.TLS_FEATURE

    def decompress(self, value: Optional[x509.Extension[x509.TLSFeature]]) -> Tuple[List[str], bool]:
        if value is None:
            return [], EXTENSION_DEFAULT_CRITICAL[self.oid]
        return [feature.name for feature in value.value], value.critical


from plumbum import local

from benchbuild.project import Project
from benchbuild.environments import container
from benchbuild.source import HTTP
from benchbuild.utils.cmd import make, tar

class LibreSSL(Project):
    """ OpenSSL """

    NAME: str = 'libressl'
    DOMAIN: str = 'encryption'
    GROUP: str = 'benchbuild'
    BINARIES = [
        "aeadtest", "aes_wrap", "asn1test", "base64test", "bftest", "bntest",
        "bytestringtest", "casttest", "chachatest", "cipherstest",
        "cts128test", "destest", "dhtest", "dsatest", "ecdhtest", "ecdsatest",
        "ectest", "enginetest", "evptest", "exptest", "gcm128test",
        "gost2814789t", "hmactest", "ideatest", "igetest", "md4test",
        "md5test", "mdc2test", "mont", "pbkdf2", "pkcs7test", "poly1305test",
        "pq_test", "randtest", "rc2test", "rc4test", "rmdtest", "sha1test",
        "sha256test", "sha512test", "shatest", "ssltest", "timingsafe",
        "utf8test"
    ]
    SOURCE = [
        HTTP(remote={
            '2.1.6.':
            'http://ftp.openbsd.org/pub/OpenBSD/LibreSSL/libressl-2.1.6.tar.gz'
        },
             local='libressl.tar.gz')
    ]
    CONTAINER = container.Buildah().from_('debian:buster-slim')

    def compile(self):
        libressl_source = local.path(self.source_of('libressl.tar.gz'))
        libressl_version = self.version_of('libressl.tar.gz')

        self.cflags += ["-fPIC"]

        clang = compiler.cc(self)

        tar("xfz", libressl_source)
        unpack_dir = local.path(f'libressl-{libressl_version}')
        configure = local[unpack_dir / "configure"]
        configure = run.watch(configure)
        make_ = run.watch(make)

        with local.cwd(unpack_dir):
            with local.env(CC=str(clang)):
                configure("--disable-asm", "--disable-shared",
                          "--enable-static", "--disable-dependency-tracking",
                          "--with-pic=yes")

            make_("-j8")
            make_tests = make["-Ctests", "-j8"]
            make_tests = run.watch(make_tests)
            make_tests(LibreSSL.BINARIES)

    def run_tests(self):
        libressl_version = self.version_of('libressl.tar.gz')
        unpack_dir = local.path(f'libressl-{libressl_version}')
        with local.cwd(unpack_dir / "tests"):
            for binary in LibreSSL.BINARIES:
                wrapping.wrap(local.cwd / binary, self)

        with local.cwd(unpack_dir):
            make_ = run.watch(make)
            make_("V=1", "check", "-i")


# -*- coding: utf-8 -*-
#!/usr/bin/python
"""
Created on Thu Jan 17 10:06:03 2019

@author: pageot
"""

import pandas as pd
import numpy as np
import os
import datetime
import matplotlib.pyplot as plt

d={}
d["data_file"]="/datalocal/vboxshare/THESE/CLASSIFICATION/DONNES_SIG/DONNEES_METEO/SYNOP_MF"
list_synop=os.listdir(d['data_file'])
list_sta=pd.read_csv("/datalocal/vboxshare/THESE/CLASSIFICATION/DONNES_SIG/DONNEES_METEO/postesSynop.csv")
list_stat_supp=list(list_sta.iloc[42:,1])
list_stat_int=list(list_sta.iloc[[26,27,28,31,32,33,34],1])
list_stat_supp.append(78890)
list_stat_supp.append(61970)
list_sta=list_sta[:42]

#data=pd.DataFrame([])
#
now = datetime.datetime.now()

df=pd.DataFrame([])
for i in list_synop:
    reader=pd.read_csv(d['data_file']+"/"+i,sep=';')
    tab=reader.rename(index=str,columns={'numer_sta':'ID'})
    for j in list_stat_int:
        tabdata=tab[tab.ID == j]
        tab_sta=list_sta.append([list_sta]*(tabdata.shape[0]/42),ignore_index=True)
        Fusion=tab_sta.merge(tabdata, on='ID')
        df2=Fusion.drop_duplicates(keep="last")
        df=df.append(df2,ignore_index=True)
Data_climat=df.iloc[:,[1,2,3,4,5,6,12,-18]]
Data_climat['t']=Data_climat['t'].replace(to_replace=['mq'],value=-9999)
Data_climat['rr24']=Data_climat['rr24'].replace(to_replace=['mq'],value=-9999)
Data_climat[['t','rr24']]=Data_climat[['t','rr24']].astype(float)
Data_climat['t']=Data_climat['t']-273.15
Data_climat.drop_duplicates(keep="last", inplace=True)
a=[]

Data_climat['date']=Data_climat['date'].astype(str)
#Spliter les dates en AAAAMMJJ et
JJall=[]
for s in Data_climat.date:
    JJ=s[0:8]
    JJall.append(JJ)
Data_climat['JJ']=JJall
Data_climat['JJ']=Data_climat['JJ'].astype(int)
DF_Tmean=Data_climat.groupby(['Nom','JJ'])['t'].mean().reset_index()

now2= datetime.datetime.now()
print (now2-now)
DF_Tmean.to_csv("DATA_climat.csv",sep="\t")

#==============================================================================
# divers Test
#==============================================================================
#Tmean=[]
#rowa=[]
#stat=list(set(Data_climat.Nom))
#jj=list(set(Data_climat.JJ))
#jj=sorted(jj)

#for l in stat:
#    for t in jj:
#        a=Data_climat.t.loc[(Data_climat['Nom']==l) & (Data_climat['JJ']==t)].mean()
#        Tmean.append(a)
## ajoute condition de présence de la date sur le lieu.
#rowa=sorted(rowa)
#list_id=list(Data_climat.id)
#ju=[]
#for i in list_id:
#    g=list_id[i] in rowa
#    ju.append(g)
#
#
#Data_climat['drop']=ju
#Data_climat['drop']=Data_climat['drop'].replace(to_replace=False,value=pd.NaT)
#df_climat=Data_climat
#Data_climat=Data_climat.dropna()
#Data_climat['t_mean']=Tmean

#extraction des stations d'interets

#list_stat_int=list(list_sta.iloc[[26,27,28,31,32,33,34],1])
#dfs_cli=pd.DataFrame([])
#for k in list_stat_int:
#    df_climat=data.loc[data.ID==k]
#    dfs_cli=dfs_cli.append(df_climat,ignore_index=True)
#    data_climat=dfs_cli.iloc[:,[1,2,3,4,5,6,12,-18]]
#    data_climat['t']=data_climat['t'].replace(to_replace=['mq'],value=-9999)
#    data_climat['rr24']=data_climat['rr24'].replace(to_replace=['mq'],value=-9999)
#    data_climat[['t','rr24']]=data_climat[['t','rr24']].astype(float)
#    data_climat['t']=data_climat['t']-273.15
#    data_climat.drop_duplicates(keep="last", inplace=True)
#a=[]
#for r in data_climat.date:
#    if r%100000 == 0:
#        b=1
#    else:
#        b=pd.NaT
#    a.append(b)
#data_climat['drop']=a
#
#data_climat['date']=data_climat['date'].astype(str)
##Spliter les dates en AAAAMMJJ et
#JJall=[]
#for s in data_climat.date:
#    JJ=s[0:8]
#    JJall.append(JJ)
#data_climat['JJ']=JJall
#data_climat['JJ']=data_climat['JJ'].astype(int)
#
#Tmean=[]
#for t in data_climat.JJ:
#    a= data_climat.t.loc[data_climat['JJ']==t].mean()
#    Tmean.append(a)
#data_climat['t_mean']=Tmean
#data_climat=data_climat.dropna()
#now2= datetime.datetime.now()
#print (now2-now)
#Data_climat.to_csv("DATA_climat.csv",sep="\t")

##test extctraction deux colones + conversion T en T°c
#test=Data_climat.iloc[0:]
#test['date']=test['date'].astype(str)
#idr=[i for i in np.arange(0,test.shape[0],1)]
#test['id']=idr
##Spliter les dates en AAAAMMJJ et
#JJall=[]
#for s in test.date:
#    JJ=s[0:8]
#    JJall.append(JJ)
#test['JJ']=JJall
#test['JJ']=test['JJ'].astype(int)
#
#temean=[]
#rowa=[]
#stat=list(set(test.Nom))
#jj=list(set(test.JJ))
#
#for t in jj:
#    for l in stat:
#        a=test.t.loc[(test['Nom']==l) & (test['JJ']==t)].mean()
#        temean.append(a)
#        h=test.id.loc[(test['Nom']==l) & (test['JJ']==t)].iloc[0]
#        rowa.append(h)
#rowa=sorted(rowa)
#list_id=list(test.id)
##Moyenner les dates
#
#
#
##supp les lignes en trop afin d'ajoute la valeur journalière
#
#a=[]
#for i in list_id:
#    g=list_id[i] in rowa
#    a.append(g)
#
#
#test['drop']=a
#test['drop']=test['drop'].replace(to_replace=False,value=pd.NaT)
#test1['t_mean']=temean
#test1=test.dropna()
#depa= []
#fin = []
#for t in test.date:
#    if t%100000 == 10000:
#        print t
#        fin.append(t)
#    if t%100000 == 0:
#        print t
#        depa.append(t)
#
#redd=pd.DataFrame([])
#bluee=pd.DataFrame([])
#for i in depa:
#    qa=test.loc[test['date'] == i]
#    redd=redd.append(qa,ignore_index=False)
#for h in fin:
#    tr=test.loc[test['date'] ==h]
#    bluee=bluee.append(tr, ignore_index=False)
#redd.drop_duplicates(keep="last",inplace=True)
#bluee.drop_duplicates(keep="last", inplace=True)
##tré les ID par
#bluee.sort_index(axis = 0, ascending = True,inplace =True)
#redd.sort_index(axis = 0, ascending = True,inplace =True)
## 2 solution
#depar=pd.DataFrame(redd.iloc[:,-1])
#depar=depar.reset_index()
#fini=pd.DataFrame(bluee.iloc[:,-1])
#fini=fini.reset_index()
#tabs=[depar,fini]
#
#boucletab=pd.concat(tabs,axis=1,ignore_index=False)
#boucletab.replace(to_replace=['Nan'],value=-9999,inplace=True)
#boucletab.drop('index',axis=1,inplace=True)
##boucletab.columns = range(boucletab.shape[1])
#boucletab=boucletab.astype(int)
#date_moyenne=[]
#for index,row in boucletab.iterrows():
#    print row
#    a=test.t[row[0]:row[1]].mean()
#    print a
#    date_moyenne.append(a)

#
##==============================================================================
## creation plot dynamique rr + t
##==============================================================================
#plt.plot(DF_Tmean.Nom, DF_Tmean.t)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 20 15:51:23 2020

@author: emmadevin
"""

import os
import sys
import pandas as pd
import numpy as np
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter

#os.environ["CARTOPY_USER_BACKGROUNDS"] = "/home/opt/anaconda3/lib/python3.7/site-packages/cartopy/data/raster/natural_earth"

bias = pd.read_csv('/Users/emmadevin/Work/USGS Work/USGS PLUM/Data/LME_SoCal/bias.csv')
siteTerms = pd.read_csv('/Users/emmadevin/Work/USGS Work/USGS PLUM/Data/LME_SoCal/site_terms.csv')
eventTerms = pd.read_csv('/Users/emmadevin/Work/USGS Work/USGS PLUM/Data/LME_SoCal/event_terms.csv')

dataset = pd.read_csv('/Users/emmadevin/Work/USGS Work/USGS PLUM/Data/SoCal_withMMIpred.csv')

locations = dataset[['StationID','StationLatitude','StationLongitude']]
locations =  locations.drop_duplicates(subset = 'StationID')
locations = locations.reset_index()

df = locations.merge(siteTerms, how='inner',indicator=False)

low = df['TotalResidual']>-1
high = df['TotalResidual']<1

df1 = df[low]
df1 = df1[high]

lon = df1['StationLongitude']
lat = df1['StationLatitude']
site = df1['TotalResidual']

data_crs = ccrs.PlateCarree()

# s = site
# S = np.array(site)
# S1 = S.reshape(len(lon), len(lat))
# S2 = np.transpose(S1)
SiteTermsCoordinates = pd.DataFrame()

SiteTermsCoordinates['lat']=lat
SiteTermsCoordinates['lon']=lon
SiteTermsCoordinates['site']=site

SiteTermsCoordinates.to_csv('/Users/emmadevin/Work/USGS Work/USGS PLUM/Data/SiteTermsCoord.csv')

ax = plt.axes(projection=ccrs.PlateCarree())
#ax.background_img(name='BM', resolution='low')
#ax.stock_img()
ax.set_global()
ax.coastlines()
ax.set_xticks([-125,-120,-119,-118,-117,-116,-115,-110], crs=ccrs.PlateCarree())
ax.set_yticks([-90, -60, -30, 0, 30,33,34,35,40,45, 60, 90], crs=ccrs.PlateCarree())
ax.set_extent([-119, -117.5, 33.5, 34.5], crs=ccrs.PlateCarree())
lon_formatter = LongitudeFormatter(zero_direction_label=True)
lat_formatter = LatitudeFormatter()
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)
plt.scatter(lon, lat, s = 4, transform=data_crs, c=site, cmap='seismic')
#plt.contour(lon, lat, S2, cmap='seismic')
cbar = plt.colorbar()
cbar.set_label('Site Term')
#cbar.set_clim(-1.0, 1.0)
plt.title('Map of LA with Site Terms')
plt.scatter(-118.2437, 33.9522, s = 6000, facecolors = 'none', edgecolors = 'k', label = 'LA')


import tweepy
import tweepy.models
import time

class StreamingClient:
    def __init__(self, api: tweepy.API, interval=5):
        self.api = api
        self.interval = interval
        self.tweets = []
        self.api.wait_on_rate_limit = True

    def on_tweet(self, data: tweepy.models.Status):
        return data

    def filter(self, query):
        first = False
        while True:
            tweets = []
            # 最新のツイートを取得
            new_tweets = tweepy.Cursor(
                self.api.search_tweets, q=query, result_type="recent", count=5, tweet_mode="extended").items(5)

            # 初回のみツイートを取得
            if first == False:
                self.tweets = list((tweet for tweet in new_tweets))
                first = True
                time.sleep(self.interval)
                continue

            for tweet in new_tweets:
                if len(self.tweets) != 0:
                    # 既に取得済みのツイートは無視
                    if tweet in self.tweets:
                        break
                tweets.append(tweet)

            for tweet in tweets:
                self.on_tweet(tweet)
            # 更新があった場合はツイートを更新
            if len(tweets) > 0:
                self.tweets = tweets

            time.sleep(self.interval)


import numpy as np
import cv2
'''
input_file = input('input filename\n')
num_images =  len([f for f in os.listdir(input_file)if os.path.isfile(os.path.join(input_file, f))])
output_file = 'filtered_images'

try:
    os.mkdir(output_file)
except OSError:
    print ("Creation of the directory {} failed".format(output_file))
print(num_images)
for i in range(num_images):
    img  = cv2.imread('{}/frame{}.jpg'.format(input_file,i))
    shape = np.shape(img)
    height = int(shape[0]/2)
    cropped = img[height:shape[0],:]
    img=cv2.cvtColor(cropped,cv2.COLOR_BGR2RGB)
    vectorized = img.reshape((-1,3))
    vectorized = np.float32(vectorized)
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
    K = 2
    attempts=10
    ret,label,center=cv2.kmeans(vectorized,K,None,criteria,attempts,cv2.KMEANS_PP_CENTERS)
    center = np.uint8(center)
    res = center[label.flatten()]
    result_image = res.reshape((img.shape))
    figure_size = 15

    dst = cv2.medianBlur(result_image,35)

    cv2.imwrite('{}/frame_{}.jpg'.format(output_file,i),dst)'''

def filtering(filename):
    img = cv2.imread(filename)
    shape = np.shape(img)
    height = int(shape[0]/2)
    cropped = img[height:shape[0],:]
    img = cv2.cvtColor(cropped,cv2.COLOR_BGR2RGB)
    vectorized = img.reshape((-1,3))
    vectorized = np.float32(vectorized)
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
    K = 2
    attempts = 10
    ret, label, center = cv2.kmeans(vectorized,K,None,criteria,attempts,cv2.KMEANS_PP_CENTERS)
    center = np.uint8(center)
    res = center[label.flatten()]
    result_image = res.reshape((img.shape))

    output_image = cv2.medianBlur(result_image, 55)
    return output_image

if __name__ == '__main__':
    filtering('frame.jpg')


#!/usr/bin/env python3

from string import digits

even = odd = 0

x = input('Enter a string including a series of digits: ')

even = len([c for c in x if c in digits and int(c) % 2 == 0])

odd = len([c for c in x if c in digits and int(c) % 2 != 0])

print(f'{x} contained {even} even digits and {odd} odd digits!')


import tensorflow as tf
import numpy as np
import pickle
from tools import accuracy

##########parameters of data###########
image_size = 48
num_labels = 7
num_channels = 1
#######################################

###############load data###############
pickle_file = './data/fer2013.pickle'
f = open(file=pickle_file, mode='rb')
data = pickle.load(file=f)

train_data, train_labels = data['train_data'], data['train_labels']
valid_data, valid_labels = data['valid_data'], data['valid_labels']
test_data, test_labels = data['test_data'], data['test_labels']
del data

def reformat(dataset, labels): # we have to reshapebecause of conv layers
    dataset = dataset.reshape((-1, image_size, image_size, num_channels)).astype(np.float32)
    return dataset, labels
train_data, train_labels = reformat(train_data, train_labels)
valid_data, valid_labels = reformat(valid_data, valid_labels)
test_data, test_labels = reformat(test_data, test_labels)

print('Loaded from pickle.')
print('Training set:', train_data.shape, ', labels:', train_labels.shape)
print('Validation set:', valid_data.shape, ', labels:', valid_labels.shape)
print('Test set:', test_data.shape, ', labels:', test_labels.shape)
print('#'*40)
#######################################

###########hyper parameters############
batch_size = 64

reg_const = 0.05 # l2 reg constant

# learning rate: exponential decay
initial_learning_rate = 0.04
decay_steps = 1000
decay_rate = 0.65

filter1 = 5 # patch_size
filter2 = 2
depth1 = 20
depth2 = 50

hidden = 500 # num of neurons in fully connected hidden layer

num_steps = 64*20
#######################################

graph = tf.Graph()
with graph.as_default():

    # input data
    tf_train_data = tf.placeholder(shape=(batch_size, image_size, image_size, num_channels),
                                   dtype=tf.float32)
    tf_train_labels = tf.placeholder(shape=(batch_size, num_labels),
                                     dtype=tf.float32)
    tf_valid_data = tf.constant(value=valid_data)
    tf_test_data = tf.constant(value=test_data)

    # variables
    global_step = tf.Variable(0) # counts number of steps taken

    l1_conv = tf.Variable(tf.truncated_normal([filter1, filter1, num_channels, depth1], stddev=0.1))
    b1_conv = tf.Variable(tf.zeros([depth1]))
    l2_conv = tf.Variable(tf.truncated_normal([filter2, filter2, depth1, depth2], stddev=0.1))
    b2_conv = tf.Variable(tf.zeros([depth2]))

    l3 = tf.Variable(tf.truncated_normal([(image_size//4)*(image_size//4)*depth2, hidden], stddev=0.1))
    b3 = tf.Variable(tf.zeros([hidden]))
    l4 = tf.Variable(tf.truncated_normal([hidden, num_labels], stddev=0.1))
    b4 = tf.Variable(tf.zeros([num_labels]))

    weights_and_biases = [l1_conv, b1_conv, l2_conv, b2_conv, l3, b3, l4, b4] # list of all weights and biases

    # computation
    def model(X):
        cl1 = tf.nn.conv2d(input=X,
                           filter=l1_conv,
                           strides=[1,1,1,1],
                           padding='SAME')
        cl1_relu = tf.nn.relu(cl1 + b1_conv)
        cl1_maxpool = tf.nn.max_pool(value=cl1_relu,
                                     ksize=[1,2,2,1], # velikost v jednotlivyh dimenzich: 1 v batch, 2 v height a width, 1 v channels
                                     strides=[1,2,2,1], # posun v jednotlivych dimenzich
                                     padding='SAME')

        cl2 = tf.nn.conv2d(input=cl1_maxpool,
                           filter=l2_conv,
                           strides=[1,1,1,1],
                           padding='SAME')
        cl2_relu = tf.nn.relu(cl2 + b2_conv)
        cl2_maxpool = tf.nn.max_pool(value=cl2_relu,
                                     ksize=[1,2,2,1],
                                     strides=[1,2,2,1],
                                     padding='SAME')

        shape = cl2_maxpool.get_shape().as_list()
        cl2_reshape = tf.reshape(tensor=cl2_maxpool, shape=[shape[0], shape[1]*shape[2]*shape[3]]) # for example from [128, 7, 7, 16] to [16, 784] = [128, 7*7*16]
        cl3 = tf.nn.relu(tf.matmul(cl2_reshape, l3) + b3)
        output = tf.matmul(cl3, l4) + b4
        return output

    logits = model(tf_train_data) # logits for training

    penalty = 0.5*reg_const*sum([tf.nn.l2_loss(x) for x in weights_and_biases]) # penalties for weights and biases
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits,tf_train_labels)) + penalty

    # optimizer
    learning_rate = tf.train.exponential_decay(learning_rate=initial_learning_rate,
                                               global_step=global_step,
                                               decay_steps=decay_steps,
                                               decay_rate=decay_rate)
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(loss=loss, global_step=global_step)

    # predictions
    train_prediction = tf.nn.softmax(logits)
    valid_prediction = tf.nn.softmax(model(X=valid_data))
    test_prediction = tf.nn.softmax(model(X=test_data))

with tf.Session(graph=graph) as session:
    tf.initialize_all_variables().run()
    print('Initialized\n{}'.format('-'*40))
    for step in range(num_steps):
        offset = (step*batch_size) % (train_labels.shape[0] - batch_size) # we slide through all data
        batch_data = train_data[offset:(offset+batch_size), :]
        batch_labels = train_labels[offset:(offset+batch_size), :]
        feed_dict = {tf_train_data: batch_data, # feed by this batch data
                     tf_train_labels: batch_labels}
        _, l, predictions = session.run(fetches=[optimizer, loss, train_prediction],
                                        feed_dict=feed_dict)

        if (step % 500 == 0):
            print('Minibatch loss at step {}: {:.3f}'.format(step, l))
            print('Learning rate: {:.3f}'.format(learning_rate.eval()))
            print('Minibatch accuracy: {:.1f}%'.format(accuracy(predictions, batch_labels)))
            print('Validation accuracy: {:.1f}%'.format(accuracy(valid_prediction.eval(), valid_labels)))
            print('-'*40)
    print('Test accuracy: {:.1f}%'.format(accuracy(test_prediction.eval(), test_labels)))


from queue import PriorityQueue

class Solution:
    def swimInWater(self, grid: List[List[int]]) -> int:
        m, n = len(grid), len(grid[0])

        is_in_bound = lambda p: 0 <= p[0] < m and 0 <= p[1] < n
        get_adjacent_points = lambda p: (
            (p[0] + dr, p[1] + dc) for dr, dc in ((-1, 0), (0, -1), (1, 0), (0, 1))
        )
        make_point = (
            lambda p: (grid[p[0]][p[1]], p[0], p[1]) if is_in_bound(p) else (-1, -1, -1)
        )

        start_point, end_point = make_point((0, 0)), make_point((m - 1, n - 1))

        pq = PriorityQueue()
        pq.put(start_point)

        max_time = 0
        visited_points = set()
        while not pq.empty():
            e, i, j = pq.get()

            visited_points.add((i, j))
            max_time = max(max_time, e)

            if (e, i, j) == end_point:
                return max_time

            for p in get_adjacent_points((i, j)):
                if is_in_bound(p) and p not in visited_points:
                    pq.put(make_point(p))

        return max_time


"""
10799 쇠막대기

"""

import sys

input = sys.stdin.readline
iron = list(input().strip())
ans = 0
ls = []

for i in range(len(iron)):
    if iron[i] == '(':
        ls.append('(')

    else:
        if iron[i-1] == '(':
            ls.pop()
            ans += len(ls)

        else:
            ls.pop()
            ans += 1

print(ans)


import tensorflow as tf

a_1 = tf.constant(2)
b_1 = tf.constant(3)
c_1 = tf.constant(4)
a_2 = tf.placeholder(tf.int32, [3])
b_2 = tf.constant(2)
a_3 = tf.placeholder(tf.int32, [None])
b_3 = tf.constant(10)

add_op = a_1 + b_1
clac1_op = a_1 + b_1 + c_1
clac2_op = (a_1 + b_1) * c_1
x2_op = a_2 * b_2
x10_op = a_3 * b_3
d2_op = a_3 / b_3

sess = tf.Session()
res1_1 = sess.run(clac1_op)
res2_1 = sess.run(clac2_op)
res1_2 = sess.run(x2_op, feed_dict = {a_2 : [1, 2, 3]})
res2_2 = sess.run(x2_op, feed_dict = {a_2 : [10, 20, 10]})
res1_3 = sess.run(x10_op, feed_dict = {a_3 : [1, 2, 3, 4, 5]})
res2_3 = sess.run(x10_op, feed_dict = {a_3 : [10, 20]})
res3_3 = sess.run(d2_op, feed_dict = {a_3 : [1, 2, 3, 4, 5]})
# print(res1_1)
# print(res2_1)
# print(res1_2)
# print(res2_2)
print(res1_3)
print(res2_3)
print(res3_3)


# (c) adarsh-goel
import time
import math
import logging
import secrets
import mimetypes
from ..vars import Var
from aiohttp import web
from ..bot import StreamBot
from Adarsh import StartTime
from ..utils.custom_dl import TGCustomYield, chunk_size, offset_fix
from Adarsh.utils.render_template import render_page
from ..utils.time_format import get_readable_time
routes = web.RouteTableDef()
from urllib.parse import quote_plus
kg18="ago"

@routes.get("/", allow_head=True)
async def root_route_handler(request):
    return web.json_response({"status": "running",
                              "maintained_by": "Adarsh Goel",
                              "uptime": get_readable_time(time.time() - StartTime),
                              "Bot was last updated": get_readable_time(time.time()),
                              "ago":"",
                              "telegram_bot": '@'+(await StreamBot.get_me()).username,
                              "Bot Version":"3.0.1"})

@routes.get("/watch/{message_id}")
async def stream_handler(request):
    try:
        message_id = int(request.match_info['message_id'])
        return web.Response(text=await render_page(message_id), content_type='text/html')
    except ValueError as e:
        logging.error(e)
        raise web.HTTPNotFound

@routes.get("/download/{message_id}")
@routes.get("/{message_id}")
async def old_stream_handler(request):
    try:
        message_id = int(request.match_info['message_id'])
        return await media_streamer(request, message_id)
    except ValueError as e:
        logging.error(e)
        raise web.HTTPNotFound

async def media_streamer(request, message_id: int):
    range_header = request.headers.get('Range', 0)
    media_msg = await StreamBot.get_messages(Var.BIN_CHANNEL, message_id)
    file_properties = await TGCustomYield().generate_file_properties(media_msg)
    file_size = file_properties.file_size

    if range_header:
        from_bytes, until_bytes = range_header.replace('bytes=', '').split('-')
        from_bytes = int(from_bytes)
        until_bytes = int(until_bytes) if until_bytes else file_size - 1
    else:
        from_bytes = request.http_range.start or 0
        until_bytes = request.http_range.stop or file_size - 1

    req_length = until_bytes - from_bytes

    new_chunk_size = await chunk_size(req_length)
    offset = await offset_fix(from_bytes, new_chunk_size)
    first_part_cut = from_bytes - offset
    last_part_cut = (until_bytes % new_chunk_size) + 1
    part_count = math.ceil(req_length / new_chunk_size)
    body = TGCustomYield().yield_file(media_msg, offset, first_part_cut, last_part_cut, part_count,
                                      new_chunk_size)

    file_name = file_properties.file_name if file_properties.file_name \
        else f"{secrets.token_hex(2)}.jpeg"
    mime_type = file_properties.mime_type if file_properties.mime_type \
        else f"{mimetypes.guess_type(file_name)}"

    return_resp = web.Response(
        status=206 if range_header else 200,
        body=body,
        headers={
            "Content-Type": mime_type,
            "Content-Range": f"bytes {from_bytes}-{until_bytes}/{file_size}",
            "Content-Disposition": f'attachment; filename="{file_name}"',
            "Accept-Ranges": "bytes",
        }
    )

    if return_resp.status == 200:
        return_resp.headers.add("Content-Length", str(file_size))

    return return_resp


"""
This code is refer from:
https://github.com/roatienza/deep-text-recognition-benchmark/blob/master/modules/vitstr.py
"""

import numpy as np
import torch
import torch.nn as nn
from pytorchocr.modeling.backbones.rec_svtrnet import Block, PatchEmbed

# import paddle
# import paddle.nn as nn
# from ppocr.modeling.backbones.rec_svtrnet import Block, PatchEmbed, zeros_, trunc_normal_, ones_

scale_dim_heads = {'tiny': [192, 3], 'small': [384, 6], 'base': [768, 12]}

class ViTSTR(nn.Module):
    def __init__(self,
                 img_size=[224, 224],
                 in_channels=1,
                 scale='tiny',
                 seqlen=27,
                 patch_size=[16, 16],
                 embed_dim=None,
                 depth=12,
                 num_heads=None,
                 mlp_ratio=4,
                 qkv_bias=True,
                 qk_scale=None,
                 drop_path_rate=0.,
                 drop_rate=0.,
                 attn_drop_rate=0.,
                 norm_layer='nn.LayerNorm',
                 act_layer='gelu',
                 epsilon=1e-6,
                 out_channels=None,
                 **kwargs):
        super().__init__()
        self.seqlen = seqlen
        embed_dim = embed_dim if embed_dim is not None else scale_dim_heads[
            scale][0]
        num_heads = num_heads if num_heads is not None else scale_dim_heads[
            scale][1]
        out_channels = out_channels if out_channels is not None else embed_dim
        self.patch_embed = PatchEmbed(
            img_size=img_size,
            in_channels=in_channels,
            embed_dim=embed_dim,
            patch_size=patch_size,
            mode='linear')
        num_patches = self.patch_embed.num_patches

        self.pos_embed = nn.Parameter(torch.zeros(1, num_patches + 1, embed_dim))
        self.cls_token = nn.Parameter(torch.zeros(1, 1, embed_dim))

        self.pos_drop = nn.Dropout(p=drop_rate)

        dpr = np.linspace(0, drop_path_rate, depth)
        self.blocks = nn.ModuleList([
            Block(
                dim=embed_dim,
                num_heads=num_heads,
                mlp_ratio=mlp_ratio,
                qkv_bias=qkv_bias,
                qk_scale=qk_scale,
                drop=drop_rate,
                attn_drop=attn_drop_rate,
                drop_path=dpr[i],
                norm_layer=norm_layer,
                act_layer=act_layer,
                epsilon=epsilon,
                prenorm=False) for i in range(depth)
        ])
        self.norm = eval(norm_layer)(embed_dim, eps=epsilon)

        self.out_channels = out_channels

        torch.nn.init.xavier_normal_(self.pos_embed)
        torch.nn.init.xavier_normal_(self.cls_token)
        self.apply(self._init_weights)

    def _init_weights(self, m):
        # weight initialization
        if isinstance(m, nn.Conv2d):
            nn.init.kaiming_normal_(m.weight, mode='fan_out')
            if m.bias is not None:
                nn.init.zeros_(m.bias)
        elif isinstance(m, nn.BatchNorm2d):
            nn.init.ones_(m.weight)
            nn.init.zeros_(m.bias)
        elif isinstance(m, nn.Linear):
            nn.init.normal_(m.weight, 0, 0.01)
            if m.bias is not None:
                nn.init.zeros_(m.bias)
        elif isinstance(m, nn.ConvTranspose2d):
            nn.init.kaiming_normal_(m.weight, mode='fan_out')
            if m.bias is not None:
                nn.init.zeros_(m.bias)
        elif isinstance(m, nn.LayerNorm):
            nn.init.ones_(m.weight)
            nn.init.zeros_(m.bias)

    def forward_features(self, x):
        B = x.shape[0]
        x = self.patch_embed(x)
        # cls_tokens = paddle.tile(self.cls_token, repeat_times=[B, 1, 1])
        cls_tokens = self.cls_token.repeat(B, 1, 1)
        x = torch.cat((cls_tokens, x), dim=1)
        x = x + self.pos_embed
        x = self.pos_drop(x)
        for blk in self.blocks:
            x = blk(x)
        x = self.norm(x)
        return x

    def forward(self, x):
        x = self.forward_features(x)
        x = x[:, :self.seqlen]
        return x.permute(0, 2, 1).unsqueeze(2)


import os
import re

import wget
from qiniu import Auth, put_file, etag

import config

# 加载配置
access_key = config.access_key
secret_key = config.secret_key
bucket_name = config.bucket_name
ROOT_DIR_PATH = config.ROOT_DIR_PATH
OLD_IMG_URL_PREFIX = config.OLD_IMG_URL_PREFIX
OLD_IMG_URL_REPLACE_PREFIX = config.OLD_IMG_URL_REPLACE_PREFIX
NEW_IMG_URL_PREFIX = config.NEW_IMG_URL_PREFIX

def upload(local_path, upload_path):
    """
    上传方法
    :param local_path:
    :param upload_path:
    :return:
    """
    print("正在上传 {} ，上传到 {}".format(local_path, upload_path))
    # 构建鉴权对象
    q = Auth(access_key, secret_key)
    key = upload_path
    # 生成上传 Token，可以指定过期时间等
    token = q.upload_token(bucket_name, key, 3600)

    # 要上传文件的本地路径
    localfile = local_path
    ret, info = put_file(token, key, localfile, version='v2')
    print(info)

    assert ret['key'] == key
    assert ret['hash'] == etag(localfile)

def subdir_list(dirname):
    """获取目录下所有子目录名
    @param dirname: str 目录的完整路径
    @return: list(str) 所有子目录完整路径组成的列表
    """
    return list(filter(os.path.isdir, map(lambda filename: os.path.join(dirname, filename), os.listdir(dirname))))

def file_list(dirname, ext='.csv'):
    """获取目录下所有特定后缀的文件
    @param dirname: str 目录的完整路径
    @param ext: str 后缀名, 以点号开头
    @return: list(str) 所有子文件名(不包含路径)组成的列表
    """
    return list(filter(lambda filename: os.path.splitext(filename)[1] == ext, os.listdir(dirname)))

def list_file_path(root_dirname, ext='.md'):
    """
    获取文件路径列表
    :param root_dirname:
    :param ext:
    :return:
    """
    file_path_list = []
    dirname_list = subdir_list(root_dirname)
    for dirname in dirname_list:
        filename_list = file_list(dirname=dirname, ext='.md')
        for filename in filename_list:
            file_path_list.append(r'{}\{}'.format(dirname, filename))
    return file_path_list

def replace_file_img_url(file_path):
    """
    替换文件中的图床地址，通过下载图片再上传到七牛云实现
    :param file_path:
    :return:
    """
    # 存储读取的文件内容，用于更新文件，从而实现修改的效果
    file_data = ""
    i = 0
    with open(file_path, "r", encoding="utf-8") as f:
        for line in f:
            # 正则匹配当前行
            # 读内容
            res = re.search(OLD_IMG_URL_PREFIX + r'.+\.(png|jpg|gif|jpeg|webp)', line)
            if res:
                # 提取url
                img_url = line[res.span()[0]: res.span()[1]]

                # 替换成备用的url
                img_url = img_url.replace(OLD_IMG_URL_PREFIX, OLD_IMG_URL_REPLACE_PREFIX)
                print(os.path.dirname(__file__) + os.path.basename(img_url))

                # 通过备用的url下载到本地
                img_path = wget.download(img_url, "temp/" + os.path.basename(img_url))

                # 上传到七牛云
                upload(img_path, img_url[len(OLD_IMG_URL_REPLACE_PREFIX) + 1:])

                # 删除本地图片
                os.remove(img_path)

                # 更新当前行的图床地址
                line = line.replace(OLD_IMG_URL_PREFIX, NEW_IMG_URL_PREFIX)
            file_data += line

    # 写出文件
    with open(file_path, "w", encoding="utf-8") as f:
        f.write(file_data)

if __name__ == '__main__':
    for file_path in list_file_path(ROOT_DIR_PATH):
        print("正在处理：" + file_path)
        replace_file_img_url(file_path)


from validations import get_validated_integer, get_validated_string
import math

def single_loop_print(L):
    """
    prints the list that the user has filled
    prints the list in a better format and only once

    :param L: list
    :return: None
    """
    for i in range(0, len(L)):
        output = "{:10} --- {:^10}".format(L[i][0], L[i][1])
        print(output)

def print_with_indexes(L):
    """
    prints the user's list with indexes

    :param L: list
    :return: None
    """
    for i in range(0, len(L)):
        output = "{:3} : {:10} {:10} ".format(i,L[i][0], L[i][1])
        print(output)

def fill_fruitbowl(L):
    """
    the required list is two-dimensional, and the sub list must be of the form [str, int],
    requests user input for index and quantity,
    adds new fruit and quantity to the list,
    prints confirmation

    :param L: list
    :return: None
    """
    type_of_fruit = get_validated_string("What fruit would you like to add to the bowl?",3,20)
    quantity_of_fruit = get_validated_integer("How many of that fruit would you like to add?",0, math.inf)
    temporary_list = [type_of_fruit, quantity_of_fruit]
    L.append(temporary_list)
    print("." * 60)
    print("You have entered {} {}s in the fruit bowl!".format(quantity_of_fruit, type_of_fruit))
    print("." * 60)

def update_fruitbowl(L):
    """
    the required updated list is two-dimensional, and the sub list must be of the form [str, int],
    prints list of fruit with indexes,
    requests user input for index and new quantity,
    requests user input for either add or subtract function to run
    changes quantity for that fruit
    prints confirmation

    :param L:
    :return: None
    """
    #test that the list has something in it.
    if len(L)==0:
        print("There is nothing in your fruit bowl")
        return None
    print_with_indexes(L)
    my_index = get_validated_integer("Please choose the index number of the fruit you would like to change?",0,len(L)-1)
    add_or_subtract = get_validated_string("Enter 'A' to add, or any other key to subtract fruit: -> ", 1,1).upper()
    if add_or_subtract == "A":
        amount_fruit_added = get_validated_integer("How many {}s do you want to add?".format(L[my_index][0]),1,50)
        L[my_index][1] += amount_fruit_added
        print("." * 60)
        add_confirmation = "You have now added {} {}s to your fruit bowl!".format(amount_fruit_added, L[my_index][0])
        print(add_confirmation)
    else:
        amount_fruit_subtracted = get_validated_integer("How many {}s do you want to take away?".format(L[my_index][0]),1,L[my_index][1])
        L[my_index][1] -= amount_fruit_subtracted
        print("." * 60)
        sub_confirmation = "You have now removed {} {}s from your fruit bowl!".format(amount_fruit_subtracted, L[my_index][0])
        print(sub_confirmation)

def menu():
    """
    prints menu options
    requests user menu choice
    calls different functions depending on which menu option was chosen

    :return: None
    """

    #fruitbowl_list = []
    full_bowl = [
        ["Orange",10],
        ["Pear", 4],
        ["Carrot", 12]
    ]
    fruitbowl_list = full_bowl

    my_menu = [
        ["A", "Add fruit items"],
        ["R", "Review the contents of your fruit bowl"],
        ["C", "Change quantity of a fruit you have in the bowl"],
        ["Q", "Quit"],
    ]

    run = True
    while run == True:
        for i in range(0, len(my_menu)):
            print("{:3} : {}".format(my_menu[i][0], my_menu[i][1]))
        option = get_validated_string("Please choose an option: -> ",1,1).upper()
        print("." * 60)
        if option == "R":
            single_loop_print(fruitbowl_list)
            print("." * 60)
        elif option == "C":
            update_fruitbowl(fruitbowl_list)
            print("." * 60)
        elif option == "A":
            fill_fruitbowl(fruitbowl_list)
        elif option == "Q":
            print("Quitting...")
            run = False
        else:
            print("Invalid Entry")

if __name__ == "__main__":
    menu()


import math
import torch
from torch import nn
from detector.nets.commons import Focus, CBR, SPP, BottleNeckCSP, width_grow, depth_grow, model_scale

def fuse_conv_and_bn(conv, bn):
    # https://tehnokv.com/posts/fusing-batchnorm-and-conv/
    with torch.no_grad():
        # init
        fusedconv = nn.Conv2d(conv.in_channels,
                              conv.out_channels,
                              kernel_size=conv.kernel_size,
                              stride=conv.stride,
                              padding=conv.padding,
                              groups=conv.groups,
                              bias=True).to(conv.weight.device)

        # prepare filters
        w_conv = conv.weight.clone().view(conv.out_channels, -1)
        w_bn = torch.diag(bn.weight.div(torch.sqrt(bn.eps + bn.running_var)))
        fusedconv.weight.copy_(torch.mm(w_bn, w_conv).view(fusedconv.weight.size()))

        # prepare spatial bias
        b_conv = torch.zeros(conv.weight.size(0), device=conv.weight.device) if conv.bias is None else conv.bias
        b_bn = bn.bias - bn.weight.mul(bn.running_mean).div(torch.sqrt(bn.running_var + bn.eps))
        fusedconv.bias.copy_(torch.mm(w_bn, b_conv.reshape(-1, 1)).reshape(-1) + b_bn)

        return fusedconv

default_anchors = [
    [10, 13, 16, 30, 33, 23],
    [30, 61, 62, 45, 59, 119],
    [116, 90, 156, 198, 373, 326],
]
default_strides = [8., 16., 32.]

class YOLOv5Backbone(nn.Module):
    def __init__(self, in_channel=3, depth_multiples=0.33, width_multiples=0.50):
        super(YOLOv5Backbone, self).__init__()
        channel_64 = width_grow(64, width_multiples)
        channel_128 = width_grow(128, width_multiples)
        channel_256 = width_grow(256, width_multiples)
        channel_512 = width_grow(512, width_multiples)
        channel_1024 = width_grow(1024, width_multiples)
        self.out_channels = [channel_256, channel_512, channel_1024]
        self.stem = Focus(in_channel, channel_64, 3)
        self.layer1 = nn.Sequential(
            CBR(channel_64, channel_128, 3, 2),
            BottleNeckCSP(channel_128, channel_128, depth_grow(3, depth_multiples))
        )

        self.layer2 = nn.Sequential(
            CBR(channel_128, channel_256, 3, 2),
            BottleNeckCSP(channel_256, channel_256, depth_grow(9, depth_multiples))
        )

        self.layer3 = nn.Sequential(
            CBR(channel_256, channel_512, 3, 2),
            BottleNeckCSP(channel_512, channel_512, depth_grow(9, depth_multiples))
        )

        self.layer4 = nn.Sequential(
            CBR(channel_512, channel_1024, 3, 2),
            SPP(channel_1024, channel_1024, (5, 9, 13)),
            BottleNeckCSP(channel_1024, channel_1024, depth_grow(3, depth_multiples), shortcut=False)
        )

    def forward(self, x):
        x = self.stem(x)
        x = self.layer1(x)
        c3 = self.layer2(x)
        c4 = self.layer3(c3)
        c5 = self.layer4(c4)
        return [c3, c4, c5]

class YOLOv5Neck(nn.Module):
    def __init__(self, c3, c4, c5, blocks=1):
        super(YOLOv5Neck, self).__init__()
        self.latent_c5 = CBR(c5, c4, 1, 1)
        self.c4_fuse = BottleNeckCSP(c4 * 2, c4, blocks=blocks, shortcut=False)
        self.latent_c4 = CBR(c4, c3)
        self.c3_out = BottleNeckCSP(c3 * 2, c3, blocks=blocks, shortcut=False)
        self.c3_c4 = CBR(c3, c3, 3, 2)
        self.c4_out = BottleNeckCSP(c3 * 2, c4, blocks=blocks, shortcut=False)
        self.c4_c5 = CBR(c4, c4, 3, 2)
        self.c5_out = BottleNeckCSP(c4 * 2, c5, blocks=blocks, shortcut=False)

    def forward(self, xs):
        c3, c4, c5 = xs
        latent_c5 = self.latent_c5(c5)
        f4 = torch.cat([nn.UpsamplingNearest2d(scale_factor=2)(latent_c5), c4], dim=1)
        c4_fuse = self.c4_fuse(f4)
        latent_c4 = self.latent_c4(c4_fuse)
        f3 = torch.cat([nn.UpsamplingNearest2d(scale_factor=2)(latent_c4), c3], dim=1)
        c3_out = self.c3_out(f3)
        c3_c4 = self.c3_c4(c3_out)
        c4_out = self.c4_out(torch.cat([c3_c4, latent_c4], dim=1))
        c4_c5 = self.c4_c5(c4_out)
        c5_out = self.c5_out(torch.cat([c4_c5, latent_c5], dim=1))
        return [c3_out, c4_out, c5_out]

class YOLOv5Head(nn.Module):
    def __init__(self, c3, c4, c5, num_cls=80, strides=None, anchors=None):
        super(YOLOv5Head, self).__init__()
        self.num_cls = num_cls
        self.output_num = num_cls + 5
        if anchors is None:
            anchors = default_anchors
        self.anchors = anchors
        if strides is None:
            strides = default_strides
        self.strides = strides
        assert len(self.anchors) == len(self.strides)
        self.layer_num = len(self.anchors)
        self.anchor_per_grid = len(self.anchors[0]) // 2
        self.grids = [torch.zeros(1)] * self.layer_num
        a = torch.tensor(self.anchors, requires_grad=False).float().view(self.layer_num, -1, 2)
        normalize_anchors = a / torch.tensor(strides, requires_grad=False).float().view(3, 1, 1)
        self.register_buffer("normalize_anchors", normalize_anchors.clone())
        self.register_buffer("anchor_grid", a.clone().view(self.layer_num, 1, -1, 1, 1, 2))
        self.heads = nn.ModuleList(
            nn.Conv2d(x, self.output_num * self.anchor_per_grid, 1) for x in [c3, c4, c5]
        )
        for mi, s in zip(self.heads, strides):  # from
            b = mi.bias.view(self.anchor_per_grid, -1)  # conv.bias(255) to (3,85)
            b[:, 4] += math.log(8. / (640. / s) ** 2)  # obj (8 objects per 640 image)
            b[:, 5:] += math.log(0.6 / (self.num_cls - 0.99))  # cls
            mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True)

    def forward(self, xs):
        z = list()
        assert len(xs) == self.layer_num
        for i in range(self.layer_num):
            xs[i] = self.heads[i](xs[i])
            bs, _, ny, nx = xs[i].shape
            xs[i] = xs[i].view(bs, self.anchor_per_grid, self.output_num, ny, nx).permute(0, 1, 3, 4, 2).contiguous()
            if not self.training:  # inference
                if self.grids[i].shape[2:4] != xs[i].shape[2:4]:
                    self.grids[i] = self._make_grid(nx, ny).to(xs[i].device)
                # grid: bs,anchor_per_grid,ny,nx,2
                # xs[i]:bs,anchor_per_grid,ny,nx,output
                y = xs[i].sigmoid()
                y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grids[i]) * self.strides[i]
                y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i]
                z.append(y.view(bs, -1, self.output_num))
        return (xs, self.normalize_anchors) if self.training else torch.cat(z, 1)

    @staticmethod
    def _make_grid(nx=20, ny=20):
        yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)])
        return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float()

class YOLOv5(nn.Module):
    def __init__(self, in_channel=3,
                 num_cls=80,
                 scale_name="s",
                 strides=None,
                 anchors=None):
        super(YOLOv5, self).__init__()
        depth_multiples, width_multiples = model_scale(scale_name)
        self.backbones = YOLOv5Backbone(in_channel, depth_multiples, width_multiples)
        c3, c4, c5 = self.backbones.out_channels
        self.neck = YOLOv5Neck(c3, c4, c5, blocks=depth_grow(3, depth_multiples))
        self.head = YOLOv5Head(c3, c4, c5, num_cls=num_cls, strides=strides, anchors=anchors)

    def forward(self, x):
        x = self.head(self.neck(self.backbones(x)))
        return x

    def fuse(self):  # fuse model Conv2d() + BatchNorm2d() layers
        print('Fusing layers... ')
        for m in self.modules():
            if type(m) is CBR:
                m._non_persistent_buffers_set = set()  # pytorch 1.6.0 compatability
                m.conv = fuse_conv_and_bn(m.conv, m.bn)  # update conv
                delattr(m, 'bn')  # remove batchnorm
                m.forward = m.fuseforward  # update forward
        return self

if __name__ == '__main__':
    input_tensor = torch.rand(size=(1, 3, 416, 416))
    net = YOLOv5(in_channel=3, scale_name='s')
    out, norm_anchors = net(input_tensor)
    for item in out:
        print(item.shape)
    # print(norm_anchors)
    # print(norm_anchors.requires_grad)
    # with torch.no_grad():
    #     net.eval()
    #     out = net(input_tensor)
    #     print(out.shape)
    #     print(out.requires_grad)
    # with open("yolov5s_cvt.txt", 'w') as wf:
    #     weights = net.state_dict()
    #     for i, name in enumerate(weights):
    #         item = weights[name]
    #         print(i, name, item.shape)
    #         print(name, file=wf)


import pyinotify
import logging
import asyncio
import os
import shlex
import xdg.DesktopEntry
import xdg.BaseDirectory
import xdg.Menu
from pathlib import PosixPath, Path
from typing import Optional, List, Union, Dict, Callable
import qubesadmin
import qubesadmin.vm
import qubesadmin.events

from . import constants

logger = logging.getLogger('qubes-appmenu')

def exec_parse(desktop_entry: xdg.DesktopEntry.DesktopEntry):
    """
    Parse Exec field according to specification and return an already-split
    exec command, ready to be used in subprocess.Popen et al.
    """
    split_str = shlex.split(desktop_entry.getExec())
    result = []
    for s in split_str:
        if s in ['%f', '%F', '%u', '%U', '%d', '%D', '%n', '%N', '%v',
                 '%m', '%k']:
            continue
        if s == '%i' and desktop_entry.getIcon():
            result.extend(['--icon', desktop_entry.getIcon()])
            continue
        if s == '%c':
            result.append(desktop_entry.getName())
            continue
        result.append(s)
    return result

class ApplicationInfo:
    """
    Class representing data within a single .desktop file.
    """
    def __init__(self, qapp, file_path):
        self.qapp: qubesadmin.Qubes = qapp
        self.file_path: PosixPath = file_path
        self.app_icon: Optional[str] = None
        self.vm_icon: Optional[str] = None
        self.app_name: Optional[str] = None
        self.sort_name: Optional[str] = None
        self.vm: Optional[qubesadmin.vm.QubesVM] = None
        self.entry_name: Optional[str] = None
        self.exec: List[str] = []
        self.disposable: bool = False
        self.categories = []
        self.entries: List = []

    def load_data(self, entry):
        """Fill own data with information from xdg.DesktopEntry provided."""
        vm_name = entry.get('X-Qubes-VmName') or None
        try:
            self.vm = self.qapp.domains[vm_name]
        except KeyError:
            self.vm = None

        self.app_name = entry.getName() or ''
        if self.vm:
            self.app_name = self.app_name.split(": ", 1)[-1]
        self.sort_name = str(self.app_name).lower()
        self.vm_icon = self.vm.icon if self.vm else None
        self.app_icon = entry.getIcon()
        self.disposable = bool(entry.get('X-Qubes-NonDispvmExec'))
        self.entry_name = entry.get('X-Qubes-AppName') or self.file_path.name
        if self.disposable:
            self.entry_name = constants.DISPOSABLE_PREFIX + self.entry_name
        self.exec = exec_parse(entry)

        self.categories = entry.getCategories()

        for menu_entry in self.entries:
            menu_entry.update_contents()

    def get_command_for_vm(self, vm=None):
        """Get execution command for a specified VM. We're not using contents
        of an Exec field directly because freshly-minted DispVMs don't have
         their own .desktop files."""
        command = self.exec
        if vm and not self.vm:
            logger.warning('Unexpected command: cannot run local'
                           ' application for a non-local VM: %s', vm)
            return command
        if vm and str(self.vm) != str(vm):
            # replace name of the old VM - used for opening apps from DVM
            # template in their child dispvm
            if len(command) < 6 or command[5] != str(self.vm):
                logger.error(
                    'Unexpected command for a disposable VM: %s', command)
                return []
            return command[:5] + [str(vm)] + command[6:]
        return command

    def is_qubes_specific(self):
        """Check if the current file represents a qubes-generated app."""
        return 'X-Qubes-VM' in self.categories

class DesktopFileManager:
    """
    Class that loads, caches and observes changes in .desktop files.
    """
    desktop_dirs = [
        Path(xdg.BaseDirectory.xdg_data_home) / 'applications',
        Path('/usr/share/applications')]

    # pylint: disable=invalid-name
    class EventProcessor(pyinotify.ProcessEvent):
        """pyinotify helper class"""
        def __init__(self, parent):
            self.parent = parent
            super().__init__()

        def process_IN_CREATE(self, event):
            """On file create, attempt to load it. This can lead to spurious
            warnings due to 0-byte files being loaded, but in some cases
            is necessary to correctly process files."""
            try:
                self.parent.load_file(event.pathname)
            except FileNotFoundError:
                self.parent.remove_file(event.pathname)

        def process_IN_DELETE(self, event):
            """
            On file delete, remove the tile and all its children menu entries
            """
            self.parent.remove_file(event.pathname)

        def process_IN_MODIFY(self, event):
            """On modify, simply attempt to laod the file again."""
            try:
                self.parent.load_file(event.pathname)
            except FileNotFoundError:
                self.parent.remove_file(event.pathname)

        def process_IN_MOVED_FROM(self, event):
            """On move from, act like delete happened."""
            self.process_IN_DELETE(event)

        def process_IN_MOVED_TO(self, event):
            """On move to, act like create happened."""
            self.process_IN_CREATE(event)

    def __init__(self, qapp):
        self.qapp = qapp
        self.watch_manager = None
        self.notifier = None
        self.watches = []
        self._callbacks: List[Callable] = []

        # directories used by Qubes menu tools, not necessarily all possible
        # XDG directories
        self.current_environments = \
            os.environ.get('XDG_CURRENT_DESKTOP', '').split(':')

        self.app_entries: Dict[Path, ApplicationInfo] = {}

        for directory in self.desktop_dirs:
            if not os.path.exists(directory):
                try:
                    os.makedirs(directory, exist_ok=True)
                except OSError:
                    # situation is strange, just ignore this directory
                    continue
            for file in os.listdir(directory):
                self.load_file(directory / file)
        self._initialize_watchers()

    def register_callback(self, func):
        """
        Register callbacks to be executed on newly loaded files.
        Will only be executed on correctly loaded ApplicationInfos. The callback
        should also add created widget (if any) to ApplicationInfo's entries
        field.
        """
        self._callbacks.append(func)
        for info in self.app_entries.values():
            func(info)

    def get_app_infos(self):
        """Get all available ApplicationInfos. Needed for initial loading
        of favorites."""
        for info in self.app_entries.values():
            yield info

    def remove_file(self, path: Union[str, Path]):
        """Remove a file provided by path from local cache. Also removes
        all child menu entries."""
        if isinstance(path, str):
            path = Path(path)
        app_info = self.app_entries.get(path)

        if app_info:
            for child in app_info.entries:
                parent = child.get_parent()
                parent.remove(child)
                parent.invalidate_filter()
            del self.app_entries[path]

    def load_file(self, path: Union[str, Path]):
        """
        Load a file. If file was already known, its ApplicationInfo is
        refreshed, otherwise a new ApplicationInfo object will be created
        and all callbacks registered will be executed."""
        if isinstance(path, str):
            path = Path(path)
            if not path.exists() or path.stat().st_size == 0:
                # event received while file was being deleted or created
                self.remove_file(path)
                return

        if not path.name.endswith('.desktop'):
            return

        try:
            entry = xdg.DesktopEntry.DesktopEntry(path)
        except Exception as ex:  # pylint: disable=broad-except
            logger.warning(
                'Cannot load desktop entry file %s: %s', path, str(ex))
            self.remove_file(path)
            return

        if not self._eligibility_check(entry):
            self.remove_file(path)
            return

        app_info = self.app_entries.get(path, None)
        if not app_info:
            new_entry = True
            app_info = ApplicationInfo(self.qapp, path)
            self.app_entries[path] = app_info
        else:
            new_entry = False
        app_info.load_data(entry)

        if new_entry:
            for func in self._callbacks:
                func(app_info)

    def _eligibility_check(self, entry: xdg.DesktopEntry.DesktopEntry):
        """Check if the loaded entry should be shown in the menu at all,
        based on current environment."""
        if entry.getHidden():
            return False
        if entry.getOnlyShowIn():
            if not set(entry.getOnlyShowIn()).intersection(
                    self.current_environments):
                return False
        if entry.getNotShowIn():
            if set(entry.getNotShowIn()).intersection(
                    self.current_environments):
                return False
        if entry.get('X-AppStream-Ignore'):
            return False
        return True

    def _initialize_watchers(self):
        """
        Initialize all watcher entities.
        """
        self.watch_manager = pyinotify.WatchManager()

        # pylint: disable=no-member
        mask = pyinotify.IN_CREATE | pyinotify.IN_DELETE | \
               pyinotify.IN_MODIFY | pyinotify.IN_MOVED_FROM | \
               pyinotify.IN_MOVED_TO

        loop = asyncio.get_event_loop()

        self.notifier = pyinotify.AsyncioNotifier(
            self.watch_manager, loop,
            default_proc_fun=DesktopFileManager.EventProcessor(self))

        for path in self.desktop_dirs:
            self.watches.append(
                self.watch_manager.add_watch(
                    str(path), mask, rec=True, auto_add=True))


from fastapi import Depends, FastAPI, exceptions
from loguru import logger

from app.routers import media_route, tweet_route, user_route
from app.utils.database import engine
from app.utils.exceptions import (
    logging_dependency,
    my_exception_handler,
    my_validation_exception_handler,
)
from app.utils.logconfig import init_logger

init_logger()
app = FastAPI(debug=True)
app.add_exception_handler(
    exceptions.RequestValidationError, handler=my_validation_exception_handler
)
app.add_exception_handler(exceptions.HTTPException, handler=my_exception_handler)
app.include_router(
    user_route.router,
    prefix="/api",
    tags=["users"],
    dependencies=[Depends(logging_dependency)],
)
app.include_router(
    media_route.router,
    prefix="/api",
    tags=["medias"],
    dependencies=[Depends(logging_dependency)],
)
app.include_router(
    tweet_route.router,
    prefix="/api",
    tags=["tweets"],
    dependencies=[Depends(logging_dependency)],
)

@app.on_event("startup")
async def startup():
    logger.info("Server started")

@app.on_event("shutdown")
async def shutdown():
    await engine.dispose()


class Solution:
    def wordPattern(self, pattern: str, s: str) -> bool:
        r = set()
        record = {}
        strArr = s.split(" ")
        if len(pattern) != len(strArr):
            return False

        for i in range(len(pattern)):
            if pattern[i] not in record:
                if strArr[i] not in r:
                    record[pattern[i]] = strArr[i]
                    r.add(strArr[i])
                else:
                    return False
            else:
                if record[pattern[i]] != strArr[i]:
                    return False

        return True


from telegram.ext import ConversationHandler
from handlers.food_handlers.food_handlers import (
    get_day_ration_handler,
    get_one_ration_handler,
    select_food_handler
)
from handlers.workout_handlers.test_workout_handlers import (
    back_2_main_menu_handler
)
from handlers.general_handlers import (
    encorrect_msg_main_handler
)

from utils.navigation import (
    CHOICE_FOOD_KEY,
    BACK_KEY,
    MAIN_MENU_KEY
)

# Выбор рациона питания
conv_food = ConversationHandler(
    entry_points=[select_food_handler],
    states={
        # ADD_USER_TRAIN: [conv_add_user_train], Вписать все в choice_train_key
        CHOICE_FOOD_KEY: [
            get_day_ration_handler,
            get_one_ration_handler,
            back_2_main_menu_handler,
            encorrect_msg_main_handler
        ],
    },
    fallbacks=[],
    map_to_parent={
        BACK_KEY: MAIN_MENU_KEY,
        MAIN_MENU_KEY: MAIN_MENU_KEY
    },
    name="food_conversation",
    persistent = True,
)


#!/usr/bin/python3

from setuptools import setup, find_packages
# To use a consistent encoding
from codecs import open
from os import path

here = path.abspath(path.dirname(__file__))
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
    long_description = f.read()

setup(
    name='Postcodes',

    # Versions should comply with PEP440.  For a discussion on single-sourcing
    # the version across setup.py and the project code, see
    # https://packaging.python.org/en/latest/single_source_version.html
    version='1.0.0',

    description='library that supports validating and formatting post codes for UK.',
    long_description='Write a library that supports validating and formatting post codes for UK.\
      The details of which post codes are valid and which are the parts they consist of can be found at \
      https://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Formatting.',

    # The project's main homepage.
    url='https://github.com/garyb1/PostCodes',

    # Author details
    author='Gary Byrne',
    author_email='garyb1@live.ie',

    # Choose your license
    license='MIT',
    py_modules=['postcode/postcode'],
    # See https://pypi.python.org/pypi?%3Aaction=list_classifiers
    classifiers=[
        # How mature is this project? Common values are
        #   3 - Alpha
        #   4 - Beta
        #   5 - Production/Stable
        'Development Status :: 3 - Alpha',

        # Indicate who your project is intended for
        'Intended Audience :: Developers',
        'Topic :: Software Development :: Build Tools',

        # Pick your license as you wish (should match "license" above)
        'License :: OSI Approved :: MIT License',

        # Specify the Python versions you support here. In particular, ensure
        # that you indicate whether you support Python 2, Python 3 or both.
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
    ],

    # What does your project relate to?
    keywords='UK Postcodes',
    install_requires=['geopy==1.11.0', 'requests==2.20.0'],
)


import nilearn
from dfsio import readdfs, writedfs
from surfproc import view_patch_vtk, patch_color_labels
import numpy as np
from scipy.spatial import cKDTree

def interpolate_labels(fromsurf=[], tosurf=[]):
    ''' interpolate labels from surface to to surface'''
    tree = cKDTree(fromsurf.vertices)
    d, inds = tree.query(tosurf.vertices, k=1, p=2)
    tosurf.labels = fromsurf.labels[inds]
    return tosurf

hemi = 'right'

outbase = 'data/BCI'
brannetome_base = '/ImagePTE1/ajoshi/code_farm/svreg/USCBrainMulti/Brainnetome/BCI-Brainnetome'
uscbrain_base = '/ImagePTE1/ajoshi/code_farm/svreg/USCBrainMulti/USCBrain'
hemi_surf_uscbrain_file = uscbrain_base + '.'+hemi+'.mid.cortex.dfs'
hemi_surf_brainnetome_file = brannetome_base + '.'+hemi+'.mid.cortex.dfs'
hemi_surf_out_file = outbase + '.'+hemi+'.mid.cortex.dfs'
hemi_surf_smooth_out_file = outbase + '.'+hemi+'.smooth.mid.cortex.dfs'

uscbrain_ids = [[502, 503, 504, 505]]
brainnnetome_ids = [[163, 164, 165, 166,
                     167, 168, 169, 170, 171, 172, 173, 174]]
uscbrain_new_ids = [[563, 564, 565, 566,
                     567, 568, 569, 570, 571, 572, 573, 574]]

uscbrain_hemi = readdfs(hemi_surf_uscbrain_file)
uscbrain_hemi_labs = uscbrain_hemi.labels.copy()
brainnetome_hemi = readdfs(hemi_surf_brainnetome_file)
brainnetome_hemi_vertices = brainnetome_hemi.vertices
brainnetome_hemi.vertices = uscbrain_hemi.vertices
uscbrain_hemi = patch_color_labels(uscbrain_hemi)
brainnetome_hemi = patch_color_labels(brainnetome_hemi)

view_patch_vtk(uscbrain_hemi)
view_patch_vtk(brainnetome_hemi)

class btmlCl:
    pass

btml = btmlCl()

class ubCl:
    pass

ub = ubCl()

for i in range(len(uscbrain_ids)):
    uid = uscbrain_ids[i]
    bid = brainnnetome_ids[i]
    uid_new = uscbrain_new_ids[i]

    u_ind = np.in1d(uscbrain_hemi.labels, uid).nonzero()[0]
    ub.labels = uscbrain_hemi.labels[u_ind]
    ub.vertices = uscbrain_hemi.vertices[u_ind, ]

    b_ind = np.in1d(brainnetome_hemi.labels, bid).nonzero()[0]
    btml.labels = brainnetome_hemi.labels[b_ind]

    btml.vertices = brainnetome_hemi.vertices[b_ind, ]

    print('Replacing', uid)
    print('With', uid_new)

    interpolate_labels(fromsurf=btml, tosurf=ub)

    for ind, r in enumerate(bid):
        ub.labels[ub.labels == r] = uid_new[ind]

    uscbrain_hemi.labels[u_ind] = ub.labels

uscbrain_hemi = patch_color_labels(uscbrain_hemi)
brainnetome_hemi = patch_color_labels(brainnetome_hemi)

view_patch_vtk(uscbrain_hemi)

#writedfs(hemi_surf_out_file, uscbrain_hemi)

uscbrain_hemi.vertices = brainnetome_hemi_vertices

writedfs(hemi_surf_out_file, uscbrain_hemi)

uscbrain_hemi.labels = uscbrain_hemi_labs

writedfs(hemi_surf_smooth_out_file, uscbrain_hemi)


def merge_sort(arr):
    if len(arr) <= 1:
        return
    mid_index = len(arr) // 2

    left_arr = arr[:mid_index]
    right_arr = arr[mid_index:]
    merge_sort(left_arr)
    merge_sort(right_arr)
    l_i, r_i, a_i = 0, 0, 0
    while l_i < len(left_arr) or r_i < len(right_arr):
        if l_i >= len(left_arr):
            arr[a_i] = right_arr[r_i]
            a_i += 1
            r_i += 1
            continue
        if r_i >= len(right_arr):
            arr[a_i] = left_arr[l_i]
            a_i += 1
            l_i += 1
            continue

        if right_arr[r_i] < left_arr[l_i]:
            arr[a_i] = right_arr[r_i]
            a_i += 1
            r_i += 1
        else:
            arr[a_i] = left_arr[l_i]
            a_i += 1
            l_i += 1

if __name__ == "__main__":
    arr = [4, 3, 2, 5, 10, 2]
    merge_sort(arr)
    print(arr)


#!/usr/bin/env python
# encoding: utf-8
import cv2
import numpy as np

"""ease_motion_detector.
This is ease motion detection algorithm.

Example:
        $ python ease_motion_detector.py

Todo:
    * cca function

"""
__author__ = "Kevin Lee"
__affiliate__ = "lidengju.com"
__copyright__ = "Copyright 2018, Kevin"
__license__ = "MIT"
__version__ = "0.1"
__maintainer__ = "Kevin Lee"
__email__ = "kevinleeex@foxmail.com"
__status__ = "Development"

class EaseMotionDetector():
    def onChanged(self, val):
        self.diff_thresh = val

    def __init__(self, diff_thresh=25, move_sense=0.05, show_window=True):
        self.writer = None
        self.font = None
        self.diff_thresh = diff_thresh
        self.move_sense = move_sense
        self.init_flag = True
        self.show_window = show_window
        self.frame = None
        self.capture = cv2.VideoCapture(0)
        self.frame = self.capture.read()[1]
        self.frame_w = self.frame.shape[1]  # frame width
        self.frame_h = self.frame.shape[0]  # frame height
        self.frame_area = self.frame_w * self.frame_h  # frame area
        self.gray_frame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY)
        self.fusion_frame = np.float32(self.frame)
        self.absdiff_frame = None
        self.previous_frame = None

        self.cur_contour = None
        self.cur_area = 0

        # show window with track bar.
        if show_window:
            self.window_name = "Motion Detection Result"
            cv2.namedWindow(self.window_name)
            cv2.createTrackbar("Difference Threshold", self.window_name, self.diff_thresh, 100, self.onChanged)

    def run(self):
        while True:
            cur_frame = self.capture.read()[1]
            self.processFrame(cur_frame)

            if self.hasMoved():
                print("Object moved")
            cv2.imshow("Diff", self.gray_frame)
            cv2.drawContours(cur_frame, self.cur_contour, -1, (0, 0, 255), 2)

            if self.show_window:
                cv2.imshow(self.window_name, cur_frame)
            key = cv2.waitKey(50) % 0x100
            if key == 27:
                break

    def processFrame(self, cur_frame):
        """
        Image processing, blur--> compute diff--> gray_frame--> dilation--> eroding
        :param cur_frame: current frame
        :return: result
        """
        cv2.blur(cur_frame, (3, 3), cur_frame)
        # first time to compute
        if self.init_flag:
            self.init_flag = False
            self.absdiff_frame = cur_frame.copy()
            self.previous_frame = cur_frame.copy()
            # self.average_frame = cv2.convertScaleAbs(cur_frame)
        else:
            cv2.accumulateWeighted(cur_frame, self.fusion_frame, 0.05)  # compute average
        self.previous_frame = cv2.convertScaleAbs(self.fusion_frame)
        self.absdiff_frame = cv2.absdiff(cur_frame, self.previous_frame)  # abs(average - cur_frame)

        self.gray_frame = cv2.cvtColor(self.absdiff_frame, cv2.COLOR_BGR2GRAY)
        cv2.threshold(self.gray_frame, self.diff_thresh, 255, cv2.THRESH_BINARY, self.gray_frame)

        # define kernels
        dilate_kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15, 15))
        erode_kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10, 10))
        cv2.dilate(self.gray_frame, dilate_kernel, self.gray_frame)  # dilation
        result = cv2.erode(self.gray_frame, erode_kernel, self.gray_frame)  # eroding
        return result

    def hasMoved(self):
        """
        Judge the object is moving or not.
        :return: is_moved(boolean)
        """
        out, contours, hierarchy = cv2.findContours(self.gray_frame, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        self.cur_contour = contours
        max_contour = []
        for i in range(len(contours)):
            c = cv2.contourArea(contours[i])
            if c < 0.005 * self.frame_area:
                # fill small white blob in black
                cv2.drawContours(self.gray_frame, contours, i, (0, 0, 0), -1)
            else:
                max_contour.append(contours[i])
            self.cur_area += c
        self.cur_contour = max_contour
        val_area = self.cur_area / self.frame_area  # calculate the ratio of cur_area/frame_area
        self.cur_area = 0

        if val_area > self.move_sense:
            return True
        else:
            return False

    def cca(self, src_img):
        """
        connected component analysis
        :param src_img: source image to analysis
        :return:
        """
        contours = self.cur_contour
        for i in range(len(contours)):
            pass

if __name__ == '__main__':
    detect = EaseMotionDetector(diff_thresh=40)
    detect.run()