import caffe
import cv2
import sys

def deploy(img_path):

    caffe.set_mode_gpu()

    # Initialize the Caffe model using the model trained in DIGITS. Which two files constitute your trained model?
    net = caffe.Classifier('/dli/data/digits/20200325-142619-1525/deploy.prototxt', '/dli/data/digits/20200325-142619-1525/snapshot_iter_5400.caffemodel',
                           channel_swap=(2,1,0),
                           raw_scale=255,
                           image_dims=(256, 256))

    # Create an input that the network expects. This is different for each project, so don't worry about the exact steps, but find the dataset job directory to show you know that whatever preprocessing is done during training must also be done during deployment.
    input_image= caffe.io.load_image(img_path)
    input_image = cv2.resize(input_image, (256,256))
    mean_image = caffe.io.load_image('/dli/data/digits/20200325-141614-588c/mean.jpg')
    input_image = input_image-mean_image

    # Make prediction. What is the function and the input to the function needed to make a prediction?
    prediction = net.predict([input_image])  ##REPLACE WITH THE FUNCTION THAT RETURNS THE OUTPUT OF THE NETWORK##([##REPLACE WITH THE INPUT TO THE FUNCTION##])

    # Create an output that is useful to a user. What is the condition that should return "whale" vs. "not whale"?
    if prediction.argmax()==0: ##REPLACE WITH THE CONDITION THAT WOULD MAKE THE FUNCTION RETURN WHALE##:
        return "whale"
    else:
        return "not whale"

##Ignore this part
if __name__ == '__main__':
    print(deploy(sys.argv[1]))


'''
Created on Oct 4, 2014

@author: sunnycomes
'''

from tornado.web import RequestHandler

from src.common.post_parser import get_all_parsed_posts
from src.common.settings import get_site_info, get_3rd_party_snippets

class IndexHandler(RequestHandler):
    '''
    This handler is used to deal with the requests for index page.
    Visiting url is http://localhost:9999/
    '''
    def get(self):
        posts = get_all_parsed_posts()

        params = get_site_info()

        snippets = get_3rd_party_snippets()

        template_file_name = "index.html"

        self.render(template_file_name, posts = posts, params = params, snippets = snippets)

handler = (r"/", IndexHandler)


import os
import re

from rich import print
from rich.console import Console

MINT: str = "./cadence/transactions/mint-batch.cdc"

# Uncomment some of these to make the script work

# MAINNET
NETWORK: str = "mainnet"
FROM: str = "0x1f56a1e665826a52"
CONTRACT: str = "82b54037a8f180cf"
LOWEST_BLOCK = 19200000
# LOWEST_BLOCK = 19430500

# TESTNET
# NETWORK: str = "testnet"

def main():
    console = Console()

    # Get block height
    command = f"flow blocks get latest --network {NETWORK}"
    block_height = os.popen(command).read()
    block_height = re.search("Height\s*([0-9]*)", block_height)
    block_height = block_height.group(1)
    block_height = int(block_height)

    distributed = []

    with console.status(f"[bold green]Fetching down to flow block {LOWEST_BLOCK}"):
        while block_height > LOWEST_BLOCK:
            scan_blocks = 1000
            command = f"""
                flow events get \
                A.{CONTRACT}.Shard.Withdraw \
                --network {NETWORK} \
                --start {block_height - scan_blocks} \
                --end {block_height}
            """
            events = os.popen(command).read()
            console.log(f"Fetched blocks {block_height - scan_blocks}")

            withdrawals = re.findall("([0-9]*)\s*.*from.*(0x[a-f0-9]*)", events)

            for withdrawal in withdrawals:
                if FROM in withdrawal:
                    distributed.append(withdrawal)

            block_height -= scan_blocks

        console.print("[bold]Total Distibuted Shards:", len(distributed))

if __name__ == "__main__":
    main()


from lantz.core.helpers import UNSET, MISSING

from lantz.core.feat import FeatProxy, DictFeatProxy, Feat, DictFeat
from ..log import LOGGER
from ..utils import LANTZ_BUILDING_DOCS
from ..utils.qt import QtGui

def register_wrapper(cls):
    """Register a class as lantz wrapper for QWidget subclasses.

    The class must contain a field (_WRAPPERS) with a tuple of the
    QWidget subclasses that it wraps.

    Parameters
    ----------

    Returns
    -------

    """
    for wrapped in cls._WRAPPED:
        if wrapped in cls._WRAPPERS:
            LOGGER.warn('{} is already registered to {}.'.format(wrapped, cls._WRAPPERS[wrapped]))

        if LANTZ_BUILDING_DOCS:
            cls._WRAPPERS[wrapped] = type(wrapped.__name__ + 'Wrapped',
                                          (cls, ), {'_IS_LANTZ_WRAPPER': True})
        else:
            cls._WRAPPERS[wrapped] = type(wrapped.__name__ + 'Wrapped',
                                          (cls, wrapped), {'_IS_LANTZ_WRAPPER': True})

    return cls

@register_wrapper
class WidgetMixin(object):
    """Mixin class to provide extra functionality to QWidget derived controls.

    Derived class must override _WRAPPED to indicate with which classes it
    can be mixed.

    To wrap an existing widget object use::

    If you want lantz to provide an appropriate wrapped widget for a given feat::

    In any case, after wrapping a widget you need to bind it to a feat::

    Finally, you need to

    Parameters
    ----------

    Returns
    -------

    >>> widget = QComboBox()
    >>> WidgetMixin.wrap(widget)

    >>> widget = WidgetMixin.from_feat(feat)

    >>> feat = driver.feats[feat_name]
    >>> widget.bind_feat(feat)

    >>> widget.lantz_target = driver
    """

    _WRAPPED = (QtGui.QWidget, )

    #: Dictionary linking Widget types with the function to patch them
    _WRAPPERS = {}

    _update_on_change = False

    def keyPressEvent(self, event):
        """When 'u' is pressed, request new units.
        When 'r' is pressed, get new value from the driver.
        """
        super().keyPressEvent(event)

        if event.text() == 'r':
            # This should also trigger a widget update if necessary.
            self.value_from_feat()

    def value(self):
        """Get widget value."""
        return super().value()

    def _as_update_value(self):
        # TODO: it would be nice to remove this.
        return self.value()

    def setValue(self, value):
        """Set widget value.
        """
        if value is MISSING:
            return
        super().setValue(value)

    def setReadOnly(self, value):
        """Set read only
        """
        super().setReadOnly(value)

    def value_from_feat(self):
        """Update the widget value with the current Feat value of the driver."""
        if self._feat is None or self._lantz_target is None:
            return

        if isinstance(self._feat, DictFeat):
            return getattr(self._lantz_target, self.feat.name)[self._feat_key]
        else:
            return getattr(self._lantz_target, self.feat.name)

    def value_to_feat(self):
        """Update the Feat value of the driver with the widget value."""
        if self._feat is None or self._lantz_target is None:
            return

        if isinstance(self._feat, DictFeat):
            getattr(self._lantz_target, self.feat.name)[self._feat_key] = self.value()
        else:
            setattr(self._lantz_target, self.feat.name, self.value())

    @property
    def update_on_change(self):
        return self._update_on_change

    @update_on_change.setter
    def update_on_change(self, value):
        self._update_on_change = value

    @property
    def readable(self):
        """If the Feat associated with the widget can be read (get)."""
        if self._feat is None:
            return False
        return self._feat.fget not in (None, MISSING)

    @property
    def writable(self):
        """If the Feat associated with the widget can be written (set)."""
        if self._feat is None:
            return False
        return self._feat.fset is not None

    def on_widget_value_changed(self, value, old_value=UNSET, key=MISSING):
        """When the widget is changed by the user, update the driver with
        the new value.
        """
        if key is not MISSING and key != self._feat_key:
            return
        if self._update_on_change:
            self.value_to_feat()

    def on_feat_value_changed(self, value, old_value=UNSET, key=MISSING):
        """When the driver value is changed, update the widget if necessary.
        """
        if key is not MISSING and key != self._feat_key:
            return
        if self.value() != value:
            self.setValue(value)

    @property
    def feat_key(self):
        """Key associated with the DictFeat."""
        return self._feat_key

    @feat_key.setter
    def feat_key(self, value):
        if self._lantz_target:
            getattr(self._lantz_target, self._feat.name + '_changed').disconnect(self.on_feat_value_changed)
        self._feat_key = value
        if self._lantz_target:
            getattr(self._lantz_target, self._feat.name + '_changed').connect(self.on_feat_value_changed)
        self.value_from_feat()

    @property
    def lantz_target(self):
        """Driver connected to the widget."""
        return self._lantz_target

    @lantz_target.setter
    def lantz_target(self, target):
        if self._lantz_target:
            self._feat_signal.disconnect(self.on_feat_value_changed)
            self.valueChanged.disconnect()

        if target is not None:
            self._lantz_target = target

            self._feat_signal = getattr(self._lantz_target, self._feat.name + '_changed')
            self._feat_signal.connect(self.on_feat_value_changed)

            self.value_from_feat()
            self.valueChanged.connect(self.on_widget_value_changed)

    def bind_feat(self, feat):
        if isinstance(feat, (FeatProxy, DictFeatProxy)):
            self._feat = feat.proxied

        elif isinstance(feat, (Feat, DictFeat)):
            self._feat = feat

        else:
            raise ValueError('feat argument must be an instance of Feat, DictFeat, FeatProxy or DictFeatProxy, not %s' % feat.__class__)

        if isinstance(self._feat, DictFeat):
            keys = feat.keys
        else:
            keys = None

        if keys:
            self._feat_key = keys[0]
        else:
            self._feat_key = MISSING

        self.setReadOnly(not self.writable)

    @property
    def feat(self):
        return self._feat

    @classmethod
    def _wrap(cls, widget):
        ChildrenWidgets.patch(widget)
        widget._lantz_target = None
        widget._feat = None
        widget._update_on_change = True

    @classmethod
    def wrap(cls, widget):
        if hasattr(widget, '_lantz_wrapped'):
            return

        if getattr(widget, '_IS_LANTZ_WRAPPER', False):
            widget._wrap(widget)
        else:
            wrapper_class = cls._WRAPPERS.get(type(widget), cls)
            wrapper_class._wrap(widget)
            widget.__class__ = wrapper_class

        widget._lantz_wrapped = True

    @classmethod
    def from_feat(cls, feat, parent=None):
        """Return a widget appropriate to represent a lantz feature.

        Parameters
        ----------
        feat :
            a lantz feature proxy, the result of inst.feats[feat_name].
        parent :
            parent widget. (Default value = None)
        """

        _get = cls._WRAPPERS.get

        if feat.values:
            if isinstance(feat.values, dict):
                tmp = set(feat.values.keys())
            else:
                tmp = set(feat.values)

            if tmp == {True, False}:
                widget = _get(QtGui.QCheckBox)
            else:
                widget = _get(QtGui.QComboBox)
        elif not feat.units is None or feat.limits:
            widget = _get(QtGui.QDoubleSpinBox)
        else:
            widget= _get(QtGui.QLineEdit)

        widget = widget(parent)
        cls.wrap(widget)

        return widget

class ChildrenWidgets(object):
    """Convenience class to iterate children.

    Parameters
    ----------
    parent :
        parent widget.

    """

    def __init__(self, parent):
        self.parent = parent

    def __getattr__(self, item):
        return self.parent.findChild((QtGui.QWidget, ), item)

    def __iter__(self):
        pending = [self.parent, ]
        qualname = {self.parent: self.parent.objectName()}
        while pending:
            obj = pending.pop()
            for child in obj.children():
                if not isinstance(child, QtGui.QWidget):
                    continue
                qualname[child] = qualname[obj] + '.' + child.objectName()
                pending.append(child)
                yield child.objectName(), qualname[child], child

    @classmethod
    def patch(cls, parent):
        if not hasattr(parent, 'widgets'):
            parent.widgets = cls(parent)


