from discord.ext import commands, flags
from PIL import Image, ImageFont, ImageDraw, ImageOps
import aiohttp
from io import BytesIO
import discord
try:
    from cogs.level import border
except:
    pass
import textwrap
from colour import Color

pokemon_help = """Creates your own pokemon card!
To run this command, you need to provide flag arguments. All argument names start with `--`.

__The flags are:__
`--image`
`--description`
`--weakness`
`--resistance`
`--retreat`
`--color`

*Only `--image` and `--description` are required.*

`--image` can be either a user OR an image URL.
`--description` is the main text of the card and includes attack info. **Attacks are separated with a semicolon**.
`--color` is the color of the text (defaults to white). Hex values are preferred.
`--weakness`, `--resistance`, and `--retreat` are all plain numbers that default to `0`.

**Example:** `$pokemon --image @Stormtorch --description Azusa laugh: stun your opponents for 5 minutes; Cringewarn: je moeder --color #c0ffee --resistance 69`
"""

class Images(commands.Cog):

    def __init__(self, bot):
        self.bot = bot
        self.font = './assets/fonts/sans.ttf'
        self.card = Image.open('./assets/images/pokemon.png')
        self.bed = Image.open('./assets/images/bed.png')
        self.default = Image.open('./assets/images/discord-logo.png')
        self.wtf = Image.open('./assets/images/wtf.png')
        self.wanted = Image.open('./assets/images/wanted.png')

    @commands.command()
    async def wanted(self, ctx, *, user: discord.User = None):
        if not user:
            user = ctx.author

        data = BytesIO(await user.avatar_url_as(format='png').read())

        def executor():
            avatar = Image.open(data)
            avatar = avatar.convert(mode='RGBA')
            avatar = ImageOps.fit(avatar, (405-40, 497-215))
            copy = self.wanted.copy()
            copy.paste(avatar, (40, 215), avatar)

            buf = BytesIO()
            copy.save(buf, 'png')
            buf.seek(0)
            return buf

        buffer = await self.bot.loop.run_in_executor(None, executor)
        await ctx.send(file=discord.File(fp=buffer, filename='wanted.png'))

    @commands.command()
    async def wtf(self, ctx, *, user: discord.User = None):
        if not user:
            user = ctx.author

        data = BytesIO(await user.avatar_url_as(format='png').read())

        def executor():
            image = Image.open(data)
            image = image.convert(mode='RGBA')
            copy = self.wtf.copy()
            image = image.resize((42, 42))
            mask = Image.new("L", image.size, 0)
            draw = ImageDraw.Draw(mask)
            draw.ellipse([(0, 0), image.size], fill=255)
            copy.paste(image, (338, 193), mask)

            buf = BytesIO()
            copy.save(buf, 'png')
            buf.seek(0)
            return buf

        buffer = await self.bot.loop.run_in_executor(None, executor)
        await ctx.send('credits to nemi', file=discord.File(fp=buffer, filename='LOLOL.png'))

    @commands.command()
    async def default(self, ctx, image_url=None):
        if not image_url:
            image = BytesIO(await ctx.author.avatar_url_as(format='png').read())
        else:
            try:
                user = await commands.UserConverter().convert(ctx, image_url)
                image = BytesIO(await user.avatar_url_as(format='png').read())
            except commands.BadArgument:
                try:
                    async with aiohttp.ClientSession() as session:
                        async with session.get(image_url) as resp:
                            if resp.status != 200:
                                return await ctx.send('The image URL you provided isnt working.')
                            image = BytesIO(await resp.read())
                except:
                    return await ctx.send('You need to provide EITHER a user or an image URL.')

        def executor(im):
            image = Image.open(im)
            image = ImageOps.fit(image, self.default.size)
            copy = self.default.copy()
            # draw = ImageDraw.Draw(copy)
            # draw.ellipse([(0, 0), copy.size], fill=(255, 255, 255, 255))
            pixels = copy.load() # create the pixel map
            pixels2 = image.load()
            for i in range(image.size[0]): # for every pixel:
                for j in range(image.size[1]):
                    if pixels[i, j][1] > 0:
                        # change to black if not red
                        pixels[i, j] = pixels2[i, j]
            buf = BytesIO()
            copy.save(buf, 'png')
            buf.seek(0)
            return buf

        buffer = await self.bot.loop.run_in_executor(None, executor, image)
        await ctx.send(file=discord.File(fp=buffer, filename='default.png'))

    @commands.command()
    async def bed(self, ctx, m1: discord.Member, m2: discord.Member = None):
        if not m2:
            m2 = m1
            m1 = ctx.author
        m1 = await m1.avatar_url_as(format='png').read()
        m2 = await m2.avatar_url_as(format='png').read()
        def executor(p1, p2):
            bed = self.bed.copy()
            m1 = Image.open(BytesIO(p1))
            m2 = Image.open(BytesIO(p2))
            m1, m2 = m1.convert(mode='RGBA'), m2.convert(mode='RGBA')
            m1 = m1.resize((250, 250))
            m2 = m2.resize((250, 250))

            mask = Image.new("L", m1.size, 0)
            draw = ImageDraw.Draw(mask)
            draw.ellipse([(0, 0), m1.size], fill=255)

            bed.paste(m1, (260, 380), mask)
            bed.paste(m2, (640, 400), mask)
            buffer = BytesIO()
            bed.save(buffer, 'png')
            buffer.seek(0)
            return buffer
        await ctx.send(file=discord.File(fp=await self.bot.loop.run_in_executor(None, executor, m1, m2), filename='bed.png'))

    @flags.command()
    @flags.add_flag('--image', nargs='+')
    @flags.add_flag('--description', nargs='+')
    @flags.add_flag('--weakness', type=int, default=0)
    @flags.add_flag('--resistance', type=int, default=0)
    @flags.add_flag('--retreat', type=int, default=0)
    @flags.add_flag('--color', '--colour', default=['#FFFFFF'], nargs='+')
    async def pokemon(self, ctx, **options):
        image = options.get('image')
        desc = options.get('description')
        weak = options.get('weakness')
        resis = options.get('resistance')
        retreat = options.get('retreat')
        color = ' '.join(options.get('color'))

        for x in (image, desc):
            if x is None:
                return await ctx.send('You must provide all of the following:\n\n'
                                      '`--image`\n`--description`')
        try:
            color = Color(color)
        except ValueError:
            return await ctx.send(f'`{color}` isn\'t a valid color.')
        rgb = tuple([int(255 * c) for c in color.rgb])
        image = ' '.join(image)
        desc = ' '.join(desc)

        try:
            user = await commands.UserConverter().convert(ctx, image)
            b = BytesIO(await user.avatar_url_as(format='png').read())
        except commands.BadArgument:
            try:
                async with aiohttp.ClientSession() as session:
                    async with session.get(image) as resp:
                        if resp.status != 200:
                            return await ctx.send('Invalid image URL provided.')
                        b = BytesIO(await resp.read())
            except:
                return await ctx.send('Invalid image URL provided.')

        def executor():
            with Image.open(b) as im:
                card = self.card.copy()
                im = ImageOps.fit(im, (344, 229))
                top_left = (38, 62)
                card.paste(im, top_left)

                draw = ImageDraw.Draw(card)
                font = ImageFont.truetype(self.font, 20)

                i = 315
                for ability in desc.split('; '):
                    wrapped = textwrap.wrap(ability, 35)
                    j = 0
                    for line in wrapped:
                        border(draw, (40, i + j), line, font, rgb, (0, 0, 0), 1)
                        j += 20
                    i += j - 20 + 40

                border(draw, (40, 508), str(weak), font, rgb, (0, 0, 0), 1)
                border(draw, (122, 508), str(resis), font, rgb, (0, 0, 0), 1)
                border(draw, (40, 548), str(retreat), font, rgb, (0, 0, 0), 1)

                buf = BytesIO()
                card.save(buf, 'PNG')
                buf.seek(0)
                return buf

        buffer = await self.bot.loop.run_in_executor(None, executor)
        await ctx.send(file=discord.File(fp=buffer, filename='card.png'))

def setup(bot):
    bot.add_cog(Images(bot))
    cmd = bot.get_command('pokemon')
    cmd.help = pokemon_help


from numbers import Number
from typing import (Any,
                    Union,
                    Iterable,
                    Mapping)

PrimitiveTypes = Number, str
PrimitiveType = Union[Union[PrimitiveTypes], None]
IterableType = Iterable[Any]
MappingType = Mapping[str, Any]
ObjectType = Union[PrimitiveType, IterableType, MappingType]


# Stripping Lines for Z->MuMu and studies of Mu efficiencies
# Electroweak Group (Conveners: S.Bifani, J.Anderson; Stripping contact: W.Barter)
#
# S.Bifani
#
# Z02MuMu signal:     StdAllLooseMuons,  pT>15GeV & MM>40GeV
# Z02MuMu efficiency: StdAllNoPIDsMuons, pT>15GeV & MM>40GeV

from Gaudi.Configuration import *
from GaudiConfUtils.ConfigurableGenerators import CombineParticles
from PhysSelPython.Wrappers import Selection
from StrippingConf.StrippingLine import StrippingLine
from StrippingUtils.Utils import LineBuilder
from StandardParticles import StdAllLooseMuons, StdAllNoPIDsMuons

confdict_Z02MuMu = { 'Z02MuMu_Prescale'  : 1.0,
                     'Z02MuMu_Postscale' : 1.0,
                     'pT'    : 15.,
                     'MMmin' : 40.
                     }

default_name = 'Z02MuMu'

class Z02MuMuConf( LineBuilder ) :

    __configuration_keys__ = ( 'Z02MuMu_Prescale',
                               'Z02MuMu_Postscale',
                               'pT',
                               'MMmin'
                               )

    def __init__( self, name, config ) :

        LineBuilder.__init__( self, name, config )

        self._myname = name

        # Define the cuts

        _pT    = '(PT>%(pT)s*GeV)'%config
        _MMmin = '(MM>%(MMmin)s*GeV)'%config

        # Z02MuMu signal

        self.sel_Z02MuMu = makeCombination( self._myname + 'Z02MuMu',
                                            StdAllLooseMuons,
                                            _pT,
                                            _MMmin
                                            )

        self.line_Z02MuMu = StrippingLine( self._myname + 'Line',
                                           prescale  = config[ 'Z02MuMu_Prescale' ],
                                           postscale = config[ 'Z02MuMu_Postscale' ],
                                           checkPV   = False,
                                           selection = self.sel_Z02MuMu
                                           )

        self.registerLine( self.line_Z02MuMu )

        # Z02MuMu efficiency

        self.sel_Z02MuMuNoPIDs = makeCombination( self._myname + 'Z02MuMuNoPIDs',
                                                  StdAllNoPIDsMuons,
                                                  _pT,
                                                  _MMmin
                                                  )

        self.line_Z02MuMuNoPIDs = StrippingLine( self._myname + 'NoPIDsLine',
                                                 prescale  = config[ 'Z02MuMu_Prescale' ],
                                                 postscale = config[ 'Z02MuMu_Postscale' ],
                                                 checkPV   = False,
                                                 selection = self.sel_Z02MuMuNoPIDs
                                                 )

        self.registerLine( self.line_Z02MuMuNoPIDs )

def makeCombination( name, _input, _daughters, _mother ) :

    _combination = CombineParticles( DecayDescriptor    = 'Z0 -> mu+ mu-',
                                     DaughtersCuts      = { 'mu+' : _daughters,
                                                            'mu-' : _daughters },
                                     MotherCut          = _mother,
                                     WriteP2PVRelations = False
                                     )

    return Selection ( name,
                       Algorithm          = _combination,
                       RequiredSelections = [ _input ]
                       )


from dataclasses import dataclass, field
from typing import Optional
from xsdata.models.datatype import XmlDuration
from .entitlement_constraint_structure import EntitlementConstraintStructure
from .entitlement_type_enumeration import EntitlementTypeEnumeration
from .sales_offer_package_ref import SalesOfferPackageRef
from .usage_parameter_version_structure import UsageParameterVersionStructure

