from pygame import *
from pygame.locals import *
from random import *

class Bullet(sprite.Sprite):
    def __init__(self,bg_size,position):
        sprite.Sprite.__init__(self)       #调用Sprite方法！！！

        self.image = image.load("image/bullet.png").convert_alpha() #一定要注意图片格式阿尔法转换！
        self.rect = self.image.get_rect()
        self.width,self.height = bg_size[0],bg_size[1]

        self.rect.left,self.rect.top = position
        self.active = True
        self.speed = 10

        self.mask = mask.from_surface(self.image)

    def move(self):
        self.rect.top -= self.speed

        if self.rect.bottom < 0:
            self.active = False

    def reset(self,position):
        self.rect.left,self.rect.top = position
        self.active = True

class Superbullet(sprite.Sprite):
    def __init__(self,bg_size,position):
        sprite.Sprite.__init__(self)

        self.image = image.load("image/superbullet2.png").convert_alpha()
        self.rect = self.image.get_rect()
        self.width,self.height = bg_size[0],bg_size[1]

        self.rect.left,self.rect.top = position
        self.speed = 20

    def move(self):
        self.rect.top -= self.speed

class Enemy_bullet(sprite.Sprite):
    def __init__(self,bg_size,position):
        sprite.Sprite.__init__(self)

        self.image = image.load("image/enemy_bullet.png").convert_alpha()
        self.rect = self.image.get_rect()
        self.width,self.height = bg_size[0],bg_size[1]

        self.speed = 2

        self.rect.left,self.rect.top = position

    def move(self):
        if self.rect.top < self.height:
            self.rect.top += self.speed

    def move_leftdown(self):
        if self.rect.top < self.height and self.rect.right > 0:
            self.rect.top += self.speed
            self.rect.left -= self.speed-1

    def move_rightdown(self):
        if self.rect.top < self.height and self.rect.left < self.width:
            self.rect.top += self.speed
            self.rect.right += self.speed-1


from math import sqrt

def primo(n):
    for y in range(2, int(sqrt(n) + 1)):
        if not n % y:
            return False
    return True
def MDC(dvdndo, dvsor):
    if dvsor == 0:
        return dvdndo
    else:
        return MDC(dvsor, dvdndo % dvsor)
def primentre(x, i):
    if MDC(x, i) == 1:
        return True
    return False

T, N, M = [int(x) for x in input().split(" ")]
A = [N, M]

for u in range(2, T):
    if primo(A[u - 1]):
        result = A[u - 1] * 2
        A.append(result)
    elif primentre(A[u - 1], A[u - 2]):
        result = 2 * (A[u - 1] + A[u - 2]) - 3
        A.append(result)
    else:
        result = A[u - 1] + 1
        A.append(result)
print(" ".join(map(str, A)))


import atexit
import os
import shutil
import tempfile
from pathlib import Path

import dask
import dask.array as da
import numpy as np
import zarr

from fibsem_tools.io.dask import store_blocks
from fibsem_tools.io.core import create_group, access, read
from fibsem_tools.io.util import list_files, list_files_parallel, split_by_suffix
from fibsem_tools.io.zarr import delete_zbranch

def _make_local_files(files):
    result = []
    for f in files:
        Path(f).parent.mkdir(parents=True, exist_ok=True)
        Path(f).touch(exist_ok=True)
        result.append(str(Path(f).absolute()))
    return result

def test_list_files(temp_dir):
    fnames = [
        os.path.join(temp_dir, f)
        for f in [
            os.path.join("foo", "foo.txt"),
            os.path.join("foo", "bar", "bar.txt"),
            os.path.join("foo", "bar", "baz", "baz.txt"),
        ]
    ]
    _make_local_files(fnames)
    files_found = list_files(temp_dir)
    assert set(files_found) == set(fnames)

def test_list_files_parellel(temp_dir):
    fnames = [
        os.path.join(temp_dir, f)
        for f in [
            os.path.join("foo", "foo.txt"),
            os.path.join("foo", "bar", "bar.txt"),
            os.path.join("foo", "bar", "baz", "baz.txt"),
        ]
    ]
    _make_local_files(fnames)
    files_found = list_files_parallel(temp_dir)
    assert set(files_found) == set(fnames)

def test_path_splitting():
    path = "s3://0/1/2.n5/3/4"
    split = split_by_suffix(path, (".n5",))
    assert split == ("s3://0/1/2.n5", "3/4", ".n5")

    path = os.path.join("0", "1", "2.n5", "3", "4")
    split = split_by_suffix(path, (".n5",))
    assert split == (os.path.join("0", "1", "2.n5"), os.path.join("3", "4"), ".n5")

    path = os.path.join("0", "1", "2.n5")
    split = split_by_suffix(path, (".n5",))
    assert split == (os.path.join("0", "1", "2.n5"), "", ".n5")

def test_store_blocks(temp_zarr):
    data = da.arange(256).reshape(16, 16).rechunk((4, 4))
    z = zarr.open(temp_zarr, mode="w", shape=data.shape, chunks=data.chunksize)
    dask.delayed(store_blocks(data, z)).compute()
    assert np.array_equal(read(temp_zarr)[:], data.compute())

def test_group_initialization():
    store_path = tempfile.mkdtemp(suffix=".zarr")
    atexit.register(shutil.rmtree, store_path)
    data = {"foo": np.arange(10), "bar": np.arange(20)}
    a_attrs = {"foo": {"a": 10}, "bar": {"b": 15}}
    g_attrs = {"bla": "bla"}
    chunks = ((2,), (2,))
    group = create_group(
        store_path,
        data.values(),
        data.keys(),
        chunks=chunks,
        group_attrs=g_attrs,
        array_attrs=a_attrs.values(),
    )
    assert g_attrs == dict(group.attrs)
    for d in data:
        assert data[d].shape == group[d].shape
        assert data[d].dtype == group[d].dtype
        assert a_attrs[d] == dict(group[d].attrs)

def test_deletion(temp_zarr):
    existing = access(f"{temp_zarr}/bar", mode="w", shape=(10,), chunks=(1,))
    existing[:] = 10
    keys = tuple(existing.chunk_store.keys())

    for key in keys:
        assert key in existing.chunk_store

    # delete
    delete_zbranch(existing)

    for key in keys:
        if existing.name in key:
            assert key not in existing.chunk_store


class Solution:
    def orangesRotting(self, grid: List[List[int]]) -> int:
        drxn=[(1,0),(0,1),(0,-1),(-1,0)]
        n=len(grid)
        m=len(grid[0])
        q=deque() #row,col,time
        unrotten=0
        for i in range(n):
            for j in range(m):
                if grid[i][j]==2:
                    q.append((i,j,0))
                if grid[i][j]==1:
                    unrotten+=1
        change=0
        t=0
        while q:
            row,col,t=q.popleft()

            for x,y in drxn:
                nrow,ncol=x+row,y+col
                if 0<=nrow<n and 0<=ncol<m and grid[nrow][ncol]==1:
                    q.append((nrow,ncol,t+1))
                    grid[nrow][ncol]=2
                    change+=1
        return t if change==unrotten else -1


import pandas as pd
from datetime import timedelta
from prefect import task, flow
import pickle
import time
# from prefect.schedules import IntervalSchedule

# Define the task to read data from a CSV file
@task
def read_csv_file(file_path):
    df = pd.read_csv(file_path, header=None)
    return df

# Define the schedule for pipeline execution (e.g., every 5 minutes)
# schedule = IntervalSchedule(interval=timedelta(minutes=5))

# Define the task to load the model
@task
def load_model(model_file):
    with open(model_file, 'rb') as file:
        model = pickle.load(file)
    return model

# Define the task to process data DataFrame
@task
def preprocess_data(data):
    processed_data = data.iloc[:, 1:29]
    actual = data.iloc[:, 30:31]
    id = data.iloc[:,0:1]
    return processed_data, actual,id

# Define the task to predict fraud
@task
def predict_data(data,model):

    prediction = model.predict(data)
    return prediction

# Define the task to write data to a CSV file
@task
def write_csv_file(file_path,data):

    data.to_csv('output.csv',mode='a', header=False, index=False)

# Define the task to combine data
@task
def combine_data(df1,df2,df3,df4):
    df4 = pd.DataFrame(df4, columns=['Prediction'])

    df5 = pd.concat([df1, df2, df3, df4], axis=1)
    return df5

# Define the flow
@flow
def process_data():
    file_path_1 = "../data_generation/source1.csv"
    file_path_2 = "../data_generation/source2.csv"
    file_path_3 = "output.csv"

    data_1 = read_csv_file(file_path_1)
    data_2 = read_csv_file(file_path_2)

    processed_data_1, actual_1, id_1= preprocess_data(data_1)
    processed_data_2, actual_2, id_2= preprocess_data(data_2)

    model_file = "../analysis/pipeline.pkl"
    model = load_model(model_file)

    prediction_1 = predict_data(processed_data_1,model)
    prediction_2 = predict_data(processed_data_2,model)

    finaldata_1 = combine_data(id_1,processed_data_1,actual_1,prediction_1)
    finaldata_2 = combine_data(id_2,processed_data_2,actual_2,prediction_2)

    write_csv_file(file_path_3, finaldata_1)
    write_csv_file(file_path_3, finaldata_2)

# Run the flow
if __name__ == "__main__":
    for i in range(2000):
        process_data()
        time.sleep(5)


from soln_analysis import *

def plot_energy(ds_name, exp_names, save_path=None, skip=1):
    if save_path == 'auto':
        save_path = f'./figures/energy_{ds_name}'

    colors = ['b', 'g', 'orange', 'r', 'violet']
    for n, exp in enumerate(exp_names):
        try:
            print(exp)
            dir_path = get_timestamped_dir(load=True, base_dir=f'{ds_name}_{exp}')
            analysis = SolnAnalysis(dir_path)
            time, energy, recon = analysis.energy(skip=skip)
            plt.plot(time, energy, c=colors[n], ls='-', label=f'{exp}: energy')
            plt.plot(time, recon, c=colors[n], ls=':', label=f'{exp}: recon')
        except:
            raise

    plt.yscale('log')
    plt.legend()
    plt.ylabel('Energy')
    plt.xlabel('Time')
    if save_path is not None:
        plt.savefig(save_path)
    plt.show()

if __name__ == '__main__':
    #plot_energy('vh', ['asynch', '1T'], save_path='auto', skip=1)
    ds_name = 'vh_dim_8'
    exp = 'lsc'
    if exp == 'dsc':
        title = 'Discrete Sparse Coding'
    else:
        title = 'Langevin Sparse Coding'

    dir_path = get_timestamped_dir(load=True, base_dir=f'{ds_name}_{exp}')
    analysis = SolnAnalysis(dir_path)
    tau_u = analysis.solver_params['tau_u']
    l1 = analysis.model_params['l1']
    min_u = l1 / tau_u

    analysis.plot_nz_hist(title=title,last_frac=.1, log=False, s_max=6, n_bins=50, ylim='auto', eps_s = min_u)
    plt.show()
    #plt.savefig(f'./figures/{ds_name}_{exp}_nz_hist.pdf')


