from numpy import size
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision.models import resnet50
from .network_utils import *
from .xception import xception
import pdb
class Segception_small(nn.Module):
    def __init__(self, num_classes, input_shape=(None, None, 3), in_channels=3, weights='imagenet', **kwargs):
        super(Segception_small, self).__init__(**kwargs)
        # load pretrained Xception

        self.base_output = {}

# bn2(147)
# block1.rep[4](74)
# 2(37)
# 12(19)
# bn4(10)
        self.base_model = xception(num_classes=1000, pretrained='imagenet')
        self.base_model.bn2.register_forward_hook(get_features(self.base_output, 'bn2'))
        self.base_model.block1.rep[4].register_forward_hook(get_features(self.base_output,'block1_sepconv2_bn'))
        self.base_model.block2.rep[4].register_forward_hook(get_features(self.base_output,'block2_sepconv2_bn'))
        self.base_model.block12.rep[4].register_forward_hook(get_features(self.base_output,'block12_sepconv2_bn'))
        self.base_model.bn4.register_forward_hook(get_features(self.base_output,'bn4'))

        # TODO get the each layer output here

        # Decoder
        self.adap_encoder_1 = EncoderAdaption(in_channels=2048, filters=128, kernel_size=3, dilation_rate=1)
        self.adap_encoder_2 = EncoderAdaption(in_channels=1024, filters=128, kernel_size=3, dilation_rate=1)
        self.adap_encoder_3 = EncoderAdaption(in_channels=256, filters=128, kernel_size=3, dilation_rate=1)
        self.adap_encoder_4 = EncoderAdaption(in_channels=128, filters=64, kernel_size=3, dilation_rate=1)
        self.adap_encoder_5 = EncoderAdaption(in_channels=64, filters=32, kernel_size=3, dilation_rate=1)

        self.decoder_conv_1 = FeatureGeneration(in_channels=128, filters=128, kernel_size=3, dilation_rate=1, blocks=3)
        self.decoder_conv_2 = FeatureGeneration(in_channels=128, filters=64, kernel_size=3, dilation_rate=1, blocks=3)
        self.decoder_conv_3 = FeatureGeneration(in_channels=64, filters=32, kernel_size=3, dilation_rate=1, blocks=3)
        self.decoder_conv_4 = FeatureGeneration(in_channels=32, filters=32, kernel_size=3, dilation_rate=1, blocks=1)
        self.aspp = ASPP_2(in_channels=32, filters=32, kernel_size=3)

        self.conv_logits = conv(in_channels=32, out_channels=num_classes, kernel_size=1, strides=1, use_bias=True)

    def forward(self, inputs, training=None, mask=None, aux_loss=False):

        # outputs = self.model_output(inputs)
        # emb1=self.model_output(inputs)
        self.base_model(inputs)
        outputs = [self.base_output['bn4'], #2048*10*10
                   self.base_output['block12_sepconv2_bn'], #1024*19*19
                   self.base_output['block2_sepconv2_bn'], #256*74*74
                   self.base_output['block1_sepconv2_bn'], #128*147*147
                   self.base_output['bn2']] #64*147*147
        # add activations to the ourputs of the model
        for i in range(len(outputs)):
            outputs[i] = nn.LeakyReLU(negative_slope=0.3)(outputs[i])

        x = self.adap_encoder_1(outputs[0])
        x = upsampling(x, scale=2)
        # TODO: why here the author's comment is 512??
        x += reshape_into(self.adap_encoder_2(outputs[1]), x)  # 512
        x = self.decoder_conv_1(x)  # 256

        x = upsampling(x, scale=2)
        x += reshape_into(self.adap_encoder_3(outputs[2]), x)  # 256
        x = self.decoder_conv_2(x)  # 256

        x = upsampling(x, scale=2)
        x += reshape_into(self.adap_encoder_4(outputs[3]), x)  # 128
        x = self.decoder_conv_3(x)  # 128

        x = self.aspp(x, operation='sum')  # 128

        x = upsampling(x, scale=2)
        x += reshape_into(self.adap_encoder_5(outputs[4]), x)  # 64
        x = self.decoder_conv_4(x)  # 64
        x = self.conv_logits(x)
        x = upsampling(x, scale=2)

        if aux_loss:
            return x, x
        else:
            # TODO: ?why
            return x

class RefineNet(nn.Module):
    def __init__(self, num_classes, base_model, **kwargs):
        super(RefineNet, self).__init__(**kwargs)
        # mirar si cabe en memoria RefineNet
        # usar esta pero con restore self.model_output.variables y el forward es con rgb y segmentacion
        # poner que justo esas a no optimizar, solo las nuevas
        self.base_model = base_model

        # Decoder
        self.conv = FeatureGeneration(in_channels=3, filters=32, kernel_size=3, dilation_rate=1, blocks=1)
        self.conv_logits = conv(filters=num_classes, kernel_size=1, strides=1, use_bias=True)

    def forward(self, inputs, training=None, mask=None, iterations=1):
        # get non refined segmentation
        segmentation = self.base_model(inputs)

        for i in range(iterations):
            x = torch.cat([inputs, segmentation], -1)
            x = self.conv(x)
            segmentation = self.conv_logits(x)

        return segmentation

class Conv_BN(nn.Module):
    def __init__(self, in_channels, filters, kernel_size, strides=1) -> None:
        super(Conv_BN, self).__init__()

        self.filters = filters
        self.kernel_size = kernel_size
        self.strides = strides

        self.conv = conv(in_channels=in_channels, out_channels=filters, kernel_size=kernel_size, strides=strides)
        # TODO: what is num_features
        self.bn = nn.BatchNorm2d(num_features=filters, eps=1e-3, momentum=0.993)

    def forward(self, inputs, training=None, activation=True):
        x = self.conv(inputs) # (batch_size, 128, 147, 147)
        x = self.bn(x) # (batch_sizze, 128, 147, 147)
        if activation:
            x = nn.LeakyReLU(negative_slope=0.3)(x)
        return x

class DepthwiseConv_BN(nn.Module):
    def __init__(self, in_channels, filters, kernel_size, strides=1, dilation_rate=1):
        super(DepthwiseConv_BN, self).__init__()

        self.filters = filters
        self.kernel_size = kernel_size
        self.strides = strides

        self.conv = SeparableConv2d(in_channels=in_channels, out_channels=filters, kernel_size=kernel_size)
        self.bn = nn.BatchNorm2d(num_features=filters, eps=1e-3, momentum=0.993)

    def forward(self, inputs, training=None):
        x = self.conv(inputs)
        x = self.bn(x)
        x = nn.LeakyReLU(negative_slope=0.3)(x)

        return x

class Transpose_Conv_BN(nn.Module):
    def __init__(self, in_channels, filters, kernel_size, strides=1):
        super(Transpose_Conv_BN, self).__init__()

        self.filters = filters
        self.kernel_size = kernel_size
        self.strides = strides

        self.conv = transposeConv(filters=filters, kernel_size=kernel_size, strides=strides)
        self.bn = nn.BatchNorm2d(num_features=filters, eps=1e-3, momentum=0.993)

    def forward(self, inputs, training=None):
        x = self.conv(inputs)
        x = self.bn(x)
        x = nn.LeakyReLU(negative_slope=0.3)(x)

        return x

class ShatheBlock(nn.Module):
    def __init__(self, in_channels, filters, kernel_size,  dilation_rate=1, bottleneck=2) -> None:
        super(ShatheBlock, self).__init__()

        self.filters = filters * bottleneck
        self.kernel_size = kernel_size

        self.conv = DepthwiseConv_BN(in_channels, self.filters, kernel_size=kernel_size, dilation_rate=dilation_rate)
        self.conv1 = DepthwiseConv_BN(self.filters, self.filters, kernel_size=kernel_size, dilation_rate=dilation_rate)
        self.conv2 = DepthwiseConv_BN(self.filters, self.filters, kernel_size=kernel_size, dilation_rate=dilation_rate)
        # TODO: why use filters not self.filters?
        self.conv3 = Conv_BN(self.filters, filters, kernel_size=1)

    def forward(self, inputs, training=None):
        x = self.conv(inputs)
        x = self.conv1(x)
        x = self.conv2(x)
        x = self.conv3(x)
        return x + inputs

class ASPP(nn.Module):
    def __init__(self, in_channels, filters, kernel_size):
        super(ASPP, self).__init__()

        self.conv1 = DepthwiseConv_BN(in_channels, filters, kernel_size=1, dilation_rate=1)
        self.conv2 = DepthwiseConv_BN(in_channels, filters, kernel_size=kernel_size, dilation_rate=4)
        self.conv3 = DepthwiseConv_BN(in_channels, filters, kernel_size=kernel_size, dilation_rate=8)
        self.conv4 = DepthwiseConv_BN(in_channels, filters, kernel_size=kernel_size, dilation_rate=16)
        self.conv5 = Conv_BN(in_channels, filters, kernel_size=1)

    def forward(self, inputs, training=None, operation='concat'):
        # TODO: check if the inputs are tensor?
        feature_map_size = inputs.size()
        image_features = torch.mean(inputs, [1, 2], keepdim=True)
        image_features = self.conv1(image_features)
        image_features = F.interpolate(image_features, size=(feature_map_size[1], feature_map_size[2]), mode='bilinear')
        x1 = self.conv2(inputs)
        x2 = self.conv3(inputs)
        x3 = self.conv4(inputs)
        if 'concat' in operation:
            x = self.conv5(torch.cat((image_features, x1, x2, x3, inputs), axis=3))
        else:
            x = image_features + x1 + x2 + x3 + inputs

        return x

class ASPP_2(nn.Module):
    def __init__(self, in_channels, filters, kernel_size):
        super(ASPP_2, self).__init__()

        self.conv1 = DepthwiseConv_BN(in_channels, filters, kernel_size=1, dilation_rate=1)
        self.conv2 = DepthwiseConv_BN(in_channels, filters, kernel_size=kernel_size, dilation_rate=4)
        self.conv3 = DepthwiseConv_BN(in_channels, filters, kernel_size=kernel_size, dilation_rate=8)
        self.conv4 = DepthwiseConv_BN(in_channels, filters, kernel_size=kernel_size, dilation_rate=16)
        self.conv6 = DepthwiseConv_BN(in_channels, filters, kernel_size=kernel_size, dilation_rate=(2, 8))
        self.conv7 = DepthwiseConv_BN(in_channels, filters, kernel_size=kernel_size, dilation_rate=(6, 3))
        self.conv8 = DepthwiseConv_BN(in_channels, filters, kernel_size=kernel_size, dilation_rate=(8, 2))
        self.conv9 = DepthwiseConv_BN(in_channels, filters, kernel_size=kernel_size, dilation_rate=(3, 6))
        self.conv5 = Conv_BN(in_channels, filters, kernel_size=1)

    def forward(self, inputs, training=None, operation='concat'):
        feature_map_size = inputs.size()
        # TODO: compare to tf version, which dim to choose?
        image_features = torch.mean(inputs, dim=(2, 3), keepdim=True)
        image_features = self.conv1(image_features)
        image_features = F.interpolate(image_features, size=(feature_map_size[2], feature_map_size[3]), mode='bilinear')
        x1 = self.conv2(inputs)
        x2 = self.conv3(inputs)
        x3 = self.conv4(inputs)
        x4 = self.conv6(inputs)
        x5 = self.conv7(inputs)
        x4 = self.conv8(inputs) + x4
        x5 = self.conv9(inputs) + x5
        if 'concat' in operation:
            x = self.conv5(torch.cat((image_features, x1, x2, x3,x4,x5, inputs), axis=3))
        else:
            x = self.conv5(image_features + x1 + x2 + x3+x5+x4) + inputs

        return x

class EncoderAdaption(nn.Module):
    def __init__(self, in_channels, filters, kernel_size, dilation_rate=1) -> None:
        super(EncoderAdaption, self).__init__()
        self.filters = filters
        self.kernel_size = kernel_size

        self.conv1 = Conv_BN(in_channels, filters, kernel_size=1)
        self.conv2 = ShatheBlock(filters, filters, kernel_size=kernel_size, dilation_rate=dilation_rate)

    def forward(self, inputs, training=None):
        x = self.conv1(inputs)
        x = self.conv2(x)
        return x