__NAMESPACE__ = "http://www.netex.org.uk/netex"

@dataclass
class SalesOfferPackageEntitlementGivenVersionStructure(UsageParameterVersionStructure):
    class Meta:
        name = "SalesOfferPackageEntitlementGiven_VersionStructure"

    sales_offer_package_ref: Optional[SalesOfferPackageRef] = field(
        default=None,
        metadata={
            "name": "SalesOfferPackageRef",
            "type": "Element",
            "namespace": "http://www.netex.org.uk/netex",
        }
    )
    minimum_qualification_period: Optional[XmlDuration] = field(
        default=None,
        metadata={
            "name": "MinimumQualificationPeriod",
            "type": "Element",
            "namespace": "http://www.netex.org.uk/netex",
        }
    )
    entitlement_constraint: Optional[EntitlementConstraintStructure] = field(
        default=None,
        metadata={
            "name": "EntitlementConstraint",
            "type": "Element",
            "namespace": "http://www.netex.org.uk/netex",
        }
    )
    entitlement_type: Optional[EntitlementTypeEnumeration] = field(
        default=None,
        metadata={
            "name": "EntitlementType",
            "type": "Element",
            "namespace": "http://www.netex.org.uk/netex",
        }
    )


#Caesar Cipher
MAX_KEY_SIZE = 26 #Key size - 26 due to 26 letters

def getMode(): #getMode function.
    while True: #True while we decide on mode.
        print('Do you wish to encrypt or decrypt a message?')
        mode = input().lower() #Get user input - ONLY STRING.
        if mode in 'encrypt e decrypt d'.split(): #Does the input contain, encrypt, e, decrypt or d.
            return mode #It does return mode e or d.
        else: #Not a valid entry - try again.
            print('Enter either "encrypt" or "e" or "decrypt" or "d".')

def getMessage(): #getMessage function.
    print('Enter your message:')
    return input() #Return the users input - ONLY STRING.

def getKey(): #getKey function.
    key = 0 #Default key as 0.
    while True: #True while we decide on key / input .
        print('Enter the key number (1-%s)' % (MAX_KEY_SIZE))
        key = int(input()) #Get users input - INT.
        if (key >= 1 and key <= MAX_KEY_SIZE): #Check if key is in the boundaries.
            return key #Return the key value.

def getTranslatedMessage(mode, message, key): #getTranslatedMessage function @param(MODE, USERS MESSAGE, KEY).
    if mode[0] == 'd': #If mode is decrypt.
        key = -key #Reverse key so algorithm below reverses.
    translated = '' #Empty string for the message.

    for symbol in message: #Loop through every letter in the message.
        if symbol.isalpha(): #Check if the letter is an alphabetic characters only.
            num = ord(symbol) #Change the letter into a number - which represents the unicode code of that letter, example: 'a' = 97
            num += key #Add the key to the letter number

            if symbol.isupper(): #Change upper case letters
                if num > ord('Z'):
                    num -= 26
                elif num < ord('A'):
                    num += 26
            elif symbol.islower(): #Change lower case letters
                if num > ord('z'):
                    num -= 26
                elif num < ord('a'):
                    num += 26

            translated += chr(num) #Add letter
        else:
            translated += symbol #Character is not am alphabetic character.
    return translated #Return message

def main():
    mode = getMode() #Call getMode
    message = getMessage() #Call getMessage
    key = getKey() #Call getKey

    print('Your translated text is:')
    print(getTranslatedMessage(mode, message, key)) #Call getTranslatedMessage with the gathered parameters.

main() #Call main()


from datetime import tzinfo
import sys
from decimal import Decimal
from enum import Enum
import uuid
from dateutil import parser
from django.core.serializers.json import DjangoJSONEncoder
from django.db.models import QuerySet, Model
from django.db.models.fields.files import FieldFile
from django.utils.functional import Promise
import re
from django_lazifier.utils.builtin_types.datetime import Dt
from django_lazifier.utils.builtin_types.obj import Obj
from django_lazifier.utils.builtin_types.str import Str

try:
    from django.db.models.query import ValuesQuerySet
except Exception as ex:
    ValuesQuerySet = None

try:
    from phonenumber_field.phonenumber import PhoneNumber
except Exception:
    PhoneNumber = None

class JsonSerializer:
    json_includes = None
    json_excludes = None

    def to_json(self):
        """
        Returns the json string of the current instance of this object.
        """
        try:
            from django_lazifier.utils.json.json import Json
        except ImportError:
            Json = sys.modules[__package__ + '.Json']

        return Json.to_json(self.get_json_dict())

    def get_json_dict(self):
        """
        The dict equivalent of the json object.
        """
        return Obj.get_dict(self, self.json_includes, self.json_excludes)

class RawJsonString:
    """
    Tell json encoder put this string as is to the final json encoding.
    """

    def __init__(self, json_str: str):
        self.json_str = json_str

    def get_json(self):
        return self.json_str

class JsonEncoder(DjangoJSONEncoder):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.__raw_json_replacement = {}

    def encode(self, o):
        json_str = super(JsonEncoder, self).encode(o)
        for ph, raw_json in self.__raw_json_replacement.items():
            placeholder = '"%s"' % ph
            json_str = json_str.replace(placeholder, raw_json)
        return json_str

    def default(self, o):
        try:
            if isinstance(o, ValuesQuerySet) or isinstance(o, QuerySet):
                return [x for x in o]
            elif isinstance(o, JsonSerializer):
                return o.get_json_dict()
            elif isinstance(o, Model):
                return Obj.get_dict(o)
            elif isinstance(o, RawJsonString):
                placeholder = '[$RawJson-%s]' % uuid.uuid4()
                self.__raw_json_replacement[placeholder] = o.get_json()
                return placeholder
            elif isinstance(o, tzinfo):
                return str(o)
            elif isinstance(o, Decimal):
                return float(o)
            elif isinstance(o, Enum):
                return o.value
            elif isinstance(o, Promise):
                # translation proxy
                return str(o)
            elif isinstance(o, FieldFile):
                return o.name
            elif isinstance(o, bytes):
                return o.decode('utf-8', 'replace')
            elif PhoneNumber and isinstance(o, PhoneNumber):
                return o.as_national
        except Exception:
            pass
        return str(o)

def _parse_datetime(dct):
    FORMAT_24H = ('%H:%M:%S', '%H:%M')
    FORMAT_12H = ('%I:%M:%S%p', '%I:%M%p', '%I:%M:%S %p', '%I:%M %p', '%I%p', '%I %p')
    def get_time(time_str):
        if Str.contains_any(time_str, 'am', 'pm', 'a.m.', 'p.m.'):
            return Dt.get_time(time_str, FORMAT_12H, default_value=False)
        elif ':' in time_str:
            return Dt.get_time(time_str, FORMAT_24H, default_value=False)
        return False

    def fuzzy_is_datetime(dt):
        # 2014
        year = re.match(r'\d{4}', dt)
        # 10:30pm
        time = re.match(r'\d?\d\d', dt)
        return bool(year or time)

    for k, v in dct.items():
        if isinstance(v, str) and not Str.is_int(v):
            try:
                time_value = get_time(v)
                if time_value is not False:
                    dct[k] = time_value
                elif fuzzy_is_datetime(v):
                    dct[k] = parser.parse(v)
            except:
                pass
    return dct


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

"""

.. moduleauthor:: Israel Laguna,
.. versionadded:: 1.0.0

"""

from httplib import (BAD_REQUEST, CONFLICT, NOT_FOUND, UNAUTHORIZED, BAD_GATEWAY)

TRANSLATES = {
    "user_not_found_en": "User not found",
    "user_not_found_es": "Usuario no encontrado",
    "user_not_found_ca": "Usuario no encontrado",
    "token_required_en": "Token required",
    "token_required_es": "Token requerido",
    "token_required_ca": "Token requerido",
    "wrong_credentias_en": "Wrong Credentials",
    "wrong_credentias_es": "Credenciales erróneas",
    "wrong_credentias_ca": "Credenciales erróneas",
}

class AppException(Exception):
    """Base Application Exception"""

    pass

class RequiredField(AppException):
    """Exception to be raised if a one or more required fields are empty."""

    def __init__(self, fields):
        self.message = 'There are mandatory fields empty: {}'.format(fields)
        self.code = BAD_REQUEST
        self.fields = fields

class RequiredParameter(AppException):
    """Exception to be raised if a one or more required parameters are empty."""

    def __init__(self, param):
        self.message = 'Parameter {param} cannot be null or empty'. \
            format(param=param)
        self.code = BAD_REQUEST
        self.param = param

class RequiredBody(AppException):
    """Exception to be raised if the request body is empty."""

    def __init__(self):
        self.message = 'Request body cannot be null or empty'
        self.code = BAD_REQUEST

class MalformedBody(AppException):
    """Exception to be raised if the request body is wrong."""

    def __init__(self, payload):
        self.message = 'Request body is wrong'
        self.code = BAD_REQUEST
        self.payload = payload

class BadContentType(AppException):
    """Exception to be raised if the request content type is not the expected."""

    def __init__(self, founded, expected):
        self.message = 'Content Type is {founded} but it must be {expected}'.format(
            founded=founded, expected=expected)
        self.code = BAD_REQUEST

class NoDataFound(AppException):
    """Exception to be raised if the key does not exist."""

    def __init__(self, key=None):
        self.message = 'No data found'
        self.key = key
        self.code = NOT_FOUND

class InvalidCursorLink(AppException):
    """Exception to be raised if a Search Cursor Link is invalid."""

    def __init__(self, cursor):
        self.message = 'Invalid cursor'
        self.cursor = cursor
        self.code = BAD_REQUEST

class EntityExists(AppException):
    """Exception to be raised if an entity to create already exists."""

    def __init__(self, entity_id=None):
        self.message = 'Entity {id} already exists'.format(
            id=entity_id) if entity_id else 'Entity already exists'
        self.entity_id = entity_id
        self.code = CONFLICT

class InvalidFieldValue(AppException):
    """Exception to be raised if an filed contains an invalid value."""

    def __init__(self, param_name=None):
        self.message = 'Field {id} value is not valid'.format(
            id=param_name) if param_name else 'Field value is not valid'
        self.entity_id = param_name
        self.code = BAD_REQUEST

class RequiredAuthorize(AppException):
    """Exception to be raised if the request not send token authorization."""

    def __init__(self, language):
        self.message = TRANSLATES['token_required_' + str(language)]
        self.code = UNAUTHORIZED

class WrongCredentials(AppException):
    """Exception to be raised if the credentials is not the expected."""

    def __init__(self, language, key=None):
        self.message = TRANSLATES['wrong_credentias_' + str(language)]
        self.key = key
        self.code = BAD_REQUEST

class UserNotFound(AppException):
    """Exception to be raised if the user does not exist."""

    def __init__(self, language, key=None):
        self.message = TRANSLATES['timeout_' + str(language)]
        self.key = key
        self.code = NOT_FOUND

class TimeOut(AppException):
    """Exception to be raised if timeout connection."""

    def __init__(self, language, key=None):
        self.message = TRANSLATES['user_not_found_' + str(language)]
        self.key = key
        self.code = BAD_GATEWAY


#encoding:utf-8
import scrapy
import re
from scrapy.selector import Selector
from whunews.items import WhunewsItem
import logging