import os
import matplotlib
matplotlib.use('Agg') # No pictures displayed
import pylab
import librosa
import librosa.display
import numpy as np

os.chdir(r'G:\000. UCD- Data Science\Research Project\Demo_Songs\spec')

y, sr = librosa.load('pop.00000.au')
librosa.feature.melspectrogram(y=y, sr=sr)
# array([[  2.891e-07,   2.548e-03, ...,   8.116e-09,   5.633e-09],
# [  1.986e-07,   1.162e-02, ...,   9.332e-08,   6.716e-09],
# ...,
# [  3.668e-09,   2.029e-08, ...,   3.208e-09,   2.864e-09],
# [  2.561e-10,   2.096e-09, ...,   7.543e-10,   6.101e-10]])

# Using a pre-computed power spectrogram

D = np.abs(librosa.stft(y))**2
S = librosa.feature.melspectrogram(S=D)

# Passing through arguments to the Mel filters
S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128,
                                    fmax=8000)

#import matplotlib.pyplot as plt
plt.figure(figsize=(10, 4))
librosa.display.specshow(librosa.power_to_db(S,
                                             ref=np.max),
                         y_axis='mel', fmax=8000,
                         x_axis='time')
plt.colorbar(format='%+2.0f dB')
plt.title('POP')
plt.tight_layout()


from django.shortcuts import render
from django.core.mail import send_mail
from .models import opentime
def responsemail(tomail,name):
    send_mail(
            'From Dentrist website  ',
            'Hello, '+name+' Thanks for using oru website. \nOur Doctor will respond soon',
            '8921test@gmail.com',
            [tomail],
            fail_silently=False,)
def response(request):
    if request.method=='POST':
        name=request.POST['name']
        mail=request.POST['mail']
        date=request.POST['date']
        time=request.POST['time']
        send_mail(
            'From Dentrist website  ',
            'Hello, '+name+' Thanks for using oru website. \nYour Appoinment has been confirmed.  You can vist the hospital on the specifed time.',
            '8921test@gmail.com',
            [mail],
            fail_silently=False,)
        send_mail(
            'Appoinment from '+name,
            name+'Has made an appoinment on :'+date+' in :'+time+'\n\n His message was: '+'\n\nHis',
            mail,
            ['sudharsansriram8921@gmail.com'],
            fail_silently=True,)
        print('hi')
        return render(request,'response.html',{'name':name})
    else:
        return render(request,'response.html')

def index(request):
    availablity = opentime.objects.all()
    context = { 'availablity': availablity,}
    return render(request,'index.html',context)

def about(request):
    return render(request,'about.html')

def blog(request):
    return render(request,'blog.html')

def contact(request):
    if request.method=='POST':
        name=request.POST['name']
        mail=request.POST['mail']
        subject=request.POST['subject']
        message=request.POST['message']
        responsemail(mail,name)
        send_mail(
            'message from '+name+' regarding '+subject,
            message+'\n\n his mail: '+mail,
            mail,
            ['sudharsansriram8921@gmail.com'],
            fail_silently=False,)
        return render(request,'contact.html',{'name':name,'mail':mail})
    else:
        return render(request,'contact.html')

def doctors(request):
    return render(request,'doctors.html')

def services(request):
    return render(request,'services.html')

def login(request):
    return render(request,'login.html')


import re

from setuptools import setup, find_packages

with open("readme.md", "r") as fh:
    long_description = fh.read()

_version_regex = (
    r"^__version__ = ('|\")((?:[0-9]+\.)*[0-9]+(?:\.?([a-z]+)(?:\.?[0-9])?)?)\1$"
)

try:
    with open("antispam/__init__.py") as stream:
        match = re.search(_version_regex, stream.read(), re.MULTILINE)
        version = match.group(2)
except FileNotFoundError:
    version = "0.0.0"

def parse_requirements_file(path):
    with open(path) as fp:
        dependencies = (d.strip() for d in fp.read().split("\n") if d.strip())
        return [d for d in dependencies if not d.startswith("#")]

setup(
    name="Discord Anti-Spam",
    version=version,
    author="Skelmis",
    author_email="ethan@koldfusion.xyz",
    description="An easy to use package for anti-spam features in python discord libraries.",
    long_description=long_description,
    long_description_content_type="text/markdown",
    project_urls={
        "Support server": "https://discord.gg/BqPNSH2jPg",
        "Issue tracker": "https://github.com/Skelmis/DPY-Anti-Spam/issues",
        "Documentation": "https://dpy-anti-spam.readthedocs.io/en/latest",
        "Homepage": "https://github.com/Skelmis/DPY-Anti-Spam",
    },
    packages=find_packages(include=("antispam", "antispam.*")),
    install_requires=parse_requirements_file("requirements.txt"),
    extras_requires={
        "dev": parse_requirements_file("dev-requirements.txt"),
        "mongo": ["motor", "dnspython", "pytz"],
        "redis": ["redis", "orjson", "hiredis"],
    },
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
        "Development Status :: 5 - Production/Stable",
    ],
    python_requires=">=3.8",
)


#
# @lc app=leetcode id=1365 lang=python3
#
# [1365] How Many Numbers Are Smaller Than the Current Number
#

# @lc code=start
from collections import defaultdict

class Solution:
    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:

        cnt = {}
        for i, num in enumerate(sorted(nums)):
            if num not in cnt:
                cnt[num] = i

        return (cnt[num] for num in nums)

# @lc code=end


import csv

regionName = []
regionNum = []
regionMenu = []
vMenu = ['서울 : 108', '대전 : 133', '대구 : 143', '부산 : 159', '광주 : 156', '울산 : 152', 'Q : 종료']
temperRegionFileList = ['seoulNullChk.csv', 'daejeonNullChk.csv', 'daeguNullChk.csv',
        'busanNullChk.csv', 'gwangjuNullChk.csv', 'ulsanNullChk.csv']
temperFirstLine = []
mydict = {}
a1 = []
a2 = []
a3 = []
for x in regionMenu:

	#temperRegionDic = dict(x)
	#print(x)
	a = {x}
	#print(a)

with open('./_dataSetGilBut01/temperRegionChk.csv','r', encoding='949') as rData:
	rData = csv.reader(rData)
	mydict = {rows[0]:rows[1] for rows in rData}
	a1.append(mydict)
	for x in a1:
		a2.append(x.values())
		for y in x:
			a3.append(y)
	for z in a2:
		menu = (list(z))

for firstLine in vMenu:
	if firstLine[5:] in mydict.keys():
		regionMenu.append(firstLine[:2]+':'+firstLine[5:])
		regionNum.append(firstLine[5:])
		regionName.append(firstLine[:2])

print(regionMenu)
print()
print(regionNum)
print()
print(regionName)


import sys
import traceback
from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout, QTreeWidget, QTreeWidgetItem, QProxyStyle, \
    QStyleOptionViewItem

class YqStyle(QProxyStyle):
    def drawControl(self, QStyle_ControlElement, QStyleOption, QPainter, widget=None):
        super().drawControl(QStyle_ControlElement, QStyleOption, QPainter, widget)

        # print(QStyle_ControlElement)

        # 这里的画是按照一行一画的
        if isinstance(QStyleOption, QStyleOptionViewItem):
            item = widget.itemFromIndex(QStyleOption.index)
            for i in range(item.columnCount()):
                print(item.text(i))

class MyWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        vbox = QVBoxLayout()
        treewidget = QTreeWidget()
        vbox.addWidget(treewidget)

        self.setLayout(vbox)

        treewidget.setColumnCount(3)

        item1 = QTreeWidgetItem(['1', '2', '3'])
        treewidget.addTopLevelItem(item1)

        item2 = QTreeWidgetItem(['1', '2', '3'])
        item1.addChild(item2)

        treewidget.setStyle(YqStyle())

def main():
    sys.excepthook = traceback.print_exception
    app = QApplication(sys.argv)
    widget = MyWidget()
    widget.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()


from typing import Iterable
from matplotlib import pyplot as plt
import nibabel as nib
import numpy as np
import os
import torchvision.transforms as tt
import torch
import torchvision
from PIL import Image

def is_image_file(filename):
    return any(filename.endswith(extension) for extension in [".nii.gz",'png','tiff','jpg',"bmp"])

def open_target_get_class(target):
        target = np.array(Image.open(target))
        target[target < 254] = 1
        target[target > 253] = 0
        # target= not target
        target = target.max()
        return target

def open_target_get_class_with_perc(target,perc):
        target = np.array(Image.open(target))
        target[target < 254] = 1
        target[target > 253] = 0
        # target= not target
        amount_of_1 = np.unique(target,return_counts=True)[1]
        if len(amount_of_1)<2:
            return 0
        else:
            if (amount_of_1[1] / (target.shape[0] * target.shape[1]* target.shape[2]) ) > perc:
                return 1
            else:
                return 0
def open_image(filename):
    """
    Open an image (*.jpg, *.png, etc).
    Args:
    filename: Name of the image file.
    returns:
    A PIL.Image.Image object representing an image.
    """
    image = Image.open(filename)
    return image
def open_target_np_slides(path):
    im = open_image(path)
    mask= np.array(im)
    li = (np.unique(mask))
    if 29 in li:
        mask[mask==29]=0
    # normal case
    # if len(li)>5:
    if 'fixed' in path:
       # print('found normal slide' + path)
        mask[mask!=255]=0
        mask[mask==255]=2
    #tumour
    else:
       # print('found tumour slide' + path)
        mask = ~mask
        mask[mask!=255]=0

        mask[mask==255]=1
    li = (np.unique(mask,return_counts=True))
    # print(li)
    return mask[:,:,0,np.newaxis]
def open_target_np(path):
    im = open_image(path)
    mask= np.array(im)
    li = (np.unique(mask))
    if 29 in li:
        mask[mask==29]=0
    # normal case
    if len(li)>5:
        mask[mask!=255]=0
        mask[mask==255]=2
    #tumour
    else:
        mask[mask!=255]=0
        mask[mask==255]=1
    return mask[:,:,0,np.newaxis]
def open_target_np_peso(path):
    im = open_image(path)
    mask= np.array(im)
    mask[mask==1]=0
    mask[mask==2]=1
    return mask[:,:,0,np.newaxis]

def open_target_np_glas(path):
    im = open_image(path)
    mask= np.array(im)
    mask[mask!=0]=1
    return mask[:,:,np.newaxis]
def open_image_np(path):
    im = open_image(path)
    array = np.array(im)
    return array

def open_image_np_bw(path):
    im = open_image(path)
    array = np.array(im)
    return array[:,:,np.newaxis].repeat(3,axis=2)
def load_nifti_img(filepath, dtype):
    '''
    NIFTI Image Loader
    :param filepath: path to the input NIFTI image
    :param dtype: dataio type of the nifti numpy array
    :return: return numpy array
    '''
    nim = nib.load(filepath)
    out_nii_array = np.array(nim.get_data(),dtype=dtype)
    out_nii_array = np.squeeze(out_nii_array) # drop singleton dim in case temporal dim exists
    meta = {'affine': nim.get_affine(),
            'dim': nim.header['dim'],
            'pixdim': nim.header['pixdim'],
            'name': os.path.basename(filepath)
            }

    return out_nii_array, meta