class calificaciones:
    califica = ["A", "B", "C", "D", "E"]
    Asignaturas = ["Matematicas", "Lengua", "Ingles", "Historia", "Fisica", "Quimica"]
    Alumno = ''
    dic = {}

    def __init__(self, alumno):
        self.Alumno = alumno
        self.media = 0
        self.count = 0
        self.contador = 0

    def setAlumno(self):
        self.Alumno = input("Alumno: ")

    def notas(self):
        self.dic[self.Alumno] = {"matematicas": int(input('Matematicas nota: ')), "Lengua": int(input('Lengua nota: ')),
                                 "Ingles": int(input('Ingles nota: ')),
                                 "Historia": int(input('Historia nota: ')), "Fisica": int(input('Fisica nota: ')),
                                 "quimica": int(input('Quimica nota: '))}

    def vernotas(self):
        for c, v in self.dic.items():
            for c, v in self.dic[c].items():
                self.count = self.count + v
                self.contador = self.contador + 1

        self.dic[self.Alumno]['sumaTotal'] = self.count
        self.dic[self.Alumno]['total'] = self.contador

    def medias(self):
        self.media = self.dic[self.Alumno]['sumaTotal'] / self.dic[self.Alumno]['total']
        if self.media < 5:
            print(self.media)
            print(self.califica[-1])
        elif self.media == 5:
            print(self.media)
            print(self.califica[-2])
        elif self.media == 6:
            print(self.media)
            print(self.califica[-3])
        elif 7 <= self.media <= 8:
            print(self.media)
            print(self.califica[-4])
        elif 9 <= self.media <= 10:
            print(self.media)
            print(self.califica[0])
        else:
            print("Fuera de limite")


# coding=utf-8
from gensim import corpora, models, similarities
import numpy as np
#要点：直接导入其中的名词进去
def run():
    #分词+选词
    cont = open('result/pure_n.txt','r',encoding='utf-8')
    nwordall = []
    for words in cont:
        ###########消除空格
        if words.strip() != '':
            real_word = words.strip().split(" ")
            nwordall.append(real_word)
        else:continue
   # 3. 选择后的词生成字典
    dictionary = corpora.Dictionary(nwordall)
    # 生成语料库
    corpus = [dictionary.doc2bow(text) for text in nwordall]
    tfidf = models.TfidfModel(corpus)
    corpus_tfidf = tfidf[corpus]
    #########开始训练模型
    print('LDA Model training:')
    # 设置主题的数目
    lda = models.LdaModel(corpus_tfidf,id2word=dictionary,num_topics=100)
    #存储model
    lda.save("model/lda_gensim")
    print('model saved')

if __name__ == '__main__':
    import time
    starttime1=time.clock()
    run()
    endtime1=time.clock()
    T1=endtime1-starttime1
    print('总用时间为(单位s)：%s'%T1)


from django.shortcuts import render, redirect, reverse, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.db.models import Q
from django.db.models.functions import Lower
from .forms import ProductForm
from .models import Product, Artist, Label, Genre, Format, Colour, Tag
import re
from django.contrib import messages

# Create your views here.

@login_required
def add_product(request):
    """ Add a product to the store """

    if request.method == 'POST':
        updated_request = request.POST.copy()
        updated_request = add_dynamically_created_data_to_the_database(
            updated_request)
        form = ProductForm(updated_request, request.FILES)
        if form.is_valid():
            form.save()
            messages.success(request, 'Successfully added product!')
            return redirect(reverse('view_products'))

    else:
        form = ProductForm()
        messages.info(
            request, 'You are about to add a new product to the store')

    template = 'products/add_product.html'
    context = {
        'form': form,
    }

    return render(request, template, context)

""" Parse the dynamic data from the Select2 Dynamic Dropdowns in
the Add Product form. First check if the data submitted is a foreign key,
if it is a foreign key(isnumeric) then it already exists in the
database and no action needs to be taken, if it isn't foreign key
then then create a new object and add it to the database. Then update the
request with the newly added data so that the form will validate correctly """

def add_dynamically_created_data_to_the_database(updated_request):

    artist_submitted_in_the_form = updated_request['artist']
    if(not artist_submitted_in_the_form.isnumeric()):
        # Create a new artist and insert it into the database
        new_artist_name = artist_submitted_in_the_form.lower()
        new_artist_name = new_artist_name.replace(" ", "_")
        new_artist_friendly_name = artist_submitted_in_the_form
        new_artist = Artist()
        new_artist.name = new_artist_name
        new_artist.friendly_name = new_artist_friendly_name
        new_artist.save()
        updated_request.update(
            {'artist': Artist.objects.get(name=new_artist.name).pk})

    label_submitted_in_the_form = updated_request['label']
    if(not label_submitted_in_the_form.isnumeric()):
        # Create a new label and insert it into the database
        new_label_name = label_submitted_in_the_form.lower()
        new_label_name = new_label_name.replace(" ", "_")
        new_label_friendly_name = label_submitted_in_the_form
        new_label = Label()
        new_label.name = new_label_name
        new_label.friendly_name = new_label_friendly_name
        new_label.save()
        updated_request.update(
            {'label': Label.objects.get(name=new_label.name).pk})

    genre_submitted_in_the_form = updated_request['genre']
    if(not genre_submitted_in_the_form.isnumeric()):
        # Create a new label and insert it into the database
        new_genre_name = genre_submitted_in_the_form.lower()
        new_genre_name = new_genre_name.replace(" ", "_")
        new_genre_friendly_name = genre_submitted_in_the_form
        new_genre = Genre()
        new_genre.name = new_genre_name
        new_genre.friendly_name = new_genre_friendly_name
        new_genre.save()
        updated_request.update(
            {'genre': Genre.objects.get(name=new_genre.name).pk})

    format_submitted_in_the_form = updated_request['format']
    if(not format_submitted_in_the_form.isnumeric()):
        # Create a new label and insert it into the database
        new_format_name = format_submitted_in_the_form.lower()
        new_format_name = new_format_name.replace(" ", "_")
        new_format_friendly_name = format_submitted_in_the_form
        new_format = Format()
        new_format.name = new_format_name
        new_format.friendly_name = new_format_friendly_name
        new_format.save()
        updated_request.update(
            {'format': Format.objects.get(name=new_format.name).pk})

    colour_submitted_in_the_form = updated_request['colour']
    if(not colour_submitted_in_the_form.isnumeric()):
        # Create a new label and insert it into the database
        new_colour_name = colour_submitted_in_the_form.lower()
        new_colour_name = new_colour_name.replace(" ", "_")
        new_colour_friendly_name = colour_submitted_in_the_form
        new_colour = Colour()
        new_colour.name = new_colour_name
        new_colour.friendly_name = new_colour_friendly_name
        new_colour.save()
        updated_request.update(
            {'colour': Colour.objects.get(name=new_colour.name).pk})

    tags_submitted_in_the_form = updated_request.getlist('tags')
    for tag in tags_submitted_in_the_form:
        if(not tag.isnumeric()):
            # Create a new label and insert it into the database
            new_tag_name = tag.lower()
            new_tag_name = new_tag_name.replace(" ", "_")
            new_tag_friendly_name = tag
            new_tag = Tag()
            new_tag.name = new_tag_name
            new_tag.friendly_name = new_tag_friendly_name
            new_tag.save()
            new_tag_index = tags_submitted_in_the_form.index(tag)
            tags_submitted_in_the_form[new_tag_index] = Tag.objects.get(
                name=new_tag.name).pk
            updated_request.setlist('tags', tags_submitted_in_the_form)

    return updated_request

def view_products(request):
    """ A view to show individual product details """

    products = Product.objects.all()

    search_query = None
    sort = None
    direction = None
    _filter = None
    genre_filters = {}
    artist_filters = {}
    label_filters = {}
    format_filters = {}
    colour_filters = {}
    tag_filters = {}
    min_price = 0.00
    max_price = 300.00

    if request.GET:
        # Product Sorting
        if 'sort' in request.GET:
            sortkey = request.GET['sort']
            sort = sortkey
            if sortkey == 'name':
                sortkey = 'lower_name'
                products = products.annotate(lower_name=Lower('name'))
            if 'direction' in request.GET:
                direction = request.GET['direction']
                if direction == 'desc':
                    sortkey = f'-{sortkey}'
            products = products.order_by(sortkey)
        # Filter Panel Multifilter
        if 'filter' in request.GET:
            _filter = Q()
            filters = request.GET.getlist('filter')
            for filter in filters:
                filter_type = (re.search('t_(.*)=', filter)).group(1)
                filter_value = (re.search('=(.*)', filter)).group(1)
                if filter_type == "genre":
                    genre_filters[filter_value] = filter_value
                    _filter.add((Q(genre__name=filter_value)),
                                _filter.connector)
                if filter_type == "artist":
                    _filter.add((Q(artist__name=filter_value)),
                                _filter.connector)
                    artist_filters[filter_value] = filter_value
                if filter_type == "label":
                    _filter.add((Q(label__name=filter_value)),
                                _filter.connector)
                    label_filters[filter_value] = filter_value
                if filter_type == "format":
                    _filter.add((Q(format__name=filter_value)),
                                _filter.connector)
                    format_filters[filter_value] = filter_value
                if filter_type == "colour":
                    _filter.add((Q(colour__name=filter_value)),
                                _filter.connector)
                    colour_filters[filter_value] = filter_value
                if filter_type == "tag":
                    _filter.add((Q(tags__name=filter_value)),
                                _filter.connector)
                    tag_filters[filter_value] = filter_value
            products = products.filter(_filter)
        # Only Products In Selected Price Range
        if 'price' in request.GET:
            price_range = request.GET['price']
            min_price = re.search('(.*)-', price_range).group(1)
            max_price = re.search('-(.*)', price_range).group(1)
            products = products.filter(price__range=(min_price, max_price))
        # Only Products By Selected Artist
        if 'artist' in request.GET:
            artist_request = request.GET['artist'].split(',')
            products = products.filter(artist__name__in=artist_request)
        # Only Products By Selected Label
        if 'label' in request.GET:
            label_request = request.GET['label'].split(',')
            products = products.filter(label__name__in=label_request)
        # Only Products By Selected Genre
        if 'genre' in request.GET:
            genre_request = request.GET['genre'].split(',')
            products = products.filter(genre__name__in=genre_request)
        # Only Products By Selected Tag
        if 'tag' in request.GET:
            tag_request = request.GET['tag'].split(',')
            products = products.filter(tags__name__in=tag_request)
        # Products matching search bar query
        if 'search_bar_main' in request.GET:
            search_query = request.GET['search_bar_main']
            if not search_query:
                return redirect(reverse('view_products'))

            search_queries = (Q(name__icontains=search_query) |
                              Q(artist__name__icontains=search_query) |
                              Q(label__name__icontains=search_query) |
                              Q(description__icontains=search_query) |
                              Q(tracklist__icontains=search_query)
                              )
            products = products.filter(search_queries)

    current_sorting = f'{sort}_{direction}'
    context = {
        'products': products,
        'genre_filters': genre_filters,
        'artist_filters': artist_filters,
        'label_filters': label_filters,
        'format_filters': format_filters,
        'colour_filters': colour_filters,
        'tag_filters': tag_filters,
        'min_price': min_price,
        'max_price': max_price,
        'current_sorting': current_sorting,
    }

    return render(request, 'products/view_products.html', context)

def view_product(request, product_id):
    """ A view to show individual product details """

    product = get_object_or_404(Product, pk=product_id)

    context = {
        'product': product,
    }

    return render(request, 'products/view_product.html', context)

@login_required
def edit_product(request, product_id):
    if not request.user.is_superuser:
        return redirect(reverse('home'))

    product = get_object_or_404(Product, pk=product_id)
    if request.method == 'POST':
        form = ProductForm(request.POST, request.FILES, instance=product)
        if form.is_valid():
            form.save()
            messages.success(request, 'Successfully updated product!')
            return redirect(reverse('view_product', args=[product.id]))

    else:
        form = ProductForm(instance=product)
        messages.info(request, f'You are editing {product}')

    template = 'products/edit_product.html'
    context = {
        'form': form,
        'product': product,
    }

    return render(request, template, context)

