from __future__ import (nested_scopes, generators, division, absolute_import, with_statement,
                        print_function, unicode_literals)

from collections import defaultdict
from contextlib import contextmanager
import inspect
import logging
import os
import re
import sys
import traceback

from twitter.common import log
from twitter.common.collections import OrderedSet
from twitter.common.lang import Compatibility
from twitter.common.log.options import LogOptions

from pants.backend.core.tasks.task import QuietTaskMixin, Task
from pants.backend.jvm.tasks.nailgun_task import NailgunTask  # XXX(pl)
from pants.base.build_environment import get_buildroot
from pants.base.build_file import BuildFile
from pants.base.cmd_line_spec_parser import CmdLineSpecParser
from pants.base.config import Config
from pants.base.rcfile import RcFile
from pants.base.workunit import WorkUnit
from pants.commands.command import Command
from pants.engine.engine import Engine
from pants.engine.round_engine import RoundEngine
from pants.goal.context import Context
from pants.goal.error import GoalError
from pants.goal.initialize_reporting import update_reporting
from pants.goal.goal import Goal
from pants.option.bootstrap_options import create_bootstrapped_options
from pants.option.global_options import register_global_options
from pants.util.dirutil import safe_mkdir

StringIO = Compatibility.StringIO

class GoalRunner(Command):
  """Lists installed goals or else executes a named goal."""

  class IntermixedArgumentsError(GoalError):
    pass

  __command__ = 'goal'
  output = None

  def __init__(self, *args, **kwargs):
    self.targets = []
    known_scopes = ['']
    for goal in Goal.all():
      # Note that enclosing scopes will appear before scopes they enclose.
      known_scopes.extend(filter(None, goal.known_scopes()))

    self.new_options = create_bootstrapped_options(known_scopes=known_scopes)
    self.config = Config.from_cache()  # Get the bootstrapped version.
    super(GoalRunner, self).__init__(*args, needs_old_options=False, **kwargs)

  def get_spec_excludes(self):
    # Note: Only call after register_options() has been called.
    return [os.path.join(self.root_dir, spec_exclude)
            for spec_exclude in self.new_options.for_global_scope().spec_excludes]

  @property
  def global_options(self):
    return self.new_options.for_global_scope()

  @contextmanager
  def check_errors(self, banner):
    errors = {}
    def error(key, include_traceback=False):
      exc_type, exc_value, _ = sys.exc_info()
      msg = StringIO()
      if include_traceback:
        frame = inspect.trace()[-2]
        filename = frame[1]
        lineno = frame[2]
        funcname = frame[3]
        code = ''.join(frame[4]) if frame[4] else None
        traceback.print_list([(filename, lineno, funcname, code)], file=msg)
      if exc_type:
        msg.write(''.join(traceback.format_exception_only(exc_type, exc_value)))
      errors[key] = msg.getvalue()
      sys.exc_clear()

    yield error

    if errors:
      msg = StringIO()
      msg.write(banner)
      invalid_keys = [key for key, exc in errors.items() if not exc]
      if invalid_keys:
        msg.write('\n  %s' % '\n  '.join(invalid_keys))
      for key, exc in errors.items():
        if exc:
          msg.write('\n  %s =>\n    %s' % (key, '\n      '.join(exc.splitlines())))
      # The help message for goal is extremely verbose, and will obscure the
      # actual error message, so we don't show it in this case.
      self.error(msg.getvalue(), show_help=False)

  def register_options(self):
    # Add a 'bootstrap' attribute to the register function, so that register_global can
    # access the bootstrap option values.
    def register_global(*args, **kwargs):
      return self.new_options.register_global(*args, **kwargs)
    register_global.bootstrap = self.new_options.bootstrap_option_values()
    register_global_options(register_global)
    for goal in Goal.all():
      goal.register_options(self.new_options)

  def setup_parser(self, parser, args):
    if not args:
      args.append('help')

    logger = logging.getLogger(__name__)

    goals = self.new_options.goals
    specs = self.new_options.target_specs
    fail_fast = self.new_options.for_global_scope().fail_fast

    for goal in goals:
      if BuildFile.from_cache(get_buildroot(), goal, must_exist=False).exists():
        logger.warning(" Command-line argument '{0}' is ambiguous and was assumed to be "
                       "a goal. If this is incorrect, disambiguate it with ./{0}.".format(goal))

    if self.new_options.is_help:
      self.new_options.print_help(goals=goals)
      sys.exit(0)

    self.requested_goals = goals

    with self.run_tracker.new_workunit(name='setup', labels=[WorkUnit.SETUP]):
      spec_parser = CmdLineSpecParser(self.root_dir, self.address_mapper,
                                      spec_excludes=self.get_spec_excludes())
      with self.run_tracker.new_workunit(name='parse', labels=[WorkUnit.SETUP]):
        for spec in specs:
          for address in spec_parser.parse_addresses(spec, fail_fast):
            self.build_graph.inject_address_closure(address)
            self.targets.append(self.build_graph.get_target(address))
    self.goals = [Goal.by_name(goal) for goal in goals]

    rcfiles = self.config.getdefault('rcfiles', type=list,
                                     default=['/etc/pantsrc', '~/.pants.rc'])
    if rcfiles:
      rcfile = RcFile(rcfiles, default_prepend=False, process_default=True)

      # Break down the goals specified on the command line to the full set that will be run so we
      # can apply default flags to inner goal nodes.  Also break down goals by Task subclass and
      # register the task class hierarchy fully qualified names so we can apply defaults to
      # baseclasses.

      sections = OrderedSet()
      for goal in Engine.execution_order(self.goals):
        for task_name in goal.ordered_task_names():
          sections.add(task_name)
          task_type = goal.task_type_by_name(task_name)
          for clazz in task_type.mro():
            if clazz == Task:
              break
            sections.add('%s.%s' % (clazz.__module__, clazz.__name__))

      augmented_args = rcfile.apply_defaults(sections, args)
      if augmented_args != args:
        # TODO(John Sirois): Cleanup this currently important mutation of the passed in args
        # once the 2-layer of command -> goal is squashed into one.
        args[:] = augmented_args
        sys.stderr.write("(using pantsrc expansion: pants goal %s)\n" % ' '.join(augmented_args))

  def run(self):
    # TODO(John Sirois): Consider moving to straight python logging.  The divide between the
    # context/work-unit logging and standard python logging doesn't buy us anything.

    # Enable standard python logging for code with no handle to a context/work-unit.
    if self.global_options.level:
      LogOptions.set_stderr_log_level((self.global_options.level or 'info').upper())
      logdir = self.global_options.logdir or self.config.get('goals', 'logdir', default=None)
      if logdir:
        safe_mkdir(logdir)
        LogOptions.set_log_dir(logdir)

        prev_log_level = None
        # If quiet, temporarily change stderr log level to kill init's output.
        if self.global_options.quiet:
          prev_log_level = LogOptions.loglevel_name(LogOptions.stderr_log_level())
          # loglevel_name can fail, so only change level if we were able to get the current one.
          if prev_log_level is not None:
            LogOptions.set_stderr_log_level(LogOptions._LOG_LEVEL_NONE_KEY)

        log.init('goals')

        if prev_log_level is not None:
          LogOptions.set_stderr_log_level(prev_log_level)
      else:
        log.init()

    # Update the reporting settings, now that we have flags etc.
    def is_quiet_task():
      for goal in self.goals:
        if goal.has_task_of_type(QuietTaskMixin):
          return True
      return False

    # Target specs are mapped to the patterns which match them, if any. This variable is a key for
    # specs which don't match any exclusion regexes. We know it won't already be in the list of
    # patterns, because the asterisks in its name make it an invalid regex.
    _UNMATCHED_KEY = '** unmatched **'

    def targets_by_pattern(targets, patterns):
      mapping = defaultdict(list)
      for target in targets:
        matched_pattern = None
        for pattern in patterns:
          if re.search(pattern, target.address.spec) is not None:
            matched_pattern = pattern
            break
        if matched_pattern is None:
          mapping[_UNMATCHED_KEY].append(target)
        else:
          mapping[matched_pattern].append(target)
      return mapping

    is_explain = self.global_options.explain
    update_reporting(self.global_options, is_quiet_task() or is_explain, self.run_tracker)

    if self.global_options.exclude_target_regexp:
      excludes = self.global_options.exclude_target_regexp
      log.debug('excludes:\n  {excludes}'.format(excludes='\n  '.join(excludes)))
      by_pattern = targets_by_pattern(self.targets, excludes)
      self.targets = by_pattern[_UNMATCHED_KEY]
      # The rest of this if-statement is just for debug logging.
      log.debug('Targets after excludes: {targets}'.format(
          targets=', '.join(t.address.spec for t in self.targets)))
      excluded_count = sum(len(by_pattern[p]) for p in excludes)
      log.debug('Excluded {count} target{plural}.'.format(count=excluded_count,
          plural=('s' if excluded_count != 1 else '')))
      for pattern in excludes:
        log.debug('Targets excluded by pattern {pattern}\n  {targets}'.format(pattern=pattern,
            targets='\n  '.join(t.address.spec for t in by_pattern[pattern])))

    context = Context(
      config=self.config,
      new_options=self.new_options,
      run_tracker=self.run_tracker,
      target_roots=self.targets,
      requested_goals=self.requested_goals,
      build_graph=self.build_graph,
      build_file_parser=self.build_file_parser,
      address_mapper=self.address_mapper,
      spec_excludes=self.get_spec_excludes()
    )

    unknown = []
    for goal in self.goals:
      if not goal.ordered_task_names():
        unknown.append(goal)

    if unknown:
      context.log.error('Unknown goal(s): %s\n' % ' '.join(goal.name for goal in unknown))
      return 1

    engine = RoundEngine()
    return engine.execute(context, self.goals)

  def cleanup(self):
    # TODO: This is JVM-specific and really doesn't belong here.
    # TODO: Make this more selective? Only kill nailguns that affect state? E.g., checkstyle
    # may not need to be killed.
    NailgunTask.killall(log.info)
    sys.exit(1)


from peewee import *
from playhouse.fields import ManyToManyField

from backend.connection_manager import db
from backend.models.feed import Feed
from backend.models.route import Route

class Stop(Model):
    id = PrimaryKeyField()
    stop_id = BigIntegerField(null=False)
    name = CharField(null=False)
    lat = FloatField(null=False)
    lng = FloatField(null=False)
    timezone = CharField(max_length=100, null=True)
    feed = ForeignKeyField(Feed, null=False, related_name='stops')
    routes = ManyToManyField(Route, related_name='stops')

    def __str__(self) -> str:
        return '{s.id} - {s.stop_id} ({s.name})'.format(s=self)

    @classmethod
    def get_nearby(cls, lat, lng, distance):
        cursor = db.execute_sql('''
                        SELECT
                            stop.id AS id,
                            %(distance_unit)s * DEGREES(ACOS(COS(RADIANS(%(lat)s))
                                             * COS(RADIANS(stop.lat))
                                             * COS(RADIANS(%(lng)s - stop.lng))
                                             + SIN(RADIANS(%(lat)s))
                                             * SIN(RADIANS(stop.lat)))) AS distance
                        FROM stop

                        WHERE stop.lat
                              BETWEEN %(lat)s - (%(radius)s / %(distance_unit)s)
                              AND %(lat)s + (%(radius)s / %(distance_unit)s)
                              AND stop.lng
                              BETWEEN %(lng)s - (%(radius)s / (%(distance_unit)s * COS(RADIANS(%(lat)s))))
                              AND %(lng)s + (%(radius)s / (%(distance_unit)s * COS(RADIANS(%(lat)s))))
                    ''', {
            "lat": lat,
            "lng": lng,
            "radius": distance / 1000,
            "distance_unit": 111.045,
        })

        result = []

        for nearby in cursor.fetchall():
            result.append(cls.get(id=nearby[0]))

        return result

    class Meta:
        database = db


