import datetime

class Bugzilla:
	"""
	Default class for a Red Hat's Bugzilla listing.

	Attributes
	------------
	description: :class:`str`
		Description of the bug in Red Hat's Bugzilla.
	id: :class:`str`
		ID of the bug in Red Hat's Bugzilla.
	url: :class:`str`
		URL of the bug in Red Hat's Bugzilla.
	"""
	def __init__(self, data):
		self.description = data['description'].strip()
		self.id = data['id']
		self.url = data ['url']

class CVSSv2:
	"""
	Default class for CVSSv2 metrics of a CVE.

	Attributes
	------------
	base_score: :class:`float`
		CVSSv2 score of the CVE.
	scoring_vector: :class:`str`
		CVSSv2 scoring vector for the CVE.
	status: :class:`str`
		Indicates how far along the investigation of the flaw has progressed.
	"""
	def __init__(self, data):
		self.base_score = float(data['cvss_base_score'])
		self.scoring_vector = data['cvss_scoring_vector']
		self.status = data['status']

class CVSSv3:
	"""
	Default class for CVSSv3 metrics of a CVE.

	Attributes
	------------
	base_score: :class:`float`
		CVSSv3 score of the CVE.
	scoring_vector: :class:`str`
		CVSSv3 scoring vector for the CVE.
	status: :class:`str`
		Indicates how far along the investigation of the flaw has progressed.
	"""
	def __init__(self, data):
		self.base_score = float(data['cvss3_base_score'])
		self.scoring_vector = data['cvss3_scoring_vector']
		self.status = data['status']

class Release:
	"""
	Default class for a package release.

	Attributes
	------------
	product_name: :class:`str`
		Name of the product the package is for.
	release_date: :class:`str`
		Release date of the package (ISO 8601 format).
	advisory: :class:`str`
		Package advisory.
	cpe: :class:`str`
		CPE string for the package.
	package_name: Optional[:class:`str`]
		Name of the package that fixes the flaw.
	"""
	def __init__(self, data):
		self.product_name = data['product_name']
		self.release_date = data['release_date']
		self.advisory = data['advisory']
		self.cpe = data['cpe']
		self.package_name = data.get('package_name', None)

class PackageState:
	"""
	Default class for a package state for a package that hasn't been fixed yet

	Attributes
	------------
	product_name: :class:`str`
		Name of the product which uses the package.
	fix_state: :class:`str`
		State of fixing, can be one of these values: 'Affected', 'Fix deferred', 'New', 'Not affected', or 'Will not fix'.
	package_name: :class:`str`
		Name of the package that hasn't been fixed.
	cpe: :class:`str`
		CPE string for the package.
	"""
	def __init__(self, data):
		self.product_name = data['product_name']
		self.fix_state = data['fix_state']
		self.package_name = data['package_name']
		self.cpe = data['cpe']

class CVE:
	"""
	The default class for a CVE.

	Attributes
	------------
	name: :class:`str`
		The CVE name.
	threat_severity: :class:`str`
		The severity of the flaw.
	public_date: :class:`str`
		When the flaw became public (ISO 8601 format).
	bugzilla: :class:`~redhat.Bugzilla`
		Redhat Bugzilla listing for this CVE.
	cvss: :class:`~redhat.CVSS`
		CVSSv2 score and metrics for this CVE.
	cwe: :class:`str`
		The CWE chain for this flaw (See the mitre.org description (https://cwe.mitre.org/about/index.html) and the list of possible cwe values (https://access.redhat.com/articles/171613).)
	details: List[:class:`str`]
		Details about the flaw, possibly from Red Hat or Mitre.
	statement: Optional[:class:`str`]
		A statement from Red Hat about the issue.
	affected_releases: Optional[List[:class:`~redhat.Release`]]
		A released Erratum that fixes the flaw for a particular product.
	package_states: List[:class:`~redhat.PackageState`]
		Information about a package / product where no fix has been released yet.

	NOT ADDED YET(DON'T KNOW FORMATTING)
	references:
		Links to more information about the issue.
	acknowledgements:
		People or organizations that are being recognized.
	mitigation:
		A way to fix or reduce the problem without updated software.
	upstream_fix:
		The version of the upstream project that fixes the flaw.
	"""
	def __init__(self, data, passthrough=None):
		self.passthrough = passthrough
		if self.passthrough:
			return
		self.name = data['name']
		self.threat_severity = data.get('threat_severity', None)
		self.public_date = data.get('public_date', None)
		self.bugzilla = Bugzilla(data['bugzilla']) if data.get('bugzilla') else None
		try:
			self.cvss = CVSSv2(data['cvss'])
		except KeyError:
			self.cvss = CVSSv3(data['cvss3']) if data.get('cvss3') else None
		self.cwe = data.get('cwe', None)
		self.details = ' '.join([detail.strip() for detail in data['details']])
		self.statement = data.get('statement', None)
		self.affected_releases = [Release(release) for release in data['affected_release']] if data.get('affected_release') else []
		self.package_states = [PackageState(package_state) for package_state in data['package_state']] if data.get('package_state') else []
		self.in_fork = False

		# self.references = data.get('references', None)
		# self.acknowledgements = data.get('acknowledgements', None)
		# self.mitigation = data.get('mitigation', None)
		# self.upstream_fix = data.get('upstream_fix', None)

	def to_dict(self):
		if self.passthrough:
			output = {
				'CVE ID': self.passthrough,
				'Headline': None,
				'CVSS Score': None,
				'RH Impact': None,
				'RHEL 7': None,
				'RHEL 7.6 EUS': None,
				'RHEL 8': None,
				'Notes': 'A CVE with this ID was not found on the Redhat Database.'
			}
			return output
		output = {
			'CVE ID': self.name,
			'Headline': self.details,
			'CVSS Score': self.cvss.base_score if self.cvss else None,
			'RH Impact': self.threat_severity,
			'RHEL 7 RHSA': None
		}
		all_states = {}
		for state in self.package_states:
			all_states[state.product_name.replace('Red Hat Enterprise Linux', 'RHEL').replace('Extended Update Support', 'EUS')] = state.fix_state
		for release in self.affected_releases:
			pname = release.product_name.replace('Red Hat Enterprise Linux', 'RHEL').replace('Extended Update Support', 'EUS')
			all_states[pname] = 'Fixed'
			if pname == 'RHEL 7':
				date = datetime.datetime.strptime(release.release_date[:10], '%Y-%m-%d')
				if date <= datetime.datetime.strptime('2019-08-05', '%Y-%m-%d'):
					self.in_fork = True
				output['RHEL 7 RHSA'] = release.advisory
		for package in ['RHEL 7', 'RHEL 7.6 EUS', 'RHEL 8']:
			if package in all_states:
				output[package] = all_states[package]
			elif package == 'RHEL 7.6 EUS' and self.in_fork:
				output[package] = 'Fixed'
			elif package == 'RHEL 7.6 EUS' and output['RHEL 7 RHSA'] and not self.in_fork:
				output[package] = "TBD"
			elif package == 'RHEL 7.6 EUS' and all_states.get('RHEL 7') == 'Not affected':
				output[package] = 'Not affected'
			elif all_states.get('RHEL 5') in ('Fixed', 'Not affected') or all_states.get('RHEL 6') in ('Fixed', 'Not affected'):
				output[package] = 'Not affected'
			elif package == 'RHEL 8' and 'RHEL 7' in all_states:
				output[package] = all_states['RHEL 7']
			else:
				if self.threat_severity in ('High', 'Critical', 'Important'):
					output[package] = 'TBD'
				else:
					output[package] = 'Assume WNF'
		if output['RHEL 7.6 EUS'] == 'Fixed':
			output['CDETS State'] = 'Resolved'
			state = 'They indicate that this bug is fixed in'
		elif output['RHEL 7.6 EUS'] == 'Not affected':
			output['CDETS State'] = 'Junk'
			state = 'They indicate that this bug does not affect'
		elif output['RHEL 7.6 EUS'] in ('Assume WNF', 'Will not fix', 'Fix deffered'):
			output['CDETS State'] = 'Closed'
			state = 'They indicate that this bug will not be fixed in'
		elif output['RHEL 7.6 EUS'] in ('Under investigation', 'Affected'):
			output['CDETS State'] = 'Hold'
			state = 'They indicate that this bug is still under investigation for'
		elif output['RHEL 7.6 EUS'] == 'TBD':
			output['CDETS State'] = 'Hold'
			if self.threat_severity in ('Medium', 'Low'):
				state = 'Patch availability is still TBD for'
			else:
				state = 'We can assume that Red Hat will not fix this in'
		if output['RHEL 8'] == 'Fixed':
			cvim = 'The patch is available in RHEL 8 and CVIM will pick it up in version 4.0. '
		else:
			cvim = ''
		output['Comments'] = f'Red Hat rates this CVE with {self.threat_severity} severity. {state} RHEL 7.6 EUS. {cvim}More information can be found online at https://access.redhat.com/security/cve/{self.name}.'
		output['Notes'] = ''
		return output


