import sys
from PyQt5 import QtWidgets, QtGui
from main_ui import Ui_AppMainWindow
from PyQt5 import QtCore as qtc
import nse_driver
import time

class Worker(qtc.QThread):
    fetch_completed = qtc.pyqtSignal(dict)
    status = qtc.pyqtSignal(bool)

    def __init__(self, parent=None):
        super(Worker, self).__init__(parent)
        self.thread_active = True

    @qtc.pyqtSlot(int)
    def call_api(self, interval):
        self.status.emit(self.thread_active)
        try:
            while self.thread_active:
                self.fetch_completed.emit(nse_driver.start_fetch())
                time.sleep(interval)
        except OverflowError:
            print("Time Interval Too Large")

    def stop(self):
        self.thread_active = False
        self.status.emit(self.thread_active)
        self.wait()

class MainDriver(QtWidgets.QMainWindow):
    fetch_requested_signal = qtc.pyqtSignal(int)

    def __init__(self):
        super().__init__()
        self.stock_data = nse_driver.start_fetch()
        self.ui = Ui_AppMainWindow()
        self.setWindowIcon(QtGui.QIcon("./icon.ico"))
        self.ui.setupUi(self)
        self.ui.start_button.released.connect(self.start_thread)
        self.ui.stop_button.clicked.connect(self.stop_thread)
        self.ui.stop_button.setEnabled(False)
        self.show()

    def start_thread(self):
        self.worker = Worker()
        self.worker_thread = qtc.QThread(self)
        self.worker.fetch_completed.connect(self.set_table)
        self.worker.fetch_completed.connect(self.set_stats)
        self.worker.status.connect(self.set_status)
        self.fetch_requested_signal.connect(self.worker.call_api)
        self.worker.moveToThread(self.worker_thread)
        self.worker_thread.start()
        self.start_fetch()

    def start_fetch(self):
        interval = self.get_interval()
        if interval is None:
            self.ui.status_bar.showMessage("Default Time Interval Selected: 30s", 5000)
            self.fetch_requested_signal.emit(30)
        else:
            self.fetch_requested_signal.emit(interval)
            self.ui.status_bar.showMessage(
                "Time Interval Selected: {}s".format(str(interval)), 5000
            )
        self.ui.start_button.setEnabled(False)
        self.ui.stop_button.setEnabled(True)
        print("Thread Started")

    def stop_thread(self):
        self.worker.stop()
        self.ui.start_button.setEnabled(True)
        self.ui.stop_button.setEnabled(False)
        print("Thread Killed")

    def get_interval(self):
        options = ("1", "15", "30", "60")
        item, submitted = QtWidgets.QInputDialog.getItem(
            self, "Set Time Interval", "Seconds:", options, 2, False,
        )
        if submitted and item:
            return int(item)

    def set_status(self, status):
        if status:
            self.ui.connection.setText("Active")
            self.ui.connection.setStyleSheet("color: green")
        else:
            self.ui.connection.setText("Inactive")
            self.ui.connection.setStyleSheet("color: red")

    def set_table(self, stock_data):
        self.ui.price_table.setRowCount(0)
        self.ui.price_table.setColumnCount(8)
        for data_list in stock_data["priceDict"]:
            row = self.ui.price_table.rowCount()
            self.ui.price_table.setRowCount(row + 1)
            self.add_table_row(row, data_list)

    def add_table_row(self, row, data_list):
        col = 0
        for item in data_list:
            cell = QtWidgets.QTableWidgetItem(item)
            self.ui.price_table.setItem(row, col, cell)
            col += 1

    def set_stats(self, stock_data):
        self.ui.total_num.setText(str(self.stock_data["stats"]["total"]))
        self.ui.profit_num.setText(str(self.stock_data["stats"]["profit"]))
        self.ui.profit_num.setStyleSheet("color: green")
        self.ui.loss_num.setText(str(self.stock_data["stats"]["loss"]))
        self.ui.loss_num.setStyleSheet("color: red")
        self.ui.no_change_num.setText(str(self.stock_data["stats"]["noChg"]))
        self.ui.no_change_num.setStyleSheet("color: grey")

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    w = MainDriver()
    w.show()
    sys.exit(app.exec_())


#gets the first testcase from each testgroup with under 1k weights, replace filepath accordingly

testgroups = ['n00050', 'n00100', 'n00200', 'n00500', 'n01000']
Rtestgroup = 'R01000'
groupnames = ['00Uncorrelated','01WeaklyCorrelated','02StronglyCorrelated','03InverseStronglyCorrelated','04AlmostStronglyCorrelated','05SubsetSum','06UncorrelatedWithSimilarWeights','07SpannerUncorrelated','08SpannerWeaklyCorrelated','09SpannerStronglyCorrelated','10MultipleStronglyCorrelated','11ProfitCeiling','12Circle']

import shutil
for groupname in groupnames:
    for testgroup in testgroups:
        filepath = "d:/OR/" + groupname + '/' + testgroup + '/' + Rtestgroup + '/s000.kp'
        newfilepath = "d:/OR/" + groupname + '/' + testgroup+".kp"
        shutil.move(filepath, newfilepath)

Solving knapsack by OR_GA/OR/testmaker.py


"""
radiality:radiality.creation

The `radiality/creation.py` is a part of `radiality`.
Apache 2.0 licensed.
"""

from itertools import zip_longest
from functools import wraps
import json

import asyncio
from websockets.exceptions import ConnectionClosed

from radiality import watch
from radiality import circuit

def event(method):
    """
    Decorator for the definition of an `event`
    """
    # Constructs the `event` specification template
    n = method.__code__.co_argcount - 1
    keys = method.__code__.co_varnames[n:0:-1]
    defaults = (method.__defaults__ or ())[::-1]
    spec_tmpl = list(zip_longest(keys, defaults))[::-1]
    spec_tmpl.append(('*event', method.__name__))

    method = asyncio.coroutine(method)

    @wraps(method)
    def _wrapper(self, *args, **kwargs):
        spec = {
            key: (value if arg is None else arg)
            for ((key, value), arg) in zip_longest(spec_tmpl, args)
        }
        spec.update(kwargs)

        yield from method(self, *args, **kwargs)
        yield from self._actualize(spec)

    _wrapper._is_event = True

    return asyncio.coroutine(_wrapper)

class Eventer(watch.Loggable, circuit.Connectable):
    """
    Emitter of the specific events
    """
    _effectors = None

    def __init__(self, logger, connector):
        """
        Initialization
        """
        self._logger = logger
        self._connector = connector

        self._effectors = {}

    # overridden from `circuit.Connectable`
    @asyncio.coroutine
    def connect(self, sid, freq):
        if sid in self._effectors:  # => reconnect
            yield from self.disconnect(sid, channel=None)

        channel = yield from super().connect(sid, freq)
        if channel:  # => register a new effector
            self.register_effector(sid, channel)
            yield from self.effector_connected(sid, freq)

        return channel

    # overridden from `circuit.Connectable`
    @asyncio.coroutine
    def disconnect(self, sid, channel):
        channel = self._effectors.pop(sid, channel)

        yield from super().disconnect(sid, channel)
        yield from self.effector_disconnected(sid)

    def register_effector(self, sid, channel):
        self._effectors[sid] = channel

    @asyncio.coroutine
    def effector_connected(self, sid, freq):
        spec = {
            '*signal': ('biconnected' if sid in self.wanted else 'connected'),
            'sid': self.sid,
            'freq': self.freq
        }

        try:
            spec = json.dumps(spec)
            yield from self._effectors[sid].send(spec)
        except ValueError:
            self.fail(
                'Invalid output -- '
                'could not encode the signal specification: %s', str(spec)
            )
        except ConnectionClosed:
            self.fail('Connection closed')

    @asyncio.coroutine
    def effector_disconnected(self, sid):
        pass

    @asyncio.coroutine
    def _transmit(self, channel, spec):
        try:
            yield from channel.send(spec)
        except ConnectionClosed:
            self.fail('Connection closed')
        else:
            return True

        return False

    @asyncio.coroutine
    def _actualize(self, spec):
        try:
            spec = json.dumps(spec)
        except ValueError:
            self.fail(
                'Invalid output -- '
                'could not encode the event specification: %s', str(spec)
            )
        else:
            for (sid, channel) in list(self._effectors.items()):
                try:
                    yield from channel.send(spec)
                except ConnectionClosed:
                    self.warn('Connection to `%s` closed', sid)
                    # Clears the wasted effector
                    self._effectors.pop(sid)
                    yield from self.effector_disconnected(sid)


# encoding: utf-8
"""
@project = javbus_crawler
@file = downloadtest
@author = ThinkPad
@create_time = 2019-10-2921:55
"""
import requests
import re
from bs4 import BeautifulSoup

url = 'https://www.dmmsee.icu/EIKI-087'
homeres = requests.get(url)
soup = BeautifulSoup(homeres.text)

paramsElems = soup.find_all('script')[8]
text = paramsElems.text
gid = text[12:24]
reqUrl = 'https://www.dmmsee.icu/ajax/uncledatoolsbyajax.php'
user_agent = 'Mozilla / 5.0 (Windows NT 10.0; Win64; x64) AppleWebKit / 537.36(KHTML, ' \
             'like Gecko) Chrome / 64.0.3282.140 Safari / 537.36 Edge / 18.17763 '
header = {'User-agent': user_agent, 'referer': url}

payload = {'gid': gid, 'uc': 0}

tableRes = requests.get(reqUrl, params=payload, headers=header, timeout=20)
tableRes.raise_for_status()

magnetSoup = BeautifulSoup(tableRes.text, 'html.parser')
magnetElems = magnetSoup.select('a[style="color:#333"]')
m = 0
pattern = r'\r|\n| |\t'
# links = []

while m < len(magnetElems):
    magnetLink = magnetElems[m].get('href')
    movieFile = re.sub(pattern, '', magnetElems[m].text)
    m += 1
    movieSize = re.sub(pattern, '', magnetElems[m].text)
    m += 1
    movieDate = re.sub(pattern, '', magnetElems[m].text)
    print(movieDate)
    if movieDate == '0000-00-00':
        movieDate = '1900-01-01'
    m += 1
    print(m, 'movieFile:{} movieSize{} movieDate{}\n'.format(movieFile, movieSize, movieDate))


# IMPORTANT: This script must be executed from the project root.
# Run> python resources/gen-docs.py

import os
from subprocess import call

currDir = os.getcwd();
libSourceDir = os.path.join(currDir,'library')
testSourceDir = os.path.join(currDir,'tests')
interSourceDir = os.path.join(currDir, 'interface')
resourcesSourceDir = os.path.join(currDir, 'resources')

libOutputDir = os.path.join(currDir,'documentation','library')
testOutputDir = os.path.join(currDir,'documentation','tests')
interOutputDir = os.path.join(currDir,'documentation','interface')
resourcesOutputDir = os.path.join(currDir,'documentation','resources')

def generateRobotFrameworkDocumentation(sourceDir, outputDir, rfModule):
  for root, dirs, files in os.walk(sourceDir):
    robotFiles = filterRobotFiles(files)
    if robotFiles:
      callDocGenerationModule(root, robotFiles, outputDir, rfModule)

def filterRobotFiles(files):
    robotOnly = list(filter(lambda x: x.endswith('.robot'), files))
    return robotOnly

def callDocGenerationModule(dir, files, outputDir, rfModule):
  for file in files:
    testDocName = file[:-5] + 'html'
    sourcePath = os.path.join(dir,file)
    outputPath = os.path.join(outputDir ,testDocName)
    call(['python', '-m', rfModule, sourcePath, outputPath])

def main():
  generateRobotFrameworkDocumentation(libSourceDir, libOutputDir, 'robot.libdoc')
  generateRobotFrameworkDocumentation(testSourceDir, testOutputDir, 'robot.testdoc')
  generateRobotFrameworkDocumentation(interSourceDir, interOutputDir, 'robot.libdoc')
  generateRobotFrameworkDocumentation(resourcesSourceDir, resourcesOutputDir, 'robot.libdoc')

if __name__ == '__main__':
  main()


# coding=utf-8
from logging.handlers import SocketHandler

class NonPicklingSocketHandler(SocketHandler):
    def emit(self, record):
        try:
            s = self.formatter.format(record).encode() + b"\n"
            self.send(s)
        except Exception:
            self.handleError(record)


