from datetime import date
from decimal import Decimal

from django.db import models
from mezzanine.pages.models import Page, PageMoveException
from django.db.models import CharField, Q, F
from django.utils.translation import (ugettext, ugettext_lazy as _,
                                      pgettext_lazy as __)
from cartridge.shop.models import Cart, ProductVariation, Order
from mezzanine.conf import settings

# Create your models here.
# The members of Page will be inherited by the Author model, such
# as title, slug, etc. For authors we can use the title field to
# store the author's name. For our model definition, we just add
# any extra fields that aren't part of the Page model, in this
# case, date of birth.

# class Author(Page):
#     dob = models.DateField("Date of birth")
#     trivia = models.TextField("Trivia")
#
#     def can_add(self, request):
#         return self.children.count() == 0
#
#     def can_delete(self, request):
#         return request.user.is_superuser or self.parent is not None
#
#     def can_move(self, request, new_parent):
#         if new_parent is None:
#             msg = 'An author page cannot be a top-level page'
#             raise PageMoveException(msg)
#
#
# class Book(models.Model):
#     author = models.ForeignKey("Author")
#     title = models.CharField(null=False, blank=False, max_length=120)
#     cover = models.ImageField(upload_to="authors")
#     date_published = models.DateField("Date published", default=date.today)

def calculate_discount_patch(self, discount):
        """
        Calculates the discount based on the items in a cart, some
        might have the discount, others might not.
        Original works for 1 code
        """
        # Discount applies to cart total if not product specific.
        products = discount.all_products()
        print("calc discount patch")
        if products.count() == 0:
            return discount.calculate(self.total_price())
        total = Decimal("0")
        # Create a list of skus in the cart that are applicable to
        # the discount, and total the discount for appllicable items.
        lookup = {"product__in": products, "sku__in": self.skus()}
        discount_variations = ProductVariation.objects.filter(**lookup)
        discount_skus = discount_variations.values_list("sku", flat=True)
        for item in self:
            if item.sku in discount_skus:
                total += discount.calculate(item.unit_price) * item.quantity
        return total

Cart.calculate_discount_patch = calculate_discount_patch

def total_price_gold(self):
    """
    Template helper function - sum of all costs of item quantities.
    """
    return sum([item.total_price for item in self])

Cart.total_price_gold = total_price_gold

def total_price_diamond(self):
    """
    Template helper function - sum of all costs of item quantities.
    """
    return sum([item.total_price for item in self])

Cart.total_price_diamond = total_price_diamond

Order.billing_detail_postcode = CharField(_("Postcode"), max_length=10, null=True, blank=True)
Order.shipping_detail_postcode = CharField(_("Postcode"), max_length=10, null=True, blank=True)


#!/usr/bin/env python3

from setuptools import setup, find_packages

install_requires = [
  'beancount',
  'pyyaml',
]

setup(
  name="BeanPorter",
  version='0.1',
  description="Beancount importer",
  long_description=
  """
  Beancount importer.
  """,
  license="MIT",
  author="WeZZard",
  author_email="me@wezzard.com",
  url="https://github.com/WeZZard/BeanPorter",
  download_url="https://github.com/WeZZard/BeanPorter",
  packages=find_packages(where='src'),
  package_dir={
    "BeanPorter": "src/BeanPorter",
    "BeanPorter.bpcml": "src/BeanPorter/bpcml",
  },
  install_requires = install_requires,
  package_data={
    'BeanPorter': ['bean_porter_config.yaml']
  },
  entry_points = {'console_scripts': ['bean-porter=BeanPorter:main']},
  python_requires='>=3.6',
)


from django.db import models
from django.conf import settings
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django_countries import CountryField
from django.core.mail import EmailMessage
from django.template.loader import render_to_string
from django.utils.translation import ugettext as _
from django.core.urlresolvers import reverse
from django_countries.countries import COUNTRIES
from statistics.models import Interest

# We add the most used countries first
COUNTRIES = list(COUNTRIES)
COUNTRIES.insert(0,('MX', _(u'Mexico')))
COUNTRIES.insert(0,('CL', _(u'Chile')))
COUNTRIES.insert(0,('US', _(u'United States')))
COUNTRIES.insert(0,('GB', _(u'United Kingdom')))
COUNTRIES.insert(0,('ES', _(u'Spain')))
COUNTRIES = tuple(COUNTRIES)

#Profile where save the personal details of the user
class Profile(models.Model):
    user = models.OneToOneField(User,unique=True,related_name='personal_profile')
    description = models.TextField(blank=False)
    subtitle = models.CharField(max_length=100,blank=False)
    image = models.ImageField(upload_to='profile_images',blank=True)
    country = CountryField(choices=COUNTRIES)
    state = models.CharField(max_length=250,blank=True)
    address = models.CharField(max_length=250,blank=True)
    phone_number = models.CharField(max_length=250,blank=True)
    website = models.CharField(max_length=250,blank=True)
    twitter_user = models.CharField(max_length=50,blank=True)
    facebook_url = models.URLField(blank=True)
    linkedin_url = models.URLField(blank=True)
    googleplus_url = models.URLField(blank=True)
    is_first_login = models.BooleanField(default=False,editable=True)

    def __unicode__(self):
        return "%s's profile" % self.user

    def had_filled_poll(self):
        filled = False
        if Interest.objects.filter(user_id=self.user.id).count():
            filled = True
        return filled

    def get_unread_received_messages(self):
        return self.user.messages_received.filter(is_read=False)

    def count_unread_received_messages(self):
        return self.user.messages_received.filter(is_read=False).count()

    def is_completed(self):
        completed = True
        if not self.description:
            completed = False
        if not self.subtitle:
            completed = False
        if not self.country:
            completed = False
        if not self.state:
            completed = False
        if not self.user.first_name:
            completed = False
        if not self.user.last_name:
            completed = False
        return completed

class Message(models.Model):
    from_user = models.ForeignKey(User,verbose_name=_('From User'),related_name='messages_sent')
    to_user = models.ForeignKey(User,verbose_name=_('To User'),related_name='messages_received')
    subject = models.CharField(max_length=200)
    message = models.TextField(_('Message'),max_length=2000)
    created_at = models.DateTimeField(auto_now_add=True)
    modified_at = models.DateTimeField(auto_now=True)
    is_read = models.BooleanField(default=False,editable=True)

    def __unicode__(self):
        return unicode(self.from_user)

    def get_url(self):
        return reverse('personal.views.list_messages')

    class META:
        ordering = ['created_at']

# -------- Signals -----------

# Create the user profile when create the User is created.
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance,description = _('Change your description'),is_first_login = True)

post_save.connect(create_user_profile, sender=User, dispatch_uid='users-profile-creation-signal')

# Email Message
def send_email_message(sender, instance, created, **kwargs):
    if created:
        message = instance
        if message.to_user.email:
            ctx_dict = { 'message': message }
            email_message = render_to_string('personal/email/mail.html',ctx_dict)
            if not message.subject:
                subject = _('TrainingMe.net - New Message')
            else:
                subject = message.subject
            msg = EmailMessage(subject, email_message, settings.DEFAULT_FROM_EMAIL, [message.to_user.email])
            msg.content_subtype = "html"  # Main content is now text/html
            msg.send()

post_save.connect(send_email_message, sender=Message, dispatch_uid='send-email-message-signal')

#Get the avatars from Social Auth
from social_auth.backends.facebook import FacebookBackend
from social_auth.backends.twitter import TwitterBackend
from social_auth.backends import google
from social_auth.signals import socialauth_registered, pre_update

def social_extra_values(sender, user, response, details, **kwargs):
    result = False

    if "id" in response:
        from urllib2 import urlopen, HTTPError
        from django.template.defaultfilters import slugify
        from django.core.files.base import ContentFile

        try:
            url = None
            if sender == FacebookBackend:
                url = "http://graph.facebook.com/%s/picture?type=large"\
                      % response["id"]
            elif sender == google.GoogleOAuth2Backend and "picture" in response:
                url = response["picture"]
            elif sender == TwitterBackend:
                url = response["profile_image_url"]

            if url:
                profile = user.get_profile()
                if not profile.image:
                    avatar = urlopen(url)
                    profile.image.save(slugify(user.username + " social") + '.jpg',
                        ContentFile(avatar.read()))
                    profile.save()

        except HTTPError:
            pass

        result = True

    return result

socialauth_registered.connect(social_extra_values, sender=None)
pre_update.connect(social_extra_values, sender=None)


import deepspeed
from math import log2
import multiprocessing
import numpy as np
import os
from random import random
from stylegan2_deepspeed.dataset import Dataset
from stylegan2_deepspeed.ema import EMA
from stylegan2_deepspeed.lookahead import Lookahead
import stylegan2_deepspeed.stylegan2 as stylegan2
import torch
import torch.nn.functional as F
from torch.optim import Adam
from torch.utils import data
from torch.utils.data.distributed import DistributedSampler
import torchvision
from tqdm import tqdm

NUM_CORES = multiprocessing.cpu_count()

def gen_hinge_loss(fake, real):
    return fake.mean()

def hinge_loss(real, fake):
    return (F.relu(1 + real) + F.relu(1 - fake)).mean()

def noise(n, latent_dim, device):
    return torch.randn(n, latent_dim).cuda(device)

def noise_list(n, layers, latent_dim, device):
    return [(noise(n, latent_dim, device), layers)]

def mixed_list(n, layers, latent_dim, device):
    tt = int(torch.rand(()).numpy() * layers)
    return noise_list(n, tt, latent_dim, device) + noise_list(n, layers - tt, latent_dim, device)

def image_noise(n, im_size, device):
    return torch.FloatTensor(n, im_size, im_size, 1).uniform_(0., 1.).cuda(device)

def forever():
  while True:
    yield

def cycle(iterable):
  while True:
    for i in iterable:
      yield i

def create_generator(args, device):
  return stylegan2.StylizedGenerator(args.image_size, network_capacity=args.network_capacity).cuda(device)

