from __future__ import annotations
from typing import TYPE_CHECKING
from data_structures.observer import Observer
from entity_base.listeners.tick_listener import TickLambda
from root_container.field_container.node.arc_curve_node import ArcCurveNode
from root_container.field_container.node.bezier_lines import BezierLines
from root_container.field_container.node.bezier_theta_node import BezierThetaNode
from root_container.field_container.path_element import PathElement

from root_container.field_container.segment.segment_type import PathSegmentType
if TYPE_CHECKING:
    from root_container.path import Path

from entity_base.listeners.drag_listener import DragLambda
from root_container.field_container.segment.segment_direction import SegmentDirection
from utility.math_functions import isInsideBox
if TYPE_CHECKING:
    from root_container.field_container.node.path_node_entity import PathNodeEntity

from abc import ABC, abstractmethod
from enum import Enum
from common.reference_frame import PointRef, Ref, VectorRef

from entity_base.listeners.click_listener import ClickLambda
from entity_base.listeners.select_listener import SelectLambda, SelectorType
from entity_base.entity import Entity

from root_container.field_container.segment.path_segment_state import PathSegmentState
from root_container.field_container.segment.PathSegmentStates.bezier_segment_state import BezierSegmentState
from root_container.field_container.segment.PathSegmentStates.arc_segment_state import ArcSegmentState
from root_container.field_container.segment.PathSegmentStates.straight_segment_state import StraightSegmentState

from adapter.path_adapter import PathAdapter, AdapterInterface

from data_structures.linked_list import LinkedListNode

from common.draw_order import DrawOrder
from utility.pygame_functions import shade
import pygame

"""
Acts as a "context" class in the state design pattern. Owns PathSegmentState objects
that define behavior for straight/arc/bezier shapes. Easy to switch between states
We also define the constants that apply across all segment types here, like color and thickness
"""

class PathSegmentEntity(Entity, AdapterInterface, Observer, PathElement['PathNodeEntity']):
    def __init__(self, parent: Entity, path: Path) -> None:

        super().__init__(parent = parent,
                         select = SelectLambda(self, "segment", type = SelectorType.SOLO),
                         drag = DragLambda(self,
                                           FonStartDrag = self.onStartDrag,
                                           FcanDrag = self.canDrag,
                                           FonDrag = self.onDrag,
                                           FonStopDrag = self.onStopDrag
                                           ),
                         tick = TickLambda(self, self.tick),
                         drawOrder = DrawOrder.SEGMENT)

        LinkedListNode.__init__(self)

        self.path = path

        self.states: dict[PathSegmentType, PathSegmentState] = {
            PathSegmentType.STRAIGHT: StraightSegmentState(self),
            PathSegmentType.ARC: ArcSegmentState(self),
            PathSegmentType.BEZIER: BezierSegmentState(self)
        }
        # on state update, recompute itself
        for stateID in self.states:
            self.states[stateID].subscribe(self, onNotify = self.recomputeEntity)

        self.currentState: PathSegmentType = PathSegmentType.STRAIGHT

        self.direction: SegmentDirection = SegmentDirection.FORWARD

        self.colorForward = [122, 210, 118]
        self.colorForwardH = shade(self.colorForward, 0.92)
        self.colorForwardA = shade(self.colorForward, 0.7)
        self.colorReversed = [191, 118, 118]
        self.colorReversedH = shade(self.colorReversed, 0.92)
        self.colorReversedA = shade(self.colorReversed, 0.7)

        # Since the construction of this segment is not complete, with
        # having both nodes not guaranteed, initialization of ArcCurveEntity
        # is delayed until both nodes are set at updateAdapter()
        self.arcNode: ArcCurveNode = None

        self.isFullyInitialized = False

        self.updateAdapter()

    def tick(self):
        return
        print(self.getPrevious(), self.getNext())

    def getState(self) -> PathSegmentState:
        return self.states[self.currentState]

    def getStateType(self) -> PathSegmentType:
        return self.currentState

    def setState(self, newState: PathSegmentType) -> None:
        oldState = self.currentState
        self.currentState = newState
        self.path.onSegmentTypeChange(self, oldState, newState)

        self.getState().onStateChange()
        self.updateAdapter()

        self.getPrevious().onAngleChange()
        self.getNext().onAngleChange()

    def onStartDrag(self, mouse: tuple):
        self.mouseStartDrag = PointRef(Ref.SCREEN, mouse)
        self.nodeStartPosition = []
        for node in [self.getPrevious(), self.getNext()]:
            self.nodeStartPosition.append(node.position)
            node.constraints.showPosition()

    def canDrag(self, mouseTuple: tuple) -> bool:
        mouse = PointRef(Ref.SCREEN, mouseTuple)

        self.mouseDelta = mouse - self.mouseStartDrag
        self.nodeGoalPosition = []
        for i, node in enumerate([self.getPrevious(), self.getNext()]):
            newPos = self.nodeStartPosition[i] + self.mouseDelta
            self.nodeGoalPosition.append(newPos)
            if not isInsideBox(*newPos.fieldRef, 0, 0, 144, 144):
                return False
        return True

    # When dragging, determine if either node is snappable. If so, do it
    def onDrag(self, mouseTuple: PointRef):
        for i, node in enumerate([self.getPrevious(), self.getNext()]):
            node.position = self.nodeGoalPosition[i]
            node.constraints.resetPositionConstraints(node.position)

        delta = VectorRef(Ref.FIELD, (0,0))
        for node in [self.getPrevious(), self.getNext()]:
            node.constrainPosition()
            if node.constraints.snappable():
                delta = node.constraints.getPosition() - node.position
                break # can only snap one node at a time

        for node in [self.getPrevious(), self.getNext()]:
            node.position += delta
            node.onNodeMove()

    def onStopDrag(self):
        for node in [self.getPrevious(), self.getNext()]:
            node.constraints.hidePosition()

    # callback when a node attached to this segment has stopped dragging
    # Pass callback on to segment states if they need to do something
    def onNodeStopDrag(self):
        self.getState().onNodeStopDrag()

    # a segment is temporary if the nodes on either ends are temporary
    def isTemporary(self) -> bool:
        if self.getPrevious() is not None and self.getPrevious().isTemporary():
            return True
        if self.getNext() is not None and self.getNext().isTemporary():
            return True
        return False

    def onNodeMove(self, node: Entity):
        self.updateAdapter()
        self.recomputeEntity()
        self.arcNode.recomputePositionRef()
        if node is self.getPrevious():
            self.getNext().onAngleChange()
        else:
            self.getPrevious().onAngleChange()

    def onReshape(self):
        self.updateAdapter()
        self.recomputeEntity()
        self.getNext().onAngleChange()
        self.getPrevious().onAngleChange()

    def getAdapter(self) -> PathAdapter:
        return self.getState().getAdapter()

    def getAllAdapters(self) -> list[PathAdapter]:
        adapters = []
        for state in self.states:
            adapters.append(self.states[state].getAdapter())
        return adapters

    def updateAdapter(self) -> None:

        # initailize arc node
        if not self.isFullyInitialized and self.getNext() is not None and self.getPrevious() is not None:
            self.arcNode = ArcCurveNode(self, self.states[PathSegmentType.ARC])
            self.bezierTheta1 = BezierThetaNode(self, self.states[PathSegmentType.BEZIER], self.getPrevious, True)
            self.bezierTheta2 = BezierThetaNode(self, self.states[PathSegmentType.BEZIER], self.getNext, False)

            # must call this after initilizing arcNode and bezierThetas,
            # so that position recomputation happens before drawing lines
            BezierLines(self)

            self.isFullyInitialized = True

        self.getState().updateAdapter()
        self.recomputeEntity()

    def toggleDirection(self):
        if self.direction == SegmentDirection.FORWARD:
            self.direction = SegmentDirection.REVERSE
        else:
            self.direction = SegmentDirection.FORWARD
        self.updateAdapter()
        self.getPrevious().onAngleChange()
        self.getNext().onAngleChange()

    def getDirection(self):
        return self.direction

    def getSegmentType(self) -> PathSegmentType:
        return self.currentState

    def getOther(self, node: PathNodeEntity):
        if self.getPrevious() is node:
            return self.getNext()
        elif self.getNext() is node:
            return self.getPrevious()
        else:
            raise ValueError("Node is not connected to this segment")

    def getColor(self, isActive: bool = False, isHovered: bool = False):

        if self.direction == SegmentDirection.REVERSE:
            if isActive:
                return self.colorReversedA
            elif isHovered:
                return self.colorReversedH
            else:
                return self.colorReversed
        else:
            if isActive:
                return self.colorForwardA
            elif isHovered:
                return self.colorForwardH
            else:
                return self.colorForward

    def getStartTheta(self) -> float:
        return self.getState().getStartTheta()

    def getEndTheta(self) -> float:
        return self.getState().getEndTheta()

    def getLinearDistance(self, ref: Ref):
        return (self.getPrevious().position - self.getNext().position).magnitude(ref)

    def getThetaAtNode(self, node: PathNodeEntity):
        if self.getPrevious() is node:
            return self.getStartTheta()
        elif self.getNext() is node:
            return self.getEndTheta()
        else:
            raise Exception("Node is not connected to this segment")

    def isTouching(self, position: PointRef) -> bool:
        return self.getState().isTouching(position)

    def defineCenter(self) -> tuple:
        return self.getState().getCenter()

    def getThickness(self, isForMouseDetection: bool = False) -> int:

        thickness = 6 if isForMouseDetection else 3

        return int(thickness * self.dimensions.RESOLUTION_RATIO)

    def draw(self, screen: pygame.Surface, isActive: bool, isHovered: bool) -> bool:
        return self.getState().draw(screen, isActive, isHovered)

    def isSelfOrNodesSelected(self):
        if self.select.isSelected:
            return True
        elif self.getPrevious().select.isSelected:
            return True
        elif self.getNext().select.isSelected:
            return True
        return False


from typing import List
from collections import deque

class Solution:
    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
        end = k - 1
        dq = deque()

        for i in range(k):
            dq.append(nums[i])
        cur_max = max(dq)
        res = [cur_max]

        while end < len(nums) - 1:
            end += 1

            last = dq.popleft()
            dq.append(nums[end])

            if last == cur_max:
                cur_max = max(dq)
            elif nums[end] > cur_max:
                cur_max = nums[end]

            res.append(cur_max)

        return res

sol = Solution()
print(sol.maxSlidingWindow([1,3,-1,-3,5,3,6,7], 3))

"""
leetcode: 239
시간초과
"""


import pandas as pd
import re
import string
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.tokenize import TweetTokenizer

#Input Dataset

data = pd.read_csv("dataset_pro.csv", sep="\t")

#Remove duplicates from the dataset

data.sort_values("Tweet", inplace = True)
data.drop_duplicates(subset ="Tweet",
                     keep = False, inplace = True)
data.sort_index(inplace = True)

#Remove tweets with less than 2 words

temp = []

for index, row in data.iterrows():
	if len(str(row['Tweet']).split()) <= 2:
	 	continue
	temp.append(row.tolist())

data = pd.DataFrame(temp,columns=['UserName','Date', 'Time', 'Longitude', 'Latitude', 'Tweet'])

#Text Processing

sw = stopwords.words('english')
all_sw = set (sw + list(string.punctuation))

tokenizer = TweetTokenizer()
ps = PorterStemmer()

updated_tweets = []
tweets = list(data["Tweet"])