from datetime import datetime
from typing import List, Union

from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

from app.models import CharityProject, Donation

async def get_not_closed_investing_objects(
        model: Union[CharityProject, Donation],
        session: AsyncSession
) -> List[Union[CharityProject, Donation]]:
    db_obj = await session.execute(
        select(model).where(
            model.fully_invested == 0
        ).order_by(model.create_date)
    )
    return db_obj.scalars().all()

def close_investing_object(
        obj_to_close: Union[CharityProject, Donation]
):
    obj_to_close.invested_amount = obj_to_close.full_amount
    obj_to_close.fully_invested = True
    obj_to_close.close_date = datetime.now()

def make_investing(
        new_obj: Union[CharityProject, Donation],
        model_obj: Union[CharityProject, Donation]
) -> (Union[CharityProject, Donation], Union[CharityProject, Donation]):
    new_obj_free_amount = new_obj.full_amount - new_obj.invested_amount
    model_obj_free_amount = model_obj.full_amount - model_obj.invested_amount
    if new_obj_free_amount == model_obj_free_amount:
        close_investing_object(new_obj)
        close_investing_object(model_obj)
    elif new_obj_free_amount > model_obj_free_amount:
        new_obj.invested_amount += model_obj_free_amount
        close_investing_object(model_obj)
    else:
        model_obj.invested_amount += new_obj_free_amount
        close_investing_object(new_obj)
    return new_obj, model_obj

async def investing_process(
        new_object: Union[CharityProject, Donation],
        model: Union[CharityProject, Donation],
        session: AsyncSession
):
    model_objects = await get_not_closed_investing_objects(model, session)
    for model_object in model_objects:
        new_obj, model_obj = make_investing(new_object, model_object)
        session.add(new_obj)
        session.add(model_obj)
    await session.commit()
    await session.refresh(new_object)


from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, InputLayer
import pandas as pd
import numpy as np
from pickle import load
from sklearn.preprocessing import MinMaxScaler

model = Sequential([
  InputLayer(input_shape=7),
  Dense(5, activation = 'relu'),
  Dense(4, activation = 'softmax')
  ])
model.load_weights("music_weights.hdf5")
model.compile(optimizer="Adam",loss='sparse_categorical_crossentropy',metrics='accuracy')

scaler = load(open('scaler.pkl', 'rb'))
emotions = ["Calm", "Energetic", "Happy", "Sad"]

def recommend_music(hist, mood):
    x = hist[["Danceability", "Acousticness", "Energy", "Instrumentalness", "Valence", "Loudness", "Speechiness"]]
    x = scaler.transform(x)
    predictions = model.predict(x)
    index_moods = np.argmax(predictions, axis =1)
    moods = []
    for index in index_moods:
        moods.append(emotions[index])
    hist["moods"] = moods
    return hist[hist["moods"] == mood].reset_index(drop =True)


#!/usr/bin/env ccp4-python
'''
Created on 16 Jan 2016

@author: hlfsimko
'''

import glob
import os
import sys

from ample.constants import SHARE_DIR
from ample.testing import test_funcs
from ample.testing.integration_util import AMPLEBaseTest

INPUT_DIR = os.path.join(SHARE_DIR, "examples", "single-model", "input")
TEST_DICT = {}

# vanilla test
args_vanilla = [
    [ '-fasta', os.path.join(INPUT_DIR, '1ujb.fasta') ],
    [ '-mtz', os.path.join(INPUT_DIR, '1ujb-sf.mtz') ],
]

###############################################################################
#
# test ensemble creation from a single structure based on residue scores
#
###############################################################################
args_from_single_model = args_vanilla + [
    [ '-percent', '20' ],
    [ '-single_model', os.path.join(INPUT_DIR, '3c7t.pdb') ],
    [ '-subcluster_radius_thresholds', 1,2,3 ],
    [ '-side_chain_treatments', 'polyAla', 'reliable', 'allatom' ],
    [ '-truncation_scorefile', os.path.join(INPUT_DIR, '3c7t_scores.csv') ],
    [ '-truncation_scorefile_header', 'residue', 'Concoord' ],
]

# Test class that holds the functions to test the RESULTS_PKL file that will be passed in
class AMPLETest(AMPLEBaseTest):
    def test_from_single_model(self):
        self.assertTrue(self.AMPLE_DICT['AMPLE_finished'])
        nensembles = len(self.AMPLE_DICT['ensembles'])
        self.assertEqual(nensembles, 15, "Incorrect number of ensembles produced: {0}".format(nensembles))
        self.assertIn('ensembles', self.AMPLE_DICT)
        self.assertGreater(len(self.AMPLE_DICT['ensembles']), 0, "No ensembles produced")
        self.assertIn('mrbump_results', self.AMPLE_DICT)
        self.assertGreater(len(self.AMPLE_DICT['mrbump_results']), 0, "No MRBUMP results")
        self.assertTrue(self.AMPLE_DICT['success'])
        self.assertGreater(self.AMPLE_DICT['mrbump_results'][0]['SHELXE_CC'], 25,"SHELXE_CC criteria not met")
        return

TEST_DICT['from_single_model'] = { 'args' : args_from_single_model,
                                   'test' :  AMPLETest,
                                 }

###############################################################################
#
# End Test Setup
#
###############################################################################

if __name__ == '__main__':
    test_funcs.parse_args(TEST_DICT)


class Solution:
    def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
        for i, num in enumerate(flowerbed):
            isCurrentPlaceClear = flowerbed[i] == 0
            isPreviousPlaceClear = i == 0 or flowerbed[i - 1] == 0
            isNextPlaceClear = i == len(flowerbed) - 1 or flowerbed[i + 1] == 0
            if isCurrentPlaceClear and isPreviousPlaceClear and isNextPlaceClear:
                flowerbed[i] = 1
                n -= 1
        return n <= 0


__all__ = ['nuke_all', 'is_empty']

def nuke_all(classes):
    'Destroys all objects'
    for cls in classes:
        for obj in cls.objects.all():
            obj.delete()

def is_empty(classes):
    '''returns False if any instances of the given classes exist, else returns True'''
    for cls in classes:
        if 0 != len(cls.objects.all()):
            return False
    return True


#Django Libs
from django.http.response import FileResponse, HttpResponse
from django.shortcuts import render
from django.urls import reverse
from django.views.generic import View, CreateView, DeleteView, UpdateView, DetailView, ListView, TemplateView
from django.db.models import Sum
from django.core.serializers import serialize

#Self Libs
from .forms import ComprasForm, ConsumidorFinalForm, ContribuyenteForm, EmpresaF, LibroForm
from .models import *
from empresas.models import Empresa as Cliente
from .export import *

#Factura CF
class FacturaCFCV(CreateView):
    model = FacturaCF
    template_name = "iva/lfcf.html"
    form_class = ConsumidorFinalForm
    def get_context_data(self, **kwargs):
        facturas = Libro.objects.get(id=self.kwargs["libro"]).facturacf
        context = super(FacturaCFCV,self).get_context_data(**kwargs)
        context["libro"] = Libro.objects.get(id=self.kwargs["libro"])
        context['direccion'] = 'cont:nueva_fcf'
        context['titulo'] = 'Crear Factura Consumidor Final'
        context["parametro"] = self.kwargs['libro']
        context["totales"] = [
            facturas.all().aggregate(total_exento=Sum('exento'))["total_exento"],
            facturas.all().aggregate(total_local=Sum('locales'))["total_local"],
            facturas.all().aggregate(total_exportacion=Sum('exportaciones'))["total_exportacion"],
            facturas.all().aggregate(total_ventasNSujetas=Sum('ventasNSujetas'))["total_ventasNSujetas"],
            facturas.all().aggregate(total_venta=Sum('ventaTotal'))["total_venta"],
            facturas.all().aggregate(total_ventaCtaTerceros=Sum('ventaCtaTerceros'))["total_ventaCtaTerceros"],
        ]
        return context

    def get_initial(self, **kwargs):
        initial = super(FacturaCFCV,self).get_initial()
        initial["libro"] = Libro.objects.get(id=self.kwargs["libro"]).id
        return initial

    def get_success_url(self,**kwargs):
        libro=Libro.objects.get(id=self.kwargs["libro"])
        return reverse("iva:nueva_fcf",args=[libro.id])

#Factura Ct
class FacturaCtCV(CreateView):
    model = FacturaCt
    template_name = "iva/lfct.html"
    form_class = ContribuyenteForm

    def get_context_data(self, **kwargs):
        facturas = Libro.objects.get(id=self.kwargs["libro"]).facturact
        context = super(FacturaCtCV,self).get_context_data(**kwargs)
        context["libro"] = Libro.objects.get(id=self.kwargs["libro"])
        context['direccion'] = 'cont:nueva_fct'
        context['titulo'] = 'Crear Factura Contribuyente'
        context["parametro"] = self.kwargs['libro']
        context["totales"] = [
            facturas.all().aggregate(total=Sum('venExentas'))["total"],
            facturas.all().aggregate(total=Sum('venGravadas'))["total"],
            facturas.all().aggregate(total=Sum('ventasNSujetas'))["total"],
            facturas.all().aggregate(total=Sum('ivaDebFiscal'))["total"],
            facturas.all().aggregate(total=Sum('vtVentas'))["total"],
            facturas.all().aggregate(total=Sum('vtIVA'))["total"],
            facturas.all().aggregate(total=Sum('ivaRetenido'))["total"],
            facturas.all().aggregate(total=Sum('total'))["total"],
        ]
        return context

    def get_initial(self, **kwargs):
        initial = super(FacturaCtCV,self).get_initial()
        initial["libro"] = Libro.objects.get(id=self.kwargs["libro"]).id
        return initial

    def get_success_url(self,**kwargs):
        libro=Libro.objects.get(id=self.kwargs["libro"])
        return reverse("iva:nueva_fct",args=[libro.id])

#Factura Cm
class FacturaCmCV(CreateView):
    model = FacturaCm
    template_name = "iva/lfcm.html"
    form_class = ComprasForm

    def get_context_data(self, **kwargs):
        facturas = Libro.objects.get(id=self.kwargs["libro"]).facturacm
        context = super(FacturaCmCV,self).get_context_data(**kwargs)
        context["libro"] = Libro.objects.get(id=self.kwargs["libro"])
        context['direccion'] = 'cont:nueva_fcm'
        context['titulo'] = 'Crear Factura Compra'
        context["parametro"] = self.kwargs['libro']
        context["totales"] = [
            facturas.all().aggregate(total=Sum('cExenteInterna'))["total"],
            facturas.all().aggregate(total=Sum('cExenteImportaciones'))["total"],
            facturas.all().aggregate(total=Sum('cGravadaInterna'))["total"],
            facturas.all().aggregate(total=Sum('cGravadaImportaciones'))["total"],
            facturas.all().aggregate(total=Sum('comprasNSujetas'))["total"],
            facturas.all().aggregate(total=Sum('ivaCdtoFiscal'))["total"],
            facturas.all().aggregate(total=Sum('totalCompra'))["total"],
            facturas.all().aggregate(total=Sum('retencionPretencion'))["total"],
            facturas.all().aggregate(total=Sum('anticipoCtaIva'))["total"],
            facturas.all().aggregate(total=Sum('ivaTerceros'))["total"],
        ]
        return context

    def get_initial(self, **kwargs):
        initial = super(FacturaCmCV,self).get_initial()
        initial["libro"] = Libro.objects.get(id=self.kwargs["libro"]).id
        return initial

    def get_success_url(self,**kwargs):
        libro=Libro.objects.get(id=self.kwargs["libro"])
        return reverse("iva:nueva_fcm",args=[libro.id])

