from bench.samples.long_tail.random_sampler.assign_result import AssignResult
import torch
from abc import ABCMeta, abstractmethod
from bench.core.executer import Executer

class BaseSampler(metaclass=ABCMeta):
    """Base class of samplers."""
    def __init__(self,
                 num,
                 pos_fraction,
                 neg_pos_ub=-1,
                 add_gt_as_proposals=True,
                 **kwargs):
        self.num = num
        self.pos_fraction = pos_fraction
        self.neg_pos_ub = neg_pos_ub
        self.add_gt_as_proposals = add_gt_as_proposals
        self.pos_sampler = self
        self.neg_sampler = self

    @abstractmethod
    def _sample_pos(self, assign_result, num_expected, **kwargs):
        """Sample positive samples."""
        pass

    @abstractmethod
    def _sample_neg(self, assign_result, num_expected, **kwargs):
        """Sample negative samples."""
        pass

    def sample(self,
               assign_result,
               bboxes,
               gt_bboxes,
               gt_labels=None,
               **kwargs):
        """Sample positive and negative bboxes.

        This is a simple implementation of bbox sampling given candidates,
        assigning results and ground truth bboxes.

        Args:
            assign_result (:obj:`AssignResult`): Bbox assigning results.
            bboxes (Tensor): Boxes to be sampled from.
            gt_bboxes (Tensor): Ground truth bboxes.
            gt_labels (Tensor, optional): Class labels of ground truth bboxes.

        Returns:
            :obj:`SamplingResult`: Sampling result.

        Example:
            >>> from mmdet.core.bbox import RandomSampler
            >>> from mmdet.core.bbox import AssignResult
            >>> from mmdet.core.bbox.demodata import ensure_rng, random_boxes
            >>> rng = ensure_rng(None)
            >>> assign_result = AssignResult.random(rng=rng)
            >>> bboxes = random_boxes(assign_result.num_preds, rng=rng)
            >>> gt_bboxes = random_boxes(assign_result.num_gts, rng=rng)
            >>> gt_labels = None
            >>> self = RandomSampler(num=32, pos_fraction=0.5, neg_pos_ub=-1,
            >>>                      add_gt_as_proposals=False)
            >>> self = self.sample(assign_result, bboxes, gt_bboxes, gt_labels)
        """
        if len(bboxes.shape) < 2:
            bboxes = bboxes[None, :]

        bboxes = bboxes[:, :4]

        gt_flags = bboxes.new_zeros((bboxes.shape[0], ), dtype=torch.uint8)
        if self.add_gt_as_proposals and len(gt_bboxes) > 0:
            if gt_labels is None:
                raise ValueError(
                    "gt_labels must be given when add_gt_as_proposals is True")
            bboxes = torch.cat([gt_bboxes, bboxes], dim=0)
            assign_result.add_gt_(gt_labels)
            gt_ones = bboxes.new_ones(gt_bboxes.shape[0], dtype=torch.uint8)
            gt_flags = torch.cat([gt_ones, gt_flags])

        num_expected_pos = int(self.num * self.pos_fraction)
        pos_inds = self.pos_sampler._sample_pos(assign_result,
                                                num_expected_pos,
                                                bboxes=bboxes,
                                                **kwargs)
        # We found that sampled indices have duplicated items occasionally.
        # (may be a bug of PyTorch)
        pos_inds = pos_inds.unique()
        num_sampled_pos = pos_inds.numel()
        num_expected_neg = self.num - num_sampled_pos
        if self.neg_pos_ub >= 0:
            _pos = max(1, num_sampled_pos)
            neg_upper_bound = int(self.neg_pos_ub * _pos)
            if num_expected_neg > neg_upper_bound:
                num_expected_neg = neg_upper_bound
        neg_inds = self.neg_sampler._sample_neg(assign_result,
                                                num_expected_neg,
                                                bboxes=bboxes,
                                                **kwargs)
        neg_inds = neg_inds.unique()

        # # Using SamplingResult needs to import a lot of python packages,
        # # so we ignore it.
        # sampling_result = SamplingResult(pos_inds, neg_inds, bboxes,
        # gt_bboxes, assign_result, gt_flags)
        # return sampling_result
        return pos_inds, neg_inds, bboxes, gt_bboxes, assign_result, gt_flags

class RandomSampler(BaseSampler):
    """Random sampler.

    Args:
        num (int): Number of samples
        pos_fraction (float): Fraction of positive samples
        neg_pos_up (int, optional): Upper bound number of negative and
            positive samples. Defaults to -1.
        add_gt_as_proposals (bool, optional): Whether to add ground truth
            boxes as proposals. Defaults to True.
    """
    def __init__(self,
                 num,
                 pos_fraction,
                 neg_pos_ub=-1,
                 add_gt_as_proposals=True,
                 **kwargs):
        super(RandomSampler, self).__init__(num, pos_fraction, neg_pos_ub,
                                            add_gt_as_proposals)

    def random_choice(self, gallery, num):
        """Random select some elements from the gallery.

        If `gallery` is a Tensor, the returned indices will be a Tensor;
        If `gallery` is a ndarray or list, the returned indices will be a
        ndarray.

        Args:
            gallery (Tensor | ndarray | list): indices pool.
            num (int): expected sample num.

        Returns:
            Tensor or ndarray: sampled indices.
        """
        assert len(gallery) >= num

        is_tensor = isinstance(gallery, torch.Tensor)
        if not is_tensor:
            gallery = torch.tensor(gallery,
                                   dtype=torch.long,
                                   device=torch.cuda.current_device())
        perm = torch.randperm(gallery.numel(), device=gallery.device)[:num]
        rand_inds = gallery[perm]
        if not is_tensor:
            rand_inds = rand_inds.cpu().numpy()
        return rand_inds

    def _sample_pos(self, assign_result, num_expected, **kwargs):
        """Randomly sample some positive samples."""
        pos_inds = torch.nonzero(assign_result.gt_inds > 0, as_tuple=False)
        if pos_inds.numel() != 0:
            pos_inds = pos_inds.squeeze(1)
        if pos_inds.numel() <= num_expected:
            return pos_inds
        else:
            return self.random_choice(pos_inds, num_expected)

    def _sample_neg(self, assign_result, num_expected, **kwargs):
        """Randomly sample some negative samples."""
        neg_inds = torch.nonzero(assign_result.gt_inds == 0, as_tuple=False)
        if neg_inds.numel() != 0:
            neg_inds = neg_inds.squeeze(1)
        if len(neg_inds) <= num_expected:
            return neg_inds
        else:
            return self.random_choice(neg_inds, num_expected)

def args_adaptor(np_args):
    max_overlaps = torch.LongTensor([0, 0.5, 0.9, 0])
    assigned_gt_inds = torch.LongTensor([-1, 1, 2, 0])
    assigned_labels = torch.LongTensor([0, 3, 4, 0])
    assign_result = AssignResult(np_args[0],
                                 assigned_gt_inds,
                                 max_overlaps,
                                 labels=assigned_labels)

    boxes = torch.from_numpy(np_args[1]).cuda()
    gt_boxes = torch.from_numpy(np_args[2]).cuda()
    return [assign_result, boxes, gt_boxes]

def executer_creator():
    coder_instance = RandomSampler(num=256,
                                   pos_fraction=0.5,
                                   neg_pos_ub=-1,
                                   add_gt_as_proposals=False)
    return Executer(coder_instance.sample, args_adaptor)


from collections import Counter

class Solution:
    def missingElement(self, nums: List[int], k: int) -> int:
        '''
        Take advantage of the sorted nature and use binary search.

        This is quite tricky as it requires to derive a formula to find accumulated missing numbers
        from the sorted array and everything else revolves around it.

        as it stands time O(n) finding missing needs to be optimized for logN binary search to shine
        space O(1)
        '''

        missing = []

        #finds number of elements missing at an index
        #for input [4, 7, 9, 10] missing at
        #index 0 is 0, index 1 is 2, index 2 is 3, index 4 is 3. missing[i] = missing at index i + missing[i-1]

        for index, val in enumerate(nums): #can be optimized with built in functions
            missing.append(nums[index]-nums[0]-index)

        #if missing number is out of bounds
        #add max from nums with k and substract the difference from accumulated missing
        if k > missing[-1]:
            return nums[-1]+(k-missing[-1])

        #binary search
        left = 0
        right = len(nums)-1

        #our goal is to find the k'th missing
        while left < right:
            mid = (left+right)//2

            if missing[mid] < k:
                left = mid +1
            else:
                right = mid

        #missing number will be on left-1
        #calcualte the missing number using formula
        return nums[left-1] + k - missing[left-1]


from django.core.exceptions import ValidationError
from django.db import models
from safedelete.models import SafeDeleteModel
from absolutum.mixins import CoreMixin, DisplayMixin

class Invite(SafeDeleteModel, CoreMixin, DisplayMixin):
    """
        МЛМ-приглашение
    """
    INVITE_STATUS = (
        ('wait', 'Ожидание'),
        ('ok', 'Выплата'),
    )
    status = models.CharField(max_length=16, choices=INVITE_STATUS, default='ok', verbose_name='Статус')
    agent = models.ForeignKey('mlm.Agent', related_name='invites', on_delete=models.CASCADE, verbose_name='Агент')
    deal = models.ForeignKey('deal.Deal', related_name='invites', on_delete=models.CASCADE, verbose_name='Сделка')
    level = models.PositiveIntegerField(default=1, verbose_name='Уровень')
    percent = models.PositiveIntegerField(default=0, verbose_name='Процент')
    cost = models.DecimalField(default='0.00', blank=True, max_digits=30, decimal_places=2, verbose_name='Стоимость')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='Время создания')

    list_display = ['deal_id', 'deal', 'level', 'percent', 'status', 'cost']
    list_form_fields = ['agent', 'deal', 'level', 'percent', 'cost']

    filters_ordered = ['agent']
    filters_fields = dict(
        agent=dict(
            label='Агента', key='agent',
            widget=dict(attrs={}, name='Select', input_type="select", model_name='mlm.Agent')
        ),
    )
    router_name = 'mlm_invite'

    class Meta:
        verbose_name = 'МЛМ-приглашение'
        verbose_name_plural = 'МЛМ-приглашения'
        default_permissions = ()
        permissions = [
            ('view_invite', 'Просматривать МЛМ-приглашения'),
            ('send_invite', 'Отправлять МЛМ-приглашения'),
        ]

    def __str__(self):
        return '%s (%s)' % (self.deal.cache.get('title'), self.cost)

    def clean(self):
        if Invite.objects.filter(deal=self.deal, level=self.level, deleted=None).exclude(pk=self.id).exists():
            raise ValidationError({'__all__': 'Инвайт уже создан'})
        return super().clean()

    @staticmethod
    def set_invite(deal, agent):
        invite, _ = Invite.objects.get_or_create(agent=agent, deal=deal)
        invite.percent = agent.level_1
        invite.cost = deal.cost / 100 * invite.percent
        invite.save()
        agent.save()  # update agent.cache

        # level 2
        agent_2 = agent.parent
        if agent_2:
            print('agent_2', agent_2)
            invite_2, _ = Invite.objects.get_or_create(agent=agent_2, deal=deal, level=2)
            invite_2.percent = agent_2.level_2
            invite_2.cost = deal.cost / 100 * invite_2.percent
            invite_2.save()
            agent_2.save()  # update agent.cache

        # level 3
        agent_3 = agent_2.parent if agent_2 else None
        if agent_3:
            invite_3, _ = Invite.objects.get_or_create(agent=agent_3, deal=deal, level=3)
            invite_3.percent = agent_3.level_3
            invite_3.cost = deal.cost / 100 * invite_3.percent
            invite_3.save()
            agent_3.save()  # update agent.cache

        return invite

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)
        self.agent.save()


from ast import Return
from django.shortcuts import render,redirect
from todoapp.models import todolist
from todoapp.forms import TodoForm
from django.contrib import messages
from django.core.paginator import Paginator
from django.contrib.auth.decorators import login_required
# Create your views here.