for tweet in tweets:
	tweet = re.sub(r'[^\x00-\x7F]+',' ', tweet)
	tweet = tweet.lower().strip()
	tweet = re.sub('[\']', ' ', tweet)
	tweet = tokenizer.tokenize(tweet)
	processed_tweet = [t for t in tweet if t not in all_sw]
	processed_tweet = [ps.stem(word) for word in processed_tweet]
	processed_tweet = ' '.join(processed_tweet)
	processed_tweet = re.sub('[.:()]', '', processed_tweet)
	updated_tweets.append(processed_tweet)

new_tweet_df = pd.DataFrame({"Tweet": updated_tweets})

data.update(new_tweet_df)

#Output processed dataset

data.to_csv('processed_dataset.csv', sep='\t')


import numpy as np
import torch
import torch.nn as nn

from hfta.ops import (get_hfta_op_for, testcase_automator, assert_allclose,
                      dump_error_msg)

def testcase_MaxPool2d(
    B=3,
    N=32,
    C=16,
    kernel_size=2,
    HWin=28,
    stride=None,
    padding=0,
    dilation=1,
    return_indices=False,
    ceil_mode=False,
    device=torch.device('cpu'),
    dtype=torch.float,
):
  with torch.no_grad():
    x_array = [
        torch.rand(N, C, HWin, HWin, device=device, dtype=dtype)
        for _ in range(B)
    ]
    x_fused = torch.cat([x.unsqueeze(1) for x in x_array], dim=1)
    args = (kernel_size,)
    kwargs = {
        'stride': stride,
        'padding': padding,
        'dilation': dilation,
        'return_indices': return_indices,
        'ceil_mode': ceil_mode,
    }
    pool_array = [nn.MaxPool2d(*args, **kwargs) for _ in range(B)]
    pool_fused = get_hfta_op_for(nn.MaxPool2d, B=B)(*args, **kwargs)
    res_array = [pool_array[b](x_array[b]) for b in range(B)]
    res_fused_actual = pool_fused(x_fused)
    if return_indices:
      y_array, indices_array = tuple(zip(*res_array))
      y_fused_actual, indices_fused_actual = res_fused_actual
    else:
      y_array = res_array
      y_fused_actual = res_fused_actual
    y_fused_expect = torch.cat([y.unsqueeze(1) for y in y_array], dim=1)
    try:
      assert_allclose(
          y_fused_actual.cpu().numpy(),
          y_fused_expect.cpu().numpy(),
          rtol=1e-4,
      )
    except AssertionError as e:
      dump_error_msg(e)
    if return_indices:
      indices_fused_expect = torch.cat(
          [indices.unsqueeze(1) for indices in indices_array],
          dim=1,
      )
      try:
        assert_allclose(
            indices_fused_actual.cpu().numpy(),
            indices_fused_expect.cpu().numpy(),
            rtol=1e-4,
        )
      except AssertionError as e:
        dump_error_msg(e)

def testcase_AdaptiveAvgPool2d(
    B=3,
    N=32,
    C=16,
    HWin=28,
    output_size=(16, 16),
    device=torch.device('cpu'),
    dtype=torch.float,
):
  with torch.no_grad():
    x_array = [
        torch.rand(N, C, HWin, HWin, device=device, dtype=dtype)
        for _ in range(B)
    ]
    x_fused = torch.cat([x.unsqueeze(1) for x in x_array], dim=1)
    args = (output_size,)
    pool_array = [nn.AdaptiveAvgPool2d(*args) for _ in range(B)]
    pool_fused = get_hfta_op_for(nn.AdaptiveAvgPool2d, B=B)(*args)
    y_array = [pool_array[b](x_array[b]) for b in range(B)]
    y_fused_actual = pool_fused(x_fused)
    y_fused_expect = torch.cat([y.unsqueeze(1) for y in y_array], dim=1)
    try:
      assert_allclose(
          y_fused_actual.cpu().numpy(),
          y_fused_expect.cpu().numpy(),
          rtol=1e-4,
      )
    except AssertionError as e:
      dump_error_msg(e)

def testcase_AvgPool2d(
    B=3,
    N=32,
    C=16,
    HWin=28,
    kernel_size=(2, 2),
    stride=(2, 2),
    padding=(1, 1),
    device=torch.device('cpu'),
    dtype=torch.float,
):
  with torch.no_grad():
    x_array = [
        torch.rand(N, C, HWin, HWin, device=device, dtype=dtype)
        for _ in range(B)
    ]
    x_fused = torch.cat([x.unsqueeze(1) for x in x_array], dim=1)
    args = (
        kernel_size,
        stride,
        padding,
    )
    pool_array = [nn.AvgPool2d(*args) for _ in range(B)]
    pool_fused = get_hfta_op_for(nn.AvgPool2d, B=B)(*args)
    y_array = [pool_array[b](x_array[b]) for b in range(B)]
    y_fused_actual = pool_fused(x_fused)
    y_fused_expect = torch.cat([y.unsqueeze(1) for y in y_array], dim=1)
    try:
      assert_allclose(
          y_fused_actual.cpu().numpy(),
          y_fused_expect.cpu().numpy(),
          rtol=1e-4,
      )
    except AssertionError as e:
      dump_error_msg(e)

if __name__ == '__main__':
  testcase_automator(
      testcase_MaxPool2d,
      {
          'B': [1, 2, 5, 10],
          'N': [1, 8, 64],
          'C': [3, 64, 128],
          'kernel_size': [1, 3, 4],
          'HWin': [32, 256],
          'stride': [1, 3, 4],
          'padding': [1],
          'dilation': [2, 5],
          'return_indices': [True],
          'ceil_mode': [True],
          'device': [torch.device('cuda:0')],
          'dtype': [torch.double],
      },
  )
  testcase_automator(
      testcase_AdaptiveAvgPool2d,
      {
          'B': [1, 2, 5, 10],
          'N': [1, 8, 64],
          'C': [3, 64, 128],
          'HWin': [32, 256],
          'output_size': [
              (5, 5),
              (7, 16),
              (17, 6),
              (14, None),
              (None, 20),
              (None, None),
              9,
          ],
          'device': [torch.device('cuda:0')],
          'dtype': [torch.double],
      },
  )
  testcase_automator(
      testcase_AvgPool2d,
      {
          'B': [1, 2, 5, 10],
          'N': [1, 8, 64],
          'C': [3, 64, 128],
          'HWin': [32, 256],
          'kernel_size': [
              (5, 5),
              (7, 16),
              (17, 6),
              9,
          ],
          'stride': [None, (1, 2), (2, 2), 3],
          'padding': [0, (1, 1), 1],
          'device': [torch.device('cuda:0')],
          'dtype': [torch.double],
      },
  )


"""
Kliko and parameter validation related functions.
"""
from pykwalify.core import Core
import yaml
import json
import os
import re
from kliko import parameters_file as parameters_file_default
from kliko import kliko_file as kliko_file_default

here = os.path.dirname(os.path.realpath(__file__))

SCHEMA_VERSION = 4

def validate_kliko(kliko, version=SCHEMA_VERSION):
    """
    validate a kliko yaml string

    args:
        kliko: a parsed kliko object

    returns:
        dict: a (nested) kliko structure

    raises:
        an exception if the string can't be parsed or is not in the following the Kliko schema
    """
    # first try to parse it, to make sure it is parsable

    schema_file = os.path.join(here, "schemas/%s.yml" % version)
    c = Core(source_data=kliko, schema_files=[schema_file])
    c.validate(raise_exception=True)
    return kliko

list_regex = re.compile('List\[(int|float|bool|file|str)\]')

type_map = {
    'choice': 'str',
    'char': 'str',
    'file': 'str',
}

def convert_to_parameters_schema(kliko):
    """
    Convert a kliko schema into a validator for the parameters generated with a kliko schema.

    args:
        kliko (str): a kliko definition
    returns:
        A structure for a pykwalify validator
    """
    mapping = {}

    for section in kliko['sections']:
        for field in section['fields']:
            value = {'required': 'required' in field}

            # check if field is a list
            match = list_regex.match(field['type'])
            if match:
                value['type'] = 'seq'
                matchtype = match.group(1)
                value['sequence'] = [{'type': type_map.get(matchtype, matchtype)}]
            # otherwise check if it is a choice
            elif field['type'] == 'choice':
                value['type'] = 'str'
                value['enum'] = list(field['choices'].keys())
            else:
                value['type'] = type_map.get(field['type'], field['type'])

            mapping[field['name']] = value

    return {'type': 'map', 'mapping': mapping}

def validate_parameters(parameters, kliko):
    """
    validate a set of parameters given a kliko definition

    args:
        parameters (dict): A structure that should follow the given kliko structure
        kliko (dict): A nested dict which defines the valid parameters in Kliko format

    returns:
        str: the parsed parameters

    raises:
        an exception if the string can't be parsed or is not in the defining valid parameters
    """
    schema = convert_to_parameters_schema(kliko)
    c = Core(source_data=parameters, schema_data=schema)
    c.validate(raise_exception=True)
    return True

def validate(kliko_file=False, paramaters_file=False):
    """
    Validate the kliko and paramaters file and parse the parameters file. Should be run inside
    the Kliko container.

    Args:
        kliko_file (str):  Path to a kliko file
        paramaters_file (str): path to a parameters file

    Returns:
        The validated and parsed paramaters file
    """
    kliko_file = kliko_file or kliko_file_default
    paramaters_file = paramaters_file or parameters_file_default

    with open(kliko_file, 'r') as f:
        kliko = yaml.safe_load(f)

    with open(paramaters_file, 'r') as f:
        parameters = json.load(f)

    return validate_opened(kliko, parameters)

def validate_opened(kliko, parameters):
    validate_kliko(kliko)
    validate_parameters(parameters, kliko)

    defaults = {}
    for section in kliko['sections']:
        for field in section['fields']:
            if 'initial' in field:
                defaults[field['name']] = field['initial']

    defaults.update(parameters)
    return defaults


'''outputToExcel.py - output the word result to an excel workbook.

'''

import wordExtract
import openpyxl
import currentTime
import os
import shelve
import shelfer # my own module shelfer.py
import logging

logging.basicConfig(level = logging.DEBUG, format = ' %(asctime)s - %(levelname)s - %(message)s')
logging.disable(logging.CRITICAL)

def output():
    # generate the shelf of the dictionary in .\data
    shelfer.work()

    #do the extraction work

    wordList = wordExtract.extract()
    logging.debug(wordList)

    # create an excel file

    wb = openpyxl.Workbook()    # workbook
    ws = wb.active   # worksheet
    # initial the headline
    ws['A1'] = '序号'
    ws['B1'] = '单词'
    ws['C1'] = '释意'
    ws['D1'] = '词频'

    # output the words and the numbers

    line = 1

    shelfFile = shelve.open('.\\data\dict')
    if shelfFile.keys() == []:
        raise Exception('Run shelfer.py first!')

    for word, num in wordList:
        try:
            #Another mode:get from the internet: translation = trans.getTranslation(word)

            if word not in shelfFile.keys():
                continue

            translation = shelfFile[word]

            line += 1
            ws['A' + str(line)] = line - 1
            ws['B' + str(line)] = word
            ws['C' + str(line)] = translation
            ws['D' + str(line)] = num
        except Exception as exc:
            print('Translation error: %s' %(exc))

    # set the column width
    ws.column_dimensions['A'].width = 5
    ws.column_dimensions['B'].width = 17
    ws.column_dimensions['C'].width = 90
    ws.column_dimensions['D'].width = 7

    # file directory work
    cT = currentTime.getTime()
    binPath = '.\\生成的文件\\'
    if not os.path.exists(binPath):
        os.makedirs(binPath)
    fname = binPath + '词频表 ' + cT + '.xlsx'

    #save
    wb.save(fname)
    print('词频表生成成功，位于\t' + os.path.abspath(fname))