class TrainingRun():
  def __init__(self, gen, gen_opt, disc, disc_opt, args, loader):
    assert log2(args.image_size).is_integer(), 'image size must be a power of 2 (64, 128, 256, 512, 1024)'

    self.gen = gen
    self.gen_opt = gen_opt
    self.disc = disc
    self.disc_opt = disc_opt
    self.loader = loader

    self.device = gen.local_rank
    self.batch_size = gen.train_micro_batch_size_per_gpu()

    self.image_size = args.image_size
    self.num_layers = int(log2(self.image_size) - 1)
    self.latent_dim = args.latent_dim
    self.lookahead = args.lookahead
    self.lookahead_k = args.lookahead_k
    self.checkpoint_every = args.checkpoint_every
    self.evaluate_every = args.evaluate_every
    self.models_dir = args.models_dir
    self.results_dir = args.results_dir
    self.model_name = args.name
    self.gen_load_from = args.gen_load_from
    self.disc_load_from = args.disc_load_from

    self.rank = args.local_rank
    self.is_primary = self.rank == 0

    self.losses = {}

    if self.is_primary:
      self.gen_ema = create_generator(args, self.device)
      self.gen_ema.requires_grad_(False)

      self.ema_k = args.ema_k
      self.ema = EMA(args.ema_beta)
    else:
      self.gen_ema = None
      self.ema = None

    self.mixed_prob = 0.9

    self.D_loss_fn = hinge_loss
    self.G_loss_fn = gen_hinge_loss

  def track_loss(self, name, loss):
    self.losses[name] = loss

  def get_training_image_batch(self):
    get_latents_fn = mixed_list if random() < self.mixed_prob else noise_list
    style = get_latents_fn(self.batch_size, self.num_layers, self.latent_dim, self.device)
    noise = image_noise(self.batch_size, self.image_size, self.device)

    return self.gen.forward(style, noise)

  def get_checkpoint_dirs(self, create_if_needed=False):
    gen_path = os.path.join(self.models_dir, self.model_name, 'gen')
    disc_path = os.path.join(self.models_dir, self.model_name, 'disc')

    if create_if_needed:
      os.makedirs(gen_path, exist_ok=True)
      os.makedirs(disc_path, exist_ok=True)

    return (gen_path, disc_path)

  def gradient_penalty(self, images, output, weight = 10):
    batch_size = images.shape[0]
    gradients = torch.autograd.grad(outputs=output, inputs=images,
                           grad_outputs=torch.ones(output.size(), device=images.device),
                           create_graph=True, retain_graph=True, only_inputs=True)[0]

    gradients = gradients.reshape(batch_size, -1)
    return weight * ((gradients.norm(2, dim=1) - 1) ** 2).mean()

  def step(self):
    # TODO: There's other loss calculations that still need to be copied from the original code.
    # apply_path_penalty, apply_gradient_penalty,
    apply_gradient_penalty = self.current_step() % 4 == 0

    # Train Discriminator
    generated_images = self.get_training_image_batch()
    fake_output_loss, fake_q_loss = self.disc.forward(generated_images)

    image_batch = []
    while len(image_batch) < self.batch_size:
      # If we're at the end of the dataset, we'll get an incomplete batch.
      image_batch = next(self.loader).cuda(self.device)
    image_batch.requires_grad_()
    real_output_loss, real_q_loss = self.disc.forward(image_batch)

    disc_loss = self.D_loss_fn(real_output_loss, fake_output_loss)

    if apply_gradient_penalty:
      gp = self.gradient_penalty(image_batch, real_output_loss)
      gp_val = gp.item()
      self.track_loss('GP', gp_val)
      disc_loss += gp

    disc_loss_value = disc_loss.item()
    self.track_loss('D', disc_loss_value)
    self.disc.backward(disc_loss)
    self.disc.step()

    # Train Generator
    generated_images = self.get_training_image_batch()
    fake_output_loss, _ = self.disc.forward(generated_images)
    real_output_loss = None

    gen_loss = self.G_loss_fn(fake_output_loss, real_output_loss)
    gen_loss_value = gen_loss.item()
    self.track_loss('G', gen_loss_value)
    self.gen.backward(gen_loss)
    self.gen.step()

    # Only run some things once per global_step.
    # Right at the very start of the next step, before we do any work.
    if self.current_microstep() == 0:
      # Joint lookahead update
      if self.lookahead and (self.current_step() + 1) % self.lookahead_k == 0:
        self.gen_opt.lookahead_step()
        self.disc_opt.lookahead_step()

      # EMA update
      if self.is_primary and (self.current_step() + 1) % self.ema_k == 0:
        self.ema.update_ema(self.gen, self.gen_ema)

  def current_iteration(self):
    # Essentially gen.micro_steps.
    # However, gen.micro_steps is reset to 0 at the start of every new training session, it's not loaded from the checkpoint.
    # So we have to calculate the current iteration instead.
    return self.gen.global_steps * self.gen.gradient_accumulation_steps() + self.current_microstep()

  def current_step(self):
    return self.gen.global_steps

  def current_microstep(self):
    return self.gen.micro_steps % self.gen.gradient_accumulation_steps()

  def train(self):
    iter = forever()

    if self.is_primary:
      iter = tqdm(iter, desc='Training')

    self.gen.train()
    self.disc.train()

    for _ in iter:
      # [All] Checkpoint
      if self.current_step() % self.checkpoint_every == 0 and self.current_microstep() == 0:
        gen_dir, disc_dir = self.get_checkpoint_dirs(create_if_needed=True)

        # TODO: Save EMA as part of generator checkpoint
        self.gen.save_checkpoint(save_dir=gen_dir)
        self.disc.save_checkpoint(save_dir=disc_dir)

      # [Primary] Generate training results
      if self.is_primary:
        if self.current_step() % self.evaluate_every == 0 and self.current_microstep() == 0:
          eval_id = self.current_step() // self.evaluate_every
          self.generate(eval_id)

      # [All] Step
      self.step()

      # [Primary] Update progress bar
      if self.is_primary:
        postfix = {'microstep': self.current_microstep(), 'step': self.current_step(), **self.losses}
        iter.set_postfix(postfix)

  def evaluate_in_chunks(self, gen, all_style, all_noise):
    imgs = []
    total_images = all_style[0][0].size(0)

    for start in range(0, total_images, self.batch_size):
      end = min(start + self.batch_size, total_images)

      # print(f'Generating {start} - {end} / {total_images}')
      style = [(s[0][start:end], s[1]) for s in all_style]
      noise = all_noise[start:end]
      next_images = gen.forward(style, noise)

      imgs.append(next_images)

    return torch.cat(imgs, dim=0)

  @torch.no_grad()
  def generate(self, eval_id):
    # Colab GDrive bug:
    # If you torchvision.utils.save_image() to a file that already exists in GDrive, it won't delete the old file.
    # It'll create a separate file with the same name alongside the old file. Because apparently that's a desirable feature?
    # Workaround: Delete the old file first.
    def save_image_delete_before_overwrite(images, output_file, nrow):
      if os.path.exists(output_file):
        os.remove(output_file)

      torchvision.utils.save_image(images, output_file, nrow=nrow)

    dest_dir = os.path.join(self.results_dir, self.model_name)
    if not os.path.exists(dest_dir):
      os.makedirs(dest_dir)

    num_rows = 8

    # Re-use latents across generators.
    total_latents = num_rows ** 2
    style = noise_list(total_latents, self.num_layers, self.latent_dim, self.device)
    noise = image_noise(total_latents, self.image_size, self.device)

    save_image_delete_before_overwrite(self.evaluate_in_chunks(self.gen, style, noise), os.path.join(dest_dir, f'{eval_id}.png'), num_rows)
    save_image_delete_before_overwrite(self.evaluate_in_chunks(self.gen_ema, style, noise), os.path.join(dest_dir, f'{eval_id}_ema.png'), num_rows)

    # Mixed latents / mixing regularities
    def tile(a, dim, n_tile):
      init_dim = a.size(dim)
      repeat_idx = [1] * a.dim()
      repeat_idx[dim] = n_tile
      a = a.repeat(*(repeat_idx))
      order_index = torch.LongTensor(np.concatenate([init_dim * np.arange(n_tile) + i for i in range(init_dim)])).cuda(self.device)
      return torch.index_select(a, dim, order_index)

    # Mix the latents of the first num_rows elements.
    style = style[0][0][:num_rows]
    tmp1 = tile(style, 0, num_rows)
    tmp2 = style.repeat(num_rows, 1)
    mixed_layer_count = self.num_layers // 2
    mixed_latents = [(tmp1, mixed_layer_count), (tmp2, self.num_layers - mixed_layer_count)]

    save_image_delete_before_overwrite(self.evaluate_in_chunks(self.gen, mixed_latents, noise), os.path.join(dest_dir, f'{eval_id}_mr.png'), num_rows)
    save_image_delete_before_overwrite(self.evaluate_in_chunks(self.gen_ema, mixed_latents, noise), os.path.join(dest_dir, f'{eval_id}_mr_ema.png'), num_rows)

  def load(self):
    # Load checkpoint, if it exists
    gen_dir, disc_dir = self.get_checkpoint_dirs()

    if (not os.path.exists(gen_dir)) or (not os.path.exists(disc_dir)):
      print("No model found to load. Starting fresh!")
      return

    # TODO: Check/override settings from constructor
    if self.is_primary:
      print("Loading from checkpoint...")

    gen_tag = self.gen_load_from if len(self.gen_load_from) > 0 else None
    disc_tag = self.disc_load_from if len(self.disc_load_from) > 0 else None

    # TODO: Load EMA as part of generator checkpoint
    self.gen.load_checkpoint(load_dir=gen_dir, tag=gen_tag)
    self.disc.load_checkpoint(load_dir=disc_dir, tag=disc_tag)

    if self.is_primary:
      print(f"Resuming from step {self.current_step()}, iteration: {self.current_iteration()}")

class Trainer():
  def __init__(self):
    pass

  def train(
    self,
    args,
  ):
    rank = args.local_rank
    ttur_mult = 2.

    gen = create_generator(args, rank)
    disc = stylegan2.AugmentedDiscriminator(args.image_size, network_capacity=args.network_capacity).cuda(rank)

    gen_opt = Adam(gen.parameters(), lr=args.learning_rate, betas=(0.5, 0.9))
    disc_opt = Adam(disc.parameters(), lr=args.learning_rate * ttur_mult, betas=(0.5, 0.9))

    if args.lookahead == True:
      gen_opt = Lookahead(gen_opt, alpha=args.lookahead_alpha)
      disc_opt = Lookahead(disc_opt, alpha=args.lookahead_alpha)

    # Setup dataset
    dataset = Dataset(args.data_dir, args.image_size)

    # Initialize deepspeed
    gen_engine, gen_opt, dataloader, *_ = deepspeed.initialize(args=args, model=gen, optimizer=gen_opt, training_data=dataset)
    disc_engine, disc_opt, *_ = deepspeed.initialize(args=args, model=disc, optimizer=disc_opt)

    # Setup dataloaders
    loader = cycle(dataloader)

    run = TrainingRun(
      gen=gen_engine,
      gen_opt=gen_opt,
      disc=disc_engine,
      disc_opt=disc_opt,
      args=args,
      loader=loader)

    if not args.new:
      run.load()

    run.train()


from django.db import models, transaction
from django.db.models import Sum, F
from rest_framework import serializers

from product.models import Category, Tag, CartItem, Product, Cart

class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = ['title', 'order']

class TagSerializer(serializers.ModelSerializer):

    class Meta:
        model = Tag
        fields = ['title', 'product']
        extra_kwargs = {
            'product': {"write_only": True},
        }

class ProductSerializer(serializers.ModelSerializer):
    tag = TagSerializer(many=True, read_only=True)

    class Meta:
        model = Product
        fields = ['id', 'title', 'slug', 'price', 'description', 'is_published', 'category', 'tag']

        extra_kwargs = {
            'category': {"write_only": True},
            'slug': {'read_only': True},
        }

class CreateProductSerializer(serializers.ModelSerializer):
    tags = TagSerializer(many=True)

    class Meta:
        model = Product
        fields = ['id', 'title', 'slug', 'price', 'description', 'is_published', 'category', 'tag']

    def create(self, validated_data):
        tags = validated_data.pop('tags')

        tag_list = []
        for tag in tags:
            title = tag.pop('title')
            tag_list.append(Tag(title=title))

        with transaction.atomic():
            instance = super().create(validated_data)
            if tag_list:
                tag_object = Tag.objects.bulk_create(tag_list)
                instance.tag.set(tag_object)
        return instance

class CartItemSerializer(serializers.ModelSerializer):
    product = ProductSerializer()

    class Meta:
        model = CartItem
        fields = ['product', 'quantity', 'order', 'active', 'cart']

class CartSerializer(serializers.ModelSerializer):
    items = CartItemSerializer(many=True)
    total_cost = serializers.SerializerMethodField()

    def get_total_cost(self, obj):
        total_price = CartItem.objects.filter(cart=obj, active=True,).\
            aggregate(total_price=Sum(F('product__price') * F('quantity'),
                                      output_field=models.DecimalField()))['total_price']
        return total_price

    class Meta:
        model = Cart
        fields = ['id', 'items', 'total_cost']

class AddToCartSerializer(serializers.ModelSerializer):

    class Meta:
        model = CartItem
        fields = ['quantity']


from fast_api_app import (
    schemas,
    models
)
from sqlalchemy.orm import Session
from fast_api_app.database import database

items = models.Item.__table__
users = models.UserTable.__table__

async def get_user(user_id: int):
    query = users.select().where(models.UserTable.id == user_id)
    resultset = await database.fetch_one(query)
    return dict(resultset)

async def create_user_item(item: schemas.ItemCreate, user_id: int):
    query = items.insert().values(title=item.title, description=item.description,
                                  price=item.price, owner_id=user_id)
    last_record_id = await database.execute(query)
    return {"id": last_record_id, **item.dict(), "owner_id": user_id}

