"""
Optional step of pretrained the detector.
Based on http://pytorch.org/tutorials/intermediate/torchvision_tutorial.html.
"""

import os
import sys
import torch

import torchvision
from torchvision.models.detection.faster_rcnn import FastRCNNPredictor

from detector.engine import train_one_epoch
import detector.utils as utils
import detector.transforms as T
from dataloaders.visual_genome import VG
from PIL import Image
from lib.pytorch_misc import save_checkpoint, get_smallest_lr
from config import BOX_SCALE

VG.split = sys.argv[1]
data_dir = sys.argv[2]
save_dir = sys.argv[3]
checkpoint_name = '%s_maskrcnn_res50fpn.pth' % VG.split

if not os.path.exists(save_dir):
    if len(save_dir) == 0:
        raise ValueError("save_dir must be a valid path")
    os.mkdir(save_dir)

class VGLoader(VG):
    def __init__(self, mode, data_dir, transforms):
        super(VGLoader, self).__init__(mode, data_dir, num_val_im=5000, filter_duplicate_rels=True,
                                            min_graph_size=-1,
                                            max_graph_size=-1,
                                            filter_non_overlap=False)
        self.transforms = transforms

    def __getitem__(self, idx):
        index = idx

        img = Image.open(os.path.join(self.images_dir, self.filenames[index])).convert('RGB')
        w, h = img.size

        gt_boxes = self.gt_boxes[index].copy()

        if VG.split == 'stanford':
            # makes boxes scale the same as images
            gt_boxes = gt_boxes / (BOX_SCALE / max(w, h))

        if self.is_train:
            # crop boxes that are too large.
            gt_boxes[:, [1, 3]] = gt_boxes[:, [1, 3]].clip(None, h)
            gt_boxes[:, [0, 2]] = gt_boxes[:, [0, 2]].clip(None, w)

        if VG.split in ['vte', 'gqa']:
            # width, height can become zero after clipping (need to double-check why)
            ind_zero = (gt_boxes[:, 2] - gt_boxes[:, 0]) == 0 & (gt_boxes[:, 0] > 0)  # x1 == x2 and x1 > 0
            gt_boxes[ind_zero, 0] -= 1
            ind_zero = (gt_boxes[:, 3] - gt_boxes[:, 1]) == 0 & (gt_boxes[:, 1] > 0)  # y1 == y2 and y1 > 0
            gt_boxes[ind_zero, 1] -= 1

        gt_boxes = torch.as_tensor(gt_boxes, dtype=torch.float32)

        target = {}
        target["boxes"] = gt_boxes
        target["labels"] = torch.from_numpy(self.gt_classes[index]).long()
        # target["masks"] = masks  # no mask annotations
        target["image_id"] = torch.tensor([idx])
        target["area"] = (gt_boxes[:, 3] - gt_boxes[:, 1]) * (gt_boxes[:, 2] - gt_boxes[:, 0])
        target["iscrowd"] = torch.zeros((len(self.gt_classes[index]),), dtype=torch.int64)  # suppose all instances are not crowd

        if self.transforms is not None:
            img, target = self.transforms(img, target)

        return img, target

def get_model_optimizer(num_classes):
    # load an instance segmentation model pre-trained pre-trained on COCO
    model = torchvision.models.detection.maskrcnn_resnet50_fpn(pretrained=True)

    # get number of input features for the classifier
    in_features = model.roi_heads.box_predictor.cls_score.in_features
    # replace the pre-trained head with a new one
    model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)

    # now get the number of input features for the mask classifier
    model.roi_heads.mask_predictor = None  # no masks in these datasets

    # construct an optimizer
    params = [p for p in model.parameters() if p.requires_grad]
    optimizer = torch.optim.SGD(params, lr=0.005,  # didn't tune these values but looks good
                                momentum=0.9, weight_decay=0.0005)

    start_epoch = -1
    if os.path.exists(checkpoint_name):
        print('loading the model and optimizer state from %s' % checkpoint_name)
        state_dict = torch.load(checkpoint_name)
        model.load_state_dict(state_dict['state_dict'])
        optimizer.load_state_dict(state_dict['optimizer'])
        start_epoch = state_dict['epoch']

    return model, optimizer, start_epoch

def get_transform(train):
    transforms = []
    transforms.append(T.ToTensor())
    if train:
        transforms.append(T.RandomHorizontalFlip(0.5))
    return T.Compose(transforms)

def main():
    # train on the GPU or on the CPU, if a GPU is not available
    device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')

    # our dataset has two classes only - background and person
    num_classes = 151 if VG.split == 'stanford' else 1704

    # use our dataset and defined transformations
    dataset = VGLoader('train', data_dir, get_transform(train=True))
    # dataset_test = GQALoader('val', get_transform(train=False))

    # define training and validation data loaders
    data_loader = torch.utils.data.DataLoader(
        dataset, batch_size=3 if VG.split == 'stanford' else 2, shuffle=True, num_workers=4,
        collate_fn=utils.collate_fn)

    # data_loader_test = torch.utils.data.DataLoader(
    #     dataset_test, batch_size=1, shuffle=False, num_workers=4,
    #     collate_fn=utils.collate_fn)

    # get the model using our helper function
    model, optimizer, start_epoch = get_model_optimizer(num_classes)
    print('start_epoch', start_epoch)

    # move model to the right device
    model.to(device)

    # and a learning rate scheduler
    lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
                                                   step_size=3,
                                                   gamma=0.1)
    # let's train it for 10 epochs
    num_epochs = 10

    for epoch in range(start_epoch + 1, num_epochs):
        print('\nepoch %d, smallest lr %f\n' % (epoch, get_smallest_lr(optimizer)))
        # train for one epoch, printing every 10 iterations
        train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=10)

        try:
            print("\nCheckpointing to %s" % os.path.join(save_dir, checkpoint_name))
            save_checkpoint(model, optimizer, os.path.join(save_dir, checkpoint_name), {'epoch': epoch})
            print('done!\n')
        except Exception as e:
            print('error saving checkpoint', e)

        # update the learning rate
        lr_scheduler.step(epoch)
        # evaluate on the test dataset
        # evaluate(model, data_loader_test, device=device)  # some issues with evaluation (check coco_eval code)

    print("That's it!")

if __name__ == "__main__":
    main()


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

import re

from platformcode import config, logger, platformtools
from core.item import Item
from core import httptools, scrapertools, tmdb, servertools

host = 'https://www.pelisplanet.to/'

def item_configurar_proxies(item):
    color_list_proxies = config.get_setting('channels_list_proxies_color', default='red')

    color_avis = config.get_setting('notification_avis_color', default='yellow')
    color_exec = config.get_setting('notification_exec_color', default='cyan')

    context = []

    tit = '[COLOR %s]Información proxies[/COLOR]' % color_avis
    context.append({'title': tit, 'channel': 'helper', 'action': 'show_help_proxies'})

    if config.get_setting('channel_pelisplanet_proxies', default=''):
        tit = '[COLOR %s][B]Quitar los proxies del canal[/B][/COLOR]' % color_list_proxies
        context.append({'title': tit, 'channel': item.channel, 'action': 'quitar_proxies'})

    tit = '[COLOR %s]Ajustes categoría proxies[/COLOR]' % color_exec
    context.append({'title': tit, 'channel': 'actions', 'action': 'open_settings'})

    plot = 'Es posible que para poder utilizar este canal necesites configurar algún proxy, ya que no es accesible desde algunos países/operadoras.'
    plot += '[CR]Si desde un navegador web no te funciona el sitio ' + host + ' necesitarás un proxy.'
    return item.clone( title = 'Configurar proxies a usar ... [COLOR plum](si no hay resultados)[/COLOR]', action = 'configurar_proxies', folder=False, context=context, plot=plot, text_color='red' )

def quitar_proxies(item):
    from modules import submnuctext
    submnuctext._quitar_proxies(item)
    return True

def configurar_proxies(item):
    from core import proxytools
    return proxytools.configurar_proxies_canal(item.channel, host)

def do_downloadpage(url, post=None, headers=None):
    # ~ por si viene de enlaces guardados
    ant_hosts = ['https://www.pelisplanet.com/']

    for ant in ant_hosts:
        url = url.replace(ant, host)

    if not url.startswith(host):
        data = httptools.downloadpage(url, post=post, headers=headers).data
    else:
        data = httptools.downloadpage_proxy('pelisplanet', url, post=post, headers=headers).data

    return data

def mainlist(item):
    return mainlist_pelis(item)

def mainlist_pelis(item):
    logger.info()
    itemlist = []

    itemlist.append(item_configurar_proxies(item))

    itemlist.append(item.clone( title = 'Buscar película ...', action = 'search', search_type = 'movie', text_color = 'deepskyblue' ))

    itemlist.append(item.clone( title = 'Catálogo', action = 'list_all', url = host ))

    itemlist.append(item.clone( title = 'Estrenos', action = 'list_all', url = host + 'genero/estrenos/' ))

    itemlist.append(item.clone( title = 'Por idioma', action = 'idiomas', search_type = 'movie' ))

    itemlist.append(item.clone( title = 'Por género', action = 'generos', search_type = 'movie' ))
    itemlist.append(item.clone( title = 'Por año', action = 'anios', search_type = 'movie' ))

    return itemlist

def idiomas(item):
    logger.info()
    itemlist = []

    itemlist.append(item.clone( title = 'Castellano', action = 'list_all', url = host + 'idioma/castellano/', text_color='moccasin' ))
    itemlist.append(item.clone( title = 'Latino', action = 'list_all', url = host + 'idioma/latino/', text_color='moccasin' ))
    itemlist.append(item.clone( title = 'Subtitulada', action = 'list_all', url = host + 'idioma/subtitulada/', text_color='moccasin' ))
    itemlist.append(item.clone( title = 'Subtitulado', action = 'list_all', url = host + 'idioma/subtitulado/', text_color='moccasin' ))

    return itemlist

def generos(item):
    logger.info()
    itemlist = []

    opciones = [
       ('accion', 'Acción'),
       ('adolecentes', 'Adolecentes'),
       ('animacion-e-infantil', 'Animación e Infantil'),
       ('anime', 'Anime'),
       ('artes-marciales', 'Artes Marciales'),
       ('aventura', 'Aventura'),
       ('biografico', 'Biográfico'),
       ('ciencia-ficcion', 'Ciencia Ficcion'),
       ('comedia', 'Comedia'),
       ('crimen', 'Crimen'),
       ('deporte', 'Deporte'),
       ('documental', 'Documental'),
       ('drama', 'Drama'),
       ('familiar', 'Familiar'),
       ('fantasia', 'Fantasia'),
       ('fantastico', 'Fantastico'),
       ('guerra', 'Guerra'),
       ('historica', 'Historica'),
       ('homosexualidad', 'Homosexualidad'),
       ('intriga', 'Intriga'),
       ('misterio', 'Misterio'),
       ('musical', 'Musical'),
       ('navideñas', 'Navideñas'),
       ('religion', 'Religión'),
       ('romance', 'Romance'),
       ('secuela', 'Secuela'),
       ('superheroes', 'Superheroes'),
       ('suspenso', 'Suspenso'),
       ('terror', 'Terror'),
       ('triller', 'Triller'),
       ('uncategorized', 'Uncategorized'),
       ('videojuegos', 'Videojuegos'),
       ('westerns', 'Westerns')
    ]

    for opc, tit in opciones:
        itemlist.append(item.clone( title=tit, url= host + 'genero/' + opc + '/', action = 'list_all', text_color = 'deepskyblue' ))

    return itemlist