#Libros vistas
class LibroCV(CreateView):
    model = Libro
    template_name = "iva/modal.html"
    form_class = LibroForm

    def get_context_data(self, **kwargs):
        context = super(LibroCV,self).get_context_data(**kwargs)
        context["empresa"] = Cliente.objects.get(id=self.kwargs["empresa"])
        context['direccion'] = 'iva:nuevo_libro'
        context['titulo'] = 'Crear Libro'
        context["tipo"] = self.kwargs["tipo"]
        context["parametro"] = self.kwargs['empresa']
        context["parametro2"] = self.kwargs['tipo']

        return context

    def get_initial(self, **kwargs):
        initial = super(LibroCV,self).get_initial()
        initial["cliente"] = Cliente.objects.get(id=self.kwargs["empresa"]).id
        initial["tipo"] = self.kwargs["tipo"]
        return initial

    def get_success_url(self,**kwargs):
        return reverse("iva:lista_libro",args=[self.kwargs["empresa"],self.kwargs["tipo"]])

class LibroLV(ListView):
    model = Libro
    template_name = "iva/llibro.html"
    context_object_name = 'libros'

    def get_context_data(self, **kwargs):
        context = super(LibroLV,self).get_context_data(**kwargs)
        context["cliente"] = Cliente.objects.get(id=self.kwargs['empresa'])
        context["tipo"] = self.kwargs["tipo"]
        return context

    def get_queryset(self):
        queryset = super(LibroLV, self).get_queryset()
        queryset = queryset.filter(cliente__id = self.kwargs['empresa'],tipo=self.kwargs["tipo"]).order_by('ano','mes')
        return queryset

class EmpresaDV(DetailView):
    model = Cliente
    template_name = "iva/detalle_cliente.html"
    context_object_name = "cliente"

#Empresa Vistas
class EmpresaCV(CreateView):
    model = Empresa
    template_name = "iva/empresa.html"
    form_class = EmpresaF

    def get_context_data(self, **kwargs):
        context = super(EmpresaCV,self).get_context_data(**kwargs)
        context['direccion'] = 'cont:nuevo_empresa'
        context['titulo'] = 'Crear Empresa'
        return context

class EmpresaDetail(DetailView):
    model = Empresa
    template_name='empresaJson.html'
    def get(self,request,*args, **kwarg ):
        empresa = Empresa.objects.get(nRegistro = self.kwargs['nReg'])
        empresa = serialize('json',[empresa,])
        return HttpResponse(empresa,'application/json')

#Exportacion
class ExportarView(View):
    def get(self, request, *args, **kwargs):
        tipo = self.kwargs.get('tipo')
        id_libro = self.kwargs.get('id_libro')
        libro = Libro.objects.get(id=id_libro)

        if tipo == 1:
            tipol = "Consumidor"
            libroEx = export_libroCF(id_libro)
        elif tipo == 2:
            tipol = "Contibuyente"
            libroEx = export_libroct(id_libro)
        elif tipo == 3:
            tipol = "Compras"
            libroEx = export_librocm(id_libro)
        print(libro)
        # create the HttpResponse object ...
        response = FileResponse(open(libroEx, 'rb'))
        return response


from flask import Flask, render_template, flash, redirect, request, url_for, jsonify
from multiprocessing import Process, Queue
from xBee_recieve import reciever

app = Flask(__name__)

processes = []
collectedData = []

def getNewXbeeData(q):
    PORT = "COM2"
    BAUD = 9600
    MAC  = "13A20041C7BFFC"

    r = reciever(PORT, BAUD, MAC)

    while True:
        msg = r.check_for_message()
        if msg:
            q.put(msg) # data needs to first be parsed, so if the msg is a json, we need to format to [msg['x'], msg['y']]

#tester method to get generated data should function the same as getNewXbeeData
def getNewRandomData(q):
    """
    temp()\n
    accel()\n
    mag()\n
    gyro()\n
    euler()\n
    quaternion()\n
    linear_accel()\n
    gravity()\n
    """
    import time
    from random import randint

    t = 0
    lastAccel = [0,0,0]
    while True:
        r = randint(5,10)/10.0
        print(r)
        time.sleep(r)
        t += r

        data = {
            "time"  : t,
            "accel" : [lastAccel[0] + randint(-20,20),lastAccel[1] + randint(-20,20),lastAccel[2] + randint(-20,20)],
            "gyro"  : [randint(-20,20),randint(-20,20),randint(-20,20)],
            "temp"  : randint(30,100),
            }
        q.put(data)

        lastAccel = data["accel"]

@app.route("/", methods=["GET", ])
def main():
    return render_template("main.html")

#main page
@app.route('/api/<data>/<num>', methods=['GET'])
def api(data, num):
    q = processes[0][0]

    while not q.empty():
        d = q.get()
        collectedData.append(d)

    #num is current size of users data, so we only give them the data they dont have
    out = []
    if "accel" in data:
        n = 0
        if "Y" in data:
            n = 1
        elif "Z" in data:
            n = 2

        for d in collectedData[int(num)::]:
            out.append([d["time"], d["accel"][n]])

    elif "gyro" in data:
        n = 0
        if "Y" in data:
            n = 1
        elif "Z" in data:
            n = 2

        for d in collectedData[int(num)::]:
            out.append([d["time"], d["gyro"][n]])

    elif data == "temp":
        for d in collectedData[int(num)::]:
            out.append([d["time"], d["temp"]])

    return jsonify(out)

if __name__ == '__main__':
    q = Queue()
    p = Process(target=getNewRandomData, args=[q,])
    processes.append((q,p))
    p.start()

    app.run(host="0.0.0.0", port=80)
    for p in processes:
        p[1].terminate()


#!/usr/bin/python3

import os
import requests

my_ip_file = os.path.join("/tmp", "myIp.txt")

def myIp():
    return requests.get("https://gianlu.dev/ip").text.strip()

def writToFile(filename, content):
    fp = open(filename, "wt", encoding="utf8")
    fp.write(content)
    fp.close()

def readFile(filename):
    fp = open(filename, "rt", encoding="utf8")
    content = fp.read().strip()
    fp.close()
    return content

if not os.path.exists(my_ip_file):
    writToFile(my_ip_file, "")

current_ip = myIp()
if current_ip != readFile(my_ip_file):
    print(current_ip, readFile(my_ip_file))
    writToFile(my_ip_file, current_ip)

    import Client
    Client.mainFunc()


import time

class criatura():
    """docstring for criatura"""
    def __init__(self, pos, dir, imagen, tic, toc, tiempo_entre_mov):
        self.pos = None
        self.dir = None
        self.imagen = None
        self.tic = None
        self.toc = None
        self.tiempo_entre_mov = None

class pac_man():
    """docstring for pac_man"""
    def __init__(self):
        criatura.__init__(
            self,
            pos = (23, 13),  # posición actual (fila, columna)
            dir = 1,  # dirección hacia la que avanza
            imagen = ('pacman0','pacman1','pacman2'),  # imágenes del pacman
            tic = time.time(),  # para el cronómetro
            toc = 0,
            tiempo_entre_mov = 1.0/10,  # muevase 10 cuadros por segundo
        )
        self.invencible = False  # se comió la vitamina
        self.boca_abierta = 1  # boca abierta 0, 1, 2, 3(1)
        self.vidas = 3

class fantasma():
    """docstring for fantasma"""
    def __init__(self):
        criatura.__init__(
            self,
            pos = (0, 0),  # posición actual (fila, columna)
            dir = 1,  # dirección hacia la que avanza
            imagen = 0,  # imágenes del fantasma
            tic = time.time(),  # para el cronómetro
            toc = 0,
            tiempo_entre_mov = 1.0/8  # se mueve 8 cuadros/segundo
        )
        self.__comido = False  # se lo comio el pacman: True/False

    def dar_comido(self, comido):
        self.__comido = comido

    def obtener_comido(self):
        return self.__comido

if __name__ == '__main__':
    pacman = pac_man()
    print(pacman.pos)

    fant = fantasma()
    print(fant.obtener_comido())


if __name__ == "__main__":
    # Метод Гаусса
    f = open("Input.txt")
    RANGE = int(f.read(1))  # Записываем количество строк матрицы

    COLUMN = int(f.read(1))  # Записываем количество столбцов матрицы

    mat = []  # Считываем матрицу из файла
    f.read(1)
    for line in f.readlines():
        mat.append(line.split())

    for i in range(RANGE):  # Преобразуем матрицу к вещественным числам
        for j in range(COLUMN):
            mat[i][j] = float(mat[i][j])
    f.close()

    mas = []  # Создаем массив для записи искомых переменных (обратный ход)

    def next_run(mainIndex):  # Прямой ход
        i = mainIndex
        while i < RANGE - 1:  # Зануляем все элементы под элементом главной диагонали
            if mat[i + 1][mainIndex] != 0 and mat[mainIndex][mainIndex] != 0:
                kof = mat[i + 1][mainIndex] / mat[mainIndex][
                    mainIndex] * -1  # Находим множитель для дальнейших преобразований
                transf(i, mainIndex, kof)
            i += 1

    def transf(secondIndex, mainIndex, kof):  # Складываем строку побочную с главной строкой умноженной на коэффициент
        j = mainIndex
        while j < COLUMN:
            mat[secondIndex + 1][j] = mat[mainIndex][j] * kof + mat[secondIndex + 1][j]
            j += 1

    def back_run(mainIndex):  # Обратный ход
        j = mainIndex
        sum = 0
        while j + 1 < COLUMN - 1:  # Находим сумму коэффициентов умноженную на найденные переменные
            sum += mat[mainIndex][j + 1] * mas[case_mas(j + 1)]
            j += 1

        sum = (sum * -1 + mat[mainIndex][COLUMN - 1]) / mat[mainIndex][
            mainIndex]  # Находим неизвестное член и добавляем его в массив
        mas.append(sum)

    def case_mas(i):  # Находим индекс элемента для соответствующего коэффициента
        return RANGE - (i + 1)

    print("Добро пожаловать сейчас мы приведем матрицу к "  # Основное тело программы
          "треугольному виду и найдем неизвестные методом Гаусса")

    print("Исходная матрица")
    for i in mat:  # Выводим матрицу
        print(i)
    print()

    for i in range(RANGE - 1):  # Прямой ход
        next_run(i)

    print("Преобразованная матрица")
    for i in mat:  # Выводим матрицу
        print(i)
    print()

    if mat[RANGE - 1][RANGE - 1] == 0:  # Обратный ход
        print("Метод Гаусса не работает")
    else:
        i = RANGE - 1
        while i >= 0:
            back_run(i)
            i -= 1
        print("Искомые переменные\n", mas)  # Выводим искомый массив переменных


import socketserver
import sys

from python import http
import requestParsing

class TCPHandler(socketserver.BaseRequestHandler):

    def handle(self):
        recieved_data = self.request.recv(1024)
        print(self.client_address[0] + " is sending data")
        print("----")
        print(recieved_data.decode())
        print("\n\n")
        sys.stdout.flush()

        header_end = "\r\n\r\n".encode() # convert end of of header mark to bytes

        data = []

        if recieved_data.find(header_end) != -1:
            data = recieved_data.split(header_end, 1) # use it to separate request
        else:
            data = [recieved_data, b'']

        # only decode header
        header = data[0].decode().split("\r\n")

        request_line = header[0].split(" ")
        if request_line[0] == "GET":
            if request_line[1] == '/':
                response = http.html_header("website/index.html")
                self.request.sendall(response)

            elif request_line[1].find(".html") != -1:
                fname = "website" + request_line[1]
                response = http.html_header(fname)
                self.request.sendall(response)

            elif request_line[1].find(".js") != -1:
                fname = "website" + request_line[1]
                response = http.js_header(fname)
                self.request.sendall(response)

            elif request_line[1].find(".css") != -1:
                fname = "website" + request_line[1]
                response = http.css_header(fname)
                self.request.sendall(response)

            elif request_line[1].find("/images/") != -1:
                fname = "website" + request_line[1]
                f_type = fname.split(".")[1]
                response = http.image_header(fname, f_type)
                self.request.sendall(response)

            else:
                response = http.not_found()
                self.request.sendall(response)
        else:
            postData = requestParsing.parseRequest(self, header, recieved_data)
            if postData != ():
                response = http.byteResponse(postData)
                self.request.sendall(response)