# -*- coding: utf-8 -*-
from __future__ import print_function

import os
import shutil
import unittest
import mock
import click
from click.testing import CliRunner

from yewdoc import YewStore, cli, ls, api, attach, browse, configure, context, convert, create, delete, \
    describe, diff, edit, find, global_pref, head, kind, ls, ping, push, read, register, \
    rename, show, status, sync, tag, tail, take, user_pref

import yewdoc

class MockRemote(object):
    def authenticate_user(self, data):
        return MockResponse({u'username': 'paul',
                             u'first_name': u'Paul',
                             u'last_name': u'Wolf',
                             u'email': u'paul.wolf@blah.com',
                             u'token': u'b9512423d9ffa2312bdcb3d5c60f7db26d5b08a1'}, 200)
class MockStore(object):
    def put_user_pref(self, s, d):
        pass

class MockYew(object):
    remote = MockRemote()
    store = MockStore()

class MockResponse:
        def __init__(self, json_data, status_code):
            self.json_data = json_data
            self.status_code = status_code

        def json(self):
            return self.json_data

class TestYewdocsClient(unittest.TestCase):

    def setUp(self):
        self.username = 'test_user'
        test_path = "{}/.yew.d/{}".format(os.path.expanduser("~"), self.username)

        # we delete entire environment for each test
        if os.path.exists(test_path):
            shutil.rmtree(test_path)

        self.store = YewStore(username=self.username)

    def create_document(self, title, content="my text", kind='md'):
        content = content
        kind = kind
        doc = self.store.create_document(title, 'default', kind)
        # doc.put_content(content.decode('utf-8'))
        doc.put_content(content)
        return doc

    def test_create_document(self):
        self.create_document("my test doc")

    def test_show_document(self):
        self.create_document("my test doc")
        runner = CliRunner()
        result = runner.invoke(cli, ['--user=test_user', 'show', 'my test doc'])
        assert result.exit_code == 0

    def test_describe_document(self):
        self.create_document("my test doc")
        runner = CliRunner()
        result = runner.invoke(cli, ['--user=test_user', 'describe', 'my test doc'])
        assert result.exit_code == 0
        assert u'location' in result.output
        assert u'kind' in result.output
        assert u'size' in result.output

    def test_tail_document(self):
        self.create_document("my test doc", content="dummy")
        runner = CliRunner()
        result = runner.invoke(cli, ['--user=test_user', 'tail', 'my test doc'])
        assert result.exit_code == 0
        assert 'dummy' in result.output

    def test_head_document(self):
        self.create_document("my test doc", content="dummy")
        runner = CliRunner()
        result = runner.invoke(cli, ['--user=test_user', 'head', 'my test doc'])
        assert result.exit_code == 0
        assert 'dummy' in result.output

    def test_kind_document(self):
        self.create_document("my test doc", content="dummy", kind='md')
        runner = CliRunner()
        result = runner.invoke(cli, ['--user=test_user', 'kind', 'my test doc', 'txt'])
        print(result.output)
        assert result.exit_code == 0

    def test_rename_document(self):
        self.create_document("my test doc", content="dummy", kind='md')
        runner = CliRunner()
        result = runner.invoke(cli, ['--user=test_user', 'rename', 'my test doc', 'my new test doc'])
        print(result.output)
        # assert result.exit_code == 0

    def test_convert_document(self):
        self.create_document("my test doc", content="dummy", kind='md')
        runner = CliRunner()
        result = runner.invoke(cli, ['--user=test_user', 'convert', 'my test doc', 'html'])
        print(result.output)
        assert result.exit_code == 0
        assert '
dummy
' in result.output

    def test_unicode(self):
        name = u"my test dóc"
        content = u"Ā ā Ă ă Ą ą Ć ć Ĉ ĉ Ċ ċ Č č Ď ď Đ đ Ē ē Ĕ ĕ Ė ė Ę ę Ě ě Ĝ ĝ Ğ ğ Ġ ġ Ģ ģ Ĥ ĥ Ħ ħ Ĩ ĩ Ī ī Ĭ ĭ Į į İ ı Ĳ ĳ Ĵ ĵ Ķ ķ ĸ Ĺ ĺ Ļ ļ Ľ ľ Ŀ ŀ Ł ł Ń ń Ņ ņ Ň ň ŉ Ŋ ŋ Ō ō Ŏ ŏ Ő ő Œ œ Ŕ ŕ Ŗ ŗ Ř ř Ś ś Ŝ ŝ Ş ş Š š Ţ ţ Ť ť Ŧ ŧ Ũ ũ Ū ū Ŭ ŭ Ů ů Ű ű Ų ų Ŵ ŵ Ŷ ŷ Ÿ Ź ź Ż ż Ž ž ſ"
        doc = self.create_document(name, content)
        assert doc.get_content() in content
        content = u" ം ഃ അ ആ ഇ ഈ ഉ ഊ ഋ ഌ എ ഏ ഐ ഒ ഓ ഔ ക ഖ ഗ ഘ ങ ച ഛ ജ ഝ ഞ ട ഠ ഡ ഢ ണ ത ഥ ദ ധ ന പ ഫ ബ ഭ മ യ ര റ ല ള ഴ വ ശ ഷ സ ഹ ാ ി ീ ു ൂ ൃ െ േ ൈ ൊ ോ ൌ ് ൗ ൠ ൡ ൦ ൧ ൨ ൩ ൪ ൫ ൬"
        doc = self.create_document(name, content)
        assert doc.get_content() in content

    def test_take(self):
        """Create document via take."""
        runner = CliRunner()
        with runner.isolated_filesystem():
            with open('hello.txt', 'w') as f:
                f.write('Hello World!')
            result = runner.invoke(cli, ['--user=test_user', 'take', 'hello.txt', '--force'])
            print(result.output)
            result = runner.invoke(cli, ['--user=test_user', 'ls', '-l'])
            assert 'hello' in result.output
            assert result.exit_code == 0

    def test_ls(self):
        runner = CliRunner()
        result = runner.invoke(cli, ['--user=test_user', 'ls', '--info'])
        print(result.output)
        assert result.exit_code == 0
        # assert result.output == 'Hello Peter!\n'

    def test_status(self):
        runner = CliRunner()
        result = runner.invoke(cli, ['--user=test_user', 'status'])
        print(result.output)
        assert result.exit_code == 0

    def test_global_pref(self):
        runner = CliRunner()
        result = runner.invoke(cli, ['global_pref'])
        assert result.exit_code == 0
        #print(result.output)

    def test_user_pref(self):
        runner = CliRunner()
        result = runner.invoke(cli, ['--user=test_user', 'user_pref'])
        assert result.exit_code == 0
        #print(result.output)

    def test_authenticate(self):
        yewdoc.yew = MockYew()
        status_code = yewdoc._authenticate('blah','blah')
        assert status_code == 200

if __name__ == '__main__':
    unittest.main()


"""
This module contains the tests to check offpele's molecular representations.
"""

import pytest

import tempfile
from offpele.main import run_offpele
from offpele.utils import get_data_file_path, temporary_cd

FORCEFIELD_NAME = 'openff_unconstrained-1.2.0.offxml'

class TestMain(object):
    """
    It wraps all tests that involve the Molecule class.
    """

    def test_offpele_default_call(self):
        LIGAND_PATH = 'ligands/BNZ.pdb'
        ligand_path = get_data_file_path(LIGAND_PATH)

        with tempfile.TemporaryDirectory() as tmpdir:
            with temporary_cd(tmpdir):
                run_offpele(ligand_path, output=tmpdir)

    def test_offpele_custom_call(self):
        LIGAND_PATH = 'ligands/BNZ.pdb'
        ligand_path = get_data_file_path(LIGAND_PATH)

        with tempfile.TemporaryDirectory() as tmpdir:
            with temporary_cd(tmpdir):
                run_offpele(ligand_path,
                            forcefield=FORCEFIELD_NAME,
                            resolution=10,
                            charges_method='gasteiger',
                            output=tmpdir,
                            with_solvent=True,
                            as_datalocal=True)


import os
import random

import torch

from torchvision import transforms

from PIL import Image
from utils import create_watermark_mask

class RandomBitMask(object):

    def __init__(self, dim=100, nbit=10, const=-10, device=torch.device('cpu')):
        self.dim    = dim
        self.nbit   = nbit
        self.const  = const
        self.device = device
        self.reset_mask()

    def __call__(self, z):
        with torch.no_grad():
            # dim = 1: 修改z的第1维（从0维开始）的内容
            # index: self.mask.repeat(z.size(0), 1), 即 (batch_size, 10). 其中，每个维度的10个数字是打乱的
            # z[i][mask[i, j]] = self.const
            # 即随机选取z的每个样本的10个元素置为-10
            return z.clone().scatter_(1, self.mask.repeat(z.size(0), 1), self.const)

    def save(self, path):
        self.mask = self.mask.cpu()
        torch.save(self.mask, path)
        self.mask = self.mask.to(self.device)

    def load(self, path):
        self.mask = torch.load(path)
        self.mask = self.mask.to(self.device)

    def reset_mask(self):
        # randperm函数：随机打乱[0, self.dim)区间内的整数
        # self.mask: [1, 10]
        self.mask = torch.randperm(self.dim)[:self.nbit].unsqueeze(0).to(self.device)