def index(request):
    return render(request,"index.html")

def contact(request):
    return render(request,"contact.html")

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

@login_required
def todo_list(request):
    if request.method=="POST":
        form = TodoForm(request.POST)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.manage = request.user
            instance.save()
            messages.success(request,("Task Added"))
            return redirect ("todo_list")
    else:
        obj=todolist.objects.filter(manage=request.user)
        paginator=Paginator(obj,5)
        page=request.GET.get('pg')
        obj=paginator.get_page(page)
        return render(request,"todo_list.html",{'obj':obj})

@login_required
def delete_list(request,id):
    obj=todolist.objects.get(pk=id)
    if obj.manage == request.user:
        obj.delete()
    else:
        messages.error(request,("Action Restricted"))
    messages.success(request,("Task Deleted"))
    return redirect("todo_list")

@login_required
def update_list(request,id):
    if request.method=="POST":
        obj=todolist.objects.get(pk=id)
        form=TodoForm(request.POST,instance=obj)
        if form.is_valid():
            form.save()
            messages.success(request,("Task Updated."))
            return redirect ("todo_list")
    else:
        list_obj=todolist.objects.get(pk=id)
        return render(request,"update.html",{'list_obj':list_obj})

def complete_list(request,id):
    obj=todolist.objects.get(pk=id)
    if obj.manage == request.user:
        obj.done=True
        obj.save()
    else:
        messages.error(request,("Action Restricted"))
    return redirect("todo_list")

def pending_list(request,id):
    obj=todolist.objects.get(pk=id)
    if obj.manage == request.user:
        obj.done=False
        obj.save()
    else:
        messages.error(request,("Action Restricted"))
    return redirect("todo_list")


import os

VIDEO_EXTENSIONS = ['.mov', '.wmv', '.mp4', '.m4v', '.ogg', '.anx', '.mpg', '.avi', '.flv', '.mpeg', '.divx', '.xvid', '.rmvb', '.mkv', '.m2v', '.ogm']
AUDIO_EXTENSIONS = ['.mp3', '.m4a', '.wma', '.mka']

MIMETYPES_EXT_MAP = {
    'video/quicktime':  ['.mov'],
    'video/mpeg':       ['.mpeg', '.mpg', '.m2v'],
    'video/mp4':        ['.mp4', '.m4v'],
    'video/flv':        ['.flv'],
    'video/x-flv':      ['.flv'],
    'video/x-ms-wmv':   ['.wmv'],
    'video/x-msvideo':  ['.avi'],
    'video/x-matroska': ['.mkv'],
    'application/ogg':  ['.ogg'],

    'audio/mpeg':       ['.mp3'],
    'audio/mp4':        ['.m4a'],
    'audio/x-ms-wma':   ['.wma'],
    'audio/x-matroska': ['.mka'],

    'application/x-bittorrent': ['.torrent']
}

EXT_MIMETYPES_MAP = {}
for (mimetype, exts) in MIMETYPES_EXT_MAP.iteritems():
    for ext in exts:
        if ext not in EXT_MIMETYPES_MAP:
            EXT_MIMETYPES_MAP[ext] = list()
        EXT_MIMETYPES_MAP[ext].append(mimetype)

def isAllowedFilename(filename):
    """
    Pass a filename to this method and it will return a boolean
    saying if the filename represents video, audio or torrent.
    """
    return isVideoFilename(filename) or isAudioFilename(filename) or isTorrentFilename(filename)

def isVideoFilename(filename):
    """
    Pass a filename to this method and it will return a boolean
    saying if the filename represents a video file.
    """
    filename = filename.lower()
    for ext in VIDEO_EXTENSIONS:
        if filename.endswith(ext):
            return True
    return False

def isAudioFilename(filename):
    """
    Pass a filename to this method and it will return a boolean
    saying if the filename represents an audio file.
    """
    filename = filename.lower()
    for ext in AUDIO_EXTENSIONS:
        if filename.endswith(ext):
            return True
    return False

def isTorrentFilename(filename):
    """
    Pass a filename to this method and it will return a boolean
    saying if the filename represents a torrent file.
    """
    filename = filename.lower()
    return filename.endswith('.torrent')

def isVideoEnclosure(enclosure):
    """
    Pass an enclosure dictionary to this method and it will return a boolean
    saying if the enclosure is a video or not.
    """
    return (_hasVideoType(enclosure) or
            _hasVideoExtension(enclosure, 'url') or
            _hasVideoExtension(enclosure, 'href'))

def _hasVideoType(enclosure):
    return ('type' in enclosure and
            (enclosure['type'].startswith(u'video/') or
             enclosure['type'].startswith(u'audio/') or
             enclosure['type'] == u"application/ogg" or
             enclosure['type'] == u"application/x-annodex" or
             enclosure['type'] == u"application/x-bittorrent" or
             enclosure['type'] == u"application/x-shockwave-flash"))

def _hasVideoExtension(enclosure, key):
    from miro import download_utils
    if key in enclosure:
        elems = download_utils.parseURL(enclosure[key], split_path=True)
        return isAllowedFilename(elems[3])
    return False

def isFeedContentType(contentType):
    """Is a content-type for a RSS feed?"""

    feedTypes = [ u'application/rdf+xml', u'application/atom+xml',
            u'application/rss+xml', u'application/podcast+xml', u'text/xml',
            u'application/xml',
        ]
    for type in feedTypes:
        if contentType.startswith(type):
            return True
    return False

def guessExtension(mimetype):
    """
    Pass a mime type to this method and it will return a corresponding file
    extension, or None if it doesn't know about the type.
    """
    possibleExtensions = MIMETYPES_EXT_MAP.get(mimetype)
    if possibleExtensions is None:
        return None
    return possibleExtensions[0]

def guessMimeType(filename):
    """
    Pass a filename to this method and it will return a corresponding mime type,
    or 'video/unknown' if the filename has a known video extension but no
    corresponding mime type, or None if it doesn't know about the file extension.
    """
    root, ext = os.path.splitext(filename)
    possibleTypes = EXT_MIMETYPES_MAP.get(ext)
    if possibleTypes is None:
        if isVideoFilename(filename):
            return 'video/unknown'
        elif isAudioFilename(filename):
            return 'audio/unknown'
        else:
            return None
    return possibleTypes[0]


import unittest
import requests
from unittest.mock import patch, Mock
from app.api import get_place_id, get_place_phone_number
from json import loads

class ApiTest(unittest.TestCase):
    API_URL = 'http://localhost:5000/getphonenumber?address='
    addresses = []

    def test_get_place_id(self):
        mock_get_patcher = patch('app.api.requests.get')
        place_details = '{\n   "candidates" : [\n      {\n         "formatted_address" : "6101 W Century Blvd, Los Angeles, CA 90045, United States",\n         "name" : "Sheraton Gateway Los Angeles Hotel",\n         "place_id" : "ChIJWQrR4tS2woARHV9LcAjcw48"\n      }\n   ],\n   "status" : "OK"\n}\n'
        mock_get = mock_get_patcher.start()
        mock_get.return_value.status_code = 200
        mock_get.return_value.text = place_details
        place = "Sheraton Gateway Los Angeles Hotel"
        response = get_place_id(place)
        mock_get_patcher.stop()
        try:
            self.assertEqual(response,'ChIJWQrR4tS2woARHV9LcAjcw48', "Failed! Respose does not match the actual outcome.")
        except AssertionError as error:
            print(error)

    def test_get_place_phone_number(self):
        mock_get_patcher = patch('app.api.requests.get')
        place_details = '{\n   "html_attributions" : [],\n   "result" : {\n      "formatted_phone_number" : "(310) 642-1111",\n      "name" : "Sheraton Gateway Los Angeles Hotel"\n   },\n   "status" : "OK"\n}\n'
        mock_get = mock_get_patcher.start()
        mock_get.return_value.status_code = 200
        mock_get.return_value.text = place_details
        place_id = 'ChIJWQrR4tS2woARHV9LcAjcw48'
        response = get_place_phone_number(place_id)
        mock_get_patcher.stop()
        try:
            self.assertEqual(response,'(310) 642-1111', "Failed! Respose does not match the actual outcome.")
        except AssertionError as error:
            print(error)

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


# Import system modules
import sys, zipfile, arcpy, os, traceback, datetime, time
from zipfile import ZipFile

#receive parameter from tool
infile = arcpy.GetParameterAsText(0)

#create timestamped folder to receive files from zip archive
fname = os.path.basename(infile).split('.')[0]
parentdir = r"C:\geodata\project_data\nrcs\ArcGIS_Pro\Export_Files"
renametime = time.ctime()
renameend = renametime.replace(" ","")
renamenocolon = renameend.replace(":","")
outdir = os.path.join(parentdir,"unzip" + fname + renamenocolon)

#unzip to created folder
with ZipFile(infile, 'r') as zipObj:
   zipObj.extractall(outdir)
   arcpy.AddMessage("Files extracted to: " + outdir)

# set environment settings
arcpy.env.workspace = outdir

# list of shapefiles in the output folder
fcList = arcpy.ListFeatureClasses()

# execute append for each input shapefile
for shapefile in fcList:
    desc = arcpy.da.Describe(shapefile)
    fields = arcpy.ListFields(shapefile)

    # for point geometry - confirm a CD field name and append to Local_Temp\Point
    if desc['shapeType'] == "Point":
        for field in fields:
            if field.name == 'pract_code':
                Point = "C:\\geodata\\project_data\\nrcs\\ArcGIS_Pro\\geodatabases\\Local_Temp.gdb\\Temporary\\Point"
                practicePoints_shp = shapefile
                arcpy.Append_management(practicePoints_shp, Point, "NO_TEST")
            else:
                arcpy.AddMessage("The input zip file <" + infile + "> does not contain a Conservation Desktop export.  Process terminated.")

    # for line geometry - confirm a CD field name and append to Local_Temp\Line
    elif desc['shapeType'] == "Polyline":
        for field in fields:
            if field.name == 'pract_code':
                Line = "C:\\geodata\\project_data\\nrcs\\ArcGIS_Pro\\geodatabases\\Local_Temp.gdb\\Temporary\\Line"
                practiceLines_shp = shapefile
                arcpy.Append_management(practiceLines_shp, Line, "NO_TEST")
            else:
                arcpy.AddMessage("The input zip file <" + infile + "> does not contain a Conservation Desktop export.  Process terminated.")

    # for polygon geometry
    elif desc['shapeType'] == "Polygon":

            for field in fields:
                # confirm a CD field name and append to Local_Temp\LandUnits
                if field.name == 'geom_stat':
                    LandUnit = "C:\\geodata\\project_data\\nrcs\\ArcGIS_Pro\\geodatabases\\Local_Temp.gdb\\Temporary\\LandUnits"
                    landUnits_shp = shapefile
                    arcpy.Append_management(landUnits_shp, LandUnit, "NO_TEST")

                # confirm a CD field name and append to Local_Temp\Polygon
                elif field.name == 'pract_code':
                    Polygon = "C:\\geodata\\project_data\\nrcs\\ArcGIS_Pro\\geodatabases\\Local_Temp.gdb\\Temporary\\Polygon"
                    practicePolygons_shp = shapefile
                    arcpy.Append_management(practicePolygons_shp, Polygon, "NO_TEST")

                else:
                    arcpy.AddMessage("The input zip file <" + infile + "> does not contain a Conservation Desktop export.  Process terminated.")

# cleanup
arcpy.Delete_management(outdir)

exit()


#!/usr/bin/env python

"""
Useful methods of class CleanCorpus:

def getRawDocuments(self, topic, maxCount = 50, combined = False)
def getSupportedTopics(self)
"""

import os
from clean_console import CleanConsole