if __name__ == '__main__':
    host = "localhost"
    port = 8000

    server = socketserver.ThreadingTCPServer((host, port), TCPHandler)
    server.serve_forever()


from django.shortcuts import render, redirect, get_object_or_404
from .forms import LibroForm
from django.shortcuts import render
from .models import Libro
from django.urls import reverse_lazy
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic.edit import CreateView
from django.views.generic.edit import UpdateView

class IngresarLibroView(LoginRequiredMixin, CreateView):
    model = Libro
    form_class = LibroForm
    template_name = 'libros/ingresar_libro.html'
    success_url = reverse_lazy('lista_libros')

    def form_valid(self, form):

        form.instance.usuario = self.request.user

        titulo = form.cleaned_data['titulo']
        autor = form.cleaned_data['autor']
        if not Libro.objects.filter(titulo=titulo, autor=autor).exists():
            return super().form_valid(form)
        else:

            form.add_error('titulo', 'Este libro ya existe en la biblioteca.')
            return self.form_invalid(form)

class EditarLibroView(LoginRequiredMixin, UpdateView):
    model = Libro
    form_class = LibroForm
    template_name = 'libros/editar_libro.html'

    success_url = reverse_lazy('lista_libros')

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['libro'] = self.get_object()
        return context

    def form_valid(self, form):
        return super().form_valid(form)

def lista_libros(request):
    libros = Libro.objects.all().order_by('titulo')
    return render(request, 'libros/lista_libros.html', {'libros': libros})

def eliminar_libro(request, libro_id):
    libro = get_object_or_404(Libro, id=libro_id)

    if request.method == 'POST':
        libro.delete()
        return redirect('lista_libros')

    return render(request, 'libros/eliminar_libro.html', {'libro': libro})

def lista_detalle_libros(request):
    libros = Libro.objects.all().order_by('titulo')
    return render(request, 'libros/lista_detalle_libros.html', {'libros': libros})


range_lower = 146810
range_higher = 612564

def is_valid(number):
  as_string = str(number)
  adjacents = []

  for i in range(1, len(as_string)):
    current = int(as_string[i])
    previous = int(as_string[i-1])
    if current < previous:
      return False

    if current == previous:
      adjacents.append((i, i-1, current))

  # Part one, comment these two lines to get part two answer
  if len(adjacents):
    return True

  number_of_adjacents = {}
  for a in adjacents:
    if a[2] in number_of_adjacents:
      number_of_adjacents[a[2]] += 1
    else:
      number_of_adjacents[a[2]] = 1

  # Part two
  if 1 in number_of_adjacents.values():
    return True

total = 0
for i in range(range_lower, range_higher+1):
  if is_valid(i):
    total += 1

print(total)
# print(is_valid(123444))


from google.cloud import firestore, storage, exceptions
import os

db = firestore.Client()
content = db.collection('fl_content')

storage_client = storage.client.Client()
bucket = storage_client.get_bucket('psyclonic-studios-website.appspot.com')

def new_transaction():
    return db.transaction()

@firestore.transactional
def get_artwork_collection(transaction, size, args):
    artworks_query = content.where('_fl_meta_.schema', '==', 'artwork')
    artworks_query = sort_query(artworks_query, args)
    artworks = []
    for artwork_ref in artworks_query.stream(transaction=transaction):
        artwork = artwork_ref.to_dict()
        image_refs = artwork['images']
        artwork['images'] = [get_sized_image_urls(image.get(transaction=transaction).to_dict(), size) for image in artwork['images']]
        artwork['inventory'] = int(artwork['inventory']) # hack to fix flamelink screwup
        artworks.append(artwork)
    return artworks

@firestore.transactional
def get_artwork(transaction, id, size):
    artwork = content.document(id).get(transaction=transaction).to_dict()
    if not artwork:
        return None
    artwork['images'] = [get_sized_image_urls(image.get(transaction=transaction).to_dict(), size) for image in artwork['images']]
    artwork['inventory'] = int(artwork['inventory']) # hack to fix flamelink screwup
    return artwork

@firestore.transactional
def get_artwork_from_ref(transaction, ref, size):
    artwork = ref.get(transaction=transaction).to_dict()
    if not artwork:
        return None
    artwork['images'] = [get_sized_image_urls(image.get(transaction=transaction).to_dict(), size) for image in artwork['images']]
    return artwork

@firestore.transactional
def get_non_series_artwork_collection(transaction, size, args):
    artworks_query = content.where('_fl_meta_.schema', '==', 'artwork').where('partOfASeries', '==', False)
    artworks_query = sort_query(artworks_query, args)
    artworks = []
    for artwork_ref in artworks_query.stream(transaction=transaction):
        artwork = artwork_ref.to_dict()
        image_refs = artwork['images']
        artwork['images'] = [get_sized_image_urls(image.get(transaction=transaction).to_dict(), size) for image in artwork['images']]
        artworks.append(artwork)
    return artworks

@firestore.transactional
def get_series_collection(transaction, size, args):
    series_query = content.where('_fl_meta_.schema', '==', 'series')
    series_query = sort_query(series_query, args)
    series_collection = []
    for series_ref in series_query.stream(transaction=transaction):
        series = series_ref.to_dict()
        series_image_refs = series['seriesImages']
        if series_image_refs:
            series_image_urls = [get_sized_image_urls(image.get(transaction=transaction).to_dict(), size) for image in series_image_refs]
            series['thumbnail_image'] = get_sized_image_urls(series_image_refs[0].get(transaction=transaction).to_dict(), size)
        else:
            artwork = series['artworks'][0].get(transaction=transaction).to_dict()
            artwork_image = artwork['images'][0].get(transaction=transaction).to_dict()
            artwork_image_url = get_sized_image_urls(artwork_image, size)
            series['thumbnail_image'] = artwork_image_url
        series_collection.append(series)
    return series_collection

@firestore.transactional
def get_series(transaction, id, size):
    series = content.document(id).get(transaction=transaction).to_dict()
    # todo
    if series is None:
        return None
    series_image_refs = series['seriesImages']
    series_image_urls = [get_sized_image_urls(image.get(transaction=transaction).to_dict(), size) for image in series_image_refs]
    if not series:
        return None
    artworks_resolved = []
    for artwork_ref in series['artworks']:
        artwork = artwork_ref.get(transaction=transaction).to_dict()
        image_refs = artwork['images']
        image_urls = [get_sized_image_urls(image.get(transaction=transaction).to_dict(), size) for image in image_refs]
        artwork['images'] = image_urls
        artwork['inventory'] = int(artwork['inventory']) # hack to fix flamelink screwup
        artworks_resolved.append(artwork)
    series['artworks_resolved'] = artworks_resolved
    series['series_images'] = series_image_urls
    return series

#
#@firestore.transactional
#def get_blog_collection(transaction, size, args):
#    blog_collection_query = content.where('_fl_meta_.schema', '==', 'posts').where('status', '==', 'published')
#    blog_collection_query = sort_query(blog_collection_query, args)
#    blog_collection = []
#    for blog_ref in blog_collection_query.stream(transaction=transaction):
#        blog = blog_ref.to_dict()
#        blog_thumbnail_ref = blog['thumbnail'][0]
#        blog_thumbnail = get_file_url(get_image_size_path(blog_thumbnail_ref.get(transaction=transaction).to_dict(), size))
#        blog['thumbnail_image'] = blog_thumbnail
#        blog_collection.append(blog)
#    return blog_collection
#
#@firestore.transactional
#def get_blog(transaction, id, size):
#    blog = content.document(id).get(transaction=transaction).to_dict()
#    thumbnail_ref = blog['thumbnail'][0]
#    blog['thumbnail_image'] = get_file_url(get_image_size_path(thumbnail_ref.get(transaction=transaction).to_dict(), size))
#    return blog

@firestore.transactional
def get_home_images(transaction):
    home_images_query = content.where('_fl_meta_.schema', '==', 'websiteImages').where('position', '==', 'Home').limit(1)
    home_images = next(home_images_query.stream(transaction=transaction)).to_dict()
    home_images['images'] = [get_sized_image_urls(image.get(transaction=transaction).to_dict()) for image in home_images['images']]
    return home_images

def get_cost(cost):
    query = content.where('_fl_meta_.schema', '==', 'costs').where('name', '==', cost).limit(1)
    cost = next(query.stream()).to_dict()
    return cost['cost']

def get_international_shipping():
    return get_cost('International shipping')

def get_website_component(component):
    query = content.where('_fl_meta_.schema', '==', 'websiteComponents').where('component', '==', component).limit(1)
    component = next(query.stream()).to_dict()
    return component['content']

def get_home_text():
    return get_website_component('Home')

def get_about():
    return get_website_component('About')

def get_policies():
    return get_website_component('Policies')

@firestore.transactional
def get_contribute_products(transaction, size, args):
    contribute_products_query = content.where('_fl_meta_.schema', '==', 'supportProducts').where('available', '==', True)
    contribute_products_query = sort_query(contribute_products_query, args)
    contribute_products = []
    for product_ref in contribute_products_query.stream(transaction=transaction):
        product = product_ref.to_dict()
        product['sku'] = f'sku_{product["id"]}'
        product_artwork_image_ref = product['artworkImage'][0]
        product['artwork_image'] = get_sized_image_urls(product_artwork_image_ref.get(transaction=transaction).to_dict(), size)
        product_image_ref = product['productImage'][0]
        product['product_image'] = get_sized_image_urls(product_image_ref.get(transaction=transaction).to_dict(), size)
        contribute_products.append(product)
    return contribute_products

#def sync_contribute_products_to_stripe():
#    contribution_product_id = STRIPE_DATA['contribution_product_id']
#    contribute_products = get_contribute_products(new_transaction(), 375, None)
#    products = {product['sku']: product for product in contribute_products}
#    stripe_skus = stripe.SKU.list(product=contribution_product_id, limit=100)['data']
#    stripe_sku_list = [sku['id'] for sku in stripe_skus]
#    existing_skus = filter(lambda sku: sku in stripe_sku_list, products.keys())
#    new_skus = filter(lambda sku: sku not in stripe_sku_list, products.keys())
#
#    for sku in existing_skus:
#        product = products[sku]
#        stripe.SKU.modify(
#            sku,
#            currency='aud',
#            inventory={'type': 'infinite'},
#            active=product['available'],
#            price=int(product['basePrice'] * 100),
#            image=product['product_image_url'],
#            product=contribution_product_id,
#            attributes={'name': product['title']}
#        )
#
#    for sku in new_skus:
#        product = products[sku]
#        stripe.SKU.create(
#            id=product['sku'],
#            currency='aud',
#            inventory={'type': 'infinite'},
#            active=product['available'],
#            price=int(product['basePrice'] * 100),
#            image=product['product_image_url'],
#            product=contribution_product_id,
#            attributes={'name': product['title']}
#        )
#
#def get_donation_skus():
#    donation_product_id = STRIPE_DATA['donation_product_id']
#    donation_skus = stripe.SKU.list(product=donation_product_id)['data']
#    return sorted(donation_skus, key=lambda sku: sku['price'])
#
#def get_shipping_sku():
#    shipping_sku = stripe.SKU.retrieve(STRIPE_DATA['shipping_sku_id'])
#    return shipping_sku

def get_contribute_text():
    return get_website_component('Contribute')

def get_subscribe():
    return get_website_component('Subscribe')

def get_contact_message():
    return get_website_component('Contact message')

def get_contact_email_template():
    return get_website_component('Contact email template')

def get_subscribe_success():
    return get_website_component('Thankyou subscribe')

def post_email_address(email):
    subscribers = db.collection('subscribers')
    subscribers.document(email).set({'subscribe': True}, merge=True)