if __name__=='__main__':
    print('main')
    output()


from test_base import t35t_case, check_output, run_command, CommandLineTestBase, make_setup_module, teardown_module, make_dirs

from os.path import join

setup_module = make_setup_module(__name__)

def get_output_path(base_path):
    return join(base_path, 'test_performance')

def create_t35t(file, name):
    @t35t_case(name)
    def test_func(self):
        self.args += [
            '--results', get_output_path(self.output_path),
            self.get_data(file),
        ]
        run_command(self.args)
    test_func.__name__ = name
    return test_func

class Tests(CommandLineTestBase):
    def __init__(self):
        super(Tests, self).__init__()

    def setup(self):
        super(Tests, self).setup()
        make_dirs(get_output_path(self.output_path))
        self.args += [
            'org.genemania.plugin.apps.QueryRunner',
            '--data', self.config.data_path,
        ]

for organism in [
    'arabidopsis',
    'worm',
    'fly',
    'human',
    'mouse',
    'yeast',
]:
    name = 'test_%s' % organism
    query = join('performance', '%s.qr' % organism)
    setattr(Tests, name, create_t35t(query, name))


# -*- coding: utf-8 -*-
"""
RADIS Hitran Functions; based on Common API hitranapi.py
"""

from os.path import abspath, expanduser, join

from radis.api.hitranapi import HITRANDatabaseManager
from radis.misc.warning import AccuracyWarning

# TODO: implement parallel=True for all isotopes ?
def fetch_hitran(
    molecule,
    extra_params=None,
    local_databases=None,
    databank_name="HITRAN-{molecule}",
    isotope=None,
    load_wavenum_min=None,
    load_wavenum_max=None,
    columns=None,
    cache=True,
    verbose=True,
    clean_cache_files=True,
    return_local_path=False,
    engine="default",
    output="pandas",
    parallel=True,
    parse_quanta=True,
):
    """Download all HITRAN lines from HITRAN website. Unzip and build a HDF5 file directly.

    Returns a Pandas DataFrame containing all lines.

    Parameters
    ----------
    molecule: str
        one specific molecule name, listed in HITRAN molecule metadata.
        See https://hitran.org/docs/molec-meta/
        Example: "H2O", "CO2", etc.
    local_databases: str
        where to create the RADIS HDF5 files. Default ``"~/.radisdb/hitran"``.
        Can be changed in ``radis.config["DEFAULT_DOWNLOAD_PATH"]`` or in ~/radis.json config file
    databank_name: str
        name of the databank in RADIS :ref:`Configuration file <label_lbl_config_file>`
        Default ``"HITRAN-{molecule}"``
    isotope: str
        load only certain isotopes : ``'2'``, ``'1,2'``, etc. If ``None``, loads
        everything. Default ``None``.
    load_wavenum_min, load_wavenum_max: float (cm-1)
        load only specific wavenumbers.
    columns: list of str
        list of columns to load. If ``None``, returns all columns in the file.
    extra_params: 'all' or None
        Downloads all additional columns available in the HAPI database for the molecule including
        parameters like `gamma_co2`, `n_co2` that are required to calculate spectrum in co2 diluent.
        For eg:
        ::

            from radis.io.hitran import fetch_hitran
            df = fetch_hitran('CO', extra_params='all', cache='regen') # cache='regen' to regenerate new database with additional columns

    Other Parameters
    ----------------
    cache: ``True``, ``False``, ``'regen'`` or ``'force'``
        if ``True``, use existing HDF5 file. If ``False`` or ``'regen'``, rebuild it.
        If ``'force'``, raise an error if cache file cannot be used (useful for debugging).
        Default ``True``.
    verbose: bool
    clean_cache_files: bool
        if ``True`` clean downloaded cache files after HDF5 are created.
    return_local_path: bool
        if ``True``, also returns the path of the local database file.
    engine: 'pytables', 'vaex', 'default'
        which HDF5 library to use. If 'default' use the value from ~/radis.json
    output: 'pandas', 'vaex', 'jax'
        format of the output DataFrame. If ``'jax'``, returns a dictionary of
        jax arrays. If ``'vaex'``, output is a :py:class:`vaex.dataframe.DataFrameLocal`

        .. note::
            Vaex DataFrames are memory-mapped. They do not take any space in RAM
            and are extremely useful to deal with the largest databases.

    parallel: bool
        if ``True``, uses joblib.parallel to load database with multiple processes
    parse_quanta: bool
        if ``True``, parse local & global quanta (required to identify lines
        for non-LTE calculations ; but sometimes lines are not labelled.)

    Returns
    -------
    df: pd.DataFrame or vaex.dataframe.DataFrameLocal
        Line list
        A HDF5 file is also created in ``local_databases`` and referenced
        in the :ref:`RADIS config file <label_lbl_config_file>` with name
        ``databank_name``
    local_path: str
        path of local database file if ``return_local_path``

    Examples
    --------
    ::

        from radis.io.hitran import fetch_hitran
        df = fetch_hitran("CO")
        print(df.columns)
        >>> Index(['id', 'iso', 'wav', 'int', 'A', 'airbrd', 'selbrd', 'El', 'Tdpair',
            'Pshft', 'gp', 'gpp', 'branch', 'jl', 'vu', 'vl'],
            dtype='object')

    .. minigallery:: radis.fetch_hitran

    Notes
    -----
    if using ``load_only_wavenum_above/below`` or ``isotope``, the whole
    database is anyway downloaded and uncompressed to ``local_databases``
    fast access .HDF5 files (which will take a long time on first call). Only
    the expected wavenumber range & isotopes are returned. The .HFD5 parsing uses
    :py:func:`~radis.io.hdf5.hdf2df`

    See Also
    --------
    :py:func:`~radis.io.hitemp.fetch_hitemp`, :py:func:`~radis.io.exomol.fetch_exomol`,
    :py:func:`~radis.io.geisa.fetch_geisa`,
    :py:func:`~radis.api.hdf5.hdf2df`, :py:meth:`~radis.lbl.loader.DatabankLoader.fetch_databank`

    """

    if r"{molecule}" in databank_name:
        databank_name = databank_name.format(**{"molecule": molecule})

    if local_databases is None:
        import radis

        local_databases = join(radis.config["DEFAULT_DOWNLOAD_PATH"], "hitran")
    local_databases = abspath(local_databases.replace("~", expanduser("~")))

    ldb = HITRANDatabaseManager(
        databank_name,
        molecule=molecule,
        local_databases=local_databases,
        engine=engine,
        extra_params=extra_params,
        verbose=verbose,
        parallel=parallel,
    )

    # Get expected local files for this database:
    local_file = ldb.get_filenames()

    # Delete files if needed:
    if cache == "regen":
        ldb.remove_local_files(local_file)
    else:
        # Raising AccuracyWarning if local_file exists and doesn't have extra columns in it
        if ldb.get_existing_files(local_file) and extra_params == "all":
            columns = ldb.get_columns(local_file[0])
            extra_columns = ["y_", "gamma_", "n_"]
            found = False
            for key in extra_columns:
                for column_name in columns:
                    if key in column_name:
                        found = True
                        break

            if not found:
                import warnings

                warnings.warn(
                    AccuracyWarning(
                        "All columns are not downloaded currently, please use cache = 'regen' and extra_params='all' to download all columns."
                    )
                )

    ldb.check_deprecated_files(
        ldb.get_existing_files(local_file),
        auto_remove=True if cache != "force" else False,
    )

    # Download files
    download_files = ldb.get_missing_files(local_file)
    if download_files:
        ldb.download_and_parse(download_files, cache=cache, parse_quanta=parse_quanta)

    # Register
    if not ldb.is_registered():
        ldb.register()

    if len(download_files) > 0 and clean_cache_files:
        ldb.clean_download_files()

    # Load and return
    df = ldb.load(
        local_file,
        columns=columns,
        within=[("iso", isotope)] if isotope is not None else [],
        # for relevant files, get only the right range :
        lower_bound=[("wav", load_wavenum_min)] if load_wavenum_min is not None else [],
        upper_bound=[("wav", load_wavenum_max)] if load_wavenum_max is not None else [],
        output=output,
    )

    return (df, local_file) if return_local_path else df

# ======================================================
# %% Test

if __name__ == "__main__":

    from radis.test.io.test_hitran_cdsd import _run_testcases

    print("Testing HITRAN parsing: ", _run_testcases())
    from radis.test.io.test_query import _run_testcases

    print("Testing HITRAN fetch: ", _run_testcases())


#!/usr/bin/env -S ipython3 -i
import matplotlib.pyplot as plt
if "science" in plt.style.available: plt.style.use("science")

import numpy as np
from skimage.io import imread
from skimage.color import rgb2gray
from skimage import img_as_ubyte

import task_3
from imshow_many import imshow_many

def threshold(img, th):
    return np.uint8(img > th)

def task_4_1(do_savefig = True):
    img_rgb = imread("test_images/pillsetc.png")
    img     = rgb2gray(img_rgb)
    th = 100
    img_th  = threshold(img, th / 256)

    imshow_many([img_rgb, img, img_th],
                ["original image",
                 "grayscaled image",
                 "thresholded at 100"],
                png_out = "figures/task_4_1.png" if do_savefig else None,
                figsize = (12, 8))

    return img_th

def task_4_2(do_savefig = True):
    img    = rgb2gray(imread("test_images/pillsetc.png"))

    hist = task_3.img_hist(img)
    cdf  = task_3.img_cdf(img)

    # PLOT SETUP
    fig, ax = plt.subplots(1, 1, figsize = (6, 4))
    ax.set_ylabel("Frequency", fontsize = 16)
    ax.set_xlabel("Pixel value", fontsize = 16)
    ax.grid()

    ax2 = ax.twinx()
    ax2.axis("off")

    # PLOTTING
    xs = range(256)
    ax.bar(xs, hist, label = "pillsetc.png histogram")
    ax2.plot(xs, cdf, color = "red", alpha = 0.5)

    plt.show()

    fig.tight_layout()
    if do_savefig:
        fig.savefig("figures/task_4_2.png", bbox_inches = "tight")

if __name__ == "__main__":
    pass
    task_4_1(do_savefig = False)
    #  task_4_2(do_savefig = False)


#!/usr/bin/env python
# -*- coding: utf-8  -*-

# conda execute
# env:
#  - python >=3

import sys, getopt, re
import excluidos

def filtrar(excluidos, revisados):
  listados = set()
  titulo=''
  pat = re.compile("^.*?\[\[(.*?)\]\].*$")
  linea = sys.stdin.readline()
  while linea:
    match = pat.match(linea)
    if match:
      titulo = match.group(1)
      if titulo not in excluidos and titulo not in revisados \
            and titulo not in listados:
        listados.add(titulo)
        print(linea, end='')
    linea = sys.stdin.readline()

def main (argv):
   filtrar(excluidos.excluidos, excluidos.revisados)

if __name__ == "__main__":
   main(sys.argv[1:])


import numpy as np
import imutils
import cv2
from matplotlib import pyplot as plt

#

bgr_img = cv2.imread('../../resources/omr-imgs/omr-1.png')  # read as it is

if bgr_img.shape[-1] == 3:  # color image
    b, g, r = cv2.split(bgr_img)  # get b,g,r
    rgb_img = cv2.merge([r, g, b])  # switch it to rgb
    gray_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2GRAY).copy()
else:
    gray_img = bgr_img.copy()

img = cv2.medianBlur(gray_img, 15)
cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0)

circles = np.uint16(np.around(circles))

for i in circles[0, :]:
    # draw the outer circle
    cv2.circle(cimg, (i[0], i[1]), i[2], (0, 255, 0), 2)
    # draw the center of the circle
    cv2.circle(cimg, (i[0], i[1]), 2, (0, 0, 255), 3)