class CleanCorpus:

  """
  ivars:

  cc = CleanConsole()
  """

  #############################
  # Public Methods
  #############################

  def __init__(self):
    self.cc = CleanConsole()
    return

  """
  @param
    topic should be supported (see getSupportedTopics)
  @return
    1) no error
    [
      {
        'sentiment' : string, # 'pos', 'neg', or 'any'
        'document' : string, # document content
      },
      ...
    ]
    2) combined
    string
    3) error (can't find directory, etc)
    False
  @notes
    maxCount applies to each sentiment ('pos', 'neg', 'any'), not these combined

  """
  def getRawDocuments(self, topic, maxCount = 50, combined = False):
    if topic not in self.getSupportedTopics():
      # unsupported topic
      return False

    pathToDir = os.path.join(
      os.path.dirname(os.path.abspath(__file__)),
      'clean_data',
      topic
    )

    if not self.cc.isDirPath(pathToDir):
      # bad
      return False

    subdirs = ['pos', 'neg', 'any']
    data = [] # each entry is { 'subdir' : string, 'docs' : [] }
    for subdir in subdirs:
      data.append({
        'subdir' : subdir,
        'docs' : [],
      })

    for entry in data:
      entry['docs'] = self.cc.getFilesFromDirectory(
        os.path.join(pathToDir, entry['subdir'])
      )
      if len(entry['docs']) > maxCount:
        entry['docs'] = entry['docs'][:maxCount]

    ret = []
    for entry in data:
      for doc in entry['docs']:
        ret.append({
          'sentiment' : entry['subdir'],
          'document' : doc,
        })

    if not combined:
      return ret
    else:
      allDocs = [elem['document'] for elem in ret]
      return '\n\n\n\n\n'.join(allDocs)

  """
  @return list of topics that are supported
  Each supported topic should match the name of the directory within engine/clean_data
  """
  def getSupportedTopics(self):
    return ['movie', 'celebrity', 'syria', 'ufo']

  #############################
  # Helpers
  #############################


# coding: utf-8
"""
Synthesis waveform for testset

usage: separation.py [options] <checkpoint0> <checkpoint1>

options:
    --hparams=<parmas>          Hyper parameters [default: ].
    --preset=<json>             Path of preset parameters (json).
    -h, --help                  Show help message.
"""
from docopt import docopt
from os.path import dirname, join, basename, splitext, exists

import numpy as np
import soundfile as sf
import torch
import torch.nn.functional as F

import audio
from hparams import hparams
from train import build_model
from nnmnkwii import preprocessing as P
from wavenet_vocoder.util import linear_quantize, inv_linear_quantize

SAMPLE_SIZE = 32000

use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")

checkpoints = {
               175.9         : 'checkpoints175pt9',
               110.          : 'checkpoints110pt0',
               68.7          : 'checkpoints68pt7',
               42.9          : 'checkpoints42pt9',
               26.8          : 'checkpoints26pt8',
               16.8          : 'checkpoints16pt8',
               10.5          : 'checkpoints10pt5',
               4.1           : 'checkpoints4pt1',
               2.56          : 'checkpoints2pt56',
               1.6           : 'checkpoints1pt6',
               1.0           : 'checkpoints1pt0',
               0.0           : 'checkpoints0pt0'
}

def main(args):
    model0 = build_model().to(device)
    model0.eval()

    model1 = build_model().to(device)
    model1.eval()
    receptive_field = model0.receptive_field

    x0_original = np.load("supra_piano/dump/dev/zf882fv0052-wave.npy")
    x0_original = x0_original[200000:200000 + SAMPLE_SIZE]

    # x0_original = np.load("vctk/dump/dev/p374_422-wave.npy")
    # x0_original = x0_original[20000:20000 + SAMPLE_SIZE]

    x1_original = np.load("vctk/dump/dev/p341_048-wave.npy")
    x1_original = x1_original[32000:32000 + SAMPLE_SIZE]

    mixed  = torch.FloatTensor(x0_original + x1_original).reshape(1, -1).to(device)

    # Write inputs
    mixed_out = inv_linear_quantize(mixed[0].detach().cpu().numpy(), hparams.quantize_channels - 1) - 1.0
    mixed_out = np.clip(mixed_out, -1, 1)
    sf.write("mixed.wav", mixed_out, hparams.sample_rate)

    x0_original_out = inv_linear_quantize(x0_original, hparams.quantize_channels - 1)
    sf.write("x0_original.wav", x0_original_out, hparams.sample_rate)

    x1_original_out = inv_linear_quantize(x1_original, hparams.quantize_channels - 1)
    sf.write("x1_original.wav", x1_original_out, hparams.sample_rate)

    # Initialize with noise
    x0 = torch.FloatTensor(np.random.uniform(-256, 512, size=(1, SAMPLE_SIZE))).to(device)
    x0 = F.pad(x0, (receptive_field, 0), "constant", 127)
    x0.requires_grad = True

    x1 = torch.FloatTensor(np.random.uniform(-256, 512, size=(1, SAMPLE_SIZE))).to(device)
    x1 = F.pad(x1, (receptive_field, 0), "constant", 127)
    x1.requires_grad = True

    # Initialize with noised GT
    x0[0, receptive_field:] = torch.FloatTensor(x0_original + np.random.normal(0, 256., x0_original.shape)).to(device)
    x1[0, receptive_field:] = torch.FloatTensor(x1_original + np.random.normal(0, 256., x1_original.shape)).to(device)

    sigmas = [175.9, 110., 68.7, 42.9, 26.8, 16.8, 10.5, 4.1, 2.56, 1.6, 1.0, 0.0]
    n_steps = 10000
    start_sigma = 256.
    end_sigma = 0.1

    # Exponential annealing
    ratio = (end_sigma / start_sigma) ** (1.0 / n_steps)
    sigma = start_sigma

    # Dummy start values
    curr_model_idx = -1
    curr_model_sigma = 1000000.

    for i in range(n_steps):
        # Bump down a model
        if sigma < curr_model_sigma:
            curr_model_idx += 1
            curr_model_sigma = sigmas[curr_model_idx]

            checkpoint_path0 = join(args["<checkpoint0>"], checkpoints[curr_model_sigma], "checkpoint_latest.pth")
            checkpoint_path1 = join(args["<checkpoint1>"], checkpoints[curr_model_sigma], "checkpoint_latest.pth")
            print("Load checkpoint0 from {}".format(checkpoint_path0))
            checkpoint0 = torch.load(checkpoint_path0)
            checkpoint1 = torch.load(checkpoint_path1)
            model0.load_state_dict(checkpoint0["state_dict"])
            model1.load_state_dict(checkpoint1["state_dict"])

        eta = .05 * (sigma ** 2)
        gamma = 15 * (1.0 / sigma) ** 2

        # Uncomment to see GT log likelihoods per sigma
        # x0[0, receptive_field:] = torch.FloatTensor(x0_original + np.random.normal(0, sigma, x0_original.shape)).to(device)
        # x1[0, receptive_field:] = torch.FloatTensor(x1_original + np.random.normal(0, sigma, x1_original.shape)).to(device)

        # Forward pass
        model0.zero_grad()
        log_prob, prediction0 = model0.smoothed_loss(x0, sigma=sigma)
        log_prob0 = torch.sum(log_prob[:, (receptive_field - 1):])
        # log_prob0 = torch.sum(log_prob)
        grad0 = torch.autograd.grad(log_prob0, x0)[0]

        x0_update = eta * grad0[:, receptive_field:]
        # x0_update = eta * grad0

        model1.zero_grad()
        log_prob, prediction1 = model1.smoothed_loss(x1, sigma=sigma)
        log_prob1 = torch.sum(log_prob[:, (receptive_field - 1):])
        # log_prob1 = torch.sum(log_prob)
        grad1 = torch.autograd.grad(log_prob1, x1)[0]

        x1_update = eta * grad1[:, receptive_field:]
        # x1_update = eta * grad1

        # Langevin step
        epsilon0 = np.sqrt(2 * eta) * torch.normal(0, 1, size=(1, SAMPLE_SIZE), device=device)
        x0_update += epsilon0

        epsilon1 = np.sqrt(2 * eta) * torch.normal(0, 1, size=(1, SAMPLE_SIZE), device=device)
        x1_update += epsilon1

        # Reconstruction step
        # x0_update -= eta * gamma * (x0[:, receptive_field:] + x1[:, receptive_field:] - mixed)
        # x1_update -= eta * gamma * (x0[:, receptive_field:] + x1[:, receptive_field:] - mixed)
        # x0_update -= eta * gamma * (x0 + x1 - mixed)
        # x1_update -= eta * gamma * (x0 + x1 - mixed)

        with torch.no_grad():
            x0[:, receptive_field:] += x0_update
            x1[:, receptive_field:] += x1_update

            # x0 += x0_update
            # x1 += x1_update

        if not i % 50: # debugging
            print("--------------")
            print('sigma = {}'.format(sigma))
            print('eta = {}'.format(eta))
            print("i {}".format(i))
            print("Max sample {}".format(
                abs(x0).max()))
            print('Mean sample logpx: {}'.format(log_prob0 / SAMPLE_SIZE))
            print('Mean sample logpy: {}'.format(log_prob1 / SAMPLE_SIZE))
            print("Max gradient update: {}".format(eta * abs(grad0).max()))
            # print("Reconstruction: {}".format(abs(x0 + x1 - mixed).mean()))

        # Reduce sigma
        sigma *= ratio

    # out0 = P.inv_mulaw_quantize(x0[0].detach().cpu().numpy(), hparams.quantize_channels - 1)
    out0 = inv_linear_quantize(x0[0].detach().cpu().numpy(), hparams.quantize_channels - 1)
    out0 = np.clip(out0, -1, 1)
    sf.write("out0.wav", out0, hparams.sample_rate)

    out1 = inv_linear_quantize(x1[0].detach().cpu().numpy(), hparams.quantize_channels - 1)
    out1 = np.clip(out1, -1, 1)
    sf.write("out1.wav", out1, hparams.sample_rate)

    import pdb
    pdb.set_trace()

if __name__ == "__main__":
    args = docopt(__doc__)

    # Load preset if specified
    if args["--preset"] is not None:
        with open(args["--preset"]) as f:
            hparams.parse_json(f.read())
    else:
        hparams_json = join(dirname(args["<checkpoint1>"]), "hparams.json")
        if exists(hparams_json):
            print("Loading hparams from {}".format(hparams_json))
            with open(hparams_json) as f:
                hparams.parse_json(f.read())

    # Override hyper parameters
    hparams.parse(args["--hparams"])
    assert hparams.name == "wavenet_vocoder"

    main(args)


"""
Abstractions and utilities for working with projects to run ecosystem checks on.
"""

from __future__ import annotations

from asyncio import create_subprocess_exec
from dataclasses import dataclass, field
from enum import Enum
from pathlib import Path
from subprocess import PIPE
from typing import Self

from ruff_ecosystem import logger
from ruff_ecosystem.check import CheckOptions
from ruff_ecosystem.format import FormatOptions
from ruff_ecosystem.types import Serializable

@dataclass(frozen=True)
class Project(Serializable):
    """
    An ecosystem target
    """

    repo: Repository
    check_options: CheckOptions = field(default_factory=lambda: CheckOptions())
    format_options: FormatOptions = field(default_factory=lambda: FormatOptions())

class RuffCommand(Enum):
    check = "check"
    format = "format"

class ProjectSetupError(Exception):
    """An error setting up a project."""

