from ..commons import logger, Species

from ..core.concept import AtlasConcept
from ..locations.location import Location
from ..locations.boundingbox import BoundingBox
from ..core.parcellation import Parcellation
from ..core.region import Region
from ..core.space import Space
from ..core.relation_qualification import Qualification as AssignmentQualification, RelationAssignment

from ..vocabularies import REGION_ALIASES

from typing import Union, List, Dict

AnatomicalAssignment = RelationAssignment[Union[Region, Location]]

class AnatomicalAnchor:
    """
    Anatomical anchor to an atlas region,
    a geometric primitive in an atlas reference space,
    or both.
    """

    _MATCH_MEMO: Dict[str, Dict[Region, AssignmentQualification]] = {}
    _MASK_MEMO = {}

    def __init__(self, species: Union[List[Species], Species, str], location: Location = None, region: Union[str, Region] = None):

        if isinstance(species, (str, Species)):
            self.species = {Species.decode(species)}
        elif isinstance(species, list):
            assert all(isinstance(_, Species) for _ in species)
            self.species = set(species)
        else:
            sp = Species.decode(species)
            if sp is None:
                raise ValueError(f"Invalid species specification: {species}")
            else:
                self.species = {sp}
        self._location_cached = location
        self._assignments: Dict[Union[AtlasConcept, Location], List[AnatomicalAssignment]] = {}
        self._last_matched_concept = None
        self._regions_cached = None
        self._regionspec = None
        if isinstance(region, Region):
            self._regions_cached = {region: AssignmentQualification.EXACT}
        elif isinstance(region, str):
            self._regionspec = region
        else:
            if region is not None:
                raise ValueError(f"Invalid region specification: {region}")
        self._aliases_cached = None

    @property
    def location(self):
        # allow to overwrite in derived classes
        return self._location_cached

    @property
    def parcellations(self) -> List[Parcellation]:
        return list({region.root for region in self.regions})

    @property
    def space(self) -> Space:
        # may be overriden by derived classes, e.g. in features.VolumeOfInterest
        if self.location is None:
            return None
        else:
            return self.location.space

    @property
    def region_aliases(self):
        if self._aliases_cached is None:
            self._aliases_cached: Dict[str, Dict[str, str]] = {
                Species.decode(species_str): region_alias_mapping
                for s in self.species
                for species_str, region_alias_mapping in REGION_ALIASES.get(str(s), {}).get(self._regionspec, {}).items()
            }
        return self._aliases_cached

    @property
    def has_region_aliases(self):
        return len(self.region_aliases) > 0

    @property
    def regions(self) -> Dict[Region, AssignmentQualification]:
        # decoding region strings is quite compute intensive, so we cache this at the class level
        if self._regions_cached is not None:
            return self._regions_cached

        if self._regionspec is None:
            self._regions_cached = {}
            return self._regions_cached

        if self._regionspec not in self.__class__._MATCH_MEMO:
            self._regions_cached = {}
            # decode the region specification into a set of region objects
            regions = {
                r: AssignmentQualification.EXACT
                for species in self.species
                for r in Parcellation.find_regions(self._regionspec)
                if r.species == species
            }
            # add more regions from possible aliases of the region spec
            for alt_species, aliases in self.region_aliases.items():
                for regionspec, qualificationspec in aliases.items():
                    for r in Parcellation.find_regions(regionspec):
                        if r.species != alt_species:
                            continue
                        if r not in self._regions_cached:
                            regions[r] = AssignmentQualification[qualificationspec.upper()]
            self.__class__._MATCH_MEMO[self._regionspec] = regions
        self._regions_cached = self.__class__._MATCH_MEMO[self._regionspec]

        return self._regions_cached

    def __str__(self):
        region = "" if self._regionspec is None else str(self._regionspec)
        location = "" if self.location is None else str(self.location)
        separator = " " if min(len(region), len(location)) > 0 else ""
        return region + separator + location

    def __repr__(self):
        return self.__str__()

    def assign(self, concept: AtlasConcept):
        """
        Match this anchoring to an atlas concept.
        """
        if concept not in self._assignments:
            matches: List[AnatomicalAssignment] = []
            if isinstance(concept, Space):
                if self.space == concept:
                    matches.append(
                        AnatomicalAssignment(self.space, concept, AssignmentQualification.EXACT)
                    )
            elif isinstance(concept, Region):
                if concept.species in self.species:
                    if any(_.matches(self._regionspec) for _ in concept) \
                            or self.has_region_aliases:  # dramatic speedup, since decoding _regionspec is expensive
                        for r in self.regions:
                            matches.append(AnatomicalAnchor.match_regions(r, concept))
                    if len(concept.root.find(self._regionspec)) == 0:
                        # We perform the (quite expensive) location-to-region test
                        # only if this anchor's regionspec is not known to the
                        # parcellation of the query region. Otherwise we can rely
                        # on the region-to-region test.
                        if self.location is not None:
                            matches.append(AnatomicalAnchor.match_location_to_region(self.location, concept))
            elif isinstance(concept, Location):
                if self.location is not None:
                    matches.append(AnatomicalAnchor.match_locations(self.location, concept))
                for region in self.regions:
                    match = AnatomicalAnchor.match_location_to_region(concept, region)
                    matches.append(None if match is None else match.invert())
            self._assignments[concept] = sorted(m for m in matches if m is not None)

        self._last_matched_concept = concept \
            if len(self._assignments[concept]) > 0 \
            else None

        return self._assignments[concept]

    def matches(self, concept: AtlasConcept):
        return len(self.assign(concept)) > 0

    @classmethod
    def match_locations(cls, location1: Location, location2: Location):
        assert all(isinstance(loc, Location) for loc in [location1, location2])
        if (location1, location2) not in cls._MATCH_MEMO:
            if location1 == location2:
                res = AnatomicalAssignment(location1, location2, AssignmentQualification.EXACT)
            elif location1.contained_in(location2):
                res = AnatomicalAssignment(location1, location2, AssignmentQualification.CONTAINED)
            elif location1.contains(location2):
                res = AnatomicalAssignment(location1, location2, AssignmentQualification.CONTAINS)
            elif location1.intersects(location2):
                res = AnatomicalAssignment(location1, location2, AssignmentQualification.OVERLAPS)
            else:
                res = None
            cls._MATCH_MEMO[location1, location2] = res
        return cls._MATCH_MEMO[location1, location2]

    @classmethod
    def match_regions(cls, region1: Region, region2: Region):
        assert all(isinstance(r, Region) for r in [region1, region2])
        if (region1, region2) not in cls._MATCH_MEMO:
            if region1 == region2:
                res = AnatomicalAssignment(region1, region2, AssignmentQualification.EXACT)
            elif region1 in region2:
                res = AnatomicalAssignment(region1, region2, AssignmentQualification.CONTAINED)
            elif region2 in region1:
                res = AnatomicalAssignment(region1, region2, AssignmentQualification.CONTAINS)
            else:
                res = None
            cls._MATCH_MEMO[region1, region2] = res
        return cls._MATCH_MEMO[region1, region2]

    @classmethod
    def match_location_to_region(cls, location: Location, region: Region):
        assert isinstance(location, Location)
        assert isinstance(region, Region)

        for subregion in region.children:
            res = cls.match_location_to_region(location, subregion)
            if res is not None:
                return res

        if (location, region) not in cls._MATCH_MEMO:
            # compute mask of the region
            mask = None
            if region.mapped_in_space(location.space, recurse=False):
                if (region, location.space) not in cls._MASK_MEMO:
                    cls._MASK_MEMO[region, location.space] = \
                        region.fetch_regional_map(space=location.space, maptype='labelled')
                mask = cls._MASK_MEMO[region, location.space]
                mask_space = location.space
                expl = (
                    f"{location} was compared with the mask of query region "
                    f"'{region.name}' in {location.space.name}."
                )
            else:
                for space in region.supported_spaces:
                    if not region.mapped_in_space(space, recurse=False):
                        continue
                    if not space.provides_image:  # siibra does not yet match locations to surface spaces
                        continue
                    if (region, space) not in cls._MASK_MEMO:
                        cls._MASK_MEMO[region, space] = region.fetch_regional_map(space=space, maptype='labelled')
                    mask = cls._MASK_MEMO[region, space]
                    mask_space = space
                    if location.space == mask_space:
                        expl = (
                            f"{location} was compared with the mask of query region '{region.name}' "
                            f"in {mask_space}."
                        )
                    else:
                        expl = (
                            f"{location} was warped from {location.space.name} and then compared "
                            f"in this space with the mask of query region '{region.name}'."
                        )
                    if mask is not None:
                        break

            if mask is None:
                logger.debug(
                    f"'{region.name}' provides no mask in a space "
                    f"to which {location} can be warped."
                )
                res = None
            else:
                # compare mask to location
                loc_warped = location.warp(mask_space)
                if loc_warped is None:
                    # seems we cannot warp our location to the mask space
                    # this typically happens when the location extends outside
                    # the brain. We might still be able the warp the
                    # bounding box of the mask to the location and check.
                    # TODO in fact we should estimate an affine matrix from the warped bounding box,
                    # and resample the mask for the inverse test to be more precise.
                    bbox_mask = BoundingBox.from_image(mask, mask_space).warp(location.space)
                    return cls.match_locations(location, bbox_mask)
                elif loc_warped.contained_in(mask):
                    res = AnatomicalAssignment(location, region, AssignmentQualification.CONTAINED, expl)
                elif loc_warped.contains(mask):
                    res = AnatomicalAssignment(location, region, AssignmentQualification.CONTAINS, expl)
                elif loc_warped.intersects(mask):
                    res = AnatomicalAssignment(location, region, AssignmentQualification.OVERLAPS, expl)
                else:
                    logger.debug(
                        f"{location} does not match mask of '{region.name}' in {mask_space.name}."
                    )
                    res = None
            cls._MATCH_MEMO[location, region] = res

        # keep mask cache small
        if len(cls._MASK_MEMO) > 3:
            # from Python 3.6, this remove the *oldest* entry
            cls._MASK_MEMO.pop(next(iter(cls._MASK_MEMO)))

        return cls._MATCH_MEMO[location, region]

    def represented_parcellations(self):
        """
        Return any parcellation objects that this anchor explicitly points to.
        """
        return [
            r for r in self.regions
            if isinstance(r, Parcellation)
        ]

    @property
    def last_match_result(self) -> List[AnatomicalAssignment]:
        return self._assignments.get(self._last_matched_concept, [])

    @property
    def last_match_description(self) -> str:
        if self.last_match_result is None:
            return ""
        else:
            return ' and '.join({str(_) for _ in self.last_match_result})


import os
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
matplotlib.use('TkAgg')