@login_required
def delete_product(request, product_id):
    """ Delete a product from the store """
    if not request.user.is_superuser:
        return redirect(reverse('home'))

    product = get_object_or_404(Product, pk=product_id)
    product.delete()
    messages.success(request, 'Product deleted!')
    return redirect(reverse('products'))


"""Test for py_wheel."""

load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
load("@rules_testing//lib:truth.bzl", "matching")
load("@rules_testing//lib:util.bzl", rt_util = "util")
load("//python:packaging.bzl", "py_wheel")
load("//tests/base_rules:util.bzl", pt_util = "util")

_tests = []

def _test_too_long_project_url_label(name, config):
    rt_util.helper_target(
        config.rule,
        name = name + "_wheel",
        distribution = name + "_wheel",
        python_tag = "py3",
        version = "0.0.1",
        project_urls = {"This is a label whose length is above the limit!": "www.example.com"},
    )
    analysis_test(
        name = name,
        target = name + "_wheel",
        impl = _test_too_long_project_url_label_impl,
        expect_failure = True,
    )

def _test_too_long_project_url_label_impl(env, target):
    env.expect.that_target(target).failures().contains_predicate(
        matching.str_matches("in `project_urls` is too long"),
    )

_tests.append(_test_too_long_project_url_label)

def py_wheel_test_suite(name):
    config = struct(rule = py_wheel, base_test_rule = py_wheel)
    native.test_suite(
        name = name,
        tests = pt_util.create_tests(_tests, config = config),
    )


import requests
from bs4 import BeautifulSoup

url = "https://parsinger.ru/html/index3_page_1.html"

responsive = requests.get(url=url)
responsive.encoding = "utf-8"

soup = BeautifulSoup(responsive.text, "lxml")

links = soup.find("div", attrs={"class": "pagen"}).find_all("a")
names = soup.find_all("a", attrs={"class": "name_item"})
# print([i.text for i in names])

number_of_pages = [int(i.text) for i in links]

res = []

for i in number_of_pages:
    url = f"https://parsinger.ru/html/index3_page_{i}.html"

    response = requests.get(url=url)
    response.encoding = "utf-8"
    soup = BeautifulSoup(response.text, "lxml")
    names = soup.find_all("a", attrs={"class": "name_item"})
    res.append([i.text for i in names])

print(res)


def match_pattern(pattern, value):
    size = len(value)
    main_char = pattern[0]
    alt_char = 'b' if main_char == 'a' else 'a'
    main_char_count = count_of(pattern, main_char)
    alt_char_count = len(pattern) - main_char_count
    max_size_main = size//main_char_count
    first_alt = pattern.find(alt_char)

    for i in range(0, max_size_main):
        remaining_length = size - i * main_char_count
        first = value[0:i]
        if alt_char_count == 0 or remaining_length % alt_char_count == 0:
            alt_index = first_alt * i
            alt_size = 0 if alt_char_count == 0 else remaining_length//alt_char_count
            second = value[alt_index: alt_size + alt_index]

            cand = get_string_from_pattern(pattern, first, second)
            if cand == value:
                return True

def get_string_from_pattern(pattern, first, second):
    final_str = ''
    for pat in pattern:
        if pat == 'a':
            final_str += first
        else:
            final_str += second
    return final_str

def count_of(pattern, main_chr):
    count = 0
    for pat in pattern:
        if pat == main_chr:
            count += 1
    return count

if match_pattern("aabab", "catcatgocatgo"):
    print("found")
else:
    print("not found")


import pandas as pd
import duckdb
import os
import gc
import numpy as np

EXPECTED_GRN_COLNAMES = ["regulator", "target", "weight"]

class LightNetwork:
  """
  The LightNetwork class is a lightweight, read-only interface to our database of weighted TF-target interactions.
  It supports just a few simple operations -- for now, just querying with a target and returning
  relevant TF's and weights.
  Under the hood, it uses duckdb to query parquet files, so data are not loaded into RAM.
  """
  def __init__(
    self,
    grn_name: str = None,
    subnetwork_names: list = [],
    files:list = [],
    df: pd.DataFrame = None,
  ):
    """Create a LightNetwork object.

    Args:
        grn_name (str, optional): source of networks, e.g. "celloracle_human"
        subnetwork_name (list, optional): filename for individual network (usually networks are grouped by cell/tissue type).
        files (list, optional): List of absolute paths to parquet files.
            These files will be queried in addition to the stuff already specified.
        df (pd.DataFrame, optional): This DF will be queried in addition to the stuff already specified.
    """
    if grn_name is None and len(subnetwork_names) > 0:
      raise ValueError("Cannot find subnetworks unless 'grn_name' is specified.")
    if grn_name is not None:
      if len(subnetwork_names) == 0:
        subnetwork_names = list_subnetworks(grn_name)
      files = files + [os.path.join(os.environ["GRN_PATH"], grn_name, "networks", s) for s in subnetwork_names]
    if len(files) == 0 and df is None:
      raise ValueError("Please provide at least one of 'grn_name' or 'files' of 'df'.")
    if df is not None:
      assert all(df.columns[0:3] == ['regulator', 'target', 'weight'] ), "Columns must have names 'regulator', 'target', 'weight'"
      assert len(df.columns)==3 or df.columns[3]=='cell_type', "If there are 4 columns, the 4th must be 'cell_type'."
    self.files = files
    self.df = df
    return

  def __str__(self):
      files_report = ""
      df_report = ""
      if len(self.files) > 0:
          files_report = "\n files: \n" + "\n".join(self.files)
      if self.df is not None:
          df_report = f"\n dataframe of shape {self.df.shape}"
      return "LightNetwork built on " + files_report + df_report

  def save(self, filename) -> None:
    if not filename.lower().endswith(".parquet"):
      raise ValueError("Filename must end with .parquet.")
    self.get_all().to_parquet(filename)
    return

  def get_all(self):
    results_from_parquet = pd.DataFrame()
    results_from_memory  = pd.DataFrame()
    if len(self.files) > 0:
      files_formatted = [f"'{file}'" for file in self.files]
      results_from_parquet = duckdb.query(
        " UNION ".join(
            [
              f"SELECT * FROM {file}" for file in files_formatted
            ]
          )
      ).df()
    if not self.df is None:
      con = duckdb.connect()
      df=self.df
      results_from_memory = con.execute(f"SELECT * FROM df").df()
    return pd.concat([results_from_parquet, results_from_memory])

  def get_regulators(self, target: str) -> pd.DataFrame:
    """Return all records having a given target gene.

    Args:
        target (str): A target gene present in this network.

    Returns:
        pd.DataFrame: All records with the given target gene
    """
    results_from_parquet = pd.DataFrame()
    results_from_memory  = pd.DataFrame()
    if len(self.files) > 0:
      files_formatted = [f"'{file}'" for file in self.files]
      results_from_parquet = duckdb.query(
        " UNION ".join(
            [
              f"SELECT * FROM {file} WHERE target = '{target}'" for file in files_formatted
            ]
          )
      ).df()
    if not self.df is None:
      con = duckdb.connect()
      df=self.df
      results_from_memory = con.execute(f"SELECT * FROM df WHERE target = '{target}'").df()
    return pd.concat([results_from_parquet, results_from_memory])

  def get_all_regulators(self) -> set:
    """Return a set of all regulators

    Returns:
        set: All distinct regulators
    """
    return self.get_all_one_field("regulator")

  def get_all_one_field(self, field: str) -> set:
    """Return a set of all regulators, or all targets, or all cell types.

    Returns:
        set: All distinct values listed in a given column from this network
    """
    assert field in {"regulator", "target", "cell_type"}, " Can only get unique vals for 'regulator', 'target', or 'cell_type' "
    results_from_parquet = pd.DataFrame(columns = [field])
    results_from_memory  = pd.DataFrame(columns = [field])
    if len(self.files) > 0:
      files_formatted = [f"'{file}'" for file in self.files]
      files_formatted = [
        file for file in files_formatted
        if field in set(duckdb.query(f"SELECT * FROM {file} WHERE 1=0").df().columns)
      ]
      if len(files_formatted) > 0:
        results_from_parquet = duckdb.query(
          " UNION ".join(
              [
                f"SELECT DISTINCT {field} FROM {file}" for file in files_formatted
              ]
            )
        ).df()
    if not self.df is None and field in set(self.df.columns):
      con = duckdb.connect()
      df=self.df
      results_from_memory = con.execute(f"SELECT DISTINCT {field} FROM df").df()
    return set(pd.concat([results_from_parquet, results_from_memory])[field].unique())

  def get_num_edges(self) -> int:
    """Return the number of available regulator-target connections.
    This assumes no overlap between edges recorded in 'files' and stuff provided in 'df'.

    Returns:
        int
    """
    results_from_parquet = 0
    results_from_memory  = 0
    if len(self.files) > 0:
      files_formatted = [f"'{file}'" for file in self.files]
      results_from_parquet = duckdb.query(
        "SELECT COUNT(*) FROM " + \
        "( " + \
          " UNION ".join(
              [
                f"SELECT * FROM {file}" for file in files_formatted
              ]
            ) + \
        " )"
      ).df().iloc[0,0]
    if not self.df is None:
      con = duckdb.connect()
      df=self.df
      results_from_memory = con.execute("SELECT COUNT(*) FROM df").df().iloc[0,0]
    return int(results_from_parquet + results_from_memory)

def load_grn_metadata( complete_only = True ):
  """Load metadata on GRN sources."""
  metadata_df = pd.read_csv(os.path.join(os.environ["GRN_PATH"], "published_networks.csv"))
  metadata_df.index = metadata_df["name"]
  if complete_only:
    metadata_df = metadata_df.loc[metadata_df['is_ready'] == "yes",:]
  return metadata_df

def list_subnetworks(grn_name: str):
  """
  List all tissues available from a given source.
  Parameters:
        - grn_name (string) source to list tissues from
  Return value: pd.DataFrame
  """
  return [f for f in os.listdir(os.path.join(os.environ["GRN_PATH"], grn_name, "networks")) if not f.startswith('.')]

def debug_grn_location(path=None):
  if path is None:
    path = get_grn_location()

  print(f"""
    GRN location is currently set to {path}.
    Set it using load_networks.set_grn_location().
    This package expects a folder structure like this:
    > tree networks
    networks
    ├── published_networks.csv
    ├── ANANSE_tissue_0.8
    │   └── networks
    │       ├── adrenal_gland.parquet
    │       ├── bone_marrow.parquet
    │       ├── brain.parquet
    │       ├── cervix.parquet
    |       ...
    ├── cellnet_human_Hg1332
    │   └── networks
    │       ├── bcell.parquet
    │       ├── colon.parquet
    │       ├── esc.parquet
    |       ...
""")
  print(f"Validating this structure is not yet automated but you can manually compare to `tree {path}`. ")
  return

def get_grn_location():
  return os.environ['GRN_PATH']

def set_grn_location(path: str):
  if not os.path.isfile(os.path.join(path, "published_networks.csv")):
    raise FileNotFoundError("published_networks.csv is missing. Try load_networks.debug_grn_location() for more help.")
  if not os.path.isfile(os.path.join(path, "cellnet_human_Hg1332", "networks", "bcell.parquet")):
    print("Checked for an example network and didn't find it; this is a bad sign. Try debug_grn_location() for more help.")
  os.environ['GRN_PATH'] = path
  return