class whunewsSpider(scrapy.Spider):
    name = 'whunews'
    allowed_domains = ['whu.edu.cn']
    start_urls = ["http://news.whu.edu.cn/wdyw.htm"]

    def parse(self,response):
        urls = response.xpath('//div[@id="page_main"]//ul//div//a/@href').extract()
        titles = response.xpath('//div[@id="page_main"]//ul//div//a/@title').extract()
        dates = response.xpath('//div[@id="page_main"]//ul//div/text()').re(r'\xa0(.*)\xa0')
        contents = [([]*3) for n in range(len(titles))]
        for n in range(len(titles)):
            contents[n].append(urls[n])
            contents[n].append(titles[n])
            contents[n].append(dates[n])
        for content in contents:
            item = WhunewsItem()
            item['news_url'] = content[0]
            item['news_title'] = content[1]
            item['news_date'] = content[2]
            news_url = "http://news.whu.edu.cn/"+content[0]
            request = scrapy.Request(news_url,callback=self.parse_news)
            request.meta['item'] = item
            yield request

    def parse_news(self,response):
        item = response.meta['item']
        news_contents = response.xpath('//div[@id="vsb_newscontent"]//span/text()').extract()
        newcontents = ''
        for element in news_contents:
            newcontents = newcontents + str(element.encode('utf-8'))
        item['news_content'] = newcontents
        yield item


import sys
import itertools
import pandas as pd
from datetime import datetime
from datetime import date

def get_all_correlations(args):
    sys.path.insert(0, args.python_path)
    from utils.Storage import Storage
    from utils.twitter_analytics_helpers import get_parameters
    from Database.BigQuery.backup_table import backup_table, drop_backup_table  # Feature PECTEN-9
    from Database.BigQuery.data_validation import before_insert, after_insert  # Feature PECTEN-9
    from Database.BigQuery.rollback_object import rollback_object  # Feature PECTEN-9

    # Get dataset name
    common_table = "PARAM_READ_DATE"
    common_list = ["BQ_DATASET", "FROM_DATE", "TO_DATE"]
    common_where = lambda x: (x["ENVIRONMENT"] == args.environment) & (x["STATUS"] == 'active')
    common_parameters = get_parameters(args.param_connection_string, common_table, common_list, common_where)

    print("all_correlations")
    # Feature PECTEN-9
    backup_table_name = backup_table(args.google_key_path, common_parameters["BQ_DATASET"],'all_correlations')

    columns = ["Date", "Open", "High", "Low", "Close", "Twitter_sent", "Constituent", "News_sent", "Constituent_id", "From_date", "To_date"]

    query = """
   WITH h AS (SELECT constituent_name,constituent_id,constituent,opening_price,closing_price,
                  daily_high,daily_low, DATE(date) as date
           FROM `{0}.historical`
           WHERE date BETWEEN TIMESTAMP('{1}') AND TIMESTAMP ('{2}')),
     t AS (SELECT constituent_name,constituent_id,constituent,date,AVG(sentiment_score) as sentiment_score
           FROM (SELECT constituent_name,constituent_id,constituent,DATE(date) as date,sentiment_score
                 FROM `{0}.tweets`
                 WHERE date BETWEEN TIMESTAMP('{1}') AND TIMESTAMP ('{2}'))
           GROUP BY date,constituent_name,constituent_id,constituent),
     n AS (SELECT constituent_name,constituent_id,constituent,news_date,AVG(score) as score
           FROM (SELECT constituent_name,constituent_id,constituent,DATE(news_date) as news_date,score
                FROM `{0}.all_news`
                WHERE news_date BETWEEN TIMESTAMP('{1}') AND TIMESTAMP ('{2}'))
           GROUP BY news_date,constituent_name,constituent_id,constituent)
SELECT
  t.date AS Date,
  n.constituent_name,
  n.constituent_id AS Constituent_id,
  n.constituent AS Constituent,
  h.opening_price AS Open,
  h.closing_price AS Close,
  h.daily_high AS High,
  h.daily_low AS Low,
  t.sentiment_score + 1 AS Twitter_sent,
  n.score + 1 AS News_sent,
  TIMESTAMP('{1}') AS From_date,
  TIMESTAMP('{2}') AS To_date
FROM
  h,t,n
WHERE
  t.date = h.date
  AND t.constituent_id = h.constituent_id
  AND h.date = n.news_date
  AND h.constituent_id = n.constituent_id
GROUP BY
  t.date,
  n.constituent_name,
  n.constituent_id,
  n.constituent,
  h.opening_price,
  h.closing_price,
  h.daily_high,
  h.daily_low,
  t.sentiment_score,
  n.score
    """.format(common_parameters["BQ_DATASET"],common_parameters["FROM_DATE"].strftime("%Y-%m-%d"),
               common_parameters["TO_DATE"].strftime("%Y-%m-%d"))

    storage_client = Storage(args.google_key_path)

    result = storage_client.get_bigquery_data(query, iterator_flag=True)
    to_insert = []

    for item in result:
        to_insert.append(dict((k,item[k].strftime('%Y-%m-%d %H:%M:%S')) if isinstance(item[k],datetime) or isinstance(item[k],date) else
                   (k,item[k]) for k in columns))

    #Feature PECTEN-9
    from_date = common_parameters["FROM_DATE"].strftime("%Y-%m-%d %H:%M:%S")
    to_date = common_parameters["TO_DATE"].strftime("%Y-%m-%d %H:%M:%S")
    try:
        before_insert(args.google_key_path,common_parameters["BQ_DATASET"],'all_correlations',
                      from_date,to_date,storage_client)
    except AssertionError as e:
        drop_backup_table(args.google_key_path,common_parameters["BQ_DATASET"],backup_table_name)
        e.args += ("Data already exists",)
        raise

    try:
        print("Inserting into BQ")
        # Feature PECTEN-9
        if storage_client.insert_bigquery_data(common_parameters["BQ_DATASET"],
                                            'all_correlations', to_insert):
            print("Data inserted to BQ")
        else:
            drop_backup_table(args.google_key_path, common_parameters["BQ_DATASET"], backup_table_name)
            return
    except Exception as e:
        print(e)
        rollback_object(args.google_key_path, 'table', common_parameters["BQ_DATASET"], None,
                        'country_data', backup_table_name)
        raise

    #Feature PECTEN-9
    try:
        after_insert(args.google_key_path,common_parameters["BQ_DATASET"],'all_correlations',
                     from_date,to_date,storage_client)
    except AssertionError as e:
        e.args += ("No data was inserted.",)
        raise
    finally:
        drop_backup_table(args.google_key_path, common_parameters["BQ_DATASET"], backup_table_name)

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('python_path', help='The connection string')
    parser.add_argument('google_key_path', help='The path of the Google key')
    parser.add_argument('param_connection_string', help='The MySQL connection string')
    parser.add_argument('environment', help='production or test')
    args = parser.parse_args()
    get_all_correlations(args)


"""
train merge models with SVM liblinear
"""
from liblinear.liblinearutil import *
import pandas as pd
import smote_variants as sv
import imbalanced_databases as imbd
import argparse, os
from pathlib import Path

def convert_model_format(model_in, model_out, features="tfidf_time"):

    if features == "tfidf":
        if "tdt4" in model_in:
            feature_list = ['Entities_all','Lemmas_all', 'Tokens_all', 'ZZINVCLUSTER_SIZE']
        else:
            feature_list = ['Entities_all', 'Entities_body', 'Entities_title',
                            'Lemmas_all', 'Lemmas_body', 'Lemmas_title',
                            'Tokens_all', 'Tokens_body', 'Tokens_title',
                            'ZZINVCLUSTER_SIZE']
    elif features == "tfidf_time":
        if "tdt4" in model_in:
            feature_list = ['Entities_all','Lemmas_all', 'Tokens_all', 'NEWEST_TS', 'OLDEST_TS', 'RELEVANCE_TS', 'ZZINVCLUSTER_SIZE']
        else:
            feature_list = ['Entities_all', 'Entities_body', 'Entities_title',
                            'Lemmas_all', 'Lemmas_body', 'Lemmas_title',
                            'Tokens_all', 'Tokens_body', 'Tokens_title',
                            'NEWEST_TS', 'OLDEST_TS', 'RELEVANCE_TS', 'ZZINVCLUSTER_SIZE']
    elif features in set(['tfidf_time_sbert', 'tfidf_time_esbert', 'tfidf_time_sinpe_esbert']):
        if "tdt4" in model_in:
            feature_list = ['Entities_all','Lemmas_all', 'Tokens_all', 'NEWEST_TS', 'OLDEST_TS', 'RELEVANCE_TS', 'ZZINVCLUSTER_SIZE', 'bert_sent_embeds']
        else:
            feature_list = ['Entities_all', 'Entities_body', 'Entities_title',
                            'Lemmas_all', 'Lemmas_body', 'Lemmas_title',
                            'Tokens_all', 'Tokens_body', 'Tokens_title',
                            'NEWEST_TS', 'OLDEST_TS', 'RELEVANCE_TS', 'ZZINVCLUSTER_SIZE', 'bert_sent_embeds']
    elif features == "tfidf_sinpe_esbert":
        if "tdt4" in model_in:
            feature_list = ['Entities_all', 'Lemmas_all', 'Tokens_all', 'ZZINVCLUSTER_SIZE', 'bert_sent_embeds']
        else:
            feature_list = ['Entities_all', 'Entities_body', 'Entities_title', 'Lemmas_all', 'Lemmas_body', 'Lemmas_title', 'Tokens_all', 'Tokens_body', 'Tokens_title', 'ZZINVCLUSTER_SIZE', 'bert_sent_embeds']
    elif features == "time_sinpe_esbert":
        feature_list = ['NEWEST_TS', 'OLDEST_TS', 'RELEVANCE_TS', 'ZZINVCLUSTER_SIZE', 'bert_sent_embeds']

    elif features == "sinpe_esbert":
        feature_list = ['ZZINVCLUSTER_SIZE', 'bert_sent_embeds']

    with open(model_in) as f:
        num_lines = len(f.readlines())-1 # excluding the extra one in the end

    with open(model_in) as f, open(model_out, "w") as out:
        featIdx = 0
        # bias
        lines = f.readlines()
        out.write(lines[-1].strip())
        out.write("\n")

        # weights
        # print(feature_list)
        for line in lines[6:-1]:
            line = feature_list[featIdx] + "\t" + line
            featIdx +=1
            out.write(line)

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--data_path", type=str, default="./output/exp_time_esbert_ep2_mgn2.0_btch8_norm1.0_max_seq_128/tfidf_time", help="input_folder")
    parser.add_argument("--features", type=str, default="tfidf_time", help="input_folder")
    parser.add_argument("--use_smote", type=int, default=0, help="input_folder")
    parser.add_argument("-f")
    args = parser.parse_args()

    y, X = svm_read_problem(args.data_path)
    # y, X = svm_read_problem(os.path.join(args.input_folder, 'train_svmlib_balanced.dat'))
    args.input_folder = os.path.dirname(args.data_path)
    X = pd.DataFrame(X).values
    if args.use_smote:
        print("using oversampled data")
        oversampler= sv.SVM_balance()
        X_samp, y_samp = oversampler.sample(X, y)
        args.output_folder = os.path.join(args.input_folder, "svm_merge_models_smote")
    else:
        print("using original data")
        X_samp, y_samp = X, y
        args.output_folder = os.path.join(args.input_folder, "svm_merge_models")

    Path(args.output_folder).mkdir(parents=True, exist_ok=True)

    # for c in [0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5]:
    for c in [0.0000001, 0.000001, 0.00001, 0.00002, 0.00003, 0.00004, 0.00005, 0.00006, 0.00007, 0.00008, 0.00009, 0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 10, 100, 1000, 10000, 100000, 1000000, 10000000]:
    # for c in [0.0001]:
        m = train(y_samp, X_samp, '-c {} -B 0.0'.format(c))
        model_in = os.path.join(args.output_folder, 'libSVM_c{}_b0.model'.format(c))
        model_out = os.path.join(args.output_folder, 'libSVM_c{}_b0.md'.format(c))
        save_model(model_in, m)
        print("saving model {}".format(model_out))
        convert_model_format(model_in, model_out, features=args.features)

    # for c in [0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5]:
    # for c in [0.0000001, 0.000001, 0.00001, 0.00002, 0.00003, 0.00004, 0.00005, 0.00006, 0.00007, 0.00008, 0.00009, 0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 10, 100, 1000, 10000, 100000, 1000000, 10000000]:
    #     m = train(y_samp, X_samp, '-c {} -B 1.0'.format(c))
    #     model_in = os.path.join(args.output_folder, 'libSVM_c{}_b1.model'.format(c))
    #     model_out = os.path.join(args.output_folder, 'libSVM_c{}_b1.md'.format(c))
    #     save_model(model_in, m)
    #     print("saving model {}".format(model_out))
    #     convert_model_format(model_in, model_out, features=args.features)