plt.subplot(121), plt.imshow(rgb_img)
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(cimg)
plt.title('Hough Transform'), plt.xticks([]), plt.yticks([])
plt.show()


#!/usr/bin/env python

scores = {}
scores_total = 0

with open("../DATA/testscores.dat") as f:

    for line in f:
        (name,score) = line.split(":")
        score = int(score)
        scores[name] = score
        scores_total += score

for student, score in sorted(scores.items()):
    grade = None
    if score > 94:
        grade = 'A'
    elif score > 88:
        grade = 'B'
    elif score > 82:
        grade = 'C'
    elif score > 74:
        grade = 'D'
    else:
        grade = 'F'
    print("{0:<20s} {1} {2}".format(student,scores[student],grade))

average = float(scores_total)/len(scores)
print("\naverage score is  {0:.2f}\n".format(average))


###

import os

path1 = "C:\\Users\\Saketh Gondela\\Desktop\\Python\\Files\\My_Details"
#path2 = "C:\\Users\\Saketh Gondela\\Desktop\\Python\\Files\\Personal"

if os.path.exists:
    print("File exists in the location")

if os.path.exists:
    print("Directory exists in the location")

if os.path.isfile(path1):
    print("It is a file!")
elif os.path.isdir(path1):
    print("It is a Directory!!!")


#!/usr/bin/python
import pickle

def main():
    num_files = 4
    fl2prediction = {}
    for i in range(num_files):
        fn = 'better_pickles/predictions_%d.pickle' % i
        fl2some = pickle.load(open(fn,'rb'))
        for fl in fl2some:
            fl2prediction[fl] = fl2some[fl]

    out_fn = 'better_pickles/fl2prediction.pickle'
    with open(out_fn,'wb') as output:
        pickle.dump(fl2prediction,output)

if __name__ == '__main__':
    main()


class Submarine:

	'''

	-------------------
	Test Documentation

	Program for submarine
	-------------------

	'''

	#class that you can put  many functions
	#def that you put object

	def __init__(self,price=10000,budget=400000,battery_distance=10000): #self is reference of object/ price=10000 , budget =
		self.cap_name = 'Chris'
		self.sub_name = 'Navy Force'
		self.price = price #milion
		self.kilometre = 0 #setting 0
		self.budget = budget
		self.totalcost = 0 #setting 0
		self.battery_distance = battery_distance

	def Missile(self):
		print('We are Department of Missile')

	def Calcommission(self):
		percent =  self.price * (10/100) #commissiom 10%
		print('Congrat! You got:  {} Million AUD'.format(percent))

	def Goto(self,enemypoint,distance):
		print(f"Let's go to {enemypoint} Distance: {distance} km")
		self.kilometre = self.kilometre + distance #self.kilometre += distance
		self.Fuel()

	def Fuel(self):
		diesel = 20 #per litre
		cal_fuel_cost = self.kilometre * diesel
		print('Current fuel cost: {:,d} AUD'.format(cal_fuel_cost))
		self.totalcost += cal_fuel_cost

	@property
	def BudgetRemaining(self):
		remaining = self.budget - self.totalcost
		print('Budget Remaining: {:,.2f} AUD'.format(remaining))
		return remaining

class ElectricSubmarine(Submarine):

	def __int__(self,price=10000,budget=400000,battery_distance=100000):
		self.sub_name = 'Stark II'
		super().__init__(price,budget,battery_distance) #super()transfer/copy every function into class ElectricSubmarine
		#sybmarine can go out 10000km/ 100 percent

	def Battery(self):
		allbattery = 100
		print('kilometre',self.kilometre)
		calculate = (self.kilometre / self.battery_distance) * 100
		print('CAL: ',calculate)

		print('We have battery remaining: {}%'.format(allbattery-calculate))

	def Fuel(self):
		kilowattcost = 5 #per kilowatt
		cal_fuel_cost = self.kilometre * kilowattcost
		print('Current Power cost: {:,d} AUD'.format(cal_fuel_cost))
		self.totalcost += cal_fuel_cost

print(__name__)
if __name__== '__main__':

	tesla = ElectricSubmarine(40000,2000000,100000) #price+budget
	print(tesla.cap_name)
	print(tesla.budget)
	tesla.Goto('Japan',10000)
	print(tesla.BudgetRemaining) #call function fuel in fucntion Budgetremaining
	tesla.Battery()

	print('-------------------------------------')

	Navykongtab = Submarine(40000,2000000,10000) #price+budget
	print(Navykongtab.cap_name)
	print(Navykongtab.budget)
	Navykongtab.Goto('Japan',10000)
	print(Navykongtab.BudgetRemaining)

'''
Navykongtab = Submarine(1000)  #calculate here with amount
print(Navykon
gtab.cap_name) #ref object or Navykongtab
print(Navykongtab.sub_name)
print('-------------------')
print(Navykongtab.kilometre)
Navykongtab.Goto('China',7000)
print(Navykongtab.kilometre)
Navykongtab.Fuel()
#Navykongtab.BudgetRemaining() > from @property
current_budget = Navykongtab.BudgetRemaining
print(current_budget * 0.2)

#compare to non-function
#sub  =  ['Chris','NavyForce2',5000]
#print(sub[0])

Navykongtab.Calcommission()
########################################
print('-------------Sub no.2--------------')
print('Before..')
Military = Submarine(70000)
print(Military.cap_name)
print('After..')
Military.cap_name = 'Evan'
print(Military.cap_name)
'''


import torch
import torch.nn as nn
import torch.nn.functional as tF
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
from torchvision import datasets, transforms, models
import numpy as np
import shutil, time, os, requests, random, copy

class C100DataGen(Dataset):
    def __init__(self,phase,imgarr,s = 0.5):
        self.phase = phase
        self.imgarr = imgarr
        self.s = s
        self.mean = np.mean(self.imgarr/255.0,axis=(0,2,3),keepdims=True) # mean of the dataset
        self.std = np.std(self.imgarr/255.0,axis=(0,2,3),keepdims=True) # std of the dataset
        self.transforms = transforms.Compose([transforms.RandomHorizontalFlip(0.5), # random horizontal flip with 0.5 probability
                                              transforms.RandomResizedCrop(32,(0.8,1.0)), # random crop with 0.8-1.0 scaling factor
                                              transforms.Compose([transforms.RandomApply([transforms.ColorJitter(0.8*self.s, # random color jitter with 0.8 scaling factor
                                                                                0.8*self.s,
                                                                                0.8*self.s,
                                                                                0.2*self.s)], p = 0.8),
                                                                  transforms.RandomGrayscale(p=0.2) # random grayscale with 0.2 probability
                                                                 ])]) # random apply color jitter and grayscale with 0.8 and 0.2 probability respectively

    def __len__(self):
        return self.imgarr.shape[0]

    # get the data for a given index
    def __getitem__(self,idx):

        x = self.imgarr[idx]
        #print(x.shape)
        x = x.astype(np.float32)/255.0

        x1 = self.augment(torch.from_numpy(x))
        x2 = self.augment(torch.from_numpy(x))

        x1 = self.preprocess(x1)
        x2 = self.preprocess(x2)

        return x1, x2

    # shuffle the data at the end of each epoch
    def on_epoch_end(self):
        self.imgarr = self.imgarr[random.sample(population = list(range(self.__len__())),k = self.__len__())]

    def preprocess(self,frame):
        frame = (frame-self.mean)/self.std
        return frame

    # data augmentation
    def augment(self, frame, transformations = None):

        if self.phase == 'train':
            frame = self.transforms(frame)
        else:
            return frame

        return frame


import sublime, sublime_plugin
import os

class CreateSnippetCommand(sublime_plugin.TextCommand):

  saveOnCreate = False

  def run(self, edit):
    # get the selection
    self.content = self.getSelectionContent(edit);

    if not self.content:
      return;

    # get as much information to prefill some snippet information
    self.getInfo()

    # create content for the snippet
    snippet = self.getSnippet();

    # open a new file
    self.newSnippet()

    # insert the string
    self.view.insert(edit, 0, snippet)

    # position the cursor to edit the content of the

    pos = sublime.Region(30, 30 + len(self.content))
    self.view.sel().clear()
    self.view.sel().add(pos)

    # either before or after this, or between, trigger a dialog to choose a package and tell to save
    self.showPackageList()

  def getSelectionContent(self, edit):
    for region in self.view.sel():
      content = self.view.substr(region)
      if len(content) != 0:
        return content
    return False;

  def getInfo(self):
    file = self.view.file_name()
    if file:
      self.ext = file[file.rfind(".") + 1:]
    else:
      self.ext = ''
    self.firstword = self.content[0:self.content.find(" ")]
    if self.content.find(" ") == -1:
      self.firstword = self.content

  def getSnippet(self):
    return "<snippet>\n\t<content><![CDATA[" + self.content.lower() + "]]></content>\n\t<tabTrigger>" + self.firstword.lower() + "</tabTrigger>\n\t<scope>source." + self.ext+ "</scope>\n\t<description>" + self.firstword[0:1].upper() + self.firstword[1:].lower() + "</description>\n</snippet>"

  def newSnippet(self):
    self.view = sublime.active_window().new_file();
    # set the syntax to XML
    self.view.set_syntax_file("Packages/XML/XML.tmLanguage")

  def saveSnippet(self):
    self.view.run_command("save")

  def getPackageList(self):
    # get a list of packages from sublime's package folder
    self.path = sublime.packages_path()

    self.packageList = []
    for dir in os.listdir(self.path):
      if os.path.isdir(os.path.join(self.path, dir)):
        self.packageList.append(dir)

  def showPackageList(self):
    self.getPackageList();
    sublime.status_message("Choose a package for the new snippet.")
    sublime.active_window().show_quick_panel(self.packageList, self.choosePackage)

  def choosePackage(self, index):

    if index != -1:
      self.snippet_filename = os.path.join(self.path, self.packageList[index], self.firstword + ".sublime-snippet")
      self.view.retarget(self.snippet_filename)
      if self.saveOnCreate:
         self.saveSnippet()
    else:
      sublime.status_message("No package selected for the new snippet.")

  def is_enabled(self):
    return self.view != None

  def description(self):
    return "Snippet: Create Snippet from selection"


from flask import Flask,render_template,request,jsonify
import datetime
import pickle
import numpy as np

app= Flask(__name__)

@app.route('/',methods=['GET','POST'])
def forecast():
    if request.method=="POST":

        startdate = datetime.datetime.strptime( request.form['sdate'],'%Y-%m-%d')
        enddate = datetime.datetime.strptime(request.form['edate'], '%Y-%m-%d')
        loadmodel = pickle.load(open('sarimax.pkl', 'rb'))
        results=np.expm1(loadmodel.predict(start=startdate, end=enddate, dynamic=False)).sum()
        results=str(round(results))
        return ("REVENUE FORECASTING FOR GIVEN DATE RANGE IS : "+results)

    return render_template('index.html')

if __name__=="__main__":
    app.run(debug=True)


#!/usr/bin/python3

import rospy
from geometry_msgs.msg import PoseStamped
from std_msgs.msg import Int64, String, Bool
import random
from itertools import combinations

num_robots = 4
robots = [f"robot{num}" for num in range(1,num_robots+1)]
bomb_duration = 10
bomb_range = 1
thrower_length_advantage = 1