@dataclass(frozen=True)
class Repository(Serializable):
    """
    A remote GitHub repository.
    """

    owner: str
    name: str
    ref: str | None

    @property
    def fullname(self) -> str:
        return f"{self.owner}/{self.name}"

    @property
    def url(self: Self) -> str:
        return f"https://github.com/{self.owner}/{self.name}"

    async def clone(self: Self, checkout_dir: Path) -> ClonedRepository:
        """
        Shallow clone this repository
        """
        if checkout_dir.exists():
            logger.debug(f"Reusing {self.owner}:{self.name}")

            if self.ref:
                logger.debug(f"Checking out ref {self.ref}")
                process = await create_subprocess_exec(
                    *["git", "checkout", "-f", self.ref],
                    cwd=checkout_dir,
                    env={"GIT_TERMINAL_PROMPT": "0"},
                    stdout=PIPE,
                    stderr=PIPE,
                )
                if await process.wait() != 0:
                    _, stderr = await process.communicate()
                    raise ProjectSetupError(
                        f"Failed to checkout {self.ref}: {stderr.decode()}"
                    )

            return await ClonedRepository.from_path(checkout_dir, self)

        logger.debug(f"Cloning {self.owner}:{self.name} to {checkout_dir}")
        command = [
            "git",
            "clone",
            "--config",
            "advice.detachedHead=false",
            "--quiet",
            "--depth",
            "1",
            "--no-tags",
        ]
        if self.ref:
            command.extend(["--branch", self.ref])

        command.extend(
            [
                f"https://github.com/{self.owner}/{self.name}",
                str(checkout_dir),
            ],
        )

        process = await create_subprocess_exec(
            *command, env={"GIT_TERMINAL_PROMPT": "0"}
        )

        status_code = await process.wait()

        logger.debug(
            f"Finished cloning {self.fullname} with status {status_code}",
        )
        return await ClonedRepository.from_path(checkout_dir, self)

@dataclass(frozen=True)
class ClonedRepository(Repository, Serializable):
    """
    A cloned GitHub repository, which includes the hash of the current commit.
    """

    commit_hash: str
    path: Path

    def url_for(
        self: Self,
        path: str,
        line_number: int | None = None,
        end_line_number: int | None = None,
    ) -> str:
        """
        Return the remote GitHub URL for the given path in this repository.
        """
        url = f"https://github.com/{self.owner}/{self.name}/blob/{self.commit_hash}/{path}"
        if line_number:
            url += f"#L{line_number}"
        if end_line_number:
            url += f"-L{end_line_number}"
        return url

    @property
    def url(self: Self) -> str:
        return f"https://github.com/{self.owner}/{self.name}@{self.commit_hash}"

    @classmethod
    async def from_path(cls, path: Path, repo: Repository):
        return cls(
            name=repo.name,
            owner=repo.owner,
            ref=repo.ref,
            path=path,
            commit_hash=await cls._get_head_commit(path),
        )

    @staticmethod
    async def _get_head_commit(checkout_dir: Path) -> str:
        """
        Return the commit sha for the repository in the checkout directory.
        """
        process = await create_subprocess_exec(
            *["git", "rev-parse", "HEAD"],
            cwd=checkout_dir,
            stdout=PIPE,
        )
        stdout, _ = await process.communicate()
        if await process.wait() != 0:
            raise ProjectSetupError(f"Failed to retrieve commit sha at {checkout_dir}")

        return stdout.decode().strip()


from tkinter import *
from tkinter import ttk as tk
from datetime import date
import tkinter.messagebox as tmsg
import tkcalendar
import cx_Oracle
import ctypes

class Attendence:
    def __init__(self, root):
        self.root = root
        self.root.geometry('1335x500+15+110')
        self.root.resizable(False, False)
        self.root.title('Attendence Details')
        self.root.config(bg = "Cyan")
        myappid = 'mycompany.myproduct.subproduct.version'  # arbitrary string
        ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
        self.root.iconbitmap(default='images/icon2.ico')
        self.frame1 = Frame(self.root, bd = 5, relief = SUNKEN)
        self.frame1.place(x = 5, y = 10, width = 1325, height = 50)
        Label(self.frame1, text = "Add Attendance Details Here", font = "Helvatica 20 bold").pack(padx = 5)
        self.frame2 = Frame(self.root, bd = 5, relief = SUNKEN)
        self.frame2.place(x = 5, y = 70, width = 400, height = 360)
        self.CID = StringVar()
        self.SID = StringVar()
        self.Date = StringVar()
        today = date.today()
        today = today.strftime("%m/%d/%y")
        self.Date.set(today)
        self.Status = StringVar()
        self.Status.set("Present")
        Label(self.frame2, text = "Attendance Details", font = "Helvatica 20 bold underline").place(x = 70, y = 10)
        Label(self.frame2, text = "Course ID", font = "Courier 16 bold").place(x = 5, y = 90)
        connection = self.connection()
        cursor = connection.cursor()
        query = 'SELECT CID FROM COURSES ORDER BY CID ASC'
        cursor.execute(query)
        row = cursor.fetchall()
        cidE = tk.Combobox(self.frame2, textvariable = self.CID, font = "Courier 12 bold")
        cidE['values'] = row
        cidE.place(x = 190, y = 90, width = 190)
        Label(self.frame2, text = "Student ID", font = "Courier 16 bold").place(x = 5, y = 140)
        cursor1 = connection.cursor()
        query1 = "SELECT SID FROM STUDENTS ORDER BY SID ASC"
        cursor1.execute(query1)
        row1 = cursor1.fetchall()
        sidE = tk.Combobox(self.frame2, textvariable = self.SID, font = "Courier 12 bold")
        sidE['values'] = row1
        sidE.place(x = 190, y = 140, width = 190)
        Label(self.frame2, text = "Date", font = "Courier 16 bold").place(x = 5, y = 190)
        Entry(self.frame2, textvariable = self.Date, font = "Courier 13 bold").place(x = 190, y = 190, width = 190)
        Button(self.frame2, text="Cal", font="Conicsansms 10 bold", fg="Black", command=self.getDate).place(x=345, y=192,height=20)
        Label(self.frame2, text = "Status", font = "Courier 16 bold").place(x = 5, y = 240)
        self.StatusE = tk.Combobox(self.frame2, textvariable = self.Status, font = "Courier 12 bold")
        self.StatusE['values'] = ["Present", "Absent"]
        self.CheckStatus()
        self.StatusE.place(x = 190, y = 240, width = 190)
        self.frame3 = Frame(self.root, bd = 5, relief = SUNKEN)
        Button(self.frame3, text="Add", font="Helvatica 14 bold", bd=2, relief=GROOVE, bg="#b3b3b3", cursor="hand2", command = self.InsertData).place(x=5, y=7, height=30)
        Button(self.frame3, text="Update", font="Helvatica 14 bold", bd=2, relief=GROOVE, bg="#b3b3b3", cursor="hand2", command = self.UpdateData).place(x=65, y=7, height=30)
        Button(self.frame3, text="Delete", font="Helvatica 14 bold", bd=2, relief=GROOVE, bg="#b3b3b3", cursor="hand2", command = self.DeleteData).place(x=150, y=7, height=30)
        Button(self.frame3, text="Clear", font="Helvatica 16 bold", bd=2, relief=GROOVE, bg="#b3b3b3", cursor="hand2", command = self.Clear).place(x=230, y=7, height=30, width=80)
        Button(self.frame3, text="Exit", font="Helvatica 16 bold", bd=2, relief=GROOVE, bg="#b3b3b3", cursor="hand2", command=self.root.destroy).place(x=315, y=7, height=30, width=70)
        self.frame3.place(x = 5, y = 440, width = 400, height = 50)
        self.frame4 = Frame(self.root, bd = 5, relief = SUNKEN)
        self.frame4.place(x = 410, y = 70, width = 920, height = 420)
        Label(self.frame4, text = "Search By", font = "Courier 16 bold").place(x = 5, y = 10)
        self.SearchBy = StringVar()
        self.Search = StringVar()
        searchbyE = tk.Combobox(self.frame4, textvariable = self.SearchBy, font = "Courier 11 bold")
        searchbyE['values'] = ['Course ID', 'Student ID', 'Date']
        searchbyE.place(x = 150, y = 10)
        self.SearchE = Entry(self.frame4, textvariable = self.Search, font = "Courier 11 bold")
        self.ValidateSearchBar()
        self.SearchE.place(x = 370, y = 10)
        Button(self.frame4, text = "Search", font = "Helvatica 14 bold", bd = 2, relief = GROOVE, bg="#b3b3b3", cursor="hand2", command = self.SearchData).place(x = 590, y = 10, height = 25, width = 150)
        Button(self.frame4, text = "Show All", font = "Helvatica 14 bold", bd = 2, relief = GROOVE, bg="#b3b3b3", cursor="hand2", command = self.FetchData).place(x = 750, y = 10, height = 25, width = 150)
        scrollX = Scrollbar(self.frame4, orient=HORIZONTAL)
        scrollX.place(x=5, y=385, width=880)
        scrollY = Scrollbar(self.frame4, orient=VERTICAL)
        scrollY.place(x=890, y=50, height=330)
        self.Tree = tk.Treeview(self.frame4, xscrollcommand=scrollX.set, yscrollcommand=scrollY.set, column=('SID', 'CID', 'Date', 'Status'))
        self.Tree.heading('SID', text='Student Id')
        self.Tree.heading('CID', text='Course Id')
        self.Tree.heading('Date', text='Date')
        self.Tree.heading('Status', text='Status')
        self.Tree.column('SID', width=80)
        self.Tree.column('CID', width=80)
        self.Tree.column('Date', width=80)
        self.Tree.column('Status', width=80)
        self.Tree['show'] = ['headings']
        self.Tree.place(x=5, y=50, width=880, height=330)
        scrollX.config(command=self.Tree.xview)
        scrollY.config(command=self.Tree.yview)
        self.Tree.bind('<Double-Button-1>', self.getCursor)
        self.FetchData()

    def ExtractEntry(self, event):
        self.Date.set(self.cal.get_date())
        self.cal.destroy()

    def getDate(self):
        try:
            self.cal.destroy()
        except:
            pass
        today = date.today()
        year = int(today.strftime('%Y'))
        month = int(today.strftime('%m'))
        day = int(today.strftime('%d'))
        self.cal = tkcalendar.Calendar(self.frame2, selectmode='day', year=year, month=month, day=day)
        self.cal.place(x=130, y=150)
        self.root.bind("<Double-Button-1>", self.ExtractEntry)

    def CheckStatus(self):
        if self.StatusE.get() == "Present" or self.StatusE.get() == 'Absent':
            # self.StatusE.config(state = "normal")
            pass
        else:
            # self.StatusE.config(state = "disable")
            tmsg.showerror("Status Error", "Student Must Be Present Or Absent")
            self.Status.set("")
        self.StatusE.after(5000, self.CheckStatus)

    def Clear(self):
        self.CID.set("")
        self.SID.set("")
        today = date.today()
        today = today.strftime("%m/%d/%y")
        self.Date.set(today)
        self.Status.set("Present")
        self.Search.set("")
        self.SearchBy.set("")

    def connection(self):
        connection = cx_Oracle.connect("system/69-Gilani-53@localhost:1521/orcl")
        return connection

    def InsertData(self):
        try:
            connection = self.connection()
            cursor = connection.cursor()
            sid = self.SID.get()
            cid = self.CID.get()
            adate = self.Date.get()
            status = self.Status.get()
            query = f"INSERT INTO ATTENDENCE (SID, CID, ADATE, STATUS) VALUES ('{sid}', '{cid}', TO_DATE('{adate}', 'MM/DD/YY'), '{status}')"
            cursor.execute(query)
            connection.commit()
            cursor.close()
            tmsg.showinfo("Good Job", f"Attendence Of Student # {self.SID.get()} In Course # {self.CID.get()} Is Inserted Successfully")
            self.FetchData()
            self.Clear()
        except Exception as e:
            tmsg.showerror("Insertion Error", f"Check SID, CID Or Error Due To {e}")

    def DeleteData(self):
        a = tmsg.askyesnocancel("Attention", "Do You Want To Delete The Attendence Of Student Permenantly?")
        if a == True:
            try:
                connection = self.connection()
                cursor = connection.cursor()
                query = f"DELETE FROM ATTENDENCE WHERE SID = '{self.SID.get()}' AND CID = '{self.CID.get()}'"
                cursor.execute(query)
                connection.commit()
                cursor.close()
                tmsg.showinfo("Good Job", f"Attendence Of Student # {self.SID.get()} In Course # {self.CID.get()} Is Deleted Successfully")
                self.FetchData()
                self.Clear()
            except Exception as e:
                tmsg.showerror("Deletion Error", f"Check SID, CID Or Error Due To {e}")
        else:
            pass

    def getCursor(self, event):
        cursor = self.Tree.focus()
        item = self.Tree.item(cursor)
        row = item['values']
        self.SID.set(row[0])
        self.CID.set(row[1])
        self.Date.set(row[2])
        self.Status.set(row[3])

    def FetchData(self):
        connection = self.connection()
        cursor = connection.cursor()
        query = "SELECT SID, CID, TO_CHAR(ADATE, 'MM/DD/YY'), STATUS FROM ATTENDENCE ORDER BY SID ASC"
        cursor.execute(query)
        rows = cursor.fetchall()
        if len(rows) != 0:
            self.Tree.delete(*self.Tree.get_children())
            for row in rows:
                self.Tree.insert("", END, values=row)
            connection.commit()
        cursor.close()

    def UpdateData(self):
        try:
            connection = self.connection()
            cursor = connection.cursor()
            query = f"UPDATE ATTENDENCE SET ADATE = TO_DATE('{self.Date.get()}', 'MM/DD/YY'), STATUS = '{self.Status.get()}' WHERE SID = '{self.SID.get()}' AND CID = '{self.CID.get()}'"
            cursor.execute(query)
            connection.commit()
            cursor.close()
            tmsg.showinfo("Good Job", f"Attendence Of Student # {self.SID.get()} In Course # {self.CID.get()} Is Updated Successfully")
            self.FetchData()
            self.Clear()
        except Exception as e:
            tmsg.showerror("Updation Error", f"Check SID, CID Or Error Due To {e}")

    def SearchData(self):
        try:
            connection = self.connection()
            cursor = connection.cursor()
            if self.SearchBy.get() == "Course ID":
                query = f"SELECT SID, CID, TO_CHAR(ADATE, 'MM/DD/YY'), STATUS FROM ATTENDENCE WHERE CID = '{self.Search.get()}'"
            elif self.SearchBy.get() == "Student ID":
                query = f"SELECT SID, CID, TO_CHAR(ADATE, 'MM/DD/YY'), STATUS FROM ATTENDENCE WHERE SID = '{self.Search.get()}'"
            elif self.SearchBy.get() == 'Date':
                query = f"SELECT SID, CID, TO_CHAR(ADATE, 'MM/DD/YY'), STATUS FROM ATTENDENCE WHERE ADATE = TO_DATE('{self.Search.get()}', 'MM/DD/YY')"
            cursor.execute(query)
            rows = cursor.fetchall()
            if len(rows) != 0:
                self.Tree.delete(*self.Tree.get_children())
                for row in rows:
                    self.Tree.insert("", END, values=row)
            connection.commit()
            cursor.close()
        except Exception as e:
            tmsg.showerror("Searching Error", f"Error Due To {e}")

    def ValidateSearchBar(self):
        if self.SearchBy.get() == 'Student ID' or self.SearchBy.get() == "Course ID" or self.SearchBy.get() == "Date":
            self.SearchE.config(state = "normal")
        else:
            self.SearchE.config(state = "disabled")
        self.SearchE.after(500, self.ValidateSearchBar)