def get_artwork_buy_email_template():
    return get_website_component('Artwork buy email')

def get_artwork_enquiry_email_template():
    return get_website_component('Artwork enquire email')

def get_series_enquiry_email_template():
    return get_website_component('Series enquire email')

def get_enquire_thankyou():
    return get_website_component('Thankyou enquiry')

def get_payment_success():
    return get_website_component('Thankyou payment')

def get_order(id):
    order = db.collection('orders').document(id).get().to_dict()
    transaction = new_transaction()
    artworks = [{'artwork': get_artwork_from_ref(transaction, artwork['artwork'], 300), 'quantity': artwork['quantity']} for artwork in  order['artworks']]
    order['artworks'] = artworks
    return order

def finalise_order(payment_intent):
    orders = db.collection('orders')
    order = orders.document(payment_intent.id)
    order.update({
        'payment_recieved': True,
        'customer': {
            'name': payment_intent.shipping.name,
            'email': payment_intent.receipt_email
        },
        'shipping': {
            'street': payment_intent.shipping.address.line1,
            'city': payment_intent.shipping.address.city,
            'state': payment_intent.shipping.address.state,
            'country': payment_intent.shipping.address.country,
            'postal_code': payment_intent.shipping.address.postal_code,
        },
        'paid_at': firestore.SERVER_TIMESTAMP
    })

    artworks = order.get().to_dict()['artworks']
    for artwork in artworks:
        artwork['artwork'].update({'inventory': firestore.Increment(-artwork['quantity'])})

def update_order(payment_intent_id, cart, subtotal, shipping_cost, total, payment_recieved):
    orders = db.collection('orders')
    order = orders.document(payment_intent_id)
    try:
        order_doc = order.get()
        if not order_doc.to_dict():
            order.set({'created_at': firestore.SERVER_TIMESTAMP}, merge=True)
    except exceptions.NotFound:
        order.set({'created_at': firestore.SERVER_TIMESTAMP}, merge=True)
    artworks = [{'artwork': content.document(id), 'quantity': cart[id]} for id in cart]
    order_update = {'payment_recieved': False, 'artworks': artworks, 'cost': {'subtotal': subtotal, 'shipping': shipping_cost, 'total': total}}
    order.update(order_update)

def get_flamelink_file_url(path):
    flamelink_path = 'flamelink/media'
    blob = bucket.blob(os.path.join(flamelink_path, path))
    return blob.public_url

def get_sized_image_urls(image_dict, upto=None):
    filename = image_dict['file']
    image_dict['full_size'] = {'width': 'full', 'storage_path': filename, 'url': get_flamelink_file_url(filename)}
    sizes = image_dict['sizes']
    if upto:
        sizes = list(filter(lambda size: size['width'] <= upto, sizes))
    for s in sizes:
        s['storage_path'] = os.path.join('sized', str(s['path']), filename)
    sizes = {s['width']: s for s in sizes}
    sizes[240] = {'width': 240, 'storage_path': os.path.join('sized', str(240), filename)}
    for size in sizes.values():
        size['url'] = get_flamelink_file_url(size['storage_path'])
    image_dict['full_size'] = sizes[max(sizes)]
    image_dict['sizes'] = sizes
    return image_dict

def sort_query(query, args=None):
    if args is None:
        return query
    sort_by = args.get('sort_by','')
    sort_direction = args.get('sort_direction','')
    if sort_by:
        if sort_direction == 'descending':
            query = query.order_by(sort_by, direction=firestore.Query.DESCENDING)
        elif sort_direction == 'ascending':
            query = query.order_by(sort_by, direction=firestore.Query.ASCENDING)
        else:
            query = query.order_by(sort_by)
    return query


from .operator import Operator
from .loader import load_from_dir
from .built_in import BUILTIN_OPERATORS

class OperatorRegistry:
    def __init__(self):
        self.plugin_contexts = load_from_dir()

    def list_operators(self):
        """Lists the available FiftyOne operators.

        Returns:
            a list of operators
        """
        plugin_contexts = self.plugin_contexts
        operators = []
        for plugin_context in plugin_contexts:
            for operator in plugin_context.instances:
                if isinstance(operator, Operator):
                    operators.append(operator)
        return operators + BUILTIN_OPERATORS

    def list_errors(self):
        """Lists the errors that occurred during operator loading.

        Returns:
            a list of errors
        """
        plugin_contexts = self.plugin_contexts
        errors = []
        for plugin_context in plugin_contexts:
            errors.extend(plugin_context.errors)
        return errors

    def operator_exists(self, operator_uri):
        """Checks if the operator exists.

        Args:
            operator_uri: the URI of the operator

        Returns:
            ``True`` if the operator exists, ``False`` otherwise
        """
        operators = self.list_operators()
        return operator_uri in [o.uri for o in operators]

    def get_operator(self, operator_uri):
        operators = self.list_operators()
        for operator in operators:
            if operator_uri == operator.uri:
                return operator

def register_operator(operator):
    """Registers a built-in operator. For internal use only.

    Args:
        operator: the operator to register
    """
    if operator.name in BUILTIN_OPERATORS:
        raise ValueError("Operator '%s' already exists" % operator.name)
    BUILTIN_OPERATORS[operator.name] = operator


import torch
import seaborn as sn
from matplotlib import pyplot as plt
from model import ConvNet
from MnistDataset import Mydataset
from torch.utils.data import DataLoader
import numpy as np
import pandas as pd

torch.manual_seed(13)

def get_score(confusion_mat):
    smooth = 0.0001  #防止出现除数为0而加上一个很小的数
    tp = np.diagonal(confusion_mat)
    fp = np.sum(confusion_mat, axis=0)
    fn = np.sum(confusion_mat, axis=1)
    precision = tp / (fp + smooth)
    recall = tp / (fn + smooth)
    f1 = 2 * precision * recall / (precision + recall + smooth)
    return precision, recall, f1

def get_confusion(confusion_matrix, out, label):
    idx = np.argmax(out.detach().numpy())
    confusion_matrix[idx, label] += 1
    return confusion_matrix

def main():
    confusion_matrix = np.zeros((10, 10))

    net = ConvNet()
    net.load_state_dict(torch.load('model_parameter\\parameter_epo90.pth'))
    test_path = ['test.txt', r'dataset/test_label.txt']
    test_dataset = Mydataset(test_path[0], test_path[1], 'cpu')
    test_dataloader = DataLoader(test_dataset, 1, True)
    for i, (pic, label) in enumerate(test_dataloader):
        out = net(pic)
        confusion_matrix = get_confusion(confusion_matrix, out, label)

    precision, recall, f1 = get_score(confusion_matrix)
    print(f'precision: {np.average(precision)}\trecall: {np.average(recall)}\tf1: {np.average(f1)}')
    confusion_mat = pd.DataFrame(confusion_matrix)
    confusion_df = pd.DataFrame(confusion_mat, index=[i for i in range(10)], columns=[i for i in range(10)])
    sn.heatmap(data=confusion_df, cmap='RdBu_r')
    plt.show()
    confusion_df.to_csv(r'confusion.csv', encoding='ANSI')

if __name__ == '__main__':
    main()


one = input()
two = input()
oneL = len(one)
twoL = len(two)
dp = [[0] * (twoL + 1) for _ in range(oneL + 1)]
for i in range(1, oneL + 1):
    for j in range(1, twoL + 1):
        if one[i - 1] == two[j - 1]:
            dp[i][j] = dp[i - 1][j - 1] + 1
        else:
            dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
word = ""
i = oneL
j = twoL
while i > 0 and j > 0:
    if one[i - 1] == two[j - 1]:
        word += one[i - 1]
        i -= 1
        j -= 1
    elif dp[i - 1][j] > dp[i][j - 1]:
        i -= 1
    else:
        j -= 1

print(word[::-1])


import json

class Destinations:
    def __init__(self):
        self.destination = ""
        self.file_name = "destinations.json"

    def write_to_json_file(self):
        dictionary = {
            "destination": self.destination
        }
        json_object = json.dumps(dictionary, indent=1, ensure_ascii=False)
        with open(self.file_name, 'a', encoding='utf-8') as f:
            f.write(json_object)


from rest_framework_simplejwt.authentication import JWTAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.decorators import (
    api_view,
    permission_classes,
    authentication_classes
)

from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404
from repository.models import Repository, Branch, Commit
from repository.serializers.repo_serializers import (
    RepositorySerializer,
    RepositoryCreateSerializer,
    RepositoryUpdateSerializer,
)

from repository.serializers.branch_serializers import BranchSerializer
from repository.serializers.commit_serializers import CommitSerializer

from users.serializers import UserSerializer

from backend.exceptions import GeneralException

from datetime import datetime
import requests
import re
import json

import pytz

@api_view(['GET'])
@authentication_classes([JWTAuthentication])
@permission_classes([IsAuthenticated])
def get_one_repo(request, repo_id):
    repo = get_object_or_404(Repository, pk=repo_id)

    serializer = RepositorySerializer(repo, many=False)
    return Response(serializer.data)

@api_view(['GET'])
@authentication_classes([JWTAuthentication])
@permission_classes([IsAuthenticated])
def get_all_repos(request, username):
    repos = Repository.objects.filter(user__username=username)

    serializer = RepositorySerializer(repos, many=True)
    return Response(serializer.data)

@api_view(['POST'])
@authentication_classes([JWTAuthentication])
@permission_classes([IsAuthenticated])
def create_repo(request):
    repo_ser = RepositoryCreateSerializer(data=request.data)

    if not repo_ser.is_valid():
        raise GeneralException("Invalid request.")

    found_repos = Repository.objects.filter(name=repo_ser.data['name'])
    if len(found_repos) > 0:
        raise GeneralException("Repository with given name already exists.")

    repo = Repository.objects.create(
        name=repo_ser.data['name'],
        description=repo_ser.data['description'],
        url=repo_ser.data['url'],
        is_private=repo_ser.data['is_private'],
        user=request.user,
    )

    repo.save()
    load_repo(repo, request.user)

    serializer = RepositorySerializer(repo, many=False)
    return Response(serializer.data)

@api_view(['PUT'])
@authentication_classes([JWTAuthentication])
@permission_classes([IsAuthenticated])
def update_repo(request, repo_id):
    repo_ser = RepositoryUpdateSerializer(data=request.data)

    if not repo_ser.is_valid():
        raise GeneralException("Invalid request.")

    repo = get_object_or_404(Repository, pk=repo_id)

    if (repo.name != repo_ser.data['name']):
        found_repos = Repository.objects.filter(name=repo_ser.data['name'])
        if len(found_repos) > 0:
            raise GeneralException(
                "Repository with given name already exists.")

    repo.name = repo_ser.data['name']
    repo.description = repo_ser.data['description']
    repo.is_private = repo_ser.data['is_private']
    repo.save()

    repo.refresh_from_db()

    serializer = RepositorySerializer(repo, many=False)
    return Response(serializer.data)

@api_view(['PUT'])
@authentication_classes([JWTAuthentication])
@permission_classes([IsAuthenticated])
def reload_repo(request, repo_id):
    repo = get_object_or_404(Repository, pk=repo_id)

    branches = Branch.objects.filter(repo__id=repo_id)

    for branch in branches:
        branch.delete()

    load_repo(repo, request.user)

    return Response()

@api_view(['DELETE'])
@authentication_classes([JWTAuthentication])
@permission_classes([IsAuthenticated])
def delete_repo(request, repo_id):
    repo = get_object_or_404(Repository, pk=repo_id)
    repo.delete()

    return Response()

def load_repo_readme(remote_username, remote_repo_name):
    # Fetch readme
    readme_info_resp = requests.get(
        'https://api.github.com/repos/{0}/{1}/readme'.format(remote_username, remote_repo_name))

    readme_info = readme_info_resp.json()
    readme_text_resp = requests.get(readme_info['download_url'])

    return readme_text_resp.text