class Robot:
    def __init__(self, name):
        self.name = name

        self.kill_sub = rospy.Subscriber(f"/{name}/kill", Bool, self.kill)
        self.pose_sub = rospy.Subscriber(f"/{name}/pose", PoseStamped, self.set_pose)
        self.stop_pub = rospy.Publisher(f"/{name}/stop", Int64, queue_size=1)
        self.target_pub = rospy.Publisher(f"/{name}/bomb_target", String, queue_size=1)
        self.pose_stamped = PoseStamped()
        self.bomb_available = True
        self.is_killed = False

    def kill(self, _):
        self.is_killed = True

    def reset_bomb(self, _):
        self.bomb_available = True

    def distance_to(self, other):
        # Euclidean distance
        a = self.pose_stamped
        try:
            b = other.pose_stamped
        except AttributeError:
            b = other
        return ((a.pose.position.x - b.pose.position.x)**2 + (a.pose.position.y - b.pose.position.y)**2) ** 0.5

    def set_pose(self, pose):
        self.pose_stamped = pose

    def throw_bomb_at(self, target):
        if self.is_killed or target.is_killed:
            return
        target_msg = String()
        target_msg.data = target.name
        self.target_pub.publish(target_msg)

        stop_duration = Int64()
        stop_duration.data = bomb_duration
        self.stop_pub.publish(stop_duration)

        self.bomb_available = False
        target.bomb_available = False
        rospy.Timer(rospy.Duration(bomb_duration) * 1.25, self.reset_bomb, oneshot=True)
        rospy.Timer(rospy.Duration(bomb_duration) * 1.25, target.reset_bomb, oneshot=True)

class Planner:
    def __init__(self):
        self.robots = [Robot(robot) for robot in robots]
        self.rate = rospy.Rate(2)

    def bomb_calculation(self):
        for robot1, robot2 in list(combinations(self.robots, 2)):
            if robot1.bomb_available and robot1.distance_to(robot2) <= bomb_range + thrower_length_advantage:
                if random.random() < 0.5:
                    robot1.throw_bomb_at(robot2)
                else:
                    robot2.throw_bomb_at(robot1)

    def run(self):
        while not rospy.is_shutdown():
            self.bomb_calculation()
            for robot in self.robots:
                if robot.is_killed:
                    self.robots.remove(robot)
            self.rate.sleep()

if __name__=="__main__":
    rospy.init_node('path_planner')
    planner = Planner()
    planner.run()


class Solution:
    def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
        dict = {}

        for i, n in enumerate(nums):
            if n in dict and abs(i - dict[n]) <= k:
                return True
            else:
                dict[n] = i

        return False


#!/usr/bin/env python
#Created by jinshiwu on 2/22/17.

fruit = ["pomegranate","cherry","apricot","date","Apple","lemon","Kiwi",
         "ORANGE","lime","Watermelon","guava","papaya","FIG","pear","banana",
         "Tamarind","persimmon","elderberry","peach","BLUEberry","lychee",
         "grape" ]
sorted_fruit = sorted(fruit)
print(' '.join(sorted_fruit))

print(sorted(fruit, key=len))

computer_guys = [
    ('Mark', 'Zuckerberg', 'Facebook'),
    ('Guido', 'Van Rossum', 'Python'),
    ('Larry', 'Wall', 'Perl'),
    ('Bill', 'Joy', 'Sun'),
    ('Bill', 'Gates', 'Microsoft'),
    ('Steve', 'Case', 'AOL'),
    ('Larry', 'Ellison', 'Oracle'),
    ('Steve', 'Jobs', 'Apple'),
    ('Dennis', 'Ritchie', 'Unix'),
]
#
# for firstName, lastName, company in sorted(computer_guys):
#     print(firstName, lastName, company)

for firstName, lastName, comapny in sorted(computer_guys, key = lambda e: e[1]):
    print(firstName, lastName, comapny)


"""""
-----------------------------------------------------------------------------------------------------------
1. Read .a List of Numbers from a file and arrange it ascending Order in a
Linked List. Take user input for a number, if found then pop the number out of the
list else insert the number in appropriate position
-----------------------------------------------------------------------------------------------------------
"""
from Scripts.com.BridgeLabs.Functional.LinkedList import LinkedList

class UnordredList:
    if __name__ == '__main__':
        # Creating the object of the linked list
        List = LinkedList()
        # opening the file in read mode and spliting it using the words String
        file = open("manitxt", "r")
        for i in file:
            words = i.split(" ")
        # After spliting adding the elements to the list
        for i in words:
            List.add(i)
        #     taking the input string from the user to search weather it is present in the list or not
        String = str(input("Enter the String to check : "))
        print("Before Searching elements in the list are : ")
        # printing the list before searching
        List.show()
        # calling the searching method in the linked list class passing the string
        # if the element is present in the list then it will delete element from the list
        if List.search(String):
            index = List.indexOf(String)
            List.pop(String)
            print("After searching elements in the list are : ")
            List.show()
            print("Element removed at :", index)
        # if the string is not present in the list it will be added to the list
        else:
            List.add(String)
            print("After searching")
            # After adding to the list printing list
            List.show()
            print("Element added at Last of the list ")
            # open the file in a+ mode it means for adding the text to the file
            # here we are adding the string to the file and after adding closing the file
            f = open("manitxt", "a+")
            f.write(String)
            f.close()


from .db import db

class UserRoutine(db.Model):
    __tablename__ = "user_routines"

    id = db.Column(db.Integer, primary_key=True)
    userId = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    routineId = db.Column(db.Integer, db.ForeignKey(
        'routines.id'), nullable=False)
    active = db.Column(db.Boolean, nullable=False, default=False)
    complete = db.Column(db.Boolean, nullable=False, default=False)
    removed = db.Column(db.Boolean, nullable=False, default=False)
    created_on = db.Column(db.DateTime, server_default=db.func.now())
    updated_on = db.Column(
        db.DateTime, server_default=db.func.now(), server_onupdate=db.func.now())

    # RLEATIONSHIPS
    routine = db.relationship('Routine',
                              back_populates='user_routines'
                              )
    user = db.relationship('User',
                           back_populates='user_routines'
                           )

    def to_dict(self):
        return {
            'id': self.id,
            'userId': self.userId,
            'routineId': self.routineId,
            'routine': self.routine.short_dict(),
            'active': self.active,
            'complete': self.complete,
            'removed': self.removed,
        }

    def short_dict(self):
        return {
            'id': self.id,
            'userId': self.userId,
            'routineId': self.routineId,
            # 'routine': self.routine.short_dict(),
            'active': self.active,
            'complete': self.complete,
            'removed': self.removed,
        }


def mid(l, r): return (l+r)//2
def left(v): return v+1
def right(v, l, r): return v+2*(mid(l, r)-l+1)
class SegmentTreeSum:
    ROOT = 1
    def __init__(self, vs):
        self.n = len(vs)
        self.xs = [0]*2*self.n
        self._build(vs, self.ROOT, 0, self.n-1)
    def _build(self, vs, v, tl, tr):
        # tl, tr -- the range v is responsible for
        if tl == tr: self.xs[v] = vs[tl]
        else:
            lchild, tm, rchild = left(v), mid(tl, tr), right(v, tl, tr)
            self._build(vs, lchild, tl, tm)
            self._build(vs, rchild, tm+1, tr)
            self.xs[v] = self.xs[lchild] + self.xs[rchild]
    def _query(self, v, tl, tr, l, r):
        # tl, tr -- the range v is response for
        # l, r -- query boundary, inclusive
        if (l > r): return 0
        if ((l == tl) and (r == tr)): return self.xs[v]
        lchild, tm, rchild = left(v), mid(tl, tr), right(v, tl, tr)
        a = self._query(lchild, tl, tm, l, min(r, tm))
        b = self._query(rchild, tm+1, tr, max(l, tm+1), r)
        return a+b
    def query(self, l, r):
        return self._query(self.ROOT, 0, self.n-1, l, r)
    def _update(self, v, tl, tr, pos, val):
        if tl == tr: self.xs[v] = val
        else:
            lchild, tm, rchild = left(v), mid(tl, tr), right(v, tl, tr)
            if pos <= tm: self._update(lchild, tl, tm, pos, val)
            else: self._update(rchild, tm+1, tr, pos, val)
            self.xs[v] = self.xs[lchild] + self.xs[rchild]
    def update(self, pos, val):
        return self._update(self.ROOT, 0, self.n-1, pos, val)

def test(tree, vs, l, r):
    expected = sum(vs[l:r+1])
    actual = tree.query(l, r)
    assert expected == actual, 'sum(%s,%s) != %s, actual=%s' % (l, r, expected, actual)

if __name__ == '__main__':
    vs = [1,1,1,1,1]
    t = SegmentTreeSum(vs)
    test(t, vs, 0, 0)
    test(t, vs, 0, 1)
    test(t, vs, 0, 2)
    test(t, vs, 0, 3)
    test(t, vs, 1, 3)
    test(t, vs, 2, 3)
    test(t, vs, 3, 3)
    t.update(1, 10)
    vs[1] = 10
    test(t, vs, 0, 1)
    test(t, vs, 1, 2)


k = int(input())
str = input()
n = len(str)
current = 0
count = 0
result = 0
data = [n + 1] * 26

for i in range(n):
  ch = ord(str[i]) - ord('a')

  if data[ch] == n + 1:
    if count < k:
      count += 1
    else:
      next = min(data)
      ind = data.index(next)
      data[ind] = n + 1
      current = next + 1

  data[ch] = i
  if count == k:
    next = min(data)
    result += next - current + 1

print(result)


import re

def is_pangram(sentence):
    lettersUsed = []

    sentence = sentence.lower()
    # Remove non-alphabetic characters
    sentence = re.sub("[^a-z]","",sentence)

    for letter in sentence:
        if (letter not in lettersUsed):
            lettersUsed.append(letter)

    if (len(lettersUsed) == 26):
        return True
    else:
        return False


n = int(input())
aa = []
for i in range(n):
    a = []
    for j in range(i + 1):
        if j == 0 or j == i:
            a.append(1)
        else:
            a.append(aa[i - 1][j - 1] + aa[i - 1][j])
    aa.append(a)
    print(*a)


from flask import Flask, render_template, redirect, url_for, request, session

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

@app.route('/assignment9', methods=["GET", "POST"])
def as9():
    curr_method = request.method
    users = {"ASAF": "BOY", "D": "S", "R": "REEE"}
    user_name = ''
    user_search = ''

    if curr_method == "GET":
        if 'user_search' in request.args:
            user_search = request.args['user_search']
        else:
            user_search = ''

    if curr_method == "POST":
        if 'user_name' in request.form:
            user_name = request.form['user_name']
        else:
            user_name = ''
        if user_name in users:
            session['nickname'] = users[user_name]
        else:
            session['nickname'] = ''

    return render_template("assignment9.html",
                           users=users,
                           user_name=user_name,
                           curr_method=curr_method,
                           user_search=user_search)

@app.route("/logout")
def logout():
    session['nickname'] = ''
    return redirect('/assignment9')

if __name__ == '__main__':
    app.run()


''' create basic config file '''
from sys import argv
from os import path
from configparser import ConfigParser

def __get_args__() -> dict:
    ''' get arguments '''
    token, chat, *admin = argv[1:]
    return {'token': token, 'chat': chat, 'admin': admin}

def __get_path__() -> str:
    '''return path to settings'''
    return path.dirname(path.abspath(__file__)) + '\\settings.ini'

def __create_settings__(token: str, chat: str, admin: str) -> None:
    '''create a config file'''
    token, chat = str(token), str(chat)
    admin = '['+', '.join(map(str, admin))+']' if isinstance(admin, list) else admin
    data_path = __get_path__()
    config = ConfigParser()
    config.add_section('requestsSettings')
    config.set('requestsSettings',
               'token', token)
    config.set('requestsSettings',
               'chat', chat)
    config.set('requestsSettings',
               'admin', admin)
    with open(data_path, 'w') as config_file:
        config.write(config_file)