def load_grn_by_subnetwork( grn_name: str, subnetwork_name: str ):
  """
  Load a given gene regulatory (sub-)network.
  Parameters:
        - grn_name (str): source to list tissues from
        - subnetwork_name (str): what subnetwork to use; see list_subnetworks(grn_name) for options
  """
  grn_location = os.path.join(get_grn_location(), grn_name, "networks", subnetwork_name)
  if not os.path.exists(grn_location):
    raise ValueError("Error locating grn! Please consult debug_grn_location().\n")

  X = pd.read_parquet( grn_location )

  # add score of -1 if missing
  if(X.shape[1] == 2):
    X["weight"] = -1

  X.set_axis(EXPECTED_GRN_COLNAMES, axis = 1, copy = False)
  # This saves mem for fat networks.
  X[EXPECTED_GRN_COLNAMES[0]].astype("category")
  X[EXPECTED_GRN_COLNAMES[1]].astype("category")
  return X

def load_grn_all_subnetworks(grn_name):
  return pd.concat(
      [load_grn_by_subnetwork(grn_name, s) for s in list_subnetworks(grn_name)]
   )

def validate_grn(
  grn_name,
  subnetwork_name,
  grn_df = None
):
  """Make sure grn conforms to the expected structure."""
  if grn_df is None:
    grn_df = load_grn_by_subnetwork( grn_name, subnetwork_name, do_validate=False )
  if len(grn_df[:, "regulator"].unique()) > len(grn_df[:, "target"].unique()):
    raise ValueError("".join([grn_name, " ", subnetwork_name, " has more regulators than targets!\n"]) )
  assert type(grn_df) == pd.DataFrame
  assert all( [grn_df.columns[i] == EXPECTED_GRN_COLNAMES[i] for i in range(3)])
  return True

def pivotNetworkLongToWide(networkEdges, regulatorColumn=0, targetColumn=1):
    """Reformat a network from a two-column dataframe to the way that celloracle needs its input.

    Args:
        network_long (pd.DataFrame): GRN structure with columns ['regulator', 'target', 'weight']
    """
    X = pd.crosstab(networkEdges.iloc[:,targetColumn], networkEdges.iloc[:,regulatorColumn])
    del networkEdges
    gc.collect()
    X = 1.0*(X > 0)
    X = X.rename_axis('gene_short_name').reset_index()
    X = X.rename_axis('peak_id').reset_index()
    gc.collect()
    return X

def pivotNetworkWideToLong(network_wide: pd.DataFrame):
    """Convert from CellOracle's preferred format to a triplet format

    Args:
        network_wide (pd.DataFrame): GRN structure in CellOracle's usual format
    """
    network_long = pd.concat([
        pd.DataFrame({
            "regulator": tf,
            "target": network_wide.loc[network_wide[tf]==1, "gene_short_name"],
            "weight": 1,
        })
        for tf in network_wide.columns[2:]
    ])
    return network_long

def makeRandomNetwork(targetGenes, TFs, density = 0, seed = 0):
    """Generate a random network formatted the way that celloracle needs its input."""
    np.random.seed(seed)
    X = pd.DataFrame(
            np.random.binomial(
                n = 1,
                p=density,
                size=(
                    len(targetGenes),
                    len(TFs)
                )
            ),
            columns = TFs,
            index = targetGenes
        )
    X.rename_axis('gene_short_name', inplace=True)
    X.reset_index(inplace=True)
    X.rename_axis('peak_id', inplace=True)
    X.reset_index(inplace=True)
    gc.collect()
    return X

def makeNetworkSparse(network_wide, defaultValue):
    """Save memory by making a sparse representation of a CellOracle-format base network"""
    network_wide.iloc[:,2:] = network_wide.iloc[:,2:].astype(pd.SparseDtype("float", defaultValue))
    return network_wide

def makeNetworkDense(network_wide):
    """Undo makeNetworkSparse"""
    network_wide.iloc[:, 2:] = np.array(network_wide.iloc[:, 2:])   #undo sparse representation
    return network_wide


import os
from pathlib import Path

from setuptools import setup
from setuptools.command.install import install

# The directory containing this file
HERE = Path(__file__).parent
README = (HERE / 'README.md').read_text()

with open('requirements.txt') as f:
    required = f.read().splitlines()

class InstallCommand(install):
    def run(self):
        install.run(self)
        os.system('mkdir -p ~/.local/share/koneko')
        os.system('cp -r ./pics/ ~/.local/share/koneko/')
        os.system('cp example_config.ini ~/.local/share/koneko/')
        os.system('cp pixiv-url.desktop ~/.local/share/koneko/')

setup(
    name='koneko',
    version='0.13.0',
    description='Browse pixiv in the terminal',
    long_description=README,
    long_description_content_type='text/markdown',
    url='https://github.com/akazukin5151/koneko',
    project_urls={
        'Source Code': 'https://github.com/akazukin5151/koneko',
        'Documentation': 'https://koneko.readthedocs.io/en/latest/',
    },
    author='akazukin5151',
    license='GPLv3',
    classifiers=[
        'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)',
        'Programming Language :: Python :: 3',
        'Operating System :: POSIX :: Linux',
        'Operating System :: MacOS',
        'Environment :: Console'
    ],
    packages=['koneko', 'koneko.url_login'],
    include_package_data=True,
    install_requires=required,
    tests_require=['pytest>=6.2,<8.0'],
    # Ueberzug's main repo no longer exists, use
    # https://github.com/WhiteBlackGoose/ueberzug-latest/
    # follow their installation instructions, or use your package manager
    #extras_require={'ueberzug': ['ueberzug~=18.1']},
    entry_points={
        'console_scripts': [
            'koneko=koneko.__main__:_main',
            'lscat=koneko.lscat_app:main',
            'koneko-url-login=koneko.url_login.main:main'
        ]
    },
    cmdclass={
        'install' : InstallCommand,
    },
)


# -*- coding: utf-8 -*-
from ff_optimum.cores.utilities import ConfigReader

from .simulated_annealing import (
    DominanceBasedMultiobjectiveSimulatedAnnealingOptimizer,
    SimulatedAnnealingOptimizer)

__all__ = ['OptimizerFactory']

class OptimizerFactory(object):

    """
    Class for creating optimizer object

    Methods
    -------
    create_optimizer_from_config(config_path)
    """

    @classmethod
    def create_optimizer_from_config(cls, config_path: str) -> object:
        """
        Parameters
        ----------
        config_path
              string contain the file path of the config file

        Returns
        -------
        Optimizer
              Optimizer object base on the config

        Raises
        ValueError
              If the algorithm is not found

        See Also
        --------
        config_reader.config_reader.ConfigReader
        """

        reader = ConfigReader(config_path)

        mosa = 'dominance_based_multiobjective_simulated_annealing'

        if mosa in reader.algorithm_name:
            return DominanceBasedMultiobjectiveSimulatedAnnealingOptimizer(
                reader.number_of_processors, reader.profile,
                reader.package_name, reader.package_settings,
                reader.param_initial_values,
                reader.constraints_source, reader.constraints_input,
                reader.commands_holder_train, reader.training_data,
                reader.plot_information, reader.alogrithm_parameters,
                reader.output_directory)

        elif 'simulated_annealing' in reader.algorithm_name:
            return SimulatedAnnealingOptimizer(
                reader.number_of_processors, reader.profile,
                reader.package_name, reader.package_settings,
                reader.param_initial_values,
                reader.constraints_source, reader.constraints_input,
                reader.commands_holder_train, reader.training_data,
                reader.plot_information, reader.alogrithm_parameters,
                reader.output_directory)

        else:
            raise ValueError('No such algorithm')


import pandas
import re
import csv
import dateutil.parser
import datetime

df = pandas.read_csv("../3lab/3lab.csv", delimiter = ",", index_col = [0], na_values = ['NA'], low_memory = False)

limiter = ['/','+',' ']

variablesSearch = [
  'javascript',
  'node.js',
  'html5',
  'reactjs'
  'backend',
  'frontend',
  'linux',
  'junior',
  'middle',
  'senior',
  'c++',
  'c#',
  'php',
  'asp.net',
  'ui',
  'ux',
  'sql',
  'vuejs',
  'angular',
  'unix',
  'java',
  'kotlin',
  'golang',
  'python'
]

variants = [
  ['big data','bigdata'],
  ['mssql','ms sql'],
  ['frontend', 'фронтенд'],
  ['backend', 'бэкенд'],
  ['developer', 'разработчик'],
  ['analyst','аналитик'],
  ['fullstack', 'фулл-стек'],
  ['programmer', 'программист'],
  ['designer', 'дизайнер'],
  ['manager', 'менеджер'],
  ['junior', 'джуниор'],
  ['middle','миддл'],
  ['senior', 'сеньор'],
  ['teamlead','тимлид'],
  ['js', 'javascript'],
  ['web','веб'],
  ['devops', 'девопс']
]

delete = ['удаленно','remote','фултайм','fulltime','(\(.*\))','(\[.*\])','\s+в\s+.*','г\.\s+.*']

print("Заменяем знаки в столбце наименования вакансий")

df["Name"] = df["Name"].apply(lambda x: x.lower()) # в нижний регистр
df["Name"] = df["Name"].apply(lambda x: x.replace('\\','/')) # замена символов
df["Name"] = df["Name"].apply(lambda x: x.replace('|','/'))
df["Name"] = df["Name"].apply(lambda x: x.replace(',','/'))
df["Name"] = df["Name"].apply(lambda x: x.replace('-',' '))
df["Name"] = df["Name"].apply(lambda x: re.sub('\s*\/\s*', '/', x)) # поиск и замена
df["Name"] = df["Name"].apply(lambda x: re.sub('\s+', ' ', x).strip()) # поиск и замена с удалением пробелов

print("Удаляем найденные слова")

for d in delete:
    regex = re.compile(d)
    df["Name"] = df["Name"].apply(lambda x: regex.sub('', x))

print("Замена слов")

for v in variants:
    for i in range(1, len(v)):
        df["Name"] = df["Name"].apply(lambda x: x.replace(v[i],v[0]))

print("Перестановка различных вариантов в названии")

for i in range(len(variablesSearch)):
    for j in range(i+1, len(variablesSearch)):
        for d in limiter:
            regex = re.compile(re.escape(variablesSearch[j])+'\s*'+re.escape(d)+'\s*'+re.escape(variablesSearch[i]))
            df["Name"] = df["Name"].apply(lambda x: regex.sub(variablesSearch[i]+limiter[0]+variablesSearch[j], x))

df["Name"] = df["Name"].apply(lambda x: re.sub('\s+', ' ', x).strip())

print("Замена пустых значений")

df["companyName"] = df["companyName"].fillna("Не указано")
df["city"] = df["city"].fillna("Не указан")
df["expierence"] = df["expierence"].fillna("Не требуется")
df["employment"] = df["employment"].fillna("Любой тип")
df["schedule"] = df["schedule"].fillna("Любой график")
df["responsibility"] = df["responsibility"].fillna("Не требуются")
df["requirement"] = df["requirement"].fillna("Не требуются")
df["keySkill"] = df["keySkill"].fillna("Не требуются")

df["minSalary"] = df.groupby(["Name", "city"]).transform(lambda x: x.fillna(x.mean()))["minSalary"]
df["maxSalary"] = df.groupby(["Name", "city"]).transform(lambda x: x.fillna(x.mean()))["maxSalary"]