class ApplyWatermark(object):
    # watermark: watermark img path
    def __init__(self, watermark, wtmk_size=16, norm=False, device=torch.device('cpu')):
        # w包含watermark（object）和mask
        w = create_watermark_mask(watermark, wtmk_size=wtmk_size, norm=norm, device=device)
        self.path = watermark
        self.norm = norm
        # obj_tmp: watermark img
        self.obj_tmp = w['object']
        self.msk_tmp = w['mask']
        self.obj = torch.empty(1, 1, 1, 1).to(device)
        self.msk = torch.empty(1, 1, 1, 1).to(device)
        self.img_size = (-1, -1, -1)

    def __call__(self, image, position='top-left'):
        # image: [bs, 3, 32, 32]
        bsz, ci, hi, wi = image.size()
        _, hw, ww     = self.obj_tmp.size()
        print(f'ApplywATERMARK: {self.obj_tmp.size()}')

        if position == 'top-left':
            idx_h, idx_w = (0, 0)
        elif position == 'top-right':
            idx_h, idx_w = (0, max(wi - ww, 0))
        elif position == 'bottom-left':
            idx_h, idx_w = (max(hi - hw, 0), 0)
        elif position == 'bottom-right':
            idx_h, idx_w = (max(hi - hw, 0), max(wi - ww, 0))
        elif position == 'center':
            idx_h, idx_w = (max(hi // 2 - hw // 2, 0), max(wi // 2 - ww // 2, 0))
        elif position == 'random':
            h = max(hi - hw, 0)
            w = max(wi - ww, 0)
            idx_h, idx_w = (random.randint(0, h), random.randint(0, w))
        else:
            idx_h, idx_w = (0, 0)

        dh = min(hi, hw)
        dw = min(wi, ww)

        if not self.img_size == (bsz, hi, wi):

            self.obj.resize_(image.size()).fill_(-1 if self.norm else 0)
            self.msk.resize_(image.size()).fill_(1)

            self.obj[..., idx_h:idx_h+dh, idx_w:idx_w+dw].copy_(self.obj_tmp[:, :dh, :dw])
            self.msk[..., idx_h:idx_h+dh, idx_w:idx_w+dw].copy_(self.msk_tmp[:, :dh, :dw])

            self.img_size = (bsz, hi, wi)

        with torch.no_grad():
            return image * self.msk + (1 - self.msk) * self.obj

    def save(self, path):
        # 将self.path下的图像复制到 path下
        os.system(f'cp {self.path} {path}')

class Grayscale(object):

    def __init__(self):
        self.f = transforms.Compose([
            transforms.ToPILImage(),
            transforms.Grayscale(num_output_channels=3),
            transforms.ToTensor(),
        ])

    def __call__(self, image):
        device = image.device
        image  = image.detach().cpu()
        return torch.cat([self.f(image[i]).unsqueeze(0) for i in range(image.size(0))]).to(device)

class FillWatermark(object):

    def __init__(self, watermark, norm=False):
        self.path = watermark
        wm = Image.open(watermark)
        self.watermark = Image.new("RGB", wm.size, (255, 255, 255))
        self.watermark.paste(wm, mask=wm.split()[3])
        if norm:
            self.transform = lambda size: transforms.Compose([
                transforms.Resize(size),
                transforms.ToTensor(),
                transforms.Normalize([0.5, ] * 3, [0.5, ] * 3),
                transforms.Lambda(lambda x: x.unsqueeze(0)),
            ])
        else:
            self.transform = lambda size: transforms.Compose([
                transforms.Resize(size),
                transforms.ToTensor(),
                transforms.Lambda(lambda x: x.unsqueeze(0)),
            ])
        self.cached_size = (0, 0)
        self.cached_watermark = None

    def __call__(self, image):
        bsz, _, h, w = image.size()
        if not self.cached_size == (h, w):
            self.cached_size = (h, w)
            self.cached_watermark = self.transform((h, w))(self.watermark)
        return self.cached_watermark.repeat(bsz, 1, 1, 1).to(image.device)

    def save(self, path):
        os.system(f'cp {self.path} {path}')

class InvertColor(object):

    def __init__(self, norm=False):
        self.f = (lambda x: x * -1) if norm else (lambda x: 1 - x)

    def __call__(self, image):
        with torch.no_grad(): return self.f(image)

class ApplyBlackPatch(object):

    def __init__(self, patch_size=16, norm=False, device=torch.device('cpu')):
        self.norm = norm
        self.patch = torch.zeros(3, patch_size, patch_size).to(device)
        self.patch_cache = torch.empty(1, 1, 1, 1).to(device)
        if norm: self.patch.fill_(-1)
        self.img_size = (-1, -1, -1)

    def __call__(self, image, position='top-left'):
        bsz, ci, hi, wi = image.size()
        _, hw, ww     = self.patch.size()

        if position == 'top-left':
            idx_h, idx_w = (0, 0)
        elif position == 'top-right':
            idx_h, idx_w = (0, max(wi - ww, 0))
        elif position == 'bottom-left':
            idx_h, idx_w = (max(hi - hw, 0), 0)
        elif position == 'bottom-right':
            idx_h, idx_w = (max(hi - hw, 0), max(wi - ww, 0))
        elif position == 'center':
            idx_h, idx_w = (max(hi // 2 - hw // 2, 0), max(wi // 2 - ww // 2, 0))
        elif position == 'random':
            h = max(hi - hw, 0)
            w = max(wi - ww, 0)
            idx_h, idx_w = (random.randint(0, h), random.randint(0, w))
        else:
            idx_h, idx_w = (0, 0)

        dh = min(hi, hw)
        dw = min(wi, ww)

        if not self.img_size == (bsz, hi, wi):

            self.patch_cache.resize_(image.size()).fill_(-1 if self.norm else 0)
            self.patch_cache[..., idx_h:idx_h+dh, idx_w:idx_w+dw].copy_(self.patch[:, :dh, :dw])

            self.img_size = (bsz, hi, wi)

        image[..., idx_h:idx_h+dh, idx_w:idx_w+dw] = self.patch_cache[..., :dh, :dw]
        return image

    def save(self, path):
        device = self.patch.device
        transforms.ToPILImage()(self.patch.detach()).save(path)
        self.patch = self.patch.to(device)

class ApplyRandomPatch(object):

    def __init__(self, patch_size=16, norm=False, device=torch.device('cpu')):
        self.norm = norm
        self.patch = torch.rand(3, patch_size, patch_size).to(device)
        self.patch_cache = torch.empty(1, 1, 1, 1).to(device)
        if norm: self.patch = self.patch * 2 - 1
        self.img_size = (-1, -1, -1)

    def __call__(self, image, position='top-left'):
        bsz, ci, hi, wi = image.size()
        _, hw, ww     = self.patch.size()

        if position == 'top-left':
            idx_h, idx_w = (0, 0)
        elif position == 'top-right':
            idx_h, idx_w = (0, max(wi - ww, 0))
        elif position == 'bottom-left':
            idx_h, idx_w = (max(hi - hw, 0), 0)
        elif position == 'bottom-right':
            idx_h, idx_w = (max(hi - hw, 0), max(wi - ww, 0))
        elif position == 'center':
            idx_h, idx_w = (max(hi // 2 - hw // 2, 0), max(wi // 2 - ww // 2, 0))
        elif position == 'random':
            h = max(hi - hw, 0)
            w = max(wi - ww, 0)
            idx_h, idx_w = (random.randint(0, h), random.randint(0, w))
        else:
            idx_h, idx_w = (0, 0)

        dh = min(hi, hw)
        dw = min(wi, ww)

        if not self.img_size == (bsz, hi, wi):

            self.patch_cache.resize_(image.size()).fill_(-1 if self.norm else 0)
            self.patch_cache[..., idx_h:idx_h+dh, idx_w:idx_w+dw].copy_(self.patch[:, :dh, :dw])

            self.img_size = (bsz, hi, wi)

        image[..., idx_h:idx_h+dh, idx_w:idx_w+dw] = self.patch_cache[..., :dh, :dw]
        return image

    def save(self, path):
        device = self.patch.device
        transforms.ToPILImage()(self.patch.cpu()).save(path)
        self.patch = self.patch.to(device)

    def load(self, path):
        device = self.patch.device
        patch = transforms.ToTensor()(Image.open(path))
        if self.norm: patch = patch * 2 - 1
        self.patch = patch.to(device)

    def reset_mask(self):
        self.patch = torch.rand_like(self.patch).to(self.patch.device)


# @Time    : 2018/6/22 18:08
# @Author  : cap
# @FileName: myOpencvStar.py
# @Software: PyCharm Community Edition
# @introduction: star特征检测
import cv2 as cv

image = cv.imread('./data/table.jpg')
cv.imshow('Original', image)

gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
cv.imshow('Gray', gray)

# 创建检测器
dectector = cv.xfeatures2d.StarDetector_create()
keypoints = dectector.detect(gray)
cv.drawKeypoints(image, keypoints, image, flags=cv.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv.imshow('Star', image)

cv.waitKey()


import logging
import os
import re

import orjson
import psycopg2
import psycopg2.extras

from firepit.exceptions import DuplicateTable
from firepit.exceptions import InvalidAttr
from firepit.exceptions import UnknownViewname
from firepit.splitter import SqlWriter
from firepit.sqlstorage import SqlStorage
from firepit.sqlstorage import infer_type
from firepit.sqlstorage import validate_name

logger = logging.getLogger(__name__)

def get_storage(url, session_id):
    dbname = url.path.lstrip('/')
    return PgStorage(dbname, url.geturl(), session_id)

def _infer_type(key, value):
    # PostgreSQL type specializations
    rtype = None
    if isinstance(value, bool):
        rtype = 'BOOLEAN'
    else:
        # Fall back to defaults
        rtype = infer_type(key, value)
    return rtype

class PgStorage(SqlStorage):
    def __init__(self, dbname, url, session_id=None):
        super().__init__()
        self.placeholder = '%s'
        self.text_min = 'LEAST'
        self.text_max = 'GREATEST'
        self.ifnull = 'COALESCE'
        self.dbname = dbname
        if not session_id:
            session_id = 'firepit'
        self.session_id = session_id
        options = f'options=--search-path%3D{session_id}'
        sep = '&' if '?' in url else '?'
        connstring = f'{url}{sep}{options}'
        self.connection = psycopg2.connect(
            connstring,
            cursor_factory=psycopg2.extras.RealDictCursor)

        if session_id:
            try:
                self._execute(f'CREATE SCHEMA IF NOT EXISTS "{session_id}";')
            except psycopg2.errors.UniqueViolation:
                self.connection.rollback()

        self._execute(f'SET search_path TO "{session_id}";')

        cursor = self._execute('BEGIN;')
        try:
            self._execute('''CREATE FUNCTION match(pattern TEXT, value TEXT)
                RETURNS boolean AS $$
                    SELECT regexp_match(value, pattern) IS NOT NULL;
            $$ LANGUAGE SQL;''', cursor=cursor)
            self._execute('''CREATE FUNCTION in_subnet(addr TEXT, net TEXT)
                RETURNS boolean AS $$
                    SELECT addr::inet <<= net::inet;
            $$ LANGUAGE SQL;''', cursor=cursor)

            # Do DB initization from base class
            stmt = ('CREATE UNLOGGED TABLE IF NOT EXISTS "__symtable" '
                    '(name TEXT, type TEXT, appdata TEXT);')
            self._execute(stmt, cursor)
            stmt = ('CREATE UNLOGGED TABLE IF NOT EXISTS "__membership" '
                    '(sco_id TEXT, var TEXT);')
            self._execute(stmt, cursor)
            stmt = ('CREATE UNLOGGED TABLE IF NOT EXISTS "__queries" '
                    '(sco_id TEXT, query_id TEXT);')
            self._execute(stmt, cursor)
            self.connection.commit()
            cursor.close()
        except psycopg2.errors.DuplicateFunction:
            self.connection.rollback()

        logger.debug("Connection to PostgreSQL DB %s successful", dbname)

    def _get_writer(self, prefix):
        """Get a DB inserter object"""
        filedir = os.path.dirname(self.dbname)
        return SqlWriter(
            filedir,
            self,
            placeholder=self.placeholder,
            infer_type=_infer_type)

    def __del__(self):
        if self.connection:
            logger.debug("Closing PostgreSQL DB connection")
            self.connection.close()

    def _query(self, query, values=None):
        """Private wrapper for logging SQL query"""
        logger.debug('Executing query: %s', query)
        cursor = self.connection.cursor()
        if not values:
            values = ()
        try:
            cursor.execute(query, values)
        except psycopg2.errors.UndefinedColumn as e:
            self.connection.rollback()
            raise InvalidAttr(str(e)) from e
        except psycopg2.errors.UndefinedTable as e:
            self.connection.rollback()
            raise UnknownViewname(str(e)) from e
        self.connection.commit()
        return cursor

    def _create_table(self, tablename, columns):
        # Same as base class, but disable WAL
        stmt = f'CREATE UNLOGGED TABLE "{tablename}" ('
        stmt += ','.join([f'"{colname}" {coltype}' for colname, coltype in columns.items()])
        stmt += ');'
        logger.debug('_create_table: "%s"', stmt)
        try:
            cursor = self._execute(stmt)
            if 'x_contained_by_ref' in columns:
                self._execute(f'CREATE INDEX "{tablename}_obs" ON "{tablename}" ("x_contained_by_ref");', cursor)
            self.connection.commit()
            cursor.close()
        except (psycopg2.errors.DuplicateTable,
                psycopg2.errors.DuplicateObject,
                psycopg2.errors.UniqueViolation) as e:
            self.connection.rollback()
            raise DuplicateTable(tablename) from e

    def _add_column(self, tablename, prop_name, prop_type):
        stmt = f'ALTER TABLE "{tablename}" ADD COLUMN "{prop_name}" {prop_type};'
        logger.debug('new_property: "%s"', stmt)
        try:
            cursor = self._execute(stmt)
            self.connection.commit()
            cursor.close()
        except psycopg2.errors.DuplicateColumn:
            self.connection.rollback()

    def _create_empty_view(self, viewname, cursor):
        cursor.execute(f'CREATE VIEW "{viewname}" AS SELECT NULL as type WHERE 1<>1;')

    def _create_view(self, viewname, select, sco_type, deps=None, cursor=None):
        """Overrides parent"""
        validate_name(viewname)
        if not cursor:
            cursor = self._execute('BEGIN;')
        if not deps:
            deps = []
        elif viewname in deps:
            # Get the query that makes up the current view
            slct = self._get_view_def(viewname)
            self._execute(f'DROP VIEW IF EXISTS "{viewname}"', cursor)
            # Swap out the viewname for its definition
            select = re.sub(f'"{viewname}"', f'({slct}) AS tmp', select)
        try:
            self._execute(f'CREATE OR REPLACE VIEW "{viewname}" AS {select}', cursor)
        except psycopg2.errors.UndefinedTable:
            # Missing dep?
            self.connection.rollback()
            cursor = self._execute('BEGIN;')
            self._create_empty_view(viewname, cursor)
        except psycopg2.errors.InvalidTableDefinition:
            # Usually "cannot drop columns from view"
            #logger.error(e, exc_info=e)
            self.connection.rollback()
            cursor = self._execute('BEGIN;')
            self._execute(f'DROP VIEW IF EXISTS "{viewname}";', cursor)
            self._execute(f'CREATE VIEW "{viewname}" AS {select}', cursor)
        self._new_name(cursor, viewname, sco_type)
        return cursor

    def _get_view_def(self, viewname):
        cursor = self._query("SELECT definition"
                             " FROM pg_views"
                             " WHERE schemaname = %s"
                             " AND viewname = %s", (self.session_id, viewname))
        viewdef = cursor.fetchone()
        stmt = viewdef['definition'].rstrip(';')

        # PostgreSQL will "expand" the original "*" to the columns
        # that existed at that time.  We need to get the star back, to
        # match SQLite3's behavior.
        return re.sub(r'^.*?FROM', 'SELECT * FROM', stmt, 1, re.DOTALL)

    def tables(self):
        cursor = self._query("SELECT table_name"
                             " FROM information_schema.tables"
                             " WHERE table_schema = %s", (self.session_id, ))
        rows = cursor.fetchall()
        return [i['table_name'] for i in rows
                if not i['table_name'].startswith('__')]

    def views(self):
        cursor = self._query("SELECT table_name"
                             " FROM information_schema.tables"
                             " WHERE table_schema = %s"
                             " AND table_type = 'VIEW'", (self.session_id, ))
        rows = cursor.fetchall()
        return [i['table_name'] for i in rows]

    def columns(self, viewname):
        validate_name(viewname)
        cursor = self._query("SELECT column_name"
                             " FROM information_schema.columns"
                             " WHERE table_schema = %s"
                             " AND table_name = %s", (self.session_id, viewname))
        rows = cursor.fetchall()
        return [i['column_name'] for i in rows]

    def schema(self, viewname):
        validate_name(viewname)
        cursor = self._query("SELECT column_name AS name, data_type AS type"
                             " FROM information_schema.columns"
                             " WHERE table_schema = %s"
                             " AND table_name = %s", (self.session_id, viewname))
        return cursor.fetchall()

    def delete(self):
        """Delete ALL data in this store"""
        cursor = self._execute('BEGIN;')
        self._execute(f'DROP SCHEMA "{self.session_id}" CASCADE;', cursor)
        self.connection.commit()
        cursor.close()

    def upsert_many(self, cursor, tablename, objs, query_id):
        cols = set()
        for obj in objs:
            cols = cols.union(obj.keys())
        colnames = list(cols)
        if tablename == 'identity':
            action = 'NOTHING'
        else:
            excluded = self._get_excluded(colnames, tablename)
            action = f'UPDATE SET {excluded}'
        valnames = ', '.join([f'"{x}"' for x in colnames])
        placeholders = ', '.join([f"({', '.join([self.placeholder] * len(colnames))})"] * len(objs))
        stmt = (f'INSERT INTO "{tablename}" ({valnames}) VALUES {placeholders}'
                f' ON CONFLICT (id) DO {action};')
        values = []
        query_values = []
        for obj in objs:
            query_values.append(obj['id'])
            query_values.append(query_id)
            for c in colnames:
                value = obj.get(c, None)
                values.append(str(orjson.dumps(value), 'utf-8') if isinstance(value, list) else value)
        logger.debug('upsert_many: count=%d table=%s columns=%s action=%s"',
                     len(objs), tablename, valnames, action)
        cursor.execute(stmt, values)

        # Now add to query table as well
        placeholders = ', '.join([f'({self.placeholder}, {self.placeholder})'] * len(objs))
        stmt = (f'INSERT INTO "__queries" (sco_id, query_id)'
                f' VALUES {placeholders}')
        cursor.execute(stmt, query_values)


from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from django.http import HttpResponse
import json
from merchants.models import AccountDetail, ProductDetail, AccessToken
from .models import Orders, OrderProducts
from shopify.models import Vendor, Products, Account, Commission, ApiAuth
from shopify.views import send_template
import requests
import datetime

class PlaceOrder(APIView):
	def post(self, request):

		response = {}
		print(request.META)

		if 'HTTP_ACCESS_TOKEN' not in request.META or not request.META['HTTP_ACCESS_TOKEN']:
			print("token not found")
			response = {"error": "You don't have access to send order email to vendor."}
			return Response(response)

		access_token = request.META['HTTP_ACCESS_TOKEN']
		verify_shop = request.META['HTTP_SHOP']

		existing_token = ApiAuth.objects.filter(platform_key=access_token,platform_name=verify_shop)
		if not existing_token:
			response = {"error": "You don't have access to send order email to vendor."}
			return Response(response)

		OrderId = request.POST['OrderId']

		domain = request.POST['domain']

		OrderUrl = request.POST['OrderUrl']

		financial_status = request.POST['financial_status']

		platform = request.POST['platform']

		products = request.POST['products']

		paymentMethod = request.POST['paymentMethod']

		customer_email=request.POST['customer_email']

		customer_first_name=request.POST['first_name']

		customer_last_name=request.POST['last_name']

		customer_address=request.POST['customer_address']

		customer_city=request.POST['city']
		customer_province=request.POST['province']

		customer_phone=request.POST['phone']
		customer_zipcode=request.POST['zipcode']
		customer_country=request.POST['country']
		paid_by_merchant = request.POST['paid_by_merchant']

		domain_detail = AccountDetail.objects.get(shop_domain=domain)
		merchant_id = domain_detail.id
		merchant_name = domain_detail.username

		existing_order = Orders.objects.filter(OrderId=OrderId)

		if not existing_order:
			order_obj = Orders(merchant_id=merchant_id, platform=platform, OrderId=OrderId, paymentMethod=paymentMethod, financial_status=financial_status, OrderUrl=OrderUrl,customer_email=customer_email,customer_first_name=customer_first_name,customer_last_name=customer_last_name,customer_address=customer_address,customer_city=customer_city,customer_province=customer_province,customer_phone=customer_phone,customer_zipcode=customer_zipcode,customer_country=customer_country,updated_status='',vendorPlateformOrderId='',paid_by_merchant=paid_by_merchant,vendor_order_number='')
			order_obj.save()
			order_id = order_obj.id
			order_date = order_obj.date
			order_time = order_obj.time

			calculated = 0

			for value in products.values():

				try:
					product_detail = ProductDetail.objects.get(PlatformProductId=int(value["id"]))
					vendor_products = []
					user_id = product_detail.vendor_id
					vendor_email = product_detail.vendor.emailid
					first_name = product_detail.vendor.first_name
					shopify_domain = product_detail.vendor.website
					token = product_detail.vendor.token

					commission = 0

					commission_detail = Commission.objects.filter(user_id=user_id)
					if commission_detail:
						commission = commission_detail[0].commission

					ProductQty = value['ProductQty']

					vendor_lis = []
					merchant_lis = []

					try:
						product_id = product_detail.product_id
						merchant_id = product_detail.merchant_id

						productdetail = Products.objects.get(id=product_id)
						product_name = productdetail.title.encode('utf-8')
						existing_quantity = productdetail.quantity

						vendor_platformid = productdetail.PlatformProductId

						new_quantity = int(existing_quantity) - int(ProductQty)
						productdetail.quantity = new_quantity
						productdetail.save()

						platform = productdetail.platform

						dropshipping_price = productdetail.dropshipping_price
						productprice = ProductQty * dropshipping_price

						calculated_commission = productprice + productprice * commission / 100
						calculated += calculated_commission

						product_obj = OrderProducts(order_id=order_id, product_id=product_id, product_name=product_name, product_price=dropshipping_price, user_id=user_id, ShopifyProductId=ShopifyProductId, ProductQty=ProductQty, date=order_date, time=order_time)
						product_obj.save()
						vendor_products.append({"product_name": product_name, "product_quantity": ProductQty, 'product_price': productprice})

						vendor_lis.append({'ProductQuantity':new_quantity,'platform':platform,'PlatformProductId':vendor_platformid,'shopify_domain':shopify_domain,'token':token})
						vendor_link = "https://shopify.fashioncircle.de/vendorApp/webhooks/update_product_inventory.php"
						# resp = requests.post(vendor_link, data=json.dumps(vendor_lis), headers={'content-type': 'application/json'})

						all_merchants = ProductDetail.objects.filter(product_id=product_id)
						for one_product in all_merchants:
							merchant_id = one_product.merchant_id

							merchant_detail = AccountDetail.objects.get(id=merchant_id)
							merch_platform = merchant_detail.platform
							merchant_domain = merchant_detail.shopify_domain
							merch_productid = one_product.PlatformProductId
							merchant_token = merchant_detail.token
							merchant_lis.append({'platform':merch_platform,'PlatformProductId':merch_productid,'ProductQuantity':new_quantity,'shopify_domain':merchant_domain,'token':merchant_token})
						merchant_link = "https://shopify.fashioncircle.de/fashioncircle/webhooks/update_product_inventory.php"
						# resp = requests.post(merchant_link, data=json.dumps(merchant_lis), headers={'content-type': 'application/json'})
					except Exception as e:
						print(e)
						pass
					template_id = "139248"
					Vars = {"first_name": first_name, "merchant_name": merchant_name, "mj-invoice-item": vendor_products, "order_price": calculated}
					send_template(vendor_email, Vars, template_id)
				except:
					pass
			# update product
			Orders.objects.filter(id=order_id).update(total_amount=calculated)
		else:
			pass

		response['success'] = "Successfully placed order."
		return Response(response)

class OrderPaid(APIView):

	def post(self, request, *args, **kwargs):

		response = {}

		if 'HTTP_ACCESS_TOKEN' not in request.META or not request.META['HTTP_ACCESS_TOKEN']:
			response = {"error": "You don't have access to send order email to vendor."}
			return Response(response)

		access_token = request.META['HTTP_ACCESS_TOKEN']
		existing_token = AccessToken.objects.filter(access_token=access_token)
		if not existing_token:
			response = {"error": "You don't have access to send order email to vendor."}
			return Response(response)

		if 'vendorName' not in request.POST or not request.POST['vendorName']:
			response['error'] = "Please enter vendorName."
			return Response(response)

		if 'OrderID' not in request.POST or not request.POST['OrderID']:
			response['error'] = "Please enter OrderID."
			return Response(response)

		vendor = request.POST['vendorName']
		order_ids = request.POST['OrderID']

		try:
			vendor_detail = Vendor.objects.get(vendor=vendor)
		except:
			response['error'] = "Vendor does not exist."
			return Response(response)
		user_id = vendor_detail.user_id

		user_detail = Account.objects.get(id=user_id)
		user_name = user_detail.first_name
		vendor_email = user_detail.emailid
		vendorShopDomain = user_detail.website

		orders = []

		commission = 0

		commission_detail = Commission.objects.filter(user_id=user_id)
		if commission_detail:
			commission = commission_detail[0].commission

		if ',' not in order_ids:
			orders.append(order_ids)
		else:
			orders = order_ids.split(",")

		calculated = 0

		calculated_commission = 0

		order_products = []
		order_list=[]
		final_product_list=[]
		final_order_list=[]

		merchant_name = ''

		for orderId in orders:
			today = datetime.date.today()
			updated_status = today.strftime("%d/%m/%Y")
			existing_record = Orders.objects.get(OrderId=orderId)
			check_status = existing_record.paid_by_merchant

			if check_status != 'paid':
				order_update = Orders.objects.filter(OrderId=orderId).update(paid_by_merchant='paid', updated_status=updated_status)
				order_detail = Orders.objects.get(OrderId=orderId)
				merchant_id = order_detail.merchant_id
				order_id = order_detail.id
				customer_email=order_detail.customer_email
				first_name=order_detail.customer_first_name
				last_name=order_detail.customer_last_name
				customer_address=order_detail.customer_address
				city=order_detail.customer_city
				province=order_detail.customer_province
				phone=order_detail.customer_phone
				zipcode=order_detail.customer_zipcode
				country=order_detail.customer_country
				order_list.append({'Order_Details':{'OrderId':orderId,'customer_email':customer_email,'first_name':first_name,'last_name':last_name,'customer_address':customer_address,'city':city,'province':province,'phone':phone,'zipcode':zipcode,'country':country,'commission':commission,'vendorShopDomain':vendorShopDomain}})

				merchant_detail = AccountDetail.objects.get(id=merchant_id)
				merchant_name = merchant_detail.username

				products = OrderProducts.objects.filter(order_id=order_id)

				for product in products:
					product_name = product.product_name
					product_price=product.product_price
					product_id=product.product_id
					try:
						product_object=Products.objects.filter(id=product_id)
						PlatformProductId=product_object[0].PlatformProductId
					except:
						PlatformProductId=''

					ProductQty = product.ProductQty
					product_price = product.product_price
					productprice = ProductQty * product_price
					product_commission = productprice * commission / 100
					price = productprice + product_commission
					calculated += price
					calculated_commission += product_commission

					final_product_list.append({'ProductName':product_name,'ProductPrice':product_price,'PlatformProductId':PlatformProductId,'ProductQty':ProductQty})

					order_products.append({"product_name": product_name, "product_quantity": ProductQty, 'product_price': productprice})

				order_list.append(final_product_list)
				final_order_list.append(order_list)
				order_list=[]
				final_product_list=[]

		template_id = "161720"

		Vars = {"first_name": user_name, "merchant_name": merchant_name, "mj-invoice-item": order_products, "total_price": calculated, "commission": calculated_commission}
		send_template(vendor_email, Vars, template_id)
		# response['success'] = "Mail sent successfully."
		vendor_link = "https://shopify.fashioncircle.de/vendorApp/webhooks/create_merchant_order.php"
		resp = requests.post(vendor_link, data=json.dumps(final_order_list), headers={'content-type': 'application/json'})
		return HttpResponse(json.dumps(final_order_list))

class FulfillmentStatus(APIView):

	def post(self, request, *args, **kwargs):

		if 'HTTP_API_KEY' not in self.request.META or not self.request.META['HTTP_API_KEY']:
			response = {"error":" You Dont Have Access To Api"}
			return Response(response)

		api_key = self.request.META['HTTP_API_KEY']
		if api_key != "pbkdf2_sha256$29000$p7lpJChcK4Lo$IHMul9j5lcPNJP4f/W1nXZknVi2N+GJIR1tZCo5C7uM=":

			response = {"error":" Wrong Api Key"}
			return Response(response)

		VendorPlatformOrderID=request.POST.get('OrderId')
		tracking_company=request.POST.get('tracking_company')
		if not tracking_company:
			tracking_company=''
		vendor_domain=request.POST.get('Domain')
		tracking_number=request.POST.get('tracking_number')
		if not tracking_number:
			tracking_number=''
		tracking_url=request.POST.get('tracking_url')
		if not tracking_url:
			tracking_url=''
		line_items=request.POST.get('line_items')
		final_lis=[]
		new_list=[]
		dicts={}

		line_items=json.loads(line_items)

		# hit the webhook first
		details=Orders.objects.filter(vendorPlateformOrderId=VendorPlatformOrderID)
		order_id=details[0].id
		merchantOrderId=details[0].OrderId
		merchant_id=details[0].merchant_id
		tracking_company=tracking_company
		tracking_url=tracking_url
		tracking_number=tracking_number

		acc_obj=AccountDetail.objects.filter(id=merchant_id)
		if acc_obj:
			token=acc_obj[0].token
			shopify_domain=acc_obj[0].shopify_domain

			for items in line_items:
				product_obj=Products.objects.get(PlatformProductId=items['product_id'])
				product_detail_obj=ProductDetail.objects.get(product_id=product_obj.id)
				merchant_platform_id=product_detail_obj.PlatformProductId
				order_pro=OrderProducts.objects.get(product_id=product_obj.id,order_id=order_id)
				dicts={'product_id':merchant_platform_id,'quantity':items['quantity'],'fulfillment_status':items['fulfillment_status']}
				new_list.append(dicts)

			final_lis.append({'line_items':new_list,'tracking_company':tracking_company,'tracking_url':tracking_url,'tracking_number':tracking_number,'merchantOrderId':str(merchantOrderId),'token':str(token),'shopify_domain':str(shopify_domain)})

			vendor_link = "https://shopify.fashioncircle.de/fashioncircle/webhooks/update_order_fulfill_status.php"
			resp = requests.post(vendor_link, data=json.dumps(final_lis), headers={'content-type': 'application/json'})

		else:
			response={'error':'No mercahnt with this PLatform ID'}
			return Response(response)

		backend_product_id=[]
		order_obj=Orders.objects.get(vendorPlateformOrderId=VendorPlatformOrderID)
		if order_obj:

			order_id=order_obj.id

			for items in line_items:

				product_obj=Products.objects.get(PlatformProductId=items['product_id'])
				order_pro=OrderProducts.objects.get(order_id=order_id,product_id=product_obj.id)
				fulfillment_status=items['fulfillment_status']
				fulfillment_status=str(fulfillment_status)

				if fulfillment_status=='fulfilled':

					xx=OrderProducts.objects.filter(order_id=order_id,product_id=product_obj.id).update(fulfillment_status='fulfilled',fulfillment_quantity=items['quantity'])
				if fulfillment_status=='partial':

					backend_fulfillment_quantity=order_pro.fulfillment_quantity
					new_ful_quan=int(backend_fulfillment_quantity)+int(items['quantity'])
					OrderProducts.objects.filter(order_id=order_id,product_id=product_obj.id).update(fulfillment_status='partial',fulfillment_quantity=new_ful_quan)
			axx=Orders.objects.filter(vendorPlateformOrderId=VendorPlatformOrderID).update(tracking_company=tracking_company,tracking_url=tracking_url,tracking_number=tracking_number)

			# check_all_products_status
			all_pro_status=OrderProducts.objects.filter(order_id=order_id)

			final_status_of_order_lis=[]
			for all_pro in all_pro_status:

				if all_pro.fulfillment_status=='fulfilled':
					pass
				else:
					final_status_of_order_lis.append('random')
			if not final_status_of_order_lis:

				Orders.objects.filter(vendorPlateformOrderId=VendorPlatformOrderID).update(fulfillment_status='fulfilled')
			else:
				Orders.objects.filter(vendorPlateformOrderId=VendorPlatformOrderID).update(fulfillment_status='partial')

			response={"success": "Order Updated Successfully"}
			return Response(response)

		else:
			response={"error": "This Order Id Does Not Exists"}
			return Response(response)


from recommender.recommender import recommender
from xlrd import open_workbook

"""
	This sample used jester and movielens as sample datasets, you can download dataset at:

	https://grouplens.org/datasets/movielens/
	http://goldberg.berkeley.edu/jester-data/

"""

def merge_preference_space(*prefs_space):
	"""
		The function merge all the preference spaces into one
	"""
	result = {}
	for space in prefs_space:
		result.update(space)

	return result

def make_preference_space(raw_data, start):
	"""
		create preference_space from raw data
		the data structure is dictionary with keys are users and values are dictionaries of items and ratings
	"""
	preference_space = {}
	for row in range(0,raw_data.nrows):
		preference_space[str('user ' + str(start + row))] = {}
		for col in range(1,raw_data.ncols):
			if raw_data.cell_value(row,col) == 99: continue
			preference_space[str('user ' + str(start + row))]['joke ' + str(col)] = float(raw_data.cell_value(row, col))

	return preference_space

def make_preference_space_Jester(file_path):
	"""
		The function takes in 3 jester dataset files and then merge the file
	"""

	raw_data_1 = open_workbook(file_path + '/jester-data-1.xls').sheets()[0]
	prefs_space_1 = make_preference_space(raw_data_1, 0)
	raw_data_2 = open_workbook(file_path + '/jester-data-2.xls').sheets()[0]
	prefs_space_2 = make_preference_space(raw_data_2, raw_data_1.nrows)
	raw_data_3 = open_workbook(file_path + '/jester-data-3.xls').sheets()[0]
	prefs_space_3 = make_preference_space(raw_data_3,raw_data_1.nrows + raw_data_2.nrows)

	return merge_preference_space(prefs_space_1, prefs_space_2, prefs_space_3)

def make_preference_space_MovieLens(path):
	"""
		The function processes Movielens data set
	"""
	movies = {}
	for line in open(path + '/movies.dat'):
		(movieid, title) = line.split('::')[0:2]
		movies[movieid] = title

	preference_space = {}
	for line in open(path + '/ratings.dat'):
		(user, movieid, rating, ts) = line.split('::')
		preference_space.setdefault(user, {})
		preference_space[user][movies[movieid]] = float(rating)

	return preference_space

def sample_jester():
	"""
		Running the application using Jester dataset:
	"""

	#Creating preference_space from Jester dataset
	preference_space = make_preference_space_Jester('../data/Jester')
	model = recommender(preference_space, recommender = 'user_based',
					number_of_items_to_recommend = 5, similarity = 'euclidean_distance')
	print(model.make_recomendation('user 36681'))

def sample_movielens():
	"""
		Running the application using Movielens dataset:
	"""

	#Creating preference_space from Movieslen dataset
	preference_space = make_preference_space_MovieLens('../data/Movielens')
	model = recommender(preference_space = preference_space, recommender = 'user_based',
						number_of_items_to_recommend = 10, similarity = 'euclidean_distance')
	print(model.make_recomendation('1'))

def main():
	sample_jester()
	sample_movielens()

if __name__=="__main__":
	main()


#!/usr/bin/python3
# -*- coding: utf-8 -*-
import time

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

#Example plug-in made with functions

def init_tab(self):
    tab = QWidget()
    tab.setLayout(make_layout())
    self.addTab(tab, 'tab1')

def make_layout():
    layout = QFormLayout()
    layout.addWidget(QLabel('example'))
    btn = QPushButton('button')
    layout.addWidget(btn)
    messagelist = QListWidget()
    layout.addWidget(messagelist)
    return(layout)


import json
import requests
from requests.api import get, head

def get_octopus_resource(uri, headers, skip_count = 0):
    items = []
    skip_querystring = ""

    if '?' in uri:
        skip_querystring = '&skip='
    else:
        skip_querystring = '?skip='

    response = requests.get((uri + skip_querystring + str(skip_count)), headers=headers)
    response.raise_for_status()

    # Get results of API call
    results = json.loads(response.content.decode('utf-8'))

    # Store results
    if 'Items' in results.keys():
        items += results['Items']

        # Check to see if there are more results
        if (len(results['Items']) > 0) and (len(results['Items']) == results['ItemsPerPage']):
            skip_count += results['ItemsPerPage']
            items += get_octopus_resource(uri, headers, skip_count)

    else:
        return results

    # return results
    return items

octopus_server_uri = 'https://YourURL'
octopus_api_key = 'API-YourAPIKey'
headers = {'X-Octopus-ApiKey': octopus_api_key}
space_name = "Default"
tagset_name = "MyTagset"
tagset_description = "My description"
tags = []
tag = {
    'Id': None,
    'Name': 'Tag1',
    'Color': '#ECAD3F',
    'Description': 'Tag Description',
    'CanonicalTagName': None
}

tags.append(tag)

# Get space
uri = '{0}/api/spaces'.format(octopus_server_uri)
spaces = get_octopus_resource(uri, headers)
space = next((x for x in spaces if x['Name'] == space_name), None)

# Check to see if tagset exists
uri = '{0}/api/{1}/tagsets?partialName={2}'.format(octopus_server_uri, space['Id'], tagset_name)
tagsets = get_octopus_resource(uri, headers)

if len(tagsets) == 0:
    tagset = {
        'Name': tagset_name,
        'Description': tagset_description,
        'Tags': tags
    }

    uri = '{0}/api/{1}/tagsets'.format(octopus_server_uri, space['Id'])
    response = requests.post(uri, headers=headers, json=tagset)
    response.raise_for_status()
else:
    print ('{0} already exists!'.format(tagset_name))


#!/usr/bin/env python

import hashlib
import numpy as np
import pandas as pd
import re

from .category import CategoriesPreprocess
from ...core import MODEL_TYPE_CLASSIFICATION
from ...core.config import CONFIG_DATASET_PREPROCESS
from ...core.log import MlLog
from .df_normalize import DfNormalize
from .df_impute import DfImpute
from ...util.sanitize import sanitize_name

def datetime_to_elapsed(dtimes):
    """
    Convert a numpy array of date_times into an 'epoch'
    If there are no missing values, an int64 is returned.
    Otherwise a float64 is used, so we can set missing to 'nan'
    """
    nanosec = 10 ** 9
    if dtimes.isnull().sum() > 0:
        # Use float, so we can use NaN in missing values
        elap = np.zeros(len(dtimes))
        is_null = dtimes.isnull()
        not_null = np.logical_not(is_null)
        elap[not_null] = dtimes[not_null].astype(np.int64) / nanosec
        elap[is_null] = np.nan
    else:
        # No missing values, we can safely use an int64
        elap = dtimes.astype(np.int64) // nanosec
    return elap

def bool_to_int_or_float(x):
    """
    Convert a numpy array of bool to a number
    If there are no missing values, an int8 is returned.
    Otherwise a float16 is used, so we can set missing to 'nan'
    """
    if x.isnull().sum() > 0:
        # Use float, so we can use NaN in missing values
        xf = np.zeros(len(x), dtype='float16')
        is_null = x.isnull()
        not_null = np.logical_not(is_null)
        xf[not_null] = x[not_null].astype(np.int8)
        xf[is_null] = np.nan
    else:
        # No missing values, we can safely use an int8
        return x.astype('int8')

class DfPreprocess(MlLog):
    """
    DataFrame preprocessing: convet categorical, one-hot encoding, impute
    missing data, normalize.

    How it works:
    Calculate some field (i.e. dataframe column) transformations
    and store the results in 'columns_to_add' and 'columns_to_remove'. Then
    apply these changes to the original dataframe to create a new dataframe.
    """

    def __init__(self, datasets, config, outputs, model_type, set_config=True):
        super().__init__(config, CONFIG_DATASET_PREPROCESS)
        self.datasets = datasets
        self.df = datasets.dataset
        self.balance = False
        self.categories = dict()  # Fields to be converted to categorical. Entries are list of categories
        self.category_column = dict()  # Store Pandas categorical definition
        self.columns_to_add = dict()
        self.columns_to_remove = set()
        self.dates = list()  # Convert these fields to dates and expand to multiple columns
        self.impute = dict()
        self.is_sanitize_column_names = True
        self.model_type = model_type
        self.na_columns = set()  # Columns added as 'missing data' indicators
        self.normalize = dict()
        self.normalize_df = None
        self.one_hot = list()  # Convert these fields to 'one hot encoding'
        self.one_hot_max_cardinality = 7
        self.outputs = outputs
        self.remove_missing_outputs = True
        self.remove_columns = list()
        self.remove_columns_after = list()
        self.remove_equal_inputs = True    # Remove columns having the exact same values
        self.skip_nas = set()  # Skip doing "missing data" on these columns (they have been covered somewhere else, e.g. one-hot)
        self.shuffle = False    # Shuffle samples
        self.std_threshold = 0.0  # Drop columns of stddev is less or equal than this threshold
        if set_config:
            self._set_from_config()

    def _add_datepart(self, field_name, prefix=None, time=True):
        """
        Helper function that creates a new dataframe with all columns relevant to
        a date in the column `field_name`.
        Source: fast.ai
        """
        has_na = self.df[field_name].isnull().sum() > 0
        self._info(f"Converting to date: field '{field_name}', dtype: {self.df[field_name].dtype}, has NA / NaT / Null: {has_na}")
        self._make_date(self.df, field_name)
        field = self.df[field_name]
        prefix = prefix if prefix else re.sub('[Dd]ate$', '', field_name)
        attr = ['year', 'month', 'week', 'day', 'dayofweek', 'dayofyear', 'is_month_end', 'is_month_start',
                'is_quarter_end', 'is_quarter_start', 'is_year_end', 'is_year_start']
        if time:
            attr = attr + ['hour', 'minute', 'second']
        df = pd.DataFrame()
        for n in attr:
            dpname = f"{prefix}:{n}"
            xi = getattr(field.dt, n.lower())
            if pd.api.types.is_bool_dtype(xi):
                xi = bool_to_int_or_float(xi)
            df[dpname] = xi
            self._info(f"Adding date part '{dpname}', type {df[dpname].dtype}, for field '{field_name}'")
        dpname = f"{prefix}:elapsed"
        df[dpname] = datetime_to_elapsed(field)
        self._info(f"Adding date part '{dpname}', type {df[dpname].dtype}, for field '{field_name}'")
        # Add to replace and remove operations
        self.columns_to_add[field_name] = df
        self.columns_to_remove.add(field_name)
        self.skip_nas.add(field_name)

    def _balance(self):
        """ Balance an unbalanced dataset """
        if not self.balance:
            self._debug(f"Balance: Disabled, skipping")
            return True
        if self.model_type != MODEL_TYPE_CLASSIFICATION:
            self._debug(f"Balance: Cannot balance for model type '{self.model_type}', skipping")
            return False
        y = self.df[self.outputs]
        uniq, counts = np.unique(y, return_counts=True)
        self._debug(f"Balance: Start, dataframe shape: {self.df.shape}, percents: {counts / counts.sum()}, counts: {counts}, cathegories: {uniq}")
        counts_max = counts.max()
        for u, c in zip(uniq, counts):
            y = self.df[self.outputs]
            num = counts_max - c
            self._debug(f"Balance: Adding samples for outputs={u}, count={c}, to add: {num}, dataframe shape: {self.df.shape}")
            if num == 0:
                continue
            replace = num > c
            weights = (y == u).astype('float').to_numpy().flatten()
            rows_to_add = self.df.sample(n=num, replace=replace, weights=weights)
            self.df = pd.concat([self.df, rows_to_add])
        y = self.df[self.outputs]
        uniq, counts = np.unique(y, return_counts=True)
        self._debug(f"Balance: End, dataframe shape: {self.df.shape}, percents: {counts / counts.sum()}, counts: {counts}, cathegories: {uniq}")

    def __call__(self):
        """
        Preprocess dataframe columns
        Returns a new (preprocessed) dataset
        """
        if not self.enable:
            self._debug(f"Preprocessing dataframe disabled, skipping. Config file '{self.config.config_file}', section '{CONFIG_DATASET_PREPROCESS}', enable='{self.enable}'")
            return self.df
        self._debug(f"Preprocessing dataframe: Start. Shape: {self.df.shape}")
        self._sanitize_column_names()
        self._shuffle()
        self._remove_columns()
        self.convert_dates()
        self.create_categories()
        self.set_df(self.create())  # We have to create the new df before replacing nas, in case the dates have missing data
        self.set_df(self._remove_rows_with_missing_outputs())
        self.nas()
        self.set_df(self.create())
        self._remove_columns_after()
        self.drop_all_na()
        self.drop_zero_std()
        self.drop_equal_inputs()
        self.impute_df = DfImpute(self.df, self.config, self.outputs, self.model_type)
        self.df = self.impute_df()
        self.normalize_df = DfNormalize(self.df, self.config, self.outputs, self.model_type)
        self.df = self.normalize_df()
        self._balance()
        self._debug(f"Preprocessing dataframe: End. Shape: {self.df.shape}")
        return self.df

    def create(self):
        """ Create a new dataFrame based on the previously calculated conversions """
        self._debug(f"Creating transformed dataset: Start")
        # Create new dataFrame
        df_new = self.df.copy()
        # Drop old columns categorical columns
        df_new.drop(list(self.columns_to_remove), axis=1, inplace=True)
        # Join new columns
        if len(self.columns_to_add) > 0:
            dfs = list([df_new])
            dfs.extend(self.columns_to_add.values())
            df_new = pd.concat(dfs, axis=1)
        self.df_new = df_new
        # Reset columns
        self.columns_to_add = dict()
        self.columns_to_remove = set()
        self._debug(f"Creating transformed dataset: End. Shape: {df_new.shape}")
        return df_new

    def create_categories(self):
        """
        Create categories as defined in YAML file.
        This creates both number categories as well as one_hot encoding
        """
        create_cats = CategoriesPreprocess(self.df, self.categories, self.outputs, self.dates, self.one_hot, self.one_hot_max_cardinality)
        create_cats()
        self.columns_to_add.update(create_cats.columns_to_add)
        self.columns_to_remove.update(create_cats.columns_to_remove)
        self.category_column.update(create_cats.category_column)
        self.na_columns.update(create_cats.na_columns)
        self.skip_nas.update(create_cats.skip_nas)

    def convert_dates(self):
        """ Convert all dates """
        self._debug(f"Converting to 'date/time' values: fields {self.dates}")
        count = 0
        for field in self.dates:
            self._add_datepart(field)
        self._debug(f"Converting to 'date/time' values: End")

    def drop_all_na(self):
        " Drop features that have all 'na' values "
        self._debug(f"Dropping columns with all missing values: Start")
        to_remove = list()
        for c in self.df.columns:
            if self.df[c].isnull().all():
                self._info(f"Dropping column '{c}', all values are missing, values head: {list(self.df[c].head())}")
                to_remove.append(c)
        self.df.drop(to_remove, axis=1, inplace=True)
        self._debug(f"Dropping columns with all missing values: End")

    def drop_equal_inputs(self):
        " Remove input columns having the exact same values "
        if not self.remove_equal_inputs:
            self._debug(f"Dropping columns with exact same values disables, skipping")
            return
        self._debug(f"Dropping columns with exact same values: Start")
        column_hash = dict()
        to_remove = list()
        for c in self.df.columns:
            # Create a hash of column 'c'
            c_hash = hashlib.sha256(pd.util.hash_pandas_object(self.df[c], index=True).values).hexdigest()
            if c_hash in column_hash:
                self._info(f"Dropping column '{c}', same values as column {column_hash[c_hash]}, hash {c_hash}")
                to_remove.append(c)
            else:
                column_hash[c_hash] = c
        self.df.drop(to_remove, axis=1, inplace=True)
        self._debug(f"Dropping columns with exact same values: End")

    def drop_zero_std(self):
        " Drop features that have standard deviation below a threshold "
        self._debug(f"Dropping columns with low standard deviation: Start, std_threshold: {self.std_threshold}")
        to_remove = list()
        for c in self.df.columns:
            if not pd.api.types.is_numeric_dtype(self.df[c]):
                continue
            stdev = self.df[c].std()
            if stdev <= self.std_threshold:
                self._info(f"Dropping column '{c}', standard deviation {stdev} <= {self.std_threshold}, values head: {list(self.df[c].head())}")
                to_remove.append(c)
        self.df.drop(to_remove, axis=1, inplace=True)
        self._debug(f"Dropping columns with low standard deviation: End")

    def _make_date(self, df, date_field):
        " Make sure `df[field_name]` is of the right date type. Source: fast.ai "
        field_dtype = df[date_field].dtype
        if isinstance(field_dtype, pd.core.dtypes.dtypes.DatetimeTZDtype):
            field_dtype = np.datetime64
        if not np.issubdtype(field_dtype, np.datetime64):
            df[date_field] = pd.to_datetime(df[date_field], infer_datetime_format=True)
            self._debug(f"Converted field '{date_field}' to {df[date_field].dtype}")

    def nas(self):
        " Add 'missing value' indicators (i.e. '*_na' columns) "
        self._debug(f"Checking NA (missing values): Start")
        for field_name in self.df.columns:
            if field_name not in self.skip_nas:
                self.na(field_name)
        self._debug(f"Checking NA (missing values): End")

    def na(self, field_name):
        " Process 'na' columns (i.e missing data) "
        # Add '*_na' column
        xi = self.df[field_name].copy()
        xi_isna = xi.isna().astype('int8')
        count_na = sum(xi_isna)
        if count_na == 0:
            return False
        df_na = pd.DataFrame()
        name_na = f"{field_name}_na"
        self.na_columns.add(name_na)
        df_na[field_name] = xi
        df_na[name_na] = xi.isna().astype('int8')
        self._info(f"Field '{field_name}' has {count_na} missing values, added column '{name_na}'")
        # Add operations
        self.columns_to_add[field_name] = df_na
        self.columns_to_remove.add(field_name)
        self.skip_nas.add(field_name)
        return True

    def _remove_columns(self):
        """ Remove columns (before transformations) """
        self.df_ori = self.df
        self._info(f"Removing columns (before): {self.remove_columns}")
        self.df.drop(self.remove_columns, inplace=True, axis=1)

    def _remove_columns_after(self):
        """ Remove columns (after transformations) """
        self.df_ori = self.df
        self._info(f"Removing columns (after): {self.remove_columns_after}")
        self.df.drop(self.remove_columns_after, inplace=True, axis=1)

    def _remove_rows_with_missing_outputs(self):
        """ Remove rows if output variable/s have missing values c"""
        if not self.remove_missing_outputs:
            self._debug("Remove missing outputs disabled, skipping")
            return
        self._debug(f"Remove samples with missing outputs: Start, outputs: {self.outputs}")
        for n in self.outputs:
            self.datasets.remove_samples_if_missing(n)
        return self.datasets.dataset
        self._debug(f"Remove samples with missing outputs: End")

    def _sanitize_column_names(self):
        """ Sanitize all column names """
        if not self.is_sanitize_column_names:
            self._debug("Sanitize column names disabled, skipping")
            return
        self._info("Sanitize column names")
        cols_ori = list(self.df.columns)
        cols_new = [sanitize_name(c) for c in self.df.columns]
        self.df.columns = cols_new
        for i in range(len(self.df.columns)):
            if cols_new[i] != cols_ori[i]:
                self._info(f"Sanitize column names: Changed '{cols_ori[i]}' to '{cols_new[i]}'")

    def set_df(self, df):
        self.df = df
        self.datasets.dataset = self.df
        return self.df

    def _shuffle(self):
        """ Shuffle samples in dataset """
        if not self.shuffle:
            self._debug(f"Shuffle samples: Disabled, skipping")
            return True
        self._debug(f"Shuffle samples: Start")
        # The following line samples 100 of the rows 'in place' without replacement
        # Reference: https://stackoverflow.com/questions/29576430/shuffle-dataframe-rows
        # Note: Resset with `drop=True` means not to create a new columns having the old index data
        self.df = self.df.sample(frac=1).reset_index(drop=True)
        self._debug(f"Shuffle samples: End")
        return True


import logging
import logging.handlers
import json

NeedsInititializing=True

class Llog(object):

    def __init__(self, nameV):
        global NeedsInititializing
        self.logger = logging.getLogger(nameV)
        self.logger.setLevel(logging.DEBUG)
        if NeedsInititializing:
            logfile = '/data/log/' + nameV + '.log'
            logserver = get_syslog()
            fh = logging.FileHandler(logfile)
            fh.setLevel(logging.DEBUG)
            # create console handler with a higher log level
            ch = logging.StreamHandler()
            ch.setLevel(logging.ERROR)
            syslogh = logging.handlers.SysLogHandler(address = (logserver,514), facility='local3')
            syslogh.setLevel(logging.INFO)
            # create formatter and add it to the handlers
            formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
            ch.setFormatter(formatter)
            fh.setFormatter(formatter)
            syslogh.setFormatter(formatter)
            # add the handlers to logger
            self.logger.addHandler(ch)
            self.logger.addHandler(fh)
            self.logger.addHandler(syslogh)
            NeedsInititializing=False

        self.logger.info("Logging Begins")

    def logd(self, obj):
        self.logger.debug(obj)

    def logi(self, obj):
        self.logger.info(obj)

def get_syslog():
    file = '/data/home/dogBreed.json'
    with open(file) as data_file:
        data = json.load(data_file)
    return(data['syslog'])


# ##### ######################### #
# #             Weatherman Willy Telegram bot          # #
# ############################## #
# ------------------------------------------------------------------------------
# Author: Ruben Wylleman
#
# Functions:
# - OpenWeatherAPI_5D_Forecast
# - OpenWeatherAPI_Current
# - Telegram API (main function)
# - - Messagehandler start
# - - Messagehandler current
# - - Messagehandler forecast
# - - Messagehandler end
#
# Usage:
# upon receiving a start command, the bot will demand if you wan't the current or forecast weather.
# 2 buttons will appear instead of the keyboard. once the user has selected,
# the buttons will disapear and the user can fill in a city name.
# Upon receiving the city name, the main fuction 'TelegramAPI' will call another function OpenWeatherAPI_Current or OpenWeatherAPI_Forecast.
#

# This bot will allow the user to receive live weather information for the requested region.

# the script starts in the TelegramAPI function upon running

import requests
import telebot
import time
from telebot import types

# the OpenWeatherAPI function will make the call to the openweather API online and parse the received data.

def OpenWeatherAPI_Current(city):
    url = 'http://api.openweathermap.org/data/2.5/weather?q={}&units=metric&appid=4fdb0f3096ed32490c0658bad4fdecdf' .format(city)
    res = requests.get(url)
    data = res.json()

    temp = data['main']['temp']
    #pressure = data['main']['pressure']
    weather = data['weather'][0]['description']
    wind_speed = data['wind']['speed']

    return("The weather in {} is '{}' with a windspeed of {}m/s. The average temperature is {}°C ".format(city, weather, wind_speed, temp))

# The OpenWeatherAPI_5D_Forecast will get the weatherforecast for 5 days for the demanded city.
# this function will return a string with weather, date time, wind speed and temperature.
def OpenWeatherAPI_5D_Forecast(city):
    url='http://api.openweathermap.org/data/2.5/forecast?q={}&units=metric&mode=json&appid=4fdb0f3096ed32490c0658bad4fdecdf' .format(city)
    res = requests.get(url)
    data = res.json() # contains raw data
    Weather_Data_15H = []
    tempAVG_array = []

# Upon receiving the data, we get multiple value's for each day. We will only take the value's for 15H of each day.
# To filter this we need multiple array's with the requested data.

    weather_array = []
    wind_speed_array = []
    date_time = []

    complete_data_list = data['list'] # will reduce the raw data to only raw data under 'list'

    for item in complete_data_list: # for every item in our filtered list, we only want the data where in the string dt_txt 15:00:00 is mentioned
        item_string = item['dt_txt']
        time_test = item_string.find("15:00:00")
        if time_test > 0: # the .find function returns a positive value if there is a hit, and a negative value if there was no hit.
            Weather_Data_15H.append(item) # we add all usefull data in a separate table

    for item in Weather_Data_15H: # from this table, we'll extractthe required info, and prepare it for presentation
        date_time.append(item['dt_txt'])
        tempAVG_array.append(item['main']['temp'])
        weather_array.append(item['weather'][0]['description'])
        wind_speed_array.append(item['wind']['speed'])

    #print (date_time)
    #print (tempAVG_array)
    #print (weather_array)
    #print (wind_speed_array)

    return_string = f"the 5 day weather forecast for {city}: \n \n"
    index_stop = len(date_time) - 1
    index = 0
    while index <= index_stop:
        return_string += f"{date_time[index]}   average temp.: {tempAVG_array[index]}°C    windspeed: {wind_speed_array[index]} m/s  weather: {weather_array[index]}\n"
        index = index + 1
    return(return_string)

# The TelegramAPI function will handle the Telegram communications. Also the script will start in this function.
def TelegramAPI():
    bot_token = "861043992:AAGPOQ752HIEl_VzaJB24eExXb1i1K4xztI"
    bot = telebot.TeleBot(token=bot_token)
    global sel_type
    sel_type = "none"

# the 'start' message handler will initiate the conversation with the user. the keyboard will change layout to force the user only 2 answers
    @bot.message_handler(commands=['start'])
    def send_welcome(message):
        WelcomeText = "Welcome to Willy's Weather Bot, i will lookup the weather for you for your desired location.\nPlease select if you wan't the current weather or the forecast."
        kb_markup = types.ReplyKeyboardMarkup(resize_keyboard = True)
        kb_markup.add('/current','/forecast')
        bot.send_message(message.chat.id, WelcomeText,  reply_markup=kb_markup)

# the 'end' message handler will react on the end command. this allows the user to reset the conversation in case he needs some explanation again.
    @bot.message_handler(commands=['end'])
    def end_standby(message):
        text = "press start to begin."
        kb_markup = types.ReplyKeyboardMarkup(resize_keyboard = True)
        kb_markup.add('/start')
        bot.send_message(message.chat.id,text,  reply_markup=kb_markup)

# some tests with inline keyboards, not yet succesfull

#        kb_markup = types.ReplyKeyboardRemove()
#        buttons = []
#        startbutton = types.InlineKeyboardButton(text="start",  callback_data='/start')
#        buttons.append(startbutton)
#        keyboard = [buttons]
#        kb_inline = types.InlineKeyboardMarkup(keyboard)
#        bot.send_message(message.chat.id, text="resetting...",  reply_markup=kb_markup)
#        bot.send_message(message.chat.id,  text="select start to begin.",  reply_markup=kb_inline)

# the 'current' command handler will respond when the user clicks the current weather option.
# this function will set the flag for the global variable sel_type which will be used in the send_weatherreport function.
# after execution of this function, the keyboard will be reset to the default keyboard.
    @bot.message_handler(commands=['current'])
    def send_current_weatherreport(message):
        global sel_type
        sel_type = "sel_current"
        reset_keyboard = types.ReplyKeyboardRemove()
        bot.send_message(message.chat.id, "please enter the name of the city.",  reply_markup=reset_keyboard)

# the 'forecast' command handler will respond like the 'current' command handler, only will he set the flag to forecast.
# after execution of this function, the keyboard will be reset to the default keyboard.
    @bot.message_handler(commands=['forecast'])
    def send_forecast_weatherreport(message):
        global sel_type
        sel_type = "sel_forecast"
        reset_keyboard = types.ReplyKeyboardRemove()
        bot.send_message(message.chat.id, "please enter the name of the city.",  reply_markup=reset_keyboard)

#This message handler will read in the message received from the user. Then the function will look at the flag setting.
# If the flag was set to 'sel_current' then the OpenWeatherAPI_Current will be called and return a respons.
# if the flag was set to 'sel_forecast' then the OpenWeatherAPI_Forecast will be called and return a respons.
# After receiving the respons, this respons is sent back to the user.
# the keyboard will be set to '/current', '/forecast' or '/end'.
# the 'current' and 'forecast' allows the user to enter another query.
# The 'end' command will return the user to the beginning of the script.

    @bot.message_handler(func=lambda msg: msg.text is not None)
    def send_weatherreport(message):
        cityname = message.text
        if sel_type == "sel_current":
            try:
                response = OpenWeatherAPI_Current(cityname)
            except Exception:
                response = "i'm sorry, but this city doesn't seem to exist in our database."

        if sel_type == "sel_forecast":
            try:
                response = OpenWeatherAPI_5D_Forecast(cityname)
            except Exception:
                response = "i'm sorry, but this city doesn't seem to exist in our database."

        kb_markup = types.ReplyKeyboardMarkup(resize_keyboard = True)
        kb_markup.add('/current',  '/forecast', '/end')
        bot.send_message(message.chat.id, response,  reply_markup = kb_markup)

    while True:
      try:
        bot.polling()
      except Exception:
          time.sleep(15)

 # this forces the program to start in the TelegramAPI function.

if __name__ == '__main__':
    TelegramAPI()


import cv2
import numpy as np
import face_data_collect
import dlib
import face_recognition
import detect_face_recognition as dfr
def rect_to_bbox(rect):
    """获得人脸矩形的坐标信息"""
    # print(rect)
    x = rect[3]
    y = rect[0]
    w = rect[1] - x
    h = rect[2] - y
    return (x, y, w, h)

recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('trainer.yml')
face_cascade = cv2.CascadeClassifier('.\opencv_module\haarcascade_frontalface_default.xml')
font = cv2.FONT_HERSHEY_SIMPLEX

idnum = 0

# cam = cv2.VideoCapture(0)
# minW = 0.1*cam.get(3)#图像宽度
# minH = 0.1*cam.get(4)#图像高度

def face_rec(camera):
    # 下面读取摄像头图像，用矩形标识检测到脸部和训练后结果比对，打印出对应标签所对应名字

    with open('pace_names.txt', 'r') as f:
        face_names = f.read()
    names = face_names.split()
    #
    # minW = 0.1 * camera.get(3)  # 图像宽度
    # minH = 0.1 * camera.get(4)  # 图像高度
    # print(minH,minW)

    while(True):
        read, img = camera.read()
        img = np.fliplr(img).copy()

        # face_locations = face_recognition.face_locations(img)  # 定位图片中的人脸
        # src_faces = []
        # src_face_num = 0
        # for (i, rect) in enumerate(face_locations):
        #     src_face_num = src_face_num + 1
        #     (x, y, w, h) = rect_to_bbox(rect)#读取单个人脸的初始坐标与宽度，高度
        #     detect_face = img[y:y + h, x:x + w]#截取出人脸部分
        #     detect_face = cv2.cvtColor(detect_face, cv2.COLOR_BGR2GRAY)#灰度化
        #     src_faces.append(detect_face)
        #     # cv2.imshow("face", detect_face)
        #     # 用框框圈住人脸，注意位置偏移
        #     img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
        #     # cv2.imshow("image", img)
        # # 人脸对齐操作并保存
        # faces_aligned = dfr.face_alignment(src_faces)
        # face_num = 0
        # for roi_gray in faces_aligned:
        #     face_num = face_num + 1
        #     # cv2.imshow("Align_face", roi_gray)

        # #harr方法检测人脸
        #由于读取的摄像头大小为640x480，所以设置人脸最小为60x60
        faces = face_cascade.detectMultiScale(img, 1.3, 5,minSize=(60,60))
        for (x, y, w, h) in faces:
            img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            roi_gray = gray[y:y + h,x:x + w]#注意别弄错了位置

            try:
                roi_gray = cv2.resize(roi_gray, (200,200), interpolation=cv2.INTER_LINEAR)
                params = recognizer.predict(roi_gray)
                print("name:%s,Confidence:%.2f" % (names[params[0]], params[1]))
                if params[1] < 70:#低于50以下是好的识别参考值，高于80的参考值都会认为是低的置信度评分
                    cv2.putText(img, names[params[0]],(x,y-20),cv2.FONT_HERSHEY_SIMPLEX, 1,255,2)
                    cv2.putText(img, "Confidence:"+str((int)(params[1])), (0, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2)
            except:
                continue

        if cv2.waitKey(int(1000/12)):
            break
    return img

if __name__ == "__main__":
    camera = cv2.VideoCapture(0)
    face_rec(camera)


import threading
import time

start = time.perf_counter()

def do_something():
    print('Sleeping 1 second...')
    time.sleep(1)
    print('Done Sleeping...')

t1 = threading.Thread(target=do_something)
t2 = threading.Thread(target=do_something)

t1.start()
t2.start()

t1.join() # complete the running thread
t2.join()

finish = time.perf_counter()

print(f'Finished in {round(finish-start, 2)} second(s)')

# Finished in 0.0 second(s) 라고 뜬 후 Done Sleeping... 이 프린트됨
# reason : while the tread is for sleeping, our script ran concurrently and continued on with the rest of the script


import os
from subprocess import call
import sys

[program, base, number] = sys.argv

#ALL_REPOS_FILE ="/Users/himesh/Library/CloudStorage/OneDrive-DalhousieUniversity/Thesis/Mootex/run2/eval/final_dataset.csv"
ALL_REPOS_FILE =base+"temp.csv"

REPO_STORE_ROOT = base+"output"
REPOS_TO_DOWNLOAD=int(number)

def downloadRepo(repoName):
    fullRepoName = repoName + ".git"
    folderName = repoName.replace("/", "_")
    if not os.path.isdir(os.path.join(REPO_STORE_ROOT, folderName)):
        os.mkdir(folderName)
        print(folderName)
        try:
            call(["git", "clone", "--depth=1", fullRepoName, folderName])
        except:
            print("Exception occurred!!")
            pass

file = open(ALL_REPOS_FILE, 'rt', errors='ignore')
if not os.path.isdir(REPO_STORE_ROOT):
    os.mkdir(REPO_STORE_ROOT)
os.chdir(REPO_STORE_ROOT)
for line in file.readlines()[:REPOS_TO_DOWNLOAD]:
    repo_name = line.strip('\n').split(',')[0]
    if not repo_name == "":
        folder_name = repo_name.replace("/", "_")
        folder_path = os.path.join(REPO_STORE_ROOT, folder_name)
        if not os.path.isdir(folder_path):
            print('downloading ' + folder_name)
            downloadRepo(repo_name)
print('Done.')


from django.contrib.auth import login
from django.contrib.auth.forms import AuthenticationForm
from django.shortcuts import render, redirect
from django.template.context_processors import csrf
from django.contrib import auth
from django.contrib.auth.models import User
from django.core.paginator import Paginator

from .models import *

def news(request):
    all_news = Article.objects.filter(article_is_it_history=False).order_by('-article_date')
    if request.user.is_authenticated():
        return render(request, 'news.html', {'all_news': all_news})

    if request.method == 'POST':
        form = AuthenticationForm(request, request.POST)
        if form.is_valid():
            login(request, form.get_user())
            return render(request, 'news.html', {'all_news': all_news})
    else:
        form = AuthenticationForm(request)
    return render(request, 'news.html', {'all_news': all_news})

def history(request, page_namber = 1):
    all_news = Article.objects.filter(article_is_it_history = True).order_by('-article_date')

    #Передана ди информация для поска
    if request.POST:
        import re
        from datetime import datetime
        import time
        search = request.POST.get('search','')
        all_news_old = all_news
        all_news = []
        for news in all_news_old:
            flag = False
            #поиск по названию
            if re.findall(search, news.article_title) != [] and flag == False:
                all_news.append(news)
                flag = True
            #поиск по тексту
            if re.findall(search, news.article_text) != [] and flag == False:
                all_news.append(news)
                flag = True
            date_str = news.article_date.strftime("%Y-%m-%d %H:%M:%S")
            #поиск по дате
            if re.findall(search, date_str) != [] and flag == False:
                all_news.append(news)
                flag = True
    else:
        search = 'Поиск'

    all_news_for_page = Paginator(all_news, 10)
    return render(request, 'history2.html', {'all_news': all_news_for_page.page(page_namber), 'search': search})

def article(request, page_id):
    news = Article.objects.get(id=page_id)
    lectors = Lecturer.objects.get(id=news.article_lecturer_id)
    lector_1 = lectors.lecturer_surname
    lector_2 = lectors.lecturer_name
    lector_3 = lectors.lecturer_patronymic
    lector_id = lectors.id
    return render(request, 'viewer.html', {'news': news, 'lector_1': lector_1,
                                           'lector_2': lector_2, 'lector_3': lector_3,
                                           'lector_id': lector_id})

def profile(request):
    return render(request, 'profile.html', {})

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

def books(request):
    return render(request, 'books.html', {})

def orgkom(request):
    orgkom_all = Lecturer.objects.filter(lecturer_orgkom = True)
    return render(request, 'orgkom.html', {'orgkom_all': orgkom_all})

def infolector(request, lector_id):
    lector = Lecturer.objects.get(id=lector_id)
    return render(request, 'infolector.html', {'lector': lector})


import sys, frida, socket

sc = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

def on_message(message, data):
    if message['type'] == 'send':
        try:
            payload = str(message['payload']) + '\n'
            sc.sendto(payload.encode(), ('127.0.0.1', 5572))
            print(str(message['payload']) + '\n')
        except:
            print('error');
    elif message['type'] == 'error':
        try:
            print(str(message['stack']) + '\n')
        except:
            print('error');
    else:
        print("something...")

jscode = '''
'''

if __name__ == "__main__":
    print("[*] Start Process ...")
    PACKAGE_NAME = sys.argv[1]

    try:
        process = frida.get_usb_device().attach(PACKAGE_NAME)
        script = process.create_script(jscode)
        script.on('message', on_message)
        script.load()
        sys.stdin.read()

    except Exception as error:
        print(error)


import os

from yacs.config import CfgNode as CN

_C = CN()
# _C.NAME = 'sgd000105_trans_adam'
# _C.NAME = 'sgd000105_trans'
# _C.NAME = 'sgd000105no_trans'

# _C.NAME = 'model2_no_trans'
# _C.NAME = 'model2_no_trans_focal'
_C.NAME = 'other_focal01_to1_pool'
_C.DESCRIPTION = ''
# _C.NAME = 'model1_focal01_to1_pool'
_C.NAME = 'pretrain_focal01_to1_pool'
_C.NAME = 'pretrain_fo_pool'
_C.PRETRAINED = False
_C.PRETRAIN_MODEL = './pretrained_model/pretrain_32000_freeze.pth'
_C.FOCAL_LOSS_ALPHA = [0.1, 5, 2, 2, 2]
# _C.NAME = 'first'
_C.OUTPUT_DIR = os.path.join('./output', _C.NAME)
_C.DEVICE = 'cuda'
_C.BACKBONE = 'resnet34'
# 这个不能变，如果变需要重新定义模型
_C.NUM_FEATURES = 1000
_C.NUM_CATEGORY = 4
_C.FREEZE_CONV_BODY_AT = 2

_C.INPUT = CN()
# Size of the smallest side of the image during training
_C.INPUT.MIN_SIZE_TRAIN = (448, )  # (800,)
# Maximum size of the side of the image during training
_C.INPUT.MAX_SIZE_TRAIN = 512
# Size of the smallest side of the image during testing
_C.INPUT.MIN_SIZE_TEST = 448
# Maximum size of the side of the image during testing
_C.INPUT.MAX_SIZE_TEST = 512
# Values to be used for image normalization
# _C.INPUT.PIXEL_MEAN = [105.0051/255, 73.4359/255, 52.3038/255]
_C.INPUT.PIXEL_MEAN = [0.485, 0.456, 0.406]
# Values to be used for image normalization
_C.INPUT.PIXEL_STD = [0.229, 0.224, 0.225]
# _C.INPUT.PIXEL_STD = [1., 1., 1.]
# Convert image to BGR format (for Caffe2 models), in range 0-255
_C.INPUT.TO_BGR255 = False
# Image ColorJitter
_C.INPUT.BRIGHTNESS = 0.00
_C.INPUT.CONTRAST = 0.00
_C.INPUT.SATURATION = 0.00
_C.INPUT.HUE = 0.00

# Flips
_C.INPUT.HORIZONTAL_FLIP_PROB_TRAIN = 0.5
_C.INPUT.VERTICAL_FLIP_PROB_TRAIN = 0.0

# Dataset
# -----------------------------------------------------------------------------
_C.DATASETS = CN()
# List of the dataset names for training, as present in paths_catalog.py
_C.DATASETS.TRAIN = 'train'
# List of the dataset names for testing, as present in paths_catalog.py
_C.DATASETS.TEST = 'val'

# Solver
# ---------------------------------------------------------------------------- #
_C.SOLVER = CN()
# pretrain
_C.SOLVER.EPOCH = 30
# 正常训练
# _C.SOLVER.EPOCH = 30
# _C.SOLVER.MAX_ITER = 15000
_C.SOLVER.BASE_LR = 0.005
_C.SOLVER.BIAS_LR_FACTOR = 2

_C.SOLVER.MOMENTUM = 0.9

_C.SOLVER.WEIGHT_DECAY = 0.0005
_C.SOLVER.WEIGHT_DECAY_BIAS = 0

_C.SOLVER.GAMMA = 0.2
# 这个数字比较难定，会和IMS_PER_BATCH有关系
# _C.SOLVER.STEPS = (2000, 5000)
_C.SOLVER.STEPS = (3000, 7000, 11000)
_C.SOLVER.WARMUP_FACTOR = 1.0 / 3
_C.SOLVER.WARMUP_ITERS = 500
_C.SOLVER.WARMUP_METHOD = "linear"

# _C.SOLVER.CHECKPOINT_PERIOD = 10000

_C.SOLVER.IMS_PER_BATCH = 16


'''

Question 7. Reverse Integer

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output:  321
Example 2:

Input: -123
Output: -321
Example 3:

Input: 120
Output: 21
Note:
Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range.
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
'''

'''
Resources :
    1.

'''

'''
Solution :
    1. Brute Force :
        - Time Complexity - O(n)
        - Run Time : 48 ms
'''

class Solution(object):
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """

        # if number is between -9 to +9, reversing will yield same value
        if abs(x) < 10:
            return x

        # save the sign
        sign = 1 if x >= 0 else -1
        # make x positive. If it is negative, muliplying with -1 will give positive.
        x = sign * x

        # make number a string
        x = str(x)

        # reverse the string
        x = x[::-1]

        # convert the number into  integer and multiply with sign
        x = sign * int(x)

        # return x if the integer is not overflow else return 0.
        return 0 if x >= 2147483647 or x <= (-2147483647 - 1) else x


from output import to_textfile
from db import save_data

def test_save_to_textfile(data):
    for el in data:
        to_textfile('all_tbody.txt', '{} [{}] | {} {} {}\n'.format(
            el['bank'],
            el['money'],
            el['currency'],
            el['buy'],
            el['sale'],
        ))
    # to_textfile('all_tbody.txt', '{}'.format(data))

def test_save_to_db():
    data = []
    data.append({
        'bank': 'bank name cxc Аль',
        'money': 'наличные',
        'currency': 'RUR',
        'buy': 350,
        'sale': 349
        })
    data.append({
        'bank': 'bank name cxc Аль',
        'money': 'наличные',
        'currency': 'RUR',
        'buy': 353,
        'sale': 349
    })
    save_data(data)