if __name__ == "__main__":
    main()


import tkinter as tk
from PIL import Image,ImageTk
from random import randint
import time

MOVE_INCREMENT = 20
moves_per_second = 12
GAME_SPEED = 1000 // moves_per_second

class Snake(tk.Canvas):
    def __init__(self):
        super().__init__(width=600, height=620, background='black', highlightthickness=0)

        self.snake_positions = [(100, 100),(80, 100),(60, 100)]
        self.food_positions = self.set_new_food_position()
        self.score=0
        self.direction = "Right"
        self.bind_all("<Key>", self.on_key_press)

        self.load_assests()
        self.create_object()

        self.after(50, self.perform_action )

    def load_assests(self):
        try:                   #try because if this fails it wont work
            self.snake_body_image=Image.open('C:/Users/INTEL/PycharmProjects/pythonProject1/iig security/img_buttons/snake.png')
            self.snake_body=ImageTk.PhotoImage(self.snake_body_image )

            self.food_body_image = Image.open('C:/Users/INTEL/PycharmProjects/pythonProject1/iig security/img_buttons/food111.png ')
            self.food_body = ImageTk.PhotoImage(self.food_body_image)
        except IOError as error:
            print(error)
            root.destroy()

    def create_object(self):
        self.create_text(
            100 , 12, text=f"score: {self.score} (speed: {moves_per_second })", tag="score", fill='white',font=('TkDefaultFont',15)
        )
        for x_position, y_position in self.snake_positions:
            self.create_image(x_position, y_position, image=self.snake_body,tag="snake")

        self.create_image(*self.food_positions,image=self.food_body, tag="food")
        self.create_rectangle(7,27,595,613,outline='white')

    def move_snake(self):
        head_x_position, head_y_position = self.snake_positions [0]
        new_head_position = (head_x_position + MOVE_INCREMENT, head_y_position)

        if self.direction == "Left":
            new_head_position = (head_x_position - MOVE_INCREMENT, head_y_position  )
        elif self.direction == "Right":
            new_head_position = (head_x_position + MOVE_INCREMENT, head_y_position  )
        elif self.direction == "Down":
            new_head_position = (head_x_position, head_y_position + MOVE_INCREMENT)
        elif self.direction == "Up":
            new_head_position = (head_x_position,head_y_position - MOVE_INCREMENT )

        #new_head_position = (head_x_position + MOVE_INCREMENT,head_y_position)

        self.snake_positions = [new_head_position ] + self.snake_positions [:-1]

        for segment, position in zip(self.find_withtag("snake"), self.snake_positions):
            self.coords(segment, position)

    def perform_action(self):
        if self.check_collisions() :
            self.end_game()
            return

        self.check_food_collision()
        self.move_snake()
        self.after(GAME_SPEED,self.perform_action)

    def check_collisions(self):
        head_x_position, head_y_position = self.snake_positions[0]

        if head_x_position in (0, 600) or head_y_position in (20, 620):
            return True
        elif (head_x_position, head_y_position) in self.snake_positions[1:]:
            return True

    def on_key_press(self,e):
        new_direction = e.keysym
        all_directions=("Up","Down","Left","Right")
        opposites = ({"Up","Down"},{"Left","Right"})

        if (
            new_direction in all_directions
            and{new_direction, self.direction  } not in opposites
        ):
            self.direction = new_direction

    def check_food_collision(self):
        if self.snake_positions[0] == self.food_positions:
            self.score += 5
            self.snake_positions.append(self.snake_positions[-1])

            if self.score % 5 == 0:
                global moves_per_second
                moves_per_second += 1

            self.create_image(
                *self.snake_positions[-1],image=self.snake_body,tag="snake"
            )

            self.food_positions = self.set_new_food_position()
            self.coords(self.find_withtag("food"),*self.food_positions)

            score = self.find_withtag("score")
            self.itemconfigure(score,text=f"Score: {self.score} (speed: {moves_per_second })",tag="score")

    def set_new_food_position(self):
        while True :
            x_position = randint(1,29)  * MOVE_INCREMENT
            y_position = randint(3,30)  * MOVE_INCREMENT
            food_position = (x_position,y_position  )

            if food_position not in self.snake_positions:
                return food_position

    def end_game(self):
        self.delete(tk.ALL)
        self.create_text(
            self.winfo_width() / 2,
            self.winfo_height() / 2,
            text=f"Game Over ! You Scored {self.score} !",
            fill='white',
            font=('TkDefaultFont',25)
        )

root=tk.Tk()
root.title('image')
root.iconbitmap('C:/Users/INTEL/PycharmProjects/pythonProject1/pics/tk1.3.ico')

board=Snake()
board.pack()

root.mainloop()


import numpy as np
import shm
from shm import kalman
from shm.watchers import watcher
import aslam
from mission.framework.task import Task
from mission.framework.combinators import Concurrent, Sequential
from mission.framework.movement import Heading, Roll, Pitch, Depth, VelocityX, VelocityY, \
    RelativeToCurrentHeading, RelativeToCurrentDepth
from mission.framework.position import MoveX, MoveY
from mission.framework.targeting import PIDLoop
from mission.framework.timing import Timer
from mission.framework.helpers import get_camera_size, call_if_function
from mission.framework.primitive import Zero
from mission.opt_aux.aux import Mode

vision = None

class Vision(Task):
    """ Get vision results and call the main task
    """
    def on_first_run(self, main_task, *args, **kwargs):
        self.main_task = main_task
        self.watcher = watcher()
        self.watcher.watch(shm.navigate_results)
        self.pull()

    def on_run(self, main_task, *args, **kwargs):
        if self.watcher.has_changed():
            self.pull()

        if not self.main_task.has_ever_finished:
            self.main_task(main_task, *args, **kwargs)
        else:
            self.finish()

    def pull(self):
        global vision
        vision = shm.navigate_results.get()

class Style(Task):
    """Base class for all styles

    Start: facing center of gate
    Finish: facing away from center of gate
    """
    def on_first_run(self, *args, **kwargs):
        # `self.__class__.__name__` gets the name of the class from an instance
        self.logi('Starting')
        self.style_on_first_run(*args, **kwargs)

    def on_run(self, *args, **kwargs):
        self.style_on_run(*args, **kwargs)

    def on_finish(self, *args, **kwargs):
        self.style_on_finish(*args, **kwargs)
        Zero()()
        self.logi('Finished in {} seconds!'.format(
            self.this_run_time - self.first_run_time))

    """
    These should be overridden by child style classes
    """
    def style_on_first_run(self, *args, **kwargs):
        pass
    def style_on_run(self, *args, **kwargs):
        pass
    def style_on_finish(self, *args, **kwargs):
        pass

class StyleBasic(Style):
    """Simply moves forward
    """
    def style_on_first_run(self, distance=5, *args, **kwargs):
        self.movement = MoveX(distance)

    def style_on_run(self, *args, **kwargs):
        if not self.movement.has_ever_finished:
            self.movement()
        else:
            self.finish()

class StyleSideways(Style):
    """Heading changes 90 degrees starboard, so that sub is facing either right or left

    If `starboard` is False, then heading changes 90 degrees port
    """
    def style_on_first_run(self, starboard=True, *args, **kwargs):
        current_heading = kalman.heading.get()
        if starboard:
            change_heading = Heading(current_heading + 90, error=1)
            movement = MoveY(-5)
        else:
            change_heading = Heading(current_heading - 90, error=1)
            movement = MoveY(5)
        heading_restore = Heading(current_heading, error=1)
        self.style_sideways = Sequential(change_heading, movement, heading_restore)

    def style_on_run(self, *args, **kwargs):
        if not self.style_sideways.has_ever_finished:
            self.style_sideways()
        else:
            self.finish()

class StyleUpsideDown(Style):
    """Roll changes 180 degrees, so that sub is upside down
    """
    def style_on_first_run(self, *args, **kwargs):
        change_roll = Roll(180, error=1)
        movement = MoveX(5)
        restore_roll = Roll(0, error=1)
        self.style_upside_down = Sequential(change_roll, movement, restore_roll)

    def style_on_run(self, *args, **kwargs):
        if not self.style_upside_down.has_ever_finished:
            self.style_upside_down()
        else:
            self.finish()

class StylePitched(Style):
    """
    Pitch changes 75 degrees, so that sub is facing either down or up
    The reason for 75 degrees is so that th sub does not rapidly twist back
    and forth, in an attempt to maintain a stable heading

    If `up` is False, then sub pitches downwards
    """
    def style_on_first_run(self, up=True, *args, **kwargs):
        if up:
            change_pitch = Pitch(75, error=1)
        else:
            change_pitch = Pitch(-75, error=1)
        movement = MoveX(5)
        restore_pitch = Pitch(0, error=1)
        self.style_pitched = Sequential(change_pitch, movement, restore_pitch)

    def style_on_run(self, *args, **kwargs):
        if not self.style_pitched.has_ever_finished:
            self.style_pitched()
        else:
            self.finish()

class StyleLoop(Style):
    """Does a loop around the center bar of the channel

    Goes forward and under, backwards and over, then forwards and over
    """
    def style_on_first_run(self, *args, **kwargs):
        move_distance = 5 # meters
        depth_offset = 1 # offset to go up or down

        def generate_curve(distance, depth_offset, depth, iterations):
            #TODO: Make this curve more 'curvy'
            movement = []
            dist_tick = distance / iterations
            current_depth = depth
            depth_tick = depth_offset / (iterations - 1)
            for t in range(iterations):
                movement.append(Concurrent(MoveX(dist_tick), Depth(current_depth, error=.1)))
                current_depth += depth_tick
            return Sequential(subtasks=movement)

        current_depth = kalman.depth.get()
        forward_and_down = generate_curve(move_distance / 2, depth_offset, current_depth, 3)
        forward_and_up = generate_curve(move_distance / 2, -depth_offset, current_depth + depth_offset, 3)
        backward_and_up = generate_curve(-move_distance / 2, -depth_offset, current_depth, 3)
        backward_and_down = generate_curve(-move_distance / 2, depth_offset, current_depth - depth_offset, 3)
        forward = Sequential(generate_curve(move_distance / 2, -depth_offset, current_depth, 3),
                             generate_curve(move_distance / 2, depth_offset, current_depth - depth_offset, 3))
        self.style_loop = Sequential(forward_and_down, forward_and_up, backward_and_up,
                                     backward_and_down, forward)

    def style_on_run(self, *args, **kwargs):
        if not self.style_loop.has_ever_finished:
            self.style_loop()
        else:
            self.finish()

class StyleSuperSpin(Style):
    def style_on_first_run(self, clockwise=True, steps=5, spins=2, *args, **kwargs):
        initial_heading = shm.kalman.heading.get()
        subspins = []
        for spin in range(spins):
            for step in range(steps):
                angle = 360 / steps * (step+1)
                if not clockwise: angle = 360 - angle
                subspins.append(Heading(initial_heading + angle, error=10))

        self.movement = Sequential(
            MoveY(0.75, deadband=0.1),
            MoveX(2, deadband=0.2),
            Concurrent(
                MoveX(3, deadband=0.2),
                Sequential(subtasks=subspins),
            )
        )

    def style_on_run(self, *args, **kwargs):
        if not self.movement.has_ever_finished:
            self.movement()
        else:
            self.finish()