print("Добавим новый признак: 'Количество дней с момента размещения' ")

df["Published at"] = df["Published at"].apply(lambda x: (datetime.datetime.now() - dateutil.parser.parse(x).replace(tzinfo=None)).days)

print("Сохранение")

df.to_csv("lab4.csv",  na_rep = 'NA', index = True, index_label = "", quotechar = '"', quoting = csv.QUOTE_NONNUMERIC, encoding = "utf-8-sig")

print("Готово")


# coding: utf-8

# In[2]:

import torch
import torch.utils.data as data
import torchvision
from PIL import Image
import os
import os.path
import bisect
import warnings

from torch._utils import _accumulate
from torchvision import datasets, models, transforms
IMG_EXTENSIONS = [
    '.jpg', '.JPG', '.jpeg', '.JPEG',
    '.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP',
]

def is_image_file(filename):
    return any(filename.endswith(extension) for extension in IMG_EXTENSIONS)

def find_classes(dir):
    classes = [d for d in os.listdir(dir) if os.path.isdir(os.path.join(dir, d))]
    classes.sort()
    class_to_idx = {classes[i]: i for i in range(len(classes))}
    return classes, class_to_idx

def pil_loader(path):
    # open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
    with open(path, 'rb') as f:
        with Image.open(f) as img:
            return img.convert('RGB')

def accimage_loader(path):
    import accimage
    try:
        return accimage.Image(path)
    except IOError:
        # Potentially a decoding problem, fall back to PIL.Image
        return pil_loader(path)

def default_loader(path):
    from torchvision import get_image_backend
    if get_image_backend() == 'accimage':
        return accimage_loader(path)
    else:
        return pil_loader(path)

def make_dataset(dir, class_to_idx,file_list):
    images = []
    #print('here')
    dir = os.path.expanduser(dir)
    set_files = [line.rstrip('\n') for line in open(file_list)]
    for target in sorted(os.listdir(dir)):
        #print(target)
        d = os.path.join(dir, target)
        if not os.path.isdir(d):
            continue

        for root, _, fnames in sorted(os.walk(d)):
            for fname in sorted(fnames):
                if is_image_file(fname):
                    dir_file=target+'/'+fname;
                    #print(dir_file)
                    if dir_file in set_files:
                        path = os.path.join(root, fname)
                        item = (path, class_to_idx[target])
                        images.append(item)

    return images

def default_loader(path):
    from torchvision import get_image_backend
    if get_image_backend() == 'accimage':
        return accimage_loader(path)
    else:
        return pil_loader(path)

class ImageFolderTrainVal(datasets.ImageFolder):
    """A generic data loader where the images are arranged in this way: ::
        root/dog/xxx.png
        root/dog/xxy.png
        root/dog/xxz.png
        root/cat/123.png
        root/cat/nsdf3.png
        root/cat/asd932_.png
    Args:
        root (string): Root directory path.
        transform (callable, optional): A function/transform that  takes in an PIL image
            and returns a transformed version. E.g, ``transforms.RandomCrop``
        target_transform (callable, optional): A function/transform that takes in the
            target and transforms it.
        loader (callable, optional): A function to load an image given its path.
     Attributes:
        classes (list): List of the class names.
        class_to_idx (dict): Dict with items (class_name, class_index).
        imgs (list): List of (image path, class_index) tuples
    #UPDATE,4thJul18:
            If calsses is given, other classes in the folder will be ignored
    """

    def __init__(self, root,files_list, transform=None, target_transform=None,
                 loader=default_loader,classes=None):
        if classes is  None:
            classes, class_to_idx = find_classes(root)
        else:
            class_to_idx = {classes[i]: i for i in range(len(classes))}
        print(root)
        imgs = make_dataset(root, class_to_idx,files_list)
        if len(imgs) == 0:
            raise(RuntimeError("Found 0 images in subfolders of: " + root + "\n"
                               "Supported image extensions are: " + ",".join(IMG_EXTENSIONS)))

        self.root = root
        self.samples = imgs
        self.classes = classes
        self.class_to_idx = class_to_idx
        self.transform = transform
        self.target_transform = target_transform
        self.loader = loader

# Split a dataset into train and val randomly:
from itertools import accumulate

class ImageFolder_Subset(ImageFolderTrainVal):
    def __init__(self, dataset, indices):
        self.dataset = dataset
        self.classes=dataset.classes
        self.indices = indices
        self.class_to_idx =dataset.class_to_idx
        self.root = dataset.root
        self.loader = dataset.loader
        self.transform=dataset.transform
        self.target_transform =dataset.target_transform
    def __getitem__(self, idx):
        return self.dataset[self.indices[idx]]

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

def random_split(dataset, lengths):
    assert sum(lengths) == len(dataset)
    indices = torch.randperm(sum(lengths))
    return [ImageFolder_Subset(dataset, indices[offset - length:offset]) for offset, length in zip(accumulate(lengths), lengths)]

class ConcatDatasetLabels( torch.utils.data.ConcatDataset):
    """
    Dataset to concatenate multiple datasets.
    Purpose: useful to assemble different existing datasets, possibly
    large-scale datasets as the concatenation operation is done in an
    on-the-fly manner.
    Arguments:
        datasets (sequence): List of datasets to be concatenated
        the output labels are shifted by the dataset index which differs from the pytorch implementation that return the original labels
    """

    def __init__(self, datasets,classes_len):
        super(ConcatDatasetLabels, self).__init__(datasets)
        self.cumulative_classes_len =list(accumulate (classes_len)  )
    def __getitem__(self, idx):
        dataset_idx = bisect.bisect_right(self.cumulative_sizes, idx)
        if dataset_idx == 0:
            sample_idx = idx
            img,label= self.datasets[dataset_idx][sample_idx]
        else:
            sample_idx = idx - self.cumulative_sizes[dataset_idx - 1]
            img,label= self.datasets[dataset_idx][sample_idx]
            label=label+self.cumulative_classes_len[dataset_idx-1]
        return img,label


from datetime import datetime
from flask_restful import Resource, reqparse
from models.expense import ExpenseModel
from flask import request
from datetime import datetime
import json
import hashlib

class ExpenseByProjectID(Resource):
    parser = reqparse.RequestParser()
    parser.add_argument('project_id',type=int,required=True,help="This field cannot be blank.")
    parser.add_argument('category_id',type=int,required=False,help="This field cannot be blank.")
    parser.add_argument('name',type=str,required=True,help="This field cannot be blank.")
    parser.add_argument('description',type=str,required=False,help="This field cannot be blank.")
    parser.add_argument('amount',type=float,required=True,help="This field cannot be blank.")
    parser.add_argument('created_at',type=datetime,required=False,help="This field cannot be blank.")
    parser.add_argument('created_by',type=str,required=False,help="This field cannot be blank.")
    parser.add_argument('updated_at',type=datetime,required=False,help="This field cannot be blank.")
    parser.add_argument('updated_by',type=str,required=False,help="This field cannot be blank.")

    def get(self, project_id):
        expenses = ExpenseModel.find_by_project_id(project_id)
        if expenses:
            return [expense.json() for expense in expenses]
        return {'message': 'No expenses found for this project'}, 200

class Expense(Resource):
    def get(self):
        expenses = ExpenseModel.query.all()
        return [expense.json() for expense in expenses], 200

    def post(self):
        data = ExpenseByProjectID.parser.parse_args()
        data['category_id'] = 2
        data['description'] = 'Server maintenance and upgrading work to incorporate BC plans'
        expense = ExpenseModel(data['project_id'], data['category_id'], data['name'], data['description'], data['amount'], data['created_at'], data['created_by'], data['updated_at'], data['updated_by'])
        expense.save_to_db()

        return {'message': 'Expense successfully created'}, 201

    def put(self):
        data = request.get_json()
        expense_id = data.get('expense_id')
        project_id = data.get('project_id')
        category_id = data.get('category_id')
        name = data.get('name')
        description = data.get('description')
        amount = data.get('amount')
        updated_by = data.get('updated_by')
        for compulsory in [expense_id, project_id, category_id, name, description, amount, updated_by]:
            if compulsory == None:
                return {'message': f"Missing field."}
        # if expense_id == None or project_id == None or category_id == None or name == None or description == None or amount == None or updated_by == None:
        #     return {'message': ''}
        expense = ExpenseModel.find_by_expense_id(expense_id)
        try:
            if expense:
                expense.project_id = project_id
                expense.category_id = category_id
                expense.name=name
                expense.description = description
                expense.amount = amount
                expense.updated_at = datetime.now()
                expense.updated_by = updated_by
                expense.save_to_db()
            else:
                return {'message': 'Expense not found'}, 404
        except Exception as e:
            return {'message': str(e)}
        return {'message': 'Expense successfully updated'}, 200

class ExpensesByExpenseID(Resource):
    def delete(self, expense_id):
        expense = ExpenseModel.find_by_expense_id(expense_id)
        if expense:
            ExpenseModel.deleteExpense(expense)
            return {'message': 'Expense successfully deleted'}, 200
        return {'message': 'Expense not found'}, 404


import os

label_list = ['확진자수','완치자수','사망여부','집단감염','백신관련','방역지침','경제지원','마스크','국제기구','병원관련']

os.system('echo cross_entropy >> multi_log.log')
for lab in label_list:
    os.system('python run_one_task_multi.py %s >> multi_log.log' % lab)

os.system('echo focal_loss >> multi_log.log')
for lab in label_list:
    os.system('python run_one_task_multi_focal.py %s >> multi_log.log' % lab)


"""This module implements calculation of acceleration constraints.
"""

import math
import numpy as np