async def get_items(user_id: int):
    query = items.select().where(models.Item.owner_id == user_id)
    resultset = await database.fetch_all(query)
    items_list = list()
    for item in resultset:
        items_list.append(dict(item))

    return items_list


#!/usr/bin/env python
# encoding: UTF-8

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

import collections

class Associations:

    def __init__(self):
        self.lookup = collections.OrderedDict([])

    def clear(self):
        for rels in self.lookup.values():
            for objs in rels.values():
                objs.clear()

    def ensemble(self, *args, **kwargs):
        return self.lookup.keys()

    def register(self, rel, primary, *args, **kwargs):
        subjects = set(args)
        for obj in {primary} | subjects:
            if obj not in self.lookup:
                self.lookup[obj] = collections.defaultdict(set)
        if rel is not None:
            self.lookup[primary][rel].update(subjects)
        return self

    def search(self, **kwargs):
        """
            Return objects matching all of the supplied parameter criteria.

        """
        return set(
            i for i in self.lookup.keys()
            if all(getattr(i, k, None) == v for k, v in kwargs.items())
        )

    def match(self, obj, forward=[], reverse=[], predicate=lambda x: True):
        """
            Return objects related to `obj` by at least one of the supplied
            relationships.

            :param forward: A sequence of forward relationships
            :param reverse: A sequence of reverse relationships
            :param predicate: A function with a single argument to filter matches

        """
        return {
            match for rel in forward for match in self.lookup[obj][rel]
            if predicate(match)
        } | {
            match
            for match in self.lookup
            for rel in reverse
            if predicate(match) and obj in self.lookup[match][rel]
        }


from PIL import Image
import cv2

cap = cv2.VideoCapture(0)
scaling_factor = 1

while True:
    ret, frame = cap.read()
    rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
    cv2.imwrite('capture.jpg', frame)

    input_image = Image.open("capture.jpg")
    width, height = input_image.size

    for i in range(2):
        for j in range(2):
            print (input_image.getpixel((i, j)))
            print ("\n")

    print (rgb[0][0])

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()


"""
In this code I create a recommendation system using hybrid method.The hybrid here uses model based
collaborative filtering as the base. To improve the performance of model based CF I performed data
preprocessing so as to have more information from the same data. I have performed Principal Component
Analysis to increase data interpretability and dimension reduction. I used only the 10 most important
features thus generated. Over model based CF, I then utitlised item based collaborative filtering. I
used prediction generared by item based as one of the feature to model based CF.

Error Distribution:
>=0 and <1: 105773
>=1 and <2: 34123
>=2 and <3: 6310
>=3 and <4: 790
>=4: 0

RSME: 0.9782285911303371
Duration: 861.13
"""

from sklearn.model_selection import train_test_split
from sklearn.decomposition import PCA
from pyspark import SparkContext, SparkConf
from datetime import datetime
from itertools import chain
import numpy as nmpy
import pandas as pnds
import json
import time
import math
import sys

NAN = [nmpy.nan]
result_test_data = dict()
result_train_data = dict()

def item_based(bid_uid_dblt, similarity):
    if bid_uid_dblt[1] not in training_data_uid:
        if bid_uid_dblt[0] in bid_actRates:
            temp = bid_actRates[bid_uid_dblt[0]]
            users_oth = list(temp.values())
            avrage_busId = sum(users_oth) / len(users_oth)
            return bid_uid_dblt, avrage_busId
        else:
            return bid_uid_dblt, 3.75

    rate_uid = training_data_uid[bid_uid_dblt[1]]

    ratings = []
    if bid_uid_dblt[0] not in bid_meanRates:
        for ind in rate_uid:
            ratings.append(rate_uid[ind])
        avrage_usrId = sum(ratings)/len(ratings)
        return bid_uid_dblt, avrage_usrId

    to_rate = bid_meanRates[bid_uid_dblt[0]]
    to_rate_set = set(to_rate.keys())

    sim_list = []
    rating_list = []

    for ind in rate_uid:
        ind_rate = (ind, rate_uid[ind])
        neighbor = bid_meanRates[ind_rate[0]]
        common_vals = set(neighbor.keys()) & to_rate_set
        if len(common_vals) < 3:
            continue
        dblt = tuple(sorted([ind_rate[0], bid_uid_dblt[0]]))
        if dblt in similarity:
            sim = similarity[dblt]
            sim_list.append(sim)
            rating_list.append(ind_rate[1])
        else:
            parta = 0
            partb = 0
            for intr in common_vals:
                parta += to_rate[intr] * to_rate[intr]
                partb += neighbor[intr] * neighbor[intr]
            parta = math.sqrt(parta)
            partb = math.sqrt(partb)
            sim_den = parta * partb
            if sim_den == 0:
                similarity[dblt] = 0
                continue
            sim = 0
            for intr in common_vals:
                sim += to_rate[intr] * neighbor[intr]
            sim = sim / sim_den
            if not sim < 0:
                sim = sim * (1 + 0.1)
            else:
                eph = (1 - 0.1) * abs(sim)
                sim += eph

            similarity[dblt] = sim
            sim_list.append(sim)
            rating_list.append(ind_rate[1])

    if sim_list == []:
        users_oth = list(bid_actRates[bid_uid_dblt[0]].values())
        avrage_busId = sum(users_oth)/len(users_oth)
        return bid_uid_dblt, avrage_busId
    elif len(sim_list) >= 5:
        sim_rating = sorted(tuple(zip(sim_list, rating_list)), key=lambda x: x[0])[0:5]
        den = 0
        num = 0
        for sr in sim_rating:
            den += abs(sr[0])
            num += sr[0] * sr[1]
        if num <= 25:
            temp = bid_actRates[bid_uid_dblt[0]]
            users_oth = list(temp.values())
            avrage_busId = sum(users_oth) / len(users_oth)
            return bid_uid_dblt, avrage_busId
        else:
            pred_val = num / den
            return bid_uid_dblt, pred_val
    else:
        temp = bid_actRates[bid_uid_dblt[0]]
        users_oth = list(temp.values())
        avrage_busId = sum(users_oth) / len(users_oth)
        return bid_uid_dblt, avrage_busId

"""
 Finds mean rating per business
 Finds normalised rating (actual rate - mean) and appends to res
 Appends actual rating to result
"""
def normalise_ratings(bid_listuid):
    total = 0
    for uids_strs in bid_listuid[1]:
        total += uids_strs[1]
    rBar = total / len(bid_listuid[1])

    res=[]
    for uids_strs in bid_listuid[1]:
        res.append((uids_strs[0], uids_strs[1]-rBar))

    k=[]
    for uids_strs in bid_listuid[1]:
        k.append((uids_strs[0], uids_strs[1]))
    return bid_listuid[0],dict(k),dict(res)

def get_matrix_train(val, uids_dfs, bids_dfs):
    list2 = [result_train_data[(val[0], val[1][0])]]
    if val[1][0] in uids_dfs.index:
        if val[0] in bids_dfs.index:
            list1 = list(uids_dfs.loc[val[1][0]])
            return  list1 + list(bids_dfs.loc[val[0]]) +  list2
        else:
            return list1 + NAN * bids_dfs.shape[1] + list2
    elif val[1][0] not in uids_dfs.index:
        shp = uids_dfs.shape[1]
        if val[0] in bids_dfs.index:
            return NAN * shp + list(bids_dfs.loc[val[0]]) + list2
    else:
        return NAN * shp + NAN * bids_dfs.shape[1] + list2

def get_matrix_test(val, uids_dfs, bids_dfs):
    uds = uids_dfs.shape[1]
    rtd = [result_test_data[val]]
    if val[1] in uids_dfs.index:
        if val[0] in bids_dfs.index:
            return list(uids_dfs.loc[val[1]]) + list(bids_dfs.loc[val[0]]) + rtd
        else:
            return list(uids_dfs.loc[val[1]]) + NAN * bids_dfs.shape[1] + rtd
    elif val[1] not in uids_dfs.index:
        if val[0] in bids_dfs.index:
            return NAN * uds + list(bids_dfs.loc[val[0]]) + rtd
    else:
        return NAN * uds + NAN * bids_dfs.shape[1] + rtd

def read_user(input_line):
    uid = input_line["user_id"]
    avg_str = input_line["average_stars"]
    rev_count = input_line["review_count"]
    fans = input_line["fans"]
    frnds = input_line["friends"]
    yelp_since = input_line["yelping_since"]
    useful = input_line["useful"]
    funny = input_line["funny"]
    cool = input_line["cool"]
    comp_hot = input_line["compliment_hot"]
    comp_more = input_line["compliment_more"]
    comp_prof = input_line["compliment_profile"]
    comp_cute = input_line["compliment_cute"]
    comp_lst = input_line["compliment_list"]
    comp_note = input_line["compliment_note"]
    comp_pl = input_line["compliment_plain"]
    comp_cool = input_line["compliment_cool"]
    comp_fun = input_line["compliment_funny"]
    comp_write = input_line["compliment_writer"]
    comp_ph = input_line["compliment_photos"]
    return uid, avg_str, rev_count, fans, frnds, yelp_since, useful, funny, cool, comp_hot, comp_more, comp_prof, comp_cute, comp_lst, comp_note, comp_pl, comp_cool, comp_fun, comp_write, comp_ph

def map_user(inpt):
    uid = user_ids_srno[inpt[0]]
    avg_str = float(inpt[1])
    rev_count = int(inpt[2])
    fans = int(inpt[3]) * int(inpt[3])
    frnds = 0
    if inpt[4] != "None":
        val = len(inpt[4].split(","))
        frnds = val * val
    yelp_since = (datetime.now() - datetime.strptime(inpt[5], "%Y-%m-%d")).days
    useful = int(inpt[6])
    funny = int(inpt[7])
    cool = int(inpt[8])
    comp_hot = int(inpt[9])
    comp_more = int(inpt[10])
    comp_prof = int(inpt[11])
    comp_cute = int(inpt[12])
    comp_lst = int(inpt[13])
    comp_note = int(inpt[14])
    comp_pl = int(inpt[15])
    comp_cool = int(inpt[16])
    comp_fun = int(inpt[17])
    comp_write = int(inpt[18])
    comp_ph = int(inpt[19])

    return uid, avg_str, rev_count, fans, frnds, yelp_since, useful, funny, cool, comp_hot, comp_more, comp_prof, comp_cute, comp_lst, comp_note, comp_pl, comp_cool, comp_fun, comp_write, comp_ph

def read_business(input_line):
    bid = input_line["business_id"]
    star = input_line["stars"]
    rev_count = input_line["review_count"]
    state = input_line["state"]
    cat = input_line["categories"]
    city = input_line["city"]
    lat = input_line["latitude"]
    longi = input_line["longitude"]
    hrs = input_line["hours"]
    return bid, star, rev_count, state, cat, city, lat, longi, hrs

def map_business(inpt):
    bid = business_ids_srno[inpt[0]]
    star = float(inpt[1])
    rev_count = int(inpt[2])
    state = inpt[3]
    cat = set()
    if inpt[4]:
        cat = set(inpt[4].split(", "))
    city = inpt[5]
    lat = float(inpt[6])
    longi = float(inpt[7])
    hrs = set()
    if inpt[8]:
        hrs = set(inpt[8].keys())
    return bid, star, rev_count, state, cat, city, lat, longi, hrs