DEFAULT_DEADBAND = 0.03

class AlignHeading(Task):
    def on_first_run(self, *args, **kwargs):
        self.pid = PIDLoop(
            input_value=self.x_ratio,
            output_function=RelativeToCurrentHeading(),
            target=0.5,
            deadband=DEFAULT_DEADBAND/3,
            p=40,
            d=20,
            negate=True,
        )

    def on_run(self, *args, **kwargs):
        # Try to center on entire gate first
        if vision.bottom_prob or (vision.left_prob and vision.right_prob):
            self.pid()

        elif vision.left_prob: # Otherwise try to find other vertical bar
            RelativeToCurrentHeading(1)()
        elif vision.right_prob:
            RelativeToCurrentHeading(-1)()

        if self.pid.finished:
            self.finish()

    def x_ratio(self):
        """ Precondition: we can see the bottom bar, or the left and right bars
        """
        CAM_WIDTH, CAM_HEIGHT = get_camera_size(vision)

        if vision.bottom_prob:
            avg_x = (vision.bottom_x1 + vision.bottom_x2) / 2
            return avg_x / CAM_WIDTH
        else: # We see left and right bars
            left_x_avg = (vision.left_x1 + vision.left_x2) / 2
            right_x_avg = (vision.right_x1 + vision.right_x2) / 2
            avg = (left_x_avg + right_x_avg) / 2
            return avg / CAM_WIDTH

class AlignDepth(Task):
    def on_first_run(self, *args, **kwargs):
        self.pid = PIDLoop(
            input_value=self.y_ratio,
            output_function=RelativeToCurrentDepth(),
            target=0.75,
            deadband=DEFAULT_DEADBAND,
            p=2,
            negate=True,
        )

    def on_run(self, *args, **kwargs):
        if vision.bottom_prob or vision.left_prob or vision.right_prob:
            self.pid()

        if self.pid.finished:
            self.finish()

    def y_ratio(self):
        """ Precondition: we can see at least one of the bars
        """
        CAM_WIDTH, CAM_HEIGHT = get_camera_size(vision)

        if vision.bottom_prob:
            avg_y = (vision.bottom_y1 + vision.bottom_y2) / 2
            return avg_y / CAM_HEIGHT
        elif vision.left_prob or vision.right_prob:
            return 1.1

class AlignFore(Task):
    def on_first_run(self, *args, **kwargs):
        self.pid = PIDLoop(
            input_value=self.width_ratio,
            output_function=VelocityX(),
            target=0.55,
            deadband=DEFAULT_DEADBAND,
            p=2,
        )

        # Maximum ratio of camera width bottom bar can be away from camera
        # edge
        self.EDGE_PROXIMITY = 0.05

        # Speed to back away from the bottom bar at when too close
        self.BACKUP_SPEED = 0.15

        # Speed to approach the gate at when not fully visible
        self.APPROACH_SPEED = 1

        # Min width of gate to begin carefully approaching
        self.MIN_WIDTH = 0.3

        self.STATE_INFO = {
            'lost': "Can't see full gate, flooring it",
            'found': 'Gate found, aligning',
            'too close': 'Too close to gate, backing up',
        }
        self.state = 'lost'
        self.old_state = ''

    def on_run(self, *args, **kwargs):
        if vision.bottom_prob:
            # If the bottom bar touches the edge of the camera image, we're too
            # close and need to back up a bit. Otherwise, try to make it fill a
            # portion of the camera's width.
            CAM_WIDTH, CAM_HEIGHT = get_camera_size(vision)

            left_x, right_x = vision.bottom_x1, vision.bottom_x2
            if left_x > right_x:
                left_x, right_x = right_x, left_x
            left_prox = left_x / CAM_WIDTH
            right_prox = 1 - (right_x / CAM_WIDTH)

            top_y, bottom_y = vision.bottom_y1, vision.bottom_y2
            if top_y > bottom_y:
                top_y, bottom_y = bottom_y, top_y
            top_prox = top_y / CAM_HEIGHT
            bottom_prox = 1 - (bottom_y / CAM_HEIGHT)

            if left_prox < self.EDGE_PROXIMITY or right_prox < self.EDGE_PROXIMITY or \
                    top_prox < self.EDGE_PROXIMITY or bottom_prox < self.EDGE_PROXIMITY:
                VelocityX(-self.BACKUP_SPEED)()
                self.state = 'too close'

            else:
                self.fast_approach()

        else:
            self.fast_approach()

        if self.state != self.old_state:
            self.logi(self.STATE_INFO[self.state])
            self.old_state = self.state

        if self.pid.finished:
            self.finish()

    def fast_approach(self):
        """ If we're too far from the gate, approach fast. Otherwise, carefully
        align to a fixed distance from the gate.
        """
        if (vision.bottom_prob or \
                (vision.left_prob and vision.right_prob)) and \
                self.width_ratio() >= self.MIN_WIDTH:
            self.pid()
            self.state = 'found'
        else:
            VelocityX(self.APPROACH_SPEED)()
            self.state = 'lost'

    def width_ratio(self):
        CAM_WIDTH, CAM_HEIGHT = get_camera_size(vision)

        if vision.bottom_prob:
            length = abs(vision.bottom_x1 - vision.bottom_x2)
            return length / CAM_WIDTH
        else:
            # We can see both left and right bars
            left_x = (vision.left_x1 + vision.left_x2) / 2
            right_x = (vision.right_x1 + vision.right_x2) / 2
            return (right_x - left_x) / CAM_WIDTH

class AlignSway(Task):
    def on_first_run(self, *args, **kwargs):
        self.pid = PIDLoop(
            input_value=self.height_diff_ratio,
            output_function=VelocityY(),
            target=0,
            deadband=0.01,
            p=20,
            d=10,
        )

    def on_run(self, *args, **kwargs):
        # If we see both bars, try to sway to minimize their height difference
        if vision.left_prob and vision.right_prob and vision.bottom_prob:
            self.pid()

        if self.pid.finished:
            self.finish()

    def height_diff_ratio(self):
        CAM_WIDTH, CAM_HEIGHT = get_camera_size(vision)
        left_height = abs(vision.left_y1 - vision.left_y2)
        right_height = abs(vision.right_y1 - vision.right_y2)
        return (right_height - left_height) / CAM_HEIGHT

class AlignChannel(Task):
    def on_first_run(self, *args, **kwargs):
        self.pids_task = Concurrent(
            AlignHeading(),
            AlignDepth(),
            AlignFore(),
            AlignSway(),
            finite=False,
        )
        self.logi('Starting')

    def on_run(self, *args, **kwargs):
        if not self.pids_task.finished:
            self.pids_task()
        else:
            self.finish()

    def on_finish(self, *args, **kwargs):
        Zero()()
        self.logi('Finished')

class OptimalMission(Task):
    def on_first_run(self, mode=None, main_task=None, *args, **kwargs):
        self.main_task = main_task

    def on_run(self, mode=None, main_task=None, *args, **kwargs):
        if not self.main_task.has_ever_finished:
            self.main_task(mode, *args, **kwargs)
        else:
            self.finish()

    def desiredModules(self):
        return [shm.vision_modules.Navigate]

    def possibleModes(self):
        if self.finished:
            return []
        else:
            return [Mode(name='Full', expectedPoints=800, expectedTime=60)]

align = lambda: Vision(AlignChannel())
barrel_roll = lambda: Vision(StyleBarrelRoll())
unroll = lambda: Vision(StyleBarrelRoll(clockwise=False, period=3, velocity=0))
full = lambda: OptimalMission(main_task=Vision(Sequential(
    # aslam.Target(obj=aslam.world.navigation, finalPosition=np.array([0, -3, 0]), finalTolerance = np.array([0.05, 0.05, 0.05]), observationBoundingBox = (np.array([-2, -2, 0]), np.array([-1, -1, 0]) ) ),
    # aslam.Orient(aslam.world.navigation),
    AlignChannel(),
    StyleSuperSpin(),
)))
basicfull = lambda: Vision(Sequential(AlignChannel(), StyleBasic()))

basic = lambda: Vision(StyleBasic())
pitched = lambda: Vision(StylePitched())
sideways = lambda: Vision(StyleSideways())
upside_down = lambda: Vision(StyleUpsideDown())
loop = lambda: Vision(StyleLoop())
superspin = lambda: StyleSuperSpin()

class Flip180(Task):
    def on_first_run(self, *args, **kwargs):
        #heading = Heading((kalman.heading.get() + 180) % 360, error=1)
        self.flip = Sequential(Pitch(0, error=1), Roll(0, error=1), Timer(1.5), Heading(lambda: kalman.heading.get() + 180, error=1), Timer(1))
    def on_run(self, *args, **kwargs):
        self.flip()
        if self.flip.has_ever_finished:
            self.finish()

est_all = lambda: Sequential(basic(), Heading(90, error=1), pitched(), Heading(270, error=1), sideways(), Heading(90, error=1), upside_down(), Heading(270, error=1), Depth(2.1, error=.1), loop())


s = 7
nums = [1,4,4]

class Solution:
    def minSubArrayLen(self, s, nums) -> int:
        limit = s
        if not nums:
            return 0
        size = len(nums)
        left, right = 0, 0
        sum, ans = 0, float("inf")
        while right <= size -1 and left <= size -1 and left <= right:
            while sum < limit and right <= size -1:  #找到最近的，长度 > sum的字符串
                sum += nums[right]
                right += 1
            while  sum >= limit and left <= size -1 and left <= right:# 压缩左边界， 让sum < 目标数
                sum -= nums[left]
                left += 1

                length = right - left + 1  # 更新窗口大小，如果没有进到这一步，说明没有符合要求的窗口，不更新ans
                if length < ans:
                    ans = length

        if ans == float("inf"):
            return 0
        else:
            return ans

    def minSubArrayLen1(self, s: int, nums) -> int:
        if not nums:
            return 0
        length = len(nums)
        elem_sum = nums.copy()
        for i in range(1, length):
            elem_sum[i] += elem_sum[i-1]
        left = right = 0
        ans = float("inf")
        for right, num in enumerate(nums):
            while left <= right and elem_sum[right] - elem_sum[left] + nums[left] >= s:
                ans = min(ans,right -left + 1)
                left += 1
        return ans if ans != float("inf") else 0

print(Solution().minSubArrayLen(3,[1,1]))

滑动窗口/209. 长度最小的子数组.py

209. 长度最小的子数组.py


import pandas as pd

# Reads the excel file so you can work with it in python
df1 = pd.read_excel(r'C:\Users\Rislynn\Documents\Python Pandas Excels\VizData.xlsx')
df2 = pd.read_excel(r'C:\Users\Rislynn\Documents\Python Pandas Excels\CustomerData.xlsx')

# Takes a data frame as an input and returns a data frame with only the specified columns
# You specify the columns with an array of 0 and 1 whose length = # columns
# a value of 1 in the ith postion (i = 1 to # columns) indicates that column should be retained
def delete_cols(d,a):  # d is a dataframe and a is array of 0,1 that indicate if column in that position shoudl be kept
    # if len(a) != len(d.columns):
    #   print("Array is not long enough")
    col_to_keep = []  # initializes array that will contain the values of the columns to keep

    for i in range(len(a)):
        if a[i] == 1:  # If the ith entry is 1 then you will add this column to the list of ones to keep
            col_to_keep.append(i)

    return d.iloc[:, col_to_keep]

# print(delete_cols(df1,[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0]))

def reorder_cols(d,a):  # d is a dataframe, a is an array with the index of the column in the order you would like now
#[0,1,5,6,7,8,2,3,4] column 5 would move to the third position for example
    return d.iloc[:, a]

#print(reorder_cols(df1,[0,1,5,6,7,8,2,3,4]))