def load_repo(repo, user):
    groups = re.findall(r"^https:\/\/github.com\/(.*)\/(.*)", repo.url)
    remote_username = groups[0][0]
    remote_repo_name = groups[0][1]

    # Get and set README
    repo.readme = load_repo_readme(remote_username, remote_repo_name)
    repo.save()

    branches_resp = requests.get(
        'https://api.github.com/repos/{0}/{1}/branches'.format(remote_username, remote_repo_name))

    for b in branches_resp.json():
        branch = Branch.objects.create(
            name=b['name'],
            creator=user,
            repo=repo,
            last_commit=None,
        )

        branch.save()

        commits_resp = requests.get(
            'https://api.github.com/repos/{0}/{1}/commits?sha={2}'
            .format(remote_username, remote_repo_name, b['name']))

        for c in commits_resp.json():
            c_time = datetime.strptime(
                c['commit']['author']['date'], '%Y-%m-%dT%H:%M:%SZ')

            timezone = pytz.timezone("Europe/Belgrade")
            c_time_zoned = timezone.localize(c_time)

            commit = Commit.objects.create(
                message=c['commit']['message'],
                hash=c['sha'],
                timestamp=c_time_zoned,
                author_email=c['commit']['author']['email'],
                branch=branch,
            )

    # Add latest commit to branch
    for b in branches_resp.json():
        branches = Branch.objects.filter(
            repo__name=repo.name, name=b['name'])

        commits = Commit.objects.filter(
            branch__name=b['name'], hash=b['commit']['sha'])

        if len(branches) > 0:
            if len(commits) > 0:
                branches[0].last_commit = commits[0]
                branches[0].save()

    return Response(commits_resp.json())

@api_view(['GET'])
@authentication_classes([JWTAuthentication])
@permission_classes([IsAuthenticated])
def get_all_branches(request, repo_name):
    repos = Branch.objects.filter(repo__name=repo_name)

    serializer = BranchSerializer(repos, many=True)
    return Response(serializer.data)

@api_view(['GET'])
@authentication_classes([JWTAuthentication])
@permission_classes([IsAuthenticated])
def get_all_commits(request, repo_name, branch_name):
    repos = Commit.objects.filter(
        branch__repo__name=repo_name).filter(branch__name=branch_name.replace('~', '/'))

    serializer = CommitSerializer(repos, many=True)
    return Response(serializer.data)

@api_view(['GET'])
@authentication_classes([JWTAuthentication])
@permission_classes([IsAuthenticated])
def get_repo_collaborators(request, repo_id):
    repo = get_object_or_404(Repository, pk=repo_id)
    serializer = UserSerializer(repo.collaborators, many=True)
    return Response(serializer.data)

@api_view(['PUT'])
@authentication_classes([JWTAuthentication])
@permission_classes([IsAuthenticated])
def update_collaborators(request, repo_id):
    repo = get_object_or_404(Repository, pk=repo_id)
    signed_in_user = request.user.id

    if repo.user.id != signed_in_user:
        raise GeneralException("Not authorized")

    user_id_list = request.data

    if len(user_id_list) > 0:
        repo.collaborators.clear()
        repo.collaborators.add(*user_id_list)
    else:
        repo.assignees.clear()

    repo.save()
    repo.refresh_from_db()

    serializer = UserSerializer(repo.collaborators, many=True)
    return Response(serializer.data)

@api_view(['GET'])
@authentication_classes([JWTAuthentication])
@permission_classes([IsAuthenticated])
def search_users_for_collaborators(request, repo_id, search_value):
    signed_in_user = request.user.id
    repo = get_object_or_404(Repository, pk=repo_id);
    if repo.user.id != signed_in_user:
        raise GeneralException("Not authorized")

    repo_collaborators = repo.collaborators.all()

    potential_collaborators = User.objects.filter(is_active=True, is_superuser=False, is_staff=False, username__icontains=search_value).exclude(pk=signed_in_user)

    serializer = UserSerializer(potential_collaborators.difference(repo_collaborators), many=True)
    return Response(serializer.data)


#coding=utf8
import numpy as np
np.random.seed(1337)  # for reproducibility
import re
import h5py
import os
from nltk import tokenize
from keras.preprocessing.text import Tokenizer, text_to_word_sequence
from attention import Attention_input1, Attention_input2
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.utils.np_utils import to_categorical
from keras.layers import Reshape, Dense, Input, Flatten, Dropout, merge, BatchNormalization
from keras.layers import TimeDistributed, LSTM, GRU, Bidirectional
from keras.models import Model
from keras.optimizers import SGD, Adadelta, Adam, RMSprop
from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras.layers.core import Reshape, RepeatVector

from keras.callbacks import EarlyStopping
from keras.models import Sequential, Model
from keras.layers import Activation, Dense, Dropout, Embedding, Flatten, Input, Merge, Convolution1D, MaxPooling1D

GLOVE_DIR = '../data/'

MAX_SEQUENCE_LENGTH = 140
MAX_NB_WORDS = 10000
EMBEDDING_DIM = 200
VALIDATION_SPLIT = 0.1
NB_EPOCH = 100
NB_CLASS = 3
DIM_HIDDEN = 128
DIM_LSTM = 128

# datamode = 'mul'
datamode = 'single'

if datamode == 'mul':
    DATA_PATH = '../data/MSVA_multiple_17024.h5'
    BATCH_SIZE = 128
else:
    DATA_PATH = '../data/MSVA_single_4511.h5'
    BATCH_SIZE = 32

def load_data():
    read_file = h5py.File(DATA_PATH, 'r')
    texts = read_file['txt_data'][:]
    labels = read_file['label'][:]
    scenes = read_file['scene_data'][:]
    objects = read_file['object_data'][:]
    return texts,labels,scenes,objects
def split_data(data,VALIDATION_SPLIT):
    nb_validation_samples = int(VALIDATION_SPLIT * data.shape[0])
    data_train = data[:-(nb_validation_samples * 2)]
    data_val = data[-(nb_validation_samples * 2):-(nb_validation_samples)]
    data_test = data[-nb_validation_samples:]
    return data_train,data_val,data_test

def dp_txt(txt):
    # nonEnglish_regex = re.compile('[^a-zA-Z0-9\\?\\!\\,\\.@#\\+\\-=\\*\'\"><&\\$%\\(\\)\\[\\]:;]+')
    hashtag_pattern = re.compile('#[a-zA-Z0-9]+')
    at_pattern = re.compile('@[a-zA-Z0-9]+')
    http_pattern = re.compile("((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?")
    txt = txt.strip()
    txt_hashtag = re.sub(hashtag_pattern, '', txt)
    txt_nonat = re.sub(at_pattern, '', txt_hashtag)
    txt_nonhttp = re.sub(http_pattern, '', txt_nonat)
    txt = txt_nonhttp
    return txt

def fun():
    texts,labels,scenes,objects = load_data()
    new_texts = []
    for idx in range(len(texts)):
        text = texts[idx]
        text = dp_txt(str(text))
        new_texts.append(text)
    texts = new_texts

    tokenizer = Tokenizer(nb_words=MAX_NB_WORDS)
    tokenizer.fit_on_texts(texts)
    sequences = tokenizer.texts_to_sequences(texts)
    word_index = tokenizer.word_index

    text_data = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LENGTH)
    labels = to_categorical(np.asarray(labels))

    # print('Text tensor shape:', text_data.shape)
    # print('Label tensor shape:', labels.shape)
    # print('Scene tensor shape:', scenes.shape)
    # print('Object tensor shape:', objects.shape)
    # # split the text_data into a training set and a validation set
    rand = np.arange(labels.shape[0])
    np.random.shuffle(rand)
    indices = rand

    text_data = text_data[indices]
    labels = labels[indices]
    scenes = scenes[indices]
    objects = objects[indices]
    text_train,text_val,text_test = split_data(text_data,VALIDATION_SPLIT)
    label_train,label_val,label_test = split_data(labels,VALIDATION_SPLIT)
    scene_train,scene_val,scene_test = split_data(scenes,VALIDATION_SPLIT)
    object_train,object_val,object_test = split_data(objects,VALIDATION_SPLIT)

    text_shape = text_train.shape[1:]
    scene_shape = scene_train.shape[1:]
    object_shape = object_train.shape[1:]

    embeddings_index = {}
    f = open(os.path.join(GLOVE_DIR, 'glove.6B.200d.txt'))
    for line in f:
        values = line.split()
        word = values[2]
        coefs = np.asarray(values[1], dtype='float32')
        embeddings_index[word] = coefs
    f.close()

    nb_words = min(MAX_NB_WORDS, len(word_index))
    embedding_matrix = np.zeros((nb_words + 1, EMBEDDING_DIM))
    for word, i in word_index.items():
        if i > MAX_NB_WORDS:
            continue
        embedding_vector = embeddings_index.get(word)
        if embedding_vector is not None:
            embedding_matrix[i] = embedding_vector

    embedding_layer = Embedding(nb_words + 1,
                                EMBEDDING_DIM,
                                weights=[embedding_matrix],
                                input_length=MAX_SEQUENCE_LENGTH,
                                trainable=True)

    save_best = ModelCheckpoint('../../model/{}.hdf5'.format('my_weight'), save_best_only=True)
    elstop = EarlyStopping(monitor='val_loss', min_delta=1e-4, patience=5)

    # Image Sence
    scene_input = Input(shape=scene_shape, dtype='float32')
    img_scene = Dense(DIM_HIDDEN, activation='relu')(scene_input)
    img_scene_encoder = RepeatVector(text_shape[0], name='scene-repeat')(img_scene)

    # Image Object
    object_input = Input(shape=object_shape, dtype='float32')
    img_object = Dense(DIM_HIDDEN, activation='relu')(object_input)
    img_object_encoder = RepeatVector(text_shape[0], name='object-repeat')(img_object)

    # Text
    txt_input = Input(shape=text_shape, dtype='float32')
    txt = embedding_layer(txt_input)
    txt_hidden = (LSTM(DIM_HIDDEN, return_sequences=True, name='tweet-lstm'))(txt)
    txt_att = Attention_input2(name='att_so')([txt_hidden, img_object_encoder, img_scene_encoder])

    # Merge
    img_txt = merge([img_scene, img_object, txt_att], mode='concat')
    img_txt = Dense(DIM_HIDDEN, activation='relu')(img_txt)
    img_txt_loss = Dense(NB_CLASS, activation='softmax', name='main_output')(img_txt)

    model = Model(input=[txt_input, scene_input, object_input], output=[img_txt_loss])
    model.compile(loss='categorical_crossentropy', optimizer='RMSprop',
                  metrics=['acc', 'fmeasure'])
    model.fit([text_train, scene_train, object_train], [label_train],
              validation_data=([text_val, scene_val, object_val], [label_val]),
              nb_epoch=NB_EPOCH, batch_size=BATCH_SIZE, callbacks=[elstop,save_best], verbose=1)

    model.load_weights('../../model/{}.hdf5'.format('my_weight'))
    score = model.evaluate([text_test, scene_test, object_test], label_test, verbose=0)

    print('results：', score[1], score[2])
    return score[1:]

if __name__ == '__main__':
    fun()


# spaceconfig = {"usemodules" : ["_collections"]}

from _collections import deque
from pytest import raises

def test_basics():
    assert deque.__module__ == 'collections'

    d = deque(xrange(-5125, -5000))
    d.__init__(xrange(200))
    for i in xrange(200, 400):
        d.append(i)
    for i in reversed(xrange(-200, 0)):
        d.appendleft(i)
    assert list(d) == range(-200, 400)
    assert len(d) == 600

    left = [d.popleft() for i in xrange(250)]
    assert left == range(-200, 50)
    assert list(d) == range(50, 400)

    right = [d.pop() for i in xrange(250)]
    right.reverse()
    assert right == range(150, 400)
    assert list(d) == range(50, 150)