if __name__ == '__main__':
    files = os.listdir("differentMDOperatorsResults")
    print(files)
    dfs = {}
    outliers = {}
    for file in files:
        df = pd.read_csv(f"differentMDOperatorsResults/{file}", delimiter=",")
        dfs[file.replace('.csv','')] = df
        all_averages = []
        total_mutants = 0
        total_methods = 0
        for idx, row in df.iterrows():
            sloc = row['Source Lines of Code']
            md = row["Mutant Density"]
            if sloc > 99:
                continue
            # all_diffs.append(sloc-md)
            all_averages.append(md / sloc)
            total_methods += 1
            total_mutants += md

        mean = np.mean(all_averages)
        std = np.std(all_averages)
        current_outliers = []
        for idx, row in df.iterrows():
            sloc = row['Source Lines of Code']
            md = row["Mutant Density"]
            average = md/sloc
            if sloc > (mean+std*2) or sloc < (mean-std*2):
                current_outliers.append(f"{row['File']};{row['Method Name']}")
        outliers[file.replace('.csv','')] = current_outliers
        # sns.displot(all_averages, kde=True).set(title='opencv distribution plot of average MD : ' + file.replace('.csv',''))
        # plt.xlim(-50, 50)
        # plt.show()
        # print(f"Mutations: {file.replace('.csv','')}, Methods: {total_methods}, Total Mutants: {total_mutants}")
        print(f"Mutations: {file.replace('.csv','')}, Mean: {mean}, Standard deviation: {std}")

    temp = []
    keys = []
    for key, val in outliers.items():
        temp.append(set(val))
        keys.append(key)

    results = set.intersection(*map(set, temp))
    print("------- Outliers over all sets ---------")
    print(len(results))
    print("------ Single mutation outliers --------")
    for index, list in enumerate(temp):

        for index2, second_list in enumerate(temp):
            if index != index2:
                list = list.difference(second_list)
        print(f"{keys[index]} : {list}")


#!/usr/bin/env python3

def round(speed,text,result):
#speed~typingSpeed #text~typingText #result~result

        #getting the player ready
        uselessVariable = input("Below are some quotes from a TV Show.\nHit the keys right and you get the flag.\nYou only have {} seconds max for each quote to give your shot.\nIf you score 5 points, you win.\n   you lose twice, you're out.\n   you are lucky, you win in the first 5 tries\nElse, have a Good Luck.\nHit Enter when READY.\n".format(speed))

        #starting the timer
        beginTime = default_timer()

        #Text to be typed
        playerInput = input("{}\n".format(text)) #\n to avoid side-by-side typing

        #stopping the timer after enter is pressed
        endTime = default_timer()
        totalTime = endTime - beginTime

        #scores
        if totalTime <=speed and playerInput == text:
                print("\nGood Going! A few more to go.")
                result[0] = result[0] + 1

        else:
                print("You need a lot of practice.\n")
                result[1] = result[1] + 1

        #final Output
        return result

#### Main fn from here.
from timeit import default_timer
import random #random library to generate random numbers

#scoreboard
result = [0,0]

list = ['We are Fun Society.', 'Your privacy has been deleted.', 'You hack people, I hack time.', 'The 1% Of The 1%', 'Are you a 1 or a Zero?', 'When we lose, we invite chaos.', 'We live in a kingdom of bullshit.', 'Annihilation is always the answer.'] #text to be typed

#game loop
while True:
        if result[0] == 5 or result[1] == 2:
                if result[0] ==  5:
                        print("Good Job Kiddo!")
                        print("Here you go:\nflag{f4st_typ1ng_1s_th3_k3y}")
                if result[1] == 2:
                        print("Shoooo away Kiddo!\nYou lost.")
                break
        else:
                typingSpeed = random.randint(8,9) #time enter the texts
                a = random.randint(0,7)
                typingText = list[a] #copying the random int a from the list

                result = round(typingSpeed, typingText, result) #function calling the game round
                print("Points Won: {}".format(result[0]))
                print("Points Lost: {}\n".format(result[1]))


from pathlib import Path
import shutil

import pandas as pd
import torch
import torchaudio
import wget

URL = 'http://download.tensorflow.org/data/speech_commands_v0.01.tar.gz'

class SpeechCommandsDataset(torch.utils.data.Dataset):
    def __init__(
        self,
        path2dir,
        transform=None,
        keywords=None,
        csv=None
    ):
        if keywords is None and csv is None:
            raise ValueError('You must specify either keywords or csv')

        self.transform = transform

        path2dir = Path(path2dir)
        self.path2dir = path2dir
        if not path2dir.exists() or len(list(path2dir.iterdir())) == 0:
            self._download(path2dir)

        if csv is None:
            keywords = keywords if isinstance(keywords, list) else [keywords]

            all_keywords = [
                p.stem for p in path2dir.glob('*')
                if p.is_dir() and not p.stem.startswith('_')
            ]

            triplets = []
            for keyword in all_keywords:
                paths = (path2dir / keyword).rglob('*.wav')
                if keyword in keywords:
                    for path2wav in paths:
                        triplets.append((str(path2wav.relative_to(path2dir)),
                                         keyword, 1))
                else:
                    for path2wav in paths:
                        triplets.append((str(path2wav.relative_to(path2dir)),
                                         keyword, 0))

            self.csv = pd.DataFrame(
                triplets,
                columns=['path', 'keyword', 'label']
            )
        else:
            self.csv = csv

        self.csv.sort_values(by='path', inplace=True, ignore_index=True)

    def __getitem__(self, index: int):
        instance = self.csv.iloc[index]

        path2wav = self.path2dir / instance['path']
        wav, sr = torchaudio.load(path2wav)
        wav = wav.sum(dim=0)

        if self.transform:
            wav = self.transform(wav)

        return {
            'wav': wav,
            'keywors': instance['keyword'],
            'label': instance['label']
        }

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

    def train_val_split(self, train_frac, augs=None):
        indices = torch.randperm(len(self))
        train_sz = int(len(self) * train_frac)
        train_indices = indices[:train_sz]
        val_indices = indices[train_sz:]

        train_df = self.csv.iloc[train_indices].reset_index(drop=True)
        val_df = self.csv.iloc[val_indices].reset_index(drop=True)
        train_set = SpeechCommandsDataset(self.path2dir, csv=train_df,
                                          transform=augs)
        val_set = SpeechCommandsDataset(self.path2dir, csv=val_df)
        return train_set, val_set

    def _download(self, path2dir: Path):
        path2dir.mkdir(parents=True, exist_ok=True)
        archive_path = path2dir / 'archive.tar.gz'
        if archive_path.exists():
            raise RuntimeError('archive.tar.gz exists in root directory')
        wget.download(URL, str(archive_path))
        shutil.unpack_archive(str(archive_path), str(path2dir))
        archive_path.unlink()


import stage
import game
import theme
import curses

screen = None

def drawTile(x, y, tile='', color=None):
    color = color or theme.get_color('default')

    x = x * 2 + stage.padding[3] * 2 + stage.width / 2
    y += stage.padding[0] + stage.height / 2

    screen.addstr(y, x, tile, color)
    if (len(tile) < 2):
        screen.addstr(y, x + 1, tile, color)

def drawGameOver():
    drawTile(-4, -1, " GAME OVER ", theme.get_color('border'))
    drawTile(-7, 1, " Press ENTER to restart ", theme.get_color('border'))

def drawScore():
    score_formatted = str(game.score).zfill(2)
    drawTile(
        (stage.width / 2) - 1,
        (-stage.height / 2) - 1,
        score_formatted,
        theme.get_color('border')
        )

def drawLives():
    posx = (-stage.width / 2) + 3
    for x in xrange(1, game.lives + 1):
        posx += 1
        drawTile(
            posx,
            (-stage.height / 2) - 1,
            theme.get_tile('lives'),
            theme.get_color('lives')
            )
        posx += 1
        drawTile(
            posx,
            (-stage.height / 2) - 1,
            theme.get_tile('border-h'),
            theme.get_color('border')
            )

def drawSnake():
    for part in game.snake:
        drawTile(
            part[0],
            part[1],
            theme.get_tile('snake-body'),
            theme.get_color('snake')
            )
    # Clean last tile
    drawTile(
        game.lastPos[0],
        game.lastPos[1],
        theme.get_tile('bg'),
        theme.get_color('bg')
        )

def drawApples():
    for apple in game.apples:
        drawTile(
            apple[0],
            apple[1],
            theme.get_tile('apple'),
            theme.get_color('apple')
            )

def drawGame():
    for y in range(stage.boundaries['top'], stage.boundaries['bottom']):
        for x in range(stage.boundaries['left'], stage.boundaries['right']):
            drawTile(x, y, theme.get_tile('bg'), theme.get_color('bg'))
    drawBorders()
    drawText()

def drawBorders():
    tile_v = theme.get_tile('border-v')
    tile_h = theme.get_tile('border-h')
    tile_c = theme.get_tile('border-c')
    color = theme.get_color('border')

    x_left = stage.boundaries['left']
    x_right = stage.boundaries['right']

    y_top = stage.boundaries['top']
    y_bottom = stage.boundaries['bottom']

    for y in range(y_top, y_bottom):
        drawTile(x_left - 1, y, tile_v, color)
        drawTile(x_right, y, tile_v, color)

    for x in range(x_left, x_right):
        drawTile(x, y_top - 1, tile_h, color)
        drawTile(x, y_bottom, tile_h, color)

    drawTile(x_left - 1, y_top - 1, tile_c, color)
    drawTile(x_left - 1, y_bottom, tile_c, color)
    drawTile(x_right, y_top - 1, tile_c, color)
    drawTile(x_right, y_bottom, tile_c, color)

def drawText():
    color = theme.get_color('border')
    drawTile((stage.width / 2) - 4, (-stage.height / 2) - 1, "score:", color)
    drawTile((-stage.width / 2), (-stage.height / 2) - 1, "lives:", color)
    drawTile(-5, (stage.height / 2), " Press Q to quit ", color)

def update():

    drawSnake()
    drawApples()
    drawScore()
    drawLives()

def init():
    global screen

    screen = curses.initscr()
    curses.noecho()
    curses.cbreak()
    curses.curs_set(0)
    curses.start_color()
    screen.nodelay(1)

def exit():
    screen.clear()
    screen.keypad(0)
    curses.echo()
    curses.nocbreak()
    curses.endwin()


from __future__ import unicode_literals
from django.utils.encoding import smart_text
from django.db import models
import pdb

from .. import User

class Foup(models.Model):
    foupname=models.CharField(max_length=16, verbose_name='Foup Name',primary_key=True)
    note=models.CharField(max_length=200,null=True,blank=True)
    owner=models.ForeignKey(User,on_delete=models.SET_NULL,null=True,blank=True)
    timestamp=models.DateTimeField(auto_now_add=True,auto_now=False)    #creation time
    updated=models.DateTimeField(auto_now_add=False,auto_now=True)  #update time
    def __str__(self):
        return self.foupname

    @classmethod
    def create(cls,foupname,note,owner):
        #for foup in cls.objects.all():
        foup=cls(foupname=foupname,note=note,owner=owner)
        foup.save()
        for i in range(1,26):
            foup_slot=foup.foup_slot_set.create(slot=i)
            foup_slot.save()

        return foup
    def delete(self):
        #delete a foup from database
        for foup_slot in self.foup_slot_set.all():
            if foup_slot.wafer!=None:
                foup_slot.wafer.isused='Used'
            foup_slot.delete()
        self.delete()
        return

    def show_slots(self):
        #show the 25 slots of the foup, whether it is vacant, whether the contained wafer is new or used
        for i in range(1,26):
            foup_slot=self.foup_slot_set.filter(slot=i)[0]
            if foup_slot.wafer==None:
                print(self.foupname+'\t'+'S'+str(i)+'\t'+'Vacant')
            else:
                print(self.foupname+'\t'+'S'+str(i)+'\t'+foup_slot.wafer.isused)
        return