def read_settings():
    '''read a config file'''
    data_path = __get_path__()
    if not path.isfile(data_path):
        token = input('Telegram bot token needed')
        chat = input('Telegram chatid needed')
        admin = input('Telegram admin list needed')
        __create_settings__(token, chat, admin)
    config = ConfigParser()
    config.read(data_path)
    return config

if __name__ == '__main__':
    __create_settings__(**__get_args__())


"""Trains model and exposes predictor class objects"""

import logging
import operator
import pickle

from hyperopt import fmin, hp, tpe, Trials
from joblib import dump, load
import matplotlib.pyplot as plt
from melo import Melo
import numpy as np
import pandas as pd

from .data import load_games
from . import cachedir

class MeloCFB(Melo):
    """
    Generate college football point-spread or point-total predictions
    using the Margin-dependent Elo (MELO) model.

    """
    def __init__(self, mode, kfactor, home_field, halflife, fatigue):

        # hyperparameters
        self.mode = mode
        self.kfactor = kfactor
        self.home_field = home_field
        self.halflife = halflife
        self.fatigue = fatigue

        # model operation mode: 'spread' or 'total'
        if self.mode not in ['spread', 'total']:
            raise ValueError(
                "Unknown mode; valid options are 'spread' and 'total'")

        # mode-specific training hyperparameters
        self.commutes, self.compare, self.lines = {
            'total': (True, operator.add, np.arange(-0.5, 146.5)),
            'spread': (False, operator.sub, np.arange(-85.5, 86.5)),
        }[mode]

        # connect to college football stats database
        games_ = load_games(refresh=False)
        self.games = self.format_gamedata(games_)

        # instantiate the Melo base class
        super(MeloCFB, self).__init__(
            self.kfactor, lines=self.lines, sigma=1.0,
            regress=self.regress, regress_unit='year',
            commutes=self.commutes)

        # calibrate the model using the game data
        self.fit(
            self.games.date,
            self.games.team_home,
            self.games.team_away,
            self.compare(
                self.games.score_home,
                self.games.score_away,
            ),
            self.games.home_bias
        )

        # compute mean absolute error for calibration
        burnin = 2183
        residuals = self.residuals()
        self.loss = np.abs(residuals[burnin:]).mean()

    def format_gamedata(self, games):
        """
        Generator that yields a tuple of attributes for each game.

        """
        # sort games by date
        games.date = pd.to_datetime(games.date)
        games.sort_values('date', inplace=True)

        # game dates for every team
        game_dates = pd.concat([
            games[['date', 'team_home']].rename(
                columns={'team_home': 'team'}
            ),
            games[['date', 'team_away']].rename(
                columns={'team_away': 'team'}
            ),
        ]).sort_values('date')

        # calculate rest days
        for team in ['home', 'away']:
            games_prev = game_dates.rename(
                columns={'team': 'team_{}'.format(team)}
            )
            games_prev['date_{}_prev'.format(team)] = games.date

            games = pd.merge_asof(
                games, games_prev,
                on='date', by='team_{}'.format(team),
                allow_exact_matches=False
            )

        # add rest days columns
        rested = pd.Timedelta('255 days')
        games['home_rest'] = (games.date - games.date_home_prev).fillna(rested)
        games['away_rest'] = (games.date - games.date_away_prev).fillna(rested)

        # add bias factors
        home_fatigue = self.fatigue * np.exp(-games.home_rest.dt.days / 7.)
        away_fatigue = self.fatigue * np.exp(-games.away_rest.dt.days / 7.)
        relative_fatigue = self.compare(home_fatigue, away_fatigue)
        games['home_bias'] = self.home_field - relative_fatigue

        # drop unwanted columns
        games.drop(columns=['date_home_prev', 'date_away_prev'], inplace=True)

        return games

    def regress(self, years):
        """
        Regresses future ratings to the mean.

        """
        with np.errstate(divide='ignore'):
            return 1 - .5**np.divide(years, self.halflife)

    def bias(self, home_rest_days, away_rest_days):
        """
        Computes circumstantial bias factor given each team's rest.
        Accounts for home field advantage and rest.

        """
        home_fatigue = self.fatigue * np.exp(-home_rest_days / 7.)
        away_fatigue = self.fatigue * np.exp(-away_rest_days / 7.)

        return self.home_field - self.compare(home_fatigue, away_fatigue)

    def probability(self, times, labels1, labels2, bias=None, lines=0):
        """
        Survival function probability distribution

        """
        bias = self.home_field if bias is None else bias
        return super(MeloCFB, self).probability(
            times, labels1, labels2, bias=bias, lines=lines
        )

    def percentile(self, times, labels1, labels2, bias=None, p=50):
        """
        Distribution percentiles

        """
        bias = self.home_field if bias is None else bias
        return super(MeloCFB, self).percentile(
            times, labels1, labels2, bias=bias, p=p
        )

    def quantile(self, times, labels1, labels2, bias=None, q=.5):
        """
        Distribution quantiles

        """
        bias = self.home_field if bias is None else bias
        return super(MeloCFB, self).quantile(
            times, labels1, labels2, bias=bias, q=q
        )

    def mean(self, times, labels1, labels2, bias=None):
        """
        Distribution mean

        """
        bias = self.home_field if bias is None else bias
        return super(MeloCFB, self).mean(
            times, labels1, labels2, bias=bias
        )

    def median(self, times, labels1, labels2, bias=None):
        """
        Distribution median

        """
        bias = self.home_field if bias is None else bias
        return super(MeloCFB, self).median(
            times, labels1, labels2, bias=bias
        )

    def sample(self, times, labels1, labels2, bias=None, size=100):
        """
        Sample the distribution

        """
        bias = self.home_field if bias is None else bias
        return super(MeloCFB, self).sample(
            times, labels1, labels2, bias=bias, size=size
        )

    @classmethod
    def from_cache(cls, mode, steps=200, retrain=False):
        """
        Optimizes the MeloCFB model hyper parameters. Returns cached values
        if retrain is False and the parameters are cached, otherwise it
        optimizes the parameters and saves them to the cache.

        """
        cachefile = cachedir / '{}.pkl'.format(mode)

        if not retrain and cachefile.exists():
            logging.debug('loading {} model from cache', mode)
            model = load(cachefile)
            return model

        def objective(params):
            return cls(mode, *params).loss

        space = (
            hp.uniform('kfactor', 0.0, 1.0),
            hp.uniform('home_field', 0.0, 0.5),
            hp.uniform('halflife', 0.0, 40.0),
            hp.uniform('fatigue', 0.0, 1.0),
        )

        trials = Trials()

        logging.info(f'optimizing {mode} hyperparameters')

        parameters = fmin(objective, space, algo=tpe.suggest,
                          max_evals=steps, trials=trials,
                          show_progressbar=False)

        plotdir = cachedir / 'plots'
        plotdir.mkdir(exist_ok=True)

        fig, axes = plt.subplots(
            ncols=4, figsize=(12, 3), sharey=True)

        losses = trials.losses()

        for ax, (label, vals) in zip(axes.flat, trials.vals.items()):
            c = plt.cm.coolwarm(np.linspace(0, 1, len(vals)))
            ax.scatter(vals, losses, c=c)
            ax.axvline(parameters[label], color='k')
            ax.set_xlabel(label)
            if ax.is_first_col():
                ax.set_ylabel('Mean absolute error')

        plotfile = plotdir / '{}_params.pdf'.format(mode)
        plt.tight_layout()
        plt.savefig(str(plotfile))

        model = cls(mode, **parameters)

        logging.info('writing cache file %s', cachefile)

        cachefile.parent.mkdir(exist_ok=True)
        dump(model, cachefile, protocol=pickle.HIGHEST_PROTOCOL)

        return model


"""Tweet quote images with hashtags in perpetuity (as long as terminal
is open)."""
import numpy as np
import pandas as pd
import threading
from datetime import datetime, timedelta
import helper.tweetwrapper as twrap
import helper.images as images

# ----------------------------------------------------
# Local functions.
# ----------------------------------------------------
def new_id(df,track_id):
    return_flag = False
    while return_flag is False:
        quote_id = np.random.randint(df.index[0], df.index[-1]+1)
        if quote_id not in track_id[-10:]:
            return_flag = True
    return quote_id

def console_message(text):
    print("""{time}: I tweeted:\n{tweet}\n""".format(
        time=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        tweet=text))

def tweet_quote(track_id):
    # read quotes data:
    df = pd.read_csv("bookquotes.csv", comment="#")
    df.loc[:,"title"] = df.title.fillna("unknown")
    df.loc[:,"author"] = df.author.fillna("unknown")

    # choose a quote that fits the conditions present
    # in new_id():
    quote_id = new_id(df,track_id)
    auth = df.loc[quote_id,"author"]
    book = df.loc[quote_id,"title"]
    quote = df.loc[quote_id, "quote"]
    topics = df.loc[quote_id, "topics"]
    poetry_flag = df.loc[quote_id, "poetry"]

    # add quotes around the quote text and book text:
    quote = "'%s'" %quote
    if book!="unknown":
        book="'%s'" %book

    # create tweet hashtags:
    tweet_text = twrap.generate_tweet(auth,book,quote,topics)

    # create tweet image:
    images.imake(
        auth,
        book,
        quote,
        imagec="black",
        textc="white",
        borderx=80,
        text_anchor=(80,-10),
        fontsize=60,
        poetry_flag=poetry_flag)

    # upload tweet image:
    upload_stdout = twrap.upload_media("bin/quote_of_day.png")
    media_id = upload_stdout["media_id"]

    # tweet the quote:
    twrap.tweetme(tweet_text,media_id=media_id)

    # print to console:
    console_message(quote)

    # track the quote id:
    track_id.append(quote_id)

def when_last_tweet(verbose=False):
    stdout = twrap.get_last_tweet()
    datetime_str = stdout[0]["created_at"]
    aware_utc = datetime.strptime(
        datetime_str,
        "%a %b %d %H:%M:%S %z %Y")

    # the Twitter API returns UTC times, but datetime.now()
    # returns local time (Central US for me). I can't figure
    # out how to intelligently assign timezones to the
    # objects, so I'll just strip the Twitter API's timezone
    # info, and manually convert it to CT by subtracting
    # 6 hours.
    naive_utc = aware_utc.replace(tzinfo=None)
    dt = timedelta(hours=6)
    naive_ct = naive_utc - dt

    # print when the last tweet occurred:
    if verbose is True:
        print("The last tweet occurred at: %s" %(
            naive_ct.strftime("%Y-%m-%d %H:%M:%S")))
    return naive_ct

# ----------------------------------------------------
# Execute command.
# ----------------------------------------------------
# define interval:
interval_hours = 1
interval_seconds = interval_hours*60*60

# define hours when the bot can tweet:
hi = 7 #first hour; 7am
he = 22 #last hour; 10pm

# track previous quotes to reduce repeats:
track_id = []

# state when the last tweet occurred:
last_tweet_time = when_last_tweet(verbose=True)

# tweet immediately if the time between now and the last
# tweet is larger than some tolerance:
start_tolerance = timedelta(hours=1)
now=datetime.now()
time_elapsed = now-last_tweet_time
if time_elapsed > start_tolerance:
    tweet_quote(track_id)

# create event and execute once every interval:
ticker = threading.Event()
while ticker.wait(interval_seconds) is False:
    now = datetime.now() #returns datetime in CT; UTC-6hours
    ti = datetime(
        year=now.year,
        month=now.month,
        day=now.day,
        hour=hi)
    te = datetime(
        year=now.year,
        month=now.month,
        day=now.day,
        hour=he)
    if now >= ti and now <= te:
        tweet_quote(track_id)
    else:
        print("no tweet; outside acceptable time.")


