import numpy as np

from typing import Tuple, List
from experiments.datasets import load_binary_dataset

def compute_mean_stddev_times(elapsed_times: List[float]) -> Tuple[float, float]:
    mean_time = np.mean(elapsed_times).item()
    stddev_time = 2.0 * np.std(elapsed_times).item() / np.sqrt(len(elapsed_times))
    return mean_time, stddev_time

def load_dataset(
    name: str,
    mar_p: float = 0.5,
    n_samples: int = 1000,
    seed: int = 42
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
    assert 0.0 < mar_p < 1.0
    random_state = np.random.RandomState(seed)
    data_train, _, data_test = load_binary_dataset('experiments/datasets', name, raw=True)
    indices = random_state.choice(len(data_test), size=n_samples, replace=True)  # Resample the test set
    evi_data = data_test[indices]
    data_train, evi_data = data_train.astype(np.float32), evi_data.astype(np.float32)
    mar_data = evi_data.copy()
    mar_data[random_state.rand(*mar_data.shape) <= mar_p] = np.nan
    return data_train, evi_data, mar_data


"""Python utilities to interact with PyMOL.

This package contains the following modules:

list -- Description.

them -- Description.

here -- Description.

"""

__all__ = [
    "Pose",
    "findseq",
    "aaindex",
    "Resfile",
    "Gremlin",
    "Mutations",
    "Native",
    "Cycler",
    "DNAselections",
    "GenUtils",
    "InterfaceUtils",
    "MoleculeUtils",
    "PDBBrowser",
    "PackingMeasureUtils",
    "pdb_hb_uns",
    "rosetta_vdw",
    "LigandView",
    "CavityView",
    "FindSurfaceResidues",
    "sym_util",
    "transform",
    ]


import polars as pl
from bs4 import BeautifulSoup
from reusable import regex_year, log, get_one_page, crawl, clean_concat

def gen_url(start, val, field):  # fields = groups, genres, ...
#     params = {field: val, 'start': start, 'ref_': 'adv_nxt'} could be passed to requests.get
    return f"https://www.imdb.com/search/title/?{field}={val}&start={start}&ref_=adv_nxt"

def extract_data(res, logger):
    # regexes: https://www.freecodecamp.org/news/web-scraping-sci-fi-movies-from-imdb-with-python/
    html = BeautifulSoup(res.text, 'html.parser')
    log(logger, f'Extracting content of {res.url}')
    containers = html.find_all('div', class_='lister-item mode-advanced')
    fields = [
        ['h3', 'span', {'class_': 'lister-item-year text-muted unbold'}, 'year'],
        ['p', 'span', {'class_': 'certificate'}, 'rating'],
        ['p', 'span', {'class_': 'genre'}, 'genre'],
        ['p', 'span', {'class_': 'runtime'}, 'runtime'],
    ]
    fields_names = [f[3] for f in fields] + ['title', 'imdb_rating', 'm_score', 'vote']
    dic = {field: [] for field in fields_names}

    for container in containers:
        if container.find('div', class_='ratings-metascore') is not None:
            dic['title'].append(container.h3.a.text)

            for field in fields:
                ext = getattr(container, field[0]).find(field[1], **field[2])
                dic[field[3]].append(ext.text if ext is not None else None)

            ext = container.strong
            dic['imdb_rating'].append(ext.text if ext is not None else None)

            ext = container.find('span', class_='metascore')
            dic['m_score'].append(ext.text if ext is not None else None)

            ext = container.find('span', attrs={'name':'nv'})
            dic['vote'].append(ext.get('data-value', None) if isinstance(ext, dict) else None)

    regexes = [
        ['year', *regex_year],
        ['runtime', r'(\d+)', pl.UInt16],
        ['imdb_rating', r'(\d+.\d+)', pl.Float32],
        ['m_score', r'(\d+)', pl.UInt8],
        ['vote', r'(\d+)', pl.UInt32],
    ]
    return (
        pl.DataFrame(dic)
        .with_columns(pl.all().cast(pl.Utf8))
        .with_columns([
            pl.col(col).str.extract(regex).cast(dtype)
            for col, regex, dtype in regexes
        ])
    )

if __name__ == "__main__":
    from itertools import count
    from pathlib import Path
    from loguru import logger as logg

    groups = [
        'top_100',
        'top_250',
        'top_1000',
        'bottom_100',
        'bottom_250',
        'bottom_1000'
        'oscar_winner',
        'emmy_winner',
        'golden_globe_winner',
        'oscar_nominee',
        'emmy_nominee',
        'golden_globe_nominee',
        'best_picture_winner',
        'best_director_winner',
        'oscar_best_picture_nominees',
        'oscar_best_director_nominees',
        'national_film_preservation_board_winner',
        'razzie_winner',
        'razzie_nominee',
    ]

    data_groups = []
    for group in groups:
        iterator = count(1, 50)
        # https://www.imdb.com/search/title: default display option 50 per page
        save_path = Path(f'./imdb/{group}.parquet')
        def get_datum(name):
            return get_one_page(extract_data, gen_url(name, group, 'groups'), logg)
        data_groups.append(crawl(get_datum, iterator, save_path, 1, logg))
    clean_concat(data_groups).write_parquet(Path('./imdb/groups.parquet'))


digits = 32
max_count = 500
count = 0

def writeout(nums):

	global count

	ret = "1"
	for num in nums:
		if num == 1:
			ret+="11"
		else:
			ret+="00"
	ret+="1"
	div = ["3 4 5 6 7 8 9 10 11"]
	print (ret+" "+" ".join(div))
	count += 1

def dfs(nums):

	if count >= max_count:
		return

	if len(nums) == 15:
		writeout(nums)

	else:
		nums.append(0)
		dfs(nums)
		del nums[-1]
		nums.append(1)
		dfs(nums)
		del nums[-1]

print("Case #1:")
dfs([])


import random
from typing import List, Optional

import tqdm

def simulate(num_barbers: int = 4,
             haircut_time: int = 15,
             num_other_customers_in_line: int = 1,
             per_chance_wait_tiffany: float = .25,
             initial_wait_times: Optional[List[float]] = None,
             initial_customers_prefer_tiffany: Optional[List[bool]] = None
             ) -> float:

    # Randomly initialize the time remaining for each barber (0 is Tiffany) if
    # not provided
    barbers_current_time_remaining = (initial_wait_times
                                      if initial_wait_times is not None
                                      else [random.randrange(0, haircut_time)
                                            for _ in range(num_barbers)])

    # Randomly determine whether customers prefer tiffany if not provided
    customers_prefer_tiffany = (initial_customers_prefer_tiffany
                                if initial_customers_prefer_tiffany is not None
                                else [random.random() < per_chance_wait_tiffany
                                      for _ in range(num_other_customers_in_line)])

    wait_time = 0

    for customer_id in range(len(customers_prefer_tiffany)):
        if customers_prefer_tiffany[customer_id]:
            # If customer prefers tiffany then just add them to her time
            barbers_current_time_remaining[0] += haircut_time
            continue

        # Allow the barber who is closest to finishing to finish.
        next_barber_i = barbers_current_time_remaining.index(
            min(barbers_current_time_remaining)
        )
        elapsed_time = barbers_current_time_remaining[next_barber_i]
        # Add the time for that barber to finish to the wait time
        wait_time += elapsed_time
        # and subtract it from the rest of the barbers
        barbers_current_time_remaining = [time - elapsed_time
                                          for time in barbers_current_time_remaining]

        # The barber with the least amount of time just got a new client so
        # reset their time remaining
        barbers_current_time_remaining[next_barber_i] = haircut_time

    # All the customers ahead of you are now being served. We're waiting for
    # Tiffany so we should add her remaining time to the wait time
    wait_time += barbers_current_time_remaining[0]

    return wait_time

# 10 min initial wait, plus customer will wait for tiffany so additional 15 min
assert simulate(initial_wait_times=[10, 9],
                initial_customers_prefer_tiffany=[True]) == 25

# 10 min initial, customer will go to barber 2, so total total 10 min wait
assert simulate(initial_wait_times=[10, 9],
                initial_customers_prefer_tiffany=[False]) == 10

# Tiffany finishes first so initial 10 min wait, then she sees customer even
# though no preference
assert simulate(initial_wait_times=[10, 11],
                initial_customers_prefer_tiffany=[False]) == 25

# 10 min initial wait, first customer sees tiffany since shes done first,
# 2nd customer waits on tiffany, total time 40 min
assert simulate(initial_wait_times=[10, 15],
                initial_customers_prefer_tiffany=[False, True]) == 40

assert simulate(initial_wait_times=[10, 15, 10],
                initial_customers_prefer_tiffany=[False, True, False, False]) == 40

ITERATIONS = 100_000_000

results = [simulate() for _ in tqdm.tqdm(range(ITERATIONS))]

print(sum(results) / ITERATIONS)

# Output 13.95017875


import pandas as pd
import datetime as dt
import dateutil.tz

ams = dateutil.tz.gettz('Europe/Amsterdam')
utc = dateutil.tz.UTC

class ImbalancePriceReader(object):
    def __init__(self, market_data='../data/tennet_balans_delta/tennet_balans_delta_15m.csv'):
        market_df = pd.read_csv(market_data, parse_dates=True)
        market_df.index = pd.to_datetime(market_df['time'], utc=True)

        market_df = market_df.drop('time', axis=1)
        self.market_df = market_df

    def get_day(self, day=None):
        if day is None:
            day = dt.datetime(2020, 12, 27, tzinfo=utc)
        else:
            day = day.replace(hour=0, minute=0, second=0, microsecond=0)

        start_of_day = day
        end_of_day = day + dt.timedelta(days=1)

        return self.get_specific_time(start_of_day, end_of_day)

    def get_specific_time(self, start_of_time=None, end_of_time=None):
        if start_of_time is None:
            start_of_time = dt.datetime(2020, 12, 27, tzinfo=utc)

        if end_of_time is None:
            end_of_time = start_of_time + dt.timedelta(days=1)

        filtered_df = self.market_df[(self.market_df.index > start_of_time) & (self.market_df.index <= end_of_time)]
        return filtered_df

if __name__ == '__main__':
    imbalance_price_reader = ImbalancePriceReader()
    print(imbalance_price_reader.get_day())
    print(imbalance_price_reader.get_specific_time())


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

# © 2017-2019, ETH Zurich, Institut für Theoretische Physik
# Author: Dominik Gresch <greschd@gmx.ch>
"""
Defines a workflow for running the first-principles calculations using VASP.
"""

import copy
from collections import ChainMap

from fsc.export import export

from aiida import orm
from aiida.engine import ToContext

from aiida_tools import check_workchain_step

from aiida_vasp.calcs.vasp import VaspCalculation  # pylint: disable=import-error,useless-suppression

from .._calcfunctions import merge_nested_dict
from .wannier_input import VaspWannierInput
from .reference_bands import VaspReferenceBands
from ._base import FirstPrinciplesRunBase

@export
class VaspFirstPrinciplesRun(FirstPrinciplesRunBase):
    """
    Workflow for calculating the inputs needed for tight-binding calculation and evaluation with VASP. The workflow first performs an SCF step, and then passes the WAVECAR file to the bandstructure and Wannier90 input calculations.
    """
    @classmethod
    def define(cls, spec):
        super(VaspFirstPrinciplesRun, cls).define(spec)

        # Top-level parameters
        spec.input('code', valid_type=orm.Code, help='Code that runs VASP.')
        spec.input(
            'parameters',
            valid_type=orm.Dict,
            help=
            'Parameters passed to all VASP calculations, unless explicitly overwritten.'
        )
        spec.input_namespace(
            'calculation_kwargs',
            required=False,
            dynamic=True,
            help=
            'Keyword arguments passed to all VASP calculations, unless explicitly overwritten.'
        )

        # Optional parameters to override for specific calculations.
        # TODO: Use expose for the sub-workflows.
        for sub_calc in ['scf', 'bands', 'to_wannier']:
            spec.input_namespace(
                sub_calc,
                required=False,
                dynamic=True,
                help="Inputs passed to the '{}' sub-workflow / calculation".
                format(sub_calc)
            )

        spec.input(
            'bands.merge_kpoints',
            valid_type=orm.Bool,
            default=lambda: orm.Bool(False),
            help=
            'Determines whether the k-point mesh needs to be added for the bandstructure calculation. This is needed for hybrid functional calculations.'
        )

        spec.expose_outputs(VaspReferenceBands)
        spec.expose_outputs(VaspWannierInput)

        spec.outline(cls.run_scf, cls.run_bands_and_wannier, cls.finalize)

    def _collect_common_inputs(
        self, namespace, expand_kwargs=False, force_parameters=None
    ):
        """
        Join the top-level inputs and inputs set in a specific namespace.
        """
        ns_inputs = self.inputs.get(namespace, {})
        ns_parameters = ns_inputs.get('parameters')
        if ns_parameters is None:
            parameters = self.inputs.parameters
        else:
            parameters = merge_nested_dict(
                dict_primary=ns_parameters,
                dict_secondary=self.inputs.parameters
            )
            if force_parameters:
                parameters = merge_nested_dict(
                    dict_primary=orm.Dict(dict=force_parameters).store(),
                    dict_secondary=parameters
                )
        calculation_kwargs = copy.deepcopy(
            dict(
                ChainMap(
                    ns_inputs.get('calculation_kwargs', {}),
                    self.inputs.calculation_kwargs
                )
            )
        )
        res = dict(
            code=self.inputs.code,
            structure=self.inputs.structure,
            parameters=parameters,
        )
        if expand_kwargs:
            res.update(calculation_kwargs)
        else:
            res['calculation_kwargs'] = calculation_kwargs
        return res

    @check_workchain_step
    def run_scf(self):
        """
        Run the SCF calculation step.
        """
        self.report('Launching SCF calculation.')

        return ToContext(
            scf=self.submit(
                VaspCalculation,
                potential={(kind, ): pot
                           for kind, pot in self.inputs.potentials.items()},
                kpoints=self.inputs.kpoints_mesh,
                settings=orm.Dict(
                    dict={
                        'ADDITIONAL_RETRIEVE_LIST': ['WAVECAR'],
                        'parser_settings': {
                            'add_wavecar': True
                        }
                    }
                ),
                **self._collect_common_inputs(
                    'scf',
                    force_parameters={'lwave': True},
                    expand_kwargs=True
                )
            )
        )

    def _collect_process_inputs(self, namespace):
        """
        Helper to collect the inputs for the reference bands and wannier input workflows.
        """
        scf_wavefun = self.ctx.scf.outputs.output_wavecar
        res = self._collect_common_inputs(namespace)
        res['potentials'] = self.inputs.potentials
        res['calculation_kwargs']['wavefunctions'] = scf_wavefun
        self.report('Collected inputs: {}'.format(res))
        return res

    @check_workchain_step
    def run_bands_and_wannier(self):
        """
        Run the reference bands and wannier input workflows.
        """

        self.report('Launching bands workchain.')
        bands_run = self.submit(
            VaspReferenceBands,
            kpoints=self.inputs.kpoints,
            kpoints_mesh=self.inputs.kpoints_mesh,
            merge_kpoints=self.inputs.bands['merge_kpoints'],
            **self._collect_process_inputs('bands')
        )
        self.report('Launching to_wannier workchain.')
        to_wannier_run = self.submit(
            VaspWannierInput,
            kpoints_mesh=self.inputs.kpoints_mesh,
            wannier_parameters=self.inputs.get('wannier_parameters', None),
            wannier_projections=self.inputs.get('wannier_projections', None),
            **self._collect_process_inputs('to_wannier')
        )
        return ToContext(bands=bands_run, to_wannier=to_wannier_run)

    @check_workchain_step
    def finalize(self):
        """
        Add outputs of the bandstructure and wannier input calculations.
        """
        self.report('Checking that the bands calculation used WAVECAR.')
        self.check_read_wavecar(self.ctx.bands)

        self.report(
            'Checking that the wannier input calculation used WAVECAR.'
        )
        self.check_read_wavecar(self.ctx.to_wannier)

        self.report('Retrieving outputs.')
        self.out_many(self.exposed_outputs(self.ctx.bands, VaspReferenceBands))
        self.out_many(
            self.exposed_outputs(self.ctx.to_wannier, VaspWannierInput)
        )

    @staticmethod
    def check_read_wavecar(sub_workflow):
        """
        Check that the calculation in the given sub-workflow uses the
        wavefunctions input.
        """
        for label, node in sub_workflow.get_outputs(also_labels=True):
            if label == 'CALL' and isinstance(node, VaspCalculation):
                retrieved_folder = node.get_retrieved_node()
                with open(
                    retrieved_folder.get_abs_path('_scheduler-stdout.txt')
                ) as f:
                    stdout = f.read()
                    assert 'WAVECAR not read' not in stdout
                    assert 'reading WAVECAR' in stdout


from numpy import *
from matplotlib.pyplot import *
import h5py

sd = 8
N=384
k=N/2

L0 = 20.
cmap='jet'

vmin_rho=0.85
vmax_rho=1.33

vmin_t=0.9
vmax_t = 1.23

vmin_p=0.75
vmax_p = 1.54

dat_fname1 = 'grid_mc.hdf5'
dat_fname2 = 'grid_mec.hdf5'

#figsize=(7.34,6.5)
figsize=(10,8)

# =============================================================

slc = np.s_[k,k:,:k]

fig, axes = subplots(2, 4, sharey=True, sharex=True, figsize=figsize)
fig.subplots_adjust(wspace=0.0, hspace=0.07, left=0.03, right=0.98,
                    bottom=0.115, top=0.97)

ax1=axes[0,0]
ax2=axes[0,1]
ax3=axes[0,2]
ax4=axes[0,3]

ax5=axes[1,0]
ax6=axes[1,1]
ax7=axes[1,2]
ax8=axes[1,3]

props = dict(boxstyle='square', facecolor='w', alpha=0.8)
slc_b = np.s_[::sd,::sd]

with h5py.File(dat_fname1,'r') as f:
    p = asarray(f['p'])[slc]
    rho = asarray(f['rho'])[slc]
    bx = asarray(f['bxc'])[slc]
    by = asarray(f['byc'])[slc]
    bz = asarray(f['bzc'])[slc]

name = '$n_e$, ATC'

im1 = ax1.imshow(rho[::-1], extent=[0,L0,0,L0], interpolation='nearest',
                vmin=vmin_rho, vmax=vmax_rho, cmap=cmap)
ax1.set_xticks([0,5,10,15,20])
# ax1.set_xticklabels([])
ax1.set_yticks([0,5,10,15,20])
# ax1.set_xlabel(r'$x$ [kpc]')
ax1.set_ylabel(r'$y$ [kpc]')
ax1.text(0.06,0.88, name, fontsize=13, bbox=props, transform=ax1.transAxes)
fig.colorbar(im1, ax=ax1)

dx = L0 * float(sd)/k#/90
X,Y = meshgrid(arange(0,L0,dx), arange(0,L0,dx))
Btot = sqrt(bx[slc_b]**2 + by[slc_b]**2 + bz[slc_b]**2)
Bm   = sqrt(bx[slc_b]**2 + by[slc_b]**2)

ax1.quiver(X,Y, bx[slc_b]/Bm, by[slc_b]/Bm,
        color='k', scale = 30, width=3e-3, headwidth=2.8)

# --------------------------------------------------------------

T=p/rho

name = '$T_e$, ATC'

im2 = ax2.imshow(T[::-1], extent=[0,L0,0,L0], interpolation='nearest',
                vmin=vmin_t, vmax=vmax_t, cmap=cmap)
ax2.set_xticks([0,5,10,15,20])
ax2.set_yticks([0,5,10,15,20])
# ax2.set_xlabel(r'$x$ [kpc]')
# ax2.set_ylabel(r'$y$ [kpc]')
ax2.text(0.06,0.88, name, fontsize=13, bbox=props, transform=ax2.transAxes)
fig.colorbar(im2, ax=ax2, ticks=[0.9,1,1.1,1.2])

ax2.quiver(X,Y, bx[slc_b]/Bm, by[slc_b]/Bm,
        color='k', scale = 30, width=3e-3, headwidth=2.8)

# --------------------------------------------------------------

name = '$T_i$, ATC'

im3 = ax3.imshow(T[::-1], extent=[0,L0,0,L0], interpolation='nearest',
                vmin=vmin_t, vmax=vmax_t, cmap=cmap)
ax3.set_xticks([0,5,10,15,20])
ax3.set_yticks([0,5,10,15,20])
# ax3.set_xlabel(r'$x$ [kpc]')
# ax3.set_ylabel(r'$y$ [kpc]')
ax3.text(0.06,0.88, name, fontsize=13, bbox=props, transform=ax3.transAxes)
fig.colorbar(im3, ax=ax3, ticks=[0.9,1,1.1,1.2])

ax3.quiver(X,Y, bx[slc_b]/Bm, by[slc_b]/Bm,
        color='k', scale = 30, width=3e-3, headwidth=2.8)

# --------------------------------------------------------------

name = '$p_e$, ATC'

im4 = ax4.imshow(p[::-1], extent=[0,L0,0,L0], interpolation='nearest',
                vmin=vmin_p, vmax=vmax_p, cmap=cmap)
ax4.set_xticks([0,5,10,15,20])
ax4.set_yticks([0,5,10,15,20])
# ax4.set_xlabel(r'$x$ [kpc]')
# ax4.set_ylabel(r'$y$ [kpc]')
ax4.text(0.06,0.88, name, fontsize=13, bbox=props, transform=ax4.transAxes)
fig.colorbar(im4, ax=ax4, ticks=[0.8,1,1.2,1.4])

ax4.quiver(X,Y, bx[slc_b]/Bm, by[slc_b]/Bm,
        color='k', scale = 30, width=3e-3, headwidth=2.8)

# =============================================================

with h5py.File(dat_fname2,'r') as f:
    pi = asarray(f['p'])[slc]
    pe = asarray(f['pe'])[slc]
    rho = asarray(f['rho'])[slc]
    bx = asarray(f['bxc'])[slc]
    by = asarray(f['byc'])[slc]
    bz = asarray(f['bzc'])[slc]

name = '$n_e$, ATC+TT'

im5 = ax5.imshow(rho[::-1], extent=[0,L0,0,L0], interpolation='nearest',
                vmin=vmin_rho, vmax=vmax_rho, cmap=cmap)
ax5.set_xticks([0,5,10,15,20])
# ax5.set_xticklabels([])
ax5.set_yticks([0,5,10,15,20])
# ax5.set_yticklabels([])
ax5.set_xlabel(r'$x$ [kpc]')
ax5.set_ylabel(r'$y$ [kpc]')
ax5.text(0.06,0.88, name, fontsize=13, bbox=props, transform=ax5.transAxes)
fig.colorbar(im5, ax=ax5)

dx = L0 * float(sd)/k
X,Y = meshgrid(arange(0,L0,dx), arange(0,L0,dx))
Btot = sqrt(bx[slc_b]**2 + by[slc_b]**2 + bz[slc_b]**2)
Bm   = sqrt(bx[slc_b]**2 + by[slc_b]**2)

ax5.quiver(X,Y, bx[slc_b]/Bm, by[slc_b]/Bm,
        color='k', scale = 30, width=3e-3, headwidth=2.8)

# --------------------------------------------------------------

T=2*pe/rho

name = '$T_e$, ATC+TT'

im6 = ax6.imshow(T[::-1], extent=[0,L0,0,L0], interpolation='nearest',
                vmin=vmin_t, vmax=vmax_t, cmap=cmap)
ax6.set_xticks([0,5,10,15,20])
ax6.set_yticks([0,5,10,15,20])
# ax6.set_yticklabels([])
ax6.set_xlabel(r'$x$ [kpc]')
# ax6.set_ylabel(r'$y$ [kpc]')

ax6.text(0.06,0.88, name, fontsize=13, bbox=props, transform=ax6.transAxes)
fig.colorbar(im6, ax=ax6, ticks=[0.9,1,1.1,1.2])

ax6.quiver(X,Y, bx[slc_b]/Bm, by[slc_b]/Bm,
        color='k', scale = 30, width=3e-3, headwidth=2.8)

# --------------------------------------------------------------

T=2*pi/rho

name = '$T_i$, ATC+TT'

im7 = ax7.imshow(T[::-1], extent=[0,L0,0,L0], interpolation='nearest',
                vmin=vmin_t, vmax=vmax_t, cmap=cmap)
ax7.set_xticks([0,5,10,15,20])
ax7.set_yticks([0,5,10,15,20])
# ax7.set_yticklabels([])
ax7.set_xlabel(r'$x$ [kpc]')
# ax7.set_ylabel(r'$y$ [kpc]')

ax7.text(0.06,0.88, name, fontsize=13, bbox=props, transform=ax7.transAxes)
fig.colorbar(im7, ax=ax7, ticks=[0.9,1,1.1,1.2])

ax7.quiver(X,Y, bx[slc_b]/Bm, by[slc_b]/Bm,
        color='k', scale = 30, width=3e-3, headwidth=2.8)

# --------------------------------------------------------------

name = '$p_e$, ATC+TT'

im8 = ax8.imshow(2*pe[::-1], extent=[0,L0,0,L0], interpolation='nearest',
                vmin=vmin_p, vmax=vmax_p, cmap=cmap)
ax8.set_xticks([0,5,10,15,20])
ax8.set_yticks([0,5,10,15,20])
# ax8.set_yticklabels([])
ax8.set_xlabel(r'$x$ [kpc]')
# ax8.set_ylabel(r'$y$ [kpc]')

ax8.text(0.06,0.88, name, fontsize=13, bbox=props, transform=ax8.transAxes)
fig.colorbar(im8, ax=ax8, ticks=[0.8,1,1.2,1.4])

ax8.quiver(X,Y, bx[slc_b]/Bm, by[slc_b]/Bm,
        color='k', scale = 30, width=3e-3, headwidth=2.8)

show()


"""
Python program for topological sort of DAG
"""

from collections import defaultdict

class Graph:
    def __init__(self, v):
        self.v = v
        self.graph = defaultdict(list)

    def add_edge(self, src, dest):
        self.graph[src].append(dest)

    def topological_sortingUtils(self, node, stack, visited):
        visited[node] = True
        for i in self.graph[node]:
            if not visited[i]:
                self.topological_sortingUtils(i, stack, visited)
        stack.append(node)

    def topological_sorting(self):
        visited = [False]*self.v
        stack = []

        for i in range(self.v):
            if not visited[i]:
                self.topological_sortingUtils(i, stack, visited)

        print(stack[::-1])

if __name__=='__main__':
    g = Graph(6)
    g.add_edge(5, 2)
    g.add_edge(5, 0)
    g.add_edge(4, 0)
    g.add_edge(4, 1)
    g.add_edge(2, 3)
    g.add_edge(3, 1)

    print("Following is a Topological Sort of the given graph")

    # Function Call
    g.topological_sorting()


from sympy import fibonacci as fib
from sympy import sieve
import collections
import copy
import pickle

window = collections.deque([0,1,1],3) # deque object, appending to end kicks first element off the front
found = copy.copy(window) # pattern we want to check against to find period
data = []

for p in sieve.primerange(2,10000):
    q = 3 # start index counter at 3 since we're starting with the fourth fibonacci number
    while True:
        window.append(fib(q)%p)
        if window == found: # if we've looped back around
            print(p, q-3)
            data.append((p,q-3)) # add (prime,period) to list
            break
        q+=1

with open('data','wb') as file:
    pickle.dump(data,file)


'''

@author: lining
'''
import zstacklib.utils.jsonobject as jsonobject
from zstacklib.utils import shell
from zstacklib.utils import log

logger = log.get_logger(__name__)

def getCephPoolsCapacity():
    result = []

    o = shell.call('ceph osd dump -f json')
    df = jsonobject.loads(o)
    if not df.pools:
        return result

    for pool in df.pools:
        crush_rule = None
        if pool.crush_ruleset is None:
            crush_rule = pool.crush_rule
        else:
            crush_rule = pool.crush_ruleset
        poolCapacity = CephPoolCapacity(pool.pool_name, pool.size, crush_rule)
        result.append(poolCapacity)

    # fill crushRuleItemName
    o = shell.call('ceph osd crush rule dump -f json')
    crushRules = jsonobject.loads(o)
    if not crushRules:
        return result
    for poolCapacity in result:
        if poolCapacity.crushRuleSet is None:
            continue

        def setCrushRuleName(crushRule):
            if not crushRule:
                return
            for step in crushRule.steps:
                if step.op == "take":
                    poolCapacity.crushRuleItemName = step.item_name

        [setCrushRuleName(crushRule) for crushRule in crushRules if crushRule.rule_id == poolCapacity.crushRuleSet]

    # fill crushItemOsds
    o = shell.call('ceph osd tree -f json')
    tree = jsonobject.loads(o)
    if not tree.nodes:
        return result

    def findNodeById(id):
        for node in tree.nodes:
            if node.id == id:
                return node

    def findAllChilds(node):
        childs = []

        if not node.children:
            return childs

        for childId in node.children:
            child = findNodeById(childId)
            if not child:
                continue
            childs.append(child)
            if child.children:
                grandson_childs = findAllChilds(child)
                childs.extend(grandson_childs)
        return childs

    for poolCapacity in result:
        if not poolCapacity.crushRuleItemName:
            continue
        for node in tree.nodes:
            if node.name != poolCapacity.crushRuleItemName:
                continue
            if not node.children:
                continue

            osdNodes = []
            nodes = findAllChilds(node)
            for node in nodes:
                if node.type != "osd":
                    continue
                if node.name in osdNodes:
                    continue
                osdNodes.append(node.name)
            poolCapacity.crushItemOsds = osdNodes

    # fill crushItemOsdsTotalSize, poolTotalSize
    o = shell.call('ceph osd df -f json')
    osds = jsonobject.loads(o)
    if not osds.nodes:
        return result
    for poolCapacity in result:
        if not poolCapacity.crushItemOsds:
            continue
        for osdName in poolCapacity.crushItemOsds:
            for osd in osds.nodes:
                if osd.name != osdName:
                    continue
                poolCapacity.crushItemOsdsTotalSize = poolCapacity.crushItemOsdsTotalSize + osd.kb * 1024
                poolCapacity.availableCapacity = poolCapacity.availableCapacity + osd.kb_avail * 1024
                poolCapacity.usedCapacity = poolCapacity.usedCapacity + osd.kb_used * 1024

        if poolCapacity.crushItemOsdsTotalSize != 0 and poolCapacity.replicatedSize != 0:
            poolCapacity.poolTotalSize = poolCapacity.crushItemOsdsTotalSize / poolCapacity.replicatedSize
        if poolCapacity.availableCapacity != 0 and poolCapacity.replicatedSize != 0:
            poolCapacity.availableCapacity = poolCapacity.availableCapacity / poolCapacity.replicatedSize
        if poolCapacity.usedCapacity != 0 and poolCapacity.replicatedSize != 0:
            poolCapacity.usedCapacity = poolCapacity.usedCapacity / poolCapacity.replicatedSize

    return result

class CephPoolCapacity:

    def __init__(self, poolName, replicatedSize, crushRuleSet):
        self.poolName = poolName
        self.replicatedSize = replicatedSize
        self.crushRuleSet = crushRuleSet
        self.availableCapacity = 0
        self.usedCapacity = 0
        self.crushRuleItemName = None
        self.crushItemOsds = []
        self.crushItemOsdsTotalSize = 0
        self.poolTotalSize = 0


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from Rhino.Geometry import Transform  # type: ignore

def transformation_to_rhino(transformation):
    """Creates a Rhino transformation from a COMPAS transformation.

    Parameters
    ----------
    transformation : :class:`~compas.geometry.Transformation`
        COMPAS transformation.

    Returns
    -------
    :rhino:`Rhino.Geometry.Transform`

    """
    transform = Transform(1.0)
    for i in range(0, 4):
        for j in range(0, 4):
            transform[i, j] = transformation[i, j]
    return transform

def transformation_matrix_to_rhino(matrix):
    """Creates a Rhino transformation from a 4x4 transformation matrix.

    Parameters
    ----------
    matrix : list[list[float]]
        The 4x4 transformation matrix in row-major order.

    Returns
    -------
    :rhino:`Rhino.Geometry.Transform`

    """
    transform = Transform(1.0)
    for i in range(0, 4):
        for j in range(0, 4):
            transform[i, j] = matrix[i][j]
    return transform


from flask import Blueprint, Response, json, request
from flask_mysqldb import MySQL
from logger import post_log

collab = Blueprint("collab", __name__)

from manage import mysql

@collab.route('/all', methods=['GET'])
def get_all_collabs():
    try:
        cur = mysql.connection.cursor()
        cur.execute("SELECT * FROM heroku_012605fb848c7a7.collaborators;")
        all_collabs = []
        rows = cur.fetchall()
        for row in rows:
            temp_collab = {}
            temp_collab['id'] = row[0]
            temp_collab['youtube_link'] = row[1]
            all_collabs.append(temp_collab)

        post_log('GET /collabs/all')
        return Response(json.dumps({"collabs": all_collabs, "code": 200}), mimetype='application/json')
    except Exception as e:
        return {"Error": "Unable to retrive all collaborators.","error message": str(e)}

@collab.route('/<int:collabID>', methods=['GET'])
def find_collaborator(collabID):
    try:
        cur = mysql.connection.cursor()
        cur.execute("SELECT * FROM heroku_012605fb848c7a7.collaborators WHERE id = "+str(collabID)+";")
        row = cur.fetchone()
        collab = {
            'youtube_link':row[1],
            'id' : row[0]
        }

        post_log('GET /meals/<int:collabID>')
        return Response(json.dumps({"collaborator": collab, "code": 200}), mimetype='application/json')
    except Exception as e:
        return {"Error": "Unable to retrieve this collaborator.","error message": str(e)}

@collab.route('/<int:collabID>', methods=['DELETE'])
def delete_collab(collabID):
    try:
        cur = mysql.connection.cursor()
        cur.execute("DELETE FROM heroku_012605fb848c7a7.collaborators WHERE id = " + str(collabID) + ";")
        mysql.connection.commit()
        deleted = {
            'id' : collabID
        }

        post_log('DELETE /meals/<int:collabID>')
        return Response(json.dumps({"deleted": deleted, "code": 200}), mimetype='application/json')
    except Exception as e:
        return {"Error": "Unable to delete this collaborator.","error message": str(e)}

@collab.route('/<int:collabID>', methods = ['PUT'])
def update_collab_info(collabID):
    try:
        cur = mysql.connection.cursor()
        youtube_link = request.get_json()['youtube_link']
        cur.execute("UPDATE heroku_012605fb848c7a7.collaborators SET youtube_link = '"+str(youtube_link) +
        "'WHERE id = "+ str(collabID)+";")
        mysql.connection.commit()
        updated = {
            'youtube_link':youtube_link
        }

        post_log('PUT /meals/<int:collabID>')
        return Response(json.dumps({"updated": updated, "code": 201}), mimetype='application/json')
    except Exception as e:
        return {"Error": "Unable to update this collaborator." ,"error message": str(e)}

@collab.route('', methods=['POST'])
def create_collab():
    try:
        cur = mysql.connection.cursor()
        id = request.get_json()['id']
        youtube_link = request.get_json()['youtube_link']

        cur.execute("SELECT id FROM heroku_012605fb848c7a7.users Where id = " +str(id) +  ";")
        row = cur.fetchone()
        foundID = row[0]
        #print("row: ", row)
        #print("foundID: ", foundID)

        #if foundID == None:
        #    return {"NOT FOUND":"Can't create Collab if given ID does not exist in USER table"}
        #else:
        cur.execute("INSERT INTO heroku_012605fb848c7a7.collaborators (id, youtube_link) VALUES ('"
            + id + "', '"
            + youtube_link + "');")
        mysql.connection.commit()
        posted = {
            'youtube link' : youtube_link,
            'id' : id
        }

        post_log('POST /meals')
        return Response(json.dumps({"posted": posted, "code": 201}), mimetype='application/json')
    except Exception as e:
        return {"Error": "Unable to create this collaborator.", "error message": str(e)}

def getMinID():
    cur = mysql.connection.cursor()
    cur.execute("SELECT max(id) FROM heroku_012605fb848c7a7.collaborators;")
    return cur.fetchone()


import pytest

from wattage.critical_power import CriticalPower

def test_ols():
    x = list(range(10))
    test_B = 2
    test_a = 3
    y = [test_B*xi + test_a for xi in x]
    B, a = CriticalPower._ols(x, y)
    assert B == test_B
    assert a == test_a

def test_estimated_power():
    x = list(range(60, 70))
    test_B = 2
    test_a = 3
    y = [test_B/xi + test_a for xi in x]
    cp = CriticalPower(list(zip([xi for xi in x], y)))
    for xi, yi in zip(x, y):
        assert pytest.approx(yi, rel=0.001) == cp.estimated_power(xi)

def test_estimated_ftp():
    x = list(range(60, 70))
    test_B = 2
    test_a = 3
    y = [test_B/xi + test_a for xi in x]
    cp = CriticalPower(list(zip([xi for xi in x], y)))
    power_20min = test_B/1200 + test_a
    assert pytest.approx(cp.estimated_ftp(), rel=0.001) == power_20min * 0.95


"""Обратиться с странице https://habrahabr.ru/. Получить текст страницы.
При помощи регулярных выражений нужно получить все ссылки со страницы на другие."""
import requests, re

def get_links(url):
    response = requests.get(url)

    list_links = re.findall('a href=\"(https?://.+?)"', str(response.content), flags=re.MULTILINE)
    for link in list_links:
        print(link)

if __name__ == '__main__':
    get_links('https://habrahabr.ru/')


import asyncio
from pyppeteer import launch
import time
from lxml import html
import base64
import pandas as pd
from threading import Lock
import hashlib
from os import makedirs
from datetime import datetime
import pytz
from concurrent.futures import ThreadPoolExecutor
from azcaptchaapi import AZCaptchaApi

api = AZCaptchaApi('pdnkdbkqgmflzcy64jwnxcbw2t9gzhmr')
start_time_step_one = '23:58:00'
start_time_step_two = '00:00:01'
number_browsers = 5

def start_step_one():
	while True:
		current_time = datetime.now(pytz.timezone('CET')).strftime("%H:%M:%S")
		print(current_time)
		if current_time == start_time_step_one:
			break
		time.sleep(1)
def start_step_two():
	while True:
		current_time = datetime.now(pytz.timezone('CET')).strftime("%H:%M:%S")
		print(current_time)
		if current_time == start_time_step_two:
			break
		time.sleep(1)
async def handle_step_one( page ):
        await page.goto( "https://service2.diplo.de/rktermin/extern/choose_categoryList.do?locationCode=pris&realmId=362" ,  {'waitUntil': 'domcontentloaded'}  )
        print('current url 1 is ', await page.evaluate("() => window.location.href"))
        print( 'next link ')
        oneHref1 = await page.waitForXPath('//*[@id="content"]/div[1]/div[2]/a')
        navPromise1 = asyncio.ensure_future( page.waitForNavigation( {"timeout": 60000, "waitUntil": "networkidle0"} ))
        await oneHref1.click()
        await navPromise1
        print('current url 2 is ', await page.evaluate("() => window.location.href"))
        print( 'next link ')

        oneHref2 = await page.waitForXPath('//*[@id="content"]/div[1]/h3[1]/a[2]')
        navPromise2 = asyncio.ensure_future( page.waitForNavigation( {"timeout": 60000, "waitUntil": "networkidle0"} ))
        await oneHref2.click()
        await navPromise2
        print('current url 3 is ', await page.evaluate("() => window.location.href"))
        print( 'next link ')

        while True:
            content, page = await solve_captcha( page )
            if  ('captchaText' not in content) and ('<div' in content):
                return page

async def handle_step_two( page, browser ):
        await page.goto( "https://service2.diplo.de/rktermin/extern/appointment_showForm.do?action%3Aappointment_refreshCaptcha=Load+another+picture&locationCode=pris&realmId=362&categoryId=591" ,  {'waitUntil': 'domcontentloaded'}  )
        print('current url is ', await page.evaluate("() => window.location.href"))
        time.sleep( 3 )
        page = await browser.pages()
        return page[1]

async def handle_step_three( page, line ):
        # start_step_two()
        print(' handle two')
        link = "https://service2.diplo.de/rktermin/extern/appointment_addAppointment.do?locationCode=pris&realmId=362&categoryId=591&dateStr="+ date_str + "&openingPeriodId="+opening_period_id

        await page.goto( link, {'waitUntil': 'domcontentloaded'} )

        print('current url is ', await page.evaluate("() => window.location.href"))

        await page.evaluate( f"""() => {{
            document.getElementById("appointment_newAppointmentForm_fields_3__content").checked=true;
            }}""" )
        await page.evaluate( f"""() => {{
            document.getElementById("appointment_newAppointmentForm_lastname").value = '{line[0]}';
            }}""" )
        await page.evaluate( f"""() => {{
            document.getElementById("appointment_newAppointmentForm_firstname").value = '{line[1]}';
            }}""" )
        await page.evaluate( f"""() => {{
            document.getElementById("appointment_newAppointmentForm_email").value = '{line[2]}';
            }}""" )
        await page.evaluate( f"""() => {{
            document.getElementById("appointment_newAppointmentForm_emailrepeat").value = '{line[3]}';
            }}""" )
        await page.evaluate( f"""() => {{
            document.getElementById("fields0content").value = '{line[4]}';
            }}""" )
        await page.evaluate( f"""() => {{
            document.getElementById("appointment_newAppointmentForm_fields_1__content").value = '{line[5]}';
            }}""" )
        await page.evaluate( f"""() => {{
            document.getElementById("appointment_newAppointmentForm_fields_2__content").value = '{line[6]}';
            }}""" )
        while True:
            content, page = await solve_captcha2( page )
            if  ('captchaText' not in content) and ('<div' in content):
                break

        if 'appointment_thanx.do' in await page.evaluate("() => window.location.href"):
            success.append( line )
            df = pd.DataFrame( success )
            df.to_csv('results/thanx.csv', index=False, header=['Last_Name', 'First_Name', 'Email', 'Repeat_email', 'Date_of_birth', 'Passport_number', 'Phone_number', 'Date_of_birth2'])

        elif 'appointment_showDay.do' in await page.evaluate("() => window.location.href"):
            failsDay.append( line )
            df = pd.DataFrame( failsDay )
            df.to_csv('results/showday.csv', index=False, header=['Last_Name', 'First_Name', 'Email', 'Repeat_email', 'Date_of_birth', 'Passport_number', 'Phone_number', 'Date_of_birth2'])

        elif 'The server is currently busy' in await page.evaluate("() => window.location.href"):
            # driver.refresh()
            failsBusy.append( line )
            df = pd.DataFrame( failsBusy )
            df.to_csv('results/serverbusy.csv', index=False, header=['Last_Name', 'First_Name', 'Email', 'Repeat_email', 'Date_of_birth', 'Passport_number', 'Phone_number', 'Date_of_birth2'])

        elif 'appointment_showMonth.do' in await page.evaluate("() => window.location.href"):
            failsMonth.append( line )
            df = pd.DataFrame( failsMonth )
            df.to_csv('results/showmonth.csv', index=False, header=['Last_Name', 'First_Name', 'Email', 'Repeat_email', 'Date_of_birth', 'Passport_number', 'Phone_number', 'Date_of_birth2'])

        else:
            unknown.append( line )
            df = pd.DataFrame( unknown )
            df.to_csv('results/unknown.csv', index=False, header=['Last_Name', 'First_Name', 'Email', 'Repeat_email', 'Date_of_birth', 'Passport_number', 'Phone_number', 'Date_of_birth2'])

async def solve_captcha( page ):
        print( " soleve captch ")
        content = await page.evaluate('() => document.body.innerHTML')
        # content = await page.content()
        print("content")
        print( content )
        tree = html.fromstring( content )
        captcha = tree.xpath( "//captcha/div/@style" )[0].split("'")[1].replace( "data:image/jpg;base64", "")
        path = hashlib.md5( captcha.encode() ).hexdigest()
        f = open( f'captchas/{path}.jpeg', 'wb' )
        f.write( base64.b64decode( captcha ))
        f.close()

        captcha_file = open( f'captchas/{path}.jpeg', 'rb')
        captcha2 = api.solve(captcha_file)
        print( " api soleve is")
        print( captcha2.await_result() )
        captcha_file.close()
        code = captcha2.await_result()
        await page.evaluate( f"""() => {{ document.getElementById("appointment_captcha_month_captchaText").value = '{code}'; }}""")
        time.sleep( 0.1 )
        element = await page.querySelector('#appointment_captcha_month_appointment_showMonth')

        navPromise = asyncio.ensure_future( page.waitForNavigation( {"timeout": 60000, "waitUntil": "networkidle0"} ))

        await element.click()
        await navPromise
        time.sleep( 4 )
        content = await page.evaluate('() => document.body.innerHTML')
        return content, page
async def solve_captcha2( page ):
        content = await page.evaluate('() => document.body.innerHTML')
        tree = html.fromstring( content )
        captcha = tree.xpath( "//captcha/div/@style" )[0].split("'")[1].replace( "data:image/jpg;base64", "")
        path = hashlib.md5( captcha.encode() ).hexdigest()
        f = open( f'captchas/{path}.jpeg', 'wb' )
        f.write( base64.b64decode( captcha ))
        f.close()

        captcha_file = open( f'captchas/{path}.jpeg', 'rb')
        captcha2 = api.solve(captcha_file)
        print( captcha2.await_result() )
        captcha_file.close()
        code = captcha2.await_result()
        await page.evaluate( f"""() => {{ document.getElementById("appointment_newAppointmentForm_captchaText").value = '{code}'; }}""")
        time.sleep( 0.1 )
        element = await page.querySelector('#appointment_newAppointmentForm_appointment_addAppointment')

        navPromise = asyncio.ensure_future( page.waitForNavigation( {"timeout": 60000, "waitUntil": "networkidle0"} ))

        await element.click()
        time.sleep( 2 )
        await navPromise
        content = await page.evaluate('() => document.body.innerHTML')
        return content, page

# global vars
success = []
failsDay = []
failsMonth = []
failsBusy = []
unknown = []

f = open('period.txt')
date_str, opening_period_id, googlePath = f.read().splitlines()

f.close()
print(date_str, opening_period_id, googlePath)
try:
	makedirs('captchas')
except:
	pass
try:
	makedirs('results')
except:
	pass

# start_step_two()
data = pd.read_csv( 'diplo.de.csv', dtype=str).values.tolist()

async def main( line ):
        browser = await launch( headless=False, autoclose=False, executablePath=googlePath )
        page = await browser.newPage()

        try:
            resultPage = await handle_step_one( page )
            page2 = await handle_step_two( resultPage, browser )
            await handle_step_three( page2 , line )
        except:
            await browser.close()

loop = asyncio.get_event_loop()
loop.create_task( main(data[0]) )
# loop.create_task( main(data[1]) )
# loop.create_task( main(data[2]) )
# loop.create_task( main(data[3]) )

try:
    loop.run_forever()
except KeyboardInterrupt:
    pass


""" Task1
Створіть клас, який описує автомобіль.
Які атрибути та методи мають бути повністю інкапсульовані?
Доступ до таких атрибутів та зміну даних реалізуйте через методи (get, set).
"""

class Vehicle:
    """
    Base class for Car
    Vehicle with methods: drive('step', 'direction'), stop()
    """

    _DRIVING_STEPS = {'drive': ['forward', 'back'],
                      'turn': ['left', 'right']}

    @classmethod
    def _verify_driving_step(cls, step):
        if step not in cls._DRIVING_STEPS.keys():
            raise ValueError(f"Incorrect step. Available steps {cls._DRIVING_STEPS}")
        if step == "drive":
            return "drives"
        elif step == "turn":
            return "turns"

    @classmethod
    def _verify_driving_direction(cls, step, direction):
        if direction not in cls._DRIVING_STEPS[step]:
            raise ValueError(f"Incorrect direction. Available direction{cls._DRIVING_STEPS[step]}")
        return direction

    def drive(self, step, direction):
        direction = self._verify_driving_direction(step, direction)
        step = self._verify_driving_step(step)
        print(f"The vehicle {step} {direction}")

    def stop(self):
        print(f"The vehicle was stopped")

# test
if __name__ == "__main__":
    car = Vehicle()
    car.drive('drive', 'forward')
    car.drive('turn', 'right')
    car.stop()

hw3 Encapsulation and polymorphism/task1_1_vehicle.py


# Задание 7) Инициализировать две переменные. Поменять их значения местами
#* не вводя третью
#* a, b = b, a - таким не пользоваться
var_1 = 8
var_2 = 9
var_1 = var_1 + var_2   # находим сумму двух элементов var_1: 8+9=17
var_2 = var_1 - var_2   # переменную два выражаем через "сумма-элемент" var_2: 17-9
var_1 = var_1 - var_2   # переменную один выражаем через "сумма-элемент" var_1: 17-8
print("Переменная один =", var_1)
print("Переменная два =", var_2)

Задание 7.py


# factorial
# 3  1*2*3=6
# 5 1*2*3*4*5=120

n=int(input("ente number"))
fact=1
for i in range(1,n+1):
    fact=fact*i
print("factorial of",n,"=",fact)


#Import numpy module as np
import numpy as np

d1 = np.zeros((9, ), np.int32)
d1 = d1.reshape((-1, 3))

print(d1)

'''
   Creating some numpy arrays
'''
#Filled with zeros
d1_zeros = np.zeros((5), np.float32) #1d float32 zeros array
d2_zeros = np.zeros((2, 3), np.int32) #2d int32 zeros array
d5_zeros = np.zeros([i for i in range(5, 26, 5)], np.uint8) #5d uint8 zeros array

#Filled with ones
d5_ones = np.ones(d5_zeros.shape, np.bool) #5d bool ones array

#From python list
list2d = [[1, 3], [4, 5]]
d2_array = np.array(list2d, np.int32) #2d int32 array

#From uniform distribution
u4d = np.random.uniform(size=(5, 5, 5, 5)) #5x5x5x5 uniform array [0, 1.0)
u2d = np.random.uniform(low=-2.5, high=5.0, size=(2, 2)) #2x2 uniform array [-2.5, 5.0)
u3d_int = np.random.random_integers(low=5, high=15,size=(5, 5, 6)) #5x5x6 uniform int array [5, 15]

#From normal (gaussian) distribution
#3x3x8 array with elements draw from a normal distribution with 0 mean and 5 std.
g3d = np.random.normal(loc=0.0, scale=5.0, size=(3, 3, 8))

'''
   Basic operations
'''
#Getting the shape of an array
print ('Shape', g3d.shape)

#Getting the type
print ('Type', g3d.dtype)

#Changing the shape
u1d = u2d.reshape(-1)

print ('u2d new shape', u1d.shape)

#Changing the type
u3d = u3d_int.astype(np.float32)

print ('u3d new type', u3d.dtype)

#Swap axis
print ('u3d_int swapped axis shape', u3d_int.swapaxes(2, 0).shape)

#Copy array
u2d_copy = u2d.copy()

#Element-wise operations
sum_cte = d2_zeros + 5
sum2d = u2d + d2_array
rest_cte = d2_zeros - 1
rest2d = u2d - d2_array
prod_cte = sum_cte*4
prod2d = u2d * d2_array
div_cte = sum_cte/2
div2d = u2d / d2_array
exp_cte = sum_cte**5
exp2d = u2d**d2_array

#Absolute value
u2d_abs = np.abs(u2d)

print ('Min u2d', u2d.min(), 'Min u2d_abs', u2d_abs.min())

#Transpose
print ('d2_zeros transpose shape', d2_zeros.transpose().shape)

#Matrix product
mprod2d = u2d.dot(sum_cte)

#Inverse
i1 = np.linalg.inv(np.eye(2))

#SVD
U, s, V = np.linalg.svd(u2d)

#Mean
u2d_mean = u2d.mean()
u2d_mean_rows = u2d.mean(axis=1)

print ('Mean:', u2d_mean, u2d_mean_rows)

#STD
u2d_std = u2d.std()
u2d_std_rows = u2d.std(axis=1)

print ('STD:', u2d_std, u2d_std_rows)

#Sum
u2d_sum = u2d.sum()
u2d_sum_colums = u2d.sum(axis=0)

print ('Sum:', u2d_sum, u2d_sum_colums)

#Save numpy array
np.save('u2d.npy', u2d)
u2d_loaded = np.load('u2d.npy')

print ('Diference between loaded u2d and u2d', np.abs(u2d-u2d_loaded).max() < 10**(-5))

'''
   Indexing
'''
print ('Array from 1 to 7 step 2', u4d.reshape(-1)[1:7:2])
print ('Array from 1 to end step 2', u4d.reshape(-1)[1::2])
print ('Array from ini to end step 2', u4d.reshape(-1)[::2])
print ('Array from 1 to 7 step 1', u4d.reshape(-1)[1:7:])

#Negative index
print ('Last element', u1d[-1], 'Element three before the last position', u1d[-3])

#Integer array indexing
print ('Selecting elemnts 1, 2 and 3', u1d[[1, 2, 3]])

#Bolean array Indexing
no_u2d_neg = u2d.copy()
no_u2d_neg[no_u2d_neg < 0] = 0

print ('u2d no negative elements min', no_u2d_neg.min())


"""Recommender Tool for Answerable

This file contains the recommendation algorithm.
"""

from bs4 import BeautifulSoup as bs
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
from sklearn.metrics.pairwise import linear_kernel

def recommend(user_qa, feed):

    answered = [
        x[0]["title"] + " " + bs(x[0]["body"], "html.parser").getText(" ", strip=True)
        for x in user_qa
    ]
    tags_ans = [" ".join(x[0]["tags"]) for x in user_qa]

    questions = [x["title"] + x["body"] for x in feed]
    tags_unans = [" ".join(x["tags"]) for x in feed]

    nans = len(answered)
    nunans = len(questions)

    """
    The following code is an adapted version of the Content-Based recommmender
    described in this tutorial:

    https://www.datacamp.com/community/tutorials/recommender-systems-python
    """

    tfidf = TfidfVectorizer(stop_words="english")
    count = CountVectorizer(stop_words="english")

    # list of vectorized body and tags
    tfidf_matrix = tfidf.fit_transform(answered + questions)
    count_matrix = count.fit_transform(tags_ans + tags_unans)

    # similarity matrices: without and with tags
    cosine_sim_body = linear_kernel(tfidf_matrix, tfidf_matrix)
    cosine_sim_tags = linear_kernel(count_matrix, count_matrix) + cosine_sim_body

    # rows: unanswered, cols: answered
    unans_similarity_body = cosine_sim_body[nans:, :nans]
    unans_similarity_tags = cosine_sim_tags[nans:, :nans]

    # form of the following lists: [(feed index, value)]
    sum_sim_body = enumerate([sum(r) for r in unans_similarity_body])
    max_sim_body = enumerate([max(r) for r in unans_similarity_body])
    sum_sim_tags = enumerate([sum(r) for r in unans_similarity_tags])
    max_sim_tags = enumerate([max(r) for r in unans_similarity_tags])

    # sort the indices by the value
    sort_sum_sim_body = sorted(sum_sim_body, key=lambda x: x[1], reverse=True)
    sort_max_sim_body = sorted(max_sim_body, key=lambda x: x[1], reverse=True)
    sort_sum_sim_tags = sorted(sum_sim_tags, key=lambda x: x[1], reverse=True)
    sort_max_sim_tags = sorted(max_sim_tags, key=lambda x: x[1], reverse=True)

    # map each index to its classifications
    by_sum_body = {x[0]: i for i, x in enumerate(sort_sum_sim_body)}
    by_max_body = {x[0]: i for i, x in enumerate(sort_max_sim_body)}
    by_sum_tags = {x[0]: i for i, x in enumerate(sort_sum_sim_tags)}
    by_max_tags = {x[0]: i for i, x in enumerate(sort_max_sim_tags)}

    # compute the mean classification for each index
    mean_index = []
    for i in range(nunans):
        mean = (by_sum_body[i] + by_sum_tags[i] + by_max_body[i] + by_max_tags[i]) / 4
        mean_index.append((mean, i))

    # build the final recommended feed order
    by_mean = [x[1] for x in sorted(mean_index)]

    return by_mean, None


from django.http import JsonResponse
from ..models import Post, Categoria, Tema, Comentario

def routes(request):
    routes=[
        'GET /api/posts',
        'GET /api/post/:id'
    ]
    return JsonResponse(routes, safe=False)

def posts(request):
    posts = Post.objects.all()
    posts_dict = []
    for p in posts:
        posts_dict.append({
            'titulo': p.titulo,
            'contenido': p.contenido,
            'usuario': p.usuario.username,
            'tema': p.tema.titulo,
            'id': p.id
        })
    return JsonResponse(posts_dict, safe=False)

def post(request, id):
    post = Post.objects.get(id=id)
    post_dict = {
        'titulo': post.titulo,
        'contenido': post.contenido,
        'usuario': post.usuario.username,
        'tema': post.tema.titulo
    }
    return JsonResponse(post_dict, safe=False)

def categorias(request):
    cat = Categoria.objects.all()
    cat_dict = []
    for c in cat:
        cat_dict.append({
            'nombre': c.nombre
        })
    return JsonResponse(cat_dict, safe=False)

def temas(request):
    temas = Tema.objects.all()
    tema_dict = []
    for t in temas:
        tema_dict.append({
            'titulo': t.titulo,
            'contenido': t.contenido,
            'categoria': t.categoria.nombre
        })
    return JsonResponse(tema_dict, safe=False)

def comentarios(request,id):
    com = Comentario.objects.filter(post=id)
    com_dict = []
    for c in com:
        com_dict.append({
            'usuario': c.usuario.username,
            'contenido': c.contenido
        })
    return JsonResponse(com_dict, safe=False)


import numpy as np
import cv2
import matplotlib.pyplot as plt

counter = 1
def onChange(pos):
    global counter
    T1 = cv2.getTrackbarPos('T1','img')
    T2 = cv2.getTrackbarPos('T2','img2')

    _,gray_bin = cv2.threshold(gray,T1,255,cv2.THRESH_BINARY)
    _,gray2_bin = cv2.threshold(gray,T2,255,cv2.THRESH_BINARY)

    cv2.imshow('img',gray_bin)          #3번) 이진화한 영상을 표시
    cv2.imshow('img2',gray2_bin)

    if counter > 1 :
        draw_hist(T1,T2)
    counter +=1

def draw_hist(T1,T2):                   #4번) 히스토그리기 함수 설정

    hist = cv2.calcHist(images = [gray] , channels = [0], mask = None,
                     histSize= [256] , ranges = [0,256])
    hist = hist.flatten()                #4번) histogram을 gray로 부터 생성
    binX = np.arange(256)                #4번) bin을 256개로 설정
    plt.cla()
    plt.title('histogram')
    plt.bar(binX,hist,width = 1 , color ='g')                   #4번) bar차트를 이용해서 시각화
    plt.vlines(T1, ymin = 0, ymax = 3000, colors = 'orange')    #4번) T1,T2,가 bar차트에 함께 표시되도록함
    plt.text(T1, 3000, 'T1')
    plt.vlines(T2, ymin = 0, ymax = 3000, colors = 'blue')
    plt.text(T2, 3000, 'T2')
    plt.show()

gray = cv2.imread('data/cell.png',cv2.IMREAD_GRAYSCALE)  #1번) 원본 영상을 gray로 읽어들여 gray로 치환

cv2.imshow('img',gray)
cv2.imshow('img2',gray)

cv2.createTrackbar('T1','img',0,255,onChange)       #2번) 트랙바 두개를 생성
cv2.createTrackbar('T2','img2',0,255,onChange)

cv2.setTrackbarPos('T2','img2',230)                 #2번) 이진화를 적용할 임계값 230과 75를 각각 설정
cv2.setTrackbarPos('T1','img',75)


from  utils import create_dataset, load_processed_data, test_model
from train import trainer
from vis_utils import predict_on_live_video
import os

print("1: Create Dataset 2: Train Model 3: Test 4: Video Test")
while True:
  choice = int(input("Enter Your Choice :"))

  if choice ==1:
      create_dataset()
  if choice == 2:
      trainer()
  if choice ==3:

      test_model()
  if choice==4:
      print("Test on video file")

      filepath = os.getcwd()+'/UCF50/BaseballPitch/v_BaseballPitch_g01_c01.avi'
      outputpath = os.getcwd()+'Outputfiles/out1.mp4'
      window_size = 10

      predict_on_live_video(filepath, outputpath, window_size)


#! python3
# grid map: an exploration of sensitive
# dependance on initial conditions using
# a semicontinuous map.

# import third party libraries
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('dark_background')

def grid_map(x, y, a=0.1, b=0.1):
	"""
	Differential equation to produce the grid map

	Args:
		x: float
		y: float
	kwargs:
		a: float, parameter
		b: float, parameter

	Returns:
		x_n: float, next x value
		y_n: float, next y value

	"""

	x_n = a * np.cos(y)
	y_n = b * np.sin(x)

	return x_n, y_n

def produce_grid(steps, delta_t):
	"""
	Display a grid map trajectory using Euler's method

	Args:
		steps: int, number of iterations
		delta_t: float, size of time step

	Returns:
		None

	"""
	# solution array initialization
	x_arr, y_arr = [], []

	# Initial position
	x_arr.append(1.000000) , y_arr.append(0)

	# differential equation iterations
	for i in range(steps):
		current_x, current_y = x_arr[-1], y_arr[-1]

		x_dot, y_dot = grid_map(current_x, current_y)
		x_arr.append(current_x + x_dot * delta_t)
		y_arr.append(current_y + y_dot * delta_t)

	# plot figure
	plt.figure(figsize=(10,10))
	plt.plot(x_arr, y_arr, '^', color='white', alpha=1, markersize=0.1)
	plt.axis('on')
	plt.show()
	plt.close()

# step parameters
steps = 1000000
delta_t = 1

produce_grid(steps, delta_t)


from fltk import *
import os
import subprocess as sp
import signal
import platform

class mp3player(Fl_Window):
	def __init__(self, w, h, label = None):

		self.last = 0 #pos of last song
		self.dfiles = {} #dict for songs
		self.playing = False #sets playing state

		#window
		Fl_Window.__init__(self,w,h,label)
		self.begin()

		#Menubar

		menu = Fl_Menu_Bar(0,0,w,25)
		menu.add("Add/Directory",100, self.addcb)
		menu.add("Clear/All",FL_ALT|97, self.clrcb)
		menu.add("Go/First",102,self.firstcb)
		menu.add("Go/Playing",112, self.cplaycb)
		menu.add("Go/Last",108,self.lastcb)

		#output
		self.out = Fl_Output(0,25,w,25)
		self.out.color(FL_YELLOW)

		#Browser
		self.brow = Fl_Hold_Browser(0,50,w,270)

		#Buttons
		Pack = Fl_Pack(0,320,w,80)
		Pack.begin()

		back_but = Fl_Button(0,self.h()-80,80,80,'@|<')
		back_but.tooltip("Previous (ALT <-)")
		back_but.shortcut(FL_ALT|FL_Left)
		back_but.callback(self.backcb)

		play_but = Fl_Button(80,self.h()-80,80,80,'@>')
		play_but.tooltip("Play (Enter)")
		play_but.shortcut(FL_Enter)
		play_but.callback(self.playcb)

		next_but = Fl_Button(160,self.h()-80,80,80,'@>|')
		next_but.tooltip("Next (ALT ->)")
		next_but.shortcut(FL_ALT|FL_Right)
		next_but.callback(self.nextcb)

		stop_but = Fl_Button(240,self.h()-80,80,80,'@square')
		stop_but.tooltip("Stop (ALT S)")
		stop_but.shortcut(FL_ALT|115)
		stop_but.callback(self.stopcb)

		del_but = Fl_Button(320,self.h()-80,80,80,'@undo')
		del_but.tooltip("Remove (Delete)")
		del_but.shortcut(FL_Delete)
		del_but.callback(self.delcb)

		Pack.end()
		Pack.type(FL_HORIZONTAL)

		self.callback(self.closewin)
		self.end()
		self.resizable(self.brow)

	#MENUBAR METHODS
	#add files to playlist
	def addcb(self,wid):
		di = fl_dir_chooser("Open directory","")
		li = os.listdir(di)
		for name in li:
			if name[-4:] in ('.mp3'):
				self.brow.add(name[:-4])
				self.dfiles[name[:-4]] = os.path.normpath(os.path.join(di,name))
				self.last +=1
		self.brow.select(1)

	#clear files from playlist
	def clrcb(self,wid):
		self.brow.clear()
		self.delcb(self.brow)
		self.last == 0
		if self.playing == True:
			self.proc.send_signal(signal.SIGTERM)
			self.playing = False
			self.out.value('')

	#go to first
	def firstcb(self,wid):
		self.brow.select(1)

	#to to current
	def cplaycb(self,wid):
		self.brow.select(self.playingV)

	#go to last
	def lastcb(self,Fl_Browser_):
		self.brow.select(self.last)

	#BUTTS METHODS

	#plays previous song
	def backcb(self,wid):
		try:
			self.proc.send_signal(signal.SIGTERM)
			self.playing = False
		except:
			pass

		pos = self.brow.value()
		if pos == 1:
			self.brow.select(self.last)
		else:
			self.brow.select(self.brow.value()-1)
		self.playcb(self.brow)

	#play song
	def playcb(self,wid):
		try:
			if self.playing == False:
				if platform.system() == "Windows":
					self.proc = sp.Popen(['C:\Program Files (x86)\VideoLAN\VLC\VLC.exe','--intf','dummy',self.dfiles.get(self.brow.text(self.brow.value()))])
					self.playing = True
					self.out.value(self.brow.text(self.brow.value()))
					self.playingV = self.brow.value()

				elif platform.system() == "Linux":
					self.proc = sp.Popen(['vlc','--intf','dummy',self.dfiles.get(self.brow.text(self.brow.value()))])
					self.playing = True

			elif self.playing == True:
				try:
					self.proc.send_signal(signal.SIGTERM)
					self.playing = False
				except:
					pass
				self.playcb(self.brow)
		except:
			pass

	#plays next song
	def nextcb(self,wid):
		try:
			self.proc.send_signal(signal.SIGTERM)
			self.playing = False
		except:
			pass

		pos = self.brow.value()
		if pos == self.last:
			self.brow.select(1)
		else:
			self.brow.select(self.brow.value()+1)
		self.playcb(self.brow)

	#stop song
	def stopcb(self,wid):
		if self.playing == True:
			self.proc.send_signal(signal.SIGTERM)
			self.playing = False
			self.out.value('')

	#del files
	def delcb(self,wid):
		pos = self.brow.value()
		self.brow.remove(pos)
		self.brow.select(pos)

		if pos == self.playingV:
			if self.playing == True:
				self.proc.send_signal(signal.SIGTERM)
				self.playing = False
				self.out.value('')
				self.last -= 1
		else:
			self.last -= 1

	#SYNTAX METHODS
	#close win
	def closewin(self,wid):
		try:
			if self.playing == True:
				self.proc.send_signal(signal.SIGTERM)
				self.playing = False
				wid.hide()
			else:
				wid.hide()
		except:
			wid.hide()

app = mp3player(400,400,"MP3 Player")
app.show()
Fl.run()


# -*- coding: utf-8 -*-
"""
Created on Sun Aug  7 09:12:12 2022

@author: Atulan
"""

import numpy as np
import tensorflow as tf

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import TimeDistributed, Dense, SimpleRNN, RepeatVector
from tensorflow.keras.callbacks import EarlyStopping, LambdaCallback

from termcolor import colored

allChars = "90348756921+"

print(len(allChars))

# preparing Data for RNN
# 1. CTI
# 2. ITC
char_to_index = dict((c, i) for i, c in enumerate(allChars))
index_to_char = dict((i, c) for i, c in enumerate(allChars))
char_to_index

def numberGenerate():
    firstNumber = np.random.randint(3, 10) # 1 value between 3 and 10
    SecNumber = np.random.randint(3, 10)
    newValue = str(firstNumber) + "+"+str(SecNumber)
    finalValue = str(firstNumber+SecNumber)
    return newValue, finalValue

numberGenerate()

# model = Sequential()
# model.add(Conv2D(128, input_shape = (128, 128))  = 10
# model.add(Maxpooling2D(pool_size = (2, 2)))) = 7

# model.add(Conv2D(128, activation = "relu"))
# model.add(Maxpooling2D(pool_size = (2, 2))))

model = Sequential([

    SimpleRNN(128, input_shape = (None, len(allChars))),
    RepeatVector(3),

    SimpleRNN(128, return_sequences = True),
    TimeDistributed(Dense(len(allChars), activation = "softmax"))])

model.compile(loss = "categorical_crossentropy",
             optimizer = "adam",
             metrics = ["accuracy"])

model.summary()

def vector(newvalue, finalvalue):

    x = np.zeros((3, len(allChars))) # create a (3,10) array
    y = np.zeros((3, len(allChars)))

    difference_x = 3 - len(newvalue)  # newvalue= 5+4, len(newvalue) = 3, hence difference_x=0
    difference_y = 3 - len(finalvalue) # finalvalue= 9, len(finalvalue)=1, hence difference_y=2

    for i, c in enumerate(newvalue): # 5+4
        x[difference_x + i, char_to_index[c]] = 1

    for i in range(difference_x):
        x[i, difference_x["0"]] = 1

    for i, c in enumerate(finalvalue):
        y[difference_y + i, char_to_index[c]] = 1

    for i in range(difference_y):
        y[i, char_to_index["0"]] = 1

    return x, y

newValue, finalValue = numberGenerate()

print("New Value: ", newValue)
print("Final Value: ", finalValue)

x, y = vector(newValue, finalValue)

print("X Value: ", x)
print("\n")
print("Y Value: ", y)

def ReturnVector(value):
    data = [index_to_char[np.argmax(vector)] for i, vector in enumerate(value)] # take vector, take the max value from that and convert it to char
    return "".join(data)

ReturnVector(x)

ReturnVector(y)

def trainData_and_TestData(num_samples = 100000):

    x_trainData = np.zeros((num_samples, 3, len(allChars)))
    y_trainData = np.zeros((num_samples, 3, len(allChars)))

    for i in range(num_samples):
        newValue, finalValue = numberGenerate()
        x, y = vector(newValue, finalValue)

        x_trainData[i] = x
        y_trainData[i] = y

    return x_trainData, y_trainData

x_trainData, y_trainData = trainData_and_TestData()

#print("X Train Data: \n", x_trainData)

x_trainData.shape # (10000,3,12)-(num_samples, repetition, length of character)

ReturnVector(y_trainData[0])

# fit our NN
Call_backs =  LambdaCallback(
    on_epoch_end = lambda newValue, finalValue: print("{:.2f}".format(finalValue["accuracy"]), end = "|"))

earlyStop = EarlyStopping(monitor = "val_loss", patience=10)

model.fit(x_trainData,
         y_trainData,
         epochs=10,
         validation_split=0.2,
         verbose = False,
         callbacks = [Call_backs, earlyStop])


import json
import requests
from data_manager.MySqlPersistenceHelper import MySqlPersistenceHelper
import re
from time import time

class Segmentor:

    tag_url = 'http://api.bosonnlp.com/tag/analysis?space_mode=0&oov_level=4&t2s=1&special_char_conv=0'

    headers = {'X-Token': '32LFi6iQ.12850.alDbxNKXRFp6'}

    def __init__(self):
        self.connection = MySqlPersistenceHelper.get_connection()
        self.cursor = self.connection.cursor()

    @staticmethod
    def split_text_into_sentences(text):
        splits = re.split(r'[ !！,，.。?？;；~:：]', text)
        sentences = [split for split in splits if split is not '']
        punctuation = '''！？｡＂＃＄％＆＇（）＊＋，－／：；＜＝＞＠［＼］＾＿｀｛｜｝～｟｠｢｣､、〃《》「」『』【】〔〕〖〗〘〙〚〛
                        〜〝〞〟〰〾〿–—‘’‛“”„‟…‧﹏.!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'''
        exclude = set(punctuation)
        return [''.join(ch for ch in sentence if ch not in exclude) for sentence in sentences]

    @staticmethod
    def get_sentence_segment_from_boson(text):
        sentences = Segmentor.split_text_into_sentences(text)
        data = json.dumps(sentences)
        resp = requests.post(url=Segmentor.tag_url, headers=Segmentor.headers, data=data.encode('utf-8'))
        return [' '.join(dict['word']) for dict in resp.json()]

    def process_segment(self):
        table = 'movie_review'
        ran_sql = 'select * from {0} where sentenceSegment is not null'.format(table)
        self.cursor.execute(ran_sql)
        row_num = len(self.cursor.fetchall())
        begin_row_num = row_num
        restart_sql = 'select * from {0} where sentenceSegment is null'.format(table)
        self.cursor.execute(restart_sql)
        records = self.cursor.fetchall()
        total_records = len(records) + row_num

        t1 = time()

        for record in records:
            comment_id = record[0]
            comment = record[2]
            sentences = '\n'.join(Segmentor.split_text_into_sentences(comment))
            sentences_segment = '\n'.join(Segmentor.get_sentence_segment_from_boson(comment, sentences))
            sql = '''update {0} set sentenceSegment = '{1}', wordSegment = '{2}' where commentId = {3}'''\
                .format(table, sentences, sentences_segment, comment_id)
            MySqlPersistenceHelper.execute_sql(self.connection, self.cursor, sql)
            row_num += 1
            print('processing {0} ...'.format(row_num))
            if (row_num - begin_row_num) % 100 == 0:
                t2 = time()
                elapsed_time = t2 - t1
                records_to_go = total_records - row_num
                print(elapsed_time, 'sec for {0}'.format(100), 'records')
                print(records_to_go, ' records left')
                print('estimate', records_to_go / 60.0, 'min to go')
                t1 = time()
        self.cursor.close()
        self.connection.close()

if __name__ == '__main__':
    sentences = [
        '非！常！可！怕！直！男！癌！婚！姻！中！年！危！机！下！的！意！淫！基！佬！设！定！是！娘！炮！非！常！傻！逼！',
        '剧作很直男癌，看的很恶心，你国那些中年男人和家庭已经够悲惨了，这部片子却拿来调侃，而且是用最低俗的模式，没有人物，都是符号，闫妮表演还行。喜剧用一个很老的套子坚持了一个多小时。过年其实可以多出去旅游或者聚会啥的，为啥非要去电影院呢？我们的娱乐项目难道只剩下电影院了咩？奇怪。',
        '一副烂片相，却意外能看下去。直男或直男癌视角性喜剧，三观不正又会很多人不喜欢吧。不过中年男的意淫不都那样。如所有中年危机一样，都是在对生活与工作失去激情，如何被梦中女神唤醒。改编自76年法国片，豆瓣叫大象骗人，剧情基本类似，包括模仿《七年之痒》。各种误会与巧合咬的比较好，不会很出戏',
        '我是笑了呀 但是笑完觉得好讨厌男主和他的朋友们呀 已婚男出轨搞事情还理直气壮了?'
    ]
    segmentor = Segmentor()
    segmentor.process_segment()


from prelog_msg import *
import time
import sys

class TokenList:
    counter = 0

    def __init__(self, *tokens_list):
        self.tokens = tokens_list
        self.active_token = self.tokens[0]

    def next_token(self):
        if self.counter < len(self.tokens)-1:
            self.counter += 1
        else:
            user_response = input(user_info + ' All tokens have been used. Do you want to start from begin (y/n)? ')
            if 'y' in user_response.lower():
                self.counter = 0
            else:
                print('Program has stopped. You can close the window or it will be automatically closed in 5 minutes')
                time.sleep(300)
                sys.exit(0)
        self.active_token = self.tokens[self.counter]

    def get_active_token(self):
        return self.active_token

    def get_token_order(self):
        return self.counter


from transformer.positionalEncoding import get_sinusoid_encoding_table
from transformer.encoder import *
from transformer.positionwiseFeedForward import PositionwiseFeedForward

import torch
import torch.nn as nn

class DoubleEncoder(torch.nn.Module):

    def __init__(self, vector_size=300, heads=8, hidden_layers=200, dropout=0.1):
        super(DoubleEncoder, self).__init__()
        self.feedforward = PositionwiseFeedForward(vector_size, hidden_layers)
        self.layer1 = EncoderLayer(vector_size, self.feedforward, dropout)
        self.layer2 = EncoderLayer(vector_size, self.feedforward, dropout)
        self.encoder1 = Encoder(self.layer1, heads)
        self.encoder2 = Encoder(self.layer2, heads)

    def forward(self, x1, x2, batch_size=100):

        # 计算positional encoding
        x1_n_position = x1.size()[1]
        x1_d_hid = x1.size()[2]
        x1_sinusoid_table = get_sinusoid_encoding_table(x1_n_position, x1_d_hid)

        x2_n_position = x2.size()[1]
        x2_d_hid = x2.size()[2]
        x2_sinusoid_table = get_sinusoid_encoding_table(x2_n_position, x2_d_hid)

        x1_sinusoid_table = x1_sinusoid_table.repeat(batch_size, 1, 1)
        x2_sinusoid_table = x2_sinusoid_table.repeat(batch_size, 1, 1)

        # 在原始向量加入位置编码
        x1_p = x1 + x1_sinusoid_table
        x2_p = x2 + x2_sinusoid_table

        x1_output = self.encoder1(x1_p)
        x2_output = self.encoder2(x2_p)

        return x1_output, x2_output


from django.conf.urls.defaults import *
from django.contrib.comments.urls import urlpatterns

urlpatterns = patterns('comments.views',
    url(r'^(?P<object_id>\d+)/edit/$',
        view='comment_edit',
        name='comments-edit'),

    url(r'^(?P<object_id>\d+)/remove/$',
        view='comment_remove',
        name='comments-remove'),

    # let's overwrite django.contrib.comments
    url(r'^post/$',
    	view='custom_comment_post',
    	name='comments-post-comment'),

    url(r'^subscribe/(?P<object_id>[-\w]+)/$',
    	view='comment_sub_manage',
    	name='comment-sub-manage'),

    url(r'^subscribe/(?P<object_id>[-\w]+)/remove/$',
    	view='comment_sub_remove',
    	name='comment-sub-remove'),

    url( r'^', include( 'django.contrib.comments.urls' ) ),
)


import torch

from quantization.hijacker import QuantizationHijacker

def add_oscillation_trackers(model, max_bits=4, *args, **kwarks):
    tracker_dict = {}
    # Add oscillation trackers to all weight quantizers
    for name, module in model.named_modules():
        if isinstance(module, QuantizationHijacker):
            q = module.weight_quantizer.quantizer
            if q.n_bits > max_bits:
                print(
                    f"Skip tracking/freezing for {name}, too high bit {q.n_bits} (max {max_bits})"
                )
                continue
            int_fwd_wrapper = TrackOscillation(int_fwd=q.to_integer_forward, *args, **kwarks)
            q.to_integer_forward = int_fwd_wrapper
            tracker_dict[name + ".weight_quantizer"] = int_fwd_wrapper
    return tracker_dict

class TrackOscillation:
    """
    This is a wrapper of the int_forward function of a quantizer.
    It tracks the oscillations in integer domain.
    """

    def __init__(self, int_fwd, momentum=0.01, freeze_threshold=0, use_ema_x_int=True):
        self.int_fwd = int_fwd
        self.momentum = momentum

        self.prev_x_int = None
        self.prev_switch_dir = None

        # Statistics to log
        self.ema_oscillation = None
        self.oscillated_sum = None
        self.total_oscillation = None
        self.iters_since_reset = 0

        # Extra variables for weight freezing
        self.freeze_threshold = freeze_threshold  # This should be at least 2-3x the momentum value.
        self.use_ema_x_int = use_ema_x_int
        self.frozen = None
        self.frozen_x_int = None
        self.ema_x_int = None

    def __call__(self, x_float, skip_tracking=False, *args, **kwargs):
        x_int = self.int_fwd(x_float, *args, **kwargs)

        # Apply weight freezing
        if self.frozen is not None:
            x_int = ~self.frozen * x_int + self.frozen * self.frozen_x_int

        if skip_tracking:
            return x_int

        with torch.no_grad():
            # Check if everything is correctly initialized, otherwise do so
            self.check_init(x_int)

            # detect difference in x_int  NB we round to avoid int inaccuracies
            delta_x_int = torch.round(self.prev_x_int - x_int).detach()  # should be {-1, 0, 1}
            switch_dir = torch.sign(delta_x_int)  # This is {-1, 0, 1} as sign(0) is mapped to 0
            # binary mask for switching
            switched = delta_x_int != 0

            oscillated = (self.prev_switch_dir * switch_dir) == -1
            self.ema_oscillation = (
                self.momentum * oscillated + (1 - self.momentum) * self.ema_oscillation
            )

            # Update prev_switch_dir for the switch variables
            self.prev_switch_dir[switched] = switch_dir[switched]
            self.prev_x_int = x_int
            self.oscillated_sum = oscillated.sum()
            self.total_oscillation += oscillated
            self.iters_since_reset += 1

            # Freeze some weights
            if self.freeze_threshold > 0:
                freeze_weights = self.ema_oscillation > self.freeze_threshold
                self.frozen[freeze_weights] = True  # Set them to frozen
                if self.use_ema_x_int:
                    self.frozen_x_int[freeze_weights] = torch.round(self.ema_x_int[freeze_weights])
                    # Update x_int EMA which can be used for freezing
                    self.ema_x_int = self.momentum * x_int + (1 - self.momentum) * self.ema_x_int
                else:
                    self.frozen_x_int[freeze_weights] = x_int[freeze_weights]

        return x_int

    def check_init(self, x_int):
        if self.prev_x_int is None:
            # Init prev switch dir to 0
            self.prev_switch_dir = torch.zeros_like(x_int)
            self.prev_x_int = x_int.detach()  # Not sure if needed, don't think so
            self.ema_oscillation = torch.zeros_like(x_int)
            self.oscillated_sum = 0
            self.total_oscillation = torch.zeros_like(x_int)
            print("Init tracking", x_int.shape)
        else:
            assert (
                self.prev_x_int.shape == x_int.shape
            ), "Tracking shape does not match current tensor shape."

        # For weight freezing
        if self.frozen is None and self.freeze_threshold > 0:
            self.frozen = torch.zeros_like(x_int, dtype=torch.bool)
            self.frozen_x_int = torch.zeros_like(x_int)
            if self.use_ema_x_int:
                self.ema_x_int = x_int.detach().clone()
            print("Init freezing", x_int.shape)


import sys
import polib

if len(sys.argv) < 3:
    print('Usage : python3 mergePo.py gcompris_qt.po gcompris_gtk.po.'
          ' Output will be in the input file!')
    sys.exit(1)

# load an existing po file
po = polib.pofile(sys.argv[1])
poGtk = polib.pofile(sys.argv[2])

print(po.percent_translated())

# First remove all fuzzy strings
for entry in po.fuzzy_entries():
    entry.msgstr = ''
    if entry.msgid_plural:
        entry.msgstr_plural['0'] = ''
    if entry.msgid_plural and '1' in entry.msgstr_plural:
        entry.msgstr_plural['1'] = ''
    if entry.msgid_plural and '2' in entry.msgstr_plural:
        entry.msgstr_plural['2'] = ''
    entry.flags.remove('fuzzy')
    po.save()

# uncomment to create a new po
#for entry in po:
#    if entry.msgstr != "":
#        entry.msgstr = "";

#print(po.percent_translated())

# Then replace in the output all the good strings
for entry in po:
    if entry.msgstr != "":
        continue

    for entryGtk in poGtk:
        if entry.msgid.encode('utf-8') == entryGtk.msgid.encode('utf-8') and entryGtk.msgstr != "":
            entry.msgstr = entryGtk.msgstr

po.save()

print(po.percent_translated())


from django.contrib import admin
from .models import Section, Question, Answer, Response

# Register your models here.
class AnswerInline(admin.TabularInline):
    model = Answer

class QuestionAdmin(admin.ModelAdmin):
    inlines = [AnswerInline]

class QuestionInline(admin.TabularInline):
    model = Question

class SectionAdmin(admin.ModelAdmin):
    inlines = [QuestionInline]

admin.site.register(Section, SectionAdmin)
admin.site.register(Question, QuestionAdmin)
admin.site.register(Answer)
admin.site.register(Response)


from django.conf import settings
from django.conf.urls import patterns, include, url, static

urlpatterns = patterns('',
    url(r'^api/', include('social_event.urls')),
    url(r'^admin/', include('social_event_admin.urls')),
)

if settings.DEBUG:
    urlpatterns += static.static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


import numpy as np
import sys, os
sys.path.insert(0, '/app/tti')
from model import Model
from sources import RickerSource, TimeAxis, Receiver
from tti_propagators import TTIPropagators
import segyio as so
from scipy import interpolate, ndimage
from AzureUtilities import read_h5_model, write_h5_model, butter_bandpass_filter, butter_lowpass_filter, resample, process_summaries, read_coordinates, save_rec, restrict_model_to_receiver_grid, extent_gradient
from scipy import interpolate, ndimage

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

def limit_receiver_grid(xsrc, ysrc, xrec, yrec, zrec, maxoffset):

    xmin = np.max([xsrc - maxoffset, 12.5])
    xmax = np.min([xsrc + maxoffset, 9987.5])
    ymin = np.max([ysrc - maxoffset, 12.5])
    ymax = np.min([ysrc + maxoffset, 9987.5])

    print('xrange: ', xmin, ' to ', xmax)
    print('yrange: ', ymin, ' to ', ymax)

    xnew = []
    ynew = []
    znew = []

    for j in range(len(xrec)):
        if xrec[j] >= xmin and xrec[j] <= xmax and yrec[j] >= ymin and yrec[j] <= ymax:
            xnew.append(xrec[j])
            ynew.append(yrec[j])
            znew.append(zrec[j])

    xnew = np.array(xnew)
    ynew = np.array(ynew)
    znew = np.array(znew)

    return xnew, ynew, znew

def check_thomsen_parameters(model):
    err = np.where(model.delta.data > model.epsilon.data)
    num_err = np.sum([len(err[0]), len(err[1]), len(err[2])])
    if num_err > 0:
        print('Warning: delta > epsilon')

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

# Get runtime arguments and environment variables
if len(sys.argv) > 1:
    run_id = int(str(sys.argv[1]))
else:
    run_id = 0

logging = os.environ["DEVITO_LOGGING"]
num_threads = int(os.environ["OMP_NUM_THREADS"])
rootpath = os.environ["AZ_BATCH_NODE_SHARED_DIR"]

container = os.environ["BLOB_CONTAINER"]    # overthrust
space_order = int(os.environ["SPACE_ORDER"])
batchsize = int(os.environ["BATCHSIZE"])

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

# Read models
rho = read_h5_model(rootpath + '/overthrust/models/rho_with_salt.h5')
epsilon = read_h5_model(rootpath + '/overthrust/models/epsilon_with_salt.h5')
delta = read_h5_model(rootpath + '/overthrust/models/delta_with_salt.h5')
theta = read_h5_model(rootpath + '/overthrust/models/theta_with_salt.h5')
phi = read_h5_model(rootpath + '/overthrust/models/phi_with_salt.h5')
m0 = read_h5_model(rootpath + '/overthrust/models/migration_velocity.h5')
dm = read_h5_model(rootpath + '/overthrust/models/perturbation.h5')
dm[:, :, 0:29] = 0.0    # Extend water column
delta[delta >= epsilon] = .9 * epsilon[delta >= epsilon]    # fix thomsen parameters

# Read geometry
source_indices = np.load(rootpath + '/overthrust/geometry/source_indices.npy', allow_pickle=True)
idx = source_indices[run_id]
print('Process source no. ', idx, ' out of ', batchsize)

shape_full = (801, 801, 267)
origin_full = (0.0, 0.0, 0.0)
spacing = (12.5, 12.5, 12.5)

# Read coordinates and source index
file_src = rootpath + '/overthrust/geometry/src_coordinates.h5'
file_rec = rootpath + '/overthrust/geometry/rec_coordinates.h5'
xsrc, ysrc, zsrc = read_coordinates(file_src)
xrec, yrec, zrec = read_coordinates(file_rec)
xsrc = xsrc[idx]; ysrc = ysrc[idx]; zsrc = zsrc[idx]

# Limit receiver grid
buffersize = 500    # in m
maxoffset = 3287.5  # x/y direction in m
xrec, yrec, zrec = limit_receiver_grid(xsrc, ysrc, xrec, yrec, zrec, maxoffset)

# Restrict models to receiver grid
print('Original shape: ', shape_full, ' and origin: ', origin_full)
m0, shape, origin = restrict_model_to_receiver_grid(xsrc, xrec, m0, spacing, origin_full, buffer_size=buffersize, sy=ysrc, gy=yrec)
rho = restrict_model_to_receiver_grid(xsrc, xrec, rho, spacing, origin_full, buffer_size=buffersize, sy=ysrc, gy=yrec)[0]
epsilon = restrict_model_to_receiver_grid(xsrc, xrec, epsilon, spacing, origin_full, buffer_size=buffersize, sy=ysrc, gy=yrec)[0]
delta = restrict_model_to_receiver_grid(xsrc, xrec, delta, spacing, origin_full, buffer_size=buffersize, sy=ysrc, gy=yrec)[0]
theta = restrict_model_to_receiver_grid(xsrc, xrec, theta, spacing, origin_full, buffer_size=buffersize, sy=ysrc, gy=yrec)[0]
phi = restrict_model_to_receiver_grid(xsrc, xrec, phi, spacing, origin_full, buffer_size=buffersize, sy=ysrc, gy=yrec)[0]
dm = restrict_model_to_receiver_grid(xsrc, xrec, dm, spacing, origin_full, buffer_size=buffersize, sy=ysrc, gy=yrec)[0]
print('New shape: ', shape, ' and origin ', origin)

model = Model(shape=shape, origin=origin, spacing=spacing, vp=np.sqrt(1/m0), space_order=space_order,
              epsilon=epsilon, delta=delta, theta=theta, phi=phi, rho=rho, nbpml=40, dm=dm, dt=0.64)

check_thomsen_parameters(model)

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

# Time axis
t0 = 0.
tn = 2000.
dt_shot = 4.
nt = int(tn/dt_shot + 1)
dt = 0.64   #model.critical_dt*.8
time = TimeAxis(start=t0, step=dt, stop=tn)

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

rec_coords = np.concatenate((xrec.reshape(-1,1), yrec.reshape(-1,1), zrec.reshape(-1,1)), axis=1)
print('Number of receivers: ', rec_coords.shape[0])

# Source coordinates
src_coordinates = np.array([xsrc, ysrc, zsrc])
src = RickerSource(name='src', grid=model.grid, f0=0.02, time_range=time, npoint=1)
src.coordinates.data[0, 0] = src_coordinates[0]
src.coordinates.data[0, 1] = src_coordinates[1]
src.coordinates.data[0, 2] = src_coordinates[2]

# Wavelet
# wavelet = np.load(rootpath + '/overthrust/wavelet/wavelet_3D_2000.npy', allow_pickle=True)
# src.data[:] = wavelet

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

# Devito operator
tti = TTIPropagators(model, space_order=space_order)

# Data and RTM
d_obs, u0, v0, summary1 = tti.born(src, rec_coords, save=True, sub=(12, 1), autotune=('aggressive', 'runtime'))
grad, summary2 = tti.gradient(d_obs, u0, v0, sub=(12, 1), isic=True, autotune=('aggressive', 'runtime'))

# Crop PML and extend to full size
rtm = grad.data[model.nbpml:-model.nbpml, model.nbpml:-model.nbpml, model.nbpml:-model.nbpml]  # remove padding
rtm = extent_gradient(shape_full, origin_full, shape, origin, spacing, rtm)

# Save to bucket
write_h5_model(rootpath + '/overthrust/rtm/overthrust_3D_rtm_shot_' + str(idx) + '.h5', 'rtm', rtm)

# Process devito summaries and save in blob storage
kernel, gflopss, gpointss, oi, ops = process_summaries([summary1, summary2])
timings = np.array([kernel, gflopss, gpointss, oi, ops])
timings.dump(rootpath + '/overthrust/timings/timings_rtm_3D_shot_' + str(idx) + '.npy')

# Save shot
filename = rootpath + '/overthrust/data/overthrust_3D_born_data_source_' + str(idx)
save_rec(d_obs, src_coordinates, filename, nt)


'''
Space: O(n)
Time: O(n)
'''

class Solution:
    def heightChecker(self, heights: List[int]) -> int:
        temp = sorted(heights)
        count = 0
        for i in range(len(temp)):
            if temp[i] != heights[i]:
                count += 1
        return count


# 백준 1780 종이의 개수
# https://www.acmicpc.net/problem/1780

n = int(input())
li = [list(map(int, input().split())) for _ in range(n)]
zero, minus, one = 0, 0, 0

def dfs(x, y, n):
    global zero, minus, one
    start = li[x][y]    #해당 사분면의 (0,0) 원소

    for i in range(x, x+n):
        for j in range(y, y+n):
            if li[i][j] != start:
                dfs(x, y, n//3) #1번
                dfs(x, y + n//3, n//3)  #2번
                dfs(x, y + n//3 * 2, n//3)  #3번
                dfs(x + n//3, y, n//3)  #4번
                dfs(x + n // 3, y + n // 3, n // 3) #5번
                dfs(x + n // 3, y + n // 3 * 2, n // 3) #6번
                dfs(x + n // 3 * 2, y, n // 3)  #7번
                dfs(x + n // 3 * 2, y + n // 3, n // 3) #8번
                dfs(x + n // 3 * 2, y + n // 3 * 2, n // 3) #9번
                return

    if start == -1: minus += 1
    elif start == 0: zero += 1
    elif start == 1: one += 1

dfs(0, 0, n)
print(minus, zero, one)


from collections import namedtuple
import pandas as pd
import numpy as np

role_columns = ["roleId", "type"]
Role = namedtuple("Role", role_columns)

def clean_role(tsv_principals, tsv_names):
    """
    :param tsv_principals: ["tconst", "ordering", "nconst", "category", "job", "characters"]
    :param tsv_names: ["nconst", "primaryName", "birthYear", "deathYear", "primaryProfession", "knownForTitles"]
    :
    : Use tsv_names to get primary roles of a person, and union the results
    : with categories in tsv_principals to generate all the roles of the person
    """
    rows = []
    num = 1
    professions = []

    for couple in np.array(tsv_names['primaryProfession']):
        for profession in str(couple).split(','):
            professions.append(profession)

    distinct = tsv_principals['category'].drop_duplicates()

    for role in distinct:
        professions.append(role)

    professions = set(professions)

    for row in professions:
        roleId = num
        role = Role(roleId = roleId,
                    type = row)
        rows.append(role._asdict())
        num += 1
    return pd.DataFrame(rows, columns=role_columns)


import pytest
import itertools
from src.enums import Directions
from src.solver import Solver
from sample_puzzles import solvable_puzzles, unsolvable_puzzles
import time
from multiprocessing import Process

def initial_moves_permutations():
    # List of starting moves
    perms = list(tuple(itertools.permutations([Directions.RIGHT, Directions.LEFT, Directions.UP, Directions.DOWN])))
    return perms

@pytest.mark.parametrize("position, optimal_solution", solvable_puzzles)
@pytest.mark.parametrize("init_moves", initial_moves_permutations())
def test_solvable(puzzle, position, optimal_solution, init_moves):
    # Tests whether solvable positions are solvable
    puzzle.set_position(position)
    solver = Solver()
    assert solver.is_solvable(puzzle, init_moves)

@pytest.mark.parametrize("position", unsolvable_puzzles)
@pytest.mark.parametrize("init_moves", initial_moves_permutations())
def test_unsolvable(puzzle, position, init_moves):
    # Tests whether unsolvable positions are unsolvable
    puzzle.set_position(position)
    solver = Solver()
    assert not solver.is_solvable(puzzle, init_moves)

@pytest.mark.parametrize("position, optimal_solution", solvable_puzzles)
@pytest.mark.parametrize("init_moves", initial_moves_permutations())
def test_accuracy(puzzle, position, optimal_solution, init_moves):
    # Tests whether algorithm finds optimal solution
    puzzle.set_position(position)
    solver = Solver()
    solver.solve(puzzle, init_moves)
    assert solver.solvable
    assert solver.num_moves == optimal_solution

@pytest.mark.parametrize("position, optimal_solution", solvable_puzzles)
@pytest.mark.parametrize("init_moves", initial_moves_permutations())
def test_speed(puzzle, position, optimal_solution, init_moves):
    # Tests whether alogirthm is fast enough to find optimal solution
    timeout = 30
    puzzle.set_position(position)
    solver = Solver()
    p = Process(target=solver.solve, args=(puzzle, init_moves))
    p.start()
    p.join(timeout=timeout)
    assert not p.is_alive()


"""
Given an array of unsorted numbers and a target number,
find a triplet in the array whose sum is as close to the target number as possible, return the sum of the triplet.
If there are more than one such triplet, return the sum of the triplet with the smallest sum.

Example 1:
Input: [-2, 0, 1, 2], target=2
Output: 1
Explanation: The triplet [-2, 1, 2] has the closest sum to the target.

Example 2:
Input: [-3, -1, 1, 2], target=1
Output: 0
Explanation: The triplet [-3, 1, 2] has the closest sum to the target.

Example 3:
Input: [1, 0, 1, 1], target=100
Output: 3
Explanation: The triplet [1, 1, 1] has the closest sum to the target.
"""

import math

def triplet_sum_close_to_target(nums, target_sum):
    """
    Time Complexity: O(N * logN + N^2) = O(N^2)
    Space Complexity: O(N) -> Required for sorting
    :param nums:
    :param target_sum:
    :return:
    """

    # O(N * logN)
    nums.sort()

    smallest_distance = math.inf

    # O(N)
    for i in range(len(nums) - 2):

        left, right = i + 1, len(nums) - 1

        # O(N)
        while left < right:

            target_difference = target_sum - nums[i] - nums[left] - nums[right]

            if target_difference == 0:
                return target_sum

            if (abs(target_difference) < abs(smallest_distance)) \
                    or ((abs(target_difference) == abs(smallest_distance)) and (target_difference > smallest_distance)):
                smallest_distance = target_difference

            if target_difference > 0:
                left += 1
            else:
                right -= 1

    return target_sum - smallest_distance

def main():
    print(triplet_sum_close_to_target([-2, 0, 1, 2], 2))
    print(triplet_sum_close_to_target([-3, -1, 1, 2], 1))
    print(triplet_sum_close_to_target([1, 0, 1, 1], 100))

main()


from dvishparish.plans.models import KPI, GeneralPlan
from dvishparish.manager_roles.models import ResultItem
from dvishparish.users.models import User
from django.utils import timezone

def get_sum_result_by_KPI_and_user(user: User, kpi: KPI):
    """ get sum result User with KPI """
    today = timezone.now()
    qs = ResultItem.objects.filter(
        result_daily__date_add__lte=today,
        result_daily__date_add__gte=kpi.date_from,
        result_daily__user=user,
        KPI=kpi
    ).distinct()
    return sum(qs.values_list("KPI_result_amount", flat=True))

def get_сurrent_manager_kpis(user):
    today = timezone.now()
    KPIs = user.manager_kpis.filter(
        user=user,
        KPI__date_from__lte=today,
        KPI__date_to__gte=today,
    ).distinct()
    return KPIs

def get_top_5_menegers_with_general_plan(user):
    kpis_id = GeneralPlan.objects.first().kpi_items.all().values_list("KPI", flat=True)

    KPIs_plan = KPI.objects.filter(id__in=kpis_id).distinct()
    managers = User.objects.filter(groups__name="manager").distinct()
    results = []
    for manager in managers:
        sum_kpis = 0
        for kpi in KPIs_plan:
            sum_kpis += get_sum_result_by_KPI_and_user(
                manager, kpi
            )
        if user.id ==manager.id:
            results.append({
                "sum_kpis": sum_kpis,
                "user": manager,
                "is_self": True
            })
        else:
            results.append({
                "sum_kpis": sum_kpis,
                "user": manager
            })

    sort_results = sorted(
        results,
        key=lambda result: result['sum_kpis'],
        reverse=True)

    print(sort_results)

    number = 0
    i = 0
    while number == 0 and i < len(sort_results):
        if sort_results[i].get("is_self"):
            number = i
        i += 1
    if number+1 > 5:
        sort_results[5] = sort_results[number]
        sort_results[5]['number'] = number + 1
        return sort_results[:6]
    return sort_results[:5]


import os
from datetime import datetime

tempLogsDir = r"\\corp-app-11\c$\Users\trbadm\AppData\Local\TrulinX\ReportRunnerLogs"
users = []
currentTime = datetime.now()
for file in os.listdir(tempLogsDir):
    filePath = os.path.join(tempLogsDir, file)
    fileTime = datetime.fromtimestamp(os.path.getmtime(filePath))
    fileAge = currentTime - fileTime

    with open(os.path.join(tempLogsDir, file), 'r') as f:
        readLines = f.readlines()
        counter = 0
        searchString  = 'No match found in system-defined printers.'

        # Loop through readLines to find the line with the searchString
        for line in readLines:
            if searchString in line:
                if tempUserList[-1] not in users:
                    users.append(tempUserList[-1])
                    print(readLines[0].strip('\n\r'))
                    print(readLines[2].strip('\n\r'))
                    print(readLines[3])
                    continue


"""This example illustrates the usage of creator, evaluator and generator.

All the steps of airfoil creation & evaluation are detailed here;
however, the generator.py module contains certain presets (default airfoils).

Create an airfoil;
Evaluate an airfoil;
Generate a population of airfoils & optimize.
"""

from tools import creator, evaluator, generator

import time
start_time = time.time()

# Airfoil dimensions (in)
NACA_NUM = 2412
CHORD_LENGTH = 68
SEMI_SPAN = 150

# Thicknesses
SPAR_THICKNESS = 0.4
SKIN_THICKNESS = 0.1

# Component masses (lbs)
AIRFOIL_MASS = 10
SPAR_MASS = 10
STRINGER_MASS = 5

# Area (sqin)
SPAR_CAP_AREA = 0.3
STRINGER_AREA = 0.1

# Amount of stringers
TOP_STRINGERS = 6
BOTTOM_STRINGERS = 4
NOSE_TOP_STRINGERS = 3
NOSE_BOTTOM_STRINGERS = 5

SAVE_PATH = '/home/blendux/github/UCLA_MAE_154B/save/'

# Create airfoil instance
af = creator.Airfoil.from_dimensions(CHORD_LENGTH, SEMI_SPAN)
af.add_naca(NACA_NUM)
af.add_mass(AIRFOIL_MASS)
af.info_print(2)
af.info_save(SAVE_PATH, 'foo_name')

# Create spar instance
af.spar = creator.Spar()
# All spar coordinates are stored in single Spar object
af.spar.add_coord(af, 0.23)
af.spar.add_coord(af, 0.57)
# Automatically adds spar caps for each spar previously defined
af.spar.add_spar_caps(SPAR_CAP_AREA)
af.spar.add_mass(SPAR_MASS)
af.spar.add_webs(SPAR_THICKNESS)
af.spar.info_print(2)
af.spar.info_save(SAVE_PATH, 'foo_name')

# Create stringer instance
af.stringer = creator.Stringer()
# Compute the stringer coordinates from their quantity in each zone
af.stringer.add_coord(af, NOSE_TOP_STRINGERS, TOP_STRINGERS,
                      NOSE_BOTTOM_STRINGERS, BOTTOM_STRINGERS)
af.stringer.add_area(STRINGER_AREA)
af.stringer.add_mass(STRINGER_MASS)
af.stringer.add_webs(SKIN_THICKNESS)
af.stringer.info_print(2)
af.stringer.info_save(SAVE_PATH, 'foo_name')

# Plot components with matplotlib
creator.plot_geom(af, True)

# Evaluator object contains airfoil analysis results.
eval = evaluator.Evaluator(af)
# The analysis is performed in the evaluator.py module.
eval.analysis(1, 1)
eval.info_print(2)
eval.info_save(SAVE_PATH, 'foo_name')
# evaluator.plot_geom(eval)
evaluator.plot_lift(eval)

pop = generator.Population(10)

# print(help(creator))
# print(help(evaluator))
# print(help(generator))

# Print final execution time
print("--- %s seconds ---" % (time.time() - start_time))