if __name__ == '__main__':
    root = Tk()
    attendace = Attendence(root)
    root.mainloop()


# # TUTORIAT 6
# # exemple
# dict1 = {"b": 2, "c": 3, "d": 4}
# dict1.update({"f": 4, "d": 5})
# print(dict1)
# dict1.pop("D")
# val = dict1.pop("D", 0)
# print(val)
# del dict1["d"]
# print(dict1)
# for cheie in dict1.keys():
#     print(cheie)
# print(dict1.values(), type(dict1.values()))
# print(dict1.items())
#
# my_set = set("tutoriat")
# print(my_set)

# #ex1
# n = int(input("n="))
# lista1 = list()
# lista2 = []
# for i in range(n):
#     lista1.append(input("elem1= "))
# for i in range(n):
#     lista2.append(input("elem2= "))
# res_dict = dict()
# # metoda 1
# for i in range(n):
#     res_dict[lista1[i]] = lista2[i]
# print(res_dict)

# # metoda 2
# for i in range(n):
#     res_dict.update({lista1[i]: lista2[i]})
# print(res_dict)

# # metoda 3
# print(zip(lista1, lista2))
# res_dict = dict(zip(lista1, lista2))
# print(res_dict)

# # ex2
# dict1 = { "nume": "Ana", "varsta": 30, "salariu":10000, "oras": "Cluj" , "ore": 45}
# lista = ["nume", "salariu", "ore"]
# new_dict = {}
# # metoda 1
# for cheie in lista:
#     new_dict.update({cheie: dict1[cheie]})
# print(new_dict)
# # metoda 2
# new_dict2 = {cheie: dict1[cheie] for cheie in lista}
# print(new_dict2)

# ex3
# a)
n = int(input("n="))
my_list = []
for i in range(n):
    student = (input().split(" ", 1))
    my_list.append((int(student[0]), student[1], i + 1))
print(my_list)

# b)
punctaje = {i[0] for i in my_list}
print(punctaje)

# c)
my_dict = {}
for punctaj in punctaje:
    my_dict[punctaj] = [x[1:] for x in my_list if x[0] == punctaj]
print(my_dict)

# d)
punctaj_maxim = my_dict[max(my_dict)][0]
print(punctaj_maxim)

# # ex1(multimi)
# n = int(input("n="))
# m = int(input("m="))
# set1 = set()
# set2 = set()
# for i in range(n):
#     set1.add(int(input("elem1=")))
# for i in range(m):
#     set2.add(int(input("elem1=")))
# print(set1)
# print(set2)
# set3 = set1.symmetric_difference(set2)
# print(set3)

Anul I/Semestrul I/PA/Tutoriat/Tutoriat Teorie/tutoriat6.py


import pandas as pd

df = pd.read_csv("./jcg-data/1005-classified-decks.csv")

summed1 = df.groupby('archetype').sum('score')
summed1['percentage'] = (summed1['score']/df['score'].sum())*100
summed1['total'] = df['score'].sum()

print(summed1.sort_values(by='score', ascending=False))


import requests,re,os
from bs4 import BeautifulSoup

'''下载一章'''
def downdoad_one_music(one_book_title,one_page_url,one_title):
    ture_api = ('https://www.ximalaya.com/revision/play/v1/audio?id=' + one_page_url + '&ptype=1')
    header = {'User-Agent': 'Mozilla/5.0'}
    r = requests.get(ture_api,headers=header)
    demo = r.text
    # print(r.status_code)
    # r.encoding = r.apparent_encoding
    # print(r.text)
    soup = BeautifulSoup(demo, 'lxml')
    # print(soup.prettify())
    url_ids = soup.p.text
    # print(url_ids)
    final_url = re.findall('"src":"(.*?)"', url_ids)[0]
    # print(final_url)

    r = requests.get(final_url)
    # print(r.status_code)
    '''加了os判断路径之后，运行速度变慢，目前不知道原因'''
    if not os.path.exists(one_book_title):
        os.mkdir(one_book_title)
        if not os.path.exists(one_book_title+'/'+one_title + '.mp4'):
            with open(one_book_title+'/'+one_title + '.mp4', 'wb') as fb:
                print('正在下载。。。' + one_title)
                fb.write(r.content)
    else:
    #
        if not os.path.exists(one_book_title+'/'+one_title + '.mp4'):
            with open(one_book_title+'/'+one_title + '.mp4', 'wb') as fb:
                print('正在下载。。。' + one_title)
                fb.write(r.content)
        # else:
        #     pass

    pass
'''获取所有小说的id'''
def get_all_pages(url_first):               #此处输入小说目录首页
    header = {'User-Agent': 'Mozilla/5.0'}
    r = requests.get(url_first, headers=header)
    r.encoding = r.apparent_encoding
    demo = r.text
    soup = BeautifulSoup(demo, 'lxml')
    # print(soup.prettify())
    url_ids = soup.select('#anchor_sound_list > div.sound-list._c2 > div > nav > ul > li:nth-child(7) > a > span')
    n = url_ids[-1].text
    n = int(n)
    for i in range(n):
        i = str(i)
        one_page = ('https://www.ximalaya.com/youshengshu/10331475/p' + i)
        header = {'User-Agent': 'Mozilla/5.0'}
        r = requests.get(one_page, headers=header)
        demo = r.text
        # print(r.status_code)
        r.encoding = r.apparent_encoding
        # print(r.text)
        soup = BeautifulSoup(demo, 'lxml')
        # print(soup.prettify())
        url_ids = soup.select('#anchor_sound_list > div.sound-list._c2 > ul > li> div.text._c2 > a')
        one_book_title = soup.select(
            '#award > main > div.album-detail > div.clearfix > div.detail.layout-main > div.detail-wrapper._II0L > div.album-info.clearfix._II0L > div.info._II0L > h1')[
            0].text
        for url_id in url_ids:
            one_page_url = url_id.get('href').split('/')[-1]
            one_title = url_id.get('title')
            downdoad_one_music(one_book_title,one_page_url, one_title)

    pass
# def main():
# url_first='https://www.ximalaya.com/youshengshu/10331475/'
get_all_pages('https://www.ximalaya.com/youshengshu/10331475/')   #此处输入小说主页
    # pass
# if __name__=='__main__':
#     main()


from django.contrib import admin
from .models import *
# Register your models here.
admin.site.site_title = 'eBook House'
admin.site.site_header = 'eBook House'

mymodels = (UserRole,Master,Profile,Category,Book)
for model in mymodels:
    admin.site.register(model)


import os
import uuid
import trimesh
import numpy as np

MANIFOLD_SOFTWARE_DIR = "Manifold/build"

def remesh(
    vertices: np.ndarray,
    faces: np.ndarray,
    num_faces=2000,
):
    # Write the original mesh as OBJ.
    original_file = random_file_name("obj")
    with open(original_file, "w") as f:
        mesh = trimesh.Trimesh(vertices=vertices, faces=faces)
        f.write(trimesh.exchange.obj.export_obj(mesh))

    # Create a manifold of the original file.
    manifold_file = random_file_name("obj")
    manifold_script_path = os.path.join(MANIFOLD_SOFTWARE_DIR, "manifold")
    cmd = f"{manifold_script_path} {original_file} {manifold_file}"
    os.system(cmd)

    # Simplify the manifold.
    simplified_file = random_file_name("obj")
    simplify_script_path = os.path.join(MANIFOLD_SOFTWARE_DIR, "simplify")
    cmd = (
        f"{simplify_script_path} -i {manifold_file} -o {simplified_file} -f {num_faces}"
    )
    os.system(cmd)

    # Read the simplified manifold.
    with open(simplified_file, "r") as f:
        mesh = trimesh.exchange.obj.load_obj(f)

    # Prevent file spam.
    os.remove(original_file)
    os.remove(manifold_file)
    os.remove(simplified_file)

    return mesh["vertices"], mesh["faces"]

def random_file_name(ext, prefix="tmp_"):
    return f"{prefix}{uuid.uuid4()}.{ext}"


import datetime

class Card:
    def __init__(self, repetitions=0, interval=1.0, ease_factor=2.5, due_time=None):
        self.repetitions = repetitions
        self.interval = interval
        self.ease_factor = ease_factor
        if due_time is not None:
            self.due_time = due_time

    def update(self, grade):
        if grade >= 3:
            if self.repetitions == 0:
                self.interval = 1
            elif self.repetitions == 1:
                self.interval = 6
            else:
                self.interval *= self.ease_factor

            self.repetitions += 1
        else:
            self.repetitions = 0
            self.interval = 1

        self.ease_factor = max(1.3, self.ease_factor + 0.1 - (5 - grade) * (0.08 + (5 - grade) * 0.02))

        # Convert interval to next due time
        current_time = datetime.datetime.now()
        self.due_time = current_time + datetime.timedelta(days=self.interval)

    def is_due(self):
        current_time = datetime.datetime.now()
        return self.due_time <= current_time