def itemBasedCF():
    tdc = testing_data.collect()
    similarity = {}
    for val in tdc:
        bid_uid_dblt, pred_val = item_based(val, similarity)
        result_test_data[bid_uid_dblt] = pred_val

    inp_dta = training_data.map(lambda val: (val[0], val[1][0])).collect()
    for dblt in inp_dta:
        pair_y = dblt[1]
        pair_x = dblt[0]
        stars_training_data_uid = training_data_uid[pair_y].pop(pair_x)
        if training_data_uid[pair_y] == dict():
            training_data_uid.pop(pair_y)

        stars_mean_bid_meanRates = bid_meanRates[pair_x].pop(pair_y)
        if bid_meanRates[pair_x] == dict():
            bid_meanRates.pop(pair_x)

        stars_bid_actRates = bid_actRates[pair_x].pop(pair_y)
        if bid_actRates[pair_x] == dict():
            bid_actRates.pop(pair_x)

        bid_uid_dblt, pred_val = item_based(dblt, similarity)
        result_train_data[bid_uid_dblt] = pred_val

        if pair_y not in training_data_uid:
            training_data_uid[pair_y] = dict()
            training_data_uid[pair_y][pair_x] = stars_training_data_uid
        else:
            training_data_uid[pair_y][pair_x] = stars_training_data_uid

        if pair_x not in bid_meanRates:
            bid_meanRates[pair_x] = dict()
            bid_meanRates[pair_x][pair_y] = stars_mean_bid_meanRates
        else:
            bid_meanRates[pair_x][pair_y] = stars_mean_bid_meanRates

        if pair_x not in bid_actRates:
            bid_actRates[pair_x] = dict()
            bid_actRates[pair_x][pair_y] = stars_bid_actRates
        else:
            bid_actRates[pair_x][pair_y] = stars_bid_actRates

    print("Finished Item-based pred_val")

if __name__ == """__main__""":
    folder_path = sys.argv[1]
    test_path = sys.argv[2]
    st = time.time()
    conf = SparkConf().setMaster("local").set("spark.executor.memory", "4g").set("spark.driver.memory", "4g")
    sc = SparkContext(conf=conf)
    sc.setLogLevel("ERROR")

    # reading and preprocessing training data
    train_file = sc.textFile(folder_path + "yelp_train.csv")
    train_data_raw = train_file.filter(lambda val: not val.endswith("stars"))
    training_data = train_data_raw.map(lambda line: (line.split(',')[1], line.split(',')[0], line.split(',')[2]))
    busIds_train = training_data.map(lambda val: val[0]).distinct()
    distinct_bids_train = set(busIds_train.collect())
    uids_train = training_data.map(lambda val: val[1]).distinct()
    distinct_uids_train = set(uids_train.collect())

    # reading and preprocessing testing data
    test_file = sc.textFile(test_path)
    test_data_raw = test_file.filter(lambda val: not val.endswith("stars"))
    testing_data = test_data_raw.map(lambda line: (line.split(',')[1], line.split(',')[0]))
    bids_test = testing_data.map(lambda val: val[0]).distinct()
    distinct_bids_test = set(bids_test.collect())
    uid_test = testing_data.map(lambda val: val[1]).distinct()
    distinct_uids_test = set(uid_test.collect())

    business_ids = distinct_bids_train | distinct_bids_test
    business_ids_copy = business_ids
    srno_business_ids = dict(enumerate(business_ids))
    enum_business_ids_copy = enumerate(business_ids_copy)
    business_ids_srno = dict(map(lambda val:val[::-1], enum_business_ids_copy))

    user_ids = distinct_uids_train | distinct_uids_test
    user_ids_copy = user_ids
    srno_user_ids = dict(enumerate(user_ids))
    enum_user_ids_copy = enumerate(user_ids_copy)
    user_ids_srno = dict(map(lambda val:val[::-1], enum_user_ids_copy))

    training_data = training_data.map(lambda val: (business_ids_srno[val[0]], (user_ids_srno[val[1]], int(val[2][0])))).cache()
    testing_data =  testing_data.map(lambda val: (business_ids_srno[val[0]], user_ids_srno[val[1]])).cache()

    bid_uid_str_grpd = training_data.groupByKey()
    bid_uid_str_lst = bid_uid_str_grpd.map(lambda val: (val[0], list(val[1])))
    bid_uid_str_normalisd = bid_uid_str_lst.map(normalise_ratings).collect()
    a=[]
    b=[]
    c=[]
    for val in bid_uid_str_normalisd:
        a.append(val[0])
        b.append(val[1])
        c.append(val[2])
    bid_actRates = dict(zip(a,b))
    bid_meanRates = dict(zip(a,c))

    training_data_uid = training_data.map(lambda val: (val[1][0], (val[0], val[1][1]))).groupByKey()
    training_data_uid = training_data_uid.map(lambda val: (val[0], list(val[1])))
    training_data_uid = training_data_uid.map(lambda val: (val[0], dict(zip([v[0] for v in val[1]], [v[1] for v in val[1]])))).collect()
    a = []
    b = []
    for td in training_data_uid:
        a.append(td[0])
        b.append(td[1])
    training_data_uid = dict(zip(a, b))

    print("Performing Item based pred_val.")
    itemBasedCF()

    print("Reading files for Model based Collaborative Filtering.")
    usrIds = []
    with open(folder_path + "user.json") as file_reader:
        while True:
            data = file_reader.readlines(5000000)
            if not data:
                break
            uid_file_data = sc.parallelize(data).map(lambda val: json.loads(val))
            uid_file_data = uid_file_data.map(read_user)
            uid_file_data = uid_file_data.filter(lambda val: val[0] in user_ids_srno).map(map_user)
            usrIds += uid_file_data.collect()

    bid_file_data = sc.textFile(folder_path + "business.json")
    bid_file_data = bid_file_data.map(lambda val: json.loads(val))
    bid_file_data = bid_file_data.map(read_business)

    bid_file_data = bid_file_data.filter(lambda val: val[0] in business_ids_srno)

    bid_file_data = bid_file_data.map(map_business)
    busIds = bid_file_data.collect()

    cols_uid = ["user_id", "average_stars", "review_count", "fans_sqr", "friends_sqr", "yelping_since", "useful", "funny", "cool", "compliment_hot", "compliment_more", "compliment_profile", "compliment_cute", "compliment_list", "compliment_note", "compliment_plain", "compliment_cool", "compliment_funny", "compliment_writer", "compliment_photos"]
    cols_bid = ["business_id", "stars", "review_count", "state", "categories", "city", "latitude", "longitude", "hours"]
    uids_dfs = pnds.DataFrame(usrIds, columns = cols_uid)
    bids_dfs = pnds.DataFrame(busIds, columns = cols_bid)

    #-------------------------------------------
    print("Performing PCA.")
    princCompAnalysis = PCA(n_components = 5, svd_solver = 'full')
    xarr = uids_dfs.iloc[:, 9:]
    x = nmpy.array(xarr)
    print(x.shape)
    compl = princCompAnalysis.fit_transform(x)
    print(compl.shape)
    compl = nmpy.transpose(compl)
    len_compl = len(compl)
    for lc in range(len_compl):
        indx = "compliment_" + str(lc)
        uids_dfs[indx] = compl[lc]
    uids_dfs = uids_dfs.drop(columns = ["compliment_hot", "compliment_more", "compliment_profile", "compliment_cute", "compliment_list", "compliment_note", "compliment_plain", "compliment_cool", "compliment_funny", "compliment_writer", "compliment_photos"])

    print(bids_dfs.city.unique())
    city = pnds.get_dummies(bids_dfs.city, prefix = "city")
    city = nmpy.array(city)
    print(city.shape)
    princCompAnalysis = PCA(n_components = 10, svd_solver = 'full')
    city = princCompAnalysis.fit_transform(city)
    print(city.shape)
    city = nmpy.transpose(city)
    ctlen = len(city)
    for ct in range(ctlen):
        indx = "city_" + str(ct)
        bids_dfs[indx] = city[ct]
    bids_dfs = bids_dfs.drop(columns = ["city"])

    state = pnds.get_dummies(bids_dfs.state, prefix = "state")
    cols = [state.columns[-1]]
    state = state.drop(columns = cols)
    concat_lst = [bids_dfs, state]
    bids_dfs = pnds.concat(concat_lst, axis = 1)
    bids_dfs = bids_dfs.drop(columns = ["state"])

    set_cat = set()
    mat_cats = []
    for bidcat in bids_dfs.categories:
        set_cat = set(chain(set_cat, bidcat))
    for categs in set_cat:
        val1 = bids_dfs.categories.apply(lambda val: (val & set([categs])))
        val1 = val1.apply(lambda val: 0 if val == set() else 1)
        mat_cats.append(val1)
    mat_cats = nmpy.transpose(nmpy.array(mat_cats))
    princCompAnalysis = PCA(n_components=10, svd_solver='full')
    mat_cats = princCompAnalysis.fit_transform(mat_cats)
    mat_cats = nmpy.transpose(mat_cats)
    len_mc = len(mat_cats)
    for mc in range(len_mc):
        bids_dfs["category_" + str(mc)] = mat_cats[mc]
    bids_dfs = bids_dfs.drop(columns = ["categories"])

    weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
    days_mat = []
    for wd in weekdays:
        temp = bids_dfs.hours.apply(lambda val: (val & set([wd])))
        temp = temp.apply(lambda val: 0 if val == set() else 1)
        days_mat.append(temp)
    len_dm = len(days_mat)
    for ds in range(len_dm):
        ind = "Day_"+str(ds)
        bids_dfs[ind] = days_mat[ds]
    bids_dfs = bids_dfs.drop(columns=["hours"])

    print("Finished reading files for model-based CF")

    uids_dfs_pred = uids_dfs.set_index("user_id")
    bids_dfs_pred = bids_dfs.set_index("business_id")
    X = []
    for train_data in training_data.collect():
        temp = get_matrix_train(train_data, uids_dfs_pred, bids_dfs_pred)
        X.append(temp)

    X = nmpy.array(X)
    test_x_val = []
    for test_data in testing_data.collect():
        temp = get_matrix_test(test_data, uids_dfs_pred, bids_dfs_pred)
        test_x_val.append(temp)
    test_x_val = nmpy.array(test_x_val)
    temp = training_data.map(lambda val: val[1][1]).collect()
    Y = nmpy.array(temp)

    print(X.shape)
    print("Finished preparing X and Y")

    et = time.time()
    print(f"Duration: {et-st}")

    import xgboost as xgb
    from joblib import dump, load

    xgbr = load("model.md")

    estim_y_val = xgbr.predict(test_x_val)

    model_fin = ""
    rows = testing_data.collect()
    row_len = len(rows)
    for v in range(row_len):
        model_fin += srno_user_ids[rows[v][1]] + "," + srno_business_ids[rows[v][0]] + ","
        if estim_y_val[v] < 1:
            model_fin += str(1)
        elif estim_y_val[v] > 5:
            model_fin += str(5)
        else:
            model_fin += str(estim_y_val[v])
        model_fin += "\n"

    output = "user_id, business_id, stars\n"
    output += model_fin

    with open(sys.argv[3], "w") as out_file:
        out_file.writelines(output)

    et = time.time()
    print(f"Duration: {et-st}")


from tkinter import *
import pandas
FONT = "Roboto"
FONT_SIZE = 25

window = Tk()
window.title("Flash cards app")
window.config(pady=25, padx=25)

data = pandas.read_csv("data/french_words.csv")
data_dict = {row.French: row.English for (index, row) in data.iterrows()}
word_list = [ds for ds in data_dict.keys()]

count = 0
def generate_word():
    global count
    x = word_list[count]
    if count <= len(word_list) + 1:
        count += 1
    return x

print(generate_word())

def remove_from_list():
    print("removed form list")
    generate_word()

def load_more():
    generate_word()

def flip_card():
    generate_word()
    if flash_card["text"] == generate_word():
        flash_card["background"] = "white"
        flash_card["text"] = data_dict[generate_word()]
        flash_card['fg'] = "black"
    elif flash_card["text"] == data_dict[generate_word()]:
        flash_card["background"] = "#e2979c"
        flash_card["text"] = generate_word()
        flash_card['fg'] = "white"

flash_card = Button(text=generate_word(), background="#e2979c", font=(FONT,FONT_SIZE), fg="white", width=30, height=10, command=flip_card)
flash_card.pack()