def check_exceptions(image, label=None):
    if label is not None:
        if image.shape[:-1] != label.shape[:-1]:
            print('Error: mismatched size, image.shape = {0}, '
                  'label.shape = {1}'.format(image.shape, label.shape))
            #print('Skip {0}, {1}'.format(image_name, label_name))
            raise(Exception('image and label sizes do not match'))

    if image.max() < 1e-6:
        print('Error: blank image, image.max = {0}'.format(image.max()))
        #print('Skip {0} {1}'.format(image_name, label_name))
        raise (Exception('blank image exception'))

    if label.max() < 1e-6:
        print('Error:  label blank, image.max = {0}'.format(label.max()))
        #print('Skip {0} {1}'.format(image_name, label_name))
        raise (Exception('blank label exception'))

class Resize:
    def __init__(self, size,pil=False):
        assert isinstance(size, int) or (isinstance(size, Iterable) and len(size) == 2)
        if isinstance(size, int):
            self._size = (size, size)
        else:
            self._size = size

        self.toPil = tt.ToPILImage() if pil else None
        self.resize = tt.Resize(self._size)

    def __call__(self,x,y=None):

        _input=self.toPil(x) if self.toPil is not None else x
        if x.ndim < 3:
           _input=  _input.convert("L")
        _input = self.resize(_input)
        # _input = skimage.util.img_as_ubyte(_input)
        if y is not None:
            _input_y=self.toPil(y).convert("L")
            _input_y = self.resize(_input_y)
            if x.ndim < 3:

                return np.array(_input)[:,:,np.newaxis].repeat(3,axis=2), np.array(_input_y)[:,:,np.newaxis]
            return np.array(_input), np.array(_input_y)[:,:,np.newaxis]
        else:
            return np.array(_input)
def show_closest_images(train_img_embeds,k=50):
    from sklearn.neighbors import NearestNeighbors
    import numpy as np
    neigh = NearestNeighbors(n_neighbors=k)
    nn = neigh.fit(train_img_embeds[1])
    ind_list=[]
    for i in range(1700,1800):
        print(i)
        _,ind = nn.kneighbors(train_img_embeds[1][i].reshape(1,-1))
        ind_list.extend(*ind)
    topk = np.unique(ind_list,return_counts=True)[0][np.argpartition(np.unique(ind_list,return_counts=True)[1], -4)[-4:]]
    print("topk --> ",topk)
    topk_imgs=torch.cat([train_img_embeds[0][topk]],dim=0)
    img_g = torchvision.utils.make_grid(topk_imgs,nrow=4,normalize=True,range=(-1,1)).permute(1,2,0)
    plt.figure(figsize=(12, 3))
    plt.imshow(img_g)
    plt.axis('off')
def find_similar_images(query_img, query_z, key_embeds, K=8,knn=False,dist_metric='cos'):
        # Find closest K images. We use the euclidean distance here but other like cosine distance can also be used.
        if dist_metric== 'cos':
            dist = torch.cosine_similarity(query_z[None, :], key_embeds[1])
        else:
            dist = torch.cdist(query_z[None, :], key_embeds[1],p=2)
        dist = dist.squeeze(dim=0)
        if knn:
            from sklearn.neighbors import NearestNeighbors
            neigh = NearestNeighbors(n_neighbors=8)
            nn = neigh.fit(key_embeds[1])
            dist, indices = nn.kneighbors(query_z.reshape(1,-1))
            indices = indices.reshape(-1)
        else:
            dist, indices = torch.sort(dist)
        # Plot K closest images
        imgs_to_display = torch.cat(
            [query_img[None], key_embeds[0][indices[:K]]], dim=0)
        grid = torchvision.utils.make_grid(
            imgs_to_display, nrow=K+1, normalize=True, range=(-1, 1))
        grid = grid.permute(1, 2, 0)
        plt.figure(figsize=(12, 3))
        plt.imshow(grid)
        plt.axis('off')
        plt.savefig(f'grid_{indices[1]}_{dist_metric}_knn__{str(knn)}.png')
        # plt.show()


import operator
import pickle
from collections import defaultdict
from multiprocessing import Pool

import numpy as np
from scipy.spatial.distance import euclidean

# from speech_dtw import _dtw

def lol(args):
    i, x_test, train_sequences, cost_mat, test_expected_labels = args
    costs = defaultdict(int)
    for label in train_sequences.keys():
        for x_train in train_sequences[label]:
            path, cost = _dtw.multivariate_dtw(x_test, x_train,
                                               metric='euclidean')
            cost_mat[i, label] += cost
        cost_mat[i, label] /= len(train_sequences[label]) # normalize
    print(i, test_expected_labels[i], cost_mat[i])

def main():

    with open('train_sequences', 'rb') as f:
        train_sequences = pickle.load(f)
    with open('test_sequences', 'rb') as f:
        test_sequences = pickle.load(f)
    with open('test_expected_labels', 'rb') as f:
        test_expected_labels = pickle.load(f)

    for i, x_test in enumerate(test_sequences):
        test_sequences[i] = np.asarray(
            np.array([list(x_test)], dtype=np.double).T, order='c')

    for label in train_sequences.keys():
        for i, x_train in enumerate(train_sequences[label]):
            train_sequences[label][i] = np.asarray(
                np.array([list(x_train)], dtype=np.double).T, order='c')

    label_set = list(train_sequences.keys())

    cost_mat = np.ndarray(shape=(len(test_sequences), len(label_set)))

    pool = Pool()

    mapped = [(i, x_test, train_sequences, cost_mat, test_expected_labels) \
              for i, x_test in enumerate(test_sequences)]

    pool.map(lol, mapped)
    pool.close()

    pool.join()

    with open('cost_mat.dat', 'wb') as f: pickle.dump(cost_mat, f)

def score():

    with open('test_expected_labels', 'rb') as f:
        expected_labels = pickle.load(f)

    with open('cost_mat.dat', 'rb') as f:
        cost_mat = pickle.load(f)

    predicted_labels = np.argmin(cost_mat, axis=1)

    precision = np.mean(predicted_labels == expected_labels)

    return 1 - precision

if __name__ == '__main__':
    print(score())


import re
phone_num_regex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')#putting r in the parenthesis makes the escape chr as raw string
user_input = input("Enter your string : ")
phone_found = phone_num_regex.search(user_input)
if phone_found == None:
    print("sorry there is no phone number inserted")
else:
    print("The phone number found is : ", phone_found.group())

#matching multiple groups with the pipe

hero = re.compile(r'Batman|Tina Fey') #| is called pipe use for to match one of many expression(is like or statement)
mo1 = hero.search('Batman and Tina Fey')
print(mo1.group())

mo2 = hero.search('Tina Fey and Batman')
print(mo2.group())

batRegex = re.compile(r'Bat(man|mobile|copter|bat)')
mo = batRegex.search('Batmobile lost a wheel')
print(mo.group())
print(mo.group(1))

#optional matching with the ?

batRegex = re.compile(r'Bat(wo)?man')
'''? implies Match zero or one of the group preceding
this question mark'''
mo1 = batRegex.search('The Adventure of Batman')
print(mo1.group())
mo2 = batRegex.search('The Adventure of Batwoman')
print(mo2.group())

#matching zero or more with the star

batRegx = re.compile(r'Bat(wo)*man')
mo1 = batRegx.search('The Adventures of the Batwoman')
print(mo1.group())
mo2 = batRegx.search('The Adventure of Batwowowowowoman')
print(mo2.group())

#matching one or more with the plus

batRegx = re.compile(r'Bat(wo)+man')
mo1 = batRegx.search('The Adventure of Batwoman')
print(mo1.group())
mo2 = batRegx.search('The Adventures of Batwowowowoman')
print(mo2.group())
mo3 = batRegx.search('The Adventure of Batman')
print(mo3 == None)

#matching specific repetitions with curly brackets
haRegx = re.compile(r'(Ha){3}')
mo1 = haRegx.search('HaHaHa')
print(mo1.group())
'''if we want to specifiy the boundaries of the repetitions we can use like
{a,b} a->initial and b is final
{,b} -> from 0 to b
{a,} -> from a to infinity
{a} -> exactly match the a times repetitions of a phrase
'''
mo2 = haRegx.search('Ha')
print(mo2==None)

#Greedy and Nongreedy matching

greedyRegex = re.compile(r'(Ha){3,5}')
mo1 = greedyRegex.search('HaHaHaHaHa')
print(mo1.group())

nongreedyRegx = re.compile(r'(Ha){3,5}?')#the quesion mark makes it nongreedy regular expression objex
mo2 = nongreedyRegx.search('HaHaHaHaHa')
print(mo2.group())
'''By default python regular expression is greedy which means that
in ambiguous situations the will match the longest string possible'''

'''The non greedy version match the shortest string possible'''

#findall method

phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
mo = phoneNumRegex.search('Cell: 415-555-9999 Work: 212-555-0000')
print(mo.group())

'''findall() will return a list of strings as long as there are no groups
in the regular expression each string in the list is a piece of the searched
text that matched the regular expression'''

phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d') # has no groups
no_group = phoneNumRegex.findall('Cell: 415-555-9999 Work: 212-555-0000')
print(no_group)

'''if there are groups in the regular expression then findall() will return
a list of tuples each represent a found match and its items are the matched
strings for each group in the regx'''

phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d)-(\d\d\d\d)') # has groups
has_group = phoneNumRegex.findall('Cell: 415-555-9999 Work: 212-555-0000')
print(has_group)

#character classes
'''
\d -> any numeric digit 0-9
\D -> any character that is not a numeric digit from 0-9
\w -> any letter, numeric digit or underscore
\W -> any character that is not a letter, numeric digit or underscore
\s -> any space tab, or newline
\S -> any character that is not a space tab, or newline
'''
xmasRegex = re.compile(r'\d+\s\w+')
xmas = xmasRegex.findall('12 drummers, 11 pipers, 10 lords, 9 ladies, 8 maids, 7 swans, 6 geese, 5 rings, 4 birds, 3 hens, 2 doves, 1 partridge')
print(xmas)

#making your own character classes

'''
 you can define your own character class using square brackets
 You can also include ranges of letters or numbers by using a hyphen
 inside the square brackets, the normal regular expression
symbols are not interpreted as such.
 By placing a caret character ^ just after the character class\'s opening
bracket  you can make a negative character class.
 a negative character class. A negative character class
will match all the characters that are not in the character class.

'''

vowelRegex = re.compile(r'[aeiouAEIOU]')
vowel = vowelRegex.findall('RoboCop eats baby food. BABY FOOD.')
print(vowel)

consonantRegex = re.compile(r'[^aeiouAEIOU]')
consonant = consonantRegex.findall('RoboCop eats baby food. BABY FOOD.')
print(consonant)

#the caret and dollar sign characters
'''
when we use ^ at the start of a regex to indicate that  a match must occur at the beginning
when we use $ at the end f a regex to indicate that  a match must occur at the end
'''

beginsWithHello = re.compile(r'^Hello')
begin = beginsWithHello.search('Hello world!')
print(begin)
print(beginsWithHello.search('He said hello.') == None)
endsWithNumber = re.compile(r'\d$')
end = endsWithNumber.search('Your number is 42')
print(end)
print(endsWithNumber.search('Your number is forty two.') == None)
wholeStringIsNum = re.compile(r'^\d+$') #matches strings that both begin and end with one or more numeric characters
whole = wholeStringIsNum.search('1234567890')
print(whole)
print(wholeStringIsNum.search('12345xyz67890') == None)
print(wholeStringIsNum.search('12 34567890') == None)

#the wildcard character

'''
.(dot)  will match any character except for a newline
the dot character will match just one character
'''
atRegex = re.compile(r'.at')
at = atRegex.findall('The cat in the hat sat on the flat mat.')
print(at)

#matching everything with dot-star

nameRegex = re.compile(r'First Name: (.*) Last Name: (.*)')
mo = nameRegex.search('First Name: Tadele Last Name: Yednkachw')
print(mo.group(1))
print(mo.group(2))

#The dot-star uses greedy mode