import json
from typing import Optional
from bs4 import BeautifulSoup
import requests
import re
import argparse

class ObjectScrapper:
	def __init__(self) -> None:
		pass

	def __checkurl(self, asset: str):
		r = requests.get(f"https://statusinvest.com.br/fundos-imobiliarios/{asset}", headers={'User-Agent': 'Mozilla/5.0'})

		links = re.findall(r"link.href.*\b", r.text)

		for link in links:
			if(link.__contains__("error") or link.__contains__("Error Code")):
				raise Exception(f"This asset '{asset}' was not found in Statusinvest")

	def __parse_to_object (self, asset: str, position: Optional[int] = None) ->  dict:
		r = requests.get(f"https://statusinvest.com.br/fundos-imobiliarios/{asset}",headers={'User-Agent': 'Mozilla/5.0'})
		soup = BeautifulSoup(r.text, "html.parser")
		div = soup.find("div", attrs={"class": "mt-5 d-flex flex-wrap flex-lg-nowrap justify-between"})
		asset_name = soup.find("h1", attrs={"class": "lh-4"}).text[:6]
		yelds = div.find_all("strong")
		fields = div.find_all("b", attrs={"class": "sub-value fs-4 lh-3"})

		return {
				f"Asset":asset_name,
				"Position": position,
				"Previous": {
					"Value": float(yelds[0].text.replace(",", ".")),
					"BaseDate": fields[2].text,
					"PaymentDate": fields[3].text
				},
				"Next": {
					"Value": float(yelds[1].text.replace(",", ".")) if yelds[1].text != "-" else 0,
					"BaseDate": fields[6].text,
					"PaymentDate": fields[7].text
				}
			}

	def scrape(self, assets: list[str], positions: list[int], filename: Optional[str] = None) -> None:
		try:
			structure = {
				"Assets": {

				}
			}
			asset_list=[]
			for index, asset in enumerate(assets):
				self.__checkurl(asset)
				asset_list.append(self.__parse_to_object(asset, positions[index]))

			structure["Assets"] = asset_list
			if(filename):
				filename = filename.split(".")[0]+".json"
				with open(filename, "w") as destinaion:
						destinaion.write(json.dumps(structure, indent=4))
			else:
				print(json.dumps(structure, indent=4))

		except Exception as url_error:
			print(url_error)

def main():
	parser = argparse.ArgumentParser(description="Take assets inside StatusInvest and outputs inside a json file where is located")
	parser.add_argument("-a","--assets", nargs="+" ,type=str, metavar="asset_list", help="One or many assets to scrape", required=True)
	parser.add_argument("-f", "--filename", type=str, metavar="filename", help="Outputs the data to a .json File")
	parser.add_argument("-p", "--positions", nargs="+", type=int, help="List of the owned asset position. This must be related with <assets> position", required=True)

	args = parser.parse_args()

	if len(args.assets) != len(args.positions):
		raise Exception(f"Make sure that assets and position have the same amount of elements Assets:{len(args.assets)} Positions:{len(args.positions)}")

	scraper = ObjectScrapper()
	if(not args.output):
		scraper.scrape(args.assets, args.positions)
		return
	scraper.scrape(args.assets, args.positions, args.output)

if __name__ == "__main__":
	main()


"""
    A module that can help to get max performance of your laptop.
    Of course, if user's machine is a desktop it's pretty useless.
"""

import os

from sys import exit
from modules.logger import logger

class PerformanceHandler:
    """
        A class of machine performance handler.

        Attributes
        ----------
        performance_mode: str
            Current mode of cpu performance

        Methods
        -------
        boost(): void
            Boost system performance.
        powersave(): void
            Set powersave mode.
    """

    def __init__(self):
        with open("/sys/devices/system/cpu/cpu1/cpufreq/scaling_governor") as file:
            self.performance_mode = file.read()
        logger.log("Created an instance of the modules.performance_handler.PerformanceHandler class.")

    def boost(self):
        """ Boost machine performance. """
        os.system("echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor")
        self.performance_mode = "performance"

        print("Set performance mode for your CPU.")
        exit()

    def powersave(self):
        """ Set powersave mode. """
        os.system("echo powersave | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor")
        self.performance_mode = "powersave"

        print("Set powersave mode for your CPU.")
        exit()


import os
import numpy as np
import scipy as sp
import scipy.signal as signal
import scipy.fftpack as fft
import scipy.io as io
import scipy.io.wavfile
import matplotlib.pyplot as plt
import rtmidi
from rtmidi import midiconstants
import pyaudio

midiout = rtmidi.MidiOut()
midiout.open_virtual_port('TICS-MIDI')

# Define concert A.
A4_freq = 440
A4_midi = 69
freqs = np.zeros(128, dtype=int)
for i in range(0, 128):
    freqs[i] = round(A4_freq * (2**((i - A4_midi)/12)))

def get_pitch_strengths(fft_data):
    # Try to compensate for rounding the frequencies to ints by including their neighbors.
    return np.abs(fft_data)[freqs-1] + np.abs(fft_data)[freqs] + np.abs(fft_data)[freqs+1]

window_size = 22050
window = signal.hanning(window_size)

pa = pyaudio.PyAudio()

sample_rate = 44100
stream = pa.open(format = pyaudio.paInt16,
                 channels = 1,
                 rate = sample_rate,
                 input = True,
                 output = False,
                 frames_per_buffer = 1024)