check_btn = Button(text="Yes", background="#9bdeac", fg="white", width=6, height=2, font=(FONT,12, 'bold'), command=remove_from_list)
check_btn.pack(side="left", pady=30)

wrong_btn = Button(text="No", background="#e7305b", fg="white", width=6, height=2, font=(FONT,12, 'bold'), command=generate_word)
wrong_btn.pack(side="right", pady=30)

window.mainloop()


## IMPORTS

from .item import Item, items

from messages import SayText2

class Small_Backpack(Item):
    name = 'Small Backpack'
    item_type = 'backpack'
    add_weight = 25
    weight = 0.0

    def can_be_used(self, player):
        'Check if item can be used.'
        return True if player.backpack is None or self.add_weight > player.backpack.add_weight else False

    def on_pickup(self, player):
        'Upon the item being picked up.'
        if not player.inventory.add(self):
            return False

        if self.can_be_used(player):
            self.on_use(player)

        self.on_remove()
        return True

    def on_remove(self):
        'Called upon the wanted removal of the entity.'
        if self.entity is None:
            return

        if self in items:
            items.remove(self)
        self.entity.remove()

    def on_use(self, player):
        'Called upon the item being used.'
        if not self.can_be_used(player):
            return False

        player.backpack = self
        player.inventory.max_weight += self.add_weight
        SayText2('Equip ' + self.__class__.__name__).send()
        player.inventory.discard(self)
        return True


import torch
from djl_python import Input
from djl_python import Output

from transformers_model import BaseNLPModel

class TokenClassification(BaseNLPModel):

    def __init__(self):
        super().__init__("token_classification")
        self.label_list = self.mapping["label_list"].strip('][').split(', ')

    def preprocess(self, data: Input):
        input_text = data.get_as_string()
        tokens = self.tokenizer.encode_plus(input_text,
                                            max_length=self.max_length,
                                            truncation=True,
                                            padding=True,
                                            add_special_tokens=True,
                                            return_tensors='np')
        input_ids = tokens["input_ids"]
        attention_mask = tokens["attention_mask"]
        outputs = Output()
        outputs.add_as_numpy([input_ids, attention_mask])
        return outputs

    def postprocess(self, data: Input):
        input_ids = data.get_as_numpy("input_ids")[0].tolist()
        predictions = torch.from_numpy(data.get_as_numpy("data")[0])
        num_rows = predictions.shape[0]

        outputs = Output()
        result = []
        for i in range(num_rows):
            out = predictions[i].unsqueeze(0)
            out = torch.argmax(out, dim=2)
            tokens = self.tokenizer.tokenize(self.tokenizer.decode(input_ids[i]))
            prediction = [(token, self.label_list[prediction]) for token, prediction in
                          zip(tokens, out[0].tolist())]
            result.append(prediction)

        outputs.add_as_json(result)
        return outputs


import numpy as np
import cv2 as cv