class Wafer(models.Model):
    waferType=(
        ('PT', 'Particle Grade Wafer'),
        ('LR', 'Low Res Particle Grade Wafer'),
        ('MC', 'Mechanical Grade Wafer'),
        ('ER', 'External Reclaimed Wafer'),
        ('IR', 'Internal Reclaimed Wafer'),
    )

    used_choices=(
        ('N','New'),
        ('U','Used'),
    )
    isUsed=models.CharField(max_length=10,choices=used_choices)

    wafertype=models.CharField(max_length=20,choices=waferType,default='Internal Reclaimed Wafer')
    note=models.CharField(max_length=200,null=True,blank=True)
    timestamp=models.DateTimeField(auto_now_add=True,auto_now=False)    #creation time
    updated=models.DateTimeField(auto_now_add=False,auto_now=True)  #update time
    def __str__(self):
        return smart_text(self.wafertype+' '+self.isUsed)

class Foup_slot(models.Model):
    foup=models.ForeignKey(Foup,on_delete=models.CASCADE)
    wafer=models.OneToOneField(Wafer,on_delete=models.SET_NULL,blank=True,null=True)    #setting on_delete=models.SET_NULL, means at times of deleting a wafer instance, the system sets this field to NULL
    slot=models.IntegerField(default=0)
    note=models.CharField(max_length=200,null=True,blank=True)
    updated=models.DateTimeField(auto_now_add=False,auto_now=True)  #update time
    def __str__(self):
        foup_name=self.foup.foupname
        s=foup_name+' S'+str(self.slot)+'\t'
        status=''
        if self.wafer is None:
            status='Vacant'
        else:
            status='Occupied\t'+'Wafer:\t'+self.wafer.isUsed
        return s+status
    def load_new_wafers(self,wafer_type):
        #load wafer into the current foup_slot: 1. check whether this slot is vacant; 2. load new wafers
        if self.wafer!=None:
            print("Slot: "+str(self.slot)+" still has wafer. Load unsuccessfull.")
            return
        new_wafer=Wafer(isUsed='New',wafertype=wafer_type,note=self.foup.foupname+' S'+str(self.slot))
        #pdb.set_trace()
        new_wafer.save()
        self.wafer=new_wafer
        self.save()
        print("Load sucessfully.")
        return
    def reclaim_wafer(self):
        if self.wafer==None:
            print("Slot: "+str(self.slot)+" is vacant.")
        else:
            self.wafer=None
            self.save()
            print("Reclaim successfully")
        return
    def has_wafer(self):
        return self.wafer!=None
    def wafer_used(self):
        if self.has_wafer():
            if self.wafer.isUsed=='New':
                return False
            else:
                return True
        else:
            return False


import os
from shutil import copyfile

source_dir = "/raid/data/mvcnn/invbase/train"
dest_dir = "/raid/data/invbase_flat_50k"

import os
from glob import glob
result = [y for x in os.walk(source_dir) for y in glob(os.path.join(x[0], '*.png'))]

size = len(result)

count = 50000

for i in range(size):
    if (i >= count):
        break

    path_list = result[i].split(os.sep)
    l = len(path_list)
    name = path_list[l-2] + "_" + path_list[l-1]
    dst_name = os.path.join(dest_dir, name)
    copyfile(result[i], dst_name)


import pytesseract
from tqdm import tqdm
from mydatabase import SemPdf
from pdf2image import convert_from_path

def extract_text_from_pdf(pdf_path):
    # Convert PDF to image
    pages = convert_from_path(pdf_path)

    # Extract text from each page using Tesseract OCR
    text_data = ""
    for page in pages:
        text = pytesseract.image_to_string(page)
        text_data += text + "\n"

    # Return the text data
    return text_data

def extract_text_for_sempdfs(sempdfs: list[SemPdf]):
    pbar = tqdm(sempdfs)
    for sempdf in pbar:
        sempdf.text = extract_text_from_pdf(sempdf.paths[0])
        pbar.set_description("Processing %s" % sempdf.paths[0].split("/")[-1])
    return sempdfs


from functools import reduce
employees=[
    {"eid":1000,"name":"ajay","salary":25000,"designation":"developer"},
    {"eid": 1001, "name": "vjay", "salary": 22000, "designation": "developer"},
     {"eid": 1002, "name": "arun", "salary": 26000, "designation": "qa"},
     {"eid": 1003, "name": "varun", "salary": 27000, "designation": "ba"},
     {"eid": 1004, "name": "ram", "salary": 20000, "designation": "mrkt"},
]
# print employees name only
#
# for employee in employees:
#     print(employee["name"])
e_names=list(map(lambda emp:emp["name"],employees))
up_names=list(map(lambda name:name.upper(),e_names))
print(e_names)
print(up_names)
a_name=list(filter(lambda emp:emp["name"][0]=='a',employees))
print(a_name)
sal_emp=list(filter(lambda emp:emp["salary"]>23000,employees))
print(sal_emp)
developers=list(filter(lambda emp:emp["designation"]=="developer,",employees))
print(developers)
high_salary=reduce(lambda sal1,sal2:sal1 if sal1>sal2 else sal2,
                   list(map(lambda emp:emp["salary"],employees)))
print(high_salary)


import numpy as np
import os
import fnmatch
import cv2
from Helper.Util import annotate, crop

if __name__ == '__main__':
    pathImg = "/Users/aaronyu/Desktop/Project14_Image/surface-based-samples/other/canvas"
    pathTemplate = "/Users/aaronyu/Desktop/Project14_Image/surface-based-samples/other/fragment"
    color = (0,255, 0)
    thickness = 4
    imgFiles = fnmatch.filter(os.listdir(pathImg), "*.png")
    # ymlFiles = fnmatch.filter(os.listdir(pathImg), "*.yml")
    imgFiles = sorted(imgFiles)
    for img in imgFiles:
        imgFile = os.path.join(pathImg, img)
        ymlFile = os.path.join(pathImg, img.replace("png", "yml"))
        templateFile = os.path.join(pathTemplate, img)

        annotate(imgFile, ymlFile, color, thickness)

        crop(imgFile, templateFile, color, thickness)


"""empty message

Revision ID: 14de443ddc31
Revises: 2855fec8d81b
Create Date: 2021-04-27 09:58:38.405914

"""
from alembic import op
import sqlalchemy as sa
from app.auth.models import User, db
from app.promo.models import Outlet

# revision identifiers, used by Alembic.
revision = '14de443ddc31'
down_revision = '2855fec8d81b'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('outlet_group_advertise',
                    sa.Column('id', sa.Integer(), nullable=False),
                    sa.Column('outlet_id', sa.Integer(), nullable=True),
                    sa.Column('group_id', sa.Integer(), nullable=True),
                    sa.ForeignKeyConstraint(['group_id'], ['group_advertise.id'], ),
                    sa.ForeignKeyConstraint(['outlet_id'], ['outlet.id'], ),
                    sa.PrimaryKeyConstraint('id')
                    )
    op.add_column('outlet', sa.Column('user_id', sa.Integer(), nullable=True))
    op.create_foreign_key(None, 'outlet', 'user', ['user_id'], ['id'])
    # ### end Alembic commands ###
    user = User.query.first()
    outlets = Outlet.query
    for outlet in outlets:
        outlet.user_id = user.id
    db.session.commit()

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'outlet', type_='foreignkey')
    op.drop_column('outlet', 'user_id')
    op.drop_table('outlet_group_advertise')
    # ### end Alembic commands ###


import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
from scapy.layers.inet import IP,TCP
from random import randint
import pickle
from concurrent.futures import ThreadPoolExecutor

# -------------------------------------- GLOBALS --------------------------------------
opt_to_num = {opt:i  for i,opt  in enumerate(scapy.layers.inet.TCPOptions[1].keys())}
opt_to_num["NONE"] = -1
opts_set = set()
# -------------------------------------------------------------------------------------

def get_tcp_opt(p, opt, default):
    if opt in map(lambda tup : tup[0], p['TCP'].options):
        return reduce(lambda opt1,opt2 : opt1 if opt1[0]==opt else opt2, p['TCP'].options)[1]
    return default

def get_features_of(addr):
    global opts_set

    # attempt to get SYN_ACK packet from server
    syn = IP(dst=addr)/TCP(dport=443, flags='S', seq=randint(0,2**32))
    rst = IP(dst=addr)/TCP(dport=443, flags='R')
    SA  = sr1(syn, retry=5, timeout=3, verbose=0)
    tmp = send(rst, verbose=0)

    if SA == None:
        return addr, None

    opts_set.add(''.join([o[0] for o in SA['TCP'].options]))

    # extract features from the response
    # some feature names are taken from p0f in order to have reference for their meaning
    features = {
        'df'      : 1 if 'DF' in str(SA['IP'].flags) else 0 ,
        'ttl'     : SA['IP'].ttl ,
        'w_size'  : SA['TCP'].window ,
        'mss'     : get_tcp_opt(SA, 'MSS', default=-1) ,
        'w_scale' : get_tcp_opt(SA, 'WScale', default=1) ,

        'df+'  : 1  if (    'DF' in str(SA['IP'].flags) and not SA['IP'].id)    else 0 ,
        'df-'  : 1  if (not 'DF' in str(SA['IP'].flags) and     SA['IP'].id)    else 0 ,
        'fo+'  : 1  if (not 'DF' in str(SA['IP'].flags) and     SA['IP'].frag)  else 0 ,
        'fo-'  : 1  if (    'DF' in str(SA['IP'].flags) and not SA['IP'].frag)  else 0 ,

        'ecn'  : 1  if ('E' in SA['TCP'].flags)                                else 0 ,
        'seq0' : 1  if (SA['TCP'].seq == 0)                                    else 0
    }

    for i,opt in enumerate(SA['TCP'].options):
        if opt[0] in opt_to_num:
            features['opt%d' % (i)] = opt_to_num[opt[0]]
        else:
            features['opt%d' % (i)] = int(opt[0])
    for i in range(len(SA['TCP'].options), 10):
        features['opt%d' % (i)] = -1

    return addr, features

def main():
    global opts_set

    src_dir, dst_dir = sys.argv[1].replace('\\', '/'), sys.argv[2]
    skip = int(sys.argv[3])
    for filename in sorted(os.listdir(src_dir))[skip:]:
    # for filename in sorted(os.listdir(src_dir)):
        print('collecting features for file %s...', filename)
        ips, features = [], {}

        # read ips from current file
        with open('%s\\%s' % (src_dir, filename), 'rt') as curr_input_file:
            ips = list(map(lambda line : line.rstrip('\n'), curr_input_file))

        # collect features
        with ThreadPoolExecutor(max_workers = 100) as executor:
            features = {addr:data  for addr, data  in executor.map(get_features_of, ips)  if data != None}

        # save features to file
        dst_filename = '%s/%s%s' % (dst_dir, filename, '.pkl')
        with open(dst_filename, 'wb') as curr_dst_file:
            pickle.dump(features, curr_dst_file)

    print(opts_set)