nongreedyRegex = re.compile(r'<.*?>')
mo = nongreedyRegex.search('<To serve man> for dinner.>')
print(mo.group())
greedyRegex = re.compile(r'<.*>')
mo = greedyRegex.search('<To serve man> for dinner.>')
print(mo.group())

#matching newlines with the dot character

'''
By passing re.DOTALL asthe second argument to re.compile(),
you can make the dot character match
all characters, including the newline character.
'''

noNewlineRegex = re.compile('.*')
no = noNewlineRegex.search('Serve the public trust.\nProtect the innocent.\nUphold the law.').group()
print(no)
newlineRegex = re.compile('.*', re.DOTALL)
new = newlineRegex.search('Serve the public trust.\nProtect the innocent.\nUphold the law.').group()
print(new)

#case-insensitive matching

'''
to make your regex case-insensitive,
you can pass re.IGNORECASE or re.I as a second argument to re.compile().
'''

robocop = re.compile(r'robocop', re.I)
robo = robocop.search('RoboCop is part man, part machine, all cop.').group()
print(robo)

#substituting strings with the sub() method

'''
The sub() method for Regex objects is passed two arguments.
The first argument is a string to replace any matches.
The second is the string for the regular expression.
The sub() method returns a string with the substitutions applied.
'''

namesRegex = re.compile(r'Agent \w+')
replace = namesRegex.sub('TADE', 'Agent Alice gave the secret documents to Agent Bob.')
print(replace)

agentNamesRegex = re.compile(r'Agent (\w)\w*')
agent_censord = agentNamesRegex.sub(r'\1****', 'Agent Alice told Agent Carol that AgentEve knew Agent Bob was a double agent.')
print(agent_censord)

#managing complex regexes
'''
by passing re.VERBOSE as the second argument to re.compile().
re.compile() function->
    ignore whitespace and comments inside the regular expression string
'''

phoneRegex = re.compile(r'((\d{3}|\(\d{3}\))?(\s|-|\.)?\d{3}(\s|-|\.)\d{4}(\s*(ext|x|ext.)\s*\d{2,5})?)')
#we can make it simple by using verbose mode

phoneRegex = re.compile(r'''(
(\d{3}|\(\d{3}\))? # area code
(\s|-|\.)? # separator
\d{3} # first 3 digits
(\s|-|\.) # separator
\d{4} # last 4 digits
(\s*(ext|x|ext.)\s*\d{2,5})? # extension
)''', re.VERBOSE)

#combining re.IGNORECASE, re.DOTALL, and re.VERBOSE
'''
the re.compile() function takes only a single value as its second argument.
to fix the above limitation ->
use the pipe character (|) on them
'''

someRegexValue = re.compile('foo', re.IGNORECASE | re.DOTALL)
someRegexValue = re.compile('foo', re.IGNORECASE | re.DOTALL | re.VERBOSE)


import json
from urllib.parse import urljoin

from parsers.core.spiders.base import MerchSpider
from parsers.core.tools.parse import parse_comnum
from parsers.items import MerchItem

class XXIVekBySpider(MerchSpider):
    start_urls = ['https://www.21vek.by/motor_oils/synthetic_5w_40']
    search_url = 'https://search.21vek.by/api/v2.0/search/suggest?q={}'

    def parse(self, response, **kwargs):
        yield from self.parse_item_list(response)

    def parse_item_list(self, response):
        item_list = response.xpath('//ul[@class="b-result"]/li')
        for i in item_list:
            price = i.xpath('.//span/@data-price').get()
            old_price = i.xpath(
                '//span[contains(@class, "result__oldprice")]').get()
            yield MerchItem(
                title=i.xpath('.//span[@class="result__name"]/text()').get(),
                url=i.xpath(
                    './/a[contains(@class, "result__link")]/@href').get(),
                price=price,
                old_price=parse_comnum(old_price),
                discount=int((1 - float(price) / float(
                    old_price)) * 100) if old_price else None
            )

    def parse_search(self, response):
        data = json.loads(response.text)['data'][-1]
        for i in data['items']:
            price = (parse_comnum(i['price']) or '').replace(',', '.')
            if price:
                old_price = (parse_comnum(
                    i['price_without_discount']) or '').replace(',', '.')
                yield MerchItem(
                    title=i['name'],
                    url=urljoin('https://21vek.by', i['url']),
                    price=price,
                    old_price=old_price,
                    discount=int((1 - float(price) / float(
                        old_price)) * 100) if old_price else None
                )


import os
import sys
import numpy as np

path = os.getcwd().split("/")
if "newflow" in path:
    path = "/".join(path[:path.index("newflow") + 1])
else:
    path.append("newflow")
    path = "/".join(path)
sys.path.append(path)
from src.loops import Loops
from src.quasi1dsusceptibilities import Susceptibilities
from src.interaction import Interaction
from src.integrable import Integrable

class Quasi1dIntegrableSystem(Integrable):
    def __init__(self):
        self.interaction = None
        self.susceptibilities = None

    def set_all(self, susc_name: str = None, **parameters):
        Np = parameters["Np"]
        self.interaction = Interaction(**parameters)
        if susc_name is None:
            self.susceptibilities = Susceptibilities(Np=Np).append_all()
        else:
            self.susceptibilities = Susceptibilities(Np=Np)
            self.susceptibilities.append_susceptibility_by_name(
                name_string=susc_name
            )
        return self

    def set_interaction(self, g: Interaction):
        if not (isinstance(g, Interaction)):
            raise TypeError(
                f"{g} must be a <class 'Interaction'>"
            )
        self.interaction = g

    def set_susceptibilities(self, susceptibilities: Susceptibilities):
        if not (isinstance(susceptibilities, Susceptibilities)):
            raise TypeError(
                f"{susceptibilities} must be a <class Susceptibilities>."
            )
        self.susceptibilities = susceptibilities

    def initialize(self, **kwargs):
        self.interaction.initialize(**kwargs)
        self.susceptibilities.initialize()

    def initpack(self) -> np.ndarray:
        y = np.concatenate(
            (
                self.interaction.initpack(),
                self.susceptibilities.initpack()
            )
        )
        return y

    def pack(self, *vargs) -> np.ndarray:
        pass

    def unpack(self, y: np.ndarray) -> None:
        self.interaction.unpack(y[:self.interaction.Neq])
        self.susceptibilities.unpack(y[self.interaction.Neq:])

    def rg_equations(self, lflow: float) -> np.ndarray:
        dy = np.concatenate(
            (
                self.interaction.rg_equations(lflow=lflow),
                self.susceptibilities.rg_equations(self.interaction)
            )
        )
        return dy

    # def __iter__(self):
    #     return self

    # def __next(self):
    #     pass

class IntegrableSystem(Integrable):
    def __init__(self):
        self.integrable_systems = list()
        self.size = 0
        self.Neq = 0

    def add_integrable_system(self, *integ_systems: Integrable):
        for integ_sys in integ_systems:
            if not (isinstance(integ_sys, Integrable)):
                raise TypeError(
                    f"{integ_sys} must be a <class 'Integrable'> object."
                )
            else:
                self.integrable_systems.append(integ_sys)
                self.size += 1
                self.Neq += integ_sys.Neq

    def remove_integrable_system(self, *integ_systems: Integrable):
        assert(self.size > 0)
        for integ_sys in integ_systems:
            if integ_sys not in self.integrable_systems:
                raise ValueError(
                    f"{integ_sys} not found"
                )
            self.integrable_systems.remove(integ_sys)
            self.size -= 1
            self.Neq -= integ_sys.Neq

    def remove_all(self):
        self.integrable_systems.clear()
        self.size = 0
        self.Neq = 0

    def initialize(self, **kwargs):
        for integ_sys in self.integrable_systems:
            integ_sys.initialize(**kwargs)

    def initpack(self, *vargs) -> np.ndarray:
        y = self.integrable_systems[0].initpack()
        for i in range(1, self.size):
            y = np.concatenate(
                (y, self.integrable_systems[i].initpack())
            )
        return y

    def pack(self, *vargs) -> np.ndarray:
        pass

    def unpack(self, y: np.ndarray) -> None:
        index_ini = 0
        index_final = 0
        for i in range(self.size):
            index_final += self.integrable_systems[i].Neq
            self.integrable_systems[i].unpack(y[index_ini:index_final])
            index_ini = index_final

    def rg_equations(self, lflow: float) -> np.ndarray:
        dy = self.integrable_systems[0].rg_equations(lflow=lflow)
        for i in range(1, self.size):
            dy = np.concatenate(
                (dy,
                 self.integrable_systems[i].rg_equations(lflow=lflow)
                 )
            )
        return dy

    # def __iter__(self):
    #     return self

    # def __next(self):
    #     pass

if __name__ == "__main__":
    parameters = {
        "tp": 0, "tp2": 0,
        "Ef": 3000, "Np": 2,
        "g1": 0.2, "g2": 0.64, "g3": 0.03,
        "Temperature": 100
    }
    S = Susceptibilities(Np=2).append_all()
    S.delete_susceptibility_by_name('csdw cbdw singhlet triplet')
    g = Interaction(**parameters)

    # d = Quasi1dIntegrableSystem().set_all(**parameters)
    d = Quasi1dIntegrableSystem()
    d.set_interaction(g)
    d.set_susceptibilities(S)
    d.initialize(Temperature=1e-80)
    y = d.rg_equations(lflow=1)
    print(d.interaction.loops.Cooper)
    d.unpack(y)
    for name, _, _ in d.susceptibilities:
        print(name)


import contextlib
import ctypes
import os

from ctypes.wintypes import (
    BOOL,
    CHAR,
    DWORD,
    HANDLE,
    LONG,
    LPWSTR,
    MAX_PATH,
    PDWORD,
    ULONG,
)

from pipenv.vendor.shellingham._core import SHELL_NAMES

INVALID_HANDLE_VALUE = HANDLE(-1).value
ERROR_NO_MORE_FILES = 18
ERROR_INSUFFICIENT_BUFFER = 122
TH32CS_SNAPPROCESS = 2
PROCESS_QUERY_LIMITED_INFORMATION = 0x1000

kernel32 = ctypes.windll.kernel32

def _check_handle(error_val=0):
    def check(ret, func, args):
        if ret == error_val:
            raise ctypes.WinError()
        return ret

    return check

def _check_expected(expected):
    def check(ret, func, args):
        if ret:
            return True
        code = ctypes.GetLastError()
        if code == expected:
            return False
        raise ctypes.WinError(code)

    return check

class ProcessEntry32(ctypes.Structure):
    _fields_ = (
        ("dwSize", DWORD),
        ("cntUsage", DWORD),
        ("th32ProcessID", DWORD),
        ("th32DefaultHeapID", ctypes.POINTER(ULONG)),
        ("th32ModuleID", DWORD),
        ("cntThreads", DWORD),
        ("th32ParentProcessID", DWORD),
        ("pcPriClassBase", LONG),
        ("dwFlags", DWORD),
        ("szExeFile", CHAR * MAX_PATH),
    )

kernel32.CloseHandle.argtypes = [HANDLE]
kernel32.CloseHandle.restype = BOOL

kernel32.CreateToolhelp32Snapshot.argtypes = [DWORD, DWORD]
kernel32.CreateToolhelp32Snapshot.restype = HANDLE
kernel32.CreateToolhelp32Snapshot.errcheck = _check_handle(  # type: ignore
    INVALID_HANDLE_VALUE,
)

kernel32.Process32First.argtypes = [HANDLE, ctypes.POINTER(ProcessEntry32)]
kernel32.Process32First.restype = BOOL
kernel32.Process32First.errcheck = _check_expected(  # type: ignore
    ERROR_NO_MORE_FILES,
)