data = np.zeros(22050)
last_value = None
i = 0
while stream.is_active():
    raw = stream.read(1024)
    buffer = np.copy(np.frombuffer(raw, dtype=np.int16))
    data[:-1024] = data[1024:]
    data[-1024:] = buffer

    i += 1
    if i >= 10:
        i = 0
    else:
        continue
    out = fft.rfft(window * data)
    out[np.arange(0,80)] = 0
    pitch_strengths = get_pitch_strengths(out)
    value = np.argmax(pitch_strengths)
    if pitch_strengths[value] < 20000:
        value = 0
    if value != last_value:
        onset = i / sample_rate
        if last_value:
            midiout.send_message([midiconstants.NOTE_OFF, last_value, 127])
        if pitch_strengths[value] < 10000000:
            value = 0
        if value:
            midiout.send_message([midiconstants.NOTE_ON, value, 127])
            print(value, pitch_strengths[value], onset)
        last_value = value

stream.close()


import extra_keras_metrics
from keras.layers import Layer, Dense
from keras.models import Model
from typing import Tuple, List

def head(layer: Layer)->Layer:
    return Dense(1, activation="sigmoid")(layer)

def model(inputs: Tuple[Layer, Layer], output: Layer, metrics:List=None)->Model:
    model = Model(
        inputs=inputs,
        outputs=[head(output)]
    )
    model.compile(
        optimizer="nadam",
        loss="binary_crossentropy",
        metrics=["auprc", "auroc", "accuracy"] if metrics is None else metrics
    )
    return model


from django.contrib.auth.models import User
from django.db import models
from django.utils.text import slugify

Categories = (
    ('HTML', 'HTML'),
    ('CSS', 'CSS'),
    ('JavaScript', 'JavaScript'),
    ('JSON', 'JSON'),
    ('AJAX', 'AJAX'),
    ('Python', 'Python'),
    ('Django', 'Django')
)

class Book(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=200)
    author = models.CharField(max_length=200)
    description = models.TextField()
    url = models.URLField(max_length=200)
    updated_at = models.DateTimeField(auto_now=True)
    created_at = models.DateTimeField(auto_now=True)
    category = models.ForeignKey(
        'Category', on_delete=models.SET_NULL, null=True, blank=True)
    favorite = models.BooleanField(
        'Favorite', null=True, blank=True)

    def __str__(self):
        return f'Book title: {self.title} Author: {self.author} Description: {self.description} URL: {self.url} Category: {self.category} Favorite: {self.favorite}'

    class Meta:
        ordering = ['-created_at']

class Category(models.Model):
    name = models.CharField(max_length=100, choices=Categories)
    slug = models.SlugField(null=False, unique=True)

    def __str__(self):
        return f'{self.name}'

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.name)
        return super().save(*args, **kwargs)

class Favorite(models.Model):
    person = models.ForeignKey(
        User, related_name='+', on_delete=models.CASCADE)
    book = models.ForeignKey(
        Book, related_name='+', on_delete=models.CASCADE)

    def __str__(self):
        return f'Person: {self.person}, Book: {self.book.pk}'

# class User(AbstractUser)


import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from common.config_utils import read_config
from common.log_utils import log_pri

current_path = os.path.dirname(__file__)
driver_path = os.path.join(current_path, '..', read_config.driver_path)

class Browser:
    def __init__(self, driver_name=read_config.driver_name, driver_path=driver_path):
        self.__driver_path = driver_path
        self.__driver_name = driver_name

    def get_driver(self):
        if self.__driver_name.lower()=='chrome':
            return self.__get_chrome_driver()
        elif self.__driver_name.lower()=='firefox':
            return self.__get_firefox_driver()

    def __get_chrome_driver(self):
        chrome_options = Options()
        chrome_options.add_argument('--disable-gpu')  # 谷歌文档提到需要加上这个属性来规避bug
        chrome_options.add_argument('lang=zh_CN.UTF-8')  # 设置默认编码为utf-8
        chrome_options.add_experimental_option('useAutomationExtension', False)  # 取消chrome受自动控制提示
        chrome_options.add_experimental_option("prefs", {"credentials_enable_service": False,
                                                        "profile.password_manager_enabled": False})
        chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])  # 取消chrome受自动控制提示
        chrome_driver_path = os.path.join(self.__driver_path, 'chromedriver.exe')
        driver = webdriver.Chrome(options=chrome_options, executable_path=chrome_driver_path)
        log_pri.info('初始化并启动谷歌浏览器')
        return driver

    def __get_firefox_driver(self):
        firefox_driver_path = os.path.join(self.__driver_path, 'geckodriver.exe')
        driver = webdriver.Firefox(executable_path=firefox_driver_path)
        log_pri.info('初始化并启动火狐浏览器')
        return driver

if __name__ == '__main__':
    # Browser().get_chrome_driver()
    Browser().get_driver()


from ipinfo import trysites
import rich

def main():
    finaldata = trysites.trysites()
    if not finaldata:
        print("None of the IP sites could return even a valid IP.",
              "Perhaps check your network connection?")
    else:
        rich.print(finaldata)

if __name__ == "__main__":
    main()


from data.dataset.utility import Indexer
from data.dataset.abstract_dataset import AbstractDataset

class MultilexnormDataset(AbstractDataset):
    def __init__(self, args, inputs, outputs):
        super().__init__(args, inputs, outputs)
        self.inputs = inputs
        self.outputs = outputs

        valid_indices = [[i for i, word in enumerate(sentence) if self.filter(word)] for sentence in inputs]
        self.indexer = Indexer(valid_indices)

    def __getitem__(self, index):
        sentence_index, word_index = self.indexer.get_indices(index)

        out = self.outputs[sentence_index][word_index]
        raw = self.inputs[sentence_index]

        raw = raw[:word_index] + ["<extra_id_0>", raw[word_index], "<extra_id_1>"] + raw[word_index+1:]
        raw = ' '.join(raw)

        return raw, out, sentence_index, word_index

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


#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.shortcuts import render, get_object_or_404
from django.shortcuts import render
from django.views.generic import RedirectView, TemplateView
from django.conf import settings
from django.core.urlresolvers import reverse
from django.core.mail import send_mail
from django.http import HttpResponseBadRequest
from libros.models import Libro
from .models import PagoPaypal

import paypalrestsdk

# Create your views here.