# Usubstractbedgraphs v3

import argparse
import sys
import os
import numpy
from array import *

# get arguments from command line
parser = argparse.ArgumentParser(description='Uaveragebedgraphs_args')
parser.add_argument('-o','--output', help='name of output file', required=True, type=str)
parser.add_argument('-f1','--file1', help='file1', required=True, type=str)
parser.add_argument('-f2','--file2', help='file2', required=True, type=str)
parser.add_argument('-d','--delete', help='delete all overlap with file 2 if set to "Y" ', required=False, type=str, default = "N")

args = vars(parser.parse_args())
file1 = args['file1']
file2 = args['file2']
output_file = args['output']
deleteall = args['delete']

try: os.remove(output_file)
except: pass

def substract (file1, file2, newfile):
# make list of chromosomes
    chromosomes = []
    files = [file1, file2]
    for file in files:
        with open(file) as f:
            for line in f:
                line_split = line.split("\t")
                if line_split[0][0:3] == "chr": # account for different formats with and without "chr"
                    line_split[0] = line_split[0][3:]
                if line_split[0] not in chromosomes:
                    chromosomes.append(line_split[0])
    done = "no"
    while done == "no":
        try:
            current_chromosome = chromosomes.pop(0)
        except:
            break

# get length of current chromosome
        stop = 0
        for file in files: # go through all files
            with open(file) as f:
                for line in f:
                    line_split = line.split("\t")
                    if line_split[0][0:3] == "chr": # account for different formats with and without "chr"
                        line_split[0] = line_split[0][3:]
                    if line_split[0] == current_chromosome:
                        if int(line_split[2]) > stop:
                            stop = int(line_split[2])
        if stop > 0:  # continue only if values in chromosome
            output = array('f', [0] * stop)

#substract
            for x, file in enumerate(files):
                with open(file) as f:
                    for line in f:
                        line_split = line.split("\t")
                        line_split[3] = line_split[3].replace("\n", "")
                        if line_split[0][0:3] == "chr":  # account for different formats with and without "chr"
                            line_split[0] = line_split[0][3:]
                        if line_split[0] == current_chromosome:
                            for o in range(int(line_split[2]) - int(line_split[1])):
                                coord = o + int(line_split[1])
                                if x == 0:
                                    output[coord] = float(line_split[3])
                                elif x == 1:
                                    if deleteall == "N":
                                        output[coord] = output[coord] - float(line_split[3])
                                    elif deleteall == "Y":
                                        output[coord] = 0
                                    else:
                                        print("Error: -d has to be N or Y")
                                        sys.exit()

# concatenate entries with same value
            output2 = []
            temp_entry = []
            for x, value in enumerate(output):
                if value != 0:
                    if temp_entry == []: temp_entry = [current_chromosome, x, x + 1, value]
                    elif temp_entry[3] == value: temp_entry[2] += 1
                    elif temp_entry[3] != value:
                        output2.append(temp_entry)
                        temp_entry = [current_chromosome, x, x + 1, value]
                else: temp_entry == []

            if temp_entry != []:
                output2.append(temp_entry)

            with open(newfile, "a") as f:
                for row in output2:
                    f.write(str(row[0]) + "\t" + str(row[1]) + "\t" + str(row[2]) + "\t" + str(row[3]) + "\n")

############################################################################

substract(file1, file2, output_file)
print("Success :)")


class Solution(object):
    def longestConsecutive(self, nums):
        # if not nums: return 0

        # nums = sorted(nums)

        # longest, current = 1, 1
        # last = nums[0]
        # for num in nums:
        #     if num == last + 1:
        #         current += 1
        #     elif num == last:
        #         continue
        #     else:
        #         longest = max(longest, current)
        #         current = 1
        #     last = num

        # return max(longest, current)

        if not nums: return []

        longest, numSet = 1, set(nums)
        for num in numSet:
            if num - 1 not in numSet:
              next = num + 1
              newLongest = 1
              while(next in numSet):
                  newLongest += 1
                  next += 1
              longest = max(longest, newLongest)
        return longest

answer = Solution()
print(answer.longestConsecutive([1, 2, 3]))


import tkinter as tk
from get_order import *

window_main = tk.Tk(className = 'place order')
window_main.geometry('200x100')

button_submit = tk.Button(window_main, text = 'Place Order', command = place_order)
button_submit.config(width = 20, height = 2)

button_submit.pack()
window_main.mainloop()


import requests, sys, os
from utils import get_result, escape

wsdl_file = 'http://nlptools.info.uaic.ro/WebFdgRo/FdgParserRoWS?wsdl'
headers = {'content-type': 'text/xml'}

rqt_parse_pos_tagged_text = """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webFdgRo.uaic/">
   <soapenv:Header/>
   <soapenv:Body>
      <web:parsePosTaggedXML>
         <posTaggedXML>{xml}</posTaggedXML>
      </web:parsePosTaggedXML>
   </soapenv:Body>
</soapenv:Envelope>"""

rqt_parse_text = """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webFdgRo.uaic/">
   <soapenv:Header/>
   <soapenv:Body>
      <web:parseText>
         <txt>{text}</txt>
      </web:parseText>
   </soapenv:Body>
</soapenv:Envelope>"""

def parse_pos_tagget_text(text):
    print("Creating request")
    body = rqt_parse_pos_tagged_text.format(xml=text).encode("utf-8")
    print("Request sent to server")
    response = requests.post(wsdl_file, data=body, headers=headers)
    print("Response received")
    return get_result(response)

def parse_text(text):
    try:
        text = escape(text)
        print("Creating request")
        body = rqt_parse_text.format(text=text).encode("utf-8")
        print("Request sent to server")
        response = requests.post(wsdl_file, data=body, headers=headers)
        print("Response received")
        return get_result(response)
    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        print("Error at calling fdg_parser. Reason:", str(e), "File:",fname , "Line:", exc_tb.tb_lineno)


### Early column mapper script ###

import pandas as pd
import pickle
import keras
from sklearn.preprocessing import LabelEncoder
from keras.preprocessing import sequence

threshold = 0.75

CSV = r'C:\Users\mail\Downloads\data\bisgaard NOOS data.csv'
df = pd.read_csv(CSV, error_bad_lines=False, engine='c', encoding='ISO-8859-1', low_memory=False)
df = df.astype(str)
df = df.sample(1000, random_state=7)
df = df.reset_index(drop=True)

model = keras.models.load_model(r'C:\Users\mail\PycharmProjects\MLDM\Miscellaneous Code\Models\organized\6_class_MVP.h5')
tokenizer = pickle.load(open(r'C:\Users\mail\PycharmProjects\MLDM\Miscellaneous Code\Models\organized\6_class_MVP.pkl', 'rb'))
le = LabelEncoder()
print('Model, Tokenizer and LabelEncoder loaded.')

def predictClass(text, tok, model):
    text_pad = sequence.pad_sequences(tok.texts_to_sequences([text]), maxlen=300)
    score = le.inverse_transform(model.predict_classes([text_pad]))
    return score[0]

col_dict = {df.columns.get_loc(c): c for idx, c in enumerate(df.columns)}
manual_map = {0: 'Amount', 1: 'Unknown', 2: 'Name', 3: 'EAN', 4: 'EAN',
              5: 'Amount', 6: 'Price', 7: 'Price', 8: 'Price', 9: 'Price'}

map_list = []
correct = 0

lists = list(map(list, col_dict.items()))

for n in (range(len(col_dict))):
    df_select = df[col_dict[n]]
    df_select = df_select.astype(str)
    class_map = []
    for m in (range(len(df_select))):
        class_predictions = []
        targets = ['Name', 'Description', 'Category', 'Price', 'Amount', 'EAN']
        targets = le.fit_transform(targets)
        text = df_select[m]
        score = predictClass(text, tokenizer, model)
        lists[n].append(score)
        print('data:', text, ', prediction:', score)
    x = pd.Series(lists[n])
    y = pd.Series(x.value_counts())
    print('_____________________________')
    if y.iloc[0] > (len(df_select)) * threshold:
        print(y.index[0], 'Is the Majority Predicted Class.')
        for i in range(len(y) - 2):
            print('Majority Predicted classes:', y.index[i], ', predictions:', y.iloc[i], '/', len(df_select))
            class_predictions.append('Pred: {class_pred}: {num_pred} / {class_len}. Original: {origin}'
                                     .format(class_pred=y.index[i], num_pred=y.iloc[i], class_len=len(df_select),
                                             origin=col_dict[n], actual=manual_map[n]))
            if y.index[i] == manual_map[n]:
                correct += 1

    else:
        print('There is no Majority Predicted Class above the threshold.')
        for i in range(len(y) - 2):
            print('Predicted classes:', y.index[i], ', predictions:', y.iloc[i], '/', len(df_select))
            class_predictions.append(
                'Pred {num}: {class_pred}: {num_pred} / {class_len}. Original: {origin}, Actual: {actual}'
                .format(num=(i + 1), class_pred=y.index[i], num_pred=y.iloc[i], class_len=len(df_select),
                        origin=col_dict[n], actual=manual_map[n]))
    print('Original Class:', col_dict[n])
    print('__________________________________________')
    map_list.append(class_predictions)

print('Correct Maps: {correct}/{total}'.format(correct = correct, total = len(col_dict)))


from rest_framework.decorators import api_view, permission_classes
from rest_framework.response import Response
from rest_framework import status
from django.shortcuts import get_object_or_404
from rest_framework.permissions import IsAuthenticated
from SoftDesk.models import Projects, Issues, Comments, Contributors
from SoftDesk.serializers import (ProjectsSerializer, ContributorsSerializer,
                                    IssuesSerializer, CommentsSerializer)
from SoftDesk.permissions import (PermissionsViewContributor, PermissionsProjectAuthor,
                                    PermissionsCommentAuthor,
                                    PermissionsContributorAuthorProjet,
                                    PermissionsIssueAuthor)