kernel32.Process32Next.argtypes = [HANDLE, ctypes.POINTER(ProcessEntry32)]
kernel32.Process32Next.restype = BOOL
kernel32.Process32Next.errcheck = _check_expected(  # type: ignore
    ERROR_NO_MORE_FILES,
)

kernel32.GetCurrentProcessId.argtypes = []
kernel32.GetCurrentProcessId.restype = DWORD

kernel32.OpenProcess.argtypes = [DWORD, BOOL, DWORD]
kernel32.OpenProcess.restype = HANDLE
kernel32.OpenProcess.errcheck = _check_handle(  # type: ignore
    INVALID_HANDLE_VALUE,
)

kernel32.QueryFullProcessImageNameW.argtypes = [HANDLE, DWORD, LPWSTR, PDWORD]
kernel32.QueryFullProcessImageNameW.restype = BOOL
kernel32.QueryFullProcessImageNameW.errcheck = _check_expected(  # type: ignore
    ERROR_INSUFFICIENT_BUFFER,
)

@contextlib.contextmanager
def _handle(f, *args, **kwargs):
    handle = f(*args, **kwargs)
    try:
        yield handle
    finally:
        kernel32.CloseHandle(handle)

def _iter_processes():
    f = kernel32.CreateToolhelp32Snapshot
    with _handle(f, TH32CS_SNAPPROCESS, 0) as snap:
        entry = ProcessEntry32()
        entry.dwSize = ctypes.sizeof(entry)
        ret = kernel32.Process32First(snap, entry)
        while ret:
            yield entry
            ret = kernel32.Process32Next(snap, entry)

def _get_full_path(proch):
    size = DWORD(MAX_PATH)
    while True:
        path_buff = ctypes.create_unicode_buffer("", size.value)
        if kernel32.QueryFullProcessImageNameW(proch, 0, path_buff, size):
            return path_buff.value
        size.value *= 2

def get_shell(pid=None, max_depth=10):
    proc_map = {
        proc.th32ProcessID: (proc.th32ParentProcessID, proc.szExeFile)
        for proc in _iter_processes()
    }
    pid = pid or os.getpid()

    for _ in range(0, max_depth + 1):
        try:
            ppid, executable = proc_map[pid]
        except KeyError:  # No such process? Give up.
            break

        # The executable name would be encoded with the current code page if
        # we're in ANSI mode (usually). Try to decode it into str/unicode,
        # replacing invalid characters to be safe (not thoeratically necessary,
        # I think). Note that we need to use 'mbcs' instead of encoding
        # settings from sys because this is from the Windows API, not Python
        # internals (which those settings reflect). (pypa/pipenv#3382)
        if isinstance(executable, bytes):
            executable = executable.decode("mbcs", "replace")

        name = executable.rpartition(".")[0].lower()
        if name not in SHELL_NAMES:
            pid = ppid
            continue

        key = PROCESS_QUERY_LIMITED_INFORMATION
        with _handle(kernel32.OpenProcess, key, 0, pid) as proch:
            return (name, _get_full_path(proch))

    return None


from googletrans import Translator
import random
from random import randrange
import tkinter as t
from tkinter.ttk import *
from tkinter import messagebox
import threading
import pyperclip as p
prog_ver = "1.1.0"
def translations():
    translate_misc_copy_button['state'] = t.DISABLED
    translate_input_submit['state'] = t.DISABLED
    translate_input_textbox.configure(state="disabled")
    translate_output_log_textbox.configure(state="normal")
    translate_output_out_textbox.configure(state="normal")
    translate_output_log_textbox.delete("1.0", "end")
    translate_output_out_textbox.delete("1.0", "end")
    translate_output_log_textbox.configure(state="disabled")
    translate_output_out_textbox.configure(state="disabled")
    print("\nUpdate: User clicked translate button.\nUpdate: Getting trans_amnt from translate_input_amnt_entry...", end="")
    failure = True
    try:
        trans_amnt = int(translate_input_amnt_entry.get())
        print("Done!\nUpdate: trans_amnt ==", trans_amnt)
        failure = False
    except:
        print("Failed!\nError: User either did enter an integer or the program failed to parse the input.\nUpdate: Alerting user to error...", end="")
        t.messagebox.showerror("Error!", "Error: The translation amount must be a number!")
        print("Done!")
    if failure is False and trans_amnt > 1:
        failure = True
        print("\nUpdate: Getting original_text from translate_input_textbox...", end="")
        try:
            original_text = translate_input_textbox.get("1.0", "end")
            print("Done!\nUpdate: original_text ==", original_text)
            failure = False
        except:
            print("Failed!\nError: Unable to parse entered text. User may not have entered any.\nUpdate: Alerting user to error...", end="")
            t.messagebox.showerror("Error!", "Error: Unable to read entered text. Text box may be empty.")
            print("Done!")
        if failure is False:
            print("Update: Begining bad translation...")
            translator = Translator()
            trans_amnt -= 1
            languages = ["af", "sq", "am", "ar", "el", "fr", "ru", "pl", "de", "en"]
            lang_length = len(languages)
            translated_text = ""
            current_translation = 0
            x = 0
            rando = 0
            result = ""
            while x <= trans_amnt:
                rando = randrange(0, lang_length)
                current_translation = x + 1
                if x <= 0:
                    result = translator.translate(original_text, dest=languages[rando])
                else:
                    result = translator.translate(translated_text, dest=languages[rando])
                if current_translation == 1:
                    output_temp = "" + str(x + 1) + ": " + result.text
                else:
                    output_temp = "\n\n" + str(x + 1) + ": " + result.text
                print("Translation #", current_translation, ": ", result.text)
                translate_output_log_textbox.configure(state="normal")
                translate_output_log_textbox.insert("end", output_temp)
                translate_output_log_textbox.see("end")
                translate_output_log_textbox.configure(state="disabled")
                translated_text = result.text
                translate_progress_label.configure(text="Translating... (" + str(current_translation) + "/" + str(trans_amnt + 1) + ")")
                x = x + 1
                progress_amnt = x / trans_amnt
                progress_amnt = progress_amnt * 100
                progress_amnt = int(progress_amnt)
                translate_misc_progress['value'] = (progress_amnt)
            translate_output_log_textbox.configure(state="disabled")
            translate_output_out_textbox.configure(state="normal")
            translated_text = translator.translate(translated_text, dest='en')
            translate_output_out_textbox.insert("end", translated_text.text)
            translate_output_out_textbox.configure(state="disabled")
            print("Update: Translation finished!")
            translate_progress_label.configure(text="Done! (" + str(trans_amnt + 1) + "/" + str(trans_amnt + 1) + ")")
    if failure is False and trans_amnt <= 1:
        print("\nWarning!: User entered number less than or equal to one! Alerting user...", end="")
        t.messagebox.showwarning("Warning!", "The number of translations cannot be one or lower!")
        print("Done!")
    print("\n\nWaiting for user input...")
    translate_input_submit['state'] = t.NORMAL
    translate_input_textbox.configure(state="normal")
    translate_input_amnt_entry.configure(state="normal")
    translate_misc_copy_button['state'] = t.NORMAL
def copy_button():
    print("\nUpdate: User clicked copy button.\nUpdate: Getting text from translate_output_out_textbox...", end="")
    try:
        p.copy(translate_output_out_textbox.get("1.0", "end"))
        print("Done!\nUpdate: Translation copied to user's clipboard")
    except:
        print("Failed!\nError: Unable to parse text in translate_output_out_textbox! Textbox may be empty?\nUpdate: Alerting user to error...", end="")
        t.messagebox.showerror("Error!", "Error: Something when wrong while copying from the output textbox.\nThe textbox may be empty?")
        print("Done!")
    print("\n\nWaiting for user input...")
def exit_button():
    print("\nUpdate: User clicked exit button.\nUpdate: Exiting...", end="")
    window.destroy()
thread = threading.Thread(target=translations)
def start_thread():
    thread = None
    thread = threading.Thread(target=translations)
    try:
        thread.start()
    except:
        thread.join()
def about_button():
    print("\nUpdate: User clicked about button.\nUpdate: Showing program information...")
    mesg = "This program was made by MrHatman26 AKA nobody important.\n\nWhat is this?: This is a simple program that takes some text and translates it multiple times making the text (hopefully) a funny nonsensical mess.\n\nThis program was made using Python and uses the following libraries: -googletrans -random -tkinter -threading -pyperclip\n\nCurrent Version: " + prog_ver
    t.messagebox.showinfo("About", mesg)
    print("Done!\n\nWaiting for user input...")
resolution = "700x600"
title = "MrHatman26's Bad Translator " + prog_ver
print("Update: Creating main window...", end="")
window = t.Tk()
print("Done!\nUpdate: Setting window resolution to " + resolution + "...", end="")
window.geometry(resolution)
print("Done!\nUpdate: Setting window title to " + title + "...", end="")
window.title(title)
print("Done!\nUpdate: Setting icon...", end="")
window.iconbitmap("Icon.ico")
print("Done!\nUpdate: Creating GUI elements...", end="")
#Text entry frame
translate_input_frame = t.Frame(window)
translate_input_label = t.Label(translate_input_frame, text="Enter original text here:").pack(side=t.TOP)
translate_input_textbox = t.Text(translate_input_frame, height=10, width=40)
translate_input_textbox.pack()
translate_input_amnt_label = t.Label(translate_input_frame, text="Enter the amount of times you want to translate the text:").pack()
translate_input_amnt_entry = t.Entry(translate_input_frame, width=40, justify='center')
translate_input_amnt_entry.pack()
translate_input_submit = t.Button(translate_input_frame, text="Translate", width=45, command=start_thread)
translate_input_submit.pack(side=t.BOTTOM, pady=6)#To Do: Give the button a function!
translate_input_frame.pack()
#Translation log and output frame
translate_output_frame = t.Frame(window)
translate_output_log_label = t.Label(translate_output_frame, text="Log").grid(column=0, row=0)
translate_output_out_label = t.Label(translate_output_frame, text="Output").grid(column=1, row=0)
translate_output_log_textbox = t.Text(translate_output_frame, height=10, width=40)
translate_output_log_textbox.grid(column=0, row=1, padx=10)
translate_output_out_textbox = t.Text(translate_output_frame, height=10, width=40)
translate_output_out_textbox.grid(column=1, row=1, padx=10)
translate_output_frame.pack()
translate_output_log_textbox.configure(state="disabled")
translate_output_out_textbox.configure(state="disabled")
#Progress frame
translate_progress_frame = t.Frame(window)
translate_progress_label = t.Label(translate_progress_frame, text="Waiting for input...")
translate_progress_label.pack(side=t.TOP, pady=8)
translate_misc_progress = Progressbar(translate_progress_frame, orient='horizontal', length=675, mode='determinate')
translate_misc_progress.pack(side=t.BOTTOM)
translate_progress_frame.pack()
#Misc frame
translate_misc_frame = t.Frame(window)
translate_misc_copy_button = t.Button(translate_misc_frame, text="Copy", width=45, command=copy_button)
translate_misc_copy_button.pack(pady=4, side=t.TOP)
translate_misc_about_button = t.Button(translate_misc_frame, text="About", width=45, command=about_button).pack()
translate_misc_exit_button = t.Button(translate_misc_frame, text="Exit", width=45, command=exit_button).pack(side=t.BOTTOM, pady=4)
translate_misc_frame.pack()
translate_misc_copy_button['state'] = t.DISABLED
log_scrollbar = t.Scrollbar(window, command=translate_output_log_textbox.yview, width=15)
log_scrollbar.place(x=325, y=284, height=164)
translate_output_log_textbox['yscrollcommand'] = log_scrollbar.set
window.resizable(False, False)
print("Done!\n\nWaiting for input...")
window.mainloop()
print("Done!")


from typing import List
import collections