class PaypalView(RedirectView):

    permanent = False

    def _generar_lista_items(self, libro):
        """ """
        items = []
        items.append({
            "name":     str(libro),
            "sku":      str(libro.id),
            "price":    ('%.2f' % libro.precio),
            "currency": "EUR",
            "quantity": 1,
        })
        return items

    def _generar_peticion_pago_paypal(self, libro):
        """Crea el diccionario para genrar el pago paypal de libro"""
        peticion_pago = {
            "intent": "sale",
            "payer": {"payment_method": "paypal"},
            "redirect_urls": {
                "return_url": settings.SITE_URL+reverse('aceptar-pago-paypal'),
                "cancel_url": settings.SITE_URL},

            # Transaction -
            "transactions": [ {
                # ItemList
                "item_list":{
                    "items": self._generar_lista_items(libro)},

                # Amount
                "amount": {
                    "total": ('%.2f' % libro.precio),
                    "currency": 'EUR'},

                #Description
                "description": str(libro),
                }]}

        return peticion_pago

    def _generar_pago_paypal(self, libro):
        """Genera un pago de paypal para libro"""
        paypalrestsdk.configure({
            "mode":         settings.PAYPAL_MODE,
            "client_id":    settings.PAYPAL_CLIENT_ID,
            "client_secret":settings.PAYPAL_CLIENT_SECRET,})

        pago_paypal = paypalrestsdk.Payment(self._generar_peticion_pago_paypal(libro))

        if pago_paypal.create():
            for link in pago_paypal.links:
                if link.method == "REDIRECT":
                    url_pago = link.href
        else:
            raise Exception(pago.error)

        return url_pago, pago_paypal

    def get_redirect_url(self, *args, **kwargs):
        """Extrae el libro que el usuario quiere comprar, genera un pago de
        paypal por el precio del libro, y devuelve la direccion de pago que
        paypal generó"""
        libro = get_object_or_404(Libro, pk=int(kwargs['libro_pk']))
        url_pago, pago_paypal = self._generar_pago_paypal(libro)

        # Se añade el identificador del pago a la sesion para que PaypalExecuteView
        # pueda identificar al ususuario posteriorment
        self.request.session['payment_id'] = pago_paypal.id

        # Por ultimo salvar la informacion del pago para poder determinar que
        # libro le corresponde, al terminar la transaccion.
        PagoPaypal.objects.crear_pago(pago_paypal.id, libro)

        return url_pago

class PaypalExecuteView(TemplateView):

    template_name = 'pagos/paypal_exito.html'

    def _enviar_ebook_email(self, registro_pago):
        """Enviar Email con el libro al cliente """
        #TODO: admjuntar los archivos del modelo libro
        libro = registro_pago.libro
        mensaje = "Gracias por su compra de %s" % libro.titulo
        send_mail('TiendaEbook', mensaje,
	    from_email = settings.DEFAULT_FROM_EMAIL,
            recipient_list=[registro_pago.payer_email,])

    def _aceptar_pago_paypal(self, payment_id, payer_id):
        """Aceptar el pago del cliente, actualiza el registro con los datos
        del cliente proporcionados por paypal"""
        registro_pago = get_object_or_404(PagoPaypal, payment_id=payment_id)
        pago_paypal = paypalrestsdk.Payment.find(payment_id)
        if pago_paypal.execute({'payer_id': payer_id}):
            registro_pago.pagado = True
            registro_pago.payer_id = payer_id
            registro_pago.payer_email = pago_paypal.payer['payer_info']['email']
            registro_pago.save()
        else:
            raise HttpResponseBadRequest

        return registro_pago

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

        # Extraer identificacion de paypal del cliente, la id del pago,
        # y buscar el registro correspondiente.
        context = self.get_context_data(**kwargs)
        try:
            payer_id = request.GET['PayerID']
            payment_id = request.session['payment_id']
        except Exception:
            raise HttpResponseBadRequest

        registro_pago = self._aceptar_pago_paypal(payment_id, payer_id)
        self._enviar_ebook_email(registro_pago)

        return self.render_to_response(context)


# -*- coding: utf-8 -*-
import scrapy
from scrapy import Request
from VideoSpider.items import TvItem

class CctvNewsSpider(scrapy.Spider):
    name = 'CCTV_news'
    allowed_domains = ['news.cctv.com']
    start_urls = ['http://news.cctv.com/']

    def parse(self, response):
        common = response.css('div.top_nav a')
        urls = common.css('::attr(href)').extract()
        urls[0] = 'http://news.cctv.com/'
        del urls[3]
        catalogys = common.css('::text').extract()
        del catalogys[3]
        for (url, cat) in zip(urls, catalogys):
            yield Request(url=url, callback=self.parse_info, meta={'cat': cat}, dont_filter=True)

    def parse_info(self, response):
        urls = response.css('a[href*="news.cctv.com"]::attr(href)').extract()
        if 'jiankang' in response.url:
            urls = response.css('a[href*="jiankang.cctv.com"]::attr(href)').extract()
        if 'sannong' in response.url:
            urls = response.css('a[href*="sannong.cctv.com"]::attr(href)').extract()
        if 'jingji' in response.url:
            urls = response.css('a[href*="jingji.cctv.com"]::attr(href)').extract()
        if 'sports' in response.url:
            urls = response.css('a[href*="sports.cctv.com"]::attr(href)').extract()
            urls.extend(response.css('a[href*="tv.cctv.com"]::attr(href)').extract())
        urls = set(urls)
        urls = [url for url in urls if 'index' not in url and not url.endswith('/') and url.endswith('html')]
        for url in urls:
            yield Request(url=url, callback=self.parse_title, meta={'cat': response.meta['cat']}, dont_filter=True)
        pass

    def parse_title(self, response):
        name = response.css('title::text').extract()[0].partition('_')[0]
        id = response.url[response.url.rfind('/') + 1: response.url.rfind('.')]
        print((name, id))
        item = TvItem()
        item['id'] = id
        item['name'] = name
        item['isFeature'] = 1
        item['upTime'] = ''
        item['area'] = ''
        item['parts'] = 1
        item['updateTime'] = ''
        item['language'] = ''
        item['type_tag'] = response.meta['cat']
        item['playTimes'] = 0
        item['score'] = ''
        item['director'] = ''
        item['actor'] = ''
        item['describe'] = ''
        item['isVip'] = 1
        item['comment'] = 0
        item['recommend'] = ''
        item['platform'] = 'cctv'
        item['sca_app_id'] = 5018
        item['cat1'] = '新闻'
        item['cat2'] = '新闻'
        # yield item


from django.core.urlresolvers import reverse

from drop.giftcard.models import GiftCardProduct, Credit
from drop.models import Order, Product
from drop.payment.api import PaymentAPI
from drop.test_utils import DropTestCase, print_order

from decimal import Decimal