def test_maxlen():
    raises(ValueError, deque, 'abc', -1)
    raises(ValueError, deque, 'abc', -2)
    it = iter(range(10))
    d = deque(it, maxlen=3)
    assert list(it) == []
    assert repr(d) == 'deque([7, 8, 9], maxlen=3)'
    assert list(d) == range(7, 10)
    d.appendleft(3)
    assert list(d) == [3, 7, 8]
    d.extend([20, 21])
    assert list(d) == [8, 20, 21]
    d.extendleft([-7, -6])
    assert list(d) == [-6, -7, 8]

def test_maxlen_zero():
    it = iter(range(100))
    d = deque(it, maxlen=0)
    assert list(d) == []
    assert list(it) == []
    d.extend(range(100))
    assert list(d) == []
    d.extendleft(range(100))
    assert list(d) == []

def test_maxlen_attribute():
    assert deque().maxlen is None
    assert deque('abc').maxlen is None
    assert deque('abc', maxlen=4).maxlen == 4
    assert deque('abc', maxlen=0).maxlen == 0
    raises((AttributeError, TypeError), "deque('abc').maxlen = 10")

def test_runtimeerror():
    d = deque('abcdefg')
    it = iter(d)
    d.pop()
    raises(RuntimeError, it.next)
    #
    d = deque('abcdefg')
    it = iter(d)
    d.append(d.pop())
    raises(RuntimeError, it.next)
    #
    d = deque()
    it = iter(d)
    d.append(10)
    raises(RuntimeError, it.next)

def test_count():
    for s in ('', 'abracadabra', 'simsalabim'*50+'abc'):
        s = list(s)
        d = deque(s)
        for letter in 'abcdeilmrs':
            assert s.count(letter) == d.count(letter)
    class MutatingCompare:
        def __eq__(self, other):
            d.pop()
            return True
    m = MutatingCompare()
    d = deque([1, 2, 3, m, 4, 5])
    raises(RuntimeError, d.count, 3)

def test_comparisons():
    d = deque('xabc'); d.popleft()
    for e in [d, deque('abc'), deque('ab'), deque(), list(d)]:
        assert (d==e) == (type(d)==type(e) and list(d)==list(e))
        assert (d!=e) == (not(type(d)==type(e) and list(d)==list(e)))

    args = map(deque, ('', 'a', 'b', 'ab', 'ba', 'abc', 'xba', 'xabc', 'cba'))
    for x in args:
        for y in args:
            assert (x == y) == (list(x) == list(y))
            assert (x != y) == (list(x) != list(y))
            assert (x <  y) == (list(x) <  list(y))
            assert (x <= y) == (list(x) <= list(y))
            assert (x >  y) == (list(x) >  list(y))
            assert (x >= y) == (list(x) >= list(y))
            assert cmp(x,y) == cmp(list(x),list(y))

def test_extend():
    d = deque('a')
    d.extend('bcd')
    assert list(d) == list('abcd')
    d.extend(d)
    assert list(d) == list('abcdabcd')

def test_iadd():
    d = deque('a')
    original_d = d
    d += 'bcd'
    assert list(d) == list('abcd')
    d += d
    assert list(d) == list('abcdabcd')
    assert original_d is d

def test_extendleft():
    d = deque('a')
    d.extendleft('bcd')
    assert list(d) == list(reversed('abcd'))
    d.extendleft(d)
    assert list(d) == list('abcddcba')

def test_getitem():
    n = 200
    l = xrange(1000, 1000 + n)
    d = deque(l)
    for j in xrange(-n, n):
        assert d[j] == l[j]
    raises(IndexError, "d[-n-1]")
    raises(IndexError, "d[n]")

def test_setitem():
    n = 200
    d = deque(xrange(n))
    for i in xrange(n):
        d[i] = 10 * i
    assert list(d) == [10*i for i in xrange(n)]
    l = list(d)
    for i in xrange(1-n, 0, -3):
        d[i] = 7*i
        l[i] = 7*i
    assert list(d) == l

def test_delitem():
    d = deque("abcdef")
    del d[-2]
    assert list(d) == list("abcdf")

def test_reverse():
    d = deque(xrange(1000, 1200))
    d.reverse()
    assert list(d) == list(reversed(range(1000, 1200)))
    #
    n = 100
    data = map(str, range(n))
    for i in range(n):
        d = deque(data[:i])
        r = d.reverse()
        assert list(d) == list(reversed(data[:i]))
        assert r is None
        d.reverse()
        assert list(d) == data[:i]

def test_rotate():
    s = tuple('abcde')
    n = len(s)

    d = deque(s)
    d.rotate(1)             # verify rot(1)
    assert ''.join(d) == 'eabcd'

    d = deque(s)
    d.rotate(-1)            # verify rot(-1)
    assert ''.join(d) == 'bcdea'
    d.rotate()              # check default to 1
    assert tuple(d) == s

    d.rotate(500000002)
    assert tuple(d) == tuple('deabc')
    d.rotate(-5000002)
    assert tuple(d) == tuple(s)

def test_len():
    d = deque('ab')
    assert len(d) == 2
    d.popleft()
    assert len(d) == 1
    d.pop()
    assert len(d) == 0
    raises(IndexError, d.pop)
    raises(IndexError, d.popleft)
    assert len(d) == 0
    d.append('c')
    assert len(d) == 1
    d.appendleft('d')
    assert len(d) == 2
    d.clear()
    assert len(d) == 0
    assert list(d) == []

def test_remove():
    d = deque('abcdefghcij')
    d.remove('c')
    assert d == deque('abdefghcij')
    d.remove('c')
    assert d == deque('abdefghij')
    raises(ValueError, d.remove, 'c')
    assert d == deque('abdefghij')

def test_repr():
    d = deque(xrange(20))
    e = eval(repr(d))
    assert d == e
    d.append(d)
    assert '...' in repr(d)

def test_hash():
    raises(TypeError, hash, deque('abc'))

def test_roundtrip_iter_init():
    d = deque(xrange(200))
    e = deque(d)
    assert d is not e
    assert d == e
    assert list(d) == list(e)

def test_reduce():
    #
    d = deque('hello world')
    r = d.__reduce__()
    assert r == (deque, (list('hello world'),))
    #
    d = deque('hello world', 42)
    r = d.__reduce__()
    assert r == (deque, (list('hello world'), 42))
    #
    class D(deque):
        pass
    d = D('hello world')
    d.a = 5
    r = d.__reduce__()
    assert r == (D, (list('hello world'), None), {'a': 5})
    #
    class D(deque):
        pass
    d = D('hello world', 42)
    d.a = 5
    r = d.__reduce__()
    assert r == (D, (list('hello world'), 42), {'a': 5})

def test_copy():
    import copy
    mut = [10]
    d = deque([mut])
    e = copy.copy(d)
    assert d is not e
    assert d == e
    mut[0] = 11
    assert d == e

def test_reversed():
    for s in ('abcd', xrange(200)):
        assert list(reversed(deque(s))) == list(reversed(s))

def test_free():
    import gc
    class X(object):
        freed = False
        def __del__(self):
            X.freed = True
    d = deque()
    d.append(X())
    d.pop()
    gc.collect(); gc.collect(); gc.collect()
    assert X.freed

def test_index_method():
    d = deque([1, 2, 3, 4, 5])
    class A(object):
        def __index__(self):
            return 1
    assert d[A()] == 2

def test_index_method_mutates():
    d = deque([1, 2, 3, 4, 5])
    class A(object):
        def __index__(self):
            d.clear()
            return 1
    with raises(IndexError):
        d[A()]
    d = deque([1, 2, 3, 4, 5])
    with raises(IndexError):
        d[A()] = 2


import re
from math import ceil

import dateparser

from aspen.database.models import TreeType
from aspen.workflows.nextstrain_run.build_plugins.base_plugin import BaseConfigPlugin

class TreeTypePlugin(BaseConfigPlugin):
    crowding_penalty: float = 0
    tree_type: TreeType
    subsampling_scheme: str = "NONE"

    def _update_config_params(self, config):
        if not config.get("builds"):
            # TODO, force MPX structure to look more like SC2's
            config["builds"] = {"aspen": {}}
        build = config["builds"]["aspen"]

        location = self.template_args["location"]
        # Make a shortcut to decide whether this is a location vs division vs country level build
        if not location.division:
            self.tree_build_level = "country"
        elif not location.location:
            self.tree_build_level = "division"
        # Fill out country/division/location fields if the group has them,
        # or remove those fields if they don't.
        location_fields = ["country", "division", "location"]
        location_values = []
        for field in location_fields:
            value = getattr(location, field)
            if value:
                build[field] = value
                location_values.append(value)
            else:
                if build.get(field):
                    del build[field]

        # NOTE: <TreeTypePlugin>.subsampling_scheme is used in 3 places:
        #   - Its lowercase'd name is used to find a markdown file with an "about this tree" description
        #   - It refers to a subsampling_scheme key in the mega nextstrain template
        #   - It's title-case'd and included in the tree title as human-readable text
        build["subsampling_scheme"] = self.subsampling_scheme

        # Update the tree's title with build type, location and date range.
        # We always provide some form of end date in the title.
        end_date = self._get_formatted_tree_end_date()
        # We base format of title on whether we have a `filter_start_date`
        if self.template_args.get("filter_start_date") is not None:
            title_template = "{tree_type} tree for samples collected in {location} between {start_date} and {end_date}"
            build["title"] = title_template.format(
                tree_type=self.subsampling_scheme.title(),
                location=", ".join(location_values),
                start_date=dateparser.parse(
                    self.template_args.get("filter_start_date")
                ).strftime("%Y-%m-%d"),
                end_date=end_date,
            )
        else:
            title_template = "{tree_type} tree for samples collected in {location} up until {end_date}"
            build["title"] = title_template.format(
                tree_type=self.subsampling_scheme.title(),
                location=", ".join(location_values),
                end_date=end_date,
            )

        if config.get("files"):
            config["files"]["description"] = config["files"]["description"].format(
                tree_type=self.subsampling_scheme.lower()
            )
        if config.get("priorities"):
            config["priorities"]["crowding_penalty"] = self.crowding_penalty

    def _get_formatted_tree_end_date(self):
        """Returns appropriate YYYY-MM-DD for tree's end date or "--" if none.

        For tree titles, we want to always have an end date to display. If
        the tree had a `filter_end_date` arg, we can use that. However, if no
        filter arg was given for the end date, we use the implicit end date of
        when the tree build was kicked off (from PhyloRun.start_datetime), as
        the tree build process can only use samples up to the moment in time
        when it was kicked off, so it's an implicit end date to samples.

        If there is no date available at all, we return "--" as an absolute
        fall back. PhyloRun.start_datetime is not actually guaranteed at the DB
        level, but all our code that creates runs always provides one (as of
        Nov 2022, every single run has a start_datetime). The fall back is
        provided just to code defensively in case something weird ever happens.
        """
        formatted_end_date = "--"  # safe default, should never happen
        filter_end_date = self.template_args.get("filter_end_date")
        if filter_end_date is not None:
            formatted_end_date = dateparser.parse(filter_end_date).strftime("%Y-%m-%d")
        else:
            # `run_start_datetime` is a `context` kwarg, so not guaranteed
            run_start_datetime = getattr(self, "run_start_datetime", None)
            if run_start_datetime is not None:
                formatted_end_date = run_start_datetime.strftime("%Y-%m-%d")
            else:
                print("WARNING -- Run missing a start_datetime. Default to '--'")
        return formatted_end_date

    def update_config(self, config):
        self._update_config_params(config)

        subsampling = config["subsampling"][self.subsampling_scheme]
        self.run_type_config(config, subsampling)

        # Remove unused subsampling schemes from our output file
        config["subsampling"] = {self.subsampling_scheme: subsampling}

    def run_type_config(self, config, subsampling):
        raise NotImplementedError("base class doesn't implement this")