class Solution:
    def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
        ans = self.subarraysWithMaxKDist(A,K)-self.subarraysWithMaxKDist(A,K-1)
        return ans

    def subarraysWithMaxKDist(self, A: List[int], K: int) -> int:
        counter = collections.Counter()
        dist = 0
        left=right=0
        res=0
        while right<len(A):
            if counter[A[right]]==0:
                dist+=1
            counter[A[right]]+=1
            while dist>K:
                counter[A[left]]-=1
                if counter[A[left]]==0:
                    dist-=1
                left+=1
            res += right-left+1
            right+=1
        return res

sol = Solution()
A=[2,2,1,2,2,2,1,1]
K=2
print(sol.subarraysWithKDistinct(A,K))


import sys
sys.stdin = open('sample_input.txt', 'r')

# 종이 붙이기 점화식
def paper(N):
    if N == 10:
        return 1
    if N ==20:
        return 3
    else:
        return paper(N-10) + (2 * paper(N-20))

T = int(input())

for tc in range(1, T+1):
    n = int(input())

    ans = paper(n)

    print('#{} {}'.format(tc, ans))


from datetime import datetime
import logging
from typing import List
from typing import Union

from flask import abort
from flask import flash
from flask import has_app_context
from flask import redirect
from flask import request
from flask import url_for
from flask_admin.actions import action
from flask_admin.base import expose
from flask_admin.contrib.sqla.fields import QuerySelectMultipleField
from flask_admin.contrib.sqla.filters import FilterEqual
from flask_admin.form import SecureForm
from flask_admin.helpers import get_form_data
from flask_admin.helpers import get_redirect_target
from flask_admin.helpers import is_form_submitted
from flask_login import current_user
from markupsafe import Markup
from markupsafe import escape
from sqlalchemy import func
from sqlalchemy.orm import query
from werkzeug import Response
import wtforms
from wtforms.fields.core import BooleanField
from wtforms.fields.simple import HiddenField
from wtforms.form import Form
from wtforms.validators import InputRequired
from wtforms.validators import ValidationError
import yaml

from pcapi import settings
from pcapi.admin.base_configuration import BaseAdminView
from pcapi.connectors.api_entreprises import get_offerer_legal_category
from pcapi.core import search
from pcapi.core.bookings.api import cancel_bookings_from_rejected_offer
from pcapi.core.categories import categories
from pcapi.core.categories import subcategories
from pcapi.core.offerers.models import Offerer
from pcapi.core.offerers.models import Venue
from pcapi.core.offers import api as offers_api
from pcapi.core.offers.api import import_offer_validation_config
from pcapi.core.offers.models import OfferValidationConfig
from pcapi.core.offers.models import OfferValidationStatus
from pcapi.core.offers.offer_validation import compute_offer_validation_score
from pcapi.core.offers.offer_validation import parse_offer_validation_config
import pcapi.core.offers.repository as offers_repository
from pcapi.core.offers.validation import check_user_can_load_config
from pcapi.domain.admin_emails import send_offer_validation_notification_to_administration
from pcapi.domain.user_emails import send_offer_validation_status_update_email
from pcapi.models import Offer
from pcapi.models import db
from pcapi.models.criterion import Criterion
from pcapi.models.offer_criterion import OfferCriterion
from pcapi.repository import repository
from pcapi.settings import IS_PROD
from pcapi.utils.human_ids import humanize
from pcapi.workers.push_notification_job import send_cancel_booking_notification

logger = logging.getLogger(__name__)

def offer_category_formatter(view, context, model, name) -> str:
    if model.subcategoryId is None:
        return ""
    return subcategories.ALL_SUBCATEGORIES_DICT[model.subcategoryId].category_id

class ExtraDataFilterEqual(FilterEqual):
    def get_column(self, alias: str) -> str:
        return super().get_column(alias).astext

class SubcategoryFilterEqual(FilterEqual):
    def __init__(self, *args, **kwargs):
        options = [(subcategory.id, subcategory.id) for subcategory in subcategories.ALL_SUBCATEGORIES]
        super().__init__(*args, **kwargs, options=options)

class CategoryFilterEqual(FilterEqual):
    def __init__(self, *args, **kwargs):
        options = [(category.id, category.id) for category in categories.ALL_CATEGORIES]
        super().__init__(*args, **kwargs, options=options)

    def apply(self, filter_query, value, alias=None):
        searched_subcategories = [
            subcategory.id
            for subcategory in subcategories.ALL_SUBCATEGORIES
            if subcategory.category_id.upper() == value.upper()
        ]
        return filter_query.filter(self.get_column(alias).in_(searched_subcategories))

class OfferChangeForm(Form):
    ids = HiddenField()
    tags = QuerySelectMultipleField(
        get_label="name",
        query_factory=lambda: Criterion.query.all(),  # pylint: disable=unnecessary-lambda
        allow_blank=True,
    )
    remove_other_tags = BooleanField(
        label="Supprimer tous les autres tags",
    )

class OfferView(BaseAdminView):
    list_template = "admin/bulk_edit_components/custom_list_with_modal.html"
    can_create = False
    can_edit = True
    can_delete = False
    can_export = True
    column_list = [
        "id",
        "name",
        "categoryId",
        "subcategoryId",
        "criteria",
        "rankingWeight",
        "validation",
        "lastValidationDate",
        "isEducational",
    ]
    column_sortable_list = ["name", "criteria", "rankingWeight", "validation", "lastValidationDate"]
    column_labels = {
        "name": "Nom",
        "subcategoryId": "Sous-catégorie",
        "categoryId": "Catégorie",
        "criteria": "Tag",
        "criteria.name": "Tag",
        "rankingWeight": "Pondération",
        "lastValidationDate": "Dernière date de validation",
        "isEducational": "Offre EAC",
    }
    # Do not add searchable column on offer view for performance reasons
    # use the filters feature instead
    column_filters = [
        "id",
        "name",
        CategoryFilterEqual(column=Offer.subcategoryId, name="Catégorie"),
        SubcategoryFilterEqual(column=Offer.subcategoryId, name="Sous-catégories"),
        "criteria.name",
        "rankingWeight",
        "validation",
        "lastValidationDate",
        "isEducational",
        ExtraDataFilterEqual(column=Offer.extraData["isbn"], name="ISBN"),
        ExtraDataFilterEqual(column=Offer.extraData["visa"], name="Visa d'exploitation"),
        ExtraDataFilterEqual(column=Offer.extraData["theater"]["allocine_movie_id"], name="Identifiant Allociné"),
    ]
    form_columns = ["criteria", "rankingWeight"]
    simple_list_pager = True

    @action("bulk_edit", "Édition multiple")
    def action_bulk_edit(self, ids):
        url = get_redirect_target() or self.get_url(".index_view")
        return redirect(url, code=307)

    @expose("/", methods=["POST"])
    def index(self):
        if request.method != "POST":
            return self.index_view()

        url = get_redirect_target() or self.get_url(".index_view")
        ids = request.form.getlist("rowid")
        joined_ids = ",".join(ids)
        change_form = OfferChangeForm()
        change_form.ids.data = joined_ids

        criteria_in_common = (
            db.session.query(Criterion)
            .join(OfferCriterion)
            .filter(OfferCriterion.offerId.in_(ids))
            .group_by(Criterion.id)
            .having(func.count(OfferCriterion.criterion) == len(ids))
            .all()
        )
        change_form.tags.data = criteria_in_common

        self._template_args["url"] = url
        self._template_args["change_form"] = change_form
        self._template_args["change_modal"] = True
        self._template_args["update_view"] = "offer.update_view"
        self._template_args["number_of_edited_items"] = f"Éditer des Offres - {len(ids)} offre(s) sélectionnée(s)"
        return self.index_view()

    @expose("/update/", methods=["POST"])
    def update_view(self):
        url = get_redirect_target() or self.get_url(".index_view")
        if request.method != "POST":
            return redirect(url)
        change_form = OfferChangeForm(request.form)
        if change_form.validate():
            offer_ids: List[str] = change_form.ids.data.split(",")
            criteria: List[OfferCriterion] = change_form.data["tags"]
            remove_other_tags = change_form.data["remove_other_tags"]

            if remove_other_tags:
                OfferCriterion.query.filter(OfferCriterion.offerId.in_(offer_ids)).delete(synchronize_session=False)

            offer_criteria: List[OfferCriterion] = []
            for criterion in criteria:
                offer_criteria.extend(
                    OfferCriterion(offerId=offer_id, criterionId=criterion.id)
                    for offer_id in offer_ids
                    if OfferCriterion.query.filter(
                        OfferCriterion.offerId == offer_id, OfferCriterion.criterionId == criterion.id
                    ).first()
                    is None
                )

            db.session.bulk_save_objects(offer_criteria)
            db.session.commit()

            # synchronize with external apis that generate playlists based on tags
            search.async_index_offer_ids(offer_ids)
            return redirect(url)

        # Form didn't validate
        flash("Le formulaire est invalide: %s" % (change_form.errors), "error")
        return redirect(url, code=307)

    def get_edit_form(self) -> wtforms.Form:
        form = super().get_form()
        form.name = wtforms.StringField(
            "Nom de l'offre",
            render_kw={"readonly": True},
        )
        if has_app_context() and self.check_super_admins():
            form.validation = wtforms.SelectField(
                "Validation",
                choices=[(s.name, s.value) for s in OfferValidationStatus],
                coerce=str,
                description="Vous pouvez choisir uniquement APPROVED ou REJECTED",
            )
        return form

    @property
    def column_formatters(self):
        formatters = super().column_formatters
        formatters.update(
            {
                "categoryId": offer_category_formatter,
            }
        )
        return formatters

    def on_form_prefill(self, form, id):  # pylint:disable=redefined-builtin
        if hasattr(form, "validation"):
            current_offer = self.session.query(self.model).get(id)
            form.validation.data = current_offer.validation.value

    def on_model_change(self, form: wtforms.Form, model: Offer, is_created: bool) -> None:
        if hasattr(form, "validation"):
            previous_validation = form._fields["validation"].object_data
            new_validation = OfferValidationStatus[form.validation.data]
            if previous_validation != new_validation and new_validation in (
                OfferValidationStatus.DRAFT,
                OfferValidationStatus.PENDING,
            ):
                raise ValidationError("Le statut de validation ne peut pas être changé vers DRAFT ou PENDING")

    def after_model_change(self, form: wtforms.Form, offer: Offer, is_created: bool = False) -> None:
        if hasattr(form, "validation"):
            previous_validation = form._fields["validation"].object_data
            new_validation = offer.validation
            if previous_validation != new_validation:
                offer.lastValidationDate = datetime.utcnow()
                if new_validation == OfferValidationStatus.APPROVED:
                    offer.isActive = True
                if new_validation == OfferValidationStatus.REJECTED:
                    offer.isActive = False
                    cancelled_bookings = cancel_bookings_from_rejected_offer(offer)
                    if cancelled_bookings:
                        send_cancel_booking_notification.delay([booking.id for booking in cancelled_bookings])

                repository.save(offer)

                recipients = (
                    [offer.venue.bookingEmail]
                    if offer.venue.bookingEmail
                    else [recipient.user.email for recipient in offer.venue.managingOfferer.UserOfferers]
                )
                send_offer_validation_status_update_email(offer, new_validation, recipients)
                send_offer_validation_notification_to_administration(new_validation, offer)

                flash("Le statut de l'offre a bien été modifié", "success")

        search.async_index_offer_ids([offer.id])

    def get_query(self) -> query:
        return self.session.query(self.model).filter(Offer.validation != OfferValidationStatus.DRAFT).from_self()