if __name__ == "__main__":
    main()


import torch

def tagged_tokens(path):
    last_is_eos = False

    with open(path) as f:
        for line in f.readlines():
            line = line.strip()
            pairs = []
            yield_line = False
            if line != '':
                tokens = line.split(' ')
                pairs.append(('<SOS>', '<SOS>'))
                for token in tokens:
                    if token != '':
                        chunks = token.split('|')
                        word = chunks[0]
                        tag = chunks[-1]
                        pairs.append((word, tag))
                        if tag != 'O':
                            yield_line = True
                pairs.append(('<EOS>', '<EOS>'))

            if yield_line:
                for pair in pairs:
                    yield pair

def generate(path):
    words_ = set()
    tags_ = set()

    count = 0
    for word, tag in tagged_tokens(path):
        count += 1
        words_.add(word)
        tags_.add(tag)

    words = { w: i for i, w in enumerate(words_) }
    tags = { t: i for i, t in enumerate(tags_) }

    with open(f'{path}-words.index', 'w') as f:
        for word in words:
            f.write(word + '\n')

    with open(f'{path}-tags.index', 'w') as f:
        for p in tags:
            f.write(p + '\n')

    train_count = count * 5 // 6
    test_count = count - train_count

    train_word_tensor = torch.LongTensor(train_count)
    train_tag_tensor = torch.LongTensor(train_count)
    test_word_tensor = torch.LongTensor(test_count)
    test_tag_tensor = torch.LongTensor(test_count)

    i = 0
    word_tensor = train_word_tensor
    tag_tensor = train_tag_tensor

    for word, tag in tagged_tokens(path):
        word_tensor[i] = words[word]
        tag_tensor[i] = tags[tag]
        i += 1
        if i == train_count:
            word_tensor = test_word_tensor
            tag_tensor = test_tag_tensor
            i = 0

    torch.save(train_word_tensor, path + '-train-words.pth')
    torch.save(train_tag_tensor, path + '-train-tags.pth')
    torch.save(test_word_tensor, path + '-test-words.pth')
    torch.save(test_tag_tensor, path + '-test-tags.pth')

if __name__ == '__main__':
    import sys
    generate(sys.argv[1])


# Matthew Hilton
# July 6th, 2021
'''
Module: Vector.py

This is the port of the Vector module in the C++ Standard
Template Library. Anything involving pointers or move semantics
Are either modified or removed. Const iterators are also removed.
However vector.data() has been implemented
'''
from typing import Any, List, Tuple

class Vector():

    '''
    This is the main Vector class. It is statically typed but dynamically sized. The type of the
    items in this vector are guarenteed to all be the same. For more information visit:

    https://www.cplusplus.com/reference/vector/vector/
    '''

    __type: type = None
    __data: List = []
    __size: int = 0

    class ConstructorFailure(Exception):
        '''
        Rasied if the constructor does not receive the proper arguments
        '''

    class OutOfRangeError(Exception):
        '''
        Rasied if the user attempts to access not in range of the Vector
        '''

    class ArgumentError(Exception):
        '''
        Rasied if a memeber function does not receive thee proper arguments
        '''

    class VectorIterator:
        '''
        An iterator class for vector
        '''

        def __init__(self, vector: 'Vector', index: int = -1) -> None:
            self.__vector: 'Vector' = vector
            self.__size: int = len(vector)
            self.__index: int = index

        def __next__(self) -> None:
            self.__size: int = len(self.__vector)
            self.__index += 1
            if self.__index < self.__size:
                return self.__vector[self.__index]
            raise StopIteration

    class VectorReverseIterator:
        '''
        An iterator class for vector that will iterate backwards
        '''

        def __init__(self, vector: 'Vector', end: bool = False) -> None:
            self.__vector: 'Vector' = vector
            self.__size: int = len(vector)
            self.__index: int = self.__size if end is False else 0

        def __next__(self) -> None:
            self.__size: int = len(self.__vector)
            self.__index -= 1
            if self.__index > -1:
                return self.__vector[self.__index]
            raise StopIteration

    def __init__(self, *args) -> None:
        '''
        Written to work similar to C++11's std::vector.

        Parameters:
            * Type:    Sets the type of vector. Must be supplied with
                       no other arguments
            * Vector:  Must be specified with no other arguments. If
                       a vector is passed, then the it will copy the
                       contents of the passed vector to the new vector.
            * List of
              values:  A list obj of values that are to be added to the
                       vector. All items in the list must match the
                       type of the first element of the list.
            * Size:    Ammount to fill vector. Must be in the first
                       position
            * Value:   Value to fill vector. Must be the last argument
                       requires a size
        '''
        # Empty Ctor
        if len(args) == 0:
            self.__type = None
            self.__data = []
            self.__size = 0
            return

        # Copy ctor
        if len(args) == 1 and isinstance(args[0], Vector):
            self.__type = args[0].__type
            self.__size = len(args[0])
            self.__data = args[0].__data
            return

        # Type instantiation ctor
        if len(args) == 1 and isinstance(args[0], type):
            self.__type = args[0]
            self.__data = []
            self.__size = 0
            return

        # List obj Ctor
        if len(args) == 1 and isinstance(args[0], list):
            self.__type = type(args[0][0])
            self.__size = len(args[0])
            self.__data = []
            for val in args[0]:
                if not isinstance(val, self.__type):
                    raise TypeError(
                        f"Vector Error (init): Value '{val}' is of type '{type(val)}' and is not \
                            of type '{self.__type}'")
                self.__data.append(val)
            return

        # Fill ctor
        if len(args) == 2 and isinstance(args[0], int):
            self.__type = type(args[1])
            self.__size = args[0]
            self.__data = [args[1] for _ in range(self.__size)]
            return

        # Fallthrough Error
        raise self.ConstructorFailure(
            f"Vector Error (init): Invalid Argument(s). ({args})")

    # Python Magic Methods
    # Magic Python Method to allow for len(vector)
    #
    # Returns the length of the vector
    def __len__(self) -> int:
        return self.__size

    # Magic Python Method that customizes the representation of print(vector)
    def __str__(self) -> str:
        return self.__data.__str__()

    # Magic Python Method that overloads the == method
    def __eq__(self, other: 'Vector') -> bool:
        if self.__size == other.__size and self.__type is other.__type:
            return self.__data == other.__data
        return False

    def type(self) -> type:
        '''Hidden Helper that returns the type of the vector'''
        return self.__type

    # Iterator Methods
    # rbegin, rend, cbegin, cend, crbegin, crend = delete

    def __iter__(self) -> Any:
        '''Magic Python Method that allows for iter(vector)
           Useful for foreach type for loops (ie: for elem in vector)'''
        return self.VectorIterator(self)

    def begin(self) -> Any:
        '''Returns the iterator to the first element in the vector'''
        return self.VectorIterator(self)

    def end(self) -> Any:
        '''Returns the iterator to the last element in the vector'''
        return self.VectorIterator(self, self.__size - 1)

    def rbegin(self) -> 'VectorReverseIterator':
        '''
        Returns the iterator to the last element in the vector.

        When next is called, you will go to the previous element
        in the vector.
        '''
        return self.VectorReverseIterator(self)

    def rend(self) -> 'VectorReverseIterator':
        '''
        Returns the iterator to the last element in the vector

        When next is called, you will go to the previous element
        in the vector.
        '''
        return self.VectorReverseIterator(self, end=True)

    # Capacity Methods

    # The following methods have not been implemented because the underlying
    # data structure is a list, not a statically sized array:
    #
    # max_size()
    # resize()
    # capacity()
    # reserve()
    # shrink_to_fit()

    def size(self) -> int:
        '''Returns the number of elements in the vector'''
        return self.__size

    def empty(self) -> bool:
        '''Checks to see if the vector is empty or not'''
        return self.__size == 0

    # Element Access
    # The following two dunder methods effectivly overload the [] operator

    # Magic Python Method that overloads operator[] access
    #
    # Returns the value at the given index
    def __getitem__(self, i: int) -> Any:
        if i >= self.__size:
            raise IndexError(
                f"VectorError (Operator[]): Index '{i}' is not in the bounds of the Vector. \
                    (Max index: {self.__size - 1})")
        return self.__data[i]

    # Magic Python Method that overloads operator[] assign
    #
    # Sets the value in the vector to the passed value at the passed index
    def __setitem__(self, i: int, value: Any) -> None:
        if isinstance(None, self.__type):
            self.__type = type(value)
        if i >= self.__size:
            raise IndexError(
                f"VectorError (Operator[]): Index '{i}' is not in the bounds of the Vector. \
                    (Max index: {self.__size - 1})")
        if not isinstance(value, self.__type):
            raise TypeError(
                f"VectorError (Operator[]): Value '{value}' is of type '{type(value)}' when type \
                    '{self.__type}' was expected.")
        self.__data[i] = value

    # Simialar to the previous Dunder methods; please just use the [] operator
    def at(self, i: int, *value: Any) -> Any:
        '''Returns the element at the given location. Will not accept assignments
           (ie: vector.at() = value) however if you want to set the value at
           that location add the value as an argument (ie: vector.at(index, value))'''
        if len(value) == 0:
            return self.__getitem__(i)

        if len(value) == 1:
            self.__setitem__(i, value[0])
            return None

        raise self.ArgumentError(
            f"Vector Error (at): Invalid Arguments. Expected Either an index or an index and a \
                value. (args: {i}, {value})")

    def front(self) -> Any:
        '''Returns the first element in the vector'''
        if self.__size < 1:
            raise self.OutOfRangeError("Vector Error (front): Vector is empty")
        return self.__data[0]

    def back(self) -> Any:
        '''Returns the last element in the vector'''
        if self.__size < 1:
            raise self.OutOfRangeError("Vector Error (back): Vector is empty")
        return self.__data[-1]

    def data(self) -> Tuple[Any]:
        '''
        Returns the underlying list as a tuple to maintain the state of the vector when this
        method is called.

        Order of elements is preserved.
        '''
        return tuple(self.__data)

    # Modifiers
    # These functions are not implemented becuase
    # assign(): Relates to iterables. Iterables are
    #           fundamentally different in python than c++

    def push_back(self, val: Any) -> None:
        '''
        Adds an element to the end of the vector.

        Raises a type error if passed value is not of the same type as the Vector
        '''
        if not isinstance(val, self.__type):
            raise TypeError(
                f"Vector Error (push_back): Value '{val}' is of type '{type(val)}' and is not of \
                    type '{self.__type}'")
        self.__data.append(val)
        self.__size += 1

    def pop_back(self) -> None:
        '''
        Pops of (removes) the last element in the vector

        If the vector is empty, raises Vector.EmptyError
        '''
        if self.__size < 1:
            raise self.OutOfRangeError(
                "Vector Error (pop_back): Unable to pop back the last element, Vector is empty!")
        self.__data.pop()
        self.__size -= 1

    def insert(self, i: int, val: Any) -> None:
        '''
        Inserts the value (val) at the location (i)

        i must be 0 <= i < Vector.size()
        val must be of the same type as the other elements in the vector
        '''

        if i < 0 or i >= self.__size:
            raise self.OutOfRangeError(
                f"Vector Error (insert): Invalid Index (Value: {i} is not 0 <= i < {self.__size})")

        if not isinstance(val, self.__type):
            raise TypeError(
                f"Vector Error (insert): Value '{val}' is of type '{type(val)}' and is not of \
                    type '{self.__type}'")

        self.__data.insert(i, val)
        self.__size += 1

    def erase(self, i: int, r: int = 1) -> None:
        '''
        Deletes the value at the location (i)

        i must be 0 <= i < Vector.size()
        '''
        if i < 0 or i >= self.__size:
            raise self.OutOfRangeError(
                f"Vector Error (insert): Invalid Index (Value: {i} is not 0 <= i < {self.__size})")

        if i + r > self.__size:
            raise self.OutOfRangeError(
                f"Vector Error (insert): Invalid Range, index + range must not exceed \
                    vector.size! ({i} + {r} > {self.__size})")

        for _ in range(r):
            self.__data.pop(i)
            self.__size -= 1

    def swap(self, v: 'Vector') -> None:
        '''Swaps the LHS elements with the RHS elements'''
        # This version has excpetions due to the fact that there are no compile
        # time errors for type checking and bounds checking.
        if self.__type is not v.__type:
            raise TypeError(
                f"ArrayError (swap): LHS vector is of type '{self.__type}' and RHS vector is of \
                    type '{v.__type}.'")

        # Generate temp list
        temp_li = self.__data

        # Clear values in lists, then -replace them with the items in the other list
        self.__data.clear()
        for item in v.__data:
            self.__data.append(item)

        v.__data.clear()
        for item in temp_li:
            v.__data.append(item)

        # Setting the sizes of the respective vectors.
        temp = self.__size
        self.__size = v.__size
        v.__size = temp

    def clear(self) -> None:
        '''
        Clears the vector of any items
        '''
        self.__size = 0
        self.__data.clear()

    def emplace(self, i: int, *args) -> None:
        '''
        Creates a new obj based on Vector.type and inserts it into the Vector at
        index (i).

        If i is not in range, this function will raise an OutOfRangeError
        '''
        if i < 0 or i >= self.__size:
            raise self.OutOfRangeError(
                f"Vector Error (emplace): Invalid Index (Value: {i} is not 0 <= i < {self.__size})")

        self.__data.insert(i, self.__type(*args))
        self.__size += 1

    def emplace_back(self, *args) -> None:
        '''
        Creates a new obj based on Vector.type and appends it to the end of the vector
        '''
        self.__data.append(self.__type(*args))
        self.__size += 1