class FeatureGeneration(nn.Module):
    def __init__(self, in_channels, filters, kernel_size, dilation_rate=1, blocks=3):
        super(FeatureGeneration, self).__init__()

        self.filters = filters
        self.kernel_size = kernel_size

        self.conv0 = Conv_BN(in_channels, self.filters, kernel_size=1)
        self.blocks = []
        for n in range(blocks):
            self.blocks = self.blocks + [
                ShatheBlock(self.filters, self.filters, kernel_size=kernel_size, dilation_rate=dilation_rate)]

    def forward(self, inputs, training=None):

        x = self.conv0(inputs)
        for block in self.blocks:
            x = block(x)

        return x

class SeparableConv2d(nn.Module):

    def __init__(self, in_channels, out_channels, kernel_size, bias=False):
        super(SeparableConv2d, self).__init__()
        self.depthwise = nn.Conv2d(in_channels, in_channels, kernel_size=kernel_size,
                                groups=in_channels, bias=bias, padding=1)
        self.pointwise = nn.Conv2d(in_channels, out_channels,
                                kernel_size=1, bias=bias)

    def forward(self, x):
        out = self.depthwise(x)
        out = self.pointwise(out)
        return out


import os
import sys
import os.path as p

file_header = "LIBRARY", "EXPORTS"

factory_lines = {
    "x64": "\t{0}=?{0}@@YAPEAUIWidget@@AEAV?$basic_istream@_WU?$char_traits@_W@std@@@std@@@Z",
    "Win32": "\t{0}=?{0}@@YAPAUIWidget@@AAV?$basic_istream@_WU?$char_traits@_W@std@@@std@@@Z",
}
info_lines = {
    "x64": "\tinfo=?info@@YAPEBDXZ",
    "Win32": "\tinfo=?info@@YAPBDXZ",
}

def main():
    directory = p.dirname(sys.argv[0])
    project_name = sys.argv[1]
    factory_line = factory_lines[sys.argv[2]]
    info_line = info_lines[sys.argv[2]]

    use_info = False
    factories = []
    with open(project_name + ".hpp") as f:
        for line in f:
            if line.startswith("IWidget* factory"):
                factories.append(line[9:line.index('(')])
            elif line.startswith("const char* info()"):
                use_info = True

    with open(project_name + sys.argv[2] + ".def", 'w') as f:
        print(*file_header, sep='\n', file=f)
        if use_info:
            print(info_line, file=f)

        for factory in factories:
            print(factory_line.format(factory), file=f)

if __name__ == "__main__":
    main()


import requests
from backend import AssemblyAI_APIKEY, getSession_FieldValue, updateSession_FieldValue
import time
from env import Upload_Dir
from mutagen.wave import WAVE

def read_file(filename, chunk_size=5242880):
    print("Reading Started ...")
    with open(filename, 'rb') as _file:
        while True:
            try:
                chunk = _file.read(chunk_size)
                if not chunk:
                    break
                yield chunk
            except Exception as e:
                print(e)
    print("Reading Stopped ...")

def text_with_speakers(filename, chk_hyperlink, chk_summary, api_id=AssemblyAI_APIKEY):
    headers = {'authorization': api_id}
    response = requests.post('https://api.assemblyai.com/v2/upload',
                             headers=headers,
                             data=read_file(filename))

    url = response.json()['upload_url']

    endpoint = "https://api.assemblyai.com/v2/transcript"
    json = {
        "audio_url": url,
        "speaker_labels": True,
        "auto_chapters": chk_summary,
        "auto_highlights": chk_hyperlink,
    }
    headers = {
        "authorization": api_id,
        "content-type": "application/json"
    }
    response = requests.post(endpoint, json=json, headers=headers)
    # print(response)
    return response.json()["id"]

def getResponse(id, api_id=AssemblyAI_APIKEY):
    endpoint = "https://api.assemblyai.com/v2/transcript/" + id
    headers = {"authorization": api_id}
    response = requests.get(endpoint, headers=headers, timeout=10)
    return response.json()

def wait_for_completion(id):
    while True:
        response = getResponse(id)
        if response['status'] == 'completed':
            return response

        time.sleep(5)

def runAssemblyAPI(Session_ID, chk_hyperlink, chk_summary):
    File_Name = getSession_FieldValue(Session_ID, "File_Name")
    File_Path = Upload_Dir + File_Name
    id = text_with_speakers(File_Path, chk_hyperlink, chk_summary)
    print(id)

    # Audio File length in seconds
    audio_length = WAVE(File_Path).info.length
    time.sleep(audio_length/5)
    wait_for_completion(id)

    response = getResponse(id)
    words = response['words']
    summary = response['chapters']
    keywords = response['auto_highlights_result']
    confidence = response['confidence']
    # print(response)
    updateSession_FieldValue(Session_ID, "Session_Status_Index", 2)
    last_speaker, Text = 'Speaker ' + words[0]['speaker'], ''
    Speaker_Rec = ''
    Total_Text = ''
    for word in words:
        if 'Speaker ' + word['speaker'] == last_speaker:
            Text += word['text'] + ' '
        else:
            Speaker_Rec += last_speaker + " : " + Text + '\n'
            Total_Text += Text + ' '
            Text = word['text'] + ' '
            last_speaker = 'Speaker ' + word['speaker']
    Speaker_Rec += last_speaker + " : " + Text + '\n'
    Total_Text += Text + ' '

    Summary = ''
    if chk_summary:
        updateSession_FieldValue(Session_ID, "Session_Status_Index", 7)
        Summary = ' '.join([i["summary"] for i in summary])

    Keywords = []
    # print(keywords)
    if chk_hyperlink:
        updateSession_FieldValue(Session_ID, "Session_Status_Index", 4)
        Keywords = [i["text"] for i in keywords["results"]]

    # print(Speaker_Rec, Summary, Keywords, confidence)
    return Total_Text, Speaker_Rec, Summary, Keywords, confidence*100


def helper(first,second,debug):
    if (debug == 0):
        print("Here")
    for i in range(20):
        if second & (1<<i):
            first ^= (1<<i)
    return first
for t in range(int(input())):
    n = int(input())
    ans = []
    first = 0
    count = 1
    hashset = set()
    hashset.add(0)
    k = False
    for i in range(n):
        while helper(first,count,1) in hashset:
            count+=1
        k = True
        first = helper(first,count,1)
        ans.append(count)
        hashset.add(first)
        count+=1
    if (k):
        print(*ans, sep = " ")


from unicodedata import normalize as unicode_normalize

from ._phonetic import _Phonetic

__all__ = ['PSHPSoundexFirst']