class GiftCardTestCase(DropTestCase):
  def test_giftcard_purchase(self):
    gcp = GiftCardProduct.objects.create(name="gift card",unit_price=1,active=True)
    product40 = self.new_product(unit_price=40)
    product20 = self.new_product(unit_price=20)
    user1 = self.new_user()

    # user1 buys a giftcard
    self.login(user1)
    order_id = self.add_to_cart(gcp,50,extra={'to': 'jon','from': 'jan'})
    items = user1.cart.items.all()
    self.assertEqual(items.count(),1)
    self.assertEqual(items[0].extra['to'],'jon')
    self.assertEqual(items[0].extra['from'],'jan')
    order = Order.objects.get(id=order_id)
    #print_order(order)
    PaymentAPI().confirm_payment(order,order.order_total,"fake transaction","fake backend","fake description")
    credit = Credit.objects.get(purchased_by=user1)

    # user2 redeems giftcard
    user2 = self.new_user()
    self.login(user2)
    giftcard = self.client.post(reverse("giftcard_redeem_ajax"),{'code':credit.code}).json()['giftcard']
    self.assertEqual(giftcard['remaining'],'50.00')

    # user2 buys an entire cart using a giftcard
    order2_id = self.add_to_cart(product40)
    payment_url = reverse("drop_payment",args=["giftcard"])
    response = self.client.post(payment_url,{'code': credit.code,'total': 40})
    self.assertTrue(response.json()['next'].startswith('/thank_you/%s/?token='%order2_id))
    order2 = Order.objects.get(pk=order2_id)
    #print_order(order2)
    self.assertTrue(order2.is_paid())

    # gift card now says less money
    giftcard = self.client.post(reverse("giftcard_redeem_ajax"),{'code':credit.code}).json()['giftcard']
    self.assertEqual(giftcard['remaining'],'10.00')

    # user2 partially pays for a cart using a giftcrd
    order3 = Order.objects.get(pk=self.add_to_cart(product20))
    #print_order(order3)

    # Two partial payments work and take order cost down to $10
    response1 = self.client.post(payment_url,{'code': credit.code,'total': 5})
    response2 = self.client.post(payment_url,{'code': credit.code,'total': 5})
    self.assertEqual(len(response2.json()['extra_price_fields']),2)
    order3 = Order.objects.get(pk=order3.id)
    self.assertEqual(order3.amount_paid,Decimal(10))

    # Third fails because balance on gift card is zero
    response3 = self.client.post(payment_url,{'code': credit.code,'total': 5})
    self.assertTrue('Your gift card balance is $0.00' in response3.json()['error'])

    # user2 fully pays with another payment method
    PaymentAPI().confirm_payment(order3,10,"fake transaction","fake backend","fake description")
    self.assertTrue(Order.objects.get(pk=order3.id).is_paid())


# Usage: python patches_store.py -mm="ram" --debug=1 -pl 32 -po 0 \
# -ts 0 -tnl 10000 -bs=5000 --batch_size=128 --filters=256 \
# -m="smcnn_semantic" --phase="repeat" -sc=False --class_n=12 \
# --log_dir="../data/summaries/" --path="../cv_data/" --im_h=8492 \
# --im_w=7995 --band_n=2 --t_len=7 --id_first=8 -tof=False -nap=10000 \
# -psv=True
# Only relevant: pl, class_n, path, im_h, im_w, band_n, t_len

from __future__ import division
import os
import math
import random
import numpy as np
from time import gmtime, strftime
import glob
import tensorflow as tf
import numpy as np
from random import shuffle
import glob
import sys
import pickle
import argparse

# Local
import utils
import deb
from model import (conv_lstm,Conv3DMultitemp,UNet,SMCNN,SMCNNlstm, SMCNN_UNet, SMCNN_conv3d, lstm, conv_lstm_semantic, SMCNN_semantic)

#Input configuration
parser = argparse.ArgumentParser(description='')
parser.add_argument('--phase', dest='phase', default='train', help='phase')
parser.add_argument('--dataset_name', dest='dataset_name', default='20160419', help='name of the dataset')
parser.add_argument('--epoch', dest='epoch', type=int, default=200, help='# of epoch')
parser.add_argument('--batch_size', dest='batch_size', type=int, default=200, help='# images in batch')
parser.add_argument('--train_size', dest='train_size', type=int, default=1e8, help='# images used to train')
parser.add_argument('--timesteps', dest='timesteps', type=int, default=7, help='# timesteps used to train')
parser.add_argument('-pl','--patch_len', dest='patch_len', type=int, default=5, help='# timesteps used to train')
parser.add_argument('--kernel', dest='kernel', type=int, default=[3,3], help='# timesteps used to train')
#parser.add_argument('--channels', dest='channels', type=int, default=7, help='# timesteps used to train')
parser.add_argument('--filters', dest='filters', type=int, default=32, help='# timesteps used to train')
#parser.add_argument('--n_classes', dest='n_classes', type=int, default=6, help='# timesteps used to train')
parser.add_argument('--checkpoint_dir', dest='checkpoint_dir', default='./checkpoint', help='models are saved here')
parser.add_argument('-m','--model', dest='model', default='convlstm', help='models are saved here')
parser.add_argument('--log_dir', dest='log_dir', default='../data/summaries/', help='models are saved here')

parser.add_argument('--debug', type=int, default=1, help='Debug')
parser.add_argument('-po','--patch_overlap', dest='patch_overlap', type=int, default=0, help='Debug')
parser.add_argument('--im_size', dest='im_size',default=[948,1068], help='Debug')
parser.add_argument('--im_h', type=int, dest='im_h',default=948, help='Debug')
parser.add_argument('--im_w', type=int, dest='im_w',default=1068, help='Debug')

parser.add_argument('--band_n', dest='band_n', type=int, default=7, help='Debug')
parser.add_argument('--t_len', dest='t_len', type=int, default=6, help='Debug')
parser.add_argument('--path', dest='path', default="../data/", help='Data path')
parser.add_argument('--class_n', dest='class_n', type=int, default=6, help='Class number')
parser.add_argument('--pc_mode', dest='pc_mode', default="local", help="Class number. 'local' or 'remote'")
parser.add_argument('-tnl','--test_n_limit', dest='test_n_limit',type=int, default=1000, help="Class number. 'local' or 'remote'")
parser.add_argument('-mm','--memory_mode', dest='memory_mode',default="ram", help="Class number. 'local' or 'remote'")
parser.add_argument('-bs','--balance_samples_per_class', dest='balance_samples_per_class',type=int,default=None, help="Class number. 'local' or 'remote'")
parser.add_argument('-ts','--test_get_stride', dest='test_get_stride',type=int,default=8, help="Class number. 'local' or 'remote'")
parser.add_argument('-nap','--n_apriori', dest='n_apriori',type=int,default=4000000, help="Class number. 'local' or 'remote'")
parser.add_argument('-sc','--squeeze_classes', dest='squeeze_classes',default=True, help="Class number. 'local' or 'remote'")

parser.add_argument('-tof','--test_overlap_full', dest='test_overlap_full',default=False, help="Class number. 'local' or 'remote'")
parser.add_argument('-fes','--fine_early_stop', dest='fine_early_stop',default=True, help="Class number. 'local' or 'remote'")
parser.add_argument('-ttmn','--train_test_mask_name', dest='train_test_mask_name',default="TrainTestMask.tif", help="Class number. 'local' or 'remote'")
parser.add_argument('--id_first', dest='id_first', type=int, default=1, help='Class number')
parser.add_argument('-ir','--im_reconstruct', dest='im_reconstruct',default=False, help="Class number. 'local' or 'remote'")

parser.add_argument('-rst','--ram_store', dest='ram_store',default=True, help="Ram store")
parser.add_argument('-psv','--patches_save', dest='patches_save',default=True, help="Patches npy store")

args = parser.parse_args()

args.n_classes=args.class_n
args.timesteps=args.t_len
args.channels=args.band_n
np.set_printoptions(suppress=True)
os.system("rm -rf ../"+args.path+"/summaries/*")

# Check if selected model has one_hot (One pixel) or semantic (Image) output type
if args.model=='unet' or args.model=='smcnn_unet' or args.model=='convlstm_semantic' or args.model=='smcnn_semantic':
    label_type='semantic'
else:
    label_type='one_hot'