import math
import unittest
from time import sleep

import tests.multitest_helpers

from crio_interface.to_crio import ToCRIO
from crio_interface.from_crio import FromCRIO

def suite():
    suite = unittest.defaultTestLoader.loadTestsFromTestCase(RotateInPlaceVerification)
    return suite

def get_test_angles():
    angle = 0
    angles = []
    for i in xrange(0, 5):
        angles.append(angle + i * math.pi / 2.0)
    return angles
#    return [0]

def distance_from_origin(x, y):
    return math.sqrt(x*x + y*y)

def min_angle(angle):
    angles = [math.fabs(angle), math.fabs(angle + math.pi*2), math.fabs(angle - math.pi*2)]
    return min(angles)

class RotateInPlaceVerification(unittest.TestCase):
    def setUp(self):
        self.sender = ToCRIO()
        self.sender.send_reboot_command()
        sleep(5)
#        self.sender.send_stop_command()
#        self.sender.send_start_command()
        self.receiver = FromCRIO()
        self.receiver.has_data.wait(180)

    def tearDown(self):
#        self.sender.send_stop_command()
        self.sender.send_heading_speed_command(self.receiver.heading, 0)
        self.sender = None
        self.receiver.cleanup()
        self.receiver = None

    multitest_steering_values = get_test_angles()
    def multitest_steering(self, angle):
        condition = self.receiver.has_data.isSet()
        self.assertTrue(condition, "Timed out waiting for the receiver to receive a packet")
        self.sender.send_heading_speed_command(angle, 0.0)
        sleep(4.8)
        self.x = self.receiver.x
        self.y = self.receiver.y
        self.heading = self.receiver.heading
        self.dist_err = distance_from_origin(self.x, self.y)
        self.assertTrue(self.dist_err < 0.5, "Not within 0.5 meters of origin after turn to %f. Distance Error was %f" % (angle, self.dist_err))
        self.error = angle - self.heading
        self.min_error = min_angle(self.error)
        self.assertTrue(math.degrees(self.min_error) < 5.0, "Heading error not less than 5 degrees after turn to %f. Error was %f" % (angle, math.degrees(self.min_error)))

tests.multitest_helpers.add_test_cases(RotateInPlaceVerification)


import json
import os

from collections import Mapping

import google.auth

from decouple import config
from google.cloud.storage.client import Client  # pylint: disable=ungrouped-imports
from google.oauth2.service_account import Credentials  # pylint: disable=ungrouped-imports

from ..exceptions import DblueStoresException
from ..logger import logger
from .base import BaseClient

DEFAULT_SCOPES = ('https://www.googleapis.com/auth/cloud-platform',)

class GCPClient(BaseClient):

    @classmethod
    def get_client(cls, *args, **kwargs):
        return cls._get_client(*args, **kwargs)

    @staticmethod
    def get_credentials(key_path=None, keyfile_dict=None, scopes=None):
        """
        Returns the Credentials object for Google API
        """
        key_path = key_path or config("GCP_KEY_FILE_PATH", default=None)
        keyfile_dict = keyfile_dict or config("GCP_KEY_FILE_DICT", default=None)
        scopes = scopes or config("GCP_SCOPES", default=None)

        if scopes is not None:
            scopes = [s.strip() for s in scopes.split(',')]
        else:
            scopes = DEFAULT_SCOPES

        if not key_path and not keyfile_dict:
            logger.info('Getting connection using `google.auth.default()` '
                        'since no key file is defined for hook.')
            credentials, _ = google.auth.default(scopes=scopes)
        elif key_path:
            # Get credentials from a JSON file.
            if key_path.endswith('.json'):
                logger.info('Getting connection using a JSON key file.')
                credentials = Credentials.from_service_account_file(
                    os.path.abspath(key_path), scopes=scopes)
            else:
                raise DblueStoresException('Unrecognised extension for key file.')
        else:
            # Get credentials from JSON data.
            try:
                if not isinstance(keyfile_dict, Mapping):
                    keyfile_dict = json.loads(keyfile_dict)

                # Convert escaped newlines to actual newlines if any.
                keyfile_dict['private_key'] = keyfile_dict['private_key'].replace('\\n', '\n')

                credentials = Credentials.from_service_account_info(keyfile_dict, scopes=scopes)
            except ValueError:  # json.decoder.JSONDecodeError does not exist on py2
                raise DblueStoresException('Invalid key JSON.')

        return credentials

    @classmethod
    def get_access_token(cls, key_path=None, keyfile_dict=None, credentials=None, scopes=None):
        credentials = credentials or cls.get_credentials(key_path=key_path,
                                                         keyfile_dict=keyfile_dict,
                                                         scopes=scopes)
        return credentials.token

    @classmethod
    def _get_client(cls, project_id=None, key_path=None, keyfile_dict=None, credentials=None, scopes=None):
        credentials = credentials or cls.get_credentials(key_path=key_path,
                                                         keyfile_dict=keyfile_dict,
                                                         scopes=scopes)

        project_id = project_id or config("GCP_PROJECT_ID", default=None)

        return Client(
            project=project_id,
            credentials=credentials
        )


from nanoepitools.annotations.annotations import GFFFeature, GFFAnnotationsReader

class MapToPromoter:
    """
    Maps a GFF feature to a promoter it overlaps with
    """
    def __init__(self, gff: GFFAnnotationsReader, promoter_before_tss: int, promoter_after_tss: int, input_will_be_sorted=False):
        self.gff = gff
        self.promoter_before_tss = promoter_before_tss
        self.promoter_after_tss = promoter_after_tss
        self.input_will_be_sorted = input_will_be_sorted
        self.last_chrom = None
        self.seeker = None

    def range_transform(self, gff_feature: GFFFeature):
        if gff_feature.direction == "+":
            promoter_range = [gff_feature.start - self.promoter_before_tss, gff_feature.start + self.promoter_after_tss]
        elif gff_feature.direction == "-":
            promoter_range = [gff_feature.end - self.promoter_after_tss, gff_feature.end + self.promoter_before_tss]

        return promoter_range

    def dist_function(self, gff_feature: GFFFeature, start: int, end: int):
        """Computes the distance of the region to the promoter of the given gff feature.
        Returns 0 as a distance if there is overlap (i.e. the promoter is fully or partially contained within the
        region or vice versa)
        """

        """Promoter location depends on the direction of the transcript"""
        promoter_range = self.range_transform(gff_feature)

        if promoter_range[0] > end or promoter_range[1] < start:
            """Our region is close to the promoter but not overlapping"""
            return 1
        else:
            """Our region is overlapping the promoter, therefore the distance is 0"""
            return 0

    def find_sorted(self, chrom, start, end):
        if chrom != self.last_chrom and self.input_will_be_sorted:
            self.last_chrom = chrom
            self.seeker = self.gff.chromosomes[chrom].get_sorted_in_range_finder()
        return self.seeker.find(start, end, range_transform=lambda x: self.range_transform(x), max_recursion=1, min_recursion=1)

    def find_unsorted(self, chrom, start, end):
        return self.gff.chromosomes[chrom].get_in_range(start, end, range_transform=lambda x: self.range_transform(x),
            max_recursion=1, min_recursion=1)

    def __call__(self, chrom, start, end):
        if self.input_will_be_sorted:
            return self.find_sorted(chrom, start, end)
        else:
            return self.find_unsorted(chrom, start, end)


import boto3
from requests_aws4auth import AWS4Auth
from opensearchpy import OpenSearch, RequestsHttpConnection
import curator
import os

retention_in_days = int(os.environ.get('RETENTION_IN_DAYS'))
host = os.environ.get('OPENSEARCH_HOST')
region = os.environ.get('AWS_REGION')
service = 'es'
credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(credentials.access_key,
                   credentials.secret_key,
                   region,
                   service,
                   session_token=credentials.token)