def merge_df(d, f, k):  #dataframe1, dataframe2, keys = # columns at the beginning to use for join
    key_col_array = []
    for i in range(k):
        key_col_array.append(d.columns[i])  # Adds the ith column name to the array (d1 and d2 need same first key columns)
    result = pd.merge(d, f, how='outer', on=key_col_array)  # Joins df and df2 on the first num_key_columns
    return result
    #print(merge_df(df1,df2,2))

#function to create the variances out of a merged
#first columns = index + # key columns

def var_col(d,k):  #Takes merged_df and makes variances columns, k = number of key columns
    c = (len(d.columns) - k)//2
    for i in range(k, 2+c):
        first_col_name = d.columns[i]
        second_col_name = d.columns[i+c]
        var_col_name = "Var " + first_col_name[0:len(d.columns[i])-2]
        if type(d.iloc[1, i]) == str:  #Checks to see if the first entry of the column is a string, probalby needs to be more robust, like check more than just the first entry
            d[var_col_name] = d[first_col_name] == d[second_col_name]
        else:
            d[var_col_name] = d[first_col_name] - d[second_col_name]
    return d

######################################################################
#Steps:
#1 Import both excels
#2 Run delete_cols on both excels
#3 Run Reorder Cols on both excels
#4 Run merge_df on both data frames with the number of keys
#5 Run var_col on the resulting data frame from the last step

clean_df1 = delete_cols(df1, [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0])
clean_df2 = delete_cols(df2, [1, 1, 1, 1, 1, 1, 1, 1])

reordered_df1 = reorder_cols(clean_df1, [0, 1, 5, 6, 7, 2, 3, 4]) #need to add in error handling if # cols <> length of array
#print(reordered_df1)
reordered_df2 = reorder_cols(clean_df2, [0, 4, 5, 6, 7, 1, 2, 3])

merged_dfs = merge_df(reordered_df1, reordered_df2, 2)
print(merged_dfs)

var_col(merged_dfs, 2)

print(merged_dfs)
merged_dfs.to_excel(r'C:\Users\Rislynn\Documents\Python Pandas Excels\ExcelCompare.xlsx')
##################


from RootInteractive.InteractiveDrawing.bokeh.bokehDrawSA import *
from ROOT import TFile, TStatToolkit, gSystem, AliTreePlayer
from bokeh.io import curdoc

output_file("test_bokehDrawSA.html")
# import logging

gSystem.Load("$ALICE_ROOT/lib/libSTAT.so")

TFile.SetCacheFileDir("../../data/")
tree = AliTreePlayer.LoadTrees("echo http://rootinteractive.web.cern.ch/RootInteractive/data/tutorial/bokehDraw/treeABCD.root", ".*", ".*ABCD.*", ".*", "", "")
TStatToolkit.AddMetadata(tree, "A.AxisTitle","A (cm)")
TStatToolkit.AddMetadata(tree, "B.AxisTitle","B (cm/s)")
TStatToolkit.AddMetadata(tree, "C.AxisTitle","B (s)")
TStatToolkit.AddMetadata(tree, "D.AxisTitle","D (a.u.)")

df = pd.DataFrame(np.random.random_sample(size=(2000, 6)), columns=list('ABCDEF'))
initMetadata(df)
df.eval("Bool=A>0.5", inplace=True)
df["AA"]=(df.A*10).round(0)/10.
df["CC"]=(df.C*5).round(0)
df["DD"]=(df.D*2).round(0)
df.head(10)
df.meta.metaData = {'A.AxisTitle': "A (cm)", 'B.AxisTitle': "B (cm/s)", 'C.AxisTitle': "C (s)", 'D.AxisTitle': "D (a.u.)", 'Bool.AxisTitle': "A>half", 'E.AxisTitle': "Category"}

figureArray = [
#   ['A'], ['C-A'], {"color": "red", "size": 7, "colorZvar":"C", "filter": "A<0.5"}],
    [['A'], ['C-A'], {"color": "red", "size": 7, "colorZvar":"C"}],
    [['A'], ['C+A', 'C-A']],
    [['B'], ['C+B', 'C-B'],{"color": "red", "size": 7, "colorZvar":"C"}],
    [['D'], ['(A+B+C)*D'], {"size": 10}],
]
widgets="slider.A(0,1,0.05,0,1), slider.B(0,1,0.05,0,1), slider.C(0,1,0.01,0.1,1), slider.D(0,1,0.01,0,1), checkbox.Bool(1), multiselect.E(0,1,2,3,4)"
#widgets="slider.A(0,1,0.05,0,1), slider.B(0,1,0.05,0,1), slider.C(0,1,0.01,0.1,1), slider.D(0,1,0.01,0,1), checkbox.Bool(1), dropdown.E(0,1,2,3,4)"
figureLayout: str = '((0,1,2, plot_height=300),(3, x_visible=1),commonX=1,plot_height=300,plot_width=1200)'
tooltips = [("VarA", "(@A)"), ("VarB", "(@B)"), ("VarC", "(@C)"), ("VarD", "(@D)")]

widgetParams=[
    ['range', ['A']],
    ['range', ['B',0,1,0.1,0,1]],
    ['range', ['C'], {'type':'minmax'}],
    ['range', ['D'], {'type':'sigma', 'bins':10, 'sigma':3}],
    ['range', ['E'], {'type':'sigmaMed', 'bins':10, 'sigma':3}],
    ['slider', ['AA'],{'bins':10}],
    ['multiSelect',["DD"]],
    ['select',["CC",0,1,2,3]],
    #['slider','F', ['@min()','@max()','@med','@min()','@median()+3*#tlm()']], # to be implmneted
]
widgetLayoutDesc=[ [0,1,2], [3,4,5], [6,7], {'sizing_mode':'scale_width'} ]

figureLayoutDesc=[
    [0,1,2, {'commonX':1,'y_visible':2,'plot_height':300}],
    [3,{'plot_height':100}],
    {'plot_height':100,'sizing_mode':'scale_width'}
]

def testOldInterface():
    output_file("test_bokehDrawSAOldInterface.html")
    fig=bokehDrawSA(df, "A>0", "A","A:B:C:D","C",widgets,0,tooltips=tooltips, layout=figureLayout)

def testBokehDrawArraySA():
    output_file("test_bokehDrawSAArray.html")
    fig=bokehDrawSA.fromArray(df, "A>0", figureArray,widgets,tooltips=tooltips, layout=figureLayout)

def testBokehDrawArraySA_tree():
    output_file("test_bokehDrawSAArray_fromTTree.html")
    fig=bokehDrawSA.fromArray(tree, "A>0", figureArray, widgets, tooltips=tooltips, layout=figureLayout)

def testBokehDrawArrayWidget():
    output_file("test_BokehDrawArrayWidget.html")
    xxx=bokehDrawSA.fromArray(df, "A>0", figureArray, widgetParams, layout=figureLayoutDesc, tooltips=tooltips,widgetLayout=widgetLayoutDesc,sizing_mode="scale_width")

def testBokehDrawArrayWidgetNoScale():
    output_file("test_BokehDrawArrayWidgetNoScale.html")
    xxx=bokehDrawSA.fromArray(df, "A>0", figureArray, widgetParams, layout=figureLayoutDesc, tooltips=tooltips,widgetLayout=widgetLayoutDesc,sizing_mode=None)

#testOldInterface()
#testBokehDrawArraySA()
#testOldInterface_tree()
#testBokehDrawArraySA_tree()
#testBokehDrawArrayWidget()
#testBokehDrawArrayWidgetNoScale()


# -*- coding: utf-8 -*-
from __future__ import absolute_import, division
from __future__ import print_function, unicode_literals
from sandhisplitter.util import invVT

class Joiner:
    # ha, this needs to be substituted, can't capture these
    # all based on rules.
    # Another time, even space separated joins are gramatically
    # valid.

    def join(self, first, second):
        if(first[len(first) - 1] == '്'):
            if(second[0] in invVT.keys()):
                first = first[0:len(first) - 1]
                second = invVT[second[0]] + second[1:]

        elif(first[len(first) - 1] == 'ം'):
            if second[0] in invVT.keys():
                first = first[0:len(first) - 1] + 'മ'
                second = invVT[second[0]] + second[1:]

        elif(second[0] in invVT.keys()):
            first = first + 'യ'
            second = invVT[second[0]] + second[1:]

        # elif(second[0] in doubling):
            # if(first[len(first)-1] )
            # second = second[0]+'്'+second

        return first + second

    def join_words(self, words):
        concat = None
        for word in words:
            if concat is None:
                concat = word
            else:
                concat = self.join(concat, word)
        return concat


import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

def input_data():
    N = 100 # number of points per class
    D = 2 # dimensionality
    K = 3 # number of classes
    X = np.zeros((N*K,D)) # data matrix (each row = single example)
    y = np.zeros((N*K,K), dtype='uint8') # class labels
    for j in range(K):
        ix = range(N*j,N*(j+1))
        r = np.linspace(0.0,1,N) # radius
        t = np.linspace(j*4,(j+1)*4,N) + np.random.randn(N)*0.5 # theta
        X[ix] = np.c_[r*np.sin(t), r*np.cos(t)]
        y[ix,j] = 1 # one-hot
    return X, y

def init_weights(shape):
    return tf.Variable(tf.random_normal(shape, stddev=0.01))

def model(X, W, b, W2, b2):
    hidden_layer = tf.nn.relu(tf.matmul(X, W) + b)
    scores = tf.matmul(hidden_layer, W2) + b2
    return scores

x_data, y_data = input_data()

D, h, K = 2, 100, 3

# placeholders for a tensor that will be always fed.
X = tf.placeholder(tf.float32, shape=[None, D])
Y = tf.placeholder(tf.float32, shape=[None, K])

W  = init_weights([D,h])
b  = init_weights([1,h])
W2 = init_weights([h,K])
b2 = init_weights([1,K])

py_x = model(X, W, b, W2, b2)

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
                        logits=py_x, labels=Y))
train_op = tf.train.AdamOptimizer().minimize(cost)
predict_op = tf.argmax(py_x, axis=1)

with tf.Session() as sess:
    tf.global_variables_initializer().run()
    for i in range(20000):
        sess.run(train_op, feed_dict={X: x_data, Y: y_data})
        if i % 1000 == 0:
            cost_val, pred_val = sess.run([cost, predict_op],
                 feed_dict={X: x_data, Y: y_data})
            accuracy = np.mean(np.argmax(y_data, axis=1) == pred_val)
            print("iteration %5d : loss %.5f : accuracy %.5f" % (i, cost_val, accuracy))

    # plot the resulting classifier
    h = 0.02
    x_min, x_max = x_data[:, 0].min() - 1, x_data[:, 0].max() + 1
    y_min, y_max = x_data[:, 1].min() - 1, x_data[:, 1].max() + 1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                      np.arange(y_min, y_max, h))
    xgrid_data = np.c_[xx.ravel(), yy.ravel()]
    Z = sess.run(predict_op, feed_dict={X: xgrid_data})
    Z = Z.reshape(xx.shape)
    fig = plt.figure()
    plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral, alpha=0.8)
    plt.scatter(x_data[:, 0], x_data[:, 1])
    # , c=np.int(np.argmax(y_data)), s=40, cmap=plt.cm.Spectral)
    plt.xlim(x_min, x_max)
    plt.ylim(y_min, y_max)
    plt.show()


# -*- coding: utf-8 -*-
import warnings
warnings.simplefilter(action='ignore', category=(FutureWarning,UserWarning))

import io, os, sys
import re
import subprocess
import traceback
import shutil
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s, p%(process)s %(levelname)-8s [%(pathname)s:%(module)s:%(funcName)s:%(lineno)d] %(message)s')
import textwrap, argparse
from argparse import RawDescriptionHelpFormatter

import cv2
from PIL import Image
utils_parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '../..'))
sys.path.append(os.path.join(utils_parent_dir, 'modules/common'))
import utils