img = cv.imread('C:/Users/DELL/Pictures/Screenshots/CM.png')
greyed = cv.cvtColor(img , cv.COLOR_BGR2GRAY)
edges = cv.Canny(greyed , 125 , 175)
cv.imshow('Edges', edges)
blank = np.zeros(img.shape,dtype='uint8')
contours , hierarchies = cv.findContours(edges , cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
cv.drawContours(blank , contours , -1, (0 , 255, 0),thickness=1)
cv.imshow('Contoured', blank)
cv.waitKey(0)


from typing import Any, Union

import ignite.distributed as idist
import torch
from ignite.engine import DeterministicEngine, Engine, Events
from torch.cuda.amp import autocast
from torch.nn import Module
from torch.optim import Optimizer
from torch.utils.data import DistributedSampler, Sampler

def setup_trainer(
    config: Any,
    model: Module,
    optimizer: Optimizer,
    loss_fn: Module,
    device: Union[str, torch.device],
    train_sampler: Sampler,
) -> Union[Engine, DeterministicEngine]:
    def train_function(engine: Union[Engine, DeterministicEngine], batch: Any):
        if config.overfit:
            # No batch norm
            model.eval()
        else:
            model.train()

        samples = batch[0].to(device, non_blocking=True)
        targets = batch[1].to(device, non_blocking=True)
        attack_targets = batch[2].to(device, non_blocking=True)
        sample_ids = batch[3].to(device, non_blocking=True)

        with autocast(config.use_amp):
            outputs = model(samples, attack_targets)
            loss = loss_fn(outputs, attack_targets, targets)

        loss.backward()
        optimizer.step()
        optimizer.zero_grad()

        train_loss = loss.item()
        engine.state.metrics = {
            "epoch": engine.state.epoch,
            "train_loss": train_loss,
        }
        return {"train_loss": train_loss}

    trainer = Engine(train_function)

    # set epoch for distributed sa5mpler
    @trainer.on(Events.EPOCH_STARTED)
    def set_epoch():
        if idist.get_world_size() > 1 and isinstance(train_sampler, DistributedSampler):
            train_sampler.set_epoch(trainer.state.epoch - 1)

    return trainer

def setup_evaluator(
    config: Any,
    model: Module,
    device: Union[str, torch.device],
) -> Engine:
    @torch.no_grad()
    def eval_function(engine: Engine, batch: Any):
        model.eval()

        samples, gt_labels, attack_targets, sample_ids = batch

        samples = samples.to(device, non_blocking=True)
        gt_labels = gt_labels.to(device, non_blocking=True)
        attack_targets = attack_targets.to(device, non_blocking=True)
        sample_ids = sample_ids.to(device, non_blocking=True)

        with autocast(config.use_amp):
            outputs, perturbations = model(samples, attack_targets, gt_labels)

        return outputs, attack_targets, {
            "gt_targets": gt_labels,
            "perturbations": perturbations
        }

    return Engine(eval_function)


#!/usr/bin/python3
import sys
import __future__

def temperature_decode(txt):
    if txt == '7fff':
        return None
    return int(txt, 16) / 10.0

def voltage_decode(txt):
    if txt == 'ff':
        return None
    return int(txt, 16) / 10.0

def decode(data):
    length = len(data)
    print(data[0:2])
    print(data[2:6])
    print(data[6:10])
    if length != 10:
        raise Exception("Bad data length, min 10 characters expected")
    return {
        "pir_count": int(data[6:10], 16),
        "temperature": temperature_decode(data[2:6]),
        "voltage": voltage_decode(data[0:2])
    }

def pprint(data):
    print('PIR count :', data['pir_count'])
    print('Voltage :', data['voltage'], 'V')
    print('Temperature :', data['temperature'], '°C')

if __name__ == '__main__':
    if len(sys.argv) != 2 or sys.argv[1] in ('help', '-h', '--help'):
        print("usage: python3 decode.py [data]")
        print("example decode paket: python3 decode.py 0123456789")
        exit(1)

    data = decode(sys.argv[1])
    pprint(data)


#rafi python try 3

#negative phi 2

import rospy
from std_msgs.msg import Int16MultiArray

import math
from colorama import Fore, Style

class RosConnection:
    def __init__(self) -> None:

        self.pub = rospy.Publisher('cmd_angle', Int16MultiArray, queue_size=10)
        rospy.init_node('angle_controller_ik', anonymous=True)
        self.rate = rospy.Rate(10)
        self.busy = False
        self.coords_to_send = []
        self.origin = (700,10)
        self.HEIGHT = 15 # 15 cm
        self.NAMES = {
            'paper': 1,
            'paper_cup' : 1,
            'plastic_bottle': 2,
            'chips_packet': 2,
            'can': 3
        }

    def send_coords(self, coords):
        # @param coords= ['name', x, y]
        self.coords_to_send += coords
        self.busy = True
        print('before for loop')
        for i, (nm, x, y) in enumerate(self.coords_to_send):
            msg = Int16MultiArray()
            print('before try')
            try:
                msg.data = [self.NAMES[nm], *self.calculate_base_rotation(nm, x, y)]
                self.pub(msg)
                print(f"in_send_coords: published")
            except TypeError as e:
                print(f"{Fore.LIGHTRED_EX}In SEND_COORDS:{e}{Style.RESET_ALL}")
                pass
        self.busy = False

    def calculate_base_rotation(self, name, x, y):
        # base_angle = msg.data[0];
        # shoulder_angle = msg.data[1];
        # elbow_angle = msg.data[2];
        # pitch_angle = msg.data[3];
        # grip_close = msg.data[4] > 0;
        base_angle = math.atan((self.origin[0]-x) / (y-self.origin[1]))
        r = math.sqrt((self.origin[0]-x)**2 + (self.origin[1]-y)**2)
        r = r - 16 # 16 cm back from actual object
        z = 5 - self.HEIGHT #Depth from base
        try:
            shoulder_angle, elbow_angle = self.inv_kinematics(r, z)
            pitch_angle = 30
            return base_angle, shoulder_angle, elbow_angle, pitch_angle
        except TypeError as e:
            print(f"{Fore.LIGHTRED_EX}In CALC_BASE_ROTATION:{e}{Style.RESET_ALL}")
            return None

    def inv_kinematics(self, r, z, l1=10, l2=10):
        if not (-1 <= (r**2+z**2-l1**2-l2**2)/(2*l1*l2) <= 1):
            # domain test of acos
            # also limit that is possible for the two links to reach
            return

        # Equations for Inverse kinematics
        phi_2 = (math.acos((r**2+z**2-l1**2-l2**2)/(2*l1*l2)))  # eqn 2
        phi_1 = math.atan2(z, r) + math.atan2(l2*math.sin(phi_2), (l1 + l2*math.cos(phi_2))) # eqn 3
        phi_2 = -phi_2
        #theta_1 = rad2deg(phi_2-phi_1)  # eqn 4 converted to degrees

        #phi_3 = arccos((r1**2-arm1**2-arm1**2)/(-2*arm1*arm2))
        #theta_2 = 180-rad2deg(phi_3)

        theta1 = math.degrees(phi_1)
        theta2 = math.degrees(phi_2)

        #print('theta 1: ', phi_1)
        #print('theta two: ', phi_2)

        return theta1, theta2


import sys
input=sys.stdin.readline
# 20m 25s
def solve(x,y,depth,per):
    # (x좌표,y좌표,행동 횟수,확률)
    global ans
    if depth==N:
        # 행동 횟수가 N번 이면
        ans+=per
        # 정답에 더함
        return

    for i in range(4):
        if p[i]>0:
            # 움직일 확률이 0 초과일 때(움직일 수 있음)
            xx,yy=x+dx[i],y+dy[i]
            # 해당 좌표에 이동 좌표를 더함
            if not visited[xx][yy]:
                # 아직 방문하지 않았으면 단순하므로
                visited[xx][yy]=True
                # 방문 표시 하고
                solve(xx,yy,depth+1,per*p[i])
                # 다른 방향도 탐색함
                visited[xx][yy]=False
                # 다 탐색했으면 되돌려 놓음

N,*p=list(map(int,input().split()))
for i in range(4): p[i]=p[i]*0.01
visited=[[False]*50 for _ in range(50)]
# [50][50]의 이차원 배열에 방문 표시함
visited[25][25]=True
# 시작하는 위치는 처음 방문한 곳이므로 True로 표시
dx,dy=[0,0,1,-1],[1,-1,0,0]
# 이동 방향
ans=0
solve(25,25,0,1)
# 넉넉하게 (25,25)에서 시작
print(ans)


# numpy
import numpy as np

# values for kernel
w_c = -1
w_a = 0.2
w_d = 0.05

# kernel
lap_kernel = np.asarray([[w_d, w_a, w_d], [w_a, w_c, w_a], [w_d, w_a, w_d]])

# test data
dimension = 10
d = np.zeros((dimension, dimension))
d[5:9, 5:9] = 1 # ones in the middle

# now apply the kernel
## for now they just die at the edges
for i in range(dimension-2): #rows
    for j in range(dimension-2): #cols

        # just making it easier in my head.
        x = i+1
        y = j+1

        # find the 3x3 subset of the data around (x, y) point
        d_sub = d[x-1:x+2, y-1:y+2]

        # convolution with laplacian.
        d[x, y] += np.sum(d_sub*lap_kernel)

## turn it into a function
def laplacian(d, laplace_kernel, dimensions):
    for i in range(dimension-2): #rows
        for j in range(dimension-2): #cols

            # just making it easier in my head.
            x = i+1
            y = j+1

            # find the 3x3 subset of the data around (x, y) point
            d_sub = d[x-1:x+2, y-1:y+2]

            # convolution with laplacian.
            d[x, y] += np.sum(d_sub*lap_kernel)

            # return d
            return(d)


from collections import Counter

def func(pattern, s):
    p = Counter(pattern)
    print(p)
    print(s)

    for wS in range(len(s)):
        print(s[wS])
        if(s[wS] in p):
            print("in pattern")
            wE = wS
            while(wE<len(s) and wE-wS+1<=len(pattern)): # replace with count of individal
                print(s[wE]+",", end=" ")
                if(s[wE] in p and p[s[wE]] > 0):
                    p[s[wE]] -=1
                    print(str(p))
                    wE+=1
                else:
                    p = Counter(pattern)
                    break
            if(wE-wS+1>len(pattern)):
                return True

    return False

# print(str(func("abc", "oidbcaf"))) #true
# print(str(func("odicf", "dc"))) #false
# print(str(func("bcdxabcdy", "bcdyabcdx"))) #true
print(str(func("hello", "ooolleoooleh"))) #false


def solve(A):
   hash_set=set()
   for ind in range(len(A)):
       string=str(A[ind][0])+"@"+str(A[ind][1])
       hash_set.add(string)

   return len(hash_set)

A = [[1, 1], [2, 2], [2, 2], [1, 1], [1, 1]]
print(solve(A))

# output:
# 2

# There are 2 unique points in the given array. They are [1, 1] and [2, 2].


from abc import ABC
import numpy as np

import matplotlib as mpl
import matplotlib.pyplot as plt

from scipy.constants import c
from scipy.optimize import curve_fit
from scipy.ndimage import gaussian_filter1d

import os
from glob import glob

from astropy.table import Table, join, vstack, hstack, unique

from desispec.io import read_spectra
from desispec.spectra import Spectra
from desispec.coaddition import coadd_cameras

import redrock.templates

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore", category=RuntimeWarning)
#warnings.filterwarnings("ignore", category=OptimizeWarning)
mpl.rc('font', size=14)

class SpectralLine(ABC):

    def __init__(self, name, wavelength):

        self.name = name
        self.line = wavelength

    def get_linewidth(self, wave, flux):
        pass
#Just a naive gaussian definition for later fitting
def gaus(x, sigma, line, A, b):
    flux = A*np.exp(-(x - line)**2/(2*sigma)**2) + b
    return flux

class SpectralLine_pEW(SpectralLine):

    def __init__(self, name, wavelength, cont_blue=None, cont_red=None):
        """Initialize a spectral line used for a pEW calculation.

        Parameters
        ----------
        line : str
            Name of line.
        wavelength : float
            restframe wavelength of line.
        cont_blue : list of two floats, or None
            Lower and upper wavelength for computing continuum on 'blue' end of line.
        cont_red : list of two floats, or None
            Lower and upper wavelength for computing continuum on 'red' end of line.
        """
        super().__init__(name, wavelength)

        if cont_blue is not None:
            self.cont_blue = cont_blue
        else:
            self.cont_blue = [self.line - 205., self.line - 195.]

        if cont_red is not None:
            self.cont_red = cont_red
        else:
            self.cont_red = [self.line + 195., self.line + 205.]

        self.vshift = None
        self.roi_wave = None
        self.roi_flux = None
        self.roi_flux_scaled = None
        self.roi_continuum = None

    def get_roi(self, wave, flux, dminwave=200., dmaxwave=200.):
        """Produce a region of interest for estimating the line pEW.

        Parameters
        ----------
        wave : ndarray
            Input wavelength from a spectrum.
        flux : ndarray
            Input flux from a spectrum.
        dminwave : float
            Distance below the line center (in Angstrom) for computing the ROI.
        dmaxwave : float
            Distance above the line center (in Angstrom) for computing the ROI.

        Returns
        -------
        vshift : ndarray
            Array of velocities in the ROI w.r.t. to the line rest frame.
        roi_wave : ndarray
            Selected wavelengths in the ROI.
        roi_flux : ndarray
            Selected flux values in the ROI.
        """
        if wave[-1] < self.line - dminwave or wave[1] > self.line + dmaxwave:
            raise ValueError('Analysis region not covered for {} line'.format(self.name))

        # Get velocity shift
        vshift = c * (wave / self.line - 1.)

        # Select the wavelengths in the region of interest around the line.
        select = (wave > self.line - dminwave) & (wave < self.line + dmaxwave)

        return vshift[select], wave[select], flux[select]

    def estimate_continuum(self, roi_wave, roi_flux):
        """Perform a linear fit of the continuum using 'blue' and 'red' sidebands of the spectra line.

        Parameters
        ----------
        roi_wave : ndarray
            Wavelengths in the region of interest of this spectral line.
        roi_flux : ndarray
            Fluxes in the region of interest of the spectral line.

        Returns
        -------
        a : float
            Intercept of the linear fit to the continuum.
        b : float
            Slope of the linear fit to the continuum.
        continuum : ndarray
            Computed continuum under the spectral line using a linear model.
        scaled_roi_flux :
            Flux in the ROI, scaled by the continuum to remove its slope.
        """
        blue_min, blue_max = self.cont_blue
        blue_avg = 0.5*(blue_min + blue_max)
        select_blue = (roi_wave > blue_min) & (roi_wave < blue_max)
        flux_blue = roi_flux[select_blue]

        red_min, red_max = self.cont_red
        red_avg = 0.5*(red_min + red_max)
        select_red = (roi_wave > red_min) & (roi_wave < red_max)
        flux_red = roi_flux[select_red]

        # Perform a linear fit to the mean flux vs mean wavelength in the blue and red sidebands of the line.
        # a = intercept, b = slope.
        b = (np.nanmean(flux_red) - np.nanmean(flux_blue)) / (red_avg - blue_avg)
        a = np.nanmean(flux_blue) - (b * blue_avg)

        # Calculate the continuum using the linear fit.
        continuum = a + b*roi_wave

        # Scale the flux by the linear continuum.
        scaled_roi_flux = roi_flux / continuum
        return a, b, continuum, scaled_roi_flux

    def estimate_peqw(self, roi_wave, scaled_roi_flux):
        """Compute the pseudo-equivalent width of the spectral line.

        Parameters
        ----------
        roi_wave : ndarray
            Wavelength in the fit region of interest of the line.
        scaled_roi_flux : ndarray
            Flux in the ROI scaled by the continuum to remove its slope.

        Returns
        -------
        """
        continuum = np.ones_like(scaled_roi_flux)
        _sum = np.sum((scaled_roi_flux - continuum) / continuum)
        _delta = np.diff(roi_wave)[0]
        return _sum * _delta

    def get_linewidth(self, wave, flux):
        """Return the line width (pseudo-equivalent width).

        Parameters
        ----------
        wave : ndarray
            Input wavelength from a spectrum.
        flux : ndarray
            Input flux from a spectrum.

        Returns
        -------
        peqw : float
            Pseudo-equivalent width of the spectral line.
        """
        vs, roi_wave, roi_flux = self.get_roi(wave, flux,self.line - np.nanmean(self.cont_blue),\
                                              np.nanmean(self.cont_red)-self.line)
        a, b, continuum, scaled_roi_flux = self.estimate_continuum(roi_wave, roi_flux)
        peqw = self.estimate_peqw(roi_wave, scaled_roi_flux)

        # Store the details of the continuum fit for debugging.
        self.vshift = vs
        self.roi_wave = roi_wave
        self.roi_flux = roi_flux
        self.roi_flux_scaled = scaled_roi_flux
        self.roi_continuum = continuum

        return peqw

    def __str__(self):
        s = '{}: {}\nBlue continuum fit: {} - {}\nRed continuum fit:  {} - {}'.format(
            self.name, self.line,
            self.cont_blue[0], self.cont_blue[1],
            self.cont_red[0], self.cont_red[1])
        return s

def is_TDE(Ha,HB,HeII,OIII,NIII, flux):
    '''
    Ha,HB, HeII, and OIII should be pEW for each of those lines respectively.
    '''
    TDE_score = 0
    blue_end = np.nanmean(flux[1000:3000])
    red_end = np.nanmean(flux[5000:7000])
    mean_flux = np.nanmean(flux)
    try:
        if OIII/Ha > 0.1:
            TDE_score -= 1.1 #if OIII is present, will be marked by an index with a noninteger part
    except ZeroDivisionError:
        pass
    if Ha > 4: #Just looks for generally wide Ha lines
        TDE_score += 1
    if HB > 4:
        TDE_score += 1
    try:
        if HeII/Ha >0.75: #looks for comparable He and Ha lines
            TDE_score += 1
    except ZeroDivisionError:
        pass
    if NIII > 4: #looks for NIII sometimes seen in TDE-bowen
        TDE_score += 1
    if blue_end/red_end >= 1.25:
        TDE_score += 1
    if mean_flux < 1:
        TDE_score -= 1.1

    return TDE_score

def Combine_multifilt(wave,flux, mask = False):
        difwave_single = []
        difflux_single = []
        difmask_single = []
        for band in wave:
            difwave_single += list(wave[band])
            difflux_single += list(flux[band])
            if mask:
                difmask_single += list(mask[band])

        if mask:
            diftable = Table([difwave_single, difflux_single,difmask_single], names = ('wave','flux', 'mask'))
        else:
            diftable = Table([difwave_single, difflux_single], names = ('wave','flux'))
        diftable.sort('wave')
        difwave_single = list(diftable['wave'])
        difflux_single = list(diftable['flux'])
        if mask:
            difmask_single = list(diftable['mask'])
            return difwave_single,difflux_single,difmask_single
        else:
            return difwave_single,difflux_single

def TDE_Check(wave,flux, redshift, scale = 3.,cont_width = 10,multifilt = False, spectype = 'GALAXY', mask = False):
    '''
    Creates a scaled flux (assumed to be roughly gaussian) for each line of interest. Then fits this scaled flux to a
    Gaussian, pulling the $\sigma$. Recalculates pEW using an ROI of the line $\pm$ scale*sigma. Appends this pEW to
    a Dictionary (lines), each line should have a list in format [wavelength, pEW]. pEW used in logic to determine whether
    or not it's a TDE.
    Parameters:
    wave, flux: wavelength and flux of spectrum
    scale: # of standard deviations to fit continuum
    cont_width: width of region to sample cont_blue and cont_red, default 5 grabs area 2.5 angstroms on either side of
        bound determined by scale*sigma
    '''
    if spectype == 'GALAXY':

        if multifilt:
            if mask:
                wave,flux, mask = Combine_multifilt(wave, flux,mask)
            else:
                wave,flux= Combine_multifilt(wave, flux)
        wave = wave/(1+redshift)
        flux = gaussian_filter1d(flux, sigma = 3)
        half_cont = cont_width/2
        lines = {'Halpha':[6562.79],'Hbeta':[4861.4], 'HeII4686':[4686],'OIII':[5007],'NIII':[4100]}
        for line in lines:
            pass1 = SpectralLine_pEW(line,lines[line][0])
            try:
                peqw_pass1 = pass1.get_linewidth(wave, flux)
                #plt.plot(pass1.roi_wave, pass1.roi_flux)
                #plt.show()
                #print(line)
                #print(peqw_pass1)

                try:
                    popt = curve_fit(gaus, pass1.roi_wave, pass1.roi_flux_scaled, p0 = [10.,pass1.line,max(pass1.roi_flux_scaled),1],check_finite = False)[0]
                    std = popt[0]
                    #print(std)
                    if scale*std <= half_cont: #Fit failed, line probably not present enough for measurement.
                        peqw_specline = 0
                        #print('Failed')

                    else:
                        cont_blue=[pass1.line - scale*std-half_cont, pass1.line - scale*std+half_cont]
                        cont_red=[pass1.line + scale*std-half_cont, pass1.line + scale*std+half_cont]
                        specline = SpectralLine_pEW(line, lines[line][0], cont_blue=cont_blue, \
                                    cont_red=cont_red)
                        if mask:
                            roistart_ind = wave.index(specline.roi_wave[0])
                            roiend_ind = wave.index(specline.roi_wave[-1]) + 1
                            if 1 in mask[roistart_ind:roiend_ind]:
                                peqw_specline = 0

                            else:
                                try:
                                    peqw_specline = specline.get_linewidth(wave, flux)
                                    #print(cont_blue,cont_red)
                                    #print(peqw_specline)
                                    #plt.plot(specline.roi_wave, specline.roi_flux)
                                    #plt.show()
                                except IndexError:
                                    peqw_specline = 0
                #print('###########')
                except RuntimeError:
                    peqw_specline = 0
            except:
                peqw_specline = 0
            lines[line].append(peqw_specline)
        return is_TDE(lines['Halpha'][-1],lines['Hbeta'][-1],lines['HeII4686'][-1],lines['OIII'][-1],\
                      lines['NIII'][-1], flux)
    else:
        return 0


from flask import Blueprint, flash, redirect, render_template, request, url_for
from flask_login import current_user, login_required
from flaskr.admins.forms import *
from flaskr.models import Notification
from sqlalchemy import desc

notifications = Blueprint("notifications", __name__)

@notifications.route("/notifications")
@login_required
def get_notifications():
    notifications = Notification.query.filter_by(
        profile_id=current_user.profile.id).order_by(desc(Notification.created_at))[:20]
    return render_template("notifications/notification.html", notifications=notifications)

@notifications.route("/notification/mark-read/<int:id>")
@login_required
def mark_read(id: int):
    notification = Notification.query.get(id)
    if notification.profile.id != current_user.profile.id:
        flash("Not authorized for this action.", "danger")
    else:
        notification.mark_read()
    return redirect(url_for("notifications.get_notifications"))

@notifications.route("/notification/mark-read-and-go/<int:id>")
@login_required
def mark_read_and_go(id: int):
    notification = Notification.query.get(id)
    # If the user is not the owner of that notification
    if notification.profile.id != current_user.profile.id:
        flash("Not authorized for this action.", "danger")
    elif not notification.is_readed:
        notification.mark_read()
    if notification.link == "":
        return redirect(url_for("notifications.get_notifications"))
    return redirect(notification.link)

@notifications.route("/notification/mark_all")
@login_required
def mark_all():
    notifications = current_user.profile.notifications
    for notification in notifications:
        if not notification.is_readed:
            notification.mark_read()
    return redirect(url_for("notifications.get_notifications"))


import os
import hashlib
import base64
import urllib
import string
import numbers
import threading
import errno
import fnmatch
import json

def makedirs_p(dirname, mode = None, reason = None):
    """
    Create a directory tree, ignoring an error if it already exists

    :param str pathname: directory tree to crate
    :param int mode: mode set the directory to
    """
    try:
        os.makedirs(dirname)
        # yes, this is a race condition--but setting the umask so
        # os.makedirs() gets the right mode would interfere with other
        # threads and processes.
        if mode:
            os.chmod(dirname, mode)
    except OSError as e:
        if not os.path.isdir(dirname):
            raise RuntimeError("%s: path for %s is not a directory: %s"
                               % (dirname, reason, e))
        if not os.access(dirname, os.W_OK):
            raise RuntimeError("%s: path for %s does not allow writes: %s"
                               % (dirname, reason, e))

def flat_keys_to_dict(d):
    """
    Given a dictionary of flat keys, convert it to a nested dictionary

    Similar to :func:`flat_slist_to_dict`, differing in the
    keys/values being in a dictionary.

    A key/value:

    >>> d["a.b.c"] = 34

    means:

    >>> d['a']['b']['c'] = 34

    Key in the input dictonary are processed in alphabetical order
    (thus, key a.a is processed before a.b.c); later keys override
    earlier keys:

    >>> d['a.a'] = 'aa'
    >>> d['a.a.a'] = 'aaa'
    >>> d['a.a.b'] = 'aab'

    will result in:

    >>> d['a']['a'] = { 'a': 'aaa', 'b': 'aab' }

    The

    >>> d['a.a'] = 'aa'

    gets overriden by the other settings

    :param dict d: dictionary of keys/values
    :returns dict: (nested) dictionary
    """
    tr = {}

    for key in sorted(d.keys()):
        _key_rep(tr, key, key, d[key])

    return tr

def rm_f(filename):
    """
    Remove a file (not a directory) unconditionally, ignore errors if
    it does not exist.
    """
    try:
        os.unlink(filename)
    except OSError as e:
        if e.errno != errno.ENOENT:
            raise

def mkid(something, l = 10):
    """
    Generate a 10 character base32 ID out of an iterable object

    :param something: anything from which an id has to be generate
      (anything iterable)
    """
    if isinstance(something, str):
        h = hashlib.sha512(something.encode('utf-8'))
    else:
        h = hashlib.sha512(something)
    return base64.b32encode(h.digest())[:l].lower().decode('utf-8', 'ignore')

class fsdb_c(object):
    """
    This is a very simple key/value flat database

    - sets are atomic and forcefully remove existing values
    - values are just strings
    - value are limited in size to 1K
    - if a field does not exist, its value is *None*

    The key space is flat, but with a convention of periods dividing
    fields, so that:

      l['a.b.c'] = 3

    is the equivalent to:

      l['a']['b']['c'] = 3

    it also makes it way faster and easier to filter for fields.

    This will be used to store data for each target; for implemntation
    examples, look at :class:`commonl.fsdb_symlink_c`.
    """
    class exception(Exception):
        pass

    def keys(self, pattern = None):
        """
        List the fields/keys available in the database

        :param str pattern: (optional) pattern against the key names
          must match, in the style of :mod:`fnmatch`. By default, all
          keys are listed.

        :returns list: list of keys
        """
        raise NotImplementedError

    def get_as_slist(self, *patterns):
        """
        Return a sorted list of tuples *(KEY, VALUE)*\s available in the
        database.

        :param list(str) patterns: (optional) list of patterns of fields
          we must list in the style of :mod:`fnmatch`. By default, all
          keys are listed.

        :returns list(str, str): list of *(KEY, VALUE)* sorted by
          *KEY* (so *a.b.c*, representing *['a']['b']['c']* goes
          after *a.b*, representing *['a']['b']*).
        """
        raise NotImplementedError

    def get_as_dict(self, *patterns):
        """
        Return a dictionary of *KEY/VALUE*\s available in the
        database.

        :param str pattern: (optional) pattern against the key names
          must match, in the style of :mod:`fnmatch`. By default, all
          keys are listed.

        :returns dict: keys and values in dictionary form
        """
        raise NotImplementedError

    def set(self, key, value, force = True):
        """
        Set a value for a key in the database unless *key* already exists

        :param str key: name of the key to set

        :param str value: value to store; *None* to remove the field;
          only *string*, *integer*, *float* and *boolean* types

        :parm bool force: (optional; default *True*) if *key* exists,
          force the new value

        :return bool: *True* if the new value was set correctly;
          *False* if *key* already exists and *force* is *False*.
        """
        assert isinstance(value, (NoneType, str, int, float, bool)), \
            f"value must be None, str, int, float, bool; got {type(value)}"

    def get(self, key, default = None):
        """
        Return the value stored for a given key

        :param str key: name of the key to retrieve

        :param str default: (optional) value to return if *key* is not
          set; defaults to *None*.

        :returns str: value associated to *key* if *key* exists;
          otherwise *default*.
        """
        raise NotImplementedError

    @staticmethod
    def create(cache_dir):
        """
        Create with the right type for the host OS

        Same params as :class:`fsdb_symlink_c`

        Note there are catchas for atomicity; read the docs for:

        - :class:`fsdb_symlink_c`
        - :class:`fsdb_file_c`
        """
        if sys.platform in ( 'linux', 'macos' ):
            return fsdb_symlink_c(cache_dir)
        else:
            return fsdb_file_c(cache_dir)

class fsdb_symlink_c(fsdb_c):
    """
    This implements a database by storing data on the destination
    argument of a Unix symbolic link

    Creating a symlink, takes only one atomic system call, which fails
    if the link already exists. Same to read it. Thus, for small
    values, it is very efficient.
    """
    class invalid_e(fsdb_c.exception):
        pass

    def __init__(self, dirname, use_uuid = None, concept = "directory"):
        """
        Initialize the database to be saved in the give location
        directory

        :param str location: Directory where the database will be kept
        """
        if not os.path.isdir(dirname):
            raise self.invalid_e("%s: invalid %s"
                                 % (os.path.basename(dirname), concept))
        if not os.access(dirname, os.R_OK | os.W_OK | os.X_OK):
            raise self.invalid_e("%s: cannot access %s"
                                 % (os.path.basename(dirname), concept))

        if use_uuid == None:
            self.uuid = mkid(str(id(self)) + str(os.getpid()))
        else:
            self.uuid = use_uuid

        self.location = dirname

    def _raw_valid(self, location):
        return os.path.islink(location)

    def _raw_read(self, location):
        return os.readlink(location)

    def _raw_write(self, location, value):
        os.symlink(value, location)

    def _raw_unlink(self, location):
        os.unlink(location)

    def _raw_rename(self, location_new, location):
        os.replace(location_new, location)

    @staticmethod
    def _raw_stat(location):
        return os.lstat(location)

    def keys(self, pattern = None):
        l = []
        for _rootname, _dirnames, filenames_raw in os.walk(self.location):
            filenames = []
            for filename_raw in filenames_raw:
                # need to filter with the unquoted name...
                filename = urllib.parse.unquote(filename_raw)
                if pattern == None or fnmatch.fnmatch(filename, pattern):
                    if self._raw_valid(os.path.join(self.location, filename_raw)):
                        l.append(filename)
        return l

    def get_as_slist(self, *patterns):
        fl = []
        for _rootname, _dirnames, filenames_raw in os.walk(self.location):
            filenames = {}
            for filename in filenames_raw:
                filenames[urllib.parse.unquote(filename)] = filename
            if patterns:	# that means no args given
                use = {}
                for filename, filename_raw in filenames.items():
                    if field_needed(filename, patterns):
                        use[filename] = filename_raw
            else:
                use = filenames
            for filename, filename_raw in use.items():
                if self._raw_valid(os.path.join(self.location, filename_raw)):
                    bisect.insort(fl, ( filename, self._get_raw(filename_raw) ))
        return fl

    def get_as_dict(self, *patterns):
        d = {}
        for _rootname, _dirnames, filenames_raw in os.walk(self.location):
            filenames = {}
            for filename in filenames_raw:
                filenames[urllib.parse.unquote(filename)] = filename
            if patterns:	# that means no args given
                use = {}
                for filename, filename_raw in filenames.items():
                    if field_needed(filename, patterns):
                        use[filename] = filename_raw
            else:
                use = filenames
            for filename, filename_raw in use.items():
                if self._raw_valid(os.path.join(self.location, filename_raw)):
                    d[filename] = self._get_raw(filename_raw)
        return d

    def set(self, key, value, force = True):
        # escape out slashes and other unsavory characters in a non
        # destructive way that won't work as a filename
        key_orig = key
        key = urllib.parse.quote(
            key, safe = '-_ ' + string.ascii_letters + string.digits)
        location = os.path.join(self.location, key)
        if value != None:
            # the storage is always a string, so encode what is not as
            # string as T:REPR, where T is type (b boolean, n number,
            # s string) and REPR is the textual repr, json valid
            if isinstance(value, bool):
                # do first, otherwise it will test as int
                value = "b:" + str(value)
            elif isinstance(value, numbers.Integral):
                # sadly, this looses precission in floats. A lot
                value = "i:%d" % value
            elif isinstance(value, numbers.Real):
                # sadly, this can loose precission in floats--FIXME:
                # better solution needed
                value = "f:%.10f" % value
            elif isinstance(value, str):
                if value.startswith("i:") \
                   or value.startswith("f:") \
                   or value.startswith("b:") \
                   or value.startswith("s:") \
                   or value == "":
                    value = "s:" + value
            else:
                raise ValueError("can't store value of type %s" % type(value))
            assert len(value) < 4096
        if value == None:
            # note that we are setting None (aka: removing the value)
            # we also need to remove any "subfield" -- KEY.a, KEY.b
            try:
                self._raw_unlink(location)
            except OSError as e:
                if e.errno != errno.ENOENT:
                    raise
            # FIXME: this can be optimized a lot, now it is redoing a
            # lot of work
            for key_itr in self.keys(key_orig + ".*"):
                key_itr_raw = urllib.parse.quote(
                    key_itr, safe = '-_ ' + string.ascii_letters + string.digits)
                location = os.path.join(self.location, key_itr_raw)
                try:
                    self._raw_unlink(location)
                except OSError as e:
                    if e.errno != errno.ENOENT:
                        raise
            return True	# already wiped by someone else
        if force == False:
            try:
                self._raw_write(location, value)
            except OSError as e:
                if e.errno != errno.EEXIST:
                    raise
                # ignore if it already exists
                return False
            return True

        # New location, add a unique thing to it so there is no
        # collision if more than one process is trying to modify
        # at the same time; they can override each other, that's
        # ok--the last one wins.
        location_new = location + "-" + str(os.getpid()) + "-" + str(threading.get_ident())
        rm_f(location_new)
        self._raw_write(location_new, value)
        self._raw_rename(location_new, location)
        return True

    def _get_raw(self, key, default = None):
        location = os.path.join(self.location, key)
        try:
            value = self._raw_read(location)
            # if the value was type encoded (see set()), decode it;
            # otherwise, it is a string
            if value.startswith("i:"):
                return json.loads(value.split(":", 1)[1])
            if value.startswith("f:"):
                return json.loads(value.split(":", 1)[1])
            if value.startswith("b:"):
                val = value.split(":", 1)[1]
                if val == "True":
                    return True
                elif val == "False":
                    return False
                raise ValueError("fsdb %s: key %s bad boolean '%s'"
                                 % (self.location, key, value))
            if value.startswith("s:"):
                # string that might start with s: or empty
                return value.split(":", 1)[1]
            return value	# other string
        except OSError as e:
            if e.errno == errno.ENOENT:
                return default
            raise

    def get(self, key, default = None):
        # escape out slashes and other unsavory characters in a non
        # destructive way that won't work as a filename
        key = urllib.parse.quote(
            key, safe = '-_ ' + string.ascii_letters + string.digits)
        return self._get_raw(key, default = default)

def field_needed(field, projections):
    """
    Check if the name *field* matches any of the *patterns* (ala
    :mod:`fnmatch`).

    :param str field: field name
    :param list(str) projections: list of :mod:`fnmatch` patterns
      against which to check field. Can be *None* and *[ ]* (empty).

    :returns bool: *True* if *field* matches a pattern in *patterns*
      or if *patterns* is empty or *None*. *False* otherwise.
    """
    if projections:
        # there is a list of must haves, check here first
        for projection in projections:
            if fnmatch.fnmatch(field, projection):
                return True	# we need this field
            # match projection a to fields a.[x.[y.[...]]]
            if field.startswith(projection + "."):
                return True
        return False		# we do not need this field
    else:
        return True	# no list, have it


"""
Distributed under the terms of the BSD 3-Clause License.

The full license is in the file LICENSE, distributed with this software.

Author: Jun Zhu
"""
from abc import abstractmethod
from enum import Enum

from ..backend.QtCore import pyqtSignal, QPointF, QRectF, Qt
from ..backend.QtGui import QBrush, QPen

from ..aesthetics import FColor
from ..graphics_scene import MouseDragEvent
from .graphics_item import GraphicsObject

class LinearRegionItem(GraphicsObject):
    """A horizontal or vertical region inbetween two lines."""
    region_changed_sgn = pyqtSignal()
    region_dragged_sgn = pyqtSignal()

    class Moving(Enum):
        NONE = 0
        BODY = 1
        TOP = 2
        BOTTOM = 3

    def __init__(self, p1: float, p2: float, *, parent=None):
        """Initialization."""
        super().__init__(parent=parent)

        self._p1 = p1
        self._p2 = p2
        self._bounding_rect = None

        self._draggable = True
        self._edge_fraction = 0.3
        self._moving = self.Moving.NONE
        self._cursor_offset = 0

        self._pen = FColor.mkPen(None)
        self._brush = None
        self.setBrush(FColor.mkBrush("b", alpha=80))
        self._hover_brush = None
        self.setHoverBrush(FColor.mkBrush("b", alpha=40))

    def setBrush(self, brush: QBrush) -> None:
        """Set the QBrush used to fill the region."""
        self._brush = brush
        self.update()

    def setHoverBrush(self, brush: QBrush) -> None:
        """Set the QBrush used to fill the region when mouse is hovering."""
        self._hover_brush = brush
        self.update()

    def setDraggable(self, state: bool) -> None:
        self._draggable = state

    def region(self) -> tuple[float, float]:
        return self._p1, self._p2

    def setRegion(self, p1: float, p2: float) -> None:
        self._p1 = p1
        self._p2 = p2
        self._updateRegion()

    @abstractmethod
    def _pos(self, p: QPointF) -> float:
        ...

    def paint(self, p, *args) -> None:
        """Override."""
        p.setPen(self._pen)
        if self._moving == self.Moving.NONE:
            p.setBrush(self._brush)
        else:
            p.setBrush(self._hover_brush)
        rect = self.boundingRect()
        p.drawRect(rect)

    def _updateRegion(self) -> None:
        self._bounding_rect = None
        self.region_changed_sgn.emit()
        self.prepareGeometryChange()

    def _updateMovingState(self, p: float):
        delta = self._p2 - self._p1
        if p < self._p1 + self._edge_fraction * delta:
            self._moving = self.Moving.BOTTOM
        elif p > self._p1 + (1 - self._edge_fraction) * delta:
            self._moving = self.Moving.TOP
        else:
            self._moving = self.Moving.BODY

    def mouseDragEvent(self, ev: MouseDragEvent) -> None:
        if not self._draggable or ev.button() != Qt.MouseButton.LeftButton:
            return

        if ev.entering():
            p = self._pos(ev.buttonDownPos())
            self._updateMovingState(p)
            self._cursor_offset = [self._p1 - p, self._p2 - p]
            self.region_dragged_sgn.emit()

        if self._moving == self.Moving.NONE:
            return

        if self._moving != self._moving.TOP:
            self._p1 = self._cursor_offset[0] + self._pos(ev.pos())
        if self._moving != self._moving.BOTTOM:
            self._p2 = self._cursor_offset[1] + self._pos(ev.pos())

        if self._p1 > self._p2:
            self._p1, self._p2 = self._p2, self._p1
            self._cursor_offset.reverse()
            if self._moving == self._moving.TOP:
                self._moving = self._moving.BOTTOM
            else:
                self._moving = self._moving.TOP

        self._updateRegion()

        if ev.exiting():
            self._moving = self.Moving.NONE

        ev.accept()

class LinearHRegionItem(LinearRegionItem):

    def _pos(self, p):
        """Override."""
        return p.x()

    def boundingRect(self) -> QRectF:
        """Override."""
        parent = self.parentItem()
        if parent is None:
            return QRectF()

        if self._bounding_rect is None:
            rect = parent.boundingRect()
            self._bounding_rect = QRectF(
                self._p1, rect.top(), self._p2 - self._p1, rect.height)
            self.prepareGeometryChange()
        return self._bounding_rect

class LinearVRegionItem(LinearRegionItem):

    def _pos(self, p):
        """Override."""
        return p.y()

    def boundingRect(self) -> QRectF:
        """Override."""
        parent = self.parentItem()
        if parent is None:
            return QRectF()

        if self._bounding_rect is None:
            rect = parent.boundingRect()
            self._bounding_rect = QRectF(
                rect.left(), self._p1, rect.width(), self._p2 - self._p1)
            self.prepareGeometryChange()
        return self._bounding_rect


import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Dropout,LSTM, BatchNormalization
from tensorflow.keras.callbacks import TensorBoard, ModelCheckpoint
from sklearn.preprocessing import StandardScaler
from PlotSplit import *
import math
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt

def Train_model(x_train,y_train,x_test,y_test,name,Data):
	model = Sequential()
	model.add(LSTM(50,return_sequences = True,input_shape = (x_train.shape[1],1)))
	model.add(Dropout(0.2))

	model.add(LSTM(50,return_sequences = True))
	model.add(Dropout(0.2))

	model.add(LSTM(50,return_sequences = True))
	model.add(Dropout(0.2))

	model.add(LSTM(50,return_sequences = False))
	model.add(Dropout(0.2))

	model.add(Dense(1))

	model.compile(optimizer = 'adam',loss = 'mean_squared_error')
	model.fit(x_train,y_train, batch_size = 64, epochs = 100 ,validation_data = (x_test,y_test))
	model.summary()

	train_prediction = model.predict(x_train)
	test_prediction = model.predict(x_test)
	# print(train_prediction)
	# print(test_prediction)
	scaler = MinMaxScaler(feature_range  = (0,1))
	external = scaler.fit(np.array(Data).reshape(-1,1))
	# external2 = scaler.fit(test_prediction)

	train_prediction = external.inverse_transform(train_prediction)
	test_prediction = external.inverse_transform(test_prediction)

	print("RMSE train_prediction: ",math.sqrt(mean_squared_error(y_train,train_prediction)))
	print("RMSE test_prediction: ",math.sqrt(mean_squared_error(y_test,test_prediction)))

	model.save(f'D:\\Github\\stockpredict\\Stock-forcast\\Saved_model\\{name}', save_format='h5')
	plot_data(Data,train_prediction,test_prediction)


class LinkedList:
    def __init__(self, data):
        self.data = data
        self.nextNodeaddr = None

node1 = LinkedList(10)

node2 = LinkedList(20)

node3 = LinkedList(30)

start = node1
node1.nextNodeaddr = node2
node2.nextNodeaddr = node3

print(node1, node2, node3)
print("\n")
print(start, node1.__dict__, node2.__dict__, node3.__dict__ )

print(start.nextNodeaddr.nextNodeaddr)

while (start is not None):
    print(start.data, end=",")
    start = start.nextNodeaddr
    print(start)


import re

import pytest

from onetl.file import FileDFWriter

@pytest.mark.parametrize(
    "option, value",
    [
        ("if_exists", "error"),
        ("partition_by", "month"),
        ("partition_by", ["year", "month"]),
        ("unknown", "abc"),
    ],
)
def test_file_df_writer_options(option, value):
    options = FileDFWriter.Options.parse({option: value})
    assert getattr(options, option) == value

def test_file_df_writer_options_mode_prohibited():
    msg = re.escape("Parameter `mode` is not allowed. Please use `if_exists` parameter instead.")
    with pytest.raises(ValueError, match=msg):
        FileDFWriter.Options(mode="error")

@pytest.mark.parametrize(
    "mode, recommended",
    [
        ("dynamic", "replace_overlapping_partitions"),
        ("static", "replace_entire_directory"),
    ],
)
def test_file_df_writer_options_unsupported_partition_overwrite(mode, recommended):
    error_msg = f"`partitionOverwriteMode` option should be replaced with if_exists='{recommended}'"

    with pytest.raises(ValueError, match=error_msg):
        FileDFWriter.Options(partitionOverwriteMode=mode)

    with pytest.raises(ValueError, match=error_msg):
        FileDFWriter.Options(partition_overwrite_mode=mode)