deb.prints(label_type)
deb.prints(args.patches_save)

def main(_):

    # Make checkpoint directory
    if not os.path.exists(args.checkpoint_dir):
        os.makedirs(args.checkpoint_dir)

    # Create a dataset object
    if label_type=='one_hot':
        data=utils.DataOneHot(debug=args.debug, patch_overlap=args.patch_overlap, im_size=args.im_size, \
                                band_n=args.band_n, t_len=args.t_len, path=args.path, class_n=args.class_n, pc_mode=args.pc_mode, \
                                test_n_limit=args.test_n_limit,memory_mode=args.memory_mode, \
                                balance_samples_per_class=args.balance_samples_per_class, test_get_stride=args.test_get_stride, \
                                n_apriori=args.n_apriori,patch_length=args.patch_len,squeeze_classes=args.squeeze_classes,im_h=args.im_h,im_w=args.im_w, \
                                id_first=args.id_first, train_test_mask_name=args.train_test_mask_name, \
                                test_overlap_full=args.test_overlap_full,ram_store=args.ram_store,patches_save=args.patches_save)
    elif label_type=='semantic':
        data=utils.DataSemantic(debug=args.debug, patch_overlap=args.patch_overlap, im_size=args.im_size, \
                                band_n=args.band_n, t_len=args.t_len, path=args.path, class_n=args.class_n, pc_mode=args.pc_mode, \
                                test_n_limit=args.test_n_limit,memory_mode=args.memory_mode, \
                                balance_samples_per_class=args.balance_samples_per_class, test_get_stride=args.test_get_stride, \
                                n_apriori=args.n_apriori,patch_length=args.patch_len,squeeze_classes=args.squeeze_classes,im_h=args.im_h,im_w=args.im_w, \
                                id_first=args.id_first, train_test_mask_name=args.train_test_mask_name, \
                                test_overlap_full=args.test_overlap_full,ram_store=args.ram_store,patches_save=args.patches_save)

    # Load images and create dataset (Extract patches)
    if args.memory_mode=="ram":
        data.create()
        deb.prints(data.ram_data["train"]["ims"].shape)

    # Run tensorflow session
    with tf.Session() as sess:

        # Create a neural network object (Define model graph)
        if args.model=='convlstm':
            model = conv_lstm(sess, batch_size=args.batch_size, epoch=args.epoch, train_size=args.train_size,
                            timesteps=args.timesteps, patch_len=args.patch_len,
                            kernel=args.kernel, channels=args.channels, filters=args.filters, n_classes=args.n_classes,
                            checkpoint_dir=args.checkpoint_dir,log_dir=args.log_dir,data=data.ram_data,conf=data.conf, debug=args.debug)
        elif args.model=='conv3d':
            model = Conv3DMultitemp(sess, batch_size=args.batch_size, epoch=args.epoch, train_size=args.train_size,
                            timesteps=args.timesteps, patch_len=args.patch_len,
                            kernel=args.kernel, channels=args.channels, filters=args.filters, n_classes=args.n_classes,
                            checkpoint_dir=args.checkpoint_dir,log_dir=args.log_dir,data=data.ram_data,conf=data.conf, debug=args.debug)
        elif args.model=='unet':
            model = UNet(sess, batch_size=args.batch_size, epoch=args.epoch, train_size=args.train_size,
                            timesteps=args.timesteps, patch_len=args.patch_len,
                            kernel=args.kernel, channels=args.channels, filters=args.filters, n_classes=args.n_classes,
                            checkpoint_dir=args.checkpoint_dir,log_dir=args.log_dir,data=data.ram_data,conf=data.conf, debug=args.debug)
        elif args.model=='smcnn':
            model = SMCNN(sess, batch_size=args.batch_size, epoch=args.epoch, train_size=args.train_size,
                            timesteps=args.timesteps, patch_len=args.patch_len,
                            kernel=args.kernel, channels=args.channels, filters=args.filters, n_classes=args.n_classes,
                            checkpoint_dir=args.checkpoint_dir,log_dir=args.log_dir,data=data.ram_data,conf=data.conf, debug=args.debug)
        elif args.model=='smcnnlstm':
            model = SMCNNlstm(sess, batch_size=args.batch_size, epoch=args.epoch, train_size=args.train_size,
                            timesteps=args.timesteps, patch_len=args.patch_len,
                            kernel=args.kernel, channels=args.channels, filters=args.filters, n_classes=args.n_classes,
                            checkpoint_dir=args.checkpoint_dir,log_dir=args.log_dir,data=data.ram_data,conf=data.conf, debug=args.debug)
        elif args.model=='smcnn_unet':
            model = SMCNN_UNet(sess, batch_size=args.batch_size, epoch=args.epoch, train_size=args.train_size,
                            timesteps=args.timesteps, patch_len=args.patch_len,
                            kernel=args.kernel, channels=args.channels, filters=args.filters, n_classes=args.n_classes,
                            checkpoint_dir=args.checkpoint_dir,log_dir=args.log_dir,data=data.ram_data,conf=data.conf, debug=args.debug)
        elif args.model=='smcnn_conv3d':
            model = SMCNN_conv3d(sess, batch_size=args.batch_size, epoch=args.epoch, train_size=args.train_size,
                            timesteps=args.timesteps, patch_len=args.patch_len,
                            kernel=args.kernel, channels=args.channels, filters=args.filters, n_classes=args.n_classes,
                            checkpoint_dir=args.checkpoint_dir,log_dir=args.log_dir,data=data.ram_data,conf=data.conf, debug=args.debug)
        elif args.model=='lstm':
            model = lstm(sess, batch_size=args.batch_size, epoch=args.epoch, train_size=args.train_size,
                            timesteps=args.timesteps, patch_len=args.patch_len,
                            kernel=args.kernel, channels=args.channels, filters=args.filters, n_classes=args.n_classes,
                            checkpoint_dir=args.checkpoint_dir,log_dir=args.log_dir,data=data.ram_data,conf=data.conf, debug=args.debug)
        elif args.model=='convlstm_semantic':
            model = conv_lstm_semantic(sess, batch_size=args.batch_size, epoch=args.epoch, train_size=args.train_size,
                            timesteps=args.timesteps, patch_len=args.patch_len,
                            kernel=args.kernel, channels=args.channels, filters=args.filters, n_classes=args.n_classes,
                            checkpoint_dir=args.checkpoint_dir,log_dir=args.log_dir,data=data.ram_data,conf=data.conf, debug=args.debug)
        elif args.model=='smcnn_semantic':
            model = SMCNN_semantic(sess, batch_size=args.batch_size, epoch=args.epoch, train_size=args.train_size,
                            timesteps=args.timesteps, patch_len=args.patch_len,
                            kernel=args.kernel, channels=args.channels, filters=args.filters, n_classes=args.n_classes,
                            checkpoint_dir=args.checkpoint_dir,log_dir=args.log_dir,data=data.ram_data,conf=data.conf, debug=args.debug)

        if args.phase == 'train':
            # Train only once
            model.train(args)

        elif args.phase == 'repeat':
            # Train for a specific number of repetitions
            model.train_repeat(args)

        elif args.phase == 'test':
            # Test best model from experiment repetitions
            model.test(args)