def lambda_handler(event, context):
    print("Checking {} OpenSearch domain".format(host))
    client = OpenSearch(
        hosts=[{'host': host, 'port': 443}],
        http_auth=awsauth,
        use_ssl=True,
        verify_certs=True,
        connection_class=RequestsHttpConnection
    )

    # Listing all indexes
    index_list = curator.IndexList(client)

    # Filtering out Kibana
    index_list.filter_kibana()

    # Filtering index list to be deleted
    index_list.filter_by_age(source='name',
                             direction='older', timestring='%Y.%m.%d', unit='days', unit_count=retention_in_days)
    print("Found {} indices that are older than {} to delete".format(len(index_list.indices), retention_in_days))

    # Deleting indexes
    if len(index_list.indices) > 0:
        curator.DeleteIndices(index_list).do_action()
    else:
        print("There is no index to delete")

if __name__ == "__main__":
    event = ''
    context = []
    lambda_handler(event, context)


#
# @lc app=leetcode id=242 lang=python
#
# [242] Valid Anagram
#
# https://leetcode.com/problems/valid-anagram/description/
#
# algorithms
# Easy (52.21%)
# Likes:    732
# Dislikes: 109
# Total Accepted:    352.3K
# Total Submissions: 670.7K
# Testcase Example:  '"anagram"\n"nagaram"'
#
# Given two strings s and t , write a function to determine if t is an anagram
# of s.
#
# Example 1:
#
#
# Input: s = "anagram", t = "nagaram"
# Output: true
#
#
# Example 2:
#
#
# Input: s = "rat", t = "car"
# Output: false
#
#
# Note:
# You may assume the string contains only lowercase alphabets.
#
# Follow up:
# What if the inputs contain unicode characters? How would you adapt your
# solution to such case?
#
#