import scrapy
from scrapy.loader import ItemLoader

from leroy.items import LeroyParserItem

class LeroyMerlinSpider(scrapy.Spider):
    name = 'leroymerlin'
    allowed_domains = ['leroymerlin.ru']
    url = 'https://leroymerlin.ru'

    def __init__(self, searchInput):
        super(LeroyMerlinSpider, self).__init__()
        self.search = searchInput
        self.start_urls = [self.url + f'/search/?q={self.search}&suggest=true']

    def parse(self, response):
        product_links = response.xpath(
            '//a[@data-qa="product-name"]/@href').extract()
        for link in product_links:
            yield response.follow(f'{self.url + link}', callback=self.parse_goods)

        next_link = response.xpath(
            '//a[contains(@aria-label, "Следующая страница")]/@href').extract()
        if next_link[0] is not None:
            next_page = self.url + next_link[0]
            yield response.follow(next_page, callback=self.parse)

    def parse_goods(self, response):
        loader = ItemLoader(item=LeroyParserItem(), response=response)
        loader.add_xpath('name', '//h1/text()')
        loader.add_value('url', response.url)
        loader.add_xpath('price', '//span[@slot="price"]/text()')
        loader.add_xpath('article', '//span[@slot="article"]/text()')
        loader.add_xpath(
            'text',
            '//uc-pdp-section-layout/uc-pdp-section-vlimited/div/p/text()')
        loader.add_xpath('photo', '//img[@alt="image thumb"]/@src')
        specifications_list = response.xpath('//dl[@class="def-list"]/child::div')
        loader.add_value('specifications', specifications_list)

        yield loader.load_item()


import requests

"""
# Set the API endpoint and the API key
endpoint = "https://api.openai.com/v1/text-davinci/completions"
api_key = "key"

# Set the input text and other request parameters
prompt = "The quick brown fox jumps over the lazy dog."
model = "text-davinci-002"
max_tokens = 256

# Make the request
response = requests.post(
    endpoint,
    headers={
        "Content-Type": "application/json",
        "Authorization": f"Bearer {api_key}",
    },
    json={
        "prompt": prompt,
        "model": model,
        "max_tokens": max_tokens,
    },
)

# Print the response
print(response.json())
"""

import openai
openai.api_key = "key"

# list engines
engines = openai.Engine.list()

# print the first engine's id
for x in engines.data:
    print(x.id)

print("\n\nresults:")

model_engine = "text-davinci-003"

# Set the prompt
prompt = "How do I calculate prime numbers in python?"

# Generate completions
completions = openai.Completion.create(engine=model_engine, prompt=prompt, max_tokens=1024, n=1,stop=None,temperature=0.5)

# Get the first completion
completion = completions.choices[0].text

# Print the completion
print(completion)


import time
from google.oauth2 import service_account
from googleapiclient.discovery import build

class GoogleAuthentication:

    def __init__(self):
        self.scopes = ['https://www.googleapis.com/auth/spreadsheets']
        self.credentials = service_account.Credentials.from_service_account_file(
            r'gsapi/credentials.json',
            scopes=self.scopes)
        self.spreadsheet_service = build('sheets', 'v4', credentials=self.credentials)

    def send_df_to_gsheets(self, df):
        spreadsheet_id = '1wJv40IjpE-8tU_SJJ8y17wX1CL2S2yzwtoqrH0pVR7U'

        body_clear = {}
        self.spreadsheet_service.spreadsheets().values().clear(
            spreadsheetId=spreadsheet_id,
            body=body_clear,
            range='A2:Z').execute()

        time.sleep(0.5)

        body_insert = {'values': df.values.tolist()}
        self.spreadsheet_service.spreadsheets().values().append(
            spreadsheetId=spreadsheet_id,
            body=body_insert,
            valueInputOption='USER_ENTERED',
            range='A2:C2').execute()


# import libraries
from yahooquery import Ticker
import yahoo_fin.stock_info as yf

def report(data):

    # create empty list for balance sheet
    balance_sheet = []

    # create empty list for income statement
    income_statement = []

    # create empty list for cashflow statement
    cashflow_statement = []

    # create empty list for tickers
    tickers = []

    for i, ticker in zip(list(range(len(data))), data):
        try:

            # getting balance sheet data from yahoo finance for the given ticker

            # if there is long term debt
            Ticker(ticker+'.JO').balance_sheet(frequency='a').LongTermDebt

            balance = Ticker(ticker+'.JO').balance_sheet(frequency='a')
            balance.asOfDate = balance.asOfDate.apply(lambda x: str(x)[:4])
            balance = balance.reset_index(drop=True)
            balance = balance.set_index('asOfDate').T
            # append in the list
            balance_sheet.append(balance[['2022','2021','2020', '2019']])

            # getting income statement data from yahoo finance for the given ticker

            # if there is gross profit
            Ticker(ticker+'.JO').income_statement(frequency='a').GrossProfit

            income = Ticker(ticker+'.JO').income_statement(frequency='a')
            income = income[income.periodType !='TTM']
            income.asOfDate = income.asOfDate.apply(lambda x: str(x)[:4])
            income = income.reset_index(drop=True)
            income = income.set_index('asOfDate').T
            # append in the list
            income_statement.append(income[['2022','2021','2020', '2019']])

            # getting cashflow statement data from yahoo finance for the given ticker
            cashflow = yf.get_cash_flow(ticker+'.JO')
            # append in the list
            cashflow_statement.append(cashflow)

            # save useful ticker
            tickers.append(ticker)

            print(str(i+1)+'. '+'Financial stock data, '+ticker+' ticker is successfully extracted.')

        except:

            print(str(i+1)+'. '+'Financial stock data, '+ticker+' ticker is not successfully extracted.')

    return balance_sheet, income_statement, cashflow_statement, tickers


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

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('cms', '0013_urlconfrevision'),
    ]

    operations = [
        migrations.CreateModel(
            name='FancyTitlePluginModel',
            fields=[
                ('cmsplugin_ptr', models.OneToOneField(primary_key=True, to='cms.CMSPlugin', parent_link=True, auto_created=True, serialize=False)),
                ('title', models.CharField(help_text='Title to be displayed', max_length=20, verbose_name='Title')),
                ('margin_top', models.CharField(help_text='Title to be displayed', choices=[('', 'None'), ('sm', 'Small'), ('md', 'Medium'), ('lg', 'Large')], default='', verbose_name='Title', max_length=20)),
                ('center', models.BooleanField(verbose_name='Centered', help_text='Should the title be centered?', max_length=20, default=False)),
            ],
            options={
                'abstract': False,
            },
            bases=('cms.cmsplugin',),
        ),
        migrations.CreateModel(
            name='PromoPluginModel',
            fields=[
                ('cmsplugin_ptr', models.OneToOneField(primary_key=True, to='cms.CMSPlugin', parent_link=True, auto_created=True, serialize=False)),
                ('background', models.ImageField(help_text='The background of the promo box', upload_to='upload/promo_box/', verbose_name='Background image')),
            ],
            options={
                'abstract': False,
            },
            bases=('cms.cmsplugin',),
        ),
    ]


import asyncio
from asyncio import streams, StreamReader, StreamWriter

import logging
import sys
import traceback
import time
import os
import json
import random

logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger(__name__)

class MQTTException(Exception):
    pass

async def read_packet(reader: StreamReader):
    # Read the packet length
    # packet_length = 100
    # multiplier = 1
    while True:
        packet_type = await reader.read(1)
        print(packet_type)
        # packet_length_byte = await reader.read(1)
        # packet_length = int.from_bytes(packet_length_byte, byteorder='big')

    #     if byte == b'':
    #         raise MQTTException("Connection closed while reading packet length")
    #     packet_length += (byte[0] & 0x7F) * multiplier
    #     multiplier *= 128
    #     if byte[0] & 0x80 == 0:
    #         break
    # # Read the packet
    # packet = await reader.read(packet_length)
    # if len(packet) != packet_length:
        # raise MQTTException("Connection closed while reading packet")
    # return packet

async def write_packet(writer: StreamWriter, packet: bytes):
    # Write the packet length
    packet_length = len(packet)
    while True:
        encoded_byte = packet_length % 128
        packet_length //= 128
        if packet_length > 0:
            encoded_byte |= 0x80
        writer.write(bytes([encoded_byte]))
        if packet_length <= 0:
            break
    # Write the packet
    writer.write(packet)
    await writer.drain()

async def write_connect(writer: StreamWriter):
    # Connect packet
    packet = b'\x10\x13\x00\x04MQTT\x04\x02\x00\x3c\x00\x07client12'
    print(len(packet))
    writer.write(packet)
    await writer.drain()

async def main():
    reader, writer = await asyncio.open_connection('localhost', 1883)

    # Send CONNECT
    # await write_packet(writerk)
    # await write_connect(writer)
    # # Receive CONNACK
    packet = await read_packet(reader)
    # print(packet)
    # # Send PUBLISH
    # await write_packet(writer, b'\x30\x0c\x00\x05topic\x00\x05hello')
    # # Receive PUBACK
    # packet = await read_packet(reader)
    # print(packet)
    # # Send DISCONNECT
    # await write_packet(writer, b'\xe0\x00')
    # writer.close()
    # await writer.wait_closed()

if __name__ == "__main__":
    asyncio.run(main())


import numpy

from eke import diffraction
from eke import elements
from eke import tools

class TemplateMatcher:
    """Compare a 2D diffraction pattern to sphere diffraction to
     find the best fit. It finds the sphere diffraction pattern
     with the maximum scalar product between the radial average
     of the pattern and the template."""
    def __init__(self, npixels: int, wavelength: float,
                 detector_distance: float, pixel_size: float,
                 size_start: float, size_end: float,
                 ntemplates: int):
        self.npixels = npixels
        self.wavelength = wavelength
        self.detector_distance = detector_distance
        self.pixel_size = pixel_size
        self.templates, self.sizes = self._generate_templates(
            size_start, size_end, ntemplates)

    def _template_radial_average(self, size: float):
        """Generate radial average of the diffraction from a sphere
        of the given size"""
        material = elements.MATERIALS["sucrose"]
        sphere_diffraction = abs(diffraction.sphere_diffraction(
            size, material, self.wavelength, (1, 2*self.npixels),
            self.detector_distance, self.pixel_size, 1))**2
        return sphere_diffraction.flatten()[self.npixels:]

    def _generate_templates(self, start_size: float,
                            end_size: float, ntemplates: int):
        """Generate a set of templates from the given size range."""
        templates = numpy.zeros((ntemplates, self.npixels))
        sizes = numpy.zeros(ntemplates)
        size_iterator = numpy.linspace(start_size, end_size, ntemplates)
        for index, size in enumerate(size_iterator):
            templates[index, ...] = self._template_radial_average(size)
            sizes[index] = size
        # Normalize templates
        # templates /= numpy.sqrt((templates**2).sum(axis=1))[:, numpy.newaxis]

        return templates, sizes

    def match(self, pattern, mask=None):
        """Find the template that best matches the pattern."""
        radial_average = tools.radial_average(pattern, mask)[:self.npixels]
        mask_radial_average = tools.radial_average(numpy.float_(mask))
        mask_radial_average = mask_radial_average[:self.npixels] > 0
        radial_average[~mask_radial_average] = 0
        dotprod = (self.templates @ radial_average)
        norm = numpy.sqrt(
            ((self.templates*mask_radial_average)**2).sum(axis=1))
        score = dotprod / norm
        # from IPython import embed; embed()
        best_index = score.argmax()
        self.last_scaling = score[best_index]
        return self.sizes[best_index]


#coding=utf-8
import sys
import urllib2
import re
from bs4 import BeautifulSoup