if __name__ == '__main__':
    tf.app.run()


#!/usr/bin/env python3

import os
import sys

#runPath = os.getcwd() #radni direktorij
runPath = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(runPath, "..")) #dodaj parent dir u PATH varijablu

from lib.Config import Configuration
from lib.Toolkit import pad
import lib.DatabaseLayer as db #cve-search lib za rad s bazom

#cpe = db.getCPEs()
from redis import exceptions as redisExceptions
try:
    r = Configuration.getRedisVendorConnection()
except:
    sys.exit(1)

# for key in r.scan_iter():
#        print (key)

#1kxun
#qianxun_yingshi

# Redis db 10 (cpe)
# Redis db 11 (notification)

def vendors(): #vraca sve vendore
    return r.sunion('o', 'a', 'h') #r.sunion('t:/a', 't:/h', 't:/o')

def vendor_products(vendor): #vraca sve proizvode od vendora
    allproduct = []
    products = r.smembers('v:' + vendor)
    for product in products:
        allproduct.append(product)
    return allproduct

#print(vendor_products("muslim_matrimonial_script_project"))

def product_versions(product): #vraca verzije po proizvodu
    allversion = []
    versions = r.smembers('p:' + product)
    for version in versions:
        allversion.append(version)
    return allversion

def get_vendor(product): #vraca vendora po proizvodu
    result = []
    allvendors = r.sunion('t:/a', 't:/h', 't:/o')
    for vendor in allvendors:
        if product in vendor_products(vendor):
            result.append(vendor)
    return result

def sortedVersionsOfProduct(product): #vraca sortirano
  p = r.smembers("p:" + product)
  return sorted(list(p))

def search_vendor(search): #trazi odredenog vendora
    result = []
    for vendor in vendors():
        if search in vendor:
            result.append(vendor)
    return result

def search_product(search): #trazi odreden proizvod
    result = []
    for vendor in vendors():
        for product in vendor_products(vendor):
            if search in product:
                result.append(product)
    return result

def search_vendor_product(search, vendor): #trazi odred proizvod kod odred vendora
    result = []
    for product in vendor_products(vendor):
        if search in product:
            result.append(product)
    return result

def sortedBrowseList(vendor):
  result = {}
  if (vendor is None) or type(vendor) == list:
    v1 = r.smembers("t:/o")
    v2 = r.smembers("t:/a")
    v3 = r.smembers("t:/h")

    vendor = sorted(list(set(list(v1) + list(v2) + list(v3))))

    cpe = None
  else:
    cpenum = r.scard("v:" + vendor)
    if cpenum < 1:
      return None
    p = r.smembers("v:" + vendor)
    cpe = sorted(list(p))

  result["vendor"] = vendor
  result["product"] = cpe

  return result

print(len(list(vendors())))


import sys
from setuptools import setup

dependencies = [
    'netaddr',
    'netifaces',
]

if sys.version_info[:2] < (2, 7):
    dependencies.append('argparse')

setup(
    name='cidrchk',
    version='1.0.0',
    author='Joseph Hunkeler',
    author_email='jhunkeler@gmail.com',
    description=('A simple "Is my computer on this subnet?" detection script.'),
    license='BSD',
    keywords='cidrchk detect network interface ethernet',
    zip_safe=True,
    scripts=[
        'cidrchk',
    ],
    install_requires=dependencies,
    classifiers=[
        'Development Status :: 5 - Production/Stable',
        'Topic :: Utilities',
        'License :: OSI Approved :: BSD License',
    ]
)


####
__author__ = 'AM7'

## dataset https://www.kaggle.com/c/challenges-in-representation-learning-facial-expression-recognition-challenge
####

import os
import cv2
import numpy as np
from random import shuffle
from tqdm import tqdm

import random
from sklearn import preprocessing
from imutils import paths
from sklearn.preprocessing import LabelBinarizer
import imutils
from sklearn.model_selection import train_test_split
from keras.applications import imagenet_utils
import math
import pickle
from model import Build_Model

PATH_ = "Kaggle/Facial_Expression_Recognition/"
LR = 1e-3 # 0.001
EPOCHS = 12
IMG_SIZE = 48

MODEL_NAME = PATH_ + '_Facial-{}-{}.model'.format(LR, 'CNN')
TRAIN_DIR = PATH_ + 'Training'
TEST_DIR = PATH_ + 'PrivateTest'
data = []
labels = []
##  (0=Angry, 1=Disgust, 2=Fear, 3=Happy, 4=Sad, 5=Surprise, 6=Neutral).

imagePaths = sorted(list(paths.list_images(TRAIN_DIR)))
random.seed(9001)
random.shuffle(imagePaths)

def process_image(imagePath):
    img = cv2.imread(imagePath, cv2.IMREAD_GRAYSCALE)
    img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))

    return img

def create_train_data(imagePaths):
    for imagPath in tqdm(imagePaths):
        img = process_image(imagPath)
        data.append(img)

        label =  imagPath.split(os.path.sep)[-2]
        labels.append(label)

    np.save(PATH_ + '_data.npy', data)
    np.save(PATH_ + '_labels.npy', labels)

if os.path.exists(PATH_ + '_data.npy') and os.path.exists(PATH_ + '_labels.npy'):
    data = np.load(PATH_ + '_data.npy')
    labels = np.load(PATH_ + '_labels.npy')
else:
    create_train_data(imagePaths)

data = np.array(data)
labels = np.array(labels,dtype='int')

LB = LabelBinarizer()
labels = LB.fit_transform(labels)
print(LB.classes_)

f = open(PATH_ + 'LB', "wb")
f.write(pickle.dumps(LB))
f.close()

(trainX, testX, trainY, testY) = train_test_split(data, labels, test_size=0.2)

trainX = np.array(trainX).reshape(-1, IMG_SIZE,IMG_SIZE, 1)
testX = np.array(testX).reshape(-1, IMG_SIZE,IMG_SIZE, 1)

model = Build_Model(IMG_SIZE, IMG_SIZE, len(LB.classes_), 1, LR)
print(len(trainX))
if os.path.exists(PATH_ + '_Facial-0.001-CNN.model.meta'):
    model.load(MODEL_NAME)
    mlb = pickle.loads(open(PATH_ + 'LB', "rb").read())
    print('model loaded!')