def parse_arguments():
  parser = argparse.ArgumentParser(
    formatter_class = argparse.RawDescriptionHelpFormatter,
    description=textwrap.dedent('''
      Given an input directory, retrieve all the images recursively with specified
      extensions, verify the real format of each image and make corrections if
      necessary.
      ''')
  )

  # Required arguments
  parser.add_argument(
    'in_dir',
    help='Directory to retrieve images to be copied.'
  )

  # Optional arguments
  parser.add_argument(
    '--pattern', required=False,
    metavar='',
    default='',
    help='Naming pattern of the desired files (default: "").'
  )

  # Optional arguments
  parser.add_argument(
    '--extensions', required=False,
    metavar='',
    default='jpg',
    help='List of extensions of the desired files, seperated by comma (default: "jpg").'
  )

  return parser.parse_args()

def process_images(in_dir, path):
  try:
    image_path = os.path.join(in_dir, path)

    # Load in the given image
    image = Image.open(image_path)
    if image.format != 'JPEG' or image.mode != 'RGB':
      if image.format != 'JPEG':
        logging.warning('Corrected image with wrong format: {}, {}'.format(image_path, image.format))
      else:
        logging.warning('Corrected image with wrong mode: {}, {}'.format(image_path, image.mode))

      image = image.convert('RGB')
      image.save(image_path, 'JPEG')
      assert Image.open(image_path).format == 'JPEG'

    # Get the dirname
    dirname = os.path.dirname(image_path)

    # Get the basename
    basename = os.path.basename(image_path)

    # Get the filename by removing extension
    name, ext = os.path.splitext(basename)

    # Ensure that extension ends with ".jpg"
    if ext != '.jpg':
      new_image_path = os.path.join(dirname, name+'.jpg')

      # Copy the image
      shutil.copyfile(image_path, new_image_path)

      # Delete the old one
      os.remove(image_path)

      logging.warning('Corrected image with wrong extension: {} -> {}'.format(image_path, new_image_path))

  except Exception as e:
    logging.error(traceback.format_exc())

def process_images_thread(in_dir, image_paths, beg, end):
  for i in range(beg, end):
    process_images(in_dir, image_paths[i])

def do_it(in_dir, pattern, extensions):
  # Check if input directory exists
  if not os.path.exists(in_dir):
    logging.critical('Failed to find {}'.format(in_dir))
    sys.exit()

  # Get all images in the input directory
  _, image_paths, _ = utils.list_files(in_dir, pattern, extensions)
  image_paths = sorted(image_paths)

  n_samples = len(image_paths)
  logging.info('Total files: {}'.format(n_samples))

  threads = utils.parallel(n_samples, process_images_thread, in_dir, image_paths)

  for i in range(len(threads)):
    threads[i].join()

if __name__ == '__main__':

  args = parse_arguments()

  # Remove the whitespaces in list of extensions
  extensions = utils.trim_whitespaces(args.extensions, side_only=False)

  # Get the list of extensions and discard the null character
  extensions = list(filter(None, extensions.split(',')))

  # Remove the redundant elements
  extensions = list(set(extensions))

  # Tidy up directories
  args.in_dir = args.in_dir.rstrip('/\\')

  do_it(args.in_dir, args.pattern, extensions)


# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""
import numpy as np
class Node:
    def __init__(self, val,data,alpha,beta):

        self.left = None
        self.right = None
        self.data = data
        self.val = val
        self.alpha =alpha
        self.beta = beta

    def insert(self, data,val):
# Compare the new value with the parent node
        if self.data:
            if data < self.data:
                if self.left is None:
                    self.left = Node(data,val)
                else:
                    self.left.insert(data,val)
            elif data > self.data:
                if self.right is None:
                    self.right = Node(data,val)
                else:
                    self.right.insert(data,val)
        else:
            self.data = data
            self.val=val

# Print the tree
    def PrintTree(self):
        if self.left:
            self.left.PrintTree()
        print(self.val," ",self.data,"alpha: ",self.alpha,"beta: ",self.beta),
        if self.right:
            self.right.PrintTree()

# Inorder traversal
# Left -> Root -> Right
    def inorderTraversal(self, root):
        res = []
        if root:
            res = self.inorderTraversal(root.left)
            res.append(root.val)
            res.append(root.data)

            res = res + self.inorderTraversal(root.right)
        return res

# Preorder traversal
# Root -> Left ->Right
    def PreorderTraversal(self, root):
        res = []
        if root:
            res.append(root.val)
            res.append(root.data)
            res = res + self.PreorderTraversal(root.left)
            res = res + self.PreorderTraversal(root.right)
        return res

# Postorder traversal
# Left ->Right -> Root
    def PostorderTraversal(self, root):
        res = []
        if root:
            res = self.PostorderTraversal(root.left)
            res = res + self.PostorderTraversal(root.right)
            res.append(root.val)
            res.append(root.data)
        return res

# findval method to compare the value with nodes
    def findval(self, lkpval):
        if lkpval < self.data:
            if self.left is None:
                return str(lkpval)+" is not Found"
            return self.left.findval(lkpval)
        elif lkpval > self.data:
            if self.right is None:
                return str(lkpval)+" is not Found"
            return self.right.findval(lkpval)
        else:
            return str(self.data) + " is found"

    def minDepth(self,root):

        if root is None:
            return 0

    # Base Case : Leaf node.This acoounts for height = 1
        if root.left is None and root.right is None:
            return 1

    # If left subtree is Null, recur for right subtree
        if root.left is None:
            return root.minDepth(root.right)+1

        # If right subtree is Null , recur for left subtree
        if root.right is None:
            return root.minDepth(root.left) +1

        return min(root.minDepth(root.left), root.minDepth(root.right))+1

    TargetDepth=0

    def Max(self,node,currDepth):
        if(node.left is None and node.right is None):
            return node.data
        if (currDepth == TargetDepth):
            return node.data
        node.data=np.NINF

        node.data=max(node.data,node.Min(node.left,currDepth+1),node.Min(node.right,currDepth+1))
        return node.data

    def Min(self,node,currDepth):
        if(node.left is None and node.right is None):
            return node.data
        if (currDepth == TargetDepth):
            return node.data
        node.data=np.Inf
        node.data=min(node.data,node.Max(node.left,currDepth+1),node.Max(node.right,currDepth+1))
        return node.data

    def MaxAlphaBeta(self,node,currDepth):
        if(node.left is None and node.right is None):
            return node.data
        if (currDepth == TargetDepth):
            return node.data
        node.data=np.NINF

        node.data=max(node.data,node.MinAlphaBeta(node.left,currDepth+1),node.MinAlphaBeta(node.right,currDepth+1))
        if node.data >= node.beta:
            return node.data
        if node.data>=node.alpha:
            node.alpha=node.data
        return node.data

    def MinAlphaBeta(self,node,currDepth):
        if(node.left is None and node.right is None):
            return node.data
        if (currDepth == TargetDepth):
            return node.data
        node.data=np.Inf
        node.data=min(node.data,node.MaxAlphaBeta(node.left,currDepth+1),node.MaxAlphaBeta(node.right,currDepth+1))
        if node.data <= node.alpha:
            return node.data
        if node.data<=node.beta:
            node.beta=node.data
        return node.data

root = Node('A', np.NINF,np.NINF,np.Inf);
root.left = Node('B', np.Inf,np.NINF,np.Inf);
root.right = Node('C', np.Inf,np.NINF,np.Inf);
root.left.left = Node('D', np.NINF,np.NINF,np.Inf);
root.left.right = Node('E', np.NINF,np.NINF,np.Inf);
root.right.left = Node('F', np.NINF,np.NINF,np.Inf);
root.right.right = Node('G', np.NINF,np.NINF,np.Inf);
root.left.left.left = Node('H',-1,np.NINF,np.Inf);
root.left.left.right = Node('I',4,np.NINF,np.Inf);
root.left.right.left = Node('J',2,np.NINF,np.Inf);
root.left.right.right = Node('K',6,np.NINF,np.Inf);
root.right.left.left = Node('L',-3,np.NINF,np.Inf);
root.right.left.right = Node('M',-5,np.NINF,np.Inf);
root.right.right.left = Node('N',0,np.NINF,np.Inf);
root.right.right.right = Node('O',7,np.NINF,np.Inf);
TargetDepth = root.minDepth(root)
print("Depth:",TargetDepth)
#value = root.Max(root, 0)
#print(value)
value = root.MaxAlphaBeta(root, 0)
#print("Value: ",value)
root.PrintTree()


__author__ = 'vsaw'

class DataSet:
    """
    A collection of sensor data stored in a format ready to be consumed by matplotlib and scipy.
    """

    # The data array
    __data = 20 * [None]
    # The timestamp array
    __timestamps = 20 * [None]

    def __init__(self):
        self.__data = 20 * [None]
        self.__timestamps = 20 * [None]

    def add(self, sensor_event):
        """
        Adds the sensor event to the data set

        :param sensor_event: The sensor event to add
        :return: None

        The data set will be of the following structure

        plot_data = [
            [
                [ Sensor 0 Dimension 0 Reading 0, Sensor 0 Dimension 0 Reading 1, ... ],
                [ Sensor 0 Dimension 1 Reading 0, Sensor 0 Dimension 1 Reading 1, ... ],
                ...
                [ Sensor 0 Dimension N Reading 0, Sensor 0 Dimension N Reading 1, ... ]
            ],
            [
                [ Sensor 1 Dimension 0 Reading 0, Sensor 1 Dimension 0 Reading 1, ... ],
                [ Sensor 1 Dimension 1 Reading 0, Sensor 1 Dimension 1 Reading 1, ... ],
                ...
                [ Sensor 1 Dimension N Reading 0, Sensor 1 Dimension N Reading 1, ... ]
            ],
            ...
            [
                [ Sensor N Dimension 0 Reading 0, Sensor N Dimension 0 Reading 1, ... ],
                [ Sensor N Dimension 1 Reading 0, Sensor N Dimension 1 Reading 1, ... ],
                ...
                [ Sensor N Dimension N Reading 0, Sensor N Dimension N Reading 1, ... ]
            ]
        ]

        plot_timestamps = [
            [ Sensor 0 Timestamp 0, Sensor 0 Timestamp 1, ... ],
            ...
            [ Sensor N Timestamp 0, Sensor N Timestamp 1, ...]
        ]

        So to get a specific reading do: plot_data[sensor_type][sensor_dimension][reading_number]

        The structure has been chosen to be easily passed into matplotlib as this expects
        a sequence of numbers and can not handle multidimensional data directly.
        """
        sensor_type = sensor_event['type']
        data = sensor_event['data']
        timestamp = sensor_event['timestamp']
        if self.__data[sensor_type] is None:
            self.__data[sensor_type] = []
            for i in range(len(data)):
                self.__data[sensor_type].append([data[i]])
            self.__timestamps[sensor_type] = [timestamp]
        else:
            for i in range(len(data)):
                self.__data[sensor_type][i].append(data[i])
            self.__timestamps[sensor_type].append(timestamp)

    def get(self, sensor_type, dimension=None, event_number=None):
        """
        Get the data stored

        :param sensor_type: The sensor type to get the data for
        :param dimension: The dimension to retrieve if the sensor has multiple dimensions. If None all dimensions will
            be returned
        :param event_number: The event number to get. If None, all events for given sensor and dimension will be
            returned.
        :return: A tuple consisting of (timestamp, data)
        """
        if self.__data[sensor_type] is None:
            return None

        timestamp = self.__timestamps[sensor_type]
        data = self.__data[sensor_type]
        if dimension is not None:
            data = data[dimension]

        return timestamp, data

    def get_contained_sensor_types(self):
        """
        Return a list of sensors in the data set

        :return: A list of sensors in the data set or empty if the set is empty
        """
        ret = []
        for i in range(len(self.__data)):
            if self.__data[i] is not None:
                ret.append(i)

        return ret