def parseIMG(content):
    imgset = set()
    soup=BeautifulSoup(content,'html.parser')
    for i1 in soup.findAll('img'):
        i2=i1.get('src')
        imgset.add(str(i2))
    return imgset

def write_outputs(imgs, filename):
    with open(filename, 'w') as f:
        for img in imgs:
            f.write(img)
            f.write('\n')

def main():
    # url = 'http://www.sjtu.edu.cn'
    # url = 'https://www.bilibili.com/'
    url = 'http://www.baidu.com'
    if len(sys.argv) > 1:
        url = sys.argv[1]
    content = urllib2.urlopen(url).read()
    imgs = parseIMG(content)
    write_outputs(imgs, 'res2.txt')

if __name__ == '__main__':
    main()


""" This file defines the main object that runs experiments. """

import matplotlib as mpl
mpl.use('Qt4Agg')

import logging
import imp
import os
import os.path
import sys
import copy
import argparse
import threading
import time
import traceback

# Add gps/python to path so that imports work.
sys.path.append('/'.join(str.split(__file__, '/')[:-2]))
from gps.gui.gps_training_gui import GPSTrainingGUI
from gps.utility.data_logger import DataLogger
from gps.sample.sample_list import SampleList

class GPSMain(object):
    """ Main class to run algorithms and experiments. """
    def __init__(self, config, quit_on_end=False):
        """
        Initialize GPSMain
        Args:
            config: Hyperparameters for experiment
            quit_on_end: When true, quit automatically on completion
        """
        self._quit_on_end = quit_on_end
        self._hyperparams = config
        self._conditions = config['common']['conditions']
        if 'train_conditions' in config['common']:
            self._train_idx = config['common']['train_conditions']
            self._test_idx = config['common']['test_conditions']
        else:
            self._train_idx = range(self._conditions)
            config['common']['train_conditions'] = config['common']['conditions']
            self._hyperparams=config
            self._test_idx = self._train_idx

        self._data_files_dir = config['common']['data_files_dir']

        self.agent = config['agent']['type'](config['agent'])
        self.data_logger = DataLogger()
        self.gui = GPSTrainingGUI(config['common']) if config['gui_on'] else None

        config['algorithm']['agent'] = self.agent
        self.algorithm = config['algorithm']['type'](config['algorithm'])

    def run(self, itr_load=None):
        """
        Run training by iteratively sampling and taking an iteration.
        Args:
            itr_load: If specified, loads algorithm state from that
                iteration, and resumes training at the next iteration.
        Returns: None
        """
        try:
            itr_start = self._initialize(itr_load)

            for itr in range(itr_start, self._hyperparams['iterations']):
                for cond in self._train_idx:
                    for i in range(self._hyperparams['num_samples']):
                        self._take_sample(itr, cond, i)

                traj_sample_lists = [
                    self.agent.get_samples(cond, -self._hyperparams['num_samples'])
                    for cond in self._train_idx
                ]

                # Clear agent samples.
                self.agent.clear_samples()

                self._take_iteration(itr, traj_sample_lists)
                pol_sample_lists = self._take_policy_samples()
                self._log_data(itr, traj_sample_lists, pol_sample_lists)
        except Exception as e:
            traceback.print_exception(*sys.exc_info())
        finally:
            self._end()

    def test_policy(self, itr, N):
        """
        Take N policy samples of the algorithm state at iteration itr,
        for testing the policy to see how it is behaving.
        (Called directly from the command line --policy flag).
        Args:
            itr: the iteration from which to take policy samples
            N: the number of policy samples to take
        Returns: None
        """
        algorithm_file = self._data_files_dir + 'algorithm_itr_%02d.pkl' % itr
        self.algorithm = self.data_logger.unpickle(algorithm_file)
        if self.algorithm is None:
            print("Error: cannot find '%s.'" % algorithm_file)
            os._exit(1) # called instead of sys.exit(), since t
        traj_sample_lists = self.data_logger.unpickle(self._data_files_dir +
            ('traj_sample_itr_%02d.pkl' % itr))

        pol_sample_lists = self._take_policy_samples(N)
        self.data_logger.pickle(
            self._data_files_dir + ('pol_sample_itr_%02d.pkl' % itr),
            copy.copy(pol_sample_lists)
        )

        if self.gui:
            self.gui.update(itr, self.algorithm, self.agent,
                traj_sample_lists, pol_sample_lists)
            self.gui.set_status_text(('Took %d policy sample(s) from ' +
                'algorithm state at iteration %d.\n' +
                'Saved to: data_files/pol_sample_itr_%02d.pkl.\n') % (N, itr, itr))

    def _initialize(self, itr_load):
        """
        Initialize from the specified iteration.
        Args:
            itr_load: If specified, loads algorithm state from that
                iteration, and resumes training at the next iteration.
        Returns:
            itr_start: Iteration to start from.
        """
        if itr_load is None:
            if self.gui:
                self.gui.set_status_text('Press \'go\' to begin.')
            return 0
        else:
            algorithm_file = self._data_files_dir + 'algorithm_itr_%02d.pkl' % itr_load
            self.algorithm = self.data_logger.unpickle(algorithm_file)
            if self.algorithm is None:
                print("Error: cannot find '%s.'" % algorithm_file)
                os._exit(1) # called instead of sys.exit(), since this is in a thread

            if self.gui:
                traj_sample_lists = self.data_logger.unpickle(self._data_files_dir +
                    ('traj_sample_itr_%02d.pkl' % itr_load))
                if self.algorithm.cur[0].pol_info:
                    pol_sample_lists = self.data_logger.unpickle(self._data_files_dir +
                        ('pol_sample_itr_%02d.pkl' % itr_load))
                else:
                    pol_sample_lists = None
                self.gui.set_status_text(
                    ('Resuming training from algorithm state at iteration %d.\n' +
                    'Press \'go\' to begin.') % itr_load)
            return itr_load + 1

    def _take_sample(self, itr, cond, i):
        """
        Collect a sample from the agent.
        Args:
            itr: Iteration number.
            cond: Condition number.
            i: Sample number.
        Returns: None
        """
        if self.algorithm._hyperparams['sample_on_policy'] \
                and self.algorithm.iteration_count > 0:
            pol = self.algorithm.policy_opt.policy
        else:
            pol = self.algorithm.cur[cond].traj_distr
        if self.gui:
            self.gui.set_image_overlays(cond)   # Must call for each new cond.
            redo = True
            while redo:
                while self.gui.mode in ('wait', 'request', 'process'):
                    if self.gui.mode in ('wait', 'process'):
                        time.sleep(0.01)
                        continue
                    # 'request' mode.
                    if self.gui.request == 'reset':
                        try:
                            self.agent.reset(cond)
                        except NotImplementedError:
                            self.gui.err_msg = 'Agent reset unimplemented.'
                    elif self.gui.request == 'fail':
                        self.gui.err_msg = 'Cannot fail before sampling.'
                    self.gui.process_mode()  # Complete request.

                self.gui.set_status_text(
                    'Sampling: iteration %d, condition %d, sample %d.' %
                    (itr, cond, i)
                )
                self.agent.sample(
                    pol, cond,
                    verbose=(i < self._hyperparams['verbose_trials'])
                )

                if self.gui.mode == 'request' and self.gui.request == 'fail':
                    redo = True
                    self.gui.process_mode()
                    self.agent.delete_last_sample(cond)
                else:
                    redo = False
        else:
            self.agent.sample(
                pol, cond,
                verbose=(i < self._hyperparams['verbose_trials'])
            )

    def _take_iteration(self, itr, sample_lists):
        """
        Take an iteration of the algorithm.
        Args:
            itr: Iteration number.
        Returns: None
        """
        if self.gui:
            self.gui.set_status_text('Calculating.')
            self.gui.start_display_calculating()
        self.algorithm.iteration(sample_lists)
        if self.gui:
            self.gui.stop_display_calculating()

    def _take_policy_samples(self, N=None):
        """
        Take samples from the policy to see how it's doing.
        Args:
            N  : number of policy samples to take per condition
        Returns: None
        """
        if 'verbose_policy_trials' not in self._hyperparams:
            # AlgorithmTrajOpt
            return None
        verbose = self._hyperparams['verbose_policy_trials']
        if self.gui:
            self.gui.set_status_text('Taking policy samples.')
        pol_samples = [[None] for _ in range(len(self._test_idx))]
        # Since this isn't noisy, just take one sample.
        # TODO: Make this noisy? Add hyperparam?
        # TODO: Take at all conditions for GUI?
        for cond in range(len(self._test_idx)):
            pol_samples[cond][0] = self.agent.sample(
                self.algorithm.policy_opt.policy, self._test_idx[cond],
                verbose=verbose, save=False, noisy=False)
        return [SampleList(samples) for samples in pol_samples]

    def _log_data(self, itr, traj_sample_lists, pol_sample_lists=None):
        """
        Log data and algorithm, and update the GUI.
        Args:
            itr: Iteration number.
            traj_sample_lists: trajectory samples as SampleList object
            pol_sample_lists: policy samples as SampleList object
        Returns: None
        """
        if self.gui:
            self.gui.set_status_text('Logging data and updating GUI.')
            self.gui.update(itr, self.algorithm, self.agent,
                traj_sample_lists, pol_sample_lists)
            self.gui.save_figure(
                self._data_files_dir + ('figure_itr_%02d.png' % itr)
            )
        if 'no_sample_logging' in self._hyperparams['common']:
            return
        self.data_logger.pickle(
            self._data_files_dir + ('algorithm_itr_%02d.pkl' % itr),
            copy.copy(self.algorithm)
        )
        self.data_logger.pickle(
            self._data_files_dir + ('traj_sample_itr_%02d.pkl' % itr),
            copy.copy(traj_sample_lists)
        )
        if pol_sample_lists:
            self.data_logger.pickle(
                self._data_files_dir + ('pol_sample_itr_%02d.pkl' % itr),
                copy.copy(pol_sample_lists)
            )

    def _end(self):
        """ Finish running and exit. """
        if self.gui:
            self.gui.set_status_text('Training complete.')
            self.gui.end_mode()
            if self._quit_on_end:
                # Quit automatically (for running sequential expts)
                os._exit(1)