else:
    print('Building model...')
    model.fit({'input': trainX}, {'targets': trainY}, n_epoch=EPOCHS, validation_set=({'input': testX}, {'targets': testY}),
        snapshot_step=len(trainX) // 32, show_metric=True, run_id=MODEL_NAME)
    model.save(MODEL_NAME)
eva = model.evaluate(testX, testY,len(trainX) // 32)
print(eva)


import os
from pathlib import Path
import sys

sys.setrecursionlimit(3000)

# Default Directory Paths
home_dir = str(Path.home())
PROJECT_DIR = os.path.join(home_dir, ".project03")
CACHE_PATH = os.path.join(PROJECT_DIR, "cache")
LOG_DIR = os.path.join(PROJECT_DIR, "logs")

os.makedirs(CACHE_PATH, exist_ok=True)
os.makedirs(LOG_DIR, exist_ok=True)


import time
import find_remove_find5
import sort_then_find3
import walk_through7

from typing import Callable, List, Any

def time_find_two_smallest(find_func: Callable[[List[float]], Any],
                           lst: List[float]) -> float:
    """find_func(lst)를 실행하는 데 몇 초가 걸렸는지 반환한다.
    """

    t1 = time.perf_counter()
    find_func(lst)
    t2 = time.perf_counter()
    return (t2 - t1) * 1000.0

if __name__ == '__main__':
    # 해면 기압을 읽는다.
    sea_levels = []
    sea_levels_file = open('sea_levels.txt', 'r')
    for line in sea_levels_file:
        sea_levels.append(float(line))
    sea_levels_file.close()

    # 각 방법의 시간을 측정한다.
    find_remove_find_time = time_find_two_smallest(
        find_remove_find5.find_two_smallest, sea_levels)

    sort_get_minimums_time = time_find_two_smallest(
        sort_then_find3.find_two_smallest, sea_levels)

    walk_through_time = time_find_two_smallest(
        walk_through7.find_two_smallest, sea_levels)

    print('"찾고 삭제하고 찾기"는 {:.2f}ms가 걸렸습니다.'.format(find_remove_find_time))
    print('"정렬하고 최솟값 얻기"는 {:.2f}ms가 걸렸습니다.'.format(
        sort_get_minimums_time))
    print('"리스트 전체 훑기"는 {:.2f}ms가 걸렸습니다.'.format(walk_through_time))


from collections import Counter
import time

class TimingCtx:
    def __init__(self, init=None, init_ns=None, first_tic=None):
        self.timings = init if init is not None else Counter()
        self.ns = init_ns if init_ns is not None else Counter()
        self.arg = None
        self.last_clear = time.time()
        self.first_tic = first_tic

    def clear(self):
        self.timings.clear()
        self.last_clear = time.time()

    def start(self, arg):
        if self.arg is not None:
            self.__exit__()
        self.__call__(arg)
        self.__enter__()

    def stop(self):
        self.start(None)

    def __call__(self, arg):
        self.arg = arg
        return self

    def __enter__(self):
        self.tic = time.time()
        if self.first_tic is None:
            self.first_tic = self.tic

    def __exit__(self, *args):
        self.timings[self.arg] += time.time() - self.tic
        self.ns[self.arg] += 1
        self.arg = None

    def __repr__(self):
        return dict(
            total=time.time() - self.last_clear,
            **dict(sorted(self.timings.items(), key=lambda kv: kv[1], reverse=True)),
        ).__repr__()

    def __truediv__(self, other):
        return {k: v / other for k, v in self.timings.items()}

    def __iadd__(self, other):
        if other != 0:
            self.timings += other.timings
            self.ns += other.ns
        return self

    def items(self):
        return self.timings.items()

    @classmethod
    def pprint_multi(cls, timings, log_fn):
        data = []
        for k in timings[0].timings.keys():
            t_mean = sum(t.timings[k] for t in timings) / len(timings)
            t_max = max(t.timings[k] for t in timings)
            n = sum(t.ns[k] for t in timings) / len(timings)
            data.append((t_mean, t_max, k, n))

        log_fn(f"TimingCtx summary of {len(timings)} timings (mean/max shown over this group):")
        ind_log_fn = lambda x: log_fn("  " + x)
        ind_log_fn(
            "{:^24}|{:^8}|{:^18}|{:^18}".format("Key", "N_mean", "t_mean (ms)", "t_max (ms)")
        )
        ind_log_fn("-" * (24 + 8 + 18 + 18))
        for t_mean, t_max, k, n in sorted(data, reverse=True):
            ind_log_fn(
                "{:^24}|{:^8.1f}|{:^18.1f}|{:^18.1f}".format(k, n, t_mean * 1e3, t_max * 1e3)
            )
        ind_log_fn("-" * (24 + 8 + 18 + 18))

        sum_t_mean = sum(t_mean for t_mean, _, _, _ in data)
        ind_log_fn("{:^24}|{:^8}|{:^18.1f}|{:^18}".format("Total", "", sum_t_mean * 1e3, ""))

    def pprint(self, log_fn):
        data = []
        for k in self.timings.keys():
            n = self.ns[k]
            t_total = self.timings[k]
            t_mean = t_total / n
            data.append((t_total, t_mean, k, n))

        log_fn(f"TimingCtx summary:")
        ind_log_fn = lambda x: log_fn("  " + x)
        ind_log_fn("{:^24}|{:^8}|{:^18}|{:^18}".format("Key", "N", "t_total (ms)", "t_mean (ms)"))
        ind_log_fn("-" * (24 + 8 + 18 + 18))
        for t_total, t_mean, k, n in sorted(data, reverse=True):
            ind_log_fn(
                "{:^24}|{:^8.1f}|{:^18.1f}|{:^18.1f}".format(k, n, t_total * 1e3, t_mean * 1e3)
            )
        ind_log_fn("-" * (24 + 8 + 18 + 18))

        elapsed = time.time() - self.first_tic
        sum_t_total = sum(t_total for t_total, _, _, _ in data)
        ind_log_fn(
            "{:^24}|{:^8}|{:^18.1f}|{:^18}".format("Lost", "", (elapsed - sum_t_total) * 1e3, "")
        )
        ind_log_fn("{:^24}|{:^8}|{:^18.1f}|{:^18}".format("Total", "", sum_t_total * 1e3, ""))

class DummyCtx:
    def __enter__(self):
        pass

    def __exit__(self, *args):
        pass

    def __call__(self, *args):
        return self


import datetime
from django.contrib import messages
from django.shortcuts import render, redirect
from django.views.decorators.http import require_GET

from MyApp.models.models import Category

# Create your views here.
pathFolder="pages/categorys/"
def index(request):
    if 'search' in request.GET:
        search = request.GET['search']
        category = Category.objects.filter(name__icontains=search)
    else:
        category = Category.objects.all()
    context = {'categorys': category}
    return render(request,pathFolder + "index.html", context)

def create(request):

    return render(request, pathFolder + "add_category.html")

def store(request):
    try:
        category = Category()
        category.name = request.POST["txtName"]
        category.createBy = 1
        messages.success(request, "Category Created")
        category.save()
    except Exception as ex:
        print("Error:" + str(ex))
    return redirect("/category/create")

def destroy(request, id):
    try:
        category = Category.objects.get(pk=id)
        category.delete()
        #messages.success(request, "Category Deleted")
    except Exception as ex:
        print("Error:" + str(ex))
    return redirect("/category")

def edit(request, id):
    try:
        category = Category.objects.get(pk=id)
        context = {"category": category}
    except Exception as ex:
        print("Error:" + str(ex))
    return render(request, pathFolder + "edit.html", context)

def update(request):
    try:
        category = Category()
        category.id = request.POST["txtId"]
        category.name = request.POST["txtName"]
        c1 = Category.objects.get(pk=category.id)
        if c1:
            c1.name = category.name;
            c1.updateAt = datetime.datetime.now()
            c1.updateBy = 1;
            c1.save()
            messages.success(request,"Category Updated")
    except Exception as ex:
        print("Error:" + str(ex))
    return redirect("/category")