@api_view(['GET', 'POST'])
@permission_classes([PermissionsViewContributor, IsAuthenticated])
def project_list(request):
    """
    List all projects in which the utilisateur is an author or was affected as a contributor, or create a new projet.
    Permissions: Only authenticated users can access.
    """
    if request.method == 'GET':
        projects = Projects.objects.filter(author_user_id=request.user.id) | Projects.objects.filter(contributor__user_id=request.user.id)
        serializer = ProjectsSerializer(projects, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

    elif request.method == 'POST':
        data = request.data.copy()
        data['author_user_id'] = request.user.id

        serializer = ProjectsSerializer(data=data)
        if serializer.is_valid():
            project = serializer.save()
            Contributors.objects.create(user_id=request.user, project_id=project, role='L\'AUTEUR')
            return Response(serializer.data, status=status.HTTP_201_CREATED)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@api_view(['GET', 'PUT', 'DELETE'])
@permission_classes([PermissionsProjectAuthor, IsAuthenticated])
def project_detail(request, project_id):
    """
    Retrieve, update or delete issues.
    Permissions: Only authenticated users can access.
                 Only the author of the project can delete or modify the project
    """
    project = get_object_or_404(Projects, id=project_id)

    if request.method == 'GET':
        serializer = ProjectsSerializer(project)
        return Response(serializer.data, status=status.HTTP_200_OK)

    elif request.method == 'PUT':
        data = request.data.copy()
        data['author_user_id'] = project.author_user_id.id

        serializer = ProjectsSerializer(project, data=data)
        if serializer.is_valid(raise_exception=True):
            serializer.save()
            return Response(serializer.data, status=status.HTTP_202_ACCEPTED)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        project.delete()
        return Response('Project deleted successfully!', status=status.HTTP_204_NO_CONTENT)

@api_view(['GET', 'POST'])
@permission_classes([IsAuthenticated])
def contributor_list(request, project_id):
    """
    List all contributors, or create a new contributor.
    Permissions: Only authenticated users can access.
                 Only the project author can attach new contributors to the project
    """
    project = get_object_or_404(Projects, id=project_id)

    if request.method == 'GET':
        contributors = Contributors.objects.filter(project_id=project)
        serializer = ContributorsSerializer(contributors, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

    elif request.method == 'POST':
        #seul l'auteur du projet peut ajouter des contributeurs
        if request.user.id == project.author_user_id.id:
            data = request.data.copy()
            data['project_id'] = project.id

            serializer = ContributorsSerializer(data=data)
            if serializer.is_valid(raise_exception=True):
                serializer.save()
                return Response(serializer.data, status=status.HTTP_201_CREATED)
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        else:
            return Response('You do not have permission to add contributors to the project. Only the author of the project has that right.')

@api_view(['DELETE'])
@permission_classes([PermissionsContributorAuthorProjet, IsAuthenticated])
def contributor_detail(request, contributor_id, project_id):
    """
    Delete contributors
    Permissions: Only authenticated users can access.
                 Only the project author can remove project contributors
    """
    contributor = get_object_or_404(Contributors, id=contributor_id)
    project = get_object_or_404(Projects, id=project_id)

    if request.method == 'DELETE':
        contributor.delete()
        return Response('Contributor deleted successfully!', status=status.HTTP_204_NO_CONTENT)

@api_view(['GET', 'POST'])
@permission_classes([IsAuthenticated])
def issue_list(request, project_id):
    """
    List all issues, or create a new issue.
    Permissions: Only authenticated users can access.
    """
    project = get_object_or_404(Projects, id=project_id)

    if request.method == 'GET':
        issues = Issues.objects.filter(project_id=project_id)
        serializer = IssuesSerializer(issues, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

    elif request.method == 'POST':
        data = request.data.copy()
        data['project_id'] = project_id
        data['author_user_id'] = request.user.id

        serializer = IssuesSerializer(data=data)
        if serializer.is_valid(raise_exception=True):
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@api_view(['PUT', 'DELETE'])
@permission_classes([PermissionsIssueAuthor, IsAuthenticated])
def issue_detail(request, project_id, issue_id):
    """
    Retrieve, update or delete issues.
    Permissions: Only authenticated users can access.
                 Only the author of the problem can modify or delete it.
    """
    project = get_object_or_404(Projects, id=project_id)
    issue = get_object_or_404(Issues, id=issue_id)

    if request.method == 'PUT':
        data = request.data.copy()
        data['project_id'] = project.id
        data['author_user_id'] = issue.author_user_id.id

        serializer = IssuesSerializer(issue, data=data)
        if serializer.is_valid(raise_exception=True):
            serializer.save()
            return Response(serializer.data, status=status.HTTP_202_ACCEPTED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        issue.delete()
        return Response('Issue successfully deleted.', status=status.HTTP_204_NO_CONTENT)

@api_view(['GET', 'POST'])
@permission_classes([IsAuthenticated])
def comments_list(request, project_id, issue_id):
    """
    List all comments, or create a new comment.
    Permissions: Only authenticated users can access.
    """
    get_object_or_404(Projects, id=project_id)
    issue = get_object_or_404(Issues, id=issue_id)

    if request.method == 'GET':
        comment = Comments.objects.filter(issues_id=issue)
        serializer = CommentsSerializer(comment, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

    elif request.method == 'POST':
        data = request.data.copy()
        data['issue'] = issue.id
        data['author'] = request.user.id

        serializer = CommentsSerializer(data=data)
        if serializer.is_valid(raise_exception=True):
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@api_view(['GET', 'PUT', 'DELETE'])
@permission_classes([PermissionsCommentAuthor, IsAuthenticated])
def comment_detail(request, project_id, issue_id, comment_id):
    """
    Retrieve, update or delete comments.
    Permissions: Only authenticated users can access.
                 Only the author of the comment can modify or delete it.
    """
    get_object_or_404(Projects, id=project_id)
    issue = get_object_or_404(Issues, id=issue_id)
    comment = get_object_or_404(Comments, id=comment_id)

    if request.method == 'GET':
        serializer = CommentsSerializer(comment)
        return Response(serializer.data, status=status.HTTP_200_OK)

    elif request.method == 'PUT':
        data = request.data.copy()
        data['issue'] = issue.id
        data['author'] = comment.author_user_id.id

        serializer = CommentsSerializer(comment, data=data)
        if serializer.is_valid(raise_exception=True):
            serializer.save()
            return Response(serializer.data, status=status.HTTP_202_ACCEPTED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        comment.delete()
        return Response('Comment successfully deleted.', status=status.HTTP_204_NO_CONTENT)


from zero_pad import zero_pad
from conv_single_step import conv_single_step
import numpy as np

def conv_forward(A_prev, W, b, hparameters):
    """
    Implements the forward propagation for a convolution function

    Arguments:
    A_prev -- output activations of the previous layer, numpy array of shape (m, n_H_prev, n_W_prev, n_C_prev)
    W -- Weights, numpy array of shape (f, f, n_C_prev, n_C)
    b -- Biases, numpy array of shape (1, 1, 1, n_C)
    hparameters -- python dictionary containing "stride" and "pad"

    Returns:
    Z -- conv output, numpy array of shape (m, n_H, n_W, n_C)
    cache -- cache of values needed for the conv_backward() function
    """

    # Retrieve dimensions from A_prev's shape
    (m, n_H_prev, n_W_prev, n_C_prev) = (A_prev.shape[0], A_prev.shape[1], A_prev.shape[2], A_prev.shape[3])

    # Retrieve dimensions from W's shape
    (f, f, n_C_prev, n_C) = (W.shape[0], W.shape[1], W.shape[2], W.shape[3])

    # Retrieve information from "hparameters"
    stride = hparameters["stride"]
    pad = hparameters["pad"]

    # Compute the dimensions of the CONV output volume using the formula given above. Hint: use int() to floor.
    n_H = int((n_H_prev + (2 * pad) - f) / stride + 1)
    n_W = int((n_W_prev + (2 * pad) - f) / stride + 1)
    #     print(n_H)
    #     print(n_W)

    # Initialize the output volume Z with zeros.
    Z = np.zeros((m, n_H, n_W, n_C))

    # Create A_prev_pad by padding A_prev
    A_prev_pad = zero_pad(A_prev, pad)
    l = 1
    for i in range(0, m):  # loop over the batch of training examples
        a_prev_pad = A_prev_pad[i, :, :, :]  # Select ith training example's padded activation
        for h in range(0, n_H):  # loop over vertical axis of the output volume
            for w in range(0, n_W):  # loop over horizontal axis of the output volume
                for c in range(0, n_C):  # loop over channels (= #filters) of the output volume

                    # Find the corners of the current "slice"
                    vert_start = h * stride
                    vert_end = f + (h * stride)
                    horiz_start = w * stride
                    horiz_end = f + (w * stride)

                    # Use the corners to define the (3D) slice of a_prev_pad (See Hint above the cell).
                    a_slice_prev = a_prev_pad[vert_start:vert_end, horiz_start:horiz_end, :]

                    # Convolve the (3D) slice with the correct filter W and bias b, to get back one output neuron.
                    Z[i, h, w, c] = conv_single_step(a_slice_prev, W[:, :, :, c], b[:, :, :, c])

    # Making sure your output shape is correct
    assert (Z.shape == (m, n_H, n_W, n_C))

    # Save information in "cache" for the backprop
    cache = (A_prev, W, b, hparameters)

    return Z, cache


# coding: utf-8
"""
a proof of concept implementation of SQLite FTS tokenizers in Python
"""
import sys

from cffi import FFI  # type: ignore

SQLITE_OK = 0
SQLITE_DONE = 101

ffi = FFI()

if sys.platform == "win32":
    import sqlite3  # noqa

    dll = ffi.dlopen("sqlite3.dll")
else:
    from ctypes.util import find_library

    try:
        # try to use _sqlite3.so first
        import _sqlite3  # noqa

        dll = ffi.dlopen(_sqlite3.__file__)
    except:
        dll = ffi.dlopen(find_library("sqlite3"))

if hasattr(sys, "getobjects"):
    # for a python built with Py_TRACE_REFS
    ffi.cdef(
        """
typedef struct sqlite3 sqlite3;
typedef struct {
  void *_ob_next;
  void *_ob_prev;
  size_t ob_refcnt;
  void *ob_type;
  sqlite3 *db;
} PyObject;
"""
    )
else:
    ffi.cdef(
        """
typedef struct sqlite3 sqlite3;
typedef struct {
  size_t ob_refcnt;
  void *ob_type;
  sqlite3 *db;
} PyObject;
"""
    )

def get_db_from_connection(c):
    db = getattr(c, "_db", None)
    if db:
        # pypy's SQLite3 connection has _db using cffi
        db = ffi.cast("sqlite3*", db)
    else:
        db = ffi.cast("PyObject *", id(c)).db
    return db

__all__ = ["get_db_from_connection", "SQLITE_OK", "SQLITE_DONE"]


'''
  Copyright (C) 2015 Eric Zhang, China

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as
  published by the Free Software Foundation, either version 3 of
  the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.

  Created on 2015-9-21
  @author: eric.zhang
  @contact: zgzczzw@163.com

'''

from const import const

#dict_max = ["123456", "12345678", "123456789", "111111", "123123", "11111111", "5201314", "000000"]
#tail = ['520', '123', '888', '123456', 'abc','!','@','#','$','%','?',';']
#head = ['hello', 'hi', 'love', '!', '#', 'aa', 'wow', 'zz','@','!','#','$','%','?',';']
#year = ['1937', '1949', '1976', '1997', '2000', '2002', '2006', '2007', '2008', '2010', '2011', '2012', '2013', '2014', '2015', '2020']
#lett = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

dict_max = []
tail = ['!', '@', '#', '$', '%', '?', ';']
head = []
year = []
lett = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

ul = []
uu = []
ll = []
e = []
yyyymmdd = []
yymmdd = []
yy = []
mmdd = []
yyyy = []

birthday = []
name = []
phoneNumer = []
qq = []
email = []
extinfo = []

minLen = const.MIN_LEN_PW
maxLen = const.MAX_LEN_PW


import os, sys
from setuptools import setup
try:
    README = open(os.path.join(os.path.dirname(__file__), 'README.rst')).read()
except:
    README = ''

# allow setup.py to be run from any path
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))

requirements = [
    'Django>=1.11',
    'googlemaps>=2.5.0',
    'unidecode'
]

try:
    import django
    if django.VERSION >= (3,1,0):
        pass
    else:
        requirements.append('jsonfield')
except ImportError:
    if sys.version_info.major >= (3,8,0):
        requirements[0] = 'Django>=3.1'
    else:
        requirements.append('jsonfield')

setup(
    name='django-google-maps-services',
    version='0.1.0',
    packages=['dj_gmap'],
    include_package_data=True,
    license='BSD License',
    description='Google maps caching functions for django. + helpers',
    long_description=README,
    author='Gerasev Kirill',
    author_email='gerasev.kirill@gmail.com',
    install_requires=requirements,
    classifiers=[
        'Environment :: Web Environment',
        'Framework :: Django',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: BSD License',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Topic :: Internet :: WWW/HTTP',
        'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
    ],
)