def anios(item):
    logger.info()
    itemlist = []

    from datetime import datetime
    current_year = int(datetime.today().year)

    for x in range(current_year, 1920, -1):
        itemlist.append(item.clone( title = str(x), url= host + 'fecha-estreno/' + str(x) + '/', action = 'list_all', text_color = 'deepskyblue' ))

    return itemlist

def list_all(item):
    logger.info()
    itemlist = []

    data = do_downloadpage(item.url)
    data = re.sub(r"\n|\r|\t|\(.*?\)|\s{2}| ", "", data)

    data = scrapertools.find_single_match(data, '
(.*?)')

    patron = 'col-sm-5".*?href="([^"]+)".+?'
    patron += 'browse-movie-link-qd.*?>([^<]+)</.+?'
    patron += '
([^<]+)
.+?'
    patron += 'title one-line">([^<]+)
.+?'
    patron += 'img-responsive" src="([^"]+)".*?'

    matches = re.compile(patron, re.DOTALL).findall(data)

    for url, qlty, year, title, thumb in matches:
        itemlist.append(item.clone( action='findvideos', url=url, title=title, thumbnail=thumb, qualities=qlty, contentType='movie', contentTitle=title, infoLabels={'year': year} ))

    tmdb.set_infoLabels(itemlist)

    if itemlist:
        next_page = scrapertools.find_single_match(data, '')

        if next_page:
            itemlist.append(item.clone( title='Siguientes ...', url=next_page, action='list_all', text_color='coral' ))

    return itemlist

def findvideos(item):
    logger.info()
    itemlist = []

    IDIOMAS = {'Castellano': 'Esp', 'Español Latino': 'Lat', 'Subtitulada': 'Vose'}

    data = do_downloadpage(item.url)
    data = re.sub(r"\n|\r|\t|\(.*?\)|\s{2}| ", "", data)

    patron = '<a id="[^"]+" style="cursor:pointer; cursor: hand" rel="([^"]+)".*?'
    patron += '(.*?).*?'
    patron += '([^<]+)'

    matches = re.compile(patron, re.DOTALL).findall(data)

    ses = 0

    for url, lang, servidor in matches:
        ses += 1

        servidor = servidor.lower().strip()

        if url.startswith('ttps://') == True: continue
        elif servidor == 'streamvips': continue
        elif servidor == 'ultrastream': continue

        if '/hqq.' in url or '/waaw.' in url or '/netu.' in url: continue

        elif '.mystream' in url: continue

        servidor = servertools.get_server_from_url(url)

        servidor = servertools.corregir_servidor(servidor)

        if servertools.is_server_available(servidor):
            if not servertools.is_server_enabled(servidor): continue
        else:
            if not config.get_setting('developer_mode', default=False): continue

        itemlist.append(Item( channel = item.channel, action = 'play', server = servidor, title = '', url = url, language = IDIOMAS.get(lang, lang) ))

    if not itemlist:
        if not ses == 0:
            platformtools.dialog_notification(config.__addon_name, '[COLOR tan][B]Sin enlaces Soportados[/B][/COLOR]')
            return

    return itemlist

def list_search(item):
    logger.info()
    itemlist = []

    data = do_downloadpage(item.url)

    patron = '<li class="itemlist searchResult".*?<a href="(.*?)".*?title="(.*?)".*?src="(.*?)"'

    matches = re.compile(patron, re.DOTALL).findall(data)

    for url, title, thumb in matches:
        itemlist.append(item.clone( action='findvideos', url=url, title=title, thumbnail=thumb, contentType='movie', contentTitle=title ))

    return itemlist

def search(item, texto):
    logger.info()
    try:
        texto = texto.replace(" ", "+")
        item.url = host + 'search/' + texto +'/'

        return list_search(item)
    except:
        import sys
        for line in sys.exc_info():
            logger.error("%s" % line)
        return []


from .models import Articles
from django.forms import ModelForm, TextInput, DateTimeInput, Textarea
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User

class ArticlesForm(ModelForm):
    class Meta:
        model = Articles
        fields = ['title', 'intro', 'full_text', 'date']

        widgets = {
            "title": TextInput(attrs={
                "class": "form-control",
                "placeholder": "Article title"
            }),
            "intro": TextInput(attrs={
                "class": "form-control",
                "placeholder": "Article introduction"
            }),
            "date": DateTimeInput(attrs={
                "class": "form-control",
                "placeholder": "Date of publication"
            }),
            "full_text": Textarea(attrs={
                "class": "form-control",
                "placeholder": "Full Article"
            })
        }

class RegisterForm(UserCreationForm):
    email = forms.EmailField()

    class Meta:
        model = User
        fields = ["username", "email", "password1", "password2"]


from django.db import models

from gitmate_config.models import SettingsBase

class Settings(SettingsBase):
    pattern = models.TextField(
        default='(fix(e[sd])?|close[sd]?|resolve[sd]?) #[1-9][0-9]*',
        help_text='Pattern used on commit messages to identify bug fixes')
    hotspot_label = models.CharField(
        max_length=25,
        default='review carefully!',
        help_text='Label for pull requests with high risk')


bl_info = {
    "name": "QOL RingArray",
    "author": "Rico Holmes",
    "version": (1, 0, 5),
    "blender": (3, 3, 0),
    "location": "View3D",
    "description": "Create a quick ring array from selected object",
    "warning": "",
    "wiki_url": "",
    "category": "Interface",
    }

import bpy,math
from bpy.props import *
from copy import copy as copy
from .prefs import *
from .functions import *
from bpy.types import (Operator,Panel)

class QOL_RingArray(Operator):
    """Make a circular array of things"""
    bl_idname = "wm.qol_ringarray"
    bl_label = "QOL RingArray"
    bl_options = {'REGISTER', 'UNDO'}
    bl_description = "Make a circular array of things"
    bl_space_type = 'VIEW_3D'

    #add properties
    number_of_objects : IntProperty(name="Number of objects",default=6,min=1,max=1000)
    radius_of_circle : FloatProperty(name="Radius of circle",default=.2,min=0.0001)
    offset_angle : FloatProperty(name="Offset angle",default=0)
    apply_transform : BoolProperty(name="Apply transform",default=True)
    hub_axis : EnumProperty(name="Axis",items=[ ('X', "X", "X"),('Y', "Y", "Y"),('Z', "Z", "Z")],default = "Z",)
    delete_original : BoolProperty(name="Delete original",default=False)
    linked_data : BoolProperty(name="Linked data",default=True)
    create_parent : BoolProperty(name="Create parent",default=False)
    merge_objects : BoolProperty(name="Merge objects",default=False)
    autoAxis : BoolProperty(name="Auto axis",default=True)
    resize : FloatProperty(name="Resize",default=1,min=0.0001,max=10000)
    tx: FloatProperty(name="tx",default=0)
    ty: FloatProperty(name="ty",default=0)
    tz: FloatProperty(name="tz",default=0)
    ry: FloatProperty(name="ry",default=0)
    rx: FloatProperty(name="rx",default=0)
    rz: FloatProperty(name="rz",default=0)

    @classmethod
    def poll(cls, context):
        #check if there is an active object, check if we're not in edit mode
        return context.active_object is not None and context.mode == 'OBJECT'

    def invoke(self, context, event):
        ra_prefs = RHRingArray_get_preferences(bpy.context)
        self.number_of_objects = ra_prefs.count
        self.radius_of_circle = ra_prefs.radius
        self.apply_transform = ra_prefs.apply_transform
        self.delete_original = ra_prefs.delete_original
        self.linked_data = ra_prefs.linked_data
        self.create_parent = ra_prefs.create_parent
        self.merge_objects = ra_prefs.merge_objects
        self.autoAxis = ra_prefs.auto_axis
        return self.execute(context)

    def execute(self, context):
        selected_objects = bpy.context.selected_objects
        act_obj_ID = str(bpy.context.active_object.name)
        act_obj = bpy.data.objects.get(act_obj_ID)
        array_loc = act_obj.location

        if act_obj.data.users > 1:
            act_obj.data.user_clear()
        if self.apply_transform:
            bpy.ops.object.transform_apply(location=False, rotation=True, scale=True)
        number_of_objects = self.number_of_objects
        radius_of_circle = self.radius_of_circle
        delete_original = self.delete_original
        offset_angle = self.offset_angle
        if self.autoAxis:
            hub_axis = quantAxis(self,context)
        else:
            hub_axis = self.hub_axis
        if self.merge_objects:
            self.create_parent = False
            self.linked_data = False

        clearSelection = bpy.ops.object.select_all(action='DESELECT')
        CreatedObjects = createRingArray(self,context,act_obj,hub_axis,offset_angle,number_of_objects)

        if self.create_parent:
            HubRot = (0,0,0)
            if hub_axis == "X": HubRot = (0,0, math.radians(90))
            if hub_axis == "Y": HubRot = (0,0,0)
            if hub_axis == "Z": HubRot = (math.radians(90),0,0)
            bpy.ops.object.empty_add(type='CIRCLE',radius = radius_of_circle,rotation = HubRot, location=array_loc)
            ringArray = bpy.context.active_object
            ringArray.name = "ringArray"
            ringArray.hide_render = True

        clearSelection

        for obj in CreatedObjects:
            if self.create_parent:
                obj.select_set(True)
                bpy.ops.object.parent_set(type='OBJECT', keep_transform=True)
                clearSelection
                ringArray.select_set(True)
            else:
                obj.select_set(True)

        act_obj = bpy.data.objects.get(act_obj_ID)

        if act_obj and act_obj.type in {"MESH"}:
            if self.merge_objects:
                bpy.ops.object.join()
                merged_mesh = bpy.context.active_object
                cursor_loc = bpy.context.scene.cursor.location
                bpy.context.scene.cursor.location = array_loc
                bpy.ops.object.origin_set(type='ORIGIN_CURSOR', center='MEDIAN')
                bpy.context.scene.cursor.location = cursor_loc

            if len(selected_objects) == 2 and self.merge_objects:
                try:
                    tgtObj = selected_objects[0]
                    modifier = tgtObj.modifiers.new(name='RABool',type="BOOLEAN")
                    modifier.operation = "DIFFERENCE"
                    modifier.solver = "FAST"
                    modifier.object = merged_mesh
                    tgtObj.select_set(True)
                    bpy.context.view_layer.objects.active = tgtObj
                    bpy.ops.object.modifier_apply(modifier="RABool")
                    clearSelection
                    merged_mesh.select_set(True)
                    bpy.ops.object.delete()
                except:
                    pass

        if delete_original:
            bpy.ops.object.select_all(action='DESELECT')
            act_obj = bpy.data.objects.get(act_obj_ID)
            act_obj.select_set(True)
            if act_obj.data.users > 1:
                act_obj.data.user_clear()
            bpy.ops.object.delete()

        return {'FINISHED'}

    def draw(self, context):
        layout = self.layout
        box = layout.box()
        box.label(text="Main")
        row = box.row()
        row.prop(self, "radius_of_circle",text="radius")
        row.prop(self, "number_of_objects",text ="count")
        row = box.row()
        row.prop(self, "offset_angle", text ="offset")
        row.prop(self, "resize",text="resize")

        box.prop(self, "autoAxis",text="Auto axis")
        if not self.autoAxis:
            box.prop(self, "hub_axis")

        box = layout.box()
        box.label(text="Tweak")
        row = box.row()
        row.label(text="Rotate:")
        row.prop(self, "rx",text="X")
        row.prop(self, "ry",text="Y")
        row.prop(self, "rz",text="Z")

        box = layout.box()
        box.label(text="Options")
        row = box.row()
        row.prop(self, "apply_transform")
        row.prop(self, "delete_original")
        if not self.merge_objects:
            row = box.row()
            row.prop(self, "create_parent")
            row.prop(self, "linked_data")
        row = box.row()
        row.prop(self, "merge_objects")
        row.operator("wm.operator_defaults",text ="Reset")

class RINGARRAY_OT_resetPreferences(Operator):
    """ Reset Add-on Preferences """
    bl_idname = "ringarray.reset_preferences"
    bl_label = "Reset Properties and Settings"
    bl_options = {"INTERNAL"}

    def execute(self, context):
        rapreferences = context.preferences.addons[__name__].preferences
        rapreferences.property_unset("count")
        rapreferences.property_unset("radius")
        rapreferences.property_unset("create_parent")
        rapreferences.property_unset("linked_data")
        rapreferences.property_unset("merge_objects")
        rapreferences.property_unset("auto_axis")
        rapreferences.property_unset("delete_original")
        rapreferences.property_unset("apply_transform")
        return {'FINISHED'}

#create an npanel
class QOL_RingArrayPanel(Panel):
    bl_label = "QOL RingArray"
    bl_idname = "OBJECT_PT_qol_ringarray"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "QOL"

    def draw(self, context):
        ra_prefs = context.preferences.addons[__name__].preferences
        self.layout.operator("wm.qol_ringarray", text="QOL RingArray")
        self.layout.prop(ra_prefs, "count")

def draw(self, context):
    self.layout.operator("wm.qol_ringarray", text="QOL RingArray")

def register():
    bpy.utils.register_class(RH_RingArray_preferences)
    bpy.utils.register_class(RINGARRAY_OT_resetPreferences)
    bpy.utils.register_class(QOL_RingArrayPanel)
    bpy.utils.register_class(QOL_RingArray)

    bpy.types.VIEW3D_MT_object_context_menu.append(draw)

def unregister():
    bpy.utils.unregister_class(RH_RingArray_preferences)
    bpy.utils.unregister_class(RINGARRAY_OT_resetPreferences)
    bpy.utils.unregister_class(QOL_RingArrayPanel)
    bpy.utils.unregister_class(QOL_RingArray)
    bpy.types.VIEW3D_MT_object_context_menu.remove(draw)

if __name__ == "__main__":
    register()


import matplotlib.pyplot as plt
import numpy as np

# TIME DOMAIN
w = 20
t = np.arange(0.01, w*2+0.01, 0.01)

s = 8
tmp1 = (t-w)/s
sigma = 0.16
tmp2 = np.exp(-(tmp1**2)/(2.0*sigma**2))
psi = (1/s)*np.pi**(-1/4)*np.cos(2*np.pi*tmp1)*tmp2

plt.subplot(2,1,1)
plt.plot(t,psi)
plt.plot([0,w*2],[np.exp(-2),np.exp(-2)])
plt.plot([0,w*2],[-np.exp(-2),-np.exp(-2)])
plt.title('Morlet time-domain')

#FREQUENCY DOMAIN
w0 = 2*np.pi
x = np.arange(0, 10.01, 0.01)
fourier_wavelength = 4*np.pi / (w0 + np.sqrt(2 + w0**2))

tmp1 = s*(x/fourier_wavelength)*sigma - w0*sigma
tmp1 = -(tmp1**2)/2.0
psi = np.exp(tmp1)

plt.subplot(2,1,2)
plt.plot(x,psi)
plt.xlim([0, 2*np.pi])
plt.title('Morlet frequency-domain')

print(np.geomspace(0.2, 100, 5))
plt.show()

accs = np.load('EMODB_MODELS/sigma_range_test/sigma_accuries22-1.npy', allow_pickle=True)

print(accs)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 24 18:34:09 2019

@author: hamedniakan
"""

from fancyimpute import KNN
import pandas as pd

data_train = pd.read_csv('/wsu/home/gn/gn85/gn8525/GP_SALES/data_train.csv' )
data_test = pd.read_csv('/wsu/home/gn/gn85/gn8525/GP_SALES/data_test.csv')

data_train_knn = pd.DataFrame(KNN(k=6).fit_transform(np.array(data_train.iloc[:,1:])) , columns = data_train.columns[1:])
data_train_knn['id'] = data_train['id']
cols = data_train_knn.columns.tolist()
cols = cols[-1:] + cols[:-1]
data_train_knn = data_train_knn[cols]
data_train_knn.to_csv('/wsu/home/gn/gn85/gn8525/GP_SALES/data_train_knn.csv')

data_test_knn = pd.DataFrame(KNN(k=6).fit_transform(np.array(data_test.iloc[:,1:])) , columns = data_test.columns[1:])
data_test_knn['id'] = data_test['id']
cols = data_test_knn.columns.tolist()
cols = cols[-1:] + cols[:-1]
data_test_knn = data_test_knn[cols]
data_test_knn.to_csv('/wsu/home/gn/gn85/gn8525/GP_SALES/data_test_knn.csv')


from PyQt5 import uic
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from Pacijent import Pacijent  # ne može pronaci Pacijent
import sqlite3

class PretragaPacijenata(QWidget):
    def __init__(self,pacijenti):
        super().__init__()
        uic.loadUi("PretragaPacijenata.ui",self)

        self.pacijenti = pacijenti

        self.pretragaGumb.clicked.connect(self.pretrazi)
        self.obrisiGumb.clicked.connect(self.obrisi)
        self.dijagnozaGumb.clicked.connect(self.pretrazidijagnozu)
        self.natragGumb.clicked.connect(self.zatvoriMe)

    def __porukaProzor(self,poruka):
        porukaProzor = QMessageBox()
        porukaProzor.setIcon(QMessageBox.Warning)
        porukaProzor.setText(poruka)
        porukaProzor.setStandardButtons(QMessageBox.Ok)
        porukaProzor.exec()

    def __otkljucaj(self):
        self.imeText.setReadOnly(False)
        self.prezimeText.setReadOnly(False)
        self.oibText.setReadOnly(False)
        self.datumRodjenjaText.setReadOnly(False)
        self.adresaText.setReadOnly(False)

    def __zakljucaj(self):
        self.imeText.setReadOnly(True)
        self.prezimeText.setReadOnly(True)
        self.oibText.setReadOnly(True)
        self.datumRodjenjaText.setReadOnly(True)
        self.adresaText.setReadOnly(True)

    def pretrazi(self):
        try:
            rez = None
            with sqlite3.connect("BazaPacijenata2.db") as conn:
                suceljeZaNaredbe = conn.cursor()
                upit="SELECT Ime, Prezime, OIB, MBO,Datum_rodjenja, Adresa FROM Pacijenti WHERE Pacijenti.MBO="+self.mboText.text()+";"
                rez = suceljeZaNaredbe.execute(upit).fetchall()
            if rez is None or len(rez)==0:
                self.__porukaProzor("Pacijent s tim MBO-om NE POSTOJI!!")
            else:
                self.__otkljucaj()
                pacijentko = rez[0]
                self.imeText.setText(pacijentko[0])
                self.prezimeText.setText(pacijentko[1])
                self.oibText.setText(str(pacijentko[2]))
                self.datumRodjenjaText.setText(pacijentko[4])
                self.adresaText.setText(pacijentko[5])
                self.__zakljucaj()
        except:
            self.__porukaProzor("POGREŠAN UNOS ZA MBO!!")

    def obrisi(self):
        self.mboText.setText("")

    def pretrazidijagnozu(self):
        self.prozorZaPretragu = Pacijent("BazaPacijenata2.db")
        self.prozorZaPretragu.show()

    def zatvoriMe(self):
        self.close()


from selenium.webdriver.chrome.service import Service
from selenium import webdriver
import time

# webdriver chromeotions is to enable chrome options, then we can adjust browser option like pop notifications through add.argument
ops = webdriver.ChromeOptions()
ops.add_argument("--disable-notifications")

service_obj = Service(r"C:\Users\davud\Desktop\SeleniumD\chromedriver.exe")
driver = webdriver.Chrome(service=service_obj, options=ops)
driver.maximize_window()
driver.implicitly_wait(10)

driver.get("https://whatmylocation.com/show-current-address.htm")


import numpy as np
import xarray as xr
import matplotlib.pyplot as plt
import sys
sys.path.insert(1, './../')
import asym_funcs as af
import os

mydir = './output/ODEn800_'

myfiles = sorted([f for f in os.listdir('./forcingdata') if ('forcing' in f and '.nc' in f and ('insol' in f or 'SW' in f) and 'zonal' not in f)])
print(myfiles)
for myfile in myfiles:
    print(myfile)
    sol = xr.open_dataset('./forcingdata/'+myfile).S.values
    print(sol.shape)
    ds = af.simple_model(mysolar=sol)
    myname = myfile.split('_n8')[0][8:]
    print(myname)
    ds['names'] = myname
    ds = ds.set_coords('names')
    ds = ds.isel(year=slice(-20,None)).mean(dim='year')
    ds.to_netcdf(mydir+myname+'.nc')

sol = xr.open_dataset('forcingdata/forcing_TOAinsolation_n800.nc').S.values
hc = xr.open_dataset('forcingdata/forcing_ERA5_HC_n800.nc').HC.values
myname = 'TOAinsolationPLUSheatconv'
print(myname)
print(sol.shape, hc.shape)
ds = af.simple_model(mysolar=sol, forc_xt = hc)
ds['names'] = myname
ds = ds.set_coords('names')
ds = ds.isel(year=slice(-20,None)).mean(dim='year')
ds.to_netcdf(mydir+myname+'.nc')

sol = xr.open_dataset('forcingdata/forcing_TOAinsolation_n800.nc').S.values
hc = xr.open_dataset('forcingdata/forcing_CERES_SFC-LW-down_anom_n800.nc').L.values
myname = 'TOAinsolationPLUSanomLWCERES'
print(myname)
print(sol.shape, hc.shape)
ds = af.simple_model(mysolar=sol, forc_xt = hc)
ds['names'] = myname
ds = ds.set_coords('names')
ds = ds.isel(year=slice(-20,None)).mean(dim='year')
ds.to_netcdf(mydir+myname+'.nc')

for mld in [10,50,100,200]:
    cw = af.mld_to_cw(mld)
    myname = 'TOAinsolation_MLD'+str(mld)+'m'
    ds = af.simple_model(mysolar=sol, mycw = cw)
    ds['cw'] = cw
    ds['names'] = myname
    ds = ds.set_coords('names')
    ds = ds.set_coords('cw')
    ds = ds.isel(year=slice(-20,None)).mean(dim='year')
    ds.to_netcdf(mydir+myname+'.nc')

## Zonal variations in SW forcing
#sol = xr.open_dataset('forcingdata/forcing_CERES_SFC-SW-down_zonal-and-meridional_n800.nc')
#mydir = './output/zonal/ODEn800_'
#for lon in sol.lon.values:
#    myname = 'CERES_SFC-SW-down_lon'+str(lon)
#    ds = af.simple_model(mysolar=sol.S.sel(lon=lon).squeeze())
#    ds['lon'] = lon
#    ds['names'] = myname
#    ds = ds.set_coords('names')
#    ds = ds.set_coords('lon')
#    ds = ds.isel(year=slice(-20,None)).mean(dim='year')
#    ds.to_netcdf(mydir+myname+'.nc')


# from pudb import set_trace; set_trace()
from typing import List
import math
from collections import defaultdict

class Solution1:
    def findAllConcatenatedWordsInADict(self, words: List[str]) -> List[str]:
        """LeetCode 472

        We have to sort by length of each word, because this can guarantee that
        if a later word is a concatenation, it must be a concatenation of some
        of the previous words.

        My original idea is to use a trie for checking whether a word is a
        concat, but I realize that traversing the trie in the case of 'cat' and
        'cats' would require recursion, because we need to try 'cat' first and
        if it doesn't work, we need to try 'cats'. But all of this with trie
        can be complicated, and I don't want to use it as the first option.

        Then I realize that the max length of each word is at most 30. That
        means if we group the non-concat by their length, to check whether a
        word is a concat, we only need to go through at most 30 times of previous
        groups of non-concat. And if we make the group as a set, each check
        is O(1). Even if we have to go through the entire 30 times for each
        check, it is still not slow, because the total number of rounds is also
        limited.

        Hence, the solution.

        O(NlogN + K^3 * N), where N = len(words), K is the max length of a single
        word. Note that the dfs takes O(K^3). Recursion is O(K^2), for each
        recurison we perform string splicing, which is another O(K).

        572 ms, faster than 56.45%
        """
        words.sort(key=lambda w: len(w))
        m = defaultdict(set)

        def dfs(i: int, word: str) -> bool:
            if i == len(word):
                return True
            for length, nonconcat in m.items():
                if word[i:i + length] in nonconcat:
                    if dfs(i + length, word):
                        return True
            return False

        res = []
        for word in words:
            if dfs(0, word):
                res.append(word)
            else:
                m[len(word)].add(word)
        return res

class Solution2:
    def findAllConcatenatedWordsInADict(self, words: List[str]) -> List[str]:
        """DP solution. Almost the same as Solution1, but using a different
        perspective.

        For each word, we determine whether a prefix is a nonconcat. If it is,
        we through the remaining to the recurion again. DP happens when a
        remaining is already in a nonconcat or concat group, we don't have to
        process it again.

        O(NlogN, K^3 * N), 556 ms, faster than 57.10%
        """
        words.sort(key=lambda w: len(w))
        nonconcat = set()
        res = set()

        def is_concat(idx: int, word: int) -> bool:
            if word[idx:] in nonconcat or word[idx:] in res:
                return True
            if idx == len(word):
                return False
            for j in range(idx, len(word)):
                if word[idx:j + 1] in nonconcat and is_concat(j + 1, word):
                    return True
            return False

        for word in words:
            if is_concat(0, word):
                res.add(word)
            else:
                nonconcat.add(word)
        return list(res)

sol = Solution2()
tests = [
    (["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"], ["catsdogcats","dogcatsdog","ratcatdogcat"]),
    (["cat","dog","catdog"], ["catdog"]),
]

for i, (words, ans) in enumerate(tests):
    res = sol.findAllConcatenatedWordsInADict(words)
    res.sort()
    ans.sort()
    if res == ans:
        print(f'Test {i}: PASS')
    else:
        print(f'Test {i}; Fail. Ans: {ans}, Res: {res}')


import sys
from collections import deque

n = int(sys.stdin.readline())
m = int(sys.stdin.readline())

graph = [[0 for _ in range(n+1)] for _ in range(n+1)]
visit = [0 for _ in range(n+1)]
for i in range(m):
    a,b = map(int,sys.stdin.readline().split())
    graph[a][b] = 1
    graph[b][a] = 1

def bfs(x):
    queue = deque()
    queue.append(x)
    visit[x] = 1 # 시작 지점
    while queue:
        a = queue.popleft()
        for i in range(1,n+1):
            if visit[i] == 0 and graph[a][i] == 1:
                visit[i] = visit[a] + 1 # 친구사이인지 친구의 친구인지 아니면 그 너머인지 판별하기 위해
                queue.append(i)

bfs(1)
cnt = 0 # 결과값
for i in range(2,n+1):
    if visit[i] < 4 and visit[i] != 0: # 친구사이 : 2 친구의 친구사이 : 3
        cnt +=1
print(cnt)


from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

import base64
import json
import os
import subprocess
import sys

from googlecloudsdk.core import exceptions
from googlecloudsdk.core import log
from googlecloudsdk.core.credentials import store
from googlecloudsdk.core.docker import client_lib
from googlecloudsdk.core.docker import constants
from googlecloudsdk.core.util import files
import six

_USERNAME = 'gclouddockertoken'
_EMAIL = 'not@val.id'
_CREDENTIAL_STORE_KEY = 'credsStore'

class UnsupportedRegistryError(client_lib.DockerError):
  """Indicates an attempt to use an unsupported registry."""

  def __init__(self, image_url):
    self.image_url = image_url

  def __str__(self):
    return ('{0} is not in a supported registry.  Supported registries are '
            '{1}'.format(self.image_url, constants.ALL_SUPPORTED_REGISTRIES))

def DockerLogin(server, username, access_token):
  """Register the username / token for the given server on Docker's keyring."""

  # Sanitize and normalize the server input.
  parsed_url = client_lib.GetNormalizedURL(server)

  server = parsed_url.geturl()

  # 'docker login' must be used due to the change introduced in
  # https://github.com/docker/docker/pull/20107 .
  docker_args = ['login']
  docker_args.append('--username=' + username)
  docker_args.append('--password=' + access_token)
  docker_args.append(server)  # The auth endpoint must be the last argument.

  docker_p = client_lib.GetDockerProcess(
      docker_args,
      stdin_file=sys.stdin,
      stdout_file=subprocess.PIPE,
      stderr_file=subprocess.PIPE)

  # Wait for docker to finished executing and retrieve its stdout/stderr.
  stdoutdata, stderrdata = docker_p.communicate()

  if docker_p.returncode == 0:
    # If the login was successful, print only unexpected info.
    _SurfaceUnexpectedInfo(stdoutdata, stderrdata)
  else:
    # If the login failed, print everything.
    log.error('Docker CLI operation failed:')
    log.out.Print(stdoutdata)
    log.status.Print(stderrdata)
    raise client_lib.DockerError('Docker login failed.')

def _SurfaceUnexpectedInfo(stdoutdata, stderrdata):
  """Reads docker's output and surfaces unexpected lines.

  Docker's CLI has a certain amount of chattiness, even on successes.

  Args:
    stdoutdata: The raw data output from the pipe given to Popen as stdout.
    stderrdata: The raw data output from the pipe given to Popen as stderr.
  """

  # Split the outputs by lines.
  stdout = [s.strip() for s in stdoutdata.splitlines()]
  stderr = [s.strip() for s in stderrdata.splitlines()]

  for line in stdout:
    # Swallow 'Login Succeeded' and 'saved in,' surface any other std output.
    if (line != 'Login Succeeded') and (
        'login credentials saved in' not in line):
      line = '%s%s' % (line, os.linesep)
      log.out.Print(line)  # log.out => stdout

  for line in stderr:
    if not _IsExpectedErrorLine(line):
      line = '%s%s' % (line, os.linesep)
      log.status.Print(line)  # log.status => stderr

def _CredentialStoreConfigured():
  """Returns True if a credential store is specified in the docker config.

  Returns:
    True if a credential store is specified in the docker config.
    False if the config file does not exist or does not contain a
    'credsStore' key.
  """
  try:
    # Not Using DockerConfigInfo here to be backward compatiable with
    # UpdateDockerCredentials which should still work if Docker is not installed
    path, is_new_format = client_lib.GetDockerConfigPath()
    contents = client_lib.ReadConfigurationFile(path)
    if is_new_format:
      return _CREDENTIAL_STORE_KEY in contents
    else:
      # The old format is for Docker <1.7.0.
      # Older Docker clients (<1.11.0) don't support credential helpers.
      return False
  except IOError:
    # Config file doesn't exist.
    return False

def ReadDockerAuthConfig():
  """Retrieve the contents of the Docker authorization entry.

  NOTE: This is public only to facilitate testing.

  Returns:
    The map of authorizations used by docker.
  """
  # Not using DockerConfigInfo here to be backward compatible with
  # UpdateDockerCredentials which should still work if Docker is not installed
  path, new_format = client_lib.GetDockerConfigPath()
  structure = client_lib.ReadConfigurationFile(path)
  if new_format:
    return structure['auths'] if 'auths' in structure else {}
  else:
    return structure

def WriteDockerAuthConfig(structure):
  """Write out a complete set of Docker authorization entries.

  This is public only to facilitate testing.

  Args:
    structure: The dict of authorization mappings to write to the
               Docker configuration file.
  """
  # Not using DockerConfigInfo here to be backward compatible with
  # UpdateDockerCredentials which should still work if Docker is not installed
  path, is_new_format = client_lib.GetDockerConfigPath()
  contents = client_lib.ReadConfigurationFile(path)
  if is_new_format:
    full_cfg = contents
    full_cfg['auths'] = structure
    file_contents = json.dumps(full_cfg, indent=2)
  else:
    file_contents = json.dumps(structure, indent=2)
  files.WriteFileAtomically(path, file_contents)

def UpdateDockerCredentials(server, refresh=True):
  """Updates the docker config to have fresh credentials.

  This reads the current contents of Docker's keyring, and extends it with
  a fresh entry for the provided 'server', based on the active gcloud
  credential.  If a credential exists for 'server' this replaces it.

  Args:
    server: The hostname of the registry for which we're freshening
       the credential.
    refresh: Whether to force a token refresh on the active credential.

  Raises:
    core.credentials.exceptions.Error: There was an error loading the
      credentials.
  """

  if refresh:
    access_token = store.GetFreshAccessToken()
  else:
    access_token = store.GetAccessToken()

  if not access_token:
    raise exceptions.Error(
        'No access token could be obtained from the current credentials.')

  if _CredentialStoreConfigured():
    try:
      # Update the credentials stored by docker, passing the sentinel username
      # and access token.
      DockerLogin(server, _USERNAME, access_token)
    except client_lib.DockerError as e:
      # Only catch docker-not-found error
      if six.text_type(e) != client_lib.DOCKER_NOT_FOUND_ERROR:
        raise

      # Fall back to the previous manual .dockercfg manipulation
      # in order to support gcloud app's docker-binaryless use case.
      _UpdateDockerConfig(server, _USERNAME, access_token)
      log.warning(
          "'docker' was not discovered on the path. Credentials have been "
          'stored, but are not guaranteed to work with the Docker client '
          ' if an external credential store is configured.')
  else:
    _UpdateDockerConfig(server, _USERNAME, access_token)

def _UpdateDockerConfig(server, username, access_token):
  """Register the username / token for the given server on Docker's keyring."""

  # NOTE: using "docker login" doesn't work as they're quite strict on what
  # is allowed in username/password.
  try:
    dockercfg_contents = ReadDockerAuthConfig()
  except (IOError, client_lib.InvalidDockerConfigError):
    # If the file doesn't exist, start with an empty map.
    dockercfg_contents = {}

  # Add the entry for our server.
  auth = username + ':' + access_token
  auth = base64.b64encode(auth.encode('ascii')).decode('ascii')

  # Sanitize and normalize the server input.
  parsed_url = client_lib.GetNormalizedURL(server)

  server = parsed_url.geturl()
  server_unqualified = parsed_url.hostname

  # Clear out any unqualified stale entry for this server
  if server_unqualified in dockercfg_contents:
    del dockercfg_contents[server_unqualified]

  dockercfg_contents[server] = {'auth': auth, 'email': _EMAIL}

  WriteDockerAuthConfig(dockercfg_contents)

def _IsExpectedErrorLine(line):
  """Returns whether or not the given line was expected from the Docker client.

  Args:
    line: The line received in stderr from Docker
  Returns:
    True if the line was expected, False otherwise.
  """
  expected_line_substrs = [
      # --email is deprecated
      '--email',
      # login success
      'login credentials saved in',
      # Use stdin for passwords
      'WARNING! Using --password via the CLI is insecure. Use --password-stdin.'
  ]
  for expected_line_substr in expected_line_substrs:
    if expected_line_substr in line:
      return True
  return False


from jira import JIRA

def authentication(host, username, token):
    """"Authentication to Jira.
    Args:
        host: hostname of jira instance
        username: username to access jira
        token: password or token. Users who use social authentications can create a token form their front-ends
    """
    try:
        options = {'server': host}
        jira = JIRA(options, basic_auth=(username, token))
        return jira
    except Exception as e:
        return e

def ticket_search(service, query, maxresults=None):
    """"Search tickets
    Args:
        service: jira service created from authentication() function
        query: JQL to filter out tickets
        maxresults: maximum of number or outputs
    """
    try:
        tickets = service.search_issues(query, maxResults=maxresults)
        return tickets
    except Exception as e:
        return e

def get_ticket_attribute(service, ticket, attribute):
    """Obtain different ticket attributes

    Args:
        service: jira service created from authentication() function
        ticket: specific ticket number eg: PROD-1111
        attribute: diffrent elements in ticket eg: title, creator etc...

    Supported attributes: summary, description, assignee, created, status, priority, reporter, creator,
    issuelinks, watches, resolver, validator, fixedbinaryversion, targetststem, approver, fixVersions, comment
    """
    try:
        ticket = service.issue(ticket)
    except Exception as e:
        return e

    attribute_dict = {
        "title": ticket.fields.summary,
        "description": ticket.fields.description,
        "status": ticket.fields.status,
        "environment": ticket.fields.environment,
        "assignee": ticket.fields.assignee,
        "reporter": ticket.fields.reporter,
        "targetststem": ticket.fields.customfield_10201,
        "fixedbinaryversion": ticket.fields.customfield_10300,
        "approver": ticket.fields.customfield_11420,
        "validator": ticket.fields.customfield_11439,
        "resolver": ticket.fields.customfield_11442,
        "fixVersions": ticket.fields.fixVersions,
        "created": ticket.fields.created,
        "priority": ticket.fields.priority,
        "issuelinks": ticket.fields.issuelinks,
        "watches": ticket.fields.watches,
        "comment": ticket.fields.comment.comments,

    }

    if attribute in attribute_dict:
        try:
            return attribute_dict.get(attribute)
        except Exception as e:
            return e
    else:
        return "Field Name still not supported"

def create_ticket(service, project, summary, description, issuetype, assignee, attachments=[]):
    """"Create a jira ticket
    Args:
        service: jira service created from authentication() function
        project: the project the ticket needed to be created. eg: PROD, REQ, NATMOBILE
        summary: title of the ticket
        description: description or the ticket
        issuetype: type of the issue eg: Task, Bug
        assignee: the person the ticket should assigned eg: vidura.d
        attachments: images or other files needed to attached to the ticket. Should be a list
    """
    try:
        new_issue = service.create_issue(project=project,  summary=summary, issuetype={'name': issuetype},
                                         description=description, assignee={'name': assignee})
    except Exception as e:
        return e
    for i in attachments:
        service.add_attachment(issue=new_issue, attachment=i)

def update_ticket(service, ticket, field, value):
    """"Change specific attribute of a ticket

    Args:
        service: jira service created from authentication() function
        ticket: specific ticket number eg: PROD-1111
        field: the attribute needed to change
        value: new value of the attribute

    fields supported: summary, description, assignee, status, priority, environment
    """
    ticket = service.issue(ticket)
    if field == "assignee":
        try:
            ticket.update(assignee={'name': value})
        except Exception as e:
            return e
    elif field == "issuetype":
        try:
            ticket.update(issuetype={'name': value})
        except Exception as e:
            return e
    else:
        try:
            ticket.update(fields={field: value})
        except Exception as e:
            return e

def delete_ticket(service, ticket):
    """Change specific attribute of a ticket. You must have the sufficient permission to delete a ticket

    Args:
        service: jira service created from authentication() function
        ticket: specific ticket number eg: PROD-1111

    """
    ticket = service.issue(ticket)
    try:
        ticket.delete()
    except Exception as e:
        return e


# Day 5, Supply Stacks
from typing import List, Tuple, DefaultDict, NamedTuple
from collections import defaultdict

Init = DefaultDict[int, List[str]]

class Instruction(NamedTuple):
    quantity : int
    from_idx : int
    to_idx   : int

# Q1 & Q2
def run(init: Init, insts: List[Instruction], keep_order: bool) -> str:
    for quan, fr, to in insts:
        if keep_order: init[to]+=[init[fr].pop() for _ in range(quan)][::-1]
        else: init[to]+=[init[fr].pop() for _ in range(quan)]
    return ''.join([v[1][-1] for v in sorted(init.items(), key=lambda x: x[0])])

# Input
def parse_input(file: str) -> Tuple[List[str], List[str]]:
    with open(file, 'r') as inp:
        init, insts = inp.read().split('\n\n')
    return [line for line in init.split('\n')], [line.strip() for line in insts.split('\n') if line]

def parse_init(init: List[str]) -> Init:
    cols = defaultdict(list)
    for row in init[:-1]:
        for i in range((len(row)+1)//4):
            check = row[4*i:4*(i+1)].strip()
            if check: cols[i] = [check[1]] + cols[i]
    return cols

def parse_insts(insts: List[str]) -> List[Instruction]:
    return [Instruction(int(q), int(fr)-1, int(to)-1) for q, fr, to in [inst.split()[1::2] for inst in insts]]

if __name__ == '__main__':
    # Samples
    sample_input = parse_input('sample')
    sample_init, sample_insts = sample_input

    # Tests
    assert run(parse_init(sample_init), parse_insts(sample_insts), False) == 'CMZ'
    assert run(parse_init(sample_init), parse_insts(sample_insts), True)  == 'MCD'

    # Puzzle input
    puzzle_input = parse_input('puzzle-input')
    puzzle_init, puzzle_insts = puzzle_input

    # Results
    q1 = run(parse_init(puzzle_init), parse_insts(puzzle_insts), False)
    q2 = run(parse_init(puzzle_init), parse_insts(puzzle_insts), True)

    print(f'Q1: {q1}')
    print(f'Q2: {q2}')


####################################################################################
# Twitter Analyzer
# by JW
#
# A simple python tool to build queries and specify search tasks for different sources
#
# pages / Query_Builder.py
####################################################################################

# IMPORT STATEMENTS ----------------------------------------------------------------

import pandas as pd
import numpy as np
from datetime import datetime as dt
from math import floor
import jsonpickle
# import os

# Twitter crawling (possibly not needed here in future versions)
import tweepy

# Streamlit Web App
import streamlit as st

# Own Functions
from helpers.db_functions import *
from helpers.twitter_api import *

### STREAMLIT Init:

# SETTING PAGE CONFIG TO WIDE MODE AND ADDING A TITLE AND FAVICON
st.set_page_config(layout="wide", page_title="Twitter Analyzer", page_icon="🐦")

# Start of the page

st.title("🐦 Twitter Analyzer")

st.write("The gateway to analyze twitter queries")

# SIDEBAR --------------------------------------------

with st.sidebar:
    st.header("Project selection")

    list_of_projects = list_projects()
    df_list_of_projects = pd.DataFrame(list_of_projects)
    option = st.selectbox(
        'Project with Twitter data:',df_list_of_projects["id"])

    query_exists, db_search_params = get_twitter_query(option)

    if query_exists:
        st.metric(label="Query", value=db_search_params["query"])
        st.metric(label="Last crawled", value=str(db_search_params["last_crawl"]))
        st.metric(label="Tweet limit", value=db_search_params["tweet_limit"])
    else:
        st.error('Twitter query was not found.', icon="🚨")

if query_exists:
    if st.button("Run new crawl"):
        with st.spinner('Wait while we collect the tweets...'):
            try:
                #tweets = get_tweets(search_params_basic)
                twitter_client_v2 = st.session_state.twitter_client_v2
                tweets = twitter_client_v2.search_recent_tweets(query=db_search_params["query"], end_time=None, max_results=db_search_params["tweet_limit"], tweet_fields=st.session_state.twitter_tweet_fields, expansions="author_id")
                # for tweet in tweets.data:
                #     st.write(tweet)
                st.session_state.temp_tweets = tweets

                st.success('Done!')
            except Exception as e:
                st.error("There was an unexpected error while collecting the tweets. Please try again and double check your API credentials.")
                st.write(e)

    if st.button("Analyze"):
        temp_tweets_data = (st.session_state.temp_tweets).data
        temp_tweets_includes = (st.session_state.temp_tweets).includes
        users = {u["id"]: u for u in temp_tweets_includes['users']}
        # for tweet in temp_tweets.data:
        #     if users[tweet.author_id]:
        #         user = users[tweet.author_id]
        #         st.write(user)
        #         st.write(user.profile_image_url)

        for result in paginator(temp_tweets_data, 10, "tweet_next", "tweet_prev"):
            display_tweet(result, users)
            "---"


# -*- coding: utf-8 -*-
"""
Created on Tue Jul 12 12:35:33 2022

@author: chggo
"""

# Python program to bulk upload jpg image files as blobs to azure storage

import os
from azure.storage.blob import BlobServiceClient, BlobClient
from azure.storage.blob import ContentSettings, ContainerClient

MY_CONNECTION_STRING = ''

MY_IMAGE_CONTAINER = "unlabeled20220109"

# Replace with the local folder which contains the image files for upload
#LOCAL_IMAGE_PATH = r"C:\Users\chggo\data\Recut\icpLocDetection\labeledData\unlabeled_20220109\result"

class AzureBlobFileUploader:
  def __init__(self):
    print("Intializing AzureBlobFileUploader")

    # Initialize the connection to Azure storage account
    self.blob_service_client =  BlobServiceClient.from_connection_string(MY_CONNECTION_STRING)

  def upload_all_images_in_folder(self):
    # Get all files with jpg extension and exclude directories
    all_file_names = [f for f in os.listdir(LOCAL_IMAGE_PATH)
                    if os.path.isfile(os.path.join(LOCAL_IMAGE_PATH, f)) and ".png" in f]

    # Upload each file
    for file_name in all_file_names:
      self.upload_image(file_name)

  def upload_image(self,file_name):
    # Create blob with same name as local file name
    blob_client = self.blob_service_client.get_blob_client(container=MY_IMAGE_CONTAINER,
                                                          blob=file_name)
    # Get full path to the file
    upload_file_path = os.path.join(LOCAL_IMAGE_PATH, file_name)

    # Create blob on storage
    # Overwrite if it already exists!
    image_content_setting = ContentSettings(content_type='image/png')
    print(f"uploading file - {file_name}")
    with open(upload_file_path, "rb") as data:
      blob_client.upload_blob(data,overwrite=True,content_settings=image_content_setting)

# Initialize class and upload files
azure_blob_file_uploader = AzureBlobFileUploader()
azure_blob_file_uploader.upload_all_images_in_folder()


from flask import Blueprint, jsonify, request

from core import names, traits
from core.npc import NPC
from core.spell import parse_spells, get_spells_by_level
import json

npc = Blueprint('npc', __name__)
spells = None

@npc.route('/status')
def status():
    return jsonify({'status': 'ok'})

@npc.route('/npc_slack', methods=['GET', 'POST'])
def npc_gen_slack():
    data = request.form['text'].split(' ')
    caster_level = None
    race = data[0]
    gender = data[1]
    archtype = data[2]
    if len(data) > 3:
        trait_count = int(data[3])
    else:
        trait_count = 5
    if len(data) > 4:
        caster_level = int(data[4])

    spells = None
    if caster_level:
        full_spell_list = parse_spells()
        spells = get_spells_by_level(caster_level, full_spell_list)

    npc = NPC(
        archtype=archtype,
        gender=gender,
        race=race,
        name=names.generate_name(race, gender),
        traits=traits.get_traits_by_count_and_archtype(trait_count, archtype),
        spells=spells
    )
    headline = "*{race} {gender}*\nName: {first} {last}\n".format(race=npc.race, gender=npc.gender, first=npc.name['first'], last=npc.name['last'])
    trait_text = "Traits:\n"
    for trait in npc.traits:
        trait_text = trait_text + '\t' + trait + '\n'
    resp_text = headline + trait_text

    if spells:
        spell_text = "Spells:\n"
        for spell_level in spells.keys():
            spell_text = spell_text + '\tLevel ' + str(spell_level) + ':\n'
            for spell in spells[spell_level].keys():
                spell_text = spell_text + '\t\t' + spell + ':\n'
        resp_text += spell_text

    resp = {
        "response_type": "in_channel",
        "text": resp_text,
        "username": "SpinNPC",
        "mrkdwn": "true"
    }
    return jsonify(resp)

@npc.route('/npc', methods=['GET', 'POST'])
def npc_gen():

    full_spell_list = parse_spells()
    spells = None

    race = request.args.get('race')
    gender = request.args.get('gender')
    archtype = request.args.get('archtype')
    trait_count = int(request.args.get('traitCount')) if request.args.get('traitCount') else 5
    caster_level = int(request.args.get('caster')) if request.args.get('caster') else None
    if caster_level:
        spells = get_spells_by_level(caster_level, full_spell_list)

    npc = NPC(
        archtype=archtype,
        gender=gender,
        race=race,
        name=names.generate_name(race, gender),
        traits=traits.get_traits_by_count_and_archtype(trait_count, archtype),
        spells=spells
    )
    return jsonify(npc.__dict__)


"""add custom error config

Revision ID: 6a67c708208f
Revises: ad4dff9923c0
Create Date: 2020-08-11 16:57:36.626822

"""
from alembic import op
import sqlalchemy as sa
import sqlalchemy_utils
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "6a67c708208f"
down_revision = "ad4dff9923c0"
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "service_instance",
        sa.Column(
            "error_responses", postgresql.JSONB(astext_type=sa.Text()), nullable=True
        ),
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column("service_instance", "error_responses")
    # ### end Alembic commands ###


#подключаем библиотеки
import pygame
from pygame.draw import *

pygame.init()

FPS = 30
screen = pygame.display.set_mode((1000, 1000))

WHITE=(255, 255, 255)
GREY=(200, 200, 200)
YELLOW=(255, 255, 0)
BLACK=(0, 0, 0)
RED=(255, 0, 0)
GREEN=(128, 128, 0)
BROWN=(101, 67, 33)
LIGHTGREY=(220, 220, 220)
DARKGREY=(150, 150, 150)

screen.fill((200, 200, 200))
rect(screen, WHITE, (0, 450, 1000, 750))

def drawEskimos(x, y, width, height):
    """
    This function draws eskimos in clothes holding a stick whose center is in point (x, y)
    Whose height is counted from the bottom of the body to the top
    and whose width is counted from the left end of the body to its right end
    """
    ellipse(screen, GREEN, (x - width/2, y - height/2, width, 2*height))
    rect(screen, WHITE, (x - width/2, y + height/2, width, 2*height))
    ellipse(screen, GREEN, (x - width/3, y + 2*height/3, width/4, height/2))
    ellipse(screen, GREEN, (x + width/6, y + 2*height/3, width/4, height/2))
    ellipse(screen, GREEN, (x - width/2, y +  25*height/24, width/3, height/6))
    ellipse(screen, GREEN, (x + width/4, y +  25*height/24, width/3, height/6))
    rect(screen, BROWN, (x - width/2, y + height/2, width, height/3))
    rect(screen, BROWN, (x - width/12, y - height/2, width/6, height))
    ellipse(screen, LIGHTGREY, (x - 5*width/12, y - 11*height/12, 5*width/6, 2*height/3))
    circle(screen, DARKGREY, (x, y - 7*height/12), width/3)
    ellipse(screen, LIGHTGREY, (x - width/4, y - 5*height/6, width/2, height/2))
    line(screen, BLACK, [x - width/5, y - 2*height/3], [x - width/12, y - 7*height/12])
    line(screen, BLACK, [x + width/5, y - 2*height/3], [x + width/12, y - 7*height/12])
    lines(screen, BLACK, False, [(x - width/6, y - height/2), (x - width/12, y - 13*height/24), (x + width/12, y - 13*height/24), (x + width/6, y - height/2)])
    ellipse(screen, GREEN, (x - 3*width/4, y - height/6, 2*width/3, height/3))
    ellipse(screen, GREEN, (x + width/12, y - height/6, 2*width/3, height/3))
    line(screen, BLACK, [x - 2*width/3, y - 11*height/12], [x - 2*width/3, y + 7*height/6])

def drawcat(x, y, size):
    '''
    this function draws cat
    '''
    ellipse(screen, GREY, (x, y, 4*size, size)) #telo kota
    ellipse(screen, GREY, (x-size/10, y-size/2, 1.1*size, size/1.2)) #golova kota
    ellipse(screen, WHITE, (x-size/40, y-size/4, 0.36*size, 0.26*size)) #glaza kota
    ellipse(screen, WHITE, (x+size/2, y-size/4, 0.36*size, 0.26*size))
    ellipse(screen, BLACK, (x+size/10, y-size/5, 0.2*size, 0.1*size))
    ellipse(screen, BLACK, (x+size/1.55, y-size/5, 0.2*size, 0.1*size))
    arc(screen, BLACK, (x+size/4, y+size/15, size/2, size/2), -5.5, -4, 1) #rot kota
    surface = pygame.Surface((3*size,size)) #hvost
    surface.set_colorkey([0, 0, 0])
    ellipse(surface, GREY, (0, 0, 3*size, size/2))
    surface2 = pygame.transform.rotate(surface, 45)
    screen.blit(surface2, (x+size*3, y-1.5*size))

    surface = pygame.Surface((3*size,size)) #1 noga
    surface.set_colorkey([0, 0, 0])
    ellipse(surface, GREY, (0, 0, 1.5*size, 0.4*size))
    surface3 = pygame.transform.rotate(surface, 40)
    screen.blit(surface3, (x-0.5*size, y-0.5*size))

    surface = pygame.Surface((3*size,size)) #2 noga
    surface.set_colorkey([0, 0, 0])
    ellipse(surface, GREY, (0, 0, 1.5*size, 0.4*size))
    surface3 = pygame.transform.rotate(surface, -30)
    screen.blit(surface3, (x+2.4*size, y+0.7*size))

    polygon(screen, GREY, [(x+0*size, y-0.3*size), (x-0*size, y-0.5*size), (x+0.1*size, y-0.4*size)]) #yshi
    polygon(screen, GREY, [(x+1*size, y-0.2*size), (x+0.9*size, y-0.5*size), (x+0.8*size, y-0.4*size)])

def drawigly(x, y, size):
    '''
    This function draw igly
    '''
    surface = pygame.Surface((3*size,size))
    surface.set_colorkey([0, 0, 0])
    ellipse(surface, YELLOW, (0, 0, 3*size, 7*size))
    screen.blit(surface, (x-0.5*size, y-1*size))
    for i in range (size//15):
        line(screen, RED, [x+5*i, (y-10*i)], [x-5*i+2*size, (y-10*i)], 2)
    rect(screen, BLACK, (x+0.75*size, y-0.92*size, 0.5*size, 0.3*size), 5)

#draw iglys
drawigly(100, 700, 100)
drawigly(250, 600, 300)
drawigly(600, 500, 200)
drawigly(0, 450, 250)

# draw eskimoses
drawEskimos(600, 850, 80, 150)
drawEskimos(800, 600, 170, 190)
drawEskimos(450, 700, 140, 100)
drawEskimos(300, 900, 30, 50)

#draw cats
drawcat(100, 800, 50)
drawcat(200, 600, 30)
drawcat(300, 500, 40)
drawcat(20, 550, 20)

pygame.display.update()
clock = pygame.time.Clock()
finished = False

while not finished:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            finished = True

pygame.quit()


import os
import json
import argparse
from tqdm import tqdm

parser = argparse.ArgumentParser(description='bdd2coco')
parser.add_argument('--bdd_dir', type=str, default='C:/Users/Sergio Gil/Desktop/bdd100k')
cfg = parser.parse_args()

src_val_dir = os.path.join(cfg.bdd_dir, 'labels', 'bdd100k_labels_images_val.json')
src_train_dir = os.path.join(cfg.bdd_dir, 'labels', 'bdd100k_labels_images_train.json')

os.makedirs(os.path.join(cfg.bdd_dir, 'labels_coco'), exist_ok=True)

dst_val_dir = os.path.join(cfg.bdd_dir, 'labels_coco', 'bdd100k_labels_images_val_coco.json')
dst_train_dir = os.path.join(cfg.bdd_dir, 'labels_coco', 'bdd100k_labels_images_train_coco.json')

def bdd2coco_detection(labeled_images, save_dir):
  attr_dict = {"categories":
    [
      {"supercategory": "none", "id": 1, "name": "person"},
      {"supercategory": "none", "id": 2, "name": "car"},
      {"supercategory": "none", "id": 3, "name": "rider"},
      {"supercategory": "none", "id": 4, "name": "bus"},
      {"supercategory": "none", "id": 5, "name": "truck"},
      {"supercategory": "none", "id": 6, "name": "bike"},
      {"supercategory": "none", "id": 7, "name": "motor"},
      {"supercategory": "none", "id": 8, "name": "traffic light"},
      {"supercategory": "none", "id": 9, "name": "traffic sign"},
      # {"supercategory": "none", "id": 10, "name": "train"},
    ]}

  id_dict = {i['name']: i['id'] for i in attr_dict['categories']}

  images = list()
  annotations = list()
  ignore_categories = set()

  counter = 0
  for i in tqdm(labeled_images):
    counter += 1
    image = dict()
    image['file_name'] = i['name']
    image['height'] = 720
    image['width'] = 1280

    image['id'] = counter

    empty_image = True

    tmp = 0
    for l in i['labels']:
      annotation = dict()
      if l['category'] in id_dict.keys():
        tmp = 1
        empty_image = False
        annotation["iscrowd"] = 0
        annotation["image_id"] = image['id']
        x1 = l['box2d']['x1']
        y1 = l['box2d']['y1']
        x2 = l['box2d']['x2']
        y2 = l['box2d']['y2']
        annotation['bbox'] = [x1, y1, x2 - x1, y2 - y1]
        annotation['area'] = float((x2 - x1) * (y2 - y1))
        annotation['category_id'] = id_dict[l['category']]
        annotation['ignore'] = 0
        annotation['id'] = l['id']
        annotation['segmentation'] = [[x1, y1, x1, y2, x2, y2, x2, y1]]
        annotations.append(annotation)
      else:
        ignore_categories.add(l['category'])

    if empty_image:
      print('empty image!')
      continue
    if tmp == 1:
      images.append(image)

  attr_dict["images"] = images
  attr_dict["annotations"] = annotations
  attr_dict["type"] = "instances"

  print('ignored categories: ', ignore_categories)
  print('saving...')
  with open(save_dir, "w") as file:
    json.dump(attr_dict, file)
  print('Done.')

def main():
  # create BDD training set detections in COCO format
  print('Loading training set...')
  with open(src_train_dir) as f:
    train_labels = json.load(f)
  print('Converting training set...')
  bdd2coco_detection(train_labels, dst_train_dir)

  # create BDD validation set detections in COCO format
  print('Loading validation set...')
  with open(src_val_dir) as f:
    val_labels = json.load(f)
  print('Converting validation set...')
  bdd2coco_detection(val_labels, dst_val_dir)

if __name__ == '__main__':
  main()


# Date 2019/5/4

import requests
import re
import time

headers = {
    'User-Agent': 'python-requests/2.21.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'
}

def get_info(url):
    res = requests.get(url,headers)
    if res.status_code == 200:
        contents = re.findall('<p>(.*?)</p>',res.content.decode('utf-8'),re.S)
        for content in contents:
            print(content)
            with open('C:/GitHub/python-learning/workspace/20190423/doupo.txt','a+') as f:
                f.write(content+'\n')
    else:
        pass

if __name__ == '__main__':
    urls = ['http://www.doupoxs.com/doupocangqiong/{}.html'.format(str(i)) for i in range(2,3)]
    for url  in urls:
        get_info(url)
        time.sleep(1)


from datetime import datetime, timedelta
import os
from airflow.decorators import dag, task
from airflow.operators.dagrun_operator import TriggerDagRunOperator
from snow.snow_utils import copy_from_stage
from webscrapers.spider_utils import run_spider
from snow.snow_utils import copy_from_stage
from s3.s3_utils import archive_raw_data

# -------------- CONFIG
DAG_VERSION = "1.0.0"
JOBSITE = "cwjobs"
RAW_DATA_TABLE = f"{JOBSITE}_raw"

BUCKET_NAME = os.getenv("S3_RAW_DATA_BUCKET_NAME")
S3_SUBFOLDER = "raw_jobs_data"
# -------------- DAG
default_args = {
    "owner": "louis",
    "retries": 5,
    "provide_context": True,
}

@dag(
    dag_id=f"dag_get_{JOBSITE}",
    start_date=datetime(2023, 7, 18),
    schedule_interval=None,
    default_args=default_args,
    catchup=False,
    dagrun_timeout=timedelta(hours=1),
    tags=["testing", f"v{DAG_VERSION}"],
)
def dag():
    @task.python()
    def project_vars_to_xcom(**context):
        """Pushes variables used at various stages of the pipeline to Airflow
        xcoms for easy retrieval."""
        context["ti"].xcom_push(key="bucket_name", value=BUCKET_NAME)

    @task.python()
    def run_scrapy_spider(**context):
        todays_date = datetime.today().strftime("%Y-%m-%d")
        output_filename = f"{JOBSITE}_jobs_{todays_date}.json"
        run_spider(output_filename)
        context["ti"].xcom_push(
            key=f"saved_{JOBSITE}_filename_for_{context['run_id']}",
            value=output_filename,
        )

    @task.python()
    def copy_raw_data_from_stage(**context):
        target_file = context["ti"].xcom_pull(
            key=f"saved_{JOBSITE}_filename_for_{context['run_id']}"
        )
        copy_from_stage(filename=target_file, target_table=RAW_DATA_TABLE)

    @task.python()
    def archive(**context):
        """Archives imported data."""
        archive_raw_data(
            bucket_name=context["ti"].xcom_pull(key="bucket_name"),
            subfolder=S3_SUBFOLDER,
            target_file=context["ti"].xcom_pull(
                key=f"saved_{JOBSITE}_filename_for_{context['run_id']}"
            ),
        )

    trigger_next_dag = TriggerDagRunOperator(
        task_id="trigger_child_dag",
        trigger_dag_id="dag_run_dbt",
    )

    (
        project_vars_to_xcom()
        >> run_scrapy_spider()
        >> copy_raw_data_from_stage()
        >> archive()
        >> trigger_next_dag
    )

dag()


# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the BSD 3-Clause License  (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://opensource.org/licenses/BSD-3-Clause
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#--------------------------------------------------------------------
# modified from "ADVENT/advent/scripts/train.py" by Tuan-Hung Vu
#--------------------------------------------------------------------
import argparse
import os
import os.path as osp
import pprint
import random
import warnings

import numpy as np
import yaml
import torch
from torch.utils import data

from advent.model.deeplabv2 import get_deeplab_v2
from advent.dataset.gta5 import GTA5DataSet
from advent.dataset.cityscapes import CityscapesDataSet as CityscapesDataSet_hard
from cityscapes import CityscapesDataSet as CityscapesDataSet_easy
from advent.domain_adaptation.config import cfg, cfg_from_file
from train_UDA import train_domain_adaptation

warnings.filterwarnings("ignore", message="numpy.dtype size changed")
warnings.filterwarnings("ignore")

def get_arguments():
    """
    Parse input arguments
    """
    parser = argparse.ArgumentParser(description="Code for domain adaptation (DA) training")
    parser.add_argument('--cfg', type=str, default=None,
                        help='optional config file', )
    parser.add_argument("--random-train", action="store_true",
                        help="not fixing random seed.")
    parser.add_argument("--tensorboard", action="store_true",
                        help="visualize training loss with tensorboardX.")
    parser.add_argument("--viz_every_iter", type=int, default=None,
                        help="visualize results.")
    parser.add_argument("--exp-suffix", type=str, default=None,
                        help="optional experiment suffix")
    parser.add_argument('--rank', type=int, default=0)
    parser.add_argument('--device_type', type=str, default='npu')
    parser.add_argument('--device_id', type=int, )
    parser.add_argument('--world_size', type=int, default=1)
    parser.add_argument('--distributed', action='store_true', default=False)
    parser.add_argument('--performance_log', action='store_true', default=False)
    return parser.parse_args()

def main():
    # LOAD ARGS
    args = get_arguments()
    print('Called with args:')
    print(args)

    # pdb.set_trace()

    assert args.cfg is not None, 'Missing cfg file'
    cfg_from_file(args.cfg)
    cfg.distributed = args.distributed
    ddp_backend = "hccl" if args.device_type == "npu" else "nccl"

    if cfg.distributed:
        torch.distributed.init_process_group(backend=ddp_backend, world_size=args.world_size, rank=args.rank)
    device = torch.device("{}:{}".format(args.device_type ,args.device_id))
    cfg.device_id = args.device_id
    cfg.rank = args.rank
    cfg.world_size = args.world_size
    cfg.device_type = args.device_type
    cfg.performance_log = args.performance_log
    if args.device_type == 'cuda':
        torch.cuda.set_device(args.device_id)
    elif args.device_type == 'npu':
        torch.npu.set_device(args.device_id)

    cfg.is_master_node = args.world_size == 1 or args.device_id == 0

    # auto-generate exp name if not specified
    if cfg.EXP_NAME == '':
        cfg.EXP_NAME = f'{cfg.SOURCE}2{cfg.TARGET}_{cfg.TRAIN.MODEL}_{cfg.TRAIN.DA_METHOD}'

    if args.exp_suffix:
        cfg.EXP_NAME += f'_{args.exp_suffix}'
    # auto-generate snapshot path if not specified
    if cfg.TRAIN.SNAPSHOT_DIR == '':
        cfg.TRAIN.SNAPSHOT_DIR = osp.join(cfg.EXP_ROOT_SNAPSHOT, cfg.EXP_NAME)
        os.makedirs(cfg.TRAIN.SNAPSHOT_DIR, exist_ok=True)
    # tensorboard
    if args.tensorboard:
        if cfg.TRAIN.TENSORBOARD_LOGDIR == '':
            cfg.TRAIN.TENSORBOARD_LOGDIR = osp.join(cfg.EXP_ROOT_LOGS, 'tensorboard', cfg.EXP_NAME)
        os.makedirs(cfg.TRAIN.TENSORBOARD_LOGDIR, exist_ok=True)
        if args.viz_every_iter is not None:
            cfg.TRAIN.TENSORBOARD_VIZRATE = args.viz_every_iter
    else:
        cfg.TRAIN.TENSORBOARD_LOGDIR = ''
    if cfg.is_master_node:
        print('Using config:')
        pprint.pprint(cfg)

    # INIT
    _init_fn = None
    if not args.random_train:
        torch.manual_seed(cfg.TRAIN.RANDOM_SEED)
        if args.device_type == 'cuda':
            torch.cuda.manual_seed(cfg.TRAIN.RANDOM_SEED)
        elif args.device_type == 'npu':
            torch.npu.manual_seed(cfg.TRAIN.RANDOM_SEED)
        np.random.seed(cfg.TRAIN.RANDOM_SEED)
        random.seed(cfg.TRAIN.RANDOM_SEED)

        def _init_fn(worker_id):
            np.random.seed(cfg.TRAIN.RANDOM_SEED + worker_id)

    if os.environ.get('ADVENT_DRY_RUN', '0') == '1':
        return

    # LOAD SEGMENTATION NET
    assert osp.exists(cfg.TRAIN.RESTORE_FROM), f'Missing init model {cfg.TRAIN.RESTORE_FROM}'
    if cfg.TRAIN.MODEL == 'DeepLabv2':
        model = get_deeplab_v2(num_classes=cfg.NUM_CLASSES, multi_level=cfg.TRAIN.MULTI_LEVEL)
        saved_state_dict = torch.load(cfg.TRAIN.RESTORE_FROM)
        if 'DeepLab_resnet_pretrained_imagenet' in cfg.TRAIN.RESTORE_FROM:
            new_params = model.state_dict().copy()
            for i in saved_state_dict:
                i_parts = i.split('.')
                if not i_parts[1] == 'layer5':
                    new_params['.'.join(i_parts[1:])] = saved_state_dict[i]
            model.load_state_dict(new_params)
        else:
            model.load_state_dict(saved_state_dict)
    else:
        raise NotImplementedError(f"Not yet supported {cfg.TRAIN.MODEL}")
    print('Model loaded')

    # DATALOADERS
    # pdb.set_trace()
    easy_dataset = CityscapesDataSet_easy(root=cfg.DATA_DIRECTORY_SOURCE,
                                 list_path=cfg.DATA_LIST_SOURCE,
                                 max_iters=cfg.TRAIN.MAX_ITERS * cfg.TRAIN.BATCH_SIZE_SOURCE * args.world_size,
                                 crop_size=cfg.TRAIN.INPUT_SIZE_SOURCE,
                                 mean=cfg.TRAIN.IMG_MEAN)
    if cfg.distributed:
        easy_sampler = torch.utils.data.distributed.DistributedSampler(easy_dataset)
    easy_loader = data.DataLoader(easy_dataset,
                                    batch_size=cfg.TRAIN.BATCH_SIZE_SOURCE,
                                    num_workers=cfg.NUM_WORKERS,
                                    shuffle=(not cfg.distributed),
                                    pin_memory=False,
                                    sampler=easy_sampler if cfg.distributed else None,
                                    worker_init_fn=_init_fn)

    # pdb.set_trace()
    hard_dataset = CityscapesDataSet_hard(root=cfg.DATA_DIRECTORY_TARGET,
                                       list_path=cfg.DATA_LIST_TARGET,
                                       set=cfg.TRAIN.SET_TARGET,
                                       info_path=cfg.TRAIN.INFO_TARGET,
                                       max_iters=cfg.TRAIN.MAX_ITERS * cfg.TRAIN.BATCH_SIZE_TARGET * args.world_size,
                                       crop_size=cfg.TRAIN.INPUT_SIZE_TARGET,
                                       mean=cfg.TRAIN.IMG_MEAN)
    if cfg.distributed:
        hard_sampler = torch.utils.data.distributed.DistributedSampler(hard_dataset)
    hard_loader = data.DataLoader(hard_dataset,
                                    batch_size=cfg.TRAIN.BATCH_SIZE_TARGET,
                                    num_workers=cfg.NUM_WORKERS,
                                    shuffle=(not cfg.distributed),
                                    pin_memory=False,
                                    sampler=hard_sampler if cfg.distributed else None,
                                    worker_init_fn=_init_fn)

    with open(osp.join(cfg.TRAIN.SNAPSHOT_DIR, 'train_cfg.yml'), 'w') as yaml_file:
        yaml.dump(cfg, yaml_file, default_flow_style=False)

    # pdb.set_trace()
    train_domain_adaptation(model, easy_loader, hard_loader, device, cfg)

if __name__ == '__main__':
    os.environ['MASTER_ADDR'] = '127.0.0.1'
    os.environ['MASTER_PORT'] = '29688'
    main()


# https://www.cnblogs.com/wyzwyz/p/14038855.html

from sortedcontainers import SortedList

def slopTrick(ls):
    ql,qr = SortedList([]),SortedList([])
    n = len(ls)
    lenN = [ls[i][1]-ls[i][0] for i in range(n)]
    ql.add(ls[0][0])
    qr.add(ls[0][0])
    ans = 0
    addl,addr = 0,0
    for i in range(1,n):
        addl -= lenN[i]
        addr += lenN[i-1]
        L = ql[-1] + addl
        R = qr[0] + addr
        if ls[i][0]<L:
            ans += L-ls[i][0]
            ql.remove(ql[-1])
            ql.add(ls[i][0] - addl)
            ql.add(ls[i][0] - addl)
            qr.add(L -addr)
        elif ls[i][0] >R:
            ans += ls[i][0] - R
            qr.remove(qr[0])
            qr.add( ls[i][0] -addr)
            qr.add(ls[i][0] - addr)
            ql.add(R-addl)
        else:
            ql.add(ls[i][0] - addl)
            qr.add( ls[i][0]-addr)
        #print(ans,addl,addr,L,R)
    return ans


import datetime
import pytz
from win32com.client import Dispatch
from twilio.rest import Client
from datetime import datetime, timedelta
from calendar_ops import (
    get_calendar_by_name,
    create_calendar_event,
    get_definite_calendar,
    get_outlook_events_for_date,
    get_labor_calendar,
)
from labor_ops import (
    gather_labor_requirements,
    schedule_labor_for_event,
    generate_labor_event_details,
    get_labor_positions_and_counts,
    get_labor_times_for_days,
    generate_labor_days_list,
    select_labor_days,
    get_labor_times_for_day,
)

def main():
    labor_calendar = get_labor_calendar()
    if not labor_calendar:
        print("Unable to access the 'Labor' calendar in Outlook.")
        return

    # Fetch the 'Definite' calendar
    calendar = get_definite_calendar()
    if not calendar:
        print("Unable to access the 'Definite' calendar in Outlook.")
        return

    # Ask the user for the target date
    target_date_str = input(
        "Which date are you scheduling labor for? (format: YYYY-MM-DD): "
    )
    target_date = datetime.strptime(target_date_str, "%Y-%m-%d")

    # Fetch and display events from the calendar for that date
    events = get_outlook_events_for_date(target_date)
    if not events:
        print(f"No events found for {target_date_str}.")
        return

    if len(events) == 1:
        print(f"\nThe only event on {target_date_str} is '{events[0].Subject}'.")
        selected_event = events[0]
    else:
        # Display the events and let the user choose one
        print("\nAvailable events on " + target_date_str + ":")
        for idx, event in enumerate(events, 1):
            # Use date() method on the datetime object to display only the date
            start_date = event.Start.astimezone(pytz.timezone("America/Chicago")).date()
            end_date = event.End.astimezone(pytz.timezone("America/Chicago")).date()
            print(f"{idx}. {event.Subject} ({start_date} - {end_date})")

        # User specifies which event they're interested in
        event_choice = int(input("Which event are you scheduling labor for? "))
        selected_event = events[event_choice - 1]

    # Schedule labor for the chosen event
    schedule_labor_for_event(selected_event)

if __name__ == "__main__":
    main()


from math import sin
from prettytable import PrettyTable

def f(x_):
    return sin(x_) - x_**2/2

# x_ - что подставляем, x_k - что выкидываем
def l_kn(x_k, x_):
    w = w_dw(n+1, x_, x_k)
    dw = w_dw(n+1, x_k, x_k)
    return w/dw

def lagrange_polynom(n_, x_):
    ans = 0
    for x_k in selected_nodes[:n_+1]:
        ans += l_kn(x_k, x_) * all_nodes_table_dict[x_k]
    return ans

# k - до какой степени-1 идем, x_ - что подставляем, x_j - что выкидываем
def w_dw(k, x_, x_j):
    ans = 1
    for x_i in selected_nodes[:k]:
        if x_i != x_j:
            ans *= (x_ - x_i)
    return ans

#  n - степень-1 многочлена, x_ - что подставляем
def newton_polynom(k, x_):
    if k == 0:
        return A_k(0)
    ans = A_k(k)
    for i in range(k):
        x_i = selected_nodes[i]
        ans *= (x_ - x_i)
    return newton_polynom(k-1, x_) + ans

def A_k(k):
    ans = 0
    for j in range(k+1):
        x_j = selected_nodes[j]
        dw = w_dw(k+1, x_j, x_j)
        ans += all_nodes_table_dict[x_j]/dw
    return ans

# получаю n+1 узел, которые близко к заданному x_
def get_nodes_we_need(x_, n):
    nodes_we_need = []
    # сортирую в порядке отдаления x_i от x
    razn_temp = set()
    temp_dict = {}
    for node in nodes:
        razn = abs(x_ - node)
        razn_temp.add(razn)
        if razn in temp_dict.keys():
            temp_dict[razn].append(node)
        else:
            temp_dict[razn] = [node]
    razn_temp = list(razn_temp)
    razn_temp.sort()
    for t in razn_temp:
        for node in temp_dict[t]:
            nodes_we_need.append(node)
    return nodes_we_need[:n + 1]

def print_table(rows_list):
    table = PrettyTable(['i', 'X_i', 'f(X_i)'])
    i = 1
    for key in rows_list:
        table.add_row([i, key, all_nodes_table_dict[key]])
        i += 1
    print(table)

print('Задача алгебраического интерполирования')
print('Вариант 1')
print('Используются равноотстоящие узлы')
a, b = map(float, input('Введите через пробел начало и конец отрезка:').split())
m = int(input('Введите m, где m+1 - число значений в таблице:'))

h = (b - a)/m
nodes = [a + i*h for i in range(m + 1)]
all_nodes_table_dict = {x: f(x) for x in nodes}
print("Исходная таблица узел-значение")
print_table(nodes)

while True:
    n = int(input(f'Введите n - максимальную степень интерполяционного многочлена, n<={m}:'))
    while n > m:
        print('Некорректное значение n!')
        n = int(input(f'Введите n - максимальную степень интерполяционного многочлена, n<={m}:'))

    x = float(input('Введите, в какой точке посчитать функцию:'))
    selected_nodes = get_nodes_we_need(x, n)
    print("Отсортированная в порядке отдаления от x таблица узел-значение")
    print_table(selected_nodes)
    L_x = lagrange_polynom(n, x)
    print(f'Значение полинома Лагранжа L(x) в точке х = {x}:', L_x)
    print('Абсолютная фактическая погрешность |f(x)-L(x)| =', abs(f(x) - L_x))
    P_x = newton_polynom(n, x)
    print(f'Значение полинома Ньютона P(x) в точке х = {x}:', P_x)
    print('Абсолютная фактическая погрешность |f(x)-P(x)| =', abs(f(x) - P_x))
    flag = input('Хотите ввести новые значения? (y/n):')
    if flag == 'n':
        break


import os,requests,threading,shutil

#执行的线程数量
threadNum=5
#储存数据集的路径
dataDir="./data/imagenet/"
#ImageNet的数据集所在的位置
sourcePath="fall.txt"
with open("fall.txt","r") as fb:
    fall=fb.read()
    fallList=fall.split("\n")
    #计算每个线程需要处理的图片数量
    if (len(fallList) % threadNum) == 0:
        numsEach=len(fallList) / threadNum
        numsLast=numsEach
    else:
        numsEach=len(fallList) // threadNum
        numsLast=len(fallList) - (numsEach * (threadNum - 1))
#写入缓存文件
os.makedirs("tmp",exist_ok=True)
for i in range(0,threadNum):
    with open("tmp/"+str(i),"w+") as fb:
        if i == threadNum:
            begin = (i - 1) * numsEach
            end = (i - 1) * numsEach + numsLast
            fb.write(fallList[begin:end])
        else:
            begin = (i - 1) * numsEach
            end = (i * numsEach) - 1
            fb.write(fallList[begin:end])
#下载文件
for i in range(0,threadNum):
    with open("tmp/"+str(i),"r") as fb:
        for line in fb:
            if line:
                fallPart=line.split().split('\t')
                assert len(fallPart) == 2
                synset = fallPart[0][0: fallPart[0].index('_')]
                imgPath = os.path.join(dataDir,'images',synset)
                os.makedirs(imgPath,exist_ok=True)
                imgUrl = fallPart[1]
                imgFile=os.path.join(imgPath,"{}.jpg".format(fallPart[0]))
                try:
                    with open(imgFile,"wb") as f:
                        f.write(requests.get(imgUrl).content())
                except:
                    pass
shutil.rmtree("tmp")