import operator
class Solution(object):
    def isAnagram_1(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        if len(s) != len(t):
            return False
        dict_s = {}
        for key in s:
            dict_s[key] = dict_s.get(key, 0) + 1
        dict_t = {}
        for key in t:
            dict_t[key] = dict_t.get(key, 0) + 1
        return dict_s == dict_t

    def isAnagram_2(self, s, t):
        return sorted(s) == sorted(t)

    def isAnagram_3(self, s, t):
        dict_s, dict_t = [0] * 26, [0] * 26
        for key in s:
            dict_s[ord(key) - ord('a')] += 1
        for key in t:
            dict_t[ord(key) - ord('a')] += 1
        return dict_s == dict_t

    def isAnagram(self, s, t):
        return operator.eq({c: s.count(c) for c in set(s)}, {c: t.count(c) for c in set(t)})


from confluent_kafka import Consumer, KafkaError
import cv2
import numpy as np

# Kafka configuration
kafka_conf = {
    'bootstrap.servers': 'localhost:9092',  # Replace with your Kafka broker address
    'group.id': 'camera_consumer_group',    # Consumer group ID
    'auto.offset.reset': 'earliest'        # Start reading from the beginning of the topic
}

# Kafka topic for frames
kafka_topic = 'camera_frames'

# Create Kafka consumer
consumer = Consumer(kafka_conf)
consumer.subscribe([kafka_topic])

def read_frames():
    while True:
        msg = consumer.poll(1.0)

        if msg is None:
            break

        if msg.error():
            if msg.error().code() == KafkaError._PARTITION_EOF:
                print("Reached end of partition")
            else:
                print(f"Error while consuming: {msg.error()}")
        else:
            # Decode the frame data
            frame_data = msg.value()

            # Convert the frame data back to an image
            frame = cv2.imdecode(np.frombuffer(frame_data, np.uint8), cv2.IMREAD_COLOR)

            # Process the frame as needed (e.g., display, save, etc.)
            cv2.imshow("IP Camera Footage", frame)

            # Exit the program if 'q' is pressed
            if cv2.waitKey(1) == ord('q'):
                break

    # Destroy all windows when done
    cv2.destroyAllWindows()

def main():
    read_frames()


import decimal
import functools
import json
import jsonschema
import lxml.etree as etree
import os
import re

import reframe as rfm
import reframe.core.exceptions as errors
import reframe.utility.jsonext as jsonext
import reframe.utility.osext as osext
from reframe.core.logging import getlogger
from reframe.core.warnings import suppress_deprecations

# The schema data version
# Major version bumps are expected to break the validation of previous schemas

DATA_VERSION = '3.1'
_SCHEMA = os.path.join(rfm.INSTALL_PREFIX, 'reframe/schemas/runreport.json')

class _RunReport:
    '''A wrapper to the run report providing some additional functionality'''

    def __init__(self, report):
        self._report = report
        self._fallbacks = []    # fallback reports

        # Index all runs by test case; if a test case has run multiple times,
        # only the last time will be indexed
        self._cases_index = {}
        for run in self._report['runs']:
            for tc in run['testcases']:
                c, p, e = tc['unique_name'], tc['system'], tc['environment']
                self._cases_index[c, p, e] = tc

        # Index also the restored cases
        for tc in self._report['restored_cases']:
            c, p, e = tc['unique_name'], tc['system'], tc['environment']
            self._cases_index[c, p, e] = tc

    def __getitem__(self, key):
        return self._report[key]

    def __getattr__(self, name):
        with suppress_deprecations():
            return getattr(self._report, name)

    def add_fallback(self, report):
        self._fallbacks.append(report)

    def slice(self, prop, when=None, unique=False):
        '''Slice the report on property ``prop``.'''

        if unique:
            returned = set()

        for tc in self._report['runs'][-1]['testcases']:
            val = tc[prop]
            if unique and val in returned:
                continue

            if when is None:
                if unique:
                    returned.add(val)

                yield val
            elif tc[when[0]] == when[1]:
                if unique:
                    returned.add(val)

                yield val

    def case(self, check, part, env):
        c, p, e = check.unique_name, part.fullname, env.name
        ret = self._cases_index.get((c, p, e))
        if ret is None:
            # Look up the case in the fallback reports
            for rpt in self._fallbacks:
                ret = rpt._cases_index.get((c, p, e))
                if ret is not None:
                    break

        return ret

    def restore_dangling(self, graph):
        '''Restore dangling dependencies in graph from the report data.

        Returns the updated graph.
        '''

        restored = []
        for tc, deps in graph.items():
            for d in deps:
                if d not in graph:
                    restored.append(d)
                    self._do_restore(d)

        return graph, restored

    def _do_restore(self, testcase):
        tc = self.case(*testcase)
        if tc is None:
            raise errors.ReframeError(
                f'could not restore testcase {testcase!r}: '
                f'not found in the report files'
            )

        dump_file = os.path.join(tc['stagedir'], '.rfm_testcase.json')
        try:
            with open(dump_file) as fp:
                testcase._check = jsonext.load(fp)
        except (OSError, json.JSONDecodeError) as e:
            raise errors.ReframeError(
                f'could not restore testcase {testcase!r}') from e

def next_report_filename(filepatt, new=True):
    if '{sessionid}' not in filepatt:
        return filepatt

    search_patt = os.path.basename(filepatt).replace('{sessionid}', r'(\d+)')
    new_id = -1
    basedir = os.path.dirname(filepatt) or '.'
    for filename in os.listdir(basedir):
        match = re.match(search_patt, filename)
        if match:
            found_id = int(match.group(1))
            new_id = max(found_id, new_id)

    if new:
        new_id += 1

    return filepatt.format(sessionid=new_id)

def _load_report(filename):
    try:
        with open(filename) as fp:
            report = json.load(fp)
    except OSError as e:
        raise errors.ReframeError(
            f'failed to load report file {filename!r}') from e
    except json.JSONDecodeError as e:
        raise errors.ReframeError(
            f'report file {filename!r} is not a valid JSON file') from e

    # Validate the report
    with open(_SCHEMA) as fp:
        schema = json.load(fp)

    try:
        jsonschema.validate(report, schema)
    except jsonschema.ValidationError as e:
        try:
            found_ver = report['session_info']['data_version']
        except KeyError:
            found_ver = 'n/a'

        raise errors.ReframeError(
            f'invalid report {filename!r} '
            f'(required data version: {DATA_VERSION}), found: {found_ver})'
        ) from e

    return _RunReport(report)

def load_report(*filenames):
    primary = filenames[0]
    rpt = _load_report(primary)

    # Add fallback reports
    for f in filenames[1:]:
        rpt.add_fallback(_load_report(f))

    return rpt

def write_report(report, filename, compress=False, link_to_last=False):
    with open(filename, 'w') as fp:
        if compress:
            jsonext.dump(report, fp)
        else:
            jsonext.dump(report, fp, indent=2)
            fp.write('\n')

        if not link_to_last:
            return

        # Add a symlink to the latest report
        basedir = os.path.dirname(filename)
        with osext.change_dir(basedir):
            link_name = 'latest.json'
            create_symlink = functools.partial(
                os.symlink, os.path.basename(filename), link_name
            )
            if not os.path.exists(link_name):
                create_symlink()
            else:
                if os.path.islink(link_name):
                    os.remove(link_name)
                    create_symlink()
                else:
                    getlogger().warning('could not create a symlink '
                                        'to the latest report file: '
                                        'path exists and is not a symlink')

def junit_xml_report(json_report):
    '''Generate a JUnit report from a standard ReFrame JSON report.'''

    xml_testsuites = etree.Element('testsuites')
    for run_id, rfm_run in enumerate(json_report['runs']):
        xml_testsuite = etree.SubElement(
            xml_testsuites, 'testsuite',
            attrib={
                'errors': '0',
                'failures': str(rfm_run['num_failures']),
                'hostname': json_report['session_info']['hostname'],
                'id': str(run_id),
                'name': f'ReFrame run {run_id}',
                'package': 'reframe',
                'tests': str(rfm_run['num_cases']),
                'time': str(json_report['session_info']['time_elapsed']),

                # XSD schema does not like the timezone format, so we remove it
                'timestamp': json_report['session_info']['time_start'][:-5],
            }
        )
        testsuite_properties = etree.SubElement(xml_testsuite, 'properties')
        for tc in rfm_run['testcases']:
            casename = (
                f"{tc['unique_name']}[{tc['system']}, {tc['environment']}]"
            )
            testcase = etree.SubElement(
                xml_testsuite, 'testcase',
                attrib={
                    'classname': tc['filename'],
                    'name': casename,

                    # XSD schema does not like the exponential format and since
                    # we do not want to impose a fixed width, we pass it to
                    # `Decimal` to format it automatically.
                    'time': str(decimal.Decimal(tc['time_total'] or 0)),
                }
            )
            if tc['result'] == 'failure':
                testcase_msg = etree.SubElement(
                    testcase, 'failure', attrib={'type': 'failure',
                                                 'message': tc['fail_phase']}
                )
                testcase_msg.text = f"{tc['fail_phase']}: {tc['fail_reason']}"

        testsuite_stdout = etree.SubElement(xml_testsuite, 'system-out')
        testsuite_stdout.text = ''
        testsuite_stderr = etree.SubElement(xml_testsuite, 'system-err')
        testsuite_stderr.text = ''

    return xml_testsuites

def junit_dump(xml, fp):
    fp.write(
        etree.tostring(xml, encoding='utf8', pretty_print=True,
                       method='xml', xml_declaration=True).decode()
    )


from __future__ import print_function
import numpy as np

def bulk1d(g,fac=0.3):
  """Return the indexes of the bulk sites"""
  ymin = np.min(g.y)
  ymax = np.max(g.y)
  out = np.zeros(len(g.r)) # output array
  for i in range(len(g.r)):
    if fac<(g.y[i]-ymin)/(ymax-ymin)<(1.0-fac): out[i] = 1.0
  return out


# display the list of .xlsx files in a given directory
import os

def getFileList(ext, dir='.'):
   filenames = os.listdir(dir)
   filenameList = []
   for filename in filenames:
      if filename.endswith(ext):
         filenameList.append(filename)
   return filenameList

filenames = getFileList('.xlsx', dir='data')
print(filenames)


from contextlib import contextmanager
import os
import sys
import subprocess
import shlex
import tempfile
from vp_utils import safe_remove, VPLogger

class VW:
    def __init__(self,
                 logger=None,
                 vw='vw',
                 moniker=None,
                 name=None,
                 bits=None,
                 loss=None,
                 passes=None,
                 log_stderr_to_file=False,
                 silent=False,
                 l1=None,
                 l2=None,
                 learning_rate=None,
                 quadratic=None,
                 audit=None,
                 power_t=None,
                 adaptive=False,
                 working_dir=None,
                 decay_learning_rate=None,
                 initial_t=None,
                 lda=None,
                 lda_D=None,
                 lda_rho=None,
                 lda_alpha=None,
                 minibatch=None,
                 total=None,
                 node=None,
                 unique_id=None,
                 span_server=None,
                 bfgs=None,
                 oaa=None,
                 old_model=None,
                 incremental=False,
                 mem=None,
                 nn=None,
                 **kwargs):
        assert moniker and passes

        if logger is None:
            self.log = VPLogger()
        else:
            self.log = logger

        self.node = node
        self.total = total
        self.unique_id = unique_id
        self.span_server = span_server
        if self.node is not None:
            assert self.total is not None
            assert self.unique_id is not None
            assert self.span_server is not None

        if name is None:
            self.handle = '%s' % moniker
        else:
            self.handle = '%s.%s' % (moniker, name)

        if self.node is not None:
            self.handle = "%s.%d" % (self.handle, self.node)

        if old_model is None:
            self.filename = '%s.model' % self.handle
            self.incremental = False
        else:
            self.filename = old_model
            self.incremental = True

        self.incremental = incremental
        self.filename = '%s.model' % self.handle

        self.name = name
        self.bits = bits
        self.loss = loss
        self.vw = vw
        self.l1 = l1
        self.l2 = l2
        self.learning_rate = learning_rate
        self.log_stderr_to_file = log_stderr_to_file
        self.silent = silent
        self.passes = passes
        self.quadratic = quadratic
        self.power_t = power_t
        self.adaptive = adaptive
        self.decay_learning_rate = decay_learning_rate
        self.audit = audit
        self.initial_t = initial_t
        self.lda = lda
        self.lda_D = lda_D
        self.lda_rho = lda_rho
        self.lda_alpha = lda_alpha
        self.minibatch = minibatch
        self.oaa = oaa
        self.bfgs = bfgs
        self.mem = mem
        self.nn = nn

        # Do some sanity checking for compatibility between models
        if self.lda:
            assert not self.l1
            assert not self.l1
            assert not self.l2
            assert not self.loss
            assert not self.adaptive
            assert not self.oaa
            assert not self.bfgs
        else:
            assert not self.lda_D
            assert not self.lda_rho
            assert not self.lda_alpha
            assert not self.minibatch

        self.working_directory = working_dir or os.getcwd()

    def vw_base_command(self, base):
        l = base
        if self.bits                is not None: l.append('-b %d' % self.bits)
        if self.learning_rate       is not None: l.append('--learning_rate=%f' % self.learning_rate)
        if self.l1                  is not None: l.append('--l1=%f' % self.l1)
        if self.l2                  is not None: l.append('--l2=%f' % self.l2)
        if self.initial_t           is not None: l.append('--initial_t=%f' % self.initial_t)
        if self.quadratic           is not None: l.append('-q %s' % self.quadratic)
        if self.power_t             is not None: l.append('--power_t=%f' % self.power_t)
        if self.loss                is not None: l.append('--loss_function=%s' % self.loss)
        if self.decay_learning_rate is not None: l.append('--decay_learning_rate=%f' % self.decay_learning_rate)
        if self.lda                 is not None: l.append('--lda=%d' % self.lda)
        if self.lda_D               is not None: l.append('--lda_D=%d' % self.lda_D)
        if self.lda_rho             is not None: l.append('--lda_rho=%f' % self.lda_rho)
        if self.lda_alpha           is not None: l.append('--lda_alpha=%f' % self.lda_alpha)
        if self.minibatch           is not None: l.append('--minibatch=%d' % self.minibatch)
        if self.oaa                 is not None: l.append('--oaa=%d' % self.oaa)
        if self.unique_id           is not None: l.append('--unique_id=%d' % self.unique_id)
        if self.total               is not None: l.append('--total=%d' % self.total)
        if self.node                is not None: l.append('--node=%d' % self.node)
        if self.span_server         is not None: l.append('--span_server=%s' % self.span_server)
        if self.mem                 is not None: l.append('--mem=%d' % self.mem)
        if self.audit:                           l.append('--audit')
        if self.bfgs:                            l.append('--bfgs')
        if self.adaptive:                        l.append('--adaptive')
        if self.nn                  is not None: l.append('--nn=%d' % self.nn)
        return ' '.join(l)

    def vw_train_command(self, cache_file, model_file):
        if os.path.exists(model_file) and self.incremental:
            return self.vw_base_command([self.vw]) + ' --passes %d --cache_file %s -i %s -f %s' \
                    % (self.passes, cache_file, model_file, model_file)
        else:
            self.log.debug('No existing model file or not options.incremental')
            return self.vw_base_command([self.vw]) + ' --passes %d --cache_file %s -f %s' \
                    % (self.passes, cache_file, model_file)

    def vw_test_command(self, model_file, prediction_file):
        return self.vw_base_command([self.vw]) + ' -t -i %s -p %s' % (model_file, prediction_file)

    def vw_test_command_library(self, model_file):
        return self.vw_base_command([]) + ' -t -i %s' % (model_file)

    @contextmanager
    def training(self):
        self.start_training()
        yield
        self.close_process()

    @contextmanager
    def predicting(self):
        self.start_predicting()
        yield
        self.close_process()

    @contextmanager
    def predicting_library(self):
        self.start_predicting_library()
        yield
        self.end_predicting_library()

    def start_training(self):
        cache_file = self.get_cache_file()
        model_file = self.get_model_file()

        # Remove the old cache and model files
        if not self.incremental:
            safe_remove(cache_file)
            safe_remove(model_file)

        # Run the actual training
        self.vw_process = self.make_subprocess(self.vw_train_command(cache_file, model_file))

        # set the instance pusher
        self.push_instance = self.push_instance_stdin

    def close_process(self):
        # Close the process
        assert self.vw_process
        self.vw_process.stdin.flush()
        self.vw_process.stdin.close()
        if self.vw_process.wait() != 0:
            raise Exception("vw_process %d (%s) exited abnormally with return code %d" % \
                (self.vw_process.pid, self.vw_process.command, self.vw_process.returncode))

    def push_instance_stdin(self, instance):
        self.vw_process.stdin.write(('%s\n' % instance).encode('utf8'))

    def start_predicting(self):
        model_file = self.get_model_file()
        # Be sure that the prediction file has a unique filename, since many processes may try to
        # make predictions using the same model at the same time
        _, prediction_file = tempfile.mkstemp(dir='.', prefix=self.get_prediction_file())
        os.close(_)

        self.vw_process = self.make_subprocess(self.vw_test_command(model_file, prediction_file))
        self.prediction_file = prediction_file
        self.push_instance = self.push_instance_stdin

    def start_predicting_library(self):
        import vw_py
        model_file = self.get_model_file()
        self.vw_process = vw_py.VW(self.vw_test_command_library(model_file))

        # Set the library instance pusher
        self.push_instance = self.predict_push_instance

    def end_predicting_library(self):
        # Close the process
        assert self.vw_process
        self.vw_process.finish()

    def parse_prediction(self, p):
        if self.lda:
            return map(float, p.split())
        else:
            return float(p.split()[0])

    def read_predictions_(self):
        for x in open(self.prediction_file):
            yield self.parse_prediction(x)
        # clean up the prediction file
        os.remove(self.prediction_file)

    def predict_push_instance(self, instance):
        return self.parse_prediction(self.vw_process.learn(('%s\n' % instance).encode('utf8')))

    def make_subprocess(self, command):
        if not self.log_stderr_to_file:
            stdout = open('/dev/null', 'w')
            stderr = open('/dev/null', 'w') if self.silent else sys.stderr
            self.current_stdout = None
            self.current_stderr = None
        else:
            # Save the output of vw to file for debugging purposes
            log_file_base = tempfile.mktemp(dir=self.working_directory, prefix="vw-")
            self.current_stdout = log_file_base + '.out'
            self.current_stderr = log_file_base + '.err'
            stdout = open(self.current_stdout, 'w')
            stderr = open(self.current_stderr, 'w')
            stdout.write(command + '\n')
            stderr.write(command + '\n')
        self.log.debug('Running command: "%s"' % str(command))
        result = subprocess.Popen(shlex.split(str(command)), stdin=subprocess.PIPE, stdout=stdout, stderr=stderr, close_fds=True, universal_newlines=True)
        result.command = command
        return result

    def get_current_stdout(self):
        return open(self.current_stdout)

    def get_current_stderr(self):
        return open(self.current_stderr)

    def get_model_file(self):
        return os.path.join(self.working_directory, self.filename)

    def get_cache_file(self):
        return os.path.join(self.working_directory, '%s.cache' % (self.handle))

    def get_prediction_file(self):
        return os.path.join(self.working_directory, '%s.prediction' % (self.handle))


#jack mcshane
#CSCI-B505 Applied Algorithms
#lab 2: insertion sort

import sys

def insertion_sort(a):
    num_swaps = 0
    for j in range(1, len(a)):
        key = a[j]
        i = j - 1

        while key < a[i] and i >= 0:
            num_swaps += 1
            a[i + 1] = a[i]
            i -= 1
        a[i+1] = key
    return num_swaps

infile = sys.stdin.readlines()
alen = int(infile[0]) # not needed for implementation
a = [int(elem) for elem in infile[1].split()]

print(insertion_sort(a))


import argparse

def main(model_name, layers, acts_name, version):
    print(model_name)
    print(layers)
    print(acts_name)
    print(version)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Save activations')
    parser.add_argument('model_name', type=str,
                        help='Specify the model of which activations are saved')
    parser.add_argument('layers', type=int, nargs='+',
                        help='Specify the layers of which activations are saved')
    parser.add_argument('acts_name', type=str,
                        help='Specify what activations are saved')
    parser.add_argument('version', type=int,
                        help='Version number. Error will be raised if the activations for the specified version' \
                             'already exists and --overwrite flag is not provided.')
    args = parser.parse_args()
    main(args.model_name, args.layers, args.acts_name, args.version)


# -*- coding: utf-8 -*-
import tensorflow as tf
from configuration import cfg
from dataset_process import input_pipeline

test_tfrecord = cfg.MODEL.MODEL.SAVE_DIR
save_model_dir = cfg.MODEL.TFRCORD_TEST

if __name__ == '__main__':

    # GPU settings
    gpus = tf.config.experimental.list_physical_devices('GPU')
    if gpus:
        for gpu in gpus:
            tf.config.experimental.set_memory_growth(gpu, True)

    # get the original_dataset
    test_dataset = input_pipeline(test_tfrecord)
    # load the model
    # model = get_model()
    # model.load_weights(filepath=save_model_dir)
    model = tf.saved_model.load(save_model_dir)

    # Get the accuracy on the test set
    loss_object = tf.keras.metrics.SparseCategoricalCrossentropy()
    test_loss = tf.keras.metrics.Mean()
    test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy()

    @tf.function
    def test_step(images, labels):
        predictions = model(images, training=False)
        t_loss = loss_object(labels, predictions)
        test_loss(t_loss)
        test_accuracy(labels, predictions)

    for test_images, test_labels in test_dataset:
        test_step(test_images, test_labels)
        print("loss: {:.5f}, test accuracy: {:.5f}".format(test_loss.result(),
                                                           test_accuracy.result()))

    print("The accuracy on test set is: {:.3f}%".format(test_accuracy.result()*100))


import time
import urllib.request
from random import randrange
urllib.request._MAXHEADERS = 1000

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())