class OfferForVenueSubview(OfferView):
    column_searchable_list = ["name", "criteria.name"]
    list_template = "admin/venue_offers_list.html"

    @expose("/", methods=(["GET", "POST"]))
    def index(self):
        if request.method == "POST":
            return super().index()
        self._template_args["venue_name"] = self._get_venue_name()
        return super().index_view()

    def is_visible(self) -> bool:
        return False

    def get_query(self) -> query:
        return self._extend_query(super().get_query())

    def get_count_query(self) -> query:
        return self._extend_query(super().get_count_query())

    def _extend_query(self, query_to_override: query) -> query:
        venue_id = request.args.get("id")

        if venue_id is None:
            abort(400, "Venue id required")

        return query_to_override.filter(Offer.venueId == venue_id)

    def _get_venue_name(self) -> str:
        venue_id = request.args.get("id")

        if venue_id is None:
            abort(400, "Venue id required")

        venue = Venue.query.filter(Venue.id == venue_id).one()
        if not venue:
            abort(404, "Ce lieu n'existe pas ou plus")

        return venue.name

def _pro_offer_url(offer_id: int) -> str:
    return f"{settings.PRO_URL}/offres/{humanize(offer_id)}/edition"

def _metabase_offer_url(offer_id: int) -> str:
    return f"https://support.internal-passculture.app/question/115?offer_id={offer_id}"

def _venue_url(venue: Venue) -> str:
    offerer_id = venue.managingOfferer.id
    venue_id = venue.id
    if venue.isVirtual:
        return f"{settings.PRO_URL}/accueil?structure={humanize(offerer_id)}"
    return f"{settings.PRO_URL}/structures/{humanize(offerer_id)}/lieux/{humanize(venue_id)}"

def _offerer_url(offerer_id: int) -> str:
    return f"{settings.PRO_URL}/accueil?structure={humanize(offerer_id)}"

def _pro_offer_link(view, context, model, name) -> Markup:
    url = _pro_offer_url(model.id)
    return Markup('Offre PC').format(escape(url))

def _related_offers_link(view, context, model, name) -> Markup:
    url = url_for("offer_for_venue.index", id=model.venue.id)
    return Markup('Offres associées').format(escape(url))

def _metabase_offer_link(view, context, model, name) -> Markup:
    url = _metabase_offer_url(model.id)
    return Markup('Offre').format(escape(url))

def _offerer_link(view, context, model, name) -> Markup:
    url = _offerer_url(model.venue.managingOffererId)
    link = Markup('{name}')
    return link.format(url=escape(url), name=escape(model.venue.managingOfferer.name))

def _venue_link(view, context, model, name) -> Markup:
    url = _venue_url(model.venue)
    link = Markup('{name}')
    return link.format(url=escape(url), name=escape(model.venue.publicName or model.venue.name))

def _compute_score(view, context, model, name) -> float:
    current_config = offers_repository.get_current_offer_validation_config()
    validation_items = parse_offer_validation_config(model, current_config)[1]
    return compute_offer_validation_score(validation_items)

class OfferValidationForm(SecureForm):
    validation = wtforms.SelectField(
        "validation",
        choices=[(choice.name, choice.value) for choice in OfferValidationStatus if choice.name != "DRAFT"],
        coerce=str,
    )

class ValidationView(BaseAdminView):
    can_create = False
    can_edit = True
    can_delete = False
    list_template = "admin/offer_validation_list.html"

    column_list = [
        "id",
        "name",
        "validation",
        "venue",
        "offerer",
        "score",
        "offer",
        "offers",
        "dateCreated",
        "isEducational",
    ]
    if IS_PROD:
        column_list.append("metabase")
    column_sortable_list = ["id", "name", "validation", "dateCreated"]
    column_labels = {
        "name": "Nom",
        "validation": "Validation",
        "venue": "Lieu",
        "offerer": "Structure",
        "offer": "Offre",
        "offers": "Offres",
        "score": "Score",
        "metabase": "Metabase",
        "dateCreated": "Date de création",
        "isEducational": "Offre EAC",
    }
    column_filters = ["name", "venue.name", "id", "dateCreated"]
    column_default_sort = ("id", True)
    page_size = 100

    def is_accessible(self):
        return super().is_accessible() and self.check_super_admins()

    @property
    def column_formatters(self):
        formatters = super().column_formatters
        formatters.update(score=_compute_score)
        formatters.update(offer=_pro_offer_link)
        formatters.update(offers=_related_offers_link)
        formatters.update(metabase=_metabase_offer_link)
        formatters.update(offerer=_offerer_link)
        formatters.update(venue=_venue_link)
        return formatters

    def get_query(self):
        return (
            self.session.query(Offer)
            .join(Venue)
            .join(Offerer)
            .filter(Offerer.validationToken.is_(None))
            .filter(Offer.validation == OfferValidationStatus.PENDING)
        )

    def get_count_query(self):
        return (
            self.session.query(func.count(Offer.id))
            .join(Venue)
            .join(Offerer)
            .filter(Offerer.validationToken.is_(None))
            .filter(Offer.validation == OfferValidationStatus.PENDING)
        )

    def _batch_validate(self, offers, validation_status):
        count = 0
        not_updated_offers = []
        for offer in offers:
            try:
                is_offer_updated = offers_api.update_pending_offer_validation(offer, validation_status)
                if is_offer_updated:
                    count += 1
                    offer.lastValidationDate = datetime.utcnow()
                    recipients = (
                        [offer.venue.bookingEmail]
                        if offer.venue.bookingEmail
                        else [recipient.user.email for recipient in offer.venue.managingOfferer.UserOfferers]
                    )
                    send_offer_validation_status_update_email(offer, validation_status, recipients)
                    send_offer_validation_notification_to_administration(validation_status, offer)
                else:
                    not_updated_offers += offer
            except Exception as exc:  # pylint: disable=broad-except
                logger.exception(
                    "Une erreur s'est produite lors de la mise à jour du statut de validation: %s",
                    exc,
                    extra={"offer": offer},
                )
        flash("%d offres ont été modifiées avec succès en %s" % (count, validation_status))
        if not_updated_offers:
            flash(
                "Une erreur s'est produite lors de la mise à jour du statut de validation des offres: %s"
                % not_updated_offers,
                "error",
            )

    @action("approve", "Approuver", "Etes-vous sûr(e) de vouloir approuver les offres sélectionnées ?")
    def action_approve(self, ids):
        offers_to_approve = Offer.query.filter(Offer.id.in_(ids))
        self._batch_validate(offers_to_approve, OfferValidationStatus.APPROVED)

    @action("reject", "Rejeter", "Etes-vous sûr(e) de vouloir rejeter les offres sélectionnées ?")
    def action_reject(self, ids):
        offers_to_reject = Offer.query.filter(Offer.id.in_(ids))
        self._batch_validate(offers_to_reject, OfferValidationStatus.REJECTED)

    @expose("/edit/", methods=["GET", "POST"])
    def edit(self) -> Response:
        offer_id = request.args["id"]
        offer = Offer.query.get(offer_id)
        form = OfferValidationForm()
        if request.method == "POST":
            form = OfferValidationForm(request.form)
            if form.validate():
                validation_status = OfferValidationStatus[form.validation.data]
                is_offer_updated = offers_api.update_pending_offer_validation(
                    offer, OfferValidationStatus[form.validation.data]
                )
                if is_offer_updated:
                    flash("Le statut de l'offre a bien été modifié", "success")
                    offer.lastValidationDate = datetime.utcnow()
                    recipients = (
                        [offer.venue.bookingEmail]
                        if offer.venue.bookingEmail
                        else [recipient.user.email for recipient in offer.venue.managingOfferer.UserOfferers]
                    )

                    send_offer_validation_status_update_email(offer, validation_status, recipients)
                    send_offer_validation_notification_to_administration(validation_status, offer)
                    if request.form["action"] == "save-and-go-next":
                        next_offer_query = (
                            Offer.query.filter(Offer.validation == OfferValidationStatus.PENDING)
                            .filter(Offer.id < offer_id)
                            .order_by(Offer.id.desc())
                            .limit(1)
                        )
                        if next_offer_query.count() > 0:
                            next_offer = next_offer_query.one()
                            return redirect(url_for(".edit", id=next_offer.id))
                        return redirect(url_for("/validation.index_view"))
                    if request.form["action"] == "save":
                        return redirect(url_for("/validation.index_view"))
                else:
                    flash("Une erreur s'est produite lors de la mise à jour du statut de validation", "error")

        form.validation.default = offer.validation.value
        form.process()
        legal_category = get_offerer_legal_category(offer.venue.managingOfferer)
        legal_category_code = legal_category["legal_category_code"] or "Ce lieu n'a pas de code de catégorie juridique"
        legal_category_label = (
            legal_category["legal_category_label"] or "Ce lieu n'a pas de libellé de catégorie juridique"
        )
        current_config = offers_repository.get_current_offer_validation_config()
        validation_items = parse_offer_validation_config(offer, current_config)[1]
        context = {
            "form": form,
            "cancel_link_url": url_for("/validation.index_view"),
            "legal_category_code": legal_category_code,
            "legal_category_label": legal_category_label,
            "pc_offer_url": _pro_offer_url(offer.id),
            "metabase_offer_url": _metabase_offer_url(offer.id) if IS_PROD else None,
            "offer_name": offer.name,
            "offer_score": compute_offer_validation_score(validation_items),
            "venue_name": offer.venue.publicName or offer.venue.name,
            "offerer_name": offer.venue.managingOfferer.name,
            "venue_url": _venue_url(offer.venue),
            "offerer_url": _offerer_url(offer.venue.managingOfferer.id),
        }
        return self.render("admin/edit_offer_validation.html", **context)