class AccelerationConstraints:
    """Calculates lower and upper constraint for commanded acceleration by RL-FOC.

    Different types of constraints can be specified by parameters.
    """

    def __init__(self, opts):
        """Initializes constraints by supplied options.

        Args:
            opts: Namespace object with options.
        """
        self.opts = opts
        self.dt = self.opts.env_dt

        self.constraint_lower = opts.constraints_lower.replace(" ", "").split("+")
        self.constraint_upper = opts.constraints_upper.replace(" ", "").split("+")

        assert opts.cstr_no_backwards_jerk > 0, "cstr_no_backwards_jerk has to be a positive number!"
        assert opts.cstr_no_crash_tg_min > 0, "cstr_no_crash_tg_min has to be a positve nubmer!"
        assert opts.cstr_no_crash_a_min < 0, "cstr_no_crash_a_min has to be a negative number!"
        assert opts.cstr_no_crash_jerk > 0, "cstr_no_crash_jerk has to be a positive number!"

    def calculate(self, state):
        """Calcuate constraints for current state.

        Args:
            state: State dict containing all signals.

        Returns:
            Tuple containing minimal and maximum acceleration (a_min, a_max).
        """
        a_min = self.lower(state)
        a_max = self.upper(state)

        # a_max may not be lower than a_min
        if a_max < a_min:
            a_max = a_min

        return (a_min, a_max)

    def lower(self, state):
        """Calculates lower constraint.

        Args:
            state: state of AccFocEnv containing all relevant measurements.

        Returns:
            lower bound for acceleration in m/s^2
        """
        a = self.opts.vehicle_a_min
        if "iso" in self.constraint_lower:
            a = max(a, self._iso_15622_acceleration_lower_limit(state["v_ego"]))
        if "no_backwards" in self.constraint_lower:
            # Approximate lagging vehicle behaviour with a dead time and assume a_ego to be constant during this time.
            T_deadtime = 0.5
            v = max(0, state["v_ego"] + min(0, state["a_ego"]) * T_deadtime)
            a = max(a, -math.sqrt(2 * v * self.opts.cstr_no_backwards_jerk))

        # minimal acceleration of car is -4.5 m/s^2
        a = min(a, 0)
        a = max(a, self.opts.vehicle_a_min)

        return a

    def upper(self, state):
        """Calculates upper constraint.

        Args:
            state: state of AccFocEnv containing all relevant measurements.

        Returns:
            upper bound for acceleration in m/s^2

        """
        a = self.opts.vehicle_a_max
        if "no_crash" in self.constraint_upper:
            a = self._no_crash_acceleration_upper_limit(state, compensate_lag=True, stop_n_go=True)
            a = max(self.opts.cstr_no_crash_a_min, a)

        # limit maximal acceleration to system limit of 3 m/s^2
        a = max(a, self.opts.vehicle_a_min)
        a = min(a, self.opts.vehicle_a_max)

        return a

    def _iso_15622_acceleration_lower_limit(self, v):
        """Lowest allowd acceleration (highes deceleration) according ISO 15622

        Args:
            v: Velocity of vehicle in m/s.

        Returns:
            Minimum allowd acceleration in m/s^2

        """
        if v < 5:
            a_iso = -5.0
        elif 5 <= v <= 20:
            a_iso = -5.0 + (v - 5.0) / 10.0
        else:
            a_iso = -3.5

        return a_iso

    def _no_crash_acceleration_upper_limit(self, state, compensate_lag=True, stop_n_go=True):
        """Calculate upper bound to satisfy safety criteria.

        Args:
            state: Current state.
            compensate_lag: Compensate lagging behaviour of ego vehicle.
            stop_n_go: Consider Stop&Go scenarios and increase required distance by stop&go distance.

        Returns:
            Maximum allowed acceleration to prevent undershooting of minimal time gap.
        """
        # Extract needed values from state.
        v_ego = state["v_ego"]
        a_ego = state["a_ego"]
        v_tar = state["v_tar"]
        a_tar = state["a_tar"]
        x_rel = state["x_rel"]
        v_rel = state["v_rel"]

        # Reduce relative distance by desired standstill distance.
        if stop_n_go:
            x_rel -= self.opts.stop_n_go_distance

        # If the time gap has fallen below the minimum value (e.g. due to a cut-in vehicle) apply maximum braking.
        if x_rel < self.opts.cstr_no_crash_tg_min * v_ego:
            return self.opts.vehicle_a_min

        # Compensate lagging dynamics of ego vehicle.
        if compensate_lag:
            T_deadtime = 0.5
            v_ego += a_ego * T_deadtime
            v_tar = v_tar + a_tar * T_deadtime
            v_rel = v_tar - v_ego
            x_rel = x_rel + (state["v_tar"] - state["v_ego"]) * T_deadtime + 0.5 * (a_tar - a_ego) * T_deadtime**2

        # Extract constraint parameter
        tg_min = self.opts.cstr_no_crash_tg_min
        a_min = self.opts.cstr_no_crash_a_min
        j_ego = -self.opts.cstr_no_crash_jerk

        # calculate relative acceleration at the critical point
        a_rel_k = a_tar - a_min
        if a_rel_k == 0.0:
            a_rel_k = 0.00001

        # define coefficents of the control equation
        coeff = np.zeros(5)
        coeff[0] = -0.25 * j_ego**2
        coeff[1] = -1/3 * j_ego * a_rel_k
        coeff[2] = j_ego * (a_tar * tg_min - v_rel)
        coeff[3] = 0
        coeff[4] = x_rel * a_rel_k * 2 - tg_min * (v_ego * a_rel_k - v_rel * a_min) * 2 - v_rel**2

        try:
            sol_t_krit = np.roots(coeff)
        except:
            print(coeff)
            print(a_rel_k)
            print(a_min)
            print(v_rel)
            print(tg_min)

        # Get valid solution (real and >0)
        real_sol_t_krit = sol_t_krit[np.isreal(sol_t_krit)]
        if len(real_sol_t_krit) > 0:
            t_krit = float(np.real(np.max(real_sol_t_krit)))
        else:
            return self.opts.cstr_no_crash_a_min

        # Calculate upper constraint
        constr_high_1 = min(self.opts.vehicle_a_max, a_min - j_ego * t_krit)

        return constr_high_1


from concurrent import futures
import grpc
import demo_pb2
import demo_pb2_grpc

class TestServicer(demo_pb2_grpc.TestServicer):
    def SendRequest(self, request, context):
        # return super().SendRequest(request, context)
        print(f"received request: {request.message} with {request.counter}")
        return demo_pb2.TestReply(message="reply from python", counter=request.counter+1)

    def ClientStream(self, request_iterator, context):
        print(f"received stream: {request_iterator}")
        total = 0
        cnt = 0
        for i in request_iterator:
            print(f"\treceived next NumberStream obj {i}", end="")
            total += i.number
            cnt += 1

        return demo_pb2.TestReply(message=f"reply from python, received {cnt} numbers", counter=total)

def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    demo_pb2_grpc.add_TestServicer_to_server(TestServicer(), server)
    server.add_insecure_port('[::]:50051')
    server.start()
    server.wait_for_termination()

if __name__ == '__main__':
    print("starting server")
    serve()