def main():
    """ Main function to be run. """
    parser = argparse.ArgumentParser(description='Run the Guided Policy Search algorithm.')
    parser.add_argument('experiment', type=str,
                        help='experiment name')
    parser.add_argument('-n', '--new', action='store_true',
                        help='create new experiment')
    parser.add_argument('-t', '--targetsetup', action='store_true',
                        help='run target setup')
    parser.add_argument('-r', '--resume', metavar='N', type=int,
                        help='resume training from iter N')
    parser.add_argument('-p', '--policy', metavar='N', type=int,
                        help='take N policy samples (for BADMM/MDGPS only)')
    parser.add_argument('-s', '--silent', action='store_true',
                        help='silent debug print outs')
    parser.add_argument('-q', '--quit', action='store_true',
                        help='quit GUI automatically when finished')
    args = parser.parse_args()

    exp_name = args.experiment
    resume_training_itr = args.resume
    test_policy_N = args.policy

    from gps import __file__ as gps_filepath
    gps_filepath = os.path.abspath(gps_filepath)
    gps_dir = '/'.join(str.split(gps_filepath, '/')[:-3]) + '/'
    exp_dir = gps_dir + 'experiments/' + exp_name + '/'
    hyperparams_file = exp_dir + 'hyperparams.py'

    if args.silent:
        logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)
    else:
        logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)

    if args.new:
        from shutil import copy

        if os.path.exists(exp_dir):
            sys.exit("Experiment '%s' already exists.\nPlease remove '%s'." %
                     (exp_name, exp_dir))
        os.makedirs(exp_dir)

        prev_exp_file = '.previous_experiment'
        prev_exp_dir = None
        try:
            with open(prev_exp_file, 'r') as f:
                prev_exp_dir = f.readline()
            copy(prev_exp_dir + 'hyperparams.py', exp_dir)
            if os.path.exists(prev_exp_dir + 'targets.npz'):
                copy(prev_exp_dir + 'targets.npz', exp_dir)
        except IOError as e:
            with open(hyperparams_file, 'w') as f:
                f.write('# To get started, copy over hyperparams from another experiment.\n' +
                        '# Visit rll.berkeley.edu/gps/hyperparams.html for documentation.')
        with open(prev_exp_file, 'w') as f:
            f.write(exp_dir)

        exit_msg = ("Experiment '%s' created.\nhyperparams file: '%s'" %
                    (exp_name, hyperparams_file))
        if prev_exp_dir and os.path.exists(prev_exp_dir):
            exit_msg += "\ncopied from     : '%shyperparams.py'" % prev_exp_dir
        sys.exit(exit_msg)

    if not os.path.exists(hyperparams_file):
        sys.exit("Experiment '%s' does not exist.\nDid you create '%s'?" %
                 (exp_name, hyperparams_file))

    hyperparams = imp.load_source('hyperparams', hyperparams_file)
    if args.targetsetup:
        try:
            import matplotlib.pyplot as plt
            from gps.agent.ros.agent_ros import AgentROS
            from gps.gui.target_setup_gui import TargetSetupGUI

            agent = AgentROS(hyperparams.config['agent'])
            TargetSetupGUI(hyperparams.config['common'], agent)

            plt.ioff()
            plt.show()
        except ImportError:
            sys.exit('ROS required for target setup.')
    elif test_policy_N:
        import random
        import numpy as np
        import matplotlib.pyplot as plt

        seed = hyperparams.config.get('random_seed', 0)
        random.seed(seed)
        np.random.seed(seed)

        data_files_dir = exp_dir + 'data_files/'
        data_filenames = os.listdir(data_files_dir)
        algorithm_prefix = 'algorithm_itr_'
        algorithm_filenames = [f for f in data_filenames if f.startswith(algorithm_prefix)]
        current_algorithm = sorted(algorithm_filenames, reverse=True)[0]
        current_itr = int(current_algorithm[len(algorithm_prefix):len(algorithm_prefix)+2])

        gps = GPSMain(hyperparams.config)
        if hyperparams.config['gui_on']:
            test_policy = threading.Thread(
                target=lambda: gps.test_policy(itr=current_itr, N=test_policy_N)
            )
            test_policy.daemon = True
            test_policy.start()

            plt.ioff()
            plt.show()
        else:
            gps.test_policy(itr=current_itr, N=test_policy_N)
    else:
        import random
        import numpy as np
        import matplotlib.pyplot as plt

        seed = hyperparams.config.get('random_seed', 0)
        random.seed(seed)
        np.random.seed(seed)

        gps = GPSMain(hyperparams.config, args.quit)
        if hyperparams.config['gui_on']:
            run_gps = threading.Thread(
                target=lambda: gps.run(itr_load=resume_training_itr)
            )
            run_gps.daemon = True
            run_gps.start()

            plt.ioff()
            plt.show()
        else:
            gps.run(itr_load=resume_training_itr)

if __name__ == "__main__":
    main()


import bpy
import os
import sys
sys.path.append('./scripts')
from helper import set_prop_val

# output properties
bpy.data.scenes['Scene'].render.resolution_x = 1280 # 1920
bpy.data.scenes['Scene'].render.resolution_y = 720 # 1080
bpy.data.scenes['Scene'].render.resolution_percentage = 100
bpy.data.scenes['Scene'].render.tile_x = 256
bpy.data.scenes['Scene'].render.tile_y = 256
bpy.data.scenes['Scene'].cycles.max_bounces = 4
bpy.data.scenes['Scene'].cycles.min_bounces = 0
bpy.data.scenes['Scene'].cycles.sample = 300

# name of object
obj_name = 'sphere'
# number of images
nimages = 25
# root directory of synthetic dataset
rdir = 'C:/Users/Admin/Documents/3D_Recon/Data/synthetic_data'
# output directory of rendered images
odir = '%s/%s/train/ps' % (rdir, obj_name)
# list of properties
props = ['tex', 'alb', 'spec', 'rough', 'concav']
# index of effective properties
ind_props = [1, 2, 3]
# get material nodes
nodes = bpy.data.materials['Material'].node_tree.nodes
# set the object visible
bpy.data.objects['Sphere'].hide_render = False

# hide all the light sources
for ind_light in range(0, nimages):
	bpy.data.objects['Lamp.%03d' % ind_light].hide_render = True

set_prop_val(nodes, 0, 2) # set texture to 0.2
for ind_1 in range(2, 9, 3):
	set_prop_val(nodes, ind_props[0], ind_1)

	for ind_2 in range(2, 9, 3):
		set_prop_val(nodes, ind_props[1], ind_2)

		for ind_3 in range(2, 9, 3):
			set_prop_val(nodes, ind_props[2], ind_3)

			subdir = '00%02d%02d%02d' % (ind_1, ind_2, ind_3) # change if ind_props changes
			outdir = '%s/%s' % (odir, subdir)

			if not os.path.exists(outdir):
				os.makedirs(outdir)

			for ind_light in range(0, nimages):
				bpy.data.objects['Lamp.%03d' % ind_light].hide_render = False
				bpy.data.scenes['Scene'].render.filepath = '%s/%04d.jpg' % (outdir, ind_light)
				bpy.ops.render.render(write_still=True)
				bpy.data.objects['Lamp.%03d' % ind_light].hide_render = True


# -*- coding: utf-8 -*-

import sys
import unittest

from vt102 import *

if sys.version_info[0] > 2:
    class TestUnicode(unittest.TestCase):
        def test_unicode_input(self):
            s = stream()
            sc = screen((2, 4))
            sc.attach(s)

            text = u"тест"
            s.process(text)

            self.assertEqual(sc.display[0], u"тест")

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


from sqlalchemy import Column, Integer, String, DateTime

from base import Base

class FanSpeed(Base):
    """ Fan Speed """

    __tablename__ = "fan_speed"

    id = Column(Integer, primary_key=True)
    ming_rig_id = Column(String(250), nullable=False)
    ming_card_id = Column(String(250), nullable=False)
    timestamp = Column(String(100), nullable=False)
    date_created = Column(DateTime, nullable=False)
    fan_speed = Column(Integer, nullable=False)
    fan_size = Column(Integer, nullable=False)
    trace_id = Column(String(100), nullable=False)

    def __init__(self, trace_id, date_created, ming_rig_id, ming_card_id, timestamp, fan_speed, fan_size):
        """ Initializes a fan speed reading """
        self.trace_id = trace_id
        self.ming_rig_id = ming_rig_id
        self.ming_card_id = ming_card_id
        self.timestamp = timestamp
        self.date_created = date_created
        self.fan_speed = fan_speed
        self.fan_size = fan_size

    def to_dict(self):
        """ Dictionary Representation of a fan speed reading """
        dict = {}
        dict['trace_id'] = self.trace_id
        dict['ming_rig_id'] = self.ming_rig_id
        dict['ming_card_id'] = self.ming_card_id
        dict['timestamp'] = self.timestamp
        dict['date_created'] = self.date_created
        dict['fan_speed'] = {}
        dict['fan_speed']['fan_size'] = self.fan_size
        dict['fan_speed']['fan_speed'] = self.fan_speed

        return dict


from django.conf import ImproperlyConfigured
from django.test import SimpleTestCase

from permissions_auditor import core
from permissions_auditor.core import ViewDetails
from permissions_auditor.tests.fixtures import views

class ViewParserTest(SimpleTestCase):

    def setUp(self):
        self.parser = core.ViewParser()

    def test_invalid_config(self):
        """
        Invalid processors should raise ImproperlyConfigured.
        """
        with self.settings(PERMISSIONS_AUDITOR_PROCESSORS=['invalid_parser']):
            self.assertRaises(ImproperlyConfigured, lambda: core.ViewParser())

    def test_no_parsers(self):
        with self.settings(PERMISSIONS_AUDITOR_PROCESSORS=[]):
            parser = core.ViewParser()
            results = parser.parse(views.LoginRequiredView)
            self.assertEqual(results, ([], False, ''))

    def test_parse_cbv(self):
        """
        The default configuration should be able to parse PermissionRequiredMixin.
        """
        results = self.parser.parse(views.PermissionRequiredViewDocstring)
        self.assertEqual(
            results, (['tests.test_perm'], True, 'Custom docstrings should be detected.')
        )

    def test_parse_func_view(self):
        """
        The default configuration should be able to parse @permission_required().
        """
        results = self.parser.parse(views.permission_required_view)
        self.assertEqual(
            results, (['tests.test_perm'], True, '')
        )

class GetViewsTest(SimpleTestCase):

    def setUp(self):
        self.module = 'permissions_auditor.tests.fixtures.views'
        self.views_results = [
            ViewDetails(
                module=self.module, name='BaseView',
                url='/',
                permissions=[], login_required=False, docstring=''
            ),
            ViewDetails(
                module=self.module, name='PermissionRequiredMultiView',
                url='/multi_perm_view/',
                permissions=['tests.test_perm', 'tests.test_perm2'],
                login_required=True, docstring=''
            ),
            ViewDetails(
                module=self.module, name='login_required_view',
                url='/new_style/login_required/',
                permissions=[], login_required=True, docstring=''
            ),
            ViewDetails(
                module=self.module, name='permission_required_view',
                url='/new_style/perm_required/',
                permissions=['tests.test_perm'], login_required=True, docstring=''
            ),
            ViewDetails(
                module=self.module, name='staff_member_required_view',
                url='/admin/staff_member_required/',
                permissions=[], login_required=True, docstring='Staff member required'
            ),
            ViewDetails(
                module=self.module, name='LoginRequiredView',
                url='/old_style/login_required/',
                permissions=[], login_required=True, docstring=''
            ),
            ViewDetails(
                module=self.module, name='PermissionRequiredView',
                url='/old_style/perm_required/',
                permissions=['tests.test_perm'], login_required=True, docstring=''
            ),
        ]

    def reload_blacklist(self):
        """
        The blacklist is loaded when the app starts, we need to override the values
        when we change settings.
        """
        core.NAMESPACE_BLACKLIST = tuple(core._get_blacklist('namespaces'))
        core.VIEW_BLACKLIST = tuple(core._get_blacklist('view_names'))
        core.MODULE_BLACKLIST = tuple(core._get_blacklist('modules'))

    def test_get_views_results(self):
        blacklist = {
            'namespaces': [],
            'view_names': [],
            'modules': [],
        }

        with self.settings(PERMISSIONS_AUDITOR_BLACKLIST=blacklist):
            self.reload_blacklist()
            views = core.get_views()
            self.assertSequenceEqual(views, self.views_results)

    def test_namespace_blacklist(self):
        blacklist = {
            'namespaces': ['admin'],
            'view_names': [],
            'modules': [],
        }

        with self.settings(PERMISSIONS_AUDITOR_BLACKLIST=blacklist):
            self.reload_blacklist()
            views = core.get_views()
            self.assertNotIn(views, self.views_results[3])  # staff_member_required_view

    def test_view_name_blacklist(self):
        blacklist = {
            'namespaces': [],
            'view_names': ['{}.BaseView'.format(self.module)],
            'modules': [],
        }

        with self.settings(PERMISSIONS_AUDITOR_BLACKLIST=blacklist):
            self.reload_blacklist()
            views = core.get_views()
            self.assertNotIn(views, self.views_results[0])  # BaseView

    def test_module_blacklist(self):
        blacklist = {
            'namespaces': [],
            'view_names': [],
            'modules': [self.module],
        }

        with self.settings(PERMISSIONS_AUDITOR_BLACKLIST=blacklist):
            self.reload_blacklist()
            views = core.get_views()
            self.assertEqual(views, [])


#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 24 08:51:40 2019

@author: jg
"""

x=8
y=15

while x!=y:
    if y>x:
        y=y-x
    else:
        x=x-y
    print (x,y)