def yaml_formatter(view, context, model, name) -> Markup:
    value = getattr(model, name)
    yaml_value = yaml.dump(value, indent=4)
    return Markup("
{}
".format(yaml_value))

def user_formatter(view, context, model, name) -> str:
    author = getattr(model, name)
    return author.email if author else ""

def date_formatter(view, context, model, name) -> datetime:
    config_date = getattr(model, name)
    return config_date.strftime("%Y-%m-%d %H:%M:%S")

class OfferValidationConfigForm(SecureForm):
    specs = wtforms.TextAreaField("Configuration", [InputRequired()])

class ImportConfigValidationOfferView(BaseAdminView):
    can_create = True
    can_edit = False
    can_view_details = True
    can_delete = False
    column_list = ["id", "dateCreated", "userId", "user"]
    column_sortable_list = ["id", "dateCreated"]
    column_labels = {
        "id": "Id",
        "dateCreated": "Date de création",
        "userId": "ID Utilisateur",
        "user": "Utilisateur",
    }

    column_default_sort = ("dateCreated", True)

    column_formatters = {
        "specs": yaml_formatter,
        "user": user_formatter,
        "dateCreated": date_formatter,
    }

    form_excluded_columns = ("id", "dateCreated", "userId", "user")
    form_widget_args = {"specs": {"rows": 40, "style": "color: black"}}

    def create_form(self, obj=None):
        if not is_form_submitted():
            current_config = offers_repository.get_current_offer_validation_config()
            if current_config:
                form = OfferValidationConfigForm()
                form.specs.data = yaml.dump(current_config.specs, indent=4, allow_unicode=True)
                return form

        return OfferValidationConfigForm(get_form_data())

    def create_model(self, form: wtforms.Form) -> Union[None, OfferValidationConfig]:
        check_user_can_load_config(current_user)
        config = import_offer_validation_config(form.specs.data, current_user)
        return config


from configparser import ConfigParser
import service
import logging
config = ConfigParser()
config.read('SETTING.ini')

def uploadJson(cursor, fileDirectory: str = config['SNOWCONNECTOR']['FILEDIRECTORY']):
    cursor.execute(
        """
        create temporary stage json_temp_int_stage
        file_format = json_format;
        """
    )

    cursor.execute("""
    put file://%s/*.json @json_temp_int_stage auto_compress=true;
    """%(fileDirectory))

    cursor.execute("""
        copy into raw_table
        from  @json_temp_int_stage
        file_format = (TYPE = json)
        on_error = 'skip_file';
        """)

    return "Upload Complete"

if __name__ == '__main__':
    cursor = service.engine.connect()
    uploadJson(cursor)


import urllib2
import re
for x in xrange(1,15):
    url = 'http://saltstarters.org/browse/?page=%s' % x
    f = urllib2.urlopen(url)
    d = f.read()
    for u in re.findall(r'\>([^\<\>]+)\<\/a\>',d):
        if u.find('/') > -1 :
            print(u)


from cairosvg import svg2png
import sys
sys.path.append("/usr/local/lib/python2.7/dist-packages")
import os
import svgwrite
import numpy as np
from utils import *
from PIL import Image, ImageOps
from tempfile import TemporaryFile
#from scipy import misc
#from onv_process import onv_convert_fromarr, show_onv

def save_strokes(data, factor=0.2, padding=10, svg_filename = '/tmp/sketch_rnn/svg/sample.svg'):
  """little function that transfer the data(strokes) to .svg"""
  #tf.gfile.MakeDirs(os.path.dirname(svg_filename))
  min_x, max_x, min_y, max_y = get_bounds(data, factor)
  #dims = (50 + max_x - min_x, 50 + max_y - min_y)
  #print (max_x, min_x, max_y, min_y)
  #return
  diff_x = max_x - min_x
  diff_y = max_y - min_y
  size = max(diff_x, diff_y) + padding/factor
  dims = (size, size)
  padding_x = size - diff_x
  padding_y = size - diff_y
  #print (dims, diff_x, diff_y, padding_x, padding_y)
  dwg = svgwrite.Drawing(svg_filename, size=dims)
  dwg.add(dwg.rect(insert=(0, 0), size=dims,fill='white'))
  lift_pen = 1
  abs_x = padding_x/2 - min_x
  abs_y = padding_y/2 - min_y
  p = "M%s,%s " % (abs_x, abs_y)
  command = "m"
  for i in range(len(data)):
    if (lift_pen == 1):
      command = "m"
    elif (command != "l"):
      command = "l"
    else:
      command = ""
    x = float(data[i,0])/factor
    y = float(data[i,1])/factor
    lift_pen = data[i, 2]
    p += command+str(x)+","+str(y)+" "
  the_color = "black"
  stroke_width = 30
  dwg.add(dwg.path(p).stroke(the_color,stroke_width).fill("none"))
  dwg.save()
  #display(SVG(dwg.tostring()))

def resize_strokes(strokes, size):
  """return resized strokes"""
  new_strokes = []
  for data in strokes:
  	data = resize_single_stroke(data, size)
  	new_strokes.append(data)
  new_strokes =  np.array(new_strokes)
  print (new_strokes.shape)
  return new_strokes

def resize_single_stroke(data, size):
  """helper function for resize_single_stroke()"""
  min_x, max_x, min_y, max_y = get_bounds(data, 1)
  #print ("before", min_x, max_x, min_y, max_y)
  width = max_x - min_x
  height = max_y - min_y
  center_x = width / 2.0 + min_x
  center_y = height / 2.0 + min_y
  #print ("width:", width, "height", height, "center_x", center_x, "center_y", center_y)
  data[0,0] = data[0,0] - center_x
  data[0,1] = data[0,1] - center_y

  min_x, max_x, min_y, max_y = get_bounds(data, 1)
  #print ("middle", min_x, max_x, min_y, max_y)
  factor = float(size) / max(width, height)
  #print ("factor", factor)
  data[:,0] = data[:,0] * factor
  data[:,1] = data[:,1] * factor

  min_x, max_x, min_y, max_y = get_bounds(data, 1)
  #print ("after", min_x, max_x, min_y, max_y)
  return data

def convert_with_cairosvg(svg_filename, png_filename):
  """helper function to convert a svg file  to a png file"""
  '''if os.path.exists(png_filename):
  	return'''
  svg2png(open(svg_filename, 'rb').read(), write_to=open(png_filename, 'wb'))

def strokes_to_npy(strokes, is_grey=True, size=64):
	"""convert strokes to svg files and then to png files and then resize to size*size and return  numpy array"""
	npy = []
	print (type(strokes), strokes.shape)

	for data in strokes:
		#print (type(data), data.shape)
		svg_filename = "tmp.svg"
		png_filename = "tmp.png"
		save_strokes(data, factor=0.5, padding=10, svg_filename=svg_filename)
		convert_with_cairosvg(svg_filename, png_filename)
		im = Image.open(png_filename)
		im = im.resize((size,size), Image.ANTIALIAS)
		im2arr = np.array(im)
		if (is_grey):
			im2arr = im2arr[:,:, 1]
		npy.append(im2arr)

	npy = np.array(npy)
	print (npy.shape)
	return npy

def strokes_to_png(strokes, classname, batchname, png_filepath, size=600, padding=125, save_onv=False):
	"""convert strokes to svg files and then to png files and then resize to size*size with padding arround
	    save the png files and return the numpy array"""
	""" set save_ong to True to save the onv files"""

	npy=[]
	index = 0
	for data in strokes:
		svg_filename = "tmp.svg"
		save_strokes(data, factor=0.5, padding=10, svg_filename=svg_filename)
		dirname = os.path.join(png_filepath, classname, batchname)
		if not os.path.exists(dirname):
			os.makedirs(dirname)
		png_filename = os.path.join(dirname, str(index) + ".png")
		convert_with_cairosvg(svg_filename, png_filename)
		im = Image.open(png_filename)
		im = im.resize((size-2*padding, size-2*padding), Image.ANTIALIAS)
		padding_arr = (padding, padding, padding, padding)
		new_im = ImageOps.expand(im, padding_arr, fill=(255, 255, 255))
		new_im.save(png_filename)
		index += 1
		#array to onv
		if (save_onv):
			im2arr = np.array(new_im)#error
			#misc.imshow(im2arr)
			onv = onv_convert_fromarr(im2arr)#eerror
			show_onv(onv)
			npy.append(onv)

	npy = np.array(npy)
	print (npy.shape)
	return npy

### Data preprocessing on original sketch files to obtain conrresponding png/onv/numpy image
def main1():
	data_filepath = '../data/'
	input_dirname = "sketch"
	output_dirname = "testmain1" # change
	is_grey = True
	for f in os.listdir(os.path.join(data_filepath, input_dirname)):
		print(f)
		if ("full" in f):
			continue
		classname = f.split('.')[0]
		outfile = os.path.join(data_filepath, output_dirname, f)
		if os.path.exists(outfile):
			continue
		if not os.path.exists(os.path.dirname(outfile)):
			os.makedirs(os.path.dirname(outfile))

		# load data
		fname = os.path.join(data_filepath, input_dirname, f)
		data = np.load(fname, encoding="bytes")
		train_strokes = data['train']
		valid_strokes = data['valid']
		test_strokes = data['test']

		### For(1)(2)(3) only one can work at a time

		# (1)resize the strokes and save to outfile
		'''resized_train_strokes = resize_strokes(train_strokes, size = 600)
		resized_valid_strokes = resize_strokes(valid_strokes, size = 600)
		resized_test_strokes = resize_strokes(test_strokes, size = 600)
		np.savez(outfile, train=resized_train_strokes, valid=resized_valid_strokes,test=resized_test_strokes)'''

		# (2)save the strokes into .png (and .onv optionally), intended for sketch-onv2seq
		png_filepath = os.path.join(data_filepath, "testpng")
		train_onv = strokes_to_png(train_strokes, classname, 'train', png_filepath=png_filepath, save_onv=False)
		valid_onv = strokes_to_png(valid_strokes, classname, 'valid', png_filepath=png_filepath, save_onv=False)
		test_onv = strokes_to_png(test_strokes, classname, 'test', png_filepath=png_filepath, save_onv=False)
		if save_onv:
			np.savez(outfile, train=train_onv, valid = valid_onv, test = test_onv)

		# (3)save the strokes into numpy array of images, intended for sketch-pix2seq
		'''train_images = strokes_to_npy(train_strokes, is_grey)
		valid_images = strokes_to_npy(valid_strokes, is_grey)
		test_images = strokes_to_npy(test_strokes, is_grey)
		np.savez(outfile, train=train_images, valid=valid_images,test=test_images)'''

### convert .svg to .png from svg_filepath/dirname to png_filepath/dirname
def main2():
	svg_filepath = '../display_svg'
	png_filepath = '../display_image'
	svg_dirlist = ['original']
	for dirname in svg_dirlist:
		for f in os.listdir(os.path.join(svg_filepath, dirname)):
			if not 'svg' in f:
				continue
			svg_filename = os.path.join(svg_filepath, dirname, f)
			png_filename = os.path.join(png_filepath, dirname, f.replace('svg','png'))
			print ("svg_filename", svg_filename, "png_filename", png_filename)
			if (not os.path.exists(os.path.dirname(png_filename))):
				os.makedirs(os.path.dirname(png_filename))
			convert_with_cairosvg(svg_filename=svg_filename, png_filename=png_filename)
			im = Image.open(png_filename)
			im.save(png_filename)

def gallery(array, ncols=3):
	print (array.shape, array[0].shape)
	nindex, height, width, intensity = array.shape
	nrows = nindex//ncols
	assert nindex == nrows*ncols
	# want result.shape = (height*nrows, width*ncols, intensity)
	result = (array.reshape(nrows, ncols, height, width, intensity)
              .swapaxes(1,2)
              .reshape(height*nrows, width*ncols, intensity))
	return result

###place multiple images to a grid for display
def main3():
	source_path = '../display_image'
	dirlist = ['original', 'onv_left_resize', 'rnn_encoder_5classes_0.2','cnn_encoder_5classes_0.2',
	'dnn_encoder_5classes_binocular_0.2','dnn_encoder_5classes_pretrainedrnn_binocular_0.2','dnn_encoder_5classes_pretrainedcnn_binocular_0.2']
	'''idx_list = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95,
	2500, 2505, 2510, 2515, 2520, 2525, 2530, 2535, 2540, 2545, 2550, 2555, 2560, 2565, 2570, 2575, 2580, 2585, 2590, 2595,
	5000, 5005, 5010, 5015, 5020, 5025, 5030, 5035, 5040, 5045, 5050, 5055, 5060, 5065, 5070, 5075, 5080, 5085, 5090, 5095,
	7500, 7505, 7510, 7515, 7520, 7525, 7530, 7535, 7540, 7545, 7550, 7555, 7560, 7565, 7570, 7575, 7580, 7585, 7590, 7595,
	10000, 10005, 10010, 10015, 10020, 10025, 10030, 10035, 10040, 10045, 10050, 10055, 10060, 10065, 10070, 10075, 10080, 10085, 10090, 10095]
	'''
	''' '''
	idx_list = [0, 25, 45, 50,  #75,
	2525, 2530, 2535,   2560, #2555,
	7515, 7540,7575, 7580, # 7570,
	10005, 10010,  10045,  10060]#, 10090
	img_list = []#None

	for dirname in dirlist:
		for idx in idx_list:
			png_filename = os.path.join(source_path, dirname, str(idx)+'.png')
			img = Image.open(png_filename)
			img = img.resize((100,100), Image.ANTIALIAS)
			img_arr = np.array(img)
			img_list.append(img_arr[:,:,0:3])

	img_list = np.array(img_list)
	result = gallery(img_list, len(idx_list))
	display_img = Image.fromarray(result)
	display_img.show()
	display_img.save("../display_20_4.png")

if __name__ == "__main__":
	main1()
	#main2()
	#main3()