class Solution:
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str
        """
        def palindrome(s, left, right):
            while left>=0 and right<len(s) and s[left]==s[right]:
                left-=1
                right+=1
            return s[left+1:right]
        ans=''
        for i in range(len(s)):
            pal1=palindrome(s, i, i)
            if len(pal1)>len(ans):
                ans=pal1
            pal2=palindrome(s, i, i+1)
            if len(pal2)>len(ans):
                ans=pal2
        return ans


import bpy
import json
import os
import numpy as np
from mathutils import *; from math import *

RESOLUTION = 512
SAMPLES = 64
BOUNCES = 1
BASE_PATH = "C:/Users/Elia/Desktop/nvdiffrecmc_relight"

os.makedirs(os.path.join(BASE_PATH, "our_relit"), exist_ok=True)

scene = bpy.context.scene
scene.world.use_nodes = True
scene.render.engine = 'CYCLES'
scene.render.film_transparent = True
scene.cycles.samples = SAMPLES
scene.cycles.max_bounces = BOUNCES
scene.cycles.diffuse_bounces = BOUNCES
scene.cycles.glossy_bounces = BOUNCES
scene.cycles.transmission_bounces = 0
scene.cycles.volume_bounces = 0
scene.cycles.transparent_max_bounces = 8
scene.cycles.use_denoising = True

scene.render.resolution_x = RESOLUTION
scene.render.resolution_y = RESOLUTION
scene.render.resolution_percentage = 100

# OpenEXR output, should not be tonemapped
scene.display_settings.display_device = 'sRGB'
scene.view_settings.view_transform = 'Standard'
scene.view_settings.exposure = 0.0
scene.view_settings.gamma = 1.0
scene.render.image_settings.file_format = 'PNG'

scene.view_layers['ViewLayer'].use_pass_combined=True
scene.view_layers['ViewLayer'].use_pass_diffuse_direct=True
scene.view_layers['ViewLayer'].use_pass_diffuse_color=True
scene.view_layers['ViewLayer'].use_pass_glossy_direct=True
scene.view_layers['ViewLayer'].use_pass_glossy_color=True

def relight_scene(SCENE, POSES, alpha_transparency=False):
    MESH_PATH = os.path.join(BASE_PATH, "our_meshes", SCENE, "mesh")
    PROBE_PATH = os.path.join(BASE_PATH, "nerfactor_data", "light_probes", "test")
    RESULTS_PATH = os.path.join(BASE_PATH, "our_relit", SCENE)

    os.makedirs(RESULTS_PATH, exist_ok=True)

    bpy.ops.object.select_all(action="SELECT")
    bpy.ops.object.delete()

    ################### Import obj mesh ###################

    imported_object = bpy.ops.import_scene.obj(filepath=os.path.join(MESH_PATH, "mesh.obj"))
    obj_object = bpy.context.selected_objects[0]
    obj_object.rotation_euler = [np.pi*0.5, 0, 0]

    ################### Fix material graph ###################

    # Get material node tree, find BSDF and specular texture
    material = obj_object.active_material
    bsdf = material.node_tree.nodes["Principled BSDF"]
    image_node_ks = bsdf.inputs["Specular"].links[0].from_node

    # Split the specular texture into metalness and roughness
    separate_node = material.node_tree.nodes.new(type="ShaderNodeSeparateRGB")
    separate_node.name="SeparateKs"
    material.node_tree.links.new(image_node_ks.outputs[0], separate_node.inputs[0])
    material.node_tree.links.new(separate_node.outputs[2], bsdf.inputs["Metallic"])
    material.node_tree.links.new(separate_node.outputs[1], bsdf.inputs["Roughness"])

    material.node_tree.links.remove(bsdf.inputs["Specular"].links[0])

    # Add alpha channel if applicable
    if alpha_transparency:
        image_node_kd = bsdf.inputs["Base Color"].links[0].from_node
        material.node_tree.links.new(image_node_kd.outputs[1], bsdf.inputs["Alpha"])

    normal_map_node = bsdf.inputs["Normal"].links[0].from_node
    texture_n_node = normal_map_node.inputs["Color"].links[0].from_node
    material.node_tree.links.remove(normal_map_node.inputs["Color"].links[0])

    normal_separate_node = material.node_tree.nodes.new(type="ShaderNodeSeparateRGB")
    normal_separate_node.name="SeparateNormal"

    normal_combine_node = material.node_tree.nodes.new(type="ShaderNodeCombineRGB")
    normal_combine_node.name="CombineNormal"

    normal_invert_node = material.node_tree.nodes.new(type="ShaderNodeMath")
    normal_invert_node.name="InvertNormal"
    normal_invert_node.operation='SUBTRACT'
    normal_invert_node.inputs[0].default_value = 1.0

    material.node_tree.links.new(texture_n_node.outputs[0], normal_separate_node.inputs['Image'])
    material.node_tree.links.new(normal_separate_node.outputs['R'], normal_combine_node.inputs['R'])

    material.node_tree.links.new(normal_separate_node.outputs['G'], normal_invert_node.inputs[1])
    material.node_tree.links.new(normal_invert_node.outputs[0], normal_combine_node.inputs['G'])

    material.node_tree.links.new(normal_separate_node.outputs['B'], normal_combine_node.inputs['B'])
    material.node_tree.links.new(normal_combine_node.outputs[0], normal_map_node.inputs["Color"])

    # Set default values
    bsdf.inputs["Specular"].default_value = 0.5
    bsdf.inputs["Specular Tint"].default_value = 0.0
    bsdf.inputs["Sheen Tint"].default_value = 0.0
    bsdf.inputs["Clearcoat Roughness"].default_value = 0.0

    ################### Load HDR probe ###################

    # Common setup
    val_cfg = json.load(open(os.path.join(BASE_PATH, "nerfactor_data", POSES)))

    camera_data   = bpy.data.cameras.new(name='Camera')
    camera_object = bpy.data.objects.new('Camera', camera_data)
    scene.collection.objects.link(camera_object)
    scene.camera  = camera_object
    camera_data.angle = val_cfg['camera_angle_x']

    # load all probes
    probes = [
        "city",
        "courtyard",
        "forest"
    ]

    envmap = scene.world.node_tree.nodes.new(type="ShaderNodeTexEnvironment")
    envmap.image = bpy.data.images.load(os.path.join(MESH_PATH, "probe.hdr"))
    scene.world.node_tree.links.new(envmap.outputs[0], scene.world.node_tree.nodes['Background'].inputs['Color'])

    for i in range(8):
        mtx = np.array(val_cfg['frames'][i]['transform_matrix'], dtype=np.float32)
        scene.camera.matrix_world = Matrix(mtx)
        scene.render.filepath = os.path.join(RESULTS_PATH, 'learned_r_{0:03d}'.format(int(i)))
        bpy.ops.render.render(write_still=True)  # render still

    for p in probes:
        envmap = scene.world.node_tree.nodes.new(type="ShaderNodeTexEnvironment")
        envmap.image = bpy.data.images.load(os.path.join(PROBE_PATH, p + ".hdr"))
        scene.world.node_tree.links.new(envmap.outputs[0], scene.world.node_tree.nodes['Background'].inputs['Color'])

        for i in range(8):
            mtx = np.array(val_cfg['frames'][i]['transform_matrix'], dtype=np.float32)
            scene.camera.matrix_world = Matrix(mtx)
            scene.render.filepath = os.path.join(RESULTS_PATH, p + '_r_{0:03d}'.format(int(i)))
            bpy.ops.render.render(write_still=True)  # render still

relight_scene(SCENE="nerfactor_hotdog", POSES="hotdog_transforms_val.json")
relight_scene(SCENE="nerfactor_lego", POSES="lego_transforms_val.json")
relight_scene(SCENE="nerfactor_ficus", POSES="ficus_transforms_val.json", alpha_transparency=True)
relight_scene(SCENE="nerfactor_drums", POSES="drums_transforms_val.json", alpha_transparency=True)


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

#下载一个 网页
url ='http://www.17k.com/list/2932117.html'
# 定义要爬虫到的章节, 15表示只爬到15章
chapter_num = 28
#模拟浏览器发送http请求
response = requests.get(url)
response.encoding ='utf-8'
html = response.text
#小说标题
tittle = re.findall(r'
(.*?)\n.*?\n\s{60}(.*?)\s{52}<', dd)
chapter_info_list = re.findall(r'href="(.*?)"\n.*?>\n.*?\n\t{17}(.*?)\n.*?\n.*?', dd)

#新建文件保存小说内容
#with open('%s.txt' % tittle) as f:
#循环每个章节，分别下载
i = 1
for chapter_info in chapter_info_list:
    if  i <= chapter_num:
        print('正在爬第'+str(i)+'章')
        # chapter_tittle = chapter_info[1]
        # chapter_url = chapter_info[0]
        chapter_url, chapter_tittle = chapter_info
        chapter_url ="http://www.17k.com%s" % chapter_url
        # print(chapter_url)
        #print(chapter_url, chapter_tittle)
        #下载章节内容
        chapter_response = requests.get(chapter_url)
        chapter_response.encoding ='utf-8'
        chapter_html = chapter_response.text
        #读取章节内容
        chapter_content = re.findall(r'
(.*?)
', chapter_html, re.S)[0]
        #清洗数据
        chapter_content = chapter_content.replace(' ', '')
        chapter_content = chapter_content.replace('　', '')
        chapter_content = chapter_content.replace('
', '')
        chapter_content = chapter_content.replace('
', '')
        chapter_content = chapter_content.replace('
', '')
        chapter_content = chapter_content.replace('\t', '')
        #持久化
        fb.write(chapter_tittle)
        fb.write(chapter_content)
        fb.write('\n')
        i = i + 1

    else:
        break
print(chapter_url, chapter_tittle)

#exit()


from tkinter import *

root=Tk()

MiFrame=Frame(root, width=500, height=400)

MiFrame.pack()

miImagen=PhotoImage(file="kono.png")
Label(MiFrame, image=miImagen).place(x=100, y=200)

#Label(MiFrame, text="Hola mundo", fg="red", font=("Comic Sans MS", 18)).place(x=100, y=200)

#Al empaquetar el label, adapta el contenedor a las dimensiones del label
#Ubica el texto en cualquier lugar de la raiz mediante coordenadas

MiFrame.mainloop()


import os
from typing import Optional, Dict, Any, List

import pandas as pd

def load_model(path_of_directory: str) -> Dict[str, Any]:
    """
    Load Pln or PlnPCA model (that has previously been saved) from the given directory for future initialization.

    Parameters
    ----------
    path_of_directory : str
        The path to the directory containing the model.

    Returns
    -------
    Dict[str, Any]
        A dictionary containing the loaded model.
    Examples
    --------
    >>> from pyPLNmodels import PlnPCA, Pln, get_real_count_data, load_model
    >>> endog= get_real_count_data()
    >>> pca = PlnPCA(endog, add_const = True)
    >>> pca.fit()
    >>> pca.save()
    >>> dict_init = load_model("PlnPCA_nbcov_1_dim_200_rank_5")
    >>> loaded_pca = PlnPCA(endog, add_const = True, dict_initialization = dict_init)
    >>> print(loaded_pca)

    >>> pln = Pln(endog, add_const = True)
    >>> pln.fit()
    >>> pln.save()
    >>> dict_init = load_model("Pln_nbcov_1_dim_200")
    >>> loaded_pln = Pln(endog, add_const = True, dict_initialization = dict_init)
    >>> print(loaded_pln)
    See also
    --------
    :func:`~pyPLNmodels.load_plnpcacollection`
    """
    working_dir = os.getcwd()
    try:
        os.chdir(path_of_directory)
    except FileNotFoundError as err:
        raise err(
            "The model has not been saved. Please be sure you have the right name of model."
        )
    all_files = os.listdir()
    data = {}
    for filename in all_files:
        if filename.endswith(".csv"):
            parameter = filename[:-4]
            try:
                data[parameter] = pd.read_csv(filename, header=None).values
            except pd.errors.EmptyDataError:
                print(
                    f"Can't load {parameter} since empty. Standard initialization will be performed for this parameter"
                )
    os.chdir(working_dir)
    return data

def load_pln(path_of_directory: str) -> Dict[str, Any]:
    """
    Alias for :func:`~pyPLNmodels.load.load_model`.
    """
    return load_model(path_of_directory)

def load_plnpca(path_of_directory: str) -> Dict[str, Any]:
    """
    Alias for :func:`~pyPLNmodels.load.load_model`.
    """
    return load_model(path_of_directory)

def load_plnpcacollection(
    path_of_directory: str, ranks: Optional[List[int]] = None
) -> Dict[int, Dict[str, Any]]:
    """
    Load PlnPCAcollection models from the given directory.

    Parameters
    ----------
    path_of_directory : str
        The path to the directory containing the PlnPCAcollection models.
    ranks : List[int], optional
        A List of ranks specifying which models to load. If None, all models in the directory will be loaded.

    Returns
    -------
    Dict[int, Dict[str, Any]]
        A dictionary containing the loaded PlnPCAcollection models, with ranks as keys.

    Raises
    ------
    ValueError
        If an invalid model name is encountered and the rank cannot be determined.

    Examples
    --------
    >>> from pyPLNmodels import PlnPCAcollection, get_real_count_data, load_plnpcacollection
    >>> endog = get_real_count_data()
    >>> pcas = PlnPCAcollection(endog, add_const = True, ranks = [4,5,6])
    >>> pcas.fit()
    >>> pcas.save()
    >>> dict_init = load_plnpcacollection("PlnPCAcollection_nbcov_1_dim_200")
    >>> loaded_pcas = PlnPCAcollection(endog, add_const = True, ranks = [4,5,6], dict_of_dict_initialization = dict_init)
    >>> print(loaded_pcas)

    See also
    --------
    :func:`~pyPLNmodels.load_model`
    """
    working_dir = os.getcwd()
    os.chdir(path_of_directory)
    if ranks is None:
        dirnames = os.listdir()
        ranks = []
        for dirname in dirnames:
            try:
                rank = int(dirname[-1])
            except ValueError:
                raise ValueError(
                    f"Can't load the model {dirname}. End of {dirname} should be an int"
                )
            ranks.append(rank)
    datas = {}
    for rank in ranks:
        datas[rank] = load_model(f"PlnPCA_rank_{rank}")
    datas["ranks"] = ranks
    os.chdir(working_dir)
    return datas


from support.api import *
import time

# <===using api ,finding threat percentage upon scanning all major antivirus softwares====>

def connect(hash):
    try:
        v = Virustotal()
        results = v.rscReport(hash)
        return results["positives"]/results["total"]*100
    except KeyError:
        return 0
    except json.decoder.JSONDecodeError:
        time.sleep(60)
        return connect(hash)


import pytest
from sklearn import clone

from kyle.calibration.calibration_methods import (
    BetaCalibration,
    HistogramBinning,
    IsotonicRegression,
    LogisticCalibration,
    TemperatureScaling,
)
from kyle.metrics import ECE

@pytest.fixture(scope="module")
def metric():
    return ECE()

@pytest.fixture(scope="module")
def calibration_method():
    return TemperatureScaling()

@pytest.mark.parametrize(
    "calibration_method",
    [
        HistogramBinning(),
        TemperatureScaling(),
        IsotonicRegression(),
        BetaCalibration(),
        LogisticCalibration(),
    ],
)
def test_calibration_methods_clonability(calibration_method):
    clone(calibration_method)

def test_methods_calibrationErrorLessAfterCalibration(
    metric, uncalibrated_samples, calibration_method
):
    ground_truth, confidences = uncalibrated_samples
    error_pre_calibration = metric.compute(confidences, ground_truth)
    calibration_method.fit(confidences, ground_truth)
    calibrated_confidences = calibration_method.get_calibrated_confidences(confidences)
    error_post_calibration = metric.compute(calibrated_confidences, ground_truth)

    assert error_post_calibration <= error_pre_calibration


import redis
from django.conf import settings
from .models import Product

# connect to redis
r = redis.Redis(host=settings.REDIS_HOST,
                port=settings.REDIS_PORT,
                db=settings.REDIS_DB)

'''This is the Recommender class that will allow you to store product purchases and
retrieve product suggestions for a given product or products'''

class Recommender(object):
# The get_product_key() method receives an ID of a Product object and builds the Redis key for the sorted set where related products are stored, which looks like
# product id purchased_with.
    def get_product_key(self, id):
        return f'product:{id}:purchased_with'

# The products_bought() method receives a list of Product objects that have been bought together (that is, belong to the same order).
    def products_bought(self, products):
# You get the product IDs for the given Product objects.
        product_ids = [p.id for p in products]
# You iterate over the product IDs. For each ID, you iterate again over the product IDs and skip the same product so that you get the products that are bought together with each product.
        for product_id in product_ids:
            for with_id in product_ids:
# You get the Redis product key for each product bought using the get_product_id() method. For a product with an ID of 33, this method returns
# the key product:33:purchased_with. This is the key for the sorted set that contains the product IDs of products that were bought together with this one.
                # get the other products bought with each product
                if product_id != with_id:
# You increment the score of each product ID contained in the sorted set by 1. The score represents the times another product has been bought together
# with the given product
                    # increment score for product purchased together
                    r.zincrby(self.get_product_key(product_id),
                              1,
                              with_id)

# method to retrieve the products that were bought together for a list of given products
    def suggest_products_for(self, products, max_results=6):
        # You get the product IDs for the given Product objects.
        product_ids = [p.id for p in products]
# If only one product is given, you retrieve the ID of the products that were bought together with the given product, ordered by the total number of times
# that they were bought together. To do so, you use Redis' ZRANGE command. You limit the number of results to the number specified in the max_results attribute (6 by default).
        if len(products) == 1:
            # only 1 product
            suggestions = r.zrange(
# products: This is a list of Product objects to get recommendations for. It can contain one or more products
                             self.get_product_key(product_ids[0]),
# max_results: This is an integer that represents the maximum number of recommendations to return.
                             0, -1, desc=True)[:max_results]
# If more than one product is given, you generate a temporary Redis key built with the IDs of the products
        else:
            # generate a temporary key
            flat_ids = ''.join([str(id) for id in product_ids])
            tmp_key = f'tmp_{flat_ids}'
# You combine and sum all scores for the items contained in the sorted set of each of the given products. This is done using the Redis ZUNIONSTORE
# command. The ZUNIONSTORE command performs a union of the sorted sets with the given keys, and stores the aggregated sum of scores of the elements in a new Redis key.
            # multiple products, combine scores of all products
            # store the resulting sorted set in a temporary key
            keys = [self.get_product_key(id) for id in product_ids]
            r.zunionstore(tmp_key, keys)
# Since you are aggregating scores, you might obtain the same products you are getting recommendations for. You remove them from the generated sorted set using the ZREM command.
            # remove ids for the products the recommendation is for
            r.zrem(tmp_key, *product_ids)
# You retrieve the IDs of the products from the temporary key, ordered by their score using the ZRANGE command. You limit the number of results to
# the number specified in the max_results attribute. Then, you remove the temporary key
            # get the product ids by their score, descendant sort
            suggestions = r.zrange(tmp_key, 0, -1,
                                   desc=True)[:max_results]
            # remove the temporary key
            r.delete(tmp_key)
        suggested_products_ids = [int(id) for id in suggestions]
# you get the Product objects with the given IDs and you order the products in the same order as them
        # get suggested products and sort by order of appearance
        suggested_products = list(Product.objects.filter(id__in=suggested_products_ids))
        suggested_products.sort(key=lambda x: suggested_products_ids.index(x.id))
        return suggested_products
# method to clear the recommendations
    def clear_purchases(self):
        for id in Product.objects.values_list('id', flat=True):
            r.delete(self.get_product_key(id))


from jinja2 import Environment, PackageLoader, select_autoescape
import pdfkit
import string
import random
import os

import anvil.server

anvil.server.connect(os.getenv('J2L_ANVIL_KEY'))

debug = bool(os.getenv('J2L_DEBUG'))
printer_name = os.getenv('J2L_PRINTER')

def __init__():
    pass

env = Environment(
    loader=PackageLoader("j2labeler"),
    autoescape=select_autoescape(),
)

options = {
    'page-width': 2 * 25.4,
    'page-height': 1 * 25.4,
    'margin-top': '0in',
    'margin-bottom': '0in',
    'margin-right': '0in',
    'margin-left': '0in',
}

@anvil.server.callable
def print_label(label_vars: dict, template: str, wkhtml_options: dict={},):
    job_id = 'label_temp/' + ''.join(random.choices(string.ascii_letters, k=10))
    template = env.get_template(template)
    pdfkit.from_string(
        template.render(label_vars=label_vars), # Returns a string of HTML
        job_id, # Random filename to avoid Weird Crap (tm)
        options=wkhtml_options, # Pass in options from the Anvil app
    )
    os.system(f'lpr -P {printer_name} {job_id}')
    if not debug:
        os.remove(job_id)
        print(f'printed label {job_id}')

@anvil.server.callable
def ping():
    print('ping')
    return True

anvil.server.wait_forever()


#!/usr/bin/python3
"""
this file implements all unittests for collector builtwith
"""

__author__ = "Lukas Reiter"
__license__ = "GPL v3.0"
__copyright__ = """Copyright 2018 Lukas Reiter

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

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

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