class PSHPSoundexFirst(_Phonetic):
    """PSHP Soundex/Viewex Coding of a first name.

    This coding is based on :cite:`Hershberg:1976`.

    Reference was also made to the German version of the same:
    :cite:`Hershberg:1979`.

    A separate class, :py:class:`PSHPSoundexLast` is used for last names.

    .. versionadded:: 0.3.6
    """

    _trans = dict(
        zip(
            (ord(_) for _ in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
            '01230120022455012523010202',
        )
    )

    _alphabetic = dict(zip((ord(_) for _ in '12345'), 'PKTLN'))

    def __init__(self, max_length: int = 4, german: bool = False) -> None:
        """Initialize PSHPSoundexFirst instance.

        Parameters
        ----------
        max_length : int
            The length of the code returned (defaults to 4)
        german : bool
            Set to True if the name is German (different rules apply)

        .. versionadded:: 0.4.0

        """
        self._max_length = max_length
        self._german = german

    def encode_alpha(self, fname: str) -> str:
        """Calculate the alphabetic PSHP Soundex/Viewex Coding of a first name.

        Parameters
        ----------
        fname : str
            The first name to encode

        Returns
        -------
        str
            The alphabetic PSHP Soundex/Viewex Coding

        Examples
        --------
        >>> pe = PSHPSoundexFirst()
        >>> pe.encode_alpha('Smith')
        'SNT'
        >>> pe.encode_alpha('Waters')
        'WTNK'
        >>> pe.encode_alpha('James')
        'JN'
        >>> pe.encode_alpha('Schmidt')
        'SN'
        >>> pe.encode_alpha('Ashcroft')
        'AKK'
        >>> pe.encode_alpha('John')
        'JN'
        >>> pe.encode_alpha('Colin')
        'KL'
        >>> pe.encode_alpha('Niall')
        'NL'
        >>> pe.encode_alpha('Sally')
        'SL'
        >>> pe.encode_alpha('Jane')
        'JN'

        .. versionadded:: 0.4.0

        """
        code = self.encode(fname).rstrip('0')
        if code == 'J7':
            return 'JN'
        elif code == 'P7':
            return 'PT'
        return code[:1] + code[1:].translate(self._alphabetic)

    def encode(self, fname: str) -> str:
        """Calculate the PSHP Soundex/Viewex Coding of a first name.

        Parameters
        ----------
        fname : str
            The first name to encode

        Returns
        -------
        str
            The PSHP Soundex/Viewex Coding

        Examples
        --------
        >>> pe = PSHPSoundexFirst()
        >>> pe.encode('Smith')
        'S530'
        >>> pe.encode('Waters')
        'W352'
        >>> pe.encode('James')
        'J700'
        >>> pe.encode('Schmidt')
        'S500'
        >>> pe.encode('Ashcroft')
        'A220'
        >>> pe.encode('John')
        'J500'
        >>> pe.encode('Colin')
        'K400'
        >>> pe.encode('Niall')
        'N400'
        >>> pe.encode('Sally')
        'S400'
        >>> pe.encode('Jane')
        'J500'

        .. versionadded:: 0.3.0
        .. versionchanged:: 0.3.6
            Encapsulated in class

        """
        fname = unicode_normalize('NFKD', fname.upper())
        fname = ''.join(c for c in fname if c in self._uc_set)

        # special rules
        if fname == 'JAMES':
            code = 'J7'
        elif fname == 'PAT':
            code = 'P7'

        else:
            # A. Prefix treatment
            if fname[:2] in {'GE', 'GI', 'GY'}:
                fname = 'J' + fname[1:]
            elif fname[:2] in {'CE', 'CI', 'CY'}:
                fname = 'S' + fname[1:]
            elif fname[:3] == 'CHR':
                fname = 'K' + fname[1:]
            elif fname[:1] == 'C' and fname[:2] != 'CH':
                fname = 'K' + fname[1:]

            if fname[:2] == 'KN':
                fname = 'N' + fname[1:]
            elif fname[:2] == 'PH':
                fname = 'F' + fname[1:]
            elif fname[:3] in {'WIE', 'WEI'}:
                fname = 'V' + fname[1:]

            if self._german and fname[:1] in {'W', 'M', 'Y', 'Z'}:
                fname = {'W': 'V', 'M': 'N', 'Y': 'J', 'Z': 'S'}[
                    fname[0]
                ] + fname[1:]

            code = fname[:1]

            # B. Soundex coding
            # code for Y unspecified, but presumably is 0
            fname = fname.translate(self._trans)
            fname = self._delete_consecutive_repeats(fname)

            code += fname[1:]
            syl_ptr = code.find('0')
            syl2_ptr = code[syl_ptr + 1 :].find('0')
            if syl_ptr != -1 and syl2_ptr != -1 and syl2_ptr - syl_ptr > -1:
                code = code[: syl_ptr + 2]

            code = code.replace('0', '')  # rule 1

        if self._max_length != -1:
            if len(code) < self._max_length:
                code += '0' * (self._max_length - len(code))
            else:
                code = code[: self._max_length]

        return code

if __name__ == '__main__':
    import doctest

    doctest.testmod()


from dataclasses import dataclass
from typing import ClassVar

@dataclass
class InfoMessage:
    """Информационное сообщение о тренировке."""

    training_type: str
    duration: float
    distance: float
    speed: float
    calories: float

    def get_message(self):
        return (
            f"Тип тренировки: {self.training_type};"
            f" Длительность: {self.duration:.3f} ч.;"
            f" Дистанция: {self.distance:.3f} км;"
            f" Ср. скорость: {self.speed:.3f} км/ч;"
            f" Потрачено ккал: {self.calories:.3f}."
        )

@dataclass
class Training:
    """Базовый класс тренировки."""

    action: int
    duration: float
    weight: float
    LEN_STEP: ClassVar[float] = 0.65
    M_IN_KM: ClassVar[float] = 1000.00
    MIN_IN_HOUR: ClassVar[float] = 60.00

    def get_distance(self) -> float:
        """Получить дистанцию в км."""
        return self.action * self.LEN_STEP / self.M_IN_KM

    def get_mean_speed(self) -> float:
        """Получить среднюю скорость движения."""
        return self.get_distance() / self.duration

    def get_spent_calories(self) -> float:
        """Получить количество затраченных калорий."""
        raise NotImplementedError(
            "Определите метод get_spent_calories"
            " в %s." % (self.__class__.__name__)
        )

    def show_training_info(self) -> InfoMessage:
        """Вернуть информационное сообщение о выполненной тренировке."""
        return InfoMessage(
            type(self).__name__,
            self.duration,
            self.get_distance(),
            self.get_mean_speed(),
            self.get_spent_calories(),
        )

@dataclass
class Running(Training):
    """Тренировка: бег."""

    RUN_COEFF_CALORIE_1: ClassVar[float] = 18
    RUN_COEFF_CALORIE_2: ClassVar[float] = 20

    def get_spent_calories(self) -> float:
        return (
            (
                self.RUN_COEFF_CALORIE_1 * self.get_mean_speed()
                - self.RUN_COEFF_CALORIE_2
            )
            * self.weight
            / self.M_IN_KM
            * self.duration
            * self.MIN_IN_HOUR
        )

@dataclass
class SportsWalking(Training):
    """Тренировка: спортивная ходьба."""

    height: float
    WLK_COEFF_CALORIE_1: ClassVar[float] = 0.035
    WLK_COEFF_CALORIE_2: ClassVar[float] = 0.029

    def get_spent_calories(self) -> float:
        return (
            self.WLK_COEFF_CALORIE_1 * self.weight
            + (self.get_mean_speed() ** 2 // self.height)
            * self.WLK_COEFF_CALORIE_2
            * self.weight
        ) * (self.duration * self.MIN_IN_HOUR)

@dataclass
class Swimming(Training):
    """Тренировка: плавание."""

    weight: float
    length_pool: float
    count_pool: float
    SWM_COEFF_CALORIE_1: ClassVar[float] = 1.1
    SWM_COEFF_CALORIE_2: ClassVar[float] = 2.0
    LEN_STEP: ClassVar[float] = 1.38

    def get_mean_speed(self) -> float:
        return (
            self.length_pool * self.count_pool / self.M_IN_KM / self.duration
        )

    def get_spent_calories(self) -> float:
        return (
            (self.get_mean_speed() + self.SWM_COEFF_CALORIE_1)
            * self.SWM_COEFF_CALORIE_2
            * self.weight
        )

def read_package(workout_type: str, data: list) -> Training:
    """Прочитать данные полученные от датчиков."""
    dict_workout_type = {"SWM": Swimming, "RUN": Running, "WLK": SportsWalking}
    kls = dict_workout_type.get(workout_type)
    if kls is None:
        raise NameError(
            f"Функция read_package - Тип тренировки {workout_type}:"
            "отсутствует в справочнике dict_workout_type"
        )
    return kls(*data)

def main(training: Training) -> None:
    """Главная функция."""
    info = training.show_training_info()
    print(info.get_message())

if __name__ == "__main__":
    packages = [
        ("SWM", [720, 1, 80, 25, 40]),
        ("RUN", [15000, 1, 75]),
        ("WLK", [9000, 1, 75, 180]),
    ]

    for workout_type, data in packages:
        main(read_package(workout_type, data))


from freezegun import freeze_time
from datetime import datetime, timedelta
from unittest.mock import ANY

from django.test import TestCase
from rest_framework.test import RequestsClient

from ...models import Referral, Client
from ..utils import create_user, generate_valid_cpf

class TestAcceptReferralView(TestCase):
    """
    Testing the methods on the 'accept-referral/' endpoint.
    """

    def setUp(self):
        """
        Initializing the RequestsClient for all tests, as well as creating
        an user and a referral.
        """

        self.creation_time = datetime.now().astimezone().isoformat()
        # this is the Datetime format used by Django
        self.target_cpf = generate_valid_cpf()
        with freeze_time(self.creation_time):
            create_user()
            Referral.objects.create(
                source_cpf="11987098390",
                target_cpf=self.target_cpf,
                status=False
            )

        self.client = RequestsClient()

    def test_should_return_200_and_specific_referral(self):
        """
        Testing if the GET method on endpoint.
        """

        URL = f'http://127.0.0.1:8000/accept-referral/{self.target_cpf}/'
        response = self.client.get(URL)
        json_response = response.json()

        expected_json_response = {
            'id': 1, 'source_cpf': '11987098390',
            'target_cpf': self.target_cpf, 'created_at': self.creation_time,
            'updated_at': self.creation_time, 'status': False}

        self.assertEqual(response.status_code, 200)
        self.assertEqual(json_response, expected_json_response)

    def test_should_return_404_for_cpf_without_referral(self):
        """
        Testing if GET method on 'accept-referral/<str:cpf>/' endpoint
        returns a 404 response if the user with the cpf on url path
        isn't connected to a valid referral on database.
        """

        unreferred_cpf = generate_valid_cpf()
        URL = f'http://127.0.0.1:8000/accept-referral/{unreferred_cpf}/'
        response = self.client.get(URL)
        json_response = response.json()

        expected_json_response = {
            'error': 'No active referral registered for this CPF'}

        self.assertEqual(response.status_code, 404)
        self.assertEqual(json_response, expected_json_response)

    def test_should_post_referral_with_201(self):
        """
        Testing if the PUT method on endpoint successfully updates the
        referral in order to accept it (status = True) and that the user
        recieves points for the referral approval.
        """

        URL = f'http://127.0.0.1:8000/accept-referral/{self.target_cpf}/'
        update_time = datetime.now().astimezone().isoformat()
        body = {
            'source_cpf': '11987098390',
            'target_cpf': self.target_cpf,
            'status': True
        }

        with freeze_time(update_time):
            response = self.client.put(URL, body)
            json_response = response.json()

        expected_json_response = {
            "Updated referral:": {
                "id": 1,
                "source_cpf": "11987098390",
                "target_cpf": self.target_cpf,
                "created_at": self.creation_time,
                "updated_at": update_time,
                "status": True
            }
        }

        client = Client.objects.get(cpf=11987098390)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(Referral.objects.count(), 1)
        self.assertEqual(json_response, expected_json_response)
        self.assertEqual(client.points, 10)

    def test_should_return_400_if_updating_To_invalid_cpf(self):
        """
        Testing if the PUT method on endpoint returns a 400 response if
        user is trying to update the referent cpf with an invalid number.
        """

        URL = f'http://127.0.0.1:8000/accept-referral/{self.target_cpf}/'
        update_time = datetime.now().astimezone().isoformat()
        body = {
            'source_cpf': '11987098390',
            'target_cpf': '11111111111',
            'status': True
        }

        response = self.client.put(URL, body)
        json_response = response.json()

        expected_json_response = {"target_cpf": ["Invalid CPF number."]}

        client = Client.objects.get(cpf=11987098390)

        self.assertEqual(response.status_code, 400)
        self.assertEqual(Referral.objects.count(), 1)
        self.assertEqual(json_response, expected_json_response)
        self.assertEqual(client.points, 0)

    def test_should_return_400_if_changing_referent_cpf(self):
        """
        Testing if the PUT method on endpoint returns a 400 response if
        user is trying to update the referent cpf.
        """

        URL = f'http://127.0.0.1:8000/accept-referral/{self.target_cpf}/'
        update_time = datetime.now().astimezone().isoformat()
        body = {
            'source_cpf': '11987098390',
            'target_cpf': generate_valid_cpf(),
            'status': True
        }

        response = self.client.put(URL, body)
        json_response = response.json()

        expected_json_response = {'error': 'cannot change users CPF'}

        client = Client.objects.get(cpf=11987098390)

        self.assertEqual(response.status_code, 400)
        self.assertEqual(Referral.objects.count(), 1)
        self.assertEqual(json_response, expected_json_response)
        self.assertEqual(client.points, 0)

    def test_should_not_return_expired_referrals(self):
        """
        Testing if expired referrals are actually deleted from the database.
        """

        target_cpf = generate_valid_cpf()
        expired_date = datetime.now() - timedelta(days=30)
        expired_date_formatted = expired_date.astimezone().isoformat()

        with freeze_time(expired_date_formatted):
            Referral.objects.create(
                source_cpf="11987098390",
                target_cpf=target_cpf,
                status=False
            )

        self.assertEqual(Referral.objects.count(), 2)
        # before calling a method that deletes expired referrals

        URL = f'http://127.0.0.1:8000/accept-referral/{target_cpf}/'
        response = self.client.get(URL)
        json_response = response.json()

        expected_json_response = {
            'error': 'No active referral registered for this CPF'}

        self.assertEqual(response.status_code, 404)
        self.assertEqual(json_response, expected_json_response)

        self.assertEqual(Referral.objects.count(), 1)
        # after calling a method that deletes expired referrals


from django.db.models import Q
from django.shortcuts import render, get_object_or_404, redirect

from carts.models import CartItem
from orders.models import OrderProduct
from .forms import ReviewForm
from .models import Product, ReviewRating, ProductGallery
from category.models import Category
from carts.views import _cart_id
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.contrib import messages

# Create your views here.

def store(request, category_slug=None):  # category_slug=None is used to make the category_slug optional
    categories = None  # categories is used to store the category_slug
    products = None  # products is used to store the products

    if category_slug != None:  # if category_slug is not None, then the category_slug is stored in categories, iT MEANS THAT IF THE CATEGORY IS SELECTED THEN THE PRODUCTS OF THAT CATEGORY WILL BE DISPLAYED OR FILTERED
        categories = get_object_or_404(Category,
                                       slug=category_slug)  # get_object_or_404() is used to get the object of the Category model with the slug=category_slug
        products = Product.objects.filter(category=categories,
                                          is_available=True)  # products is used to store the products of the category_slug
        paginator = Paginator(products, 6)  # 6 is the number of products per page
        page = request.GET.get('page')  # get the page number
        paged_products = paginator.get_page(page)  # get the products for the page number
        products_count = products.count()
    else:  # if category_slug is None, then all the products are stored in products
        products = Product.objects.all().filter(is_available=True).order_by("id") # Order_by(id) because we remove warning in the console "UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: <class 'store.models.Product'> QuerySet."
        paginator = Paginator(products, 6)  # 6 is the number of products per page
        page = request.GET.get('page')  # get the page number
        paged_products = paginator.get_page(page)  # get the products for the page number
        products_count = products.count()

    context = {
        'products': paged_products,  # products is passed as a context
        'products_count': products_count,
    }
    return render(request, 'store\store.html', context)

def product_detail(request, category_slug, product_slug):  # category_slug and product_slug are passed as parameters
    try:
        single_product = Product.objects.get(category__slug=category_slug,
                                             slug=product_slug)  # get the product with the category_slug and product_slug and category__slug, double underscore use because of foreign key and get slug from other model
        in_cart = CartItem.objects.filter(cart__cart_id=_cart_id(
            request),
            product=single_product).exists()  # check if the product is in the cart or not and return True or False

    except Exception as e:
        raise e
    if request.user.is_authenticated: # check if the user is authenticated or not
        try: # check the orderproduct table if the user already owns the product or not, then it allows the user to submit the review
            orderproduct = OrderProduct.objects.filter(user=request.user, product_id=single_product.id).exists()
        except OrderProduct.DoesNotExist:
            orderproduct = None
    else: # if the user is not authenticated then orderproduct will be None
        orderproduct = None
    # Get the reviews
    reviews = ReviewRating.objects.filter(product_id=single_product.id, status=True)

    # Get the product gallery
    product_gallery = ProductGallery.objects.filter(product_id=single_product.id)

    context = {
        'single_product': single_product,
        'in_cart': in_cart,
        'orderproduct': orderproduct,
        'reviews': reviews,
        'product_gallery': product_gallery,
    }
    return render(request, 'store\product_detail.html', context)

def search(request):
    if 'keyword' in request.GET:
        keyword = request.GET['keyword']
        if keyword:
            products = Product.objects.order_by(
                '-created_date').filter(Q(description__icontains=keyword) | Q(product_name__icontains=keyword))
            products_count = products.count()
    context = {
        'products': products,
        'products_count': products_count,
    }
    return render(request, 'store\store.html',context)

def submit_review(request,product_id):
    url = request.META.get('HTTP_REFERER') # get the last url
    if request.method == 'POST':
        try:
            reviews = ReviewRating.objects.get(user__id=request.user.id, product__id=product_id) # get the review of the user and product
            form = ReviewForm(request.POST, instance=reviews) # instance=reviews is used to update the review, if the review is already submitted
            form.save()
            messages.success(request, 'Thank you! Your review has been updated.')
            return redirect(url)

        except ReviewRating.DoesNotExist: # if the review is not submitted then create a new review
            form = ReviewForm(request.POST) # create a form instance
            if form.is_valid(): # check if the form is valid or not
                data = ReviewRating() # create an object
                data.subject = form.cleaned_data['subject']
                data.rating = form.cleaned_data['rating']
                data.review = form.cleaned_data['review']
                data.ip = request.META.get('REMOTE_ADDR') # get the ip address
                data.product_id = product_id
                data.user_id = request.user.id
                data.save() # save the data
                messages.success(request, 'Thank you! Your review has been submitted.')
                return redirect(url)


#!/usr/bin/env python
'''
WebSpy module

Loads HTML and compares with HTML local dump
'''

import sys
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter, Namespace

import requests
from bs4 import BeautifulSoup

FREE_SEARCH_FILTER = {"title": "Available in GitLab SaaS Free"}
NONFREE_SEARCH_FILTER = {"title": "Not available in SaaS Free"}

URL = 'https://about.gitlab.com/features/'
BS_PARSER = 'html.parser'

def get_html(url: str) -> str:
    '''Loads html from WEB'''
    response = requests.get(url)
    return response.text

def parse_html(url: str) -> tuple:
    '''Parse HTML and get free and non free elements count'''
    row_html = get_html(url)
    soup = BeautifulSoup(row_html, BS_PARSER)
    free_count = len(soup.find_all("a", FREE_SEARCH_FILTER))
    non_free_count = len(soup.find_all("a", NONFREE_SEARCH_FILTER))

    return free_count, non_free_count

def gitlab_parser_callback(args: Namespace):
    '''Parse GitLab website'''
    free_count, non_free_count = parse_html(args.url)
    print(f"free products: {free_count}", file=sys.stdout)
    print(f"enterprise products: {non_free_count}", file=sys.stdout)

def setup_parser():
    '''Setup parser'''
    parser = ArgumentParser(
        prog='WebSpy',
        description='GitLab web spy',
        formatter_class=ArgumentDefaultsHelpFormatter,
        add_help=True,
        argument_default='-h',
    )
    subparsers = parser.add_subparsers(
        title='commands',
        dest='command',
        required=True,
        description='WebSpy',
    )
    subparsers.required = True
    gitlab_parser = subparsers.add_parser(
        'gitlab',
        help='Get free features list from GitLab website',
        description='WebSpy subparser for GitLab',
    )
    gitlab_parser.add_argument(
        '-u',
        '--url',
        help='Target URL',
        required=False,
        default=URL,
    )
    gitlab_parser.set_defaults(callback=gitlab_parser_callback)

    return parser

def main():
    '''Main function for calling as script'''
    parser = setup_parser()
    args = parser.parse_args()
    args.callback(args)

if __name__ == '__main__':
    main()


import numpy as np
from typing import List

def trap(height: List[int]) -> int:
    if len(height) == 0:
        return 0
    # First get the max height
    max_ht=max(height); area = 0
    potential = np.zeros(max_ht); max_h_so_far = 0
    prev_ht = 0
    ## Now start collecting rain.
    for h in height:
        for j in range(h, max_h_so_far):
            potential[j]+=1

        for j in range(prev_ht,h):
            area += potential[j]
            potential[j] = 0

        if h > max_h_so_far:
            max_h_so_far = h

        prev_ht = h
    return int(area)

def trap_w_stack(height: List[int])-> int:
    st = []; area = 0; prev_ht=0
    accu_up = 0
    for i in range(len(height)):
        h = height[i]
        if h<prev_ht:
            h_del = (prev_ht-h)
            st.append([h_del,i-1])
            print(st)
            accu_up = 0
        elif h>prev_ht:
            h_del = (h-prev_ht); accu_up=0
            slack=0
            while len(st)>0 and accu_up<h_del:
                up_move = min(st[len(st)-1][0],h_del-accu_up)
                area += (i-st[len(st)-1][1]-1)*up_move
                print((i-st[len(st)-1][1]-1)*up_move)
                accu_up += up_move
                if slack+st[len(st)-1][0]<=accu_up:
                    slack+=st[len(st)-1][0]
                    st.pop()
                else:
                    st[len(st)-1][0]-=up_move
        prev_ht = h
    return area

def trap_w_stack_leetcode(height: List[int])-> int:
    ans = 0; current = 0
    st=[]
    while (current < len(height)):
        while (len(st)>0 and height[current] > height[st[len(st)-1]]):
            top = st[len(st)-1]
            st.pop()
            if len(st)==0:
                break
            distance = current - st[len(st)-1] - 1
            bounded_height = min(height[current], height[st[len(st)-1]]) \
                            - height[top]
            print(str(current)+","+str(st[len(st)-1])+","+str(top))
            ans += distance * bounded_height
        st.append(current)
        current+=1
    return ans

def trap_w_dp_leetcode(height: List[int])-> int:
    left = 0; right = len(height) - 1
    ans = 0
    left_max = 0; right_max = 0
    while (left < right):
        if (height[left] < height[right]):
            if height[left] >= left_max:
                left_max = height[left]
            else:
                ans += (left_max - height[left])
            left+=1
        else:
            if height[right] >= right_max:
                right_max = height[right]
            else:
                ans += (right_max - height[right])
            right-=1
    return ans

h = [4,2,0,3,2,5]
trap_w_dp_leetcode(h)

# h = [3,1,1,0,1,3]
# trap_w_stack(h)

# h = [4,2,0,3,2,5]
# trap(h)

# h = [0,1,0,2,1,0,1,3,2,1,2,1]
# trap_w_stack(h)

h = [0,1,0,4,1,0,1,3,2,1,2,1]
trap_w_dp_leetcode(h)


import pygame
from utils.index import *
from constantes.index import *
from components.hero.index import *
from components.bomb.index import *
from components.life.index import *
from components.enemy.index import *
from components.background.index import *

class Game:
  def __init__(self, screen, callback):
    self.screen = screen
    self.callback = callback
    self.life = Life(self.screen, self.onDie)
    self.hero = Hero(self.screen, WINDOW_WIDTH / 2, WINDOW_HEIGHT - 10, self.life.hurts)
    self.bomb = False
    self.heroHeight = self.hero.getHeight()
    x, y = self.getHeroPosition()
    self.keepBomb = Bomb(self.screen, x, y - self.heroHeight, self.bombMove)
    self.enemy = Enemy(self.screen)
    self.background = Background(self.screen)
    self.hero.setEnemy(self.enemy)

  def bombMove(self, x, y):
    x, y
    # print(x, y)

  def onDie(self):
    self.callback(SCENE_END)

  def draw(self, events):
    self.background.draw(events, self.hero)
    if self.bomb:
      if self.bomb.isDestroy(): self.bomb = False
      else: self.bomb.draw(events)
    self.hero.draw(events)
    self.enemy.draw(events)
    self.life.draw()

  def getHeroPosition(self):
    return self.hero.getPosition()

  def fire(self):
    if self.bomb == False:
      self.bomb = self.keepBomb
      self.bomb.setEnemy(self.enemy)
      x, y = self.getHeroPosition()
      self.bomb.setPosition(x, y - self.heroHeight)
      self.bomb.alive()


import matplotlib.pyplot as plt

# recall_RPN = [0.986, 0.986, 0.983, 0.979, 0.970, 0.954, 0.933]
# recall_NRPN = [0.999, 0.998, 0.997, 0.996, 0.988, 0.983, 0.971]
# x = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]
recall_RPN = [0.970, 0.965, 0.958, 0.948, 0.933, 0.911, 0.874]
recall_NRPN = [0.993, 0.990, 0.986, 0.978, 0.975, 0.963, 0.936]
x = [0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8]

data = ['RPN', 'N-RPN']
plt.rcParams['font.sans-serif'] = ['SimHei']  # 中文显示
# plt.rcParams['savefig.dpi'] = 300  # 图片像素
# plt.rcParams['figure.dpi'] = 300  # 分辨率
# plt.style.use('bmh')  # ggplot
# plt.figure(figsize=(10, 5))
# plt.title('不同backbone下FPS')
plt.xlabel("IoU")
plt.ylabel("Recall")
plt.plot(x, recall_RPN, '-o', linestyle='-', markersize=5, color='blue', label=data[0])
plt.plot(x, recall_NRPN, '-o', linestyle='-', markersize=5, color='red', label=data[1])

# plt.xlim(0, 21)
plt.ylim(0.86, 1)
plt.legend(loc=1)
plt.grid(True, linestyle='--')
plt.savefig('plot_recall_RPN.png')
plt.show()


import os

class RedShiftTextureProcessor(object):
    """A wrapper for the ``redshiftTextureProcessor.exe``

    TextureProcessor <inputfile> [options]

    Options are:
            -l              Force linear gamma (recommended for floating point textures)
            -s              Force SRGB gamma (recommended for integer textures)
            (Note the default gamma operation is as follows: -l for floating point textures and -s for integer textures)
            -p              Photometric IES data (for IES profile types)
            -wx             Used as a tiled texture with wrapping/repeats
            -wy             Used as a tiled texture with wrapping/repeats
            -isphere        Image Based Light - Sphere projection
            -ihemisphere    Image Based Light - Hemisphere projection
            -imirrorball    Image Based Light - Mirrorball projection
            -iangularmap    Image Based Light - Angular Map projection
            -ocolor         Sprite Cut-Out Map opacity from color intensity
            -oalpha         Sprite Cut-Out Map opacity from alpha
            -noskip         Disable the skipping of already converted textures if the processor thinks no data has changed
            -r              Recursively process textures in sub directories
            -log            Enable logging to log file
    """

    executable = os.path.join(
        os.environ.get("REDSHIFT_COREDATAPATH", ""), "bin/redshiftTextureProcessor"
    )

    def __init__(
        self,
        input_file_full_path,
        l=None,
        s=None,
        p=None,
        wx=None,
        wy=None,
        isphere=None,
        ihemisphere=None,
        imirrorball=None,
        iangularmap=None,
        ocolor=None,
        oalpha=None,
        noskip=False,
        r=None,
        log=None,
    ):

        self.input_file_full_path = os.path.normpath(
            os.path.expandvars(input_file_full_path)
        ).replace("\\", "/")
        self.files_to_process = []
        self.expand_tiles()
        self.noskip = noskip

    def expand_tiles(self):
        """expands any tiles and returns a list of file paths"""
        if "<" in self.input_file_full_path:
            # replace any <U> and <V> with an *
            self.input_file_full_path = (
                self.input_file_full_path.replace("<U>", "*")
                .replace("<V>", "*")
                .replace("<UDIM>", "*")
            )

        import glob

        self.files_to_process = glob.glob(self.input_file_full_path)

    def convert(self):
        """converts the given input_file to an rstexbin"""
        processed_files = []

        import subprocess
        from anima.utils.progress import ProgressManagerFactory

        pdm = ProgressManagerFactory.get_progress_manager()
        caller = pdm.register(len(self.files_to_process), title="Converting Textures")
        for file_path in self.files_to_process:
            command = '%s "%s"' % (self.executable, file_path)
            rsmap_full_path = "%s.rstexbin" % os.path.splitext(file_path)[0]

            # os.system(command)
            if os.name == "nt":
                proc = subprocess.Popen(
                    command, creationflags=subprocess.SW_HIDE, shell=True
                )
            else:
                proc = subprocess.Popen(command, shell=True)
            proc.wait()

            processed_files.append(rsmap_full_path)
            caller.step()
        caller.end_progress()

        return processed_files


import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
from tqdm import tqdm
import random

#读取数据, 这里先不考虑节点的特征X，单纯从邻接矩阵进行无监督学习，得到游走序列后用w2v进行训练，最后用Kmeans评估分类结果
A = np.fromfile(r'D:\coding\dataset\cora数据集\预处理\A.bin', dtype=np.int32).reshape(2708,2708)

#接下来用随机游走生成2*len(A)条序列供w2v训练，每一条序列限制长度为20
iter_num = 2 #每个节点迭代10次
seq_len = 20

#先定义一个类, 该类接收邻接矩阵A的一条边 w = A[i]，并由w产生跳转节点， LeetCode 528原题
class jump(object):
    def __init__(self, w):
        """
        :type w: List[int]
        """
        self.w = w
    def binary_search(self, s, r):
        #在前缀和数组s中查找满足s[i]<r<=s[i+1]的i
        left, right = 0, len(s)-1
        while(left<right):
            mid = (left+right)//2
            if(s[mid]>=r):
                #说明可能位置在左边或者就是该位置
                right = mid
            else:
                #说明可能位置在右边
                left = mid+1
        return right-1
    def pickIndex(self):
        """
        :rtype: int

        计算权值的前缀和s, s[i]表示前i个数的和, s[0]=0
        生成一个位于1~s[-1]之间的随机数r
        找到一个位置i, 满足s[i-1]<r<=s[i], 这里用二分查找
        于是此时选择的数即为第i个数

        复杂度分析：时间O(n), 空间O(n)
        """
        s = [0]*(len(self.w)+1)
        for i in range(len(self.w)): s[i+1] = s[i]+self.w[i]
        n = len(self.w)
        r = random.randint(1,s[-1])
        return self.binary_search(s, r)

#产生训练数据s
def gernerate_train_data(A, iter_num, seq_len):
    '''
    params:
        A:邻接矩阵
        iter_num:迭代次数
        seq_len:游走序列的长度

    return:
        句子列表 [sentence1, sentence2,...] shape:[len(A)*iter_num, seq_len]
    '''
    all_sentence = []
    for epoch in range(iter_num):
        for i in tqdm(range(len(A))):
            sentence = [] #存放当前游走的序列
            index = i #当前游走的节点下标
            for k in range(seq_len):
                sentence.append(str(index))
                J = jump(A[index])
                index = J.pickIndex() #下一个跳转的节点
            all_sentence.append(sentence)
    return all_sentence

all_sentence = gernerate_train_data(A, iter_num, seq_len) #生成训练数据

from gensim.models import Word2Vec
w2v = Word2Vec(
    sentences = all_sentence,
    size = 32,
    min_count = 0,
    workers = -1
)

#创建归一化向量字典
w2v_dict = {}
for i in range(len(A)):
    w2v_dict[i] = w2v.wv.__getitem__(str(i))/np.linalg.norm(w2v.wv.__getitem__(str(i)), ord=2)

#供Kmeans训练的数据
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters = 7)

X = np.array([
    w2v_dict[i] for i in range(len(A))
])

kmeans.fit(X)

#接下来评估DeepWalk+Kmeans的性能, 用nx作图并打印, 和原图进行比较
#为每一个节点指定颜色, 定义颜色编号
id2color = {
    0:'r',
    1:'g',
    2:'b',
    3:'m',
    4:'orange',
    5:'k',
    6:'c'
}

#需要得到图的边信息
edges = []
for i in range(len(A)):
    for j in range(len(A[i])):
        if A[i][j] != 0:
            edges.append([i,j])

all_nodes = np.array(list(range(len(A))))
#首先绘制原图
Y = np.fromfile(r'D:\coding\dataset\cora数据集\预处理\Y.bin', dtype=np.int32)
colormap1 = [id2color[i] for i in Y]
G1 = nx.Graph()
G1.add_nodes_from(all_nodes)
G1.add_edges_from(np.array(edges))

#绘制无监督学习的图
colormap2 = [id2color[i] for i in kmeans.labels_]
G2 = nx.Graph()
G2.add_nodes_from(all_nodes)
G2.add_edges_from(edges)

#画图
plt.subplot(1,2,1)
nx.draw(G1, with_labels=False, edge_color='y', node_color=colormap1, node_size=10)
plt.subplot(1,2,2)
nx.draw(G2, with_labels=False, edge_color='y', node_color=colormap2, node_size=10)
plt.show()


import sys

N=int(input())
card=list(map(int,sys.stdin.readline().split()))
M=int(input())
many=list(map(int,sys.stdin.readline().split()))

count={}
for c in card:
    if not c in count:
        count[c]=1
    else:
        count[c]+=1

for m in many:
    if m in count:
        print(count[m],end=' ')
    else:
        print(0,end=' ')


import re

from utility import cn2dig, get_year, request_site_page, get_content_text
from bs4 import BeautifulSoup as bs
from oss_utils import oss_add_file, init_ali_oss

ali_bucket = init_ali_oss()

def fujian_circ(db, logger):
    for each_circ_data in db.circ_data.find({'origin': '福建保监局', 'status': {'$nin': ['ignored']}}):
        announcement_url = each_circ_data['url']
        announcement_title = each_circ_data['title']

        if db.circ_data.find(
                {'url': announcement_url, 'status': 'parsed'}).count() == 1 and db.parsed_data.find(
            {'origin_url': announcement_url, 'oss_file_origin_url': announcement_url,
             'parsed': True}).count() == 1:
            continue

        logger.info('福建保监局 ' + 'Url to parse: %s' % announcement_url)

        r = request_site_page(announcement_url)
        if r is None:
            logger.error('网页请求错误 %s' % announcement_url)
            continue
        content_soup = bs(r.content, 'lxml') if r else bs('', 'lxml')

        if db.parsed_data.find(
                {'origin_url': announcement_url, 'oss_file_origin_url': announcement_url}).count() == 0:
            oss_file_map = {
                'origin_url': announcement_url,
                'oss_file_origin_url': announcement_url,
                'origin_url_id': each_circ_data['_id'],
                'oss_file_type': 'html',
                'oss_file_name': announcement_title,
                'oss_file_content': r.text.encode(r.encoding).decode('utf-8'),
                'parsed': False
            }
            insert_response = db.parsed_data.insert_one(oss_file_map)
            file_id = insert_response.inserted_id
            oss_add_file(ali_bucket, str(file_id) + '/' + announcement_title + '.html',
                         r.text.encode(r.encoding).decode('utf-8'))
            db.circ_data.update_one({'_id': each_circ_data['_id']}, {'$set': {'status': 'parsed'}})
        else:
            db.circ_data.update_one({'_id': each_circ_data['_id']}, {'$set': {'status': 'parsed'}})
            file_id = db.parsed_data.find_one({'origin_url': announcement_url,
                                               'oss_file_origin_url': announcement_url})['_id']

        table_content = content_soup.find(id='tab_content')
        if not table_content:
            logger.error('网页请求错误 %s' % announcement_url)
            continue
        content_text = get_content_text(table_content.find_all('tr')[3])
        if content_text == '':
            continue
        title = table_content.find_all('tr')[0].text.strip()

        document_code_compiler = re.compile(r'((闽保监罚|闽银保监罚决字).\d{4}.\d+号)')
        if document_code_compiler.search(content_text):
            document_code = document_code_compiler.search(content_text).group(1).strip()
            litigant_compiler = re.compile(
                document_code.replace(r'[', r'\[').replace(r']', r'\]') +
                r'\n([\s\S]*?)' + r'(\n经查|\n经检查|'
                                  r'\n?依据.*?的有关规定|\n抽查|\n经抽查|'
                                  r'\n.*?现场检查|\n.*?进行调查|\n.*?调查|'
                                  r'\n.*?信访核查|\n.*?行政许可审核中发现|'
                                  r'\n?.*?我局根据信访件提供的线索|'
                                  r'\n?2008年3月21日)')
            litigant = litigant_compiler.search(content_text).group(1).strip()
        else:
            if document_code_compiler.search(title):
                document_code = document_code_compiler.search(title).group(1).strip()
            else:
                document_code = ''
            litigant_compiler = re.compile(r'^([\s\S]*?)\n' + r'(经查|经检查|依据.*?的有关规定|抽查|经抽查|.*?现场检查|'
                                                              r'.*进行调查)')
            litigant = litigant_compiler.search(content_text).group(1).strip()

        truth_text_str = r'((经查|.*?存在.*?行为)' \
                         r'([\s\S]*?))' \
                         r'((我局认为，)?(上述|以上).*?(事实|行为|事实)(,|，)?有.*?等证据(在案)?证明(,|，|。)(足以认定。)?|' \
                         r'(我局认为，|综上，)?[^，。,；\n]*?(上述|以上).*?(行为|问题|事实).*?违反.*?第.*条.*?(的规定)?|' \
                         r'你公司虚列会议费套取费用用于其他方面开支的行为，违反了《保险法》|' \
                         r'你公司未按规定使用经核准或备案的条款费率行为，违反了《保险法》|' \
                         r'根据《保险法》第一百五十条的规定，对你处以2万元罚款)'
        truth_compiler = re.compile(truth_text_str)
        if truth_compiler.search(content_text):
            truth = truth_compiler.search(content_text).group(1).strip()
        else:
            truth_text_str = litigant + r'([\s\S]*?)' \
                                        r'((我局认为，)?(上述|以上).*?(事实|行为|事实)(,|，)?有.*?等证据(在案)?证明(,|，|。)(足以认定。)?|' \
                                        r'(我局认为，|综上，)?(上述|以上).*?(行为|问题|事实).*?违反.*?第.*条.*?(的规定)?)'
            truth_compiler = re.compile(truth_text_str)
            truth = truth_compiler.search(content_text).group(1).strip()

        if '申辩' in content_text:
            defense_text_str = r'((针对.*?行为.*?申辩意见|(当事人)?[^，。,；\n]*?(未)?提出(了)?陈述申辩(意见)?|' \
                               r'[^，。,；\n]*?向我局(报送|递交|提出)[^，。,；\n]*?|本案在审理过程中.*?提出陈述申辩|' \
                               r'[^，。,；\n]*?在(申辩材料|陈述申辩|陈述申辩意见|申辩意见)中称|[^，。,；\n]*?在听证阶段提出|' \
                               r'[^，。,；\n]*?在法定期限内(未)?提出(了)?(听证要求|陈述申辩|陈述申辩及听证要求)|' \
                               r'你在.*?材料中(称|提出)|你[^，。,；\n]*?在.*?中未对我局认定的(违法|违规)事实|' \
                               r'[^，。,；\n]*?申辩称)' \
                               r'([\s\S]*?))' \
                               r'(因此，我局决定|' \
                               r'我局经复核(认为|决定)|' \
                               r'本案现已审理终结|' \
                               r'我局经复查[^，。,；\n]*?情况|' \
                               r'我局[^，。,；\n]*?认真复核|' \
                               r'经研究，对[^，。,；\n]*?予以采纳。|' \
                               r'我局认为.*?申辩理由|' \
                               r'依据.*?我局认为.*?的申辩理由|' \
                               r'经研究|' \
                               r'经我局审核，决定|' \
                               r'我局认为，上述违法行为事实清楚、证据确凿、法律法规适当|' \
                               r'我局对陈述申辩意见进行了复核|' \
                               r'经我局审核|' \
                               r'针对[^，。,；\n]*?的(陈述)?申辩意见，我局进行了核实|' \
                               r'经查，我局认为|' \
                               r'依据现场检查及听证情况|' \
                               r'我局对陈述和申辩材料进行了复核，我局认为|' \
                               r'经查|我局认为)'
            defense_compiler = re.compile(defense_text_str, re.MULTILINE)
            defense_list = defense_compiler.findall(content_text)
            if len(defense_list) != 0:
                defense = defense_list[-1][0].strip()
                defense_response_str = defense.replace(r'[', r'\[').replace(r']', r'\]') \
                                       + r'(([\s\S]*?)' + r'(本案现已审理终结。|不符合.*?情形。|根据.*?依法可从轻或者减轻行政处罚。|' \
                                                          r'对[^，。,；\n]*?申辩意见(不予|予以)采纳|因此.*?申辩理由.*?成立。|' \
                                                          r'我局认为.*?申辩(理由|意见).*?符合.*?第.*?条.*?的条件.(予以采纳。)?|' \
                                                          r'不予采纳。|因此，不予采纳当事人的陈述申辩意见。|' \
                                                          r'因此，对你提出的陈述和申辩意见不予采纳。))'
                defense_response_compiler = re.compile(defense_response_str, re.MULTILINE)
                if defense_response_compiler.search(content_text):
                    defense_response = defense_response_compiler.search(content_text).group(1).strip()
                else:
                    if '未' in defense:
                        defense_response = ''
            else:
                defense_text_str = '([^。；\n]*?向.*?公告送达了《行政处罚事先告知书》.*?提出陈述申辩。|' \
                                   '我局依法于2012年5月25日对你公司送达了《行政处罚事先告知书》，你公司在规定的时间内未提出陈述和申辩意见，也未要求举行听证。)'
                defense_compiler = re.compile(defense_text_str, re.MULTILINE)
                defense = defense_compiler.search(content_text).group(1).strip()
                defense_response = ''
        else:
            defense = defense_response = ''

        punishment_decision_text_str = r'(((依据|根据).*?第?.*?条.*?(规定)?.?(我局)?(决定|责令|给予|于.*?向.*?发出|对.*?作出|拟对你|对你)|' \
                                       r'我局经复核认为|我局决定.*?作出(如下|以下)(行政)?处罚：|' \
                                       r'我局作出如下处罚)' \
                                       r'([\s\S]*?))' \
                                       r'(请在本处罚决定书送达之日|当事人应当在接到本处罚决定书之日|如不服本处罚决定|' \
                                       r'请(在)?接到本处罚决定书之日|如不服从本处罚决定|[^。；\n]*?如对本处罚决定不服|' \
                                       r'[^。；\n]*?对以上行政处罚决定(如)?不服)'

        punishment_decision_compiler = re.compile(punishment_decision_text_str)
        punishment_decision = punishment_decision_compiler.search(content_text).group(1).strip()

        punishment_basis_str_list = [
            r'([^\n。；：:]*?)(问题|行为|事项|情况|事实|财务不真实|财务数据不真实)([^\n。；]*?)违(反|法)([^\n。；]*?)第.*?条.*?((的|之|等)(相关)?规定)?',
            r'吴淑汝代替投保人抄录风险提示语句，违反了《人身保险新型产品信息披露管理办法》第六条的规定',
            r'平安人寿上海电销中心上述行为《保险法》（2014年修正）第一百一十六条第一项的规定',
            r'富邦财产保险有限公司福州营销服务部未严格执行条款费率，违反了《保险法》第一百三十六条的规定',
            r'账外暗中支付给中介机构委托合同以外的利益，违反了《保险公司中介业务违法行为处罚办法》第十条',
            r'对代理人培训不到位、执行代理业务核查制度不到位，违反了《保险公司中介业务违法行为处罚办法》第六条和第七条',
            r'中国平安财产保险股份有限公司宁德中心支公司违反了《保险法》第一百一十六条第十项的规定',
            r'一、富邦财产保险有限公司福州营销服务部未严格执行条款费率，违反了《保险法》第一百三十六条的规定',
            r'二、富邦财产保险有限公司福州营销服务部虚列费用，违反了《保险法》第八十六条第二款的规定',
            r'我局认为，你公司未缴存保证金，也未投保职业责任保险，违反了《保险专业代理机构监管规定》第三十九条',
            r'你公司未按照要求及时预缴当年及清缴上年的监管费，违反了《保险专业代理机构监管规定》第六十三条',
            r'经查，在你任中国人民财产保险股份有限公司福清支公司经理期间，该公司在参加2009年福清市行政事业单位公务车辆统一投保项目（招标编号：0624-09180123）的投标活动中，违反了《保险法》第一百零七条的规定'
        ]
        punishment_basis_str = '|'.join(punishment_basis_str_list)
        punishment_basis_compiler = re.compile(r'[。\n；：:]' + '(' + punishment_basis_str + ')' +
                                               '.(\n?依据|\n?根据|\n?鉴于)', re.MULTILINE)
        punishment_basis_list = punishment_basis_compiler.findall(content_text)
        punishment_basis = '；'.join([kk[0].strip() for kk in punishment_basis_list])

        publish_date_text = re.search(
            punishment_decision.replace(r'(', r'\(').replace(r')', r'\)').replace(r'[', r'\[').replace(r']', r'\]').
            replace(r'*', r'\*') + r'([\s\S]*?)$', content_text).group(1).replace('\n', '')
        if re.search(r'.{4}年.{1,2}月.{1,3}日', publish_date_text):
            publish_date = re.findall('.{4}年.{1,2}月.{1,3}日', publish_date_text)[-1].replace(' ', '')
            m = re.match("([0-9零一二两三四五六七八九十〇○ＯOΟО]+年)?([0-9一二两三四五六七八九十]+)月?([0-9一二两三四五六七八九十]+)[号日]?", publish_date)
            real_publish_date = get_year(m.group(1)) + str(cn2dig(m.group(2))) + '月' + str(cn2dig(m.group(3))) + '日'
        else:
            publish_date_text = table_content.find_all('tr')[1].text
            publish_date = re.findall(r'\d{4}-\d{1,2}-\d{1,2}', publish_date_text)[-1]
            real_publish_date = publish_date.split('-')[0] + '年' + str(int(publish_date.split('-')[1])) + '月' + str(
                int(publish_date.split('-')[2])) + '日'

        result_map = {
            'announcementTitle': title,
            'announcementOrg': '福建银保监局',
            'announcementDate': real_publish_date,
            'announcementCode': document_code,
            'facts': truth,
            'defenseOpinion': defense,
            'defenseResponse': defense_response,
            'litigant': litigant[:-1] if litigant[-1] == '：' else litigant,
            'punishmentBasement': punishment_basis,
            'punishmentDecision': punishment_decision,
            'type': '行政处罚决定',
            'oss_file_id': file_id,
            'status': 'not checked'
        }
        logger.info(result_map)
        if db.announcement.find({'announcementTitle': title, 'oss_file_id': file_id}).count() == 0:
            db.announcement.insert_one(result_map)
            logger.info('福建保监局 数据解析 ' + ' -- 数据导入完成')
        else:
            logger.info('福建保监局 数据解析 ' + ' -- 数据已经存在')
        db.parsed_data.update_one({'_id': file_id}, {'$set': {'parsed': True}})
        logger.info('福建保监局 数据解析 ' + ' -- 修改parsed完成')


import firebase_admin
from firebase_admin import credentials, db
import datetime

cred = credentials.Certificate("epam_d1a54_key.json")
firebase_admin.initialize_app(cred, {
    'databaseURL' : 'https://epam-d1a54.firebaseio.com'
})

time = datetime.datetime.now();

ref = db.reference()
orders_ref = ref.child('orders')
users_ref = ref.child('users')
books_ref = ref.child('books')
teleBot_ref = ref.child('teleBot')
teleBot_ref.set({
    'chat_to_user': {
        '7836597': 'asss',
    },
    'user_to_chat': {
        'asss': '783659795',
    },
})
# orders_ref.set({
#     'a': {
#         'from_owner_id': 'asssa',
#         'to_user_id': 'adnfs',
#         'book_id': 'bsdf',
#         'start_date': '2019-03-10',
#         'duration': '3',
#         'availability':'False',
#     },
#     'b': {
#         'from_owner_id': 'adnfs',
#         'to_user_id': 'asssa',
#         'book_id': 'ssdf',
#         'start_date': '2019-03-13',
#         'duration': '3',
#         'availability':'False',
#     },
# })
# users_ref.set({
#     'asssa':{
#         'first_name': 'Kerbez',
#         'last_name' : 'Orazgaliyeva',
#         'email' : 'kerbez2898@gmail.com',
#         'book_ids' : 'bsdf, bsde, bsda',
#     },
#     'adnfs':{
#         'first_name': 'Sanzhar',
#         'last_name' : 'Alim',
#         'email' : 'alim@gmail.com',
#         'book_ids' : 'ssdf, ssde, ssda',
#     }
# })
# books_ref.set({
#     'bsdf':{
#         'title':'Harry Potter and stone',
#         'author':'',
#         'description':'',
#         'image_path':'',
#         'rate_ids':'',
#         'availability':'False',
#         'order_ids':'a'
#     },
#     'bsde':{
#         'title': '',
#         'author': '',
#         'description': '',
#         'image_path': '',
#         'rate_ids': '',
#         'availability': 'True',
#         'order_ids': ''
#     },
#     'bsda':{
#         'title': '',
#         'author': '',
#         'description': '',
#         'image_path': '',
#         'rate_ids': '',
#         'availability': 'True',
#         'order_ids': ''
#     },
#     'ssdf':{
#         'title': 'kruassany',
#         'author': 'Yashkino',
#         'description': '',
#         'image_path': '',
#         'rate_ids': '',
#         'availability': 'False',
#         'order_ids': 'b'
#     },
#     'ssde':{
#         'title': '',
#         'author': '',
#         'description': '',
#         'image_path': '',
#         'rate_ids': '',
#         'availability': 'True',
#         'order_ids': ''
#     },
#     'ssda':{
#         'title': '',
#         'author': '',
#         'description': '',
#         'image_path': '',
#         'rate_ids': '',
#         'availability': 'True',
#         'order_ids': ''
#     },
# })


#!/usr/bin/env python3

import threading
import serial
import sys

def read_from_port(ser):
    while True:
        data = ser.readline().decode('ascii')
        print(f'{data[:-1]}')

def write_to_port(ser):
    while True:
        data = input().encode('ascii')
        data += b'\n'
        ser.write(data)

def main():

    retval = 0

    if 1 < len(sys.argv):
        try:
            ser = serial.Serial(port = sys.argv[1],
                                baudrate = 9600,
                                timeout = None,
                                parity = serial.PARITY_NONE,
                                stopbits = serial.STOPBITS_ONE)

            print(f"\nUsing serial port {ser.name}\n")

            t1 = threading.Thread(target=read_from_port, args=(ser,))
            t2 = threading.Thread(target=write_to_port, args=(ser,))

            t1.start()
            t2.start()

        except serial.SerialException as e:
            print(f"\nSerial port error: {e}")
            retval = 1
        except KeyboardInterrupt:
            print('\nExiting...')
            ser.close()
            retval = 0
    else:
        print("\nNo port argument given.")
        retval = 1

    return retval

if __name__ == "__main__":
    sys.exit(main())


test = [[0 for i in range(13)] for i in range(13)]
maxi = -1
while True:
	inp = input('u v: ')
	if inp == 'end':
		break
	a, b= list(map(int, inp.split(' ')))
	test[a][b] = test[b][a] = float(input('weight: '))
	maxi = max(maxi, max(a, b))

with open('represent/original6.txt', 'w') as file:
	for t in test[:maxi+1]:
		file.write(' '.join(str(i) for i in t[:maxi+1]) + "\n")


from typing import List

class AllPathsFromSourcetoTarget:
    def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
        if not graph or len(graph) == 0:
            return []
        results = []
        self.dfs(graph, 0, [0], results)
        return results

    def dfs(self, graph, node, path, results):
        if node == len(graph) - 1:
            results.append(list(path))
            return
        for neighbor in graph[node]:
            path.append(neighbor)
            self.dfs(graph, neighbor, path, results)
            path.pop()


import math

factors={}
def count(n):
	global factors
	while n % 2 == 0:
		if 2 not in factors:
			factors[2]=1
		else:
			factors[2]+=1
		n = n / 2

	for i in range(3,int(math.sqrt(n))+1,2):
		while n % i== 0:
			if i not in factors:
				factors[i]=1
			else:
				factors[i]+=1
			n = n / i
	if n > 2:
		if n not in factors:
			factors[int(n)]=1
		else:
			factors[int(n)]+=1

n,m=[int(x) for x in input().split()]
mod=10**9 + 7
count(m)
prod=1

for factor,exp in factors.items():
	prod*=(math.comb(exp+n-1,exp))

print(prod%mod)

# print(stars)
# # 1 2 3 1

# bars=n-1
# mod=10**9+7

# combs=math.comb(stars+bars+1,bars)%mod

# print(combs)


import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import random

mpl.rcParams['legend.fontsize'] = 10

fig = plt.figure()
ax = fig.gca(projection='3d')

xyz = []
cur = [0, 0, 0]

for _ in range(888):
    axis = random.randrange(0, 3)
    cur[axis] += random.choice([-1, 1])
    xyz.append(cur[:])

x, y, z = zip(*xyz)
ax.plot(x, y, z,c='darkslategrey',alpha=0.6, label='Random walk')
ax.scatter(x[0], y[0], z[0], c='deepskyblue',s=55,alpha=0.6, marker='o',label='Start point')
ax.scatter(x[-1], y[-1], z[-1], c='crimson',s=55,alpha=0.6, marker='<',label='End point')   # End point
ax.legend()
plt.title('3D lattice random walk')
plt.show()


from pathlib import Path
from typing import Any, Dict, Tuple

from xz import XZFile

_IntegrationCase = Tuple[Path, Dict[str, Any]]

def test_read_all(integration_case: _IntegrationCase, data_pattern: bytes) -> None:
    xz_path, metadata = integration_case
    with XZFile(xz_path) as xzfile:
        streams_items = list(
            xzfile._fileobjs.items()  # pylint: disable=protected-access
        )
        assert len(streams_items) == len(metadata["streams"])
        pos = 0
        stream_boundaries = []
        block_boundaries = []
        for stream_item, metadata_stream in zip(streams_items, metadata["streams"]):
            stream_boundaries.append(pos)
            stream_pos, stream = stream_item
            assert stream_pos == pos
            assert stream.check == metadata_stream["check"]
            block_items = list(
                stream._fileobjs.items()  # pylint: disable=protected-access
            )
            assert len(block_items) == len(metadata_stream["blocks"])
            for block_item, metadata_block in zip(
                block_items, metadata_stream["blocks"]
            ):
                block_boundaries.append(pos)
                block_pos, block = block_item
                assert block_pos == pos - stream_pos
                assert len(block) == metadata_block["length"]
                pos += metadata_block["length"]
            assert len(stream) == pos - stream_pos
        assert xzfile.stream_boundaries == stream_boundaries
        assert xzfile.block_boundaries == block_boundaries
        assert xzfile.read() == data_pattern

def test_read_reversed(integration_case: _IntegrationCase, data_pattern: bytes) -> None:
    xz_path, _ = integration_case
    with XZFile(xz_path) as xzfile:
        # we are testing the worst possible case (lots of negative seeking)
        # limit the time to test by reading in chunks instead of 1 byte at a time
        read_size = 37
        for pos in reversed(range(0, len(data_pattern), read_size)):
            xzfile.seek(pos)
            assert xzfile.read(read_size) == data_pattern[pos : pos + read_size]


import plotly
import plotly.io as pio
import plotly.graph_objects as go
import pandas as pd
from plotly.subplots import make_subplots

# list of default Plotly colors to color a trace with
DEFAULT_PLOTLY_COLORS=['rgb(31, 119, 180)', 'rgb(255, 127, 14)',
                       'rgb(44, 160, 44)', 'rgb(214, 39, 40)',
                       'rgb(148, 103, 189)', 'rgb(140, 86, 75)',
                       'rgb(227, 119, 194)', 'rgb(127, 127, 127)',
                       'rgb(188, 189, 34)', 'rgb(23, 190, 207)']

class graph_test() :
  # Constructor for routes.py to send/receive data
  #   from this program
  def __init__( self, state_list, feature1, feature2) :
    # Stores the states they want to display
    self.states = state_list

    # Stores the first feature user wants to display
    self.f1 = feature1

    # Stores the first feature user wants to display
    self.f2 = feature2
#-------------------------------------------------------------

def main() :
  # Write a list of states to see the Covid data of
  state_list = ['TX', 'FL', 'NJ']

  # Write the name first feature you want to find the data of
  feature1 = 'New Cases'

  # Write the name second feature you want to find the data of
  feature2 = 'New Deaths'

  #-------------------------------------------------------------------------------------
  # Initialize the nGraph object by calling the graph_test constructor
  nGraph = ''

  # Call the feature_graph() function below to create a Graph figure to display
  fig = ''

  # Uncomment line below once the fig object is returned from feature_graph()
  #pio.write_html(fig, file='workshop_graph.html', auto_open=False)
  #-------------------------------------------------------------------------------------

#-------------------------------------------------------------
def feature_graph( states, feature1, feature2 ) :

  #------------------------------------------------------------------------------------
  # Read CSV file generated from Task 1 using the Pandas library
  df = ''
  #------------------------------------------------------------------------------------

  #------------------------------------------------------------------------------------
  # Count the number of days that are available for each state
  # Rather than count it manually, use some of Pandas' built-in functions
  numDays = 0
  #------------------------------------------------------------------------------------

  # frames should correlate to the number of days that pass
  num_frames = numDays

  # lines that appear on the graph, should be one per state chosen
  num_traces = []

  #------------------------------------------------------------------------------------
  # Uncomment line below and convert the Date column in the CSV from a Datetime object
  #   to a String object in the form of mm-dd-YYYY format using Pandas
  #df['Date'] = ''
  #------------------------------------------------------------------------------------

  # stores the rows where each state is first used in the CSV file
  axis = []

  # finds the index for which row the state appears for the first time in the CSV
  strt_idx = 0

  for i in range(len(states)) :
    # this value will be used to determine how many lines to draw when the
    #   graph beginning to be generated
    num_traces.append( int(i) )

    # Store the indexes at which the state first appears in the CSV file
    axis.append(int(strt_idx))

    strt_idx = strt_idx + numDays + 1

  # creates a figure that will store 2 subgraphs, right next to each other
  fig = make_subplots(rows=1, cols=2, horizontal_spacing = 0.075,
                      specs=[[{"secondary_y": True}, {"secondary_y": True}]])

  # only show the state name at the far right of the drawn line (i.e. trace)
  all_text = []

  # all the data points will have nothing written on the other points other than
  #   the most recent data point
  for k in range(numDays) :
    all_text.append('')

  for j in range(len(axis)) :
    # represents the index at each iteration, used for convenience
    cur_idx = axis[j]

    #------------------------------------------------------------------------------------
    # Update x and y values read from a CSV and store those values
    #
    # x should be the dates chosen
    # y1 and y2 should be the values for each feature on that day
    x  = 0
    y1 = 0
    y2 = 0
    #------------------------------------------------------------------------------------

    # Create a way to only write the state's initials at the final
    #   data point and not on each data point, as is done here
    all_text.append(states[j])

    # x & y axis values for the first graph (on the left)
    fig.add_trace(go.Scatter(x = x, y = y1, name=states[j],
                             legendgroup=str(j),
                             marker=dict(color=DEFAULT_PLOTLY_COLORS[j]),
                             mode='lines+text',
                             text=all_text,
                             textposition='middle right'),
                  row=1, col=1, secondary_y=False)

    # x & y axis values for the second graph (on the right)
    fig.add_trace(go.Scatter(x = x, y = y2, name=st[j],
                             legendgroup=str(j),
                             marker=dict(color=DEFAULT_PLOTLY_COLORS[j]),
                             mode='lines+text',
                             text=all_text,
                             textposition='middle right',
                             showlegend=False),
                  row=1, col=2, secondary_y=False)

    # remove the name at the end of the list to make way for the name state chosen
    all_text.pop()

  # data is a parameter for Plotly's Frame object
  # it will determine what do draw from the given range in the dataset
  frames = []

  # Update the number of traces by two since there are two graphs
  for i in range(len(num_traces), len(num_traces)*2) :
    num_traces.append( int(i) )

  # want to create enough frames for all the users
  for i in range(num_frames+1) :
    # data is a parameter for Plotly's Frame object
    # it will storedata points what to draw from the dataset
    day_data = []

    for j in range(len(states)) :
      cur_idx = axis[j]

      #------------------------------------------------------------------------------------
      # Update x and y values read from a CSV and store those values
      #
      # x should be the dates chosen
      # y1 and y2 should be the values for each feature on that day
      x  = 0
      y1 = 0
      y2 = 0
      #------------------------------------------------------------------------------------

      day_data.append(go.Scatter(x=x, y=y1,visible=True))
      day_data.append(go.Scatter(x=x, y=y2,visible=True))

    # want to make an list of lists to store the different data ranges
    #   to draw from frame to frame
    if i == 0 :
      frames.append(go.Frame(data=None,
                             traces=num_traces))

    # Plotly will take drawing the dot as the first line of input
    # This will not be considered, we want to start with a line already drawn
    else :
      frames.append(go.Frame(data=day_data,
                             traces=num_traces))

  #------------------------------------------------------------------------------------
  # Create an array of date values from the start date up
  #   and an end date that is numDays after the fact
  dates = []
  #------------------------------------------------------------------------------------

  steps = []
  for i in range(num_frames+1):
    step = dict(
      method="animate",

      #--------------------------------------------------------------------------------
      # Update ticks on slider to print out values from dates array
      label=i,
      #--------------------------------------------------------------------------------

      args = [frames[i], dict(frame=dict(duration=500, redraw=False),
                         transition=dict(duration=0),
                         easing='linear',
                         fromcurrent=True,
                         mode='immediate')]
    )
    steps.append(step)

  # Creates button objects to be interactive with the slider
  play_button = dict(label='Play',
                     method='animate',
                     args=[None, dict(frame=dict(duration=0, redraw=False),
                                          transition=dict(duration=0),
                                          easing='linear',
                                          fromcurrent=True,
                                          mode='immediate')])

  pause_button = dict(label='Pause',
                      method='animate',
                      args=[None, dict(frame=dict(duration=0, redraw=False),
                                       transition=dict(duration=0),
                                       mode='immediate')])

  sliders = [dict(yanchor = 'top',
                  xanchor = 'left',
                  currentvalue = {'font': {'size': 15},'prefix': 'Date: ','visible': True,'xanchor': 'right'},
                  transition = {'duration': 0, 'easing': 'linear'},
                  pad = {'b': 10, 't': 50},
                  len = 0.8,
                  x = 0.1,
                  y = -0.15,
                  #---------------------------------------------------------------------
                  # Add the steps variable once the variable is complete
                  steps = []
                  #---------------------------------------------------------------------
            )]

  fig.frames=frames
  fig.update_layout(updatemenus=[dict(type='buttons',
                                      showactive=False,
                                      y=-0.35,
                                      x=0.05,
                                      xanchor='right',
                                      yanchor='top',
                                      buttons=[play_button, pause_button])],
                    sliders=sliders,
                    legend=dict(orientation='h',
                                yanchor='bottom',
                                y=1.02,
                                xanchor='right',
                                x=0.58,
                                title=dict(text='State(s):'),
                                valign='middle'
                                ),
                    height=550,
                    width=1500
                    )

  #------------------------------------------------------------------------------------
  # Update the x-axes to start from the first available date and end with the most
  #   recent date of data collected (i.e. update the 0s and 1s with the date list
  #   we made above)
  fig.update_xaxes(range=[ 0,
                           1 ],
                   row=1, col=1)

  fig.update_xaxes(range=[ 0,
                           1 ],
                   row=1, col=2)
  #------------------------------------------------------------------------------------

  #------------------------------------------------------------------------------------
  # Update the y-axes to include the names of the features
  fig.update_yaxes(title_text='Sample 1', row=1, col=1)
  fig.update_yaxes(title_text='Sample 2', row=1, col=2)
  #------------------------------------------------------------------------------------

  return fig
main()


#!/usr/bin/python3

def print_list_integer(my_list=[]):
    """
    Print integers from a list.

    Args:
        my_list (list of int): The list of integers to print.

    Returns:
        None
    """
    if not isinstance(my_list, list):
        raise TypeError("my_list must be a list")

    for num in my_list:
        if not isinstance(num, int):
            raise TypeError("All elements in my_list must be integers")
        print("{:d}".format(num))

# If you want to test the function
if __name__ == "__main__":
    my_list = [1, 2, 3, 4, 5]
    print_list_integer(my_list)


# -*- coding: utf-8 -*-
"""
Created on Wed Feb 23 14:03:36 2022

@author: Emma Peleg

This program runs DPV histogram data and procecess it

Maybe it will look at the melting Index? Maybe not
"""
import csv
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import os
from math import pi
import hdbscan
from scipy import stats

    ## this program can be used to analyze multiple files in multiple folders by inputting the path for the main containing folder
    ##i.e. put all of your DPV folders with data in one larger folder and run the program in the path of the larger folder
path_of_the_directory = input("Paste the path for the folder containing the desired DPV files :\n" )
ext = ".prt"

os.chdir(path_of_the_directory)

for root, dirs, files in os.walk(path_of_the_directory):
    for title in files:
        if title.endswith(".prt"):
            os.chdir(root) ##changes directory to working one each loop
            setname=title[0:-4]
            w = pd.read_table(title, header=0, delimiter='\s+')
            data_df = w[["Speed","Temperature"]].copy()

            ##diameter fixing using modal diameter from PSA
            EnergyB = np.array(w["EnergyB"])
            Old_Diameters = np.array(w["Diameter"])
            PSA_Diameter_mode = int(input("Enter modal diameter from PSA analysis: \n")) ## um
            Full_Temp = np.array(w["Temperature"])
            DC = (Old_Diameters)/(np.sqrt(EnergyB/Full_Temp**4))
            DC_mode = stats.mode(DC)[0]
            Old_D_mode=stats.mode(Old_Diameters)[0]
            New_DC=DC_mode*(PSA_Diameter_mode/Old_D_mode)
            Real_Diameters = New_DC*(np.sqrt(EnergyB/Full_Temp**4))

            data_df["Diameter"]=Real_Diameters ## adding fixed diameters
            data=np.array(data_df) ## data for speed temp diameter as array for hdbscan

            ## INPUT VARIABLES NEEDED

            """the official dataframe of the data from the DPV *.prt file is saved as df

            Proceed to do more stuff
            temp = temperature [C]
            vel = velocity [m/s]
            diam = diameter [um]
            tMelt = melting temperature [C]
            den = density [kg/m^3]  (1 g/cc = 1000 kg/m^3)
            k = thermal conductivity [W/mK]
            hf = heat of fusion [J/kg]
            h = heat transfer coefficient [W/m^2K]
            D = particle size [m]
            rp = radius of particle [m] ??

                        k = obj.thermalConductivity;
                        hf = obj.heatOfFusion;

                        biotNumber = 30000*(0.5*diam*(10^-6))/k;

                        MI = 3.3*(12*k*(temp-tMelt)*(sprayDist*(10^-3)))./(den*hf*(1+2./biotNumber).*((.5*diam*(10^-6)).^2).*vel);

                        %CALCULATE KE
                        volume = (4/3)*pi*((0.5*diam).^3)*(10^(-18));
                        KE = 0.5*den*volume.*(vel.^2) * 10^6;

            """

            """
            Do an outlier removal using hdbscan:
            """

            clusterer = hdbscan.HDBSCAN()

            fit=clusterer.fit(data_df)

            cluster_labels = clusterer.labels_
            min_cluster = clusterer.labels_.min()
            max_cluster = clusterer.labels_.max()
            cluster_prob = clusterer.probabilities_
            cluster_out = clusterer.outlier_scores_

            #sns.displot(clusterer.outlier_scores_[np.isfinite(clusterer.outlier_scores_)], rug=True)

            threshold = pd.Series(clusterer.outlier_scores_).quantile(0.9)
            outliers = np.where(clusterer.outlier_scores_ > threshold)[0]
            #cluster labels are outliers

            cluster_list = []
            for i in cluster_labels:
                cluster_list.append(i)

            outlier_list= []
            for index,i in enumerate(cluster_labels):
                if i == -1:
                    outlier_list.append(index)
                    #print(index,i)
         #plot xyz with color as label

            outlier_array = []
            for i in outlier_list:
                out = data[i]
                outlier_array.append(out)

            data_index_list=list(range(0,len(data)))

            for element in outlier_list:
                if element in data_index_list:
                    data_index_list.remove(element)

            clean_data=[]
                    ## removing all of the outliers and saving in new array
            for i in data_index_list:
                add_data=data[i]
                clean_data.append(add_data)
            clean_data_df = pd.DataFrame(clean_data,columns=["Velocity","Temperature","Diameter"])

            """
            Make the arrays for the different values without outliers
            """

            All_Temps = []      ##  list of all particle temperatures
            for i in clean_data_df["Temperature"]:
                All_Temps.append(float(i))

            Temp_Array=np.array(All_Temps)+273.15 ## converting to Kelvin

            All_Vel = []        ##  list of all velocities of particles
            for i in clean_data_df["Velocity"]:
                All_Vel.append(float(i))
            Velocity_Array=np.array(All_Vel)

            All_Diameters = []  ## list of all particle diameters
            for i in clean_data_df["Diameter"]:
                All_Diameters.append(float(i))
            Diameter_Array=np.array(All_Diameters)*(10**-6) #in meters!
            #fix diameter scale coefficient bc particles r wrong!

            ##use df and pandas using hdbscan (doesnt work with dataframes) so use a np array
            #labels clusters based on their values (-1 = statistical noise etc)
            #sort by cluster that contained most points, sort high to low
            #use velocity or T,V,D for outliers
            """

            Doing the melting index, kinetic energy, and oxidation index calculations

            """

            k = 138 ## thermal conductivity W/mK for Mo at 20C (>?????) change for different powders
            hf = 36/95.95*1000*1000 ## heat fusion for Mo in J/kg
            density_cc = 9 ## g/cc
            density = 9*(1000) ## density for Mo kg/m3
            Tm = 2623 ## melting temperature for Mo
            sprayDist = .15

            h = [] ## convective coefficient
            for i in Velocity_Array:
                h_app=0.0167*i**3 - 7.3956*i**2 + 1109.3*i - 43732
                h.append(h_app)
            h = np.array(h)

            biotNumber = h*(Diameter_Array)/k

            t_fly = []
            for i in Velocity_Array:
                t = (2*sprayDist)/i
                t_fly.append(t)

            """ Melting Index formulas"""
            simpMI = Temp_Array*t_fly/Diameter_Array

            MI = (24*k/(density*hf))*(1/(1+(4/biotNumber)))*(((Temp_Array-Tm)*(t_fly))/((Diameter_Array)**2))
            #low_Biot_MI = ((6*h)/(density*hf))*(1/(1+(4/biotNumber)))*(((Temp_Array-Tm)*(t_fly))/((Diameter_Array)**2))

            """ Kinetic Energy """

            Volume = (4/3)*pi*((.5*Diameter_Array)**3) ## cubic centimeters
            KE = 0.5*density*Volume*(Velocity_Array**2) ## Joules
            Average_KE = np.mean(KE)*10**6 ## micro Joules
            Average_KE_str = f"{Average_KE:.2f}"

            """ Oxidation Index"""

            # need oxygen concentration in gas phase

            """
            Plotting the velocities, temperatures, and particle diameters, and MI
            """
            ## Sturges Rule Calculations
            N = len(clean_data)
            K = int(1 + 3.322*np.log(N))

            fig, axs = plt.subplots(3,1,figsize=(6,6))

            axs[0].hist(Temp_Array,K,facecolor='blue',edgecolor='black')
            #axs[0].set_title('Temperature (C)')
            #axs[0].set_xlim(2500,5000)
            mean_temperature = np.mean(All_Temps)
            axs[0].set_xlabel('Temperature (C)' + " (Mean = " + str(round(mean_temperature,2)) + ")", fontsize=13)

            axs[1].hist(Velocity_Array,K,facecolor='red',edgecolor='black')
            mean_velocity = np.mean(All_Vel)
            axs[1].set_xlabel("Velocity (m/s)" + " (Mean = " + str(round(mean_velocity,2)) + ")", fontsize=13)
            #axs[1].set_title('Velocity (m/s)')
            #axs[1].set_xlim(30,300)

        #from DPV_Run_All import setname

                ##change diameter unit
            D_array_um = np.array(All_Diameters)

            axs[2].hist(D_array_um,K,facecolor='green',edgecolor='black')
            mean_diameters = np.mean(All_Diameters)
            axs[2].set_xlabel('Diameters (um)' + " (Mean = " + str(round(mean_diameters,2)) + ")", fontsize=13)
            #axs[2].set_title('Diameters (um)')
            #axs[2].set_xlim(0,60)
            plt.suptitle(setname)
            plt.tight_layout()

            plt.savefig("TVD_"+ setname[0] + ".png")
            for ax in axs.flat:
                ax.set(ylabel='Count')

            """ Melting Index Figure """

            MI_counts,MI_bins = np.histogram(MI,20)
            max_count_MI = max(MI_counts)-75
            middle_bin = np.mean(MI)-5

            ## find molten % of particles
            melt_threshold = 0
            melted_particles = [element for element in MI if element > melt_threshold]
            melted_number = len(melted_particles)
            molten_percent = len(melted_particles)/len(MI)*100
            molten_percent_str = f"{molten_percent:.2f}"

            fig2, ax1 = plt.subplots(1,1,figsize=(6,5))

            ax1=plt.hist(MI,K,facecolor='grey',edgecolor='black')
            #plt.title("Melting Index")
            plt.ylabel("Count")
            plt.xlabel("Melting Index" + ' (Molten Content '+ molten_percent_str +"%)")
            plt.axvline(x=0,ymin=0,color='black',linestyle='dotted',linewidth=3)
            plt.tight_layout()
            #plt.xlim(-5,5)
            plt.suptitle(setname)
            plt.tight_layout()
            plt.savefig("MI_"+ setname[0] + ".png")
            #plt.close()

            """ Kinetic Energy Figure """
            KE_counts,KE_bins = np.histogram(KE*10**6,20)
            max_count_KE = max(KE_counts)-10
            middle_bin_KE = np.mean(KE*10**6)+.5
            fig3, ax1 = plt.subplots(1,1,figsize=(6,5))

            ax1=plt.hist(KE*10**6,K,facecolor='grey',edgecolor='black')
            #plt.title("Melting Index")
            plt.ylabel("Count")
            plt.xlabel("Kinetic Energy [uJ]" + ' (Average KE '+ Average_KE_str +"uJ)")
            plt.tight_layout()
            x_max = 4.5*Average_KE
            #plt.text(middle_bin_KE,max_count_KE,'Average KE '+ Average_KE_str +"uJ",rotation=0)
            #plt.xlim(0,x_max)
            plt.suptitle(setname)
            plt.tight_layout()
            plt.savefig("KE_"+ setname[0] + ".png")