url = 'url'
driver.get(url)

SCROLL_PAUSE_TIME = 15

last_height = driver.execute_script("return document.body.scrollHeight")

while True:
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

    time.sleep(SCROLL_PAUSE_TIME)

    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height

x_path = 'x_path'
content = driver.find_elements_by_xpath(x_path);

for val in content:
    src = val.get_attribute('src')
    print(src)

    #  urllib.request.urlretrieve(src, 'phone/'+str(randrange(999999999)+'.png')
    urllib.request.urlretrieve(src, 'laptop/'+str(randrange(999999999))+'.png')

driver.quit()


import os
import io
import boto3
import json
import csv

# grab environment variables
ENDPOINT_NAME = os.environ['endpoint_name2']
runtime= boto3.client('runtime.sagemaker')

def lambda_handler(event, context):
    print("Received event: " + json.dumps(event, indent=2))

    data = json.dumps(event)
    payload = data
    print(payload)

    response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME,
                                       #ContentType='text/csv',
                                       ContentType='application/json',
                                       Body=payload)
    print(response)
    result = json.loads(response['Body'].read().decode())
    print(result)
    predictions = result['predictions']

    return predictions


print('From Code Wars.')
print()

def queue_time(customers, n):

    # This function calculates the amount of time required
    # to attend all the customers in a queue.
    total_minutes = 0
    while customers:
        sub = min(customers[0: n])
        total_minutes += sub
        minutes = [num - sub for num in customers[0:n]]
        customers[0: n] = minutes
        customers = [num for num in customers if num != 0]
    return total_minutes

print(queue_time([4,8,5,2], 2))  # Output - 10 minutes
print(queue_time([], 1))  # Outputs - 0 minutes


"""Particle object module for Floor-jumper"""
# Это код на Python, который определяет класс с именем Particle в библиотеке pygame.
# Класс Particle представляет собой отдельный объект particle,
# который может генерироваться и управляться отдельным классом генератора или функцией.
# Класс Particle имеет несколько методов,
# которые определяют его поведение и внешний вид на экране:

# __init__(self, screen, settings, x, y, dx, dy, width, color):
#     метод конструктора класса Particle, который принимает несколько аргументов,
#     включая экранный объект для рисования, настройки системы частиц,
#     начальное положение и скорость частицы, его ширина и цвет.
# update(self): Этот метод обновляет скорость и положение частицы на основе физики системы.
#     Он обновляет координаты x и y частицы на основе ее текущей скорости,
#     а также учитывает влияние силы тяжести и конечной скорости.
# alive(self): Этот метод проверяет, находится ли частица все еще в пределах экрана или нет. Если частица покинула экран, она считается мертвой и может быть удалена из системы.
# draw(self): Этот метод рисует частицу на экране с помощью функции
# pygame.draw.rect(), которая принимает объект экрана, цвет частицы,
# а также положение и размер частицы в качестве аргументов.
# Он просто рисует заполненный прямоугольник на экране в текущем положении частицы.
# В целом, этот код определяет базовый класс Particle,
# который может использоваться для генерации частиц
# и управления ими в системе частиц с использованием библиотеки pygame.
# Класс частиц может быть дополнительно настроен или
# расширен для создания более сложных эффектов частиц.

import pygame

class Particle():
    """A single particle object which is owned by the generator"""
    def __init__(self, screen, settings, x, y, dx, dy, width, color):
        """Save the initial state"""
        self.x = x
        self.y = y
        self.dx = dx
        self.dy = dy
        self.color = color
        self.screen = screen
        self.screen_rect = self.screen.get_rect()
        self.settings = settings
        self.width = width

    def update(self):
        """Update the particle's velocity and position"""
        self.x += self.dx
        self.dy += self.settings.gravity
        if self.dy > self.settings.terminal_velocity:
            self.dy = self.settings.terminal_velocity
        self.y += self.dy

    def alive(self):
        """Once the particle has left the screen, it's not useful, so consider it dead"""
        return self.y <= self.screen_rect.bottom

    def draw(self):
        """Draw the particle at its current location"""
        # We're not a sprite, so just draw a simple filled rect
        pygame.draw.rect(self.screen, self.color, (self.x, self.y, self.width, self.width))


import os
import math

from tqdm import tqdm
from absl import app
from absl import flags

import tensorflow as tf

from .data_loader import get_data

FLAGS = flags.FLAGS

flags.DEFINE_string("data_dir", "../dataset/cropped/", "path to the dataset dir")
flags.DEFINE_string("out_dir", "../dataset/tfrecords/", "path for the output tfrecords")

## For TFRecords
# The following functions can be used to convert a value to a type compatible
# with tf.train.Example.
def _bytes_feature(value):
  """Returns a bytes_list from a string / byte."""
  if isinstance(value, type(tf.constant(0))):
    value = value.numpy() # BytesList won't unpack a string from an EagerTensor.
  return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

def _float_feature(value):
  """Returns a float_list from a float / double."""
  return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))

def _int64_feature(value):
  """Returns an int64_list from a bool / enum / int / uint."""
  return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))

def _float_feature_list(list_val):
    features = [tf.train.Feature(
        float_list=tf.train.FloatList(value=[val])) for val in list_val]
    return tf.train.FeatureList(feature=features)

def convert_data_to_tfrecord(imgs, labels, num_split, out_dir):
    os.makedirs(out_dir, exist_ok=True)
    print("Start converting data into TFRecords.\n")
    num_data = len(imgs)
    num_per_shard = math.ceil(num_data / num_split)

    for shard_id in tqdm(range(num_split)):
        out_fname = os.path.join(out_dir,
                                 "celeb_a-{:02d}-of-{:02d}.tfrecord".format(shard_id,
                                                                            num_split))

        with tf.io.TFRecordWriter(out_fname) as writer:
            start_idx = shard_id * num_per_shard
            end_idx = min((shard_id + 1) * num_per_shard, num_data)
            for i in range(start_idx, end_idx):
                example = image_label_example(imgs[i], labels[i])
                writer.write(example.SerializeToString())

    print("Finished converting data into TFRecords.")

def image_label_example(img_path, label):
    img_string = open(img_path, 'rb').read()
    height, width, channel = tf.image.decode_jpeg(img_string).shape

    feature = {
        "image": _bytes_feature(img_string),
        "height": _int64_feature(height),
        "width": _int64_feature(width),
        "channel": _int64_feature(channel),
    }

    feature_list = {
        "label": _float_feature_list(label),
    }

    return tf.train.SequenceExample(context=tf.train.Features(feature=feature),
                                    feature_lists=tf.train.FeatureLists(
                                                    feature_list=feature_list))

def parse_tfrecords(example_proto):
    # Parse the input tf.train.Example proto using the dictionaries below
    feature_desc = {
        "image": tf.io.FixedLenFeature([], tf.string),
        "height": tf.io.FixedLenFeature([], tf.int64),
        "width": tf.io.FixedLenFeature([], tf.int64),
        "channel": tf.io.FixedLenFeature([], tf.int64),
    }

    feature_list_desc = {
        "label": tf.io.FixedLenSequenceFeature([], dtype=tf.float32)
    }

    inp, trg = tf.io.parse_single_sequence_example(example_proto,
                                                   context_features=feature_desc,
                                                   sequence_features=feature_list_desc)

    img = tf.io.decode_jpeg(inp["image"])
    label = trg["label"]

    return img, label

def main(argv):
    ipath_list, ini_lbl_list = get_data(FLAGS.data_dir)
    convert_data_to_tfrecord(ipath_list, ini_lbl_list, 10, FLAGS.out_dir)

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


from django.urls import re_path
from .views import depsoit_view, PaymentMethodView, bot_view, close_trade, trading_account_view,\
    page

urlpatterns = [
    re_path('deposit/(?P<slug>[-\w]+)/', depsoit_view, name='deposit'),
    re_path('payment-method/', PaymentMethodView.as_view(), name='payment-method'),
    re_path('bot/', bot_view, name='bot'),
    re_path('page/', page, name='page'),
    re_path('close-trade/', close_trade, name='close-trade'),
    re_path('trading-account/', trading_account_view, name='trading-account'),
]


CONFIG = [
    # Extend lines come first...
    #("OutlierToPrevLineFilter/rejected/LEFT", (150, 0, 105), 2),
    #("OutlierToPrevLineFilter/rejected/RIGHT", (150, 0, 105), 2),
    #("OutlierToPrevLineFilter/result/LEFT", (100, 200, 100), 2),
    #("OutlierToPrevLineFilter/result/RIGHT", (100, 200, 100), 2),
    ("LineAverager/result/LEFT", (255, 255, 0), 2),
    ("LineAverager/result/RIGHT", (255, 255, 0), 2),
    ("Smoother/result/LEFT", (255, 0, 255), 2),
    ("Smoother/result/RIGHT", (255, 0, 255), 2),

    # Then non-extended lines, to paint over.
    ("BasicDetector/lines", (0, 255, 0), 2),
    ("LeftRightSplitter/rejected", (0, 150, 105), 2),
    ("LowThetaFilter/rejected/LEFT", (0, 105, 150), 2),
    ("LowThetaFilter/rejected/RIGHT", (0, 105, 150), 2),
]

class Logger:
    def __init__(self):
        self.images = {}
        self.lines = {}

    def log_image(self, name, image):
        self.images[name] = image

    def log_lines(self, name, lines):
        self.lines[name] = lines

    def log_line(self, name, line):
        self.log_lines(name, [line])

    def dump_lines(self, image, drawer):
        for (name, color, thickness) in CONFIG:
            if name not in self.lines:
                print("nothing logged for {}".format(name))
                continue
            drawer.draw_lines(image, self.lines[name], color, thickness)