import tempfile
import json
from typing import List
from typing import Dict
from unittests.tests.collectors.core import CollectorProducerTestSuite
from unittests.tests.collectors.kali.modules.core import BaseKaliCollectorTestCase
from collectors.os.modules.osint.builtwith import CollectorClass as BuiltWithCollector
from database.model import DomainName
from database.model import CollectorType
from database.model import Host
from database.model import ScopeType

class BaseBuiltWithCollectorTestCase(BaseKaliCollectorTestCase):
    """
    This class implements all unittestss for the given collector
    """
    def __init__(self, test_name: str, **kwargs):
        super().__init__(test_name,
                         collector_name="builtwith",
                         collector_class=BuiltWithCollector)

    @staticmethod
    def get_command_text_outputs() -> List[str]:
        """
        This method returns example outputs of the respective collectors
        :return:
        """
        return []

    @staticmethod
    def get_command_json_outputs() -> List[Dict[str, str]]:
        """
        This method returns example outputs of the respective collectors
        :return:
        """
        json_objects = json.loads("""{
    "Relationships": [
        {
            "Domain": "unittest.com",
            "Identifiers": [
                {
                    "Type": "ip",
                    "Value": "192.168.10.1",
                    "First": 1345212000000,
                    "Last": 1565531237296,
                    "Matches": []
                },
                {
                    "Type": "google-analytics",
                    "Value": "1212341234",
                    "First": 1294059600000,
                    "Last": 1530438765239,
                    "Matches": [
                      {
                          "Domain": "unittest.ch",
                          "First": 1389790800000,
                          "Last": 1390827600000,
                          "Overlap": true
                      },
                      {
                          "Domain": "unittest.org",
                          "First": 1389790800000,
                          "Last": 1390827600000,
                          "Overlap": true
                      },
                      {
                          "Domain": "unittest.se",
                          "First": 1389790800000,
                          "Last": 1390222800000,
                          "Overlap": true
                      }
                    ]
                },
                {
                    "Type": "ip",
                    "Value": "host.www.localhost.com",
                    "First": 1507590000000,
                    "Last": 1519945200000,
                    "Matches": []
                },
                {
                    "Type": "google-tag-manager",
                    "Value": "ASDF3423",
                    "First": 1519858800000,
                    "Last": 1545920855764,
                    "Matches": [
                      {
                          "Domain": "unittest1.ch",
                          "First": 1389790800000,
                          "Last": 1390827600000,
                          "Overlap": true
                      },
                      {
                          "Domain": "unittest1.org",
                          "First": 1389790800000,
                          "Last": 1390827600000,
                          "Overlap": true
                      },
                      {
                          "Domain": "unittest1.se",
                          "First": 1389790800000,
                          "Last": 1390222800000,
                          "Overlap": true
                      }
                    ]
                }
            ]
        }
    ],
    "Errors": []
}""")
        return [json_objects]

    def test_verify_results(self):
        """
        This method checks whether the collector correctly verifies the command output
        :return:
        """
        self.init_db()
        with tempfile.TemporaryDirectory() as temp_dir:
            test_suite = CollectorProducerTestSuite(engine=self._engine,
                                                    arguments={"workspace": self._workspaces[0],
                                                               "output_dir": temp_dir})
            with self._engine.session_scope() as session:
                source = self.create_source(session, source_str=self._collector_name)
                command = self.create_command(session=session,
                                              workspace_str=self._workspaces[0],
                                              command=["kisimport", "unittest2.com"],
                                              collector_name_str=self._collector_name,
                                              collector_name_type=CollectorType.domain,
                                              scope=ScopeType.all,
                                              output_path=temp_dir)
                command.json_output = self.get_command_json_outputs()
                test_suite.verify_results(session=session,
                                          arg_parse_module=self._arg_parse_module,
                                          command=command,
                                          source=source,
                                          report_item=self._report_item)
        with self._engine.session_scope() as session:
            results = session.query(DomainName).all()
            results = [item.name for item in results]
            results.sort()
            expected_results = ["unittest.ch",
                                "unittest.org",
                                "unittest.se",
                                "unittest1.ch",
                                "unittest1.org",
                                "unittest1.se"]
            expected_results.sort()
            self.assertListEqual(expected_results, results)
            results = session.query(Host).filter_by(address="192.168.10.1").one()
            self.assertEqual("192.168.10.1", results.ipv4_address)


import hashlib

def get_nonce(hash_value):
    nonce = 0
    while True:
        test_hash = hash_value.copy()
        nonce_bytes = int(nonce).to_bytes(32, byteorder='big')
        test_hash.update(nonce_bytes)
        if test_hash.hexdigest()[0:4] == '0000':
            print('{0}번 반복'.format(nonce+1))
            return int(nonce).to_bytes(32, byteorder='big')
        nonce = nonce + 1

def print_block(data):
    for index in range(0, (len(data)//32)+1):
        print(data[index*32:(index+1)*32].hex())

def main():
    print('블록체인에 넣을 값을 입력하세요.')
    data = input('값: ')
    data_utf8 = data.encode('utf-8')

    hash_value_init = hashlib.new('sha256')

    hash_value_init.update(b'\x00'*32)
    hash_value_init.update(data_utf8)

    print('Init Strings: {0}'.format(hash_value_init.hexdigest()))

    hash_value = hash_value_init.copy()

    for index in range(1, 6):
        data_bytes = hash_value.digest()
        hash_value = hashlib.sha256()
        hash_value.update(data_bytes)
        block = data_bytes
        print('블록체인에 넣을 값을 입력하세요.')
        data = input('값: ')
        data_utf8 = data.encode('utf-8')
        hash_value.update(data_utf8)
        block = block + data_utf8
        nonce = get_nonce(hash_value)
        hash_value.update(nonce)
        block = block + nonce
        print('{0} Strings: {1}'.format(index, hash_value.hexdigest()))
        print_block(block)

if __name__ == '__main__':
    main()


class OceanMap:
    """
    This class represents the map for the ocean.

    Attributes:
        Map {2D Array}: An array with 1's and 0's, 1 representing island 0's
                        represent ocean
    """
    def __init__(self, OceanMap):
        """
        This function is used during declarion to set 'map' (Array)

        Args:
            OceanMap {2D Array} sets attribute Map for the object
        """
        self.Map = OceanMap

    def largestIsland(self):
        """
        Counts size of each island and returns size of the largest island

        Returns:
            {int} Size of the largest island
        """
        x,y = (0,0)
        largest_island = 0
        # Goes through each location on the Map
        for x in range(len(self.Map)):
            for y in range(len(self.Map[0])):
                islandSize = self.countIsland(x,y)
                if largest_island < islandSize:
                    largest_island = islandSize

        return largest_island

    def countIsland(self,x,y):
        """
        Counts the size of a single island and returns the size.

        Args:
            x {int} The X coordinate of an location of part of island
            y {int} The Y Coordiante of an location of part of island

        Returns:
            {int} The Size of the island
        """
        if (x >= len(self.Map)) or (y >= len(self.Map[0])):
            return 0

        theMap = list(map(list, self.Map))
        toSearch = [(x,y)]
        islandSize = 0
        for oceanTuple in toSearch:
            if (oceanTuple[0] >= len(self.Map) or oceanTuple[0] < 0) or (oceanTuple[1] >= len(self.Map[0] or oceanTuple[1] < 0 )):
                continue

            if(theMap[oceanTuple[0]][oceanTuple[1]] == 1):
                islandSize += 1
                theMap[oceanTuple[0]][oceanTuple[1]] = 0
                toSearch.append((oceanTuple[0]+1,oceanTuple[1]))
                toSearch.append((oceanTuple[0]-1,oceanTuple[1]))
                toSearch.append((oceanTuple[0],oceanTuple[1]+1))
                toSearch.append((oceanTuple[0],oceanTuple[1]-1))

        return islandSize

    def printMap(self):
        """
        Prints the entire map {2D Array} in the command line
        """
        for row in self.Map:
            print(row)