from __future__ import print_function
from collections import defaultdict
from concrete.util.file_io import CommunicationReader

invIdx = defaultdict(list)
commIds = []

for comm, fn in CommunicationReader('../wikiirqa/comms20.tar.gz'):
    commIds.append(comm.id)
    map = defaultdict(int)

    for section in comm.sectionList:
        for sentence in section.sentenceList:
            print(sentence)
            for t in sentence.tokenization.tokenList.tokenList:
                text = comm.text[t.textSpan.start:t.textSpan.ending]
                map[text] += 1

    for term, tf in map.iteritems():
        invIdx[term].append((comm.id, tf))


import requests
import json

class PlanetTerp:
    url = 'https://api.planetterp.com/v1/'

    # Query Parameters
    # Name	    Type	Description
    # name	    string	Required. Show the given course.
    # reviews	boolean	Optional. Show reviews for the course (reviews for professors that taught the course and have this course listed as the one being reviewed). Default: false
    def course(self, name, reviews = False):
        request_string = self.url + 'course?name=' + name.replace(" ", "%20")
        if reviews:
            request_string += '&reviews=true'

        return requests.get(request_string).json()

    # Query Parameters
    # Name	        Type	Description
    # department	string	Optional. Only get courses in a department. Must be four characters. Default: all departments
    # reviews	    boolean	Optional. Show reviews for the course (reviews for professors that taught the course and have this course listed as the one being reviewed). Default: false
    # limit	        integer	Optional. Maximum number of records to return. Must be between 1 and 1000. Default: 100
    # offset	    integer	Optional. Number of records to skip for pagination. Default: 0
    def all_courses(self, department = None, reviews = False, limit = None, offset = None):
        request_string = self.url + 'courses?'
        if department:
            request_string += 'department=' + department + '&'
        if reviews:
            request_string += 'reviews=true' + '&'
        if limit:
            request_string += 'limit=' + str(limit) + '&'
        if offset:
            request_string += 'offset=' + str(offset) + '&'

        # We ignore the last character as it will be an extra '&'
        request_url = request_string[:-1]
        return requests.get(request_url).json()

    # Query Parameters
    # Name    Type    Description
    # name    string  Required. Show the given professor.
    # reviews boolean Optional.Show reviews for the professor.Default: false
    def professor(self, name, reviews = False):
        request_string = self.url + 'professor?name=' + name.replace(" ", "%20")
        if reviews:
            request_string += '&reviews=true'

        return requests.get(request_string).json()

    # Query Parameters
    # Name	    Type	Description
    # type	    string	Optional. Show only reviews for professors or teaching assistants. Default: show both. Options: professor, ta
    # reviews	boolean	Optional. Show reviews for the professors. Default: false
    # limit	    integer	Optional. Maximum number of records to return. Must be between 1 and 1000. Default: 100
    # offset	integer	Optional. Number of records to skip for pagination. Default: 0
    def all_professors(self, type_ = None, reviews = False, limit = None, offset = None):
        request_string = self.url + 'professors?'
        if type_:
            request_string += 'type=' + type_ + '&'
        if reviews:
            request_string += 'reviews=true' + '&'
        if limit:
            request_string += 'limit=' + str(limit) + '&'
        if offset:
            request_string += 'offset=' + str(offset) + '&'

        # We ignore the last character as it will be an extra '&'
        request_url = request_string[:-1]
        return requests.get(request_url).json()

    # Query Parameters
    # Name	    Type	Description
    # course	string	Optional. Show only grades for the given course.
    # professor	string	Optional. Show only grades for the given professor.
    # semester	string	Optional. Show only grades for the given semester. Semester should be provided as the year followed by the semester code. 01 means Spring and 08 means Fall. For example, 202001 means Spring 2020. Default: all semesters
    # section	string	Optional. Show only grades for the given section. Default: all sections
    def grades(self, course = None, professor = None, semester = None, section = None):
        request_string = self.url + 'grades?'
        if not course and not professor:
            print("Error! Must provide either a course or professor as input")
            return None
        if course:
            request_string += 'course=' + course + '&'
        if professor:
            request_string += 'professor=' + professor.replace(" ", "%20") + '&'
        if semester:
            request_string += 'semester=' + semester + '&'
        if section:
            request_string += 'section=' + section + '&'

        # We ignore the last character as it will be an extra '&'
        request_url = request_string[:-1]
        return requests.get(request_url).json()


# -*- coding: utf-8 -*-
"""
Created on Sat Mar 05 23:39:47 2016

@author: xzk
"""
import collections
class Solution(object):
    def ladderLength(self, beginWord, endWord, wordList):
        """
        :type beginWord: str
        :type endWord: str
        :type wordList: Set[str]
        :rtype: int
        #BFS
        """
        queue = collections.deque([(beginWord, 1)])
        visited = set([])
        while queue:
            word, dist = queue.popleft()
            if word == endWord:
                return dist
            for i in xrange(len(word)):
                for j in 'abcdefghijklmnopqrstuvwxyz':
                    new_word = word[0:i] + j + word[i+1:]
                    if new_word not in visited and new_word in wordList:
                        queue.append((new_word, dist+1))
                        visited.add(new_word)
        return 0
    def ladderLength2(self, beginWord, endWord, wordList):
        """
        :type beginWord: str
        :type endWord: str
        :type wordList: Set[str]
        :rtype: int
        """
        front, back = set([beginWord]), set([endWord])
        wordList.discard(beginWord)
        dist = 2
        while front:
            front = wordList & set(word[0:i] + char + word[i+1:] for word in front \
                                    for i in xrange(len(beginWord)) for char in 'abcdefghijklmnopqrstuvwxyz')
            if front & back:
                return dist
            dist += 1
            if len(back) < len(front):
                front, back = back, front #Trick.
            wordList -= front
        return 0


from functools import reduce

try:
    import configparser
except ImportError:
    import ConfigParser as configparser

import os
import re

from vit import util

BASE_DIR = os.path.dirname(os.path.realpath(__file__))
BRACKETS_REGEX = re.compile("[<>]")
DEFAULT_KEYBINDINGS_SECTIONS = ('global', 'navigation', 'command', 'report')
CONFIG_SPECIAL_KEY_SUBSTITUTIONS = {
    'colon': ':',
    'equals': '=',
    'space': ' ',
}

class KeybindingError(Exception):
    pass

class KeybindingParser(object):
    def __init__(self, loader, config, action_registry):
        self.loader = loader
        self.config = config
        self.action_registry = action_registry
        self.actions = self.action_registry.get_actions()
        self.noop_action_name = self.action_registry.make_action_name(self.action_registry.noop_action_name)
        self.default_keybinding_name = self.config.get('vit', 'default_keybindings')
        self.default_keybindings = configparser.SafeConfigParser()
        self.default_keybindings.optionxform=str
        self.sections = DEFAULT_KEYBINDINGS_SECTIONS
        self.keybindings = {}
        self.multi_key_cache = {}

    def is_keybinding(self, keys):
        return keys in self.keybindings

    def items(self, section):
        try:
            return self.default_keybindings.items(section)
        except configparser.NoSectionError:
            return []

    def load_default_keybindings(self):
        name = self.default_keybinding_name
        template = '%s/keybinding/%s.ini'
        user_keybinding_file = template % (self.loader.user_config_dir, name)
        keybinding_file = template % (BASE_DIR, name)
        if util.file_readable(user_keybinding_file):
            self.default_keybindings.read(user_keybinding_file)
        elif util.file_readable(keybinding_file):
            self.default_keybindings.read(keybinding_file)
        else:
            raise KeybindingError("default_keybindings setting '%s' invalid, file not found" % name)
        for section in self.sections:
            bindings = self.items(section)
            self.add_keybindings(bindings)

    def keybinding_special_keys_substitutions(self, value):
        if value in CONFIG_SPECIAL_KEY_SUBSTITUTIONS:
            value = CONFIG_SPECIAL_KEY_SUBSTITUTIONS[value]
        return value

    def parse_keybinding_keys(self, keys):
        has_modifier = bool(re.match(BRACKETS_REGEX, keys))
        parsed_keys = ' '.join(re.sub(BRACKETS_REGEX, ' ', keys).strip().split()).lower() if has_modifier else keys
        return self.keybinding_special_keys_substitutions(parsed_keys), has_modifier

    def parse_keybinding_value(self, value, replacements={}):
        def reducer(accum, char):
            if char == '<':
                accum['in_brackets'] = True
                accum['bracket_string'] = ''
            elif char == '>':
                accum['in_brackets'] = False
                accum['keybinding'].append(self.keybinding_special_keys_substitutions(accum['bracket_string'].lower()))
            elif char == '{':
                accum['in_variable'] = True
                accum['variable_string'] = ''
            elif char == '}':
                accum['in_variable'] = False
                if accum['variable_string'] in self.actions:
                    accum['action_name'] = accum['variable_string']
                else:
                    for replacement in replacements:
                        args = replacement['match_callback'](accum['variable_string'])
                        if isinstance(args, list):
                            accum['keybinding'].append((replacement['replacement_callback'], args))
                            return accum
                    raise ValueError("unknown config variable '%s'" % accum['variable_string'])
            else:
                if accum['in_brackets']:
                    accum['bracket_string'] += char
                elif accum['in_variable']:
                    accum['variable_string'] += char
                else:
                    accum['keybinding'].append(char)
            return accum
        accum = reduce(reducer, value, {
            'keybinding': [],
            'action_name': None,
            'in_brackets': False,
            'bracket_string': '',
            'in_variable': False,
            'variable_string': '',
        })
        return accum['keybinding'], accum['action_name']

    def validate_parsed_value(self, key_groups, bound_keys, action_name):
        if bound_keys and action_name:
            raise KeybindingError("keybindings '%s' unsupported configuration: ACTION_ variables must be used alone." % key_groups)

    def is_noop_action(self, keybinding):
        return True if 'action_name' in keybinding and keybinding['action_name'] == self.noop_action_name else False

    def filter_noop_actions(self, keybindings):
        return {keys:value for (keys, value) in keybindings.items() if not self.is_noop_action(keybindings[keys])}

    def get_keybindings(self):
        return self.keybindings

    def add_keybindings(self, bindings=[], replacements={}):
        for key_groups, value in bindings:
            bound_keys, action_name = self.parse_keybinding_value(value, replacements)
            self.validate_parsed_value(key_groups, bound_keys, action_name)
            for keys in key_groups.strip().split(','):
                parsed_keys, has_modifier = self.parse_keybinding_keys(keys)
                self.keybindings[parsed_keys] = {
                    'label': keys,
                    'has_modifier': has_modifier,
                }
                if action_name:
                    self.keybindings[parsed_keys]['action_name'] = action_name
                else:
                    self.keybindings[parsed_keys]['keys'] = bound_keys
        self.keybindings = self.filter_noop_actions(self.keybindings)
        return self.get_keybindings()


import numpy as np
from dask.multiprocessing import get

from torchy.utils import timeit

import torch
from torch.nn.functional import conv2d
from torch.autograd import Variable

def conv(input, kernel):
    print("[>] [conv]")
    if isinstance(input, np.ndarray):
        input = Variable(torch.from_numpy(input))
    if isinstance(kernel, np.ndarray):
        kernel = Variable(torch.from_numpy(kernel))
    out = conv2d(input=input, weight=kernel).data
    print("[<] [conv]")
    return out

def make_zeros(fid, shape):
    print("[>] [make_zeros::fid_{}]".format(fid))
    print("[<] [make_zeros::fid_{}]".format(fid))
    return np.zeros(shape=shape)

if __name__ == '__main__':
    dsk = {'input': (np.zeros, (1, 1, 100, 100)),
           'weights': (np.zeros, (10, 1, 5, 5)),
           'conv1': (conv, 'input', 'weights')}

    with timeit() as time_info:
        output = get(dsk, 'conv1')

    print("Time Elapsed: {}".format(time_info.elapsed_time))