class OverviewPlugin(TreeTypePlugin):
    crowding_penalty = 0.1
    tree_type = TreeType.OVERVIEW
    subsampling_scheme = "OVERVIEW"

    def run_type_config(self, config, subsampling):
        if self.group.name == "Chicago Department of Public Health":
            if "--query" in subsampling["group"]["query"]:  # SC2 format
                subsampling["group"][
                    "query"
                ] = '''--query "((location == '{location}') & (division == '{division}')) | submitting_lab == 'RIPHL at Rush University Medical Center'"'''
            else:  # MPX format
                subsampling["group"]["query"] = (
                    "("
                    + subsampling["group"]["query"]
                    + ") | submitting_lab == 'RIPHL at Rush University Medical Center'"
                )

        # Handle sampling date & pango lineage filters
        apply_filters(config, subsampling, self.template_args)

        # Update our sampling for state/country level builds if necessary
        update_subsampling_for_location(self.tree_build_level, subsampling)

        # Update country and international max sequences.
        if self.tree_build_level == "country":
            subsampling["international"]["max_sequences"] = 1000
        if self.tree_build_level == "division":
            subsampling["country"]["max_sequences"] = 800
            subsampling["international"]["max_sequences"] = 200

        # If there aren't any selected samples
        # Either due to being a scheduled run or no user selection
        # Put reference sequences in include.txt so tree run don't break
        if self.num_included_samples == 0:
            if config.get("files", {}).get("include"):
                del config["files"]["include"]

class NonContextualizedPlugin(TreeTypePlugin):
    crowding_penalty = 0.1
    tree_type = TreeType.NON_CONTEXTUALIZED
    subsampling_scheme = "NON_CONTEXTUALIZED"

    def run_type_config(self, config, subsampling):
        # Handle sampling date & pango lineage filters
        apply_filters(config, subsampling, self.template_args)

        # Update our sampling for state/country level builds if necessary
        update_subsampling_for_location(self.tree_build_level, subsampling)

        # If there aren't any selected samples due to no user selection
        # Put reference sequences in include.txt so tree run don't break
        if self.num_included_samples == 0:
            if config.get("files", {}).get("include"):
                del config["files"]["include"]

# Set max_sequences for targeted builds.
class TargetedPlugin(TreeTypePlugin):
    crowding_penalty = 0
    tree_type = TreeType.TARGETED
    subsampling_scheme = "TARGETED"

    def run_type_config(self, config, subsampling):
        """
        DATA we can use in this function:
          config : the entire mega-template data structure, with some fields already updated by BaseNextstrainConfigBuilder.update_build()
          subsampling : the subsampling scheme for *this build type only* (ex: mega_template["subsampling"]["TARGETED"])
          self.subsampling_scheme : the value a few lines above
          self.crowding_penalty : the value a few lines above
          self.group : information about the group that this run is for (ex: self.group.name or self.group.default_tree_location)
          self.num_sequences : the number of aspen samples written to our fasta input file
          self.num_included_samples : the number of samples in include.txt (aspen + gisaid samples) for on-demand runs only

        EXAMPLES SECTION:
          Delete a group from a subsampling scheme:
              del subsampling["international"]

          Delete a setting from a group:
              del subsampling["international"]["seq_per_group"]

          Add a group to a subsampling scheme:
              subsampling["my_new_group_name"] = {
                  "group_by": "region",
                  "max_sequences": 200,
                  "query": '--query "(foo != {bar})"'
              }

          Add a setting to a group (this is the same as updating an existing setting!):
              subsampling["international"]["mynewsetting"] = "mynewvalue"
        """
        # Adjust group sizes if we have a lot of samples.
        closest_max_sequences = 100
        other_max_sequences = 25
        if self.num_included_samples >= 100:
            closest_max_sequences = self.num_included_samples
            other_max_sequences = int(ceil(self.num_included_samples / 4.0))

        subsampling["closest"]["max_sequences"] = closest_max_sequences

        subsampling["group"]["max_sequences"] = (
            other_max_sequences * 2
        )  # Temp mitigation for missing on-demand overview
        subsampling["state"]["max_sequences"] = (
            other_max_sequences * 2
        )  # Temp mitigation for missing on-demand overview
        subsampling["country"]["max_sequences"] = other_max_sequences
        subsampling["international"]["max_sequences"] = other_max_sequences

        # Update our sampling for state/country level builds if necessary
        update_subsampling_for_location(self.tree_build_level, subsampling)

        # Increase int'l sequences for state/country builds.
        if (
            self.tree_build_level != "location"
            and subsampling["international"]["max_sequences"] < 100
        ):
            subsampling["international"]["max_sequences"] = 100

def update_subsampling_for_location(tree_build_level, subsampling):
    if tree_build_level == "country":
        update_subsampling_for_country(subsampling)
    if tree_build_level == "division":
        update_subsampling_for_division(subsampling)

def update_subsampling_for_country(subsampling):
    # State and country aren't useful
    if "state" in subsampling:
        del subsampling["state"]
    if "country" in subsampling:
        del subsampling["country"]
    # Update our local group query
    if "--query" in subsampling["group"]["query"]:
        subsampling["group"]["query"] = '''--query "(country == '{country}')"'''
    else:
        subsampling["group"]["query"] = "(country == '{country}')"

def update_subsampling_for_division(subsampling):
    # State isn't useful
    if "state" in subsampling:
        del subsampling["state"]
    # Update our local group query
    if "--query" in subsampling["group"]["query"]:
        subsampling["group"][
            "query"
        ] = '''--query "(division == '{division}') & (country == '{country}')"'''  # Keep the country filter in case of multiple divisions worldwide
    else:
        subsampling["group"][
            "query"
        ] = "(division == '{division}') & (country == '{country}')"  # Keep the country filter in case of multiple divisions worldwide

def apply_filters(config, subsampling, template_args):
    # MPX format
    include_arguments_in_filters = False
    lineage_field = "lineage"
    if "--query" in subsampling["group"]["query"]:
        # SC2 format
        include_arguments_in_filters = True
        lineage_field = "pango_lineage"

    min_date = template_args.get("filter_start_date")
    if min_date:
        # Support date expressions like "5 days ago" in our cron schedule.
        min_date = dateparser.parse(min_date).strftime("%Y-%m-%d")
        if include_arguments_in_filters:
            subsampling["group"][
                "min_date"
            ] = f"--min-date {min_date}"  # ex: --max-date 2020-01-01
        else:
            subsampling["group"]["min-date"] = str(min_date)  # ex: max-date: 2020-01-01
    max_date = template_args.get("filter_end_date")
    if max_date:
        # Support date expressions like "5 days ago" in our cron schedule.
        max_date = dateparser.parse(max_date).strftime("%Y-%m-%d")
        if include_arguments_in_filters:
            subsampling["group"][
                "max_date"
            ] = f"--max-date {max_date}"  # ex: --max-date 2020-01-01
            if "international_serial_sampling" in subsampling:
                subsampling["international_serial_sampling"][
                    "max_date"
                ] = f"--max-date {max_date}"  # ex: --max-date 2020-01-01
        else:
            subsampling["group"]["max-date"] = str(max_date)  # ex: max-date: 2020-01-01
            if "international_serial_sampling" in subsampling:
                subsampling["international_serial_sampling"]["max-date"] = str(
                    max_date
                )  # ex: max-date: 2020-01-01

    pango_lineages = template_args.get("filter_pango_lineages")
    if pango_lineages:
        # Nextstrain is rather particular about the acceptable syntax for
        # values in the pango_lineages key. Before modifying please see
        # https://discussion.nextstrain.org/t/failure-when-specifying-multiple-pango-lineages-in-a-build/670
        clean_values = [re.sub(r"[^0-9a-zA-Z.]", "", item) for item in pango_lineages]
        clean_values.sort()
        config["builds"]["aspen"]["pango_lineage"] = clean_values
        # Remove the last " from our old query so we can inject more filters
        end_string = ""
        old_query = subsampling["group"]["query"]
        if old_query.endswith('"'):
            end_string = '"'
            old_query = old_query[:-1]
        pango_query = " & (" + lineage_field + " in {pango_lineage})"
        subsampling["group"]["query"] = old_query + pango_query + end_string


import random
from itertools import chain

import numpy as np
import pandas as pd
from cytoolz import itemmap, sliding_window, valmap
from skfusion import fusion

class DataFusionModel(object):
    def __init__(
        self, nodes, relations, init_type="random", random_state=666, n_jobs=1
    ):
        self.nodes = nodes
        self.relation_definitions = relations

        self.random_state = random_state
        self.n_jobs = n_jobs
        self.init_type = init_type

    def reconstruct(self, src, dst, idx=0, return_dataframe=True):
        relation = list(
            self.fuser.fusion_graph.get_relations(self.types[src], self.types[dst])
        )[idx]
        values = self.fuser.complete(relation)

        if return_dataframe:
            components = self.relation_definitions[(src, dst)][idx]
            return pd.DataFrame(
                values, index=components.index.values, columns=components.columns.values
            )

        return values

    def factor(self, type_name, return_dataframe=True):
        factor = self.fuser.factor(self.types[type_name])
        if not return_dataframe:
            return factor

        profile = pd.DataFrame(
            factor,
            index=self.indices[type_name],
            columns=[f"C{i:02}" for i in range(factor.shape[1])],
        )
        return profile

    def _construct_relationship(self, path, updated_factors):
        start_node = path[0]
        end_node = path[-1]

        computed_matrix = (
            self.fuser.factor(start_node)
            if not start_node.name in updated_factors
            else updated_factors[start_node.name]
        )
        print(
            type(start_node),
            start_node,
            start_node.name in updated_factors,
            computed_matrix.shape,
        )

        for src, dst in sliding_window(2, path):
            relation = list(self.fuser.fusion_graph.get_relations(src, dst))[0]
            print(relation)
            computed_matrix = np.dot(computed_matrix, self.fuser.backbone(relation))

        end_factor = (
            self.fuser.factor(end_node)
            if not end_node.name in updated_factors
            else updated_factors[end_node.name]
        )
        computed_matrix = np.dot(computed_matrix, end_factor.T)

        return computed_matrix

    def relation_profiles(self, src, dst, updated_factors=None, index=None):
        if updated_factors is None:
            updated_factors = {}
        if index is None:
            index = self.indices[src]

        paths = list(self.fuser.chain(self.types[src], self.types[dst]))

        relations = []
        for path in paths:
            rel = self._construct_relationship(path, updated_factors)
            profile = pd.DataFrame(rel, index=index, columns=self.indices[dst])
            relations.append(profile)

        return list(zip(paths, relations))

    def fit(self, method='factorization'):
        self.types = dict(
            zip(
                self.nodes.keys(),
                map(lambda x: fusion.ObjectType(*x), self.nodes.items()),
            )
        )
        print(self.types)

        self.relations = map(
            lambda x: map(
                lambda r: fusion.Relation(
                    r.values, self.types[x[0][0]], self.types[x[0][1]]
                ),
                x[1],
            ),
            self.relation_definitions.items(),
        )
        self.relations = list(chain(*self.relations))
        print(self.relations)

        self.indices = {}
        for (src, dst), dfs in self.relation_definitions.items():
            if not src in self.indices:
                self.indices[src] = list(dfs[0].index)
            if not dst in self.indices:
                self.indices[dst] = list(dfs[0].columns)

        random.seed(self.random_state)
        np.random.seed(self.random_state)

        self.fusion_graph = fusion.FusionGraph(self.relations)

        if method == 'factorization':
            fuser = fusion.Dfmf
        elif method == 'completion':
            fuser = fusion.Dfmc
        else:
            raise ValueError('method must be factorization or completion')

        self.fuser = fuser(
            init_type=self.init_type, random_state=self.random_state, n_jobs=self.n_jobs
        )

        self.fuser.fuse(self.fusion_graph)