from selenium.webdriver.common.action_chains import ActionChains
from urllib.parse import urljoin
import time
import pytest

@pytest.fixture
def catalog_url(baseurl_option):
    return urljoin(baseurl_option, '/index.php?route=product/category&path=20')

def test_login_catalog(catalog_url, browser):
    browser.get(catalog_url)
    assert browser.current_url == catalog_url

@pytest.mark.parametrize(
    'xpath, expected_text',
    [
        ('//div/h2', 'Desktops'),
        ('//div/h3', 'Refine Search')
    ]
)
def test_text_main_page(xpath, expected_text, catalog_url, browser):
    browser.get(catalog_url)
    label = browser.find_element_by_xpath(xpath)
    assert label.text == expected_text

@pytest.mark.parametrize(
    'xpath, expected_text',
    [
        ('//a[text()="MacBook"]', 'MacBook'),
        ('//a[text()="iPhone"]', 'iPhone'),
        ('//a[contains(text(), "Apple Cinema 30")]', 'Apple Cinema 30"'),
        ('//a[text()="Canon EOS 5D"]', "Canon EOS 5D")
    ]
)
def test_label_text_main_page(xpath, expected_text, catalog_url, browser):
    browser.get(catalog_url)
    label = browser.find_element_by_xpath(xpath)
    assert label.text == expected_text

def test_add_to_wish_list(catalog_url, browser):
    browser.get(catalog_url)
    buttons = browser.find_elements_by_xpath('//div[2]/div[2]/button[2]')
    for button in buttons:
        actions = ActionChains(browser)
        actions.move_to_element(button).perform()
        button.click()
        time.sleep(5)


from flask import Flask, request, send_from_directory
from PIL import Image
import pathlib
import os
import urllib

S3_BUCKET_URL = "https://s3-us-west-2.amazonaws.com/makersdistillery/"
IMAGES_PATH = pathlib.Path("images")

app = Flask(__name__)

@app.route("/images/<path:image_path>")
def images(image_path):
    file_path = IMAGES_PATH / os.path.dirname(image_path)
    file_name = os.path.basename(image_path)
    local_image_path = file_path / file_name
    _, extension = os.path.splitext(image_path)

    if not os.path.isfile(local_image_path):
        file_path.mkdir(parents=True, exist_ok=True)
        s3_url = S3_BUCKET_URL + image_path
        urllib.request.urlretrieve(s3_url, local_image_path)

    h_arg = request.args.get('h', None)
    w_arg = request.args.get('w', None)
    format_arg = request.args.get('format', None)

    if not (h_arg or w_arg or format_arg):
        return send_from_directory("images", image_path)

    image = Image.open(local_image_path)

    width, height = image.size

    new_height = height
    new_width = width

    if h_arg and not w_arg:
        new_height = int(h_arg)
        scale = new_height / height
        new_width = round(width * scale)
    elif w_arg and not h_arg:
        new_width = int(w_arg)
        scale = new_width / width
        new_height = round(height * scale)
    elif w_arg and h_arg:
        new_width = int(w_arg)
        new_height = int(h_arg)

    resized_image_path = pathlib.Path("resized", local_image_path)
    resized_image_file = "{}-{}{}".format(new_width, new_height, extension)

    if not os.path.isfile(IMAGES_PATH / resized_image_path / resized_image_file):
        (IMAGES_PATH / resized_image_path).mkdir(parents=True, exist_ok=True)
        resized_image = image.resize((new_width, new_height))
        resized_image.save(IMAGES_PATH / resized_image_path / resized_image_file)

    return send_from_directory("images", resized_image_path / resized_image_file)

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


import inspect
from pathlib import Path

import torch
import yaml
from comfy import model_detection, model_management
from comfy.sd import CLIP, VAE, load_model_weights
from comfy.model_patcher import ModelPatcher
from comfy.utils import calculate_parameters
from folder_paths import models_dir as comfy_models_path
from sd_meh import merge_methods
from sd_meh.merge import merge_models
from sd_meh.presets import BLOCK_WEIGHTS_PRESETS
from sd_meh.utils import weights_and_bases
from tensordict import TensorDict

print(Path.cwd())

MODELS_DIR = Path(comfy_models_path, "checkpoints")
MERGE_METHODS = dict(inspect.getmembers(merge_methods, inspect.isfunction))

EXTRA_CONFIG_YAML_PATH = Path(".", "extra_model_paths.yaml")
if EXTRA_CONFIG_YAML_PATH.exists():
    with EXTRA_CONFIG_YAML_PATH.open() as f:
        EXTRA_CONFIG_YAML = yaml.safe_load(f)
else:
    EXTRA_CONFIG_YAML = {}

def get_checkpoints():
    checkpoints = []
    for ext in ("ckpt", "safetensors"):
        checkpoints.extend(list(MODELS_DIR.glob(f"**/*.{ext}")))
        for c in EXTRA_CONFIG_YAML.values():
            extra_checkpoints_dir = Path(
                c["base_path"] if "base_path" in c else "", c["checkpoints"]
            )
            checkpoints.extend(list(extra_checkpoints_dir.glob(f"**/*.{ext}")))
    return {c.stem: c for c in checkpoints}

def split_model(
    sd,
    output_clip=True,
    output_vae=True,
):
    clip = None
    vae = None
    model = None
    clip_target = None

    parameters = calculate_parameters(sd, "model.diffusion_model.")
    fp16 = model_management.should_use_fp16(model_params=parameters)

    class WeightsLoader(torch.nn.Module):
        pass

    model_config = model_detection.model_config_from_unet(
        sd, "model.diffusion_model.", fp16
    )
    if model_config is None:
        raise RuntimeError("ERROR: Could not detect model type")

    offload_device = model_management.unet_offload_device()
    model = model_config.get_model(sd, "model.diffusion_model.")
    model = model.to(offload_device)
    model.load_model_weights(sd, "model.diffusion_model.")
    if output_vae:
        vae = VAE()
        w = WeightsLoader()
        w.first_stage_model = vae.first_stage_model
        load_model_weights(w, sd)

    if output_clip:
        w = WeightsLoader()
        clip_target = model_config.clip_target()
        clip = CLIP(clip_target, embedding_directory=None)
        w.cond_stage_model = clip.cond_stage_model
        sd = model_config.process_clip_state_dict(sd)
        load_model_weights(w, sd)

    left_over = sd.keys()
    if len(left_over) > 0:
        print("left over keys:", left_over)

    return (
        ModelPatcher(
            model,
            load_device=model_management.get_torch_device(),
            offload_device=offload_device,
        ),
        clip,
        vae,
    )

class MergingExecutionHelper:
    ckpts = get_checkpoints()

    @classmethod
    def INPUT_TYPES(self):
        required = {
            "model_a": (list(self.ckpts.keys()), {"default": None}),
            "model_b": (list(self.ckpts.keys()), {"default": None}),
            "merge_mode": (list(MERGE_METHODS.keys()), {"default": "weighted_sum"}),
            "base_alpha": (
                "FLOAT",
                {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.1},
            ),
        }
        optional = {
            "precision": ([16, 32], {"default": 16}),
            "model_c": (["None"] + list(self.ckpts.keys()), {"default": None}),
            "base_beta": (
                "FLOAT",
                {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.1},
            ),
            "weights_alpha": ("STRING", ""),
            "weights_beta": ("STRING", ""),
            "re_basin": (["disabled", "enabled"], {"default": "disabled"}),
            "re_basin_iterations": (
                "INT",
                {"default": 10, "min": 0, "max": 1000, "step": 1},
            ),
            "weights_clip": (["disabled", "enabled"], {"default": "disabled"}),
            "device": (["cpu", "cuda"], {"default": "cpu"}),
            "work_device": (["cpu", "cuda"], {"default": "cpu"}),
            "threads": ("INT", {"default": 1, "min": 1, "step": 1}),
            "prune": (["disabled", "enabled"], {"default": "disabled"}),
            "block_weights_preset_alpha": (
                ["None"] + list(BLOCK_WEIGHTS_PRESETS.keys()),
                {"default": None},
            ),
            "presets_alpha_lambda": (
                "FLOAT",
                {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.1},
            ),
            "block_weights_preset_alpha_b": (
                ["None"] + list(BLOCK_WEIGHTS_PRESETS.keys()),
                {"default": None},
            ),
            "block_weights_preset_beta": (
                ["None"] + list(BLOCK_WEIGHTS_PRESETS.keys()),
                {"default": None},
            ),
            "presets_beta_lambda": (
                "FLOAT",
                {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.1},
            ),
            "block_weights_preset_beta_b": (
                ["None"] + list(BLOCK_WEIGHTS_PRESETS.keys()),
                {"default": None},
            ),
        }
        self.optional_keys = list(optional.keys())
        return {
            "required": required,
            "optional": optional,
        }

    RETURN_TYPES = ("MODEL", "CLIP", "VAE")
    FUNCTION = "merge"
    CATEGORY = "meh"

    def merge(
        self,
        model_a,
        model_b,
        base_alpha,
        merge_mode,
        **kwargs,
    ):
        for opt_k in self.optional_keys:
            if opt_k not in kwargs:
                kwargs[opt_k] = None

        for k in ("re_basin", "weights_clip", "prune"):
            kwargs[k] = kwargs[k] == "enabled"

        for k in ("weights_alpha", "weights_beta"):
            kwargs[k] = None if kwargs[k] == "" else kwargs[k]

        for k in (
            "model_c",
            "block_weights_preset_alpha",
            "block_weights_preset_alpha_b",
            "block_weights_preset_beta",
            "block_weights_preset_beta_b",
        ):
            kwargs[k] = None if kwargs[k] == "None" else kwargs[k]

        models = {
            "model_a": self.ckpts[model_a],
            "model_b": self.ckpts[model_b],
        }
        if kwargs["model_c"]:
            models["model_c"] = self.ckpts[kwargs["model_c"]]

        weights, bases = weights_and_bases(
            merge_mode,
            kwargs["weights_alpha"],
            base_alpha,
            kwargs["block_weights_preset_alpha"],
            kwargs["weights_beta"],
            kwargs["base_beta"],
            kwargs["block_weights_preset_beta"],
            kwargs["block_weights_preset_alpha_b"],
            kwargs["block_weights_preset_beta_b"],
            kwargs["presets_alpha_lambda"],
            kwargs["presets_beta_lambda"],
        )

        merged = merge_models(
            models,
            weights,
            bases,
            merge_mode,
            kwargs["precision"],
            kwargs["weights_clip"],
            kwargs["re_basin"],
            kwargs["re_basin_iterations"],
            kwargs["device"],
            kwargs["work_device"],
            kwargs["prune"],
            kwargs["threads"],
        )

        if isinstance(merged, TensorDict):
            return split_model(merged.to_dict())
        return merged

NODE_CLASS_MAPPINGS = {
    "MergingExecutionHelper": MergingExecutionHelper,
}


import os

# use CPU only
os.environ["CUDA_VISIBLE_DEVICES"] = ""

import pickle
import argparse
import numpy as np
from math import ceil
from tqdm import tqdm
import tensorflow as tf
from shutil import rmtree

np.random.seed(1234)

def split(sequence, n):
  """ divide sequence into n sub-sequence evenly """
  k, m = divmod(len(sequence), n)
  return [
      sequence[i * k + min(i, m):(i + 1) * k + min(i + 1, m)] for i in range(n)
  ]

def normalize(x, x_min, x_max):
  """ scale x to be between 0 and 1 """
  return (x - x_min) / (x_max - x_min)

def fft(signals):
  """ Apply FFT over each neuron recordings """
  real = np.zeros(signals.shape, dtype=np.float32)
  imag = np.zeros(signals.shape, dtype=np.float32)

  for b in tqdm(range(signals.shape[0])):
    for n in range(signals.shape[-1]):
      x = signals[b, :, n]
      x = tf.signal.fft(x.astype(np.complex64))
      x = x.numpy()
      real[b, :, n], imag[b, :, n] = np.real(x), np.imag(x)

  return np.concatenate([real, imag], axis=-1)

def calculate_num_per_shard(hparams):
  """
  calculate the number of data per shard given sequence_length such that each
  shard is target_size GB
  """
  num_per_shard = ceil((120 / hparams.sequence_length) * 1100) * 10  # 1GB shard
  if hparams.fft:
    num_per_shard *= 2 / 3
  return int(num_per_shard * hparams.target_shard_size)

def get_segments(hparams):
  print('processing file {}...'.format(hparams.input))

  assert hparams.stride >= 1

  with open(hparams.input, 'rb') as file:
    data = pickle.load(file)

  raw_signals = np.array(data['signals'], dtype=np.float32)
  raw_spikes = np.array(data['oasis'], dtype=np.float32)

  # remove first two rows in signals
  if not hparams.is_dg_data:
    raw_signals = raw_signals[2:]
    raw_spikes = raw_spikes[2:]

  assert raw_signals.shape == raw_spikes.shape

  # set signals and spikes to WC [sequence, num. neurons, ...]
  raw_signals = np.swapaxes(raw_signals, 0, 1)
  raw_spikes = np.swapaxes(raw_spikes, 0, 1)

  hparams.num_neurons = raw_signals.shape[1]
  hparams.num_channels = hparams.num_neurons

  print('\nsegmentation with stride {}'.format(hparams.stride))
  signals, spikes, i = [], [], 0
  while i + hparams.sequence_length < raw_signals.shape[0]:
    signals.append(raw_signals[i:i + hparams.sequence_length, ...])
    spikes.append(raw_spikes[i:i + hparams.sequence_length, ...])
    i += hparams.stride

  signals = np.array(signals, dtype=np.float32)
  spikes = np.array(spikes, dtype=np.float32)

  if hparams.fft:
    print('\napply fft')
    signals = fft(signals)
    hparams.num_channels = signals.shape[-1]

  if hparams.conv2d:
    print('\nconvert to 3D matrix')
    if hparams.fft:
      # convert matrix to [sequence, num. neurons, 2]
      mid = signals.shape[-1] // 2
      real = np.expand_dims(signals[..., :mid], axis=-1)
      imag = np.expand_dims(signals[..., mid:], axis=-1)
      signals = np.concatenate((real, imag), axis=-1)
    else:
      # convert matrix to [sequence, num. neurons, 1]
      signals = np.expand_dims(signals, axis=-1)
    hparams.num_channels = signals.shape[-1]
    print('signals shape {}'.format(signals.shape))

  print('\nsignals min {:.04f}, max {:.04f}, mean {:.04f}'.format(
      np.min(signals), np.max(signals), np.mean(signals)))

  # normalize signals to [0, 1]
  hparams.signals_min = np.min(signals)
  hparams.signals_max = np.max(signals)
  if hparams.normalize:
    print('\napply normalization')
    signals = normalize(signals, hparams.signals_min, hparams.signals_max)
    print('signals min {:.04f}, max {:.04f}, mean {:.04f}'.format(
        np.min(signals), np.max(signals), np.mean(signals)))

  print('\nsignals shape {}, spikes shape {}'.format(signals.shape,
                                                     spikes.shape))

  return signals, spikes

def _bytes_feature(value):
  return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

def serialize_example(signal, spike):
  features = {
      'signal': _bytes_feature(signal.tostring()),
      'spike': _bytes_feature(spike.tostring())
  }
  example = tf.train.Example(features=tf.train.Features(feature=features))
  return example.SerializeToString()

def get_record_filename(hparams, mode, shard_id, num_shards):
  filename = '{}-{:03d}-of-{:03d}.record'.format(mode, shard_id + 1, num_shards)
  return os.path.join(hparams.output_dir, filename)

def write_to_record(hparams, mode, shard, num_shards, signals, spikes, indexes):
  record_filename = get_record_filename(hparams, mode, shard, num_shards)
  print('writing {} segments to {}...'.format(len(indexes), record_filename))

  with tf.io.TFRecordWriter(record_filename) as writer:
    for i in indexes:
      example = serialize_example(signals[i], spikes[i])
      writer.write(example)

def write_to_records(hparams, mode, signals, spikes, indexes):
  if not os.path.exists(hparams.output_dir):
    os.makedirs(hparams.output_dir)

  # calculate the number of records to create
  num_shards = 1 if hparams.num_per_shard == 0 else ceil(
      len(indexes) / hparams.num_per_shard)

  print('writing {} segments to {} {} records...'.format(
      len(indexes), num_shards, mode))

  if mode == 'train':
    hparams.num_train_shards = num_shards
  else:
    hparams.num_validation_shards = num_shards

  sharded_indexes = split(indexes, num_shards)

  for shard in range(num_shards):
    write_to_record(
        hparams,
        mode=mode,
        shard=shard,
        num_shards=num_shards,
        signals=signals,
        spikes=spikes,
        indexes=sharded_indexes[shard],
    )

def main(hparams):
  if not os.path.exists(hparams.input):
    print('input file {} does not exists'.format(hparams.input))
    exit()

  if os.path.exists(hparams.output_dir):
    if hparams.replace:
      rmtree(hparams.output_dir)
    else:
      print('output directory {} already exists\n'.format(hparams.output_dir))
      exit()

  signals, spikes = get_segments(hparams)

  # shuffle data
  indexes = np.arange(len(signals))
  np.random.shuffle(indexes)

  hparams.train_size = len(signals) - hparams.validation_size
  hparams.signal_shape = signals.shape[1:]
  hparams.spike_shape = spikes.shape[1:]

  hparams.num_per_shard = calculate_num_per_shard(hparams)

  print('\n{} segments in each shard with target shard size {}'.format(
      hparams.num_per_shard, hparams.target_shard_size))

  write_to_records(
      hparams,
      mode='train',
      signals=signals,
      spikes=spikes,
      indexes=indexes[:hparams.train_size])

  write_to_records(
      hparams,
      mode='validation',
      signals=signals,
      spikes=spikes,
      indexes=indexes[hparams.train_size:])

  # save information of the dataset
  with open(os.path.join(hparams.output_dir, 'info.pkl'), 'wb') as file:
    info = {
        'train_size': hparams.train_size,
        'validation_size': hparams.validation_size,
        'signal_shape': hparams.signal_shape,
        'spike_shape': hparams.spike_shape,
        'sequence_length': hparams.sequence_length,
        'num_neurons': hparams.num_neurons,
        'num_channels': hparams.num_channels,
        'num_train_shards': hparams.num_train_shards,
        'num_validation_shards': hparams.num_validation_shards,
        'buffer_size': min(hparams.num_per_shard, hparams.train_size),
        'normalize': hparams.normalize,
        'stride': hparams.stride,
        'fft': hparams.fft,
        'conv2d': hparams.conv2d,
    }
    if hparams.normalize:
      info['signals_min'] = hparams.signals_min
      info['signals_max'] = hparams.signals_max
    pickle.dump(info, file)

  print('saved {} TFRecords to {}'.format(
      hparams.num_train_shards + hparams.num_validation_shards,
      hparams.output_dir))

if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument(
      '--input', default='raw_data/ST260_Day4_signals4Bryan.pkl', type=str)
  parser.add_argument('--output_dir', default='tfrecords', type=str)
  parser.add_argument('--sequence_length', default=2048, type=int)
  parser.add_argument('--stride', default=2, type=int)
  parser.add_argument('--normalize', action='store_true')
  parser.add_argument('--fft', action='store_true')
  parser.add_argument('--conv2d', action='store_true')
  parser.add_argument('--replace', action='store_true')
  parser.add_argument('--validation_size', default=1000, type=float)
  parser.add_argument('--is_dg_data', action='store_true')
  parser.add_argument(
      '--target_shard_size',
      default=0.5,
      type=float,
      help='target size in GB for each TFRecord file.')
  hparams = parser.parse_args()

  main(hparams)


import time
import smbus
from datetime import datetime, timedelta
import arrow                                  # local/utc conversions

# set I2c bus addresses of clock module and non-volatile ram
DS3231ADDR = 0x68 #known versions of DS3231 use 0x68
AT24C32ADDR = 0x57  #older boards use 0x56

I2C_PORT = 1 #valid ports are 0 and 1

def _bcd_to_int(bcd):
    """
    Decode a 2x4bit BCD to a integer.
    """
    out = 0
    for digit in (bcd >> 4, bcd):
        for value in (1, 2, 4, 8):
            if digit & 1:
                out += value
            digit >>= 1
        out *= 10
    return out / 10

def _int_to_bcd(number):
    """
    Encode a one or two digits number to the BCD format.
    """
    bcd = 0
    for idx in (number // 10, number % 10):
        for value in (8, 4, 2, 1):
            if idx >= value:
                bcd += 1
                idx -= value
            bcd <<= 1
    return bcd >> 1

def _set_bit(value,index,state):
    """
    Set bit given by index (zero-based) in value to state and return the result
    """
    mask = 1 << index
    value &= ~mask
    if state:
        value |= mask
    return value

def _local2utc(dtime):
  """
  Convert a naive datetime-object in local-time to UTC
  """
  a = arrow.get(dtime,'local')
  return a.to('utc').naive

def _utc2local(dtime):
  """
  Convert a naive datetime-object in UTC to local-time
  """
  a = arrow.get(dtime,'utc')
  return a.to('local').naive

class ds3231(object):
    """
    Define the methods needed to read and update the real-time-clock module.
    """

    _SECONDS_REGISTER      = 0x00
    _MINUTES_REGISTER      = 0x01
    _HOURS_REGISTER        = 0x02
    _DAY_OF_WEEK_REGISTER  = 0x03
    _DAY_OF_MONTH_REGISTER = 0x04
    _MONTH_REGISTER        = 0x05
    _YEAR_REGISTER         = 0x06

    _ALARM1_OFFSET         = 0x07
    _ALARM1_SEC_REGISTER   = 0x07
    _ALARM1_MIN_REGISTER   = 0x08
    _ALARM1_HOUR_REGISTER  = 0x09
    _ALARM1_DATE_REGISTER  = 0x0A

    _ALARM2_OFFSET         = 0x0B
    _ALARM2_MIN_REGISTER   = 0x0B
    _ALARM2_HOUR_REGISTER  = 0x0C
    _ALARM2_DATE_REGISTER  = 0x0D

    _CONTROL_REGISTER      = 0x0E
    _STATUS_REGISTER       = 0x0F

    _AGING_REGISTER        = 0x10             # unused
    _TEMP_MSB_REGISTER     = 0x11
    _TEMP_LSB_REGISTER     = 0x12

    def __init__(self, port=I2C_PORT, utc=True,addr=DS3231ADDR, at24c32_addr=AT24C32ADDR):
        """
        ???
        """
        self._bus = smbus.SMBus(port) #valid ports are 0 and 1
        self._utc = utc
        self._addr = addr
        self._at24c32_addr = at24c32_addr

    ###########################
    # DS3231 real time clock functions
    ###########################

    def _write(self, register, data):
        """
        ???
        """
        self._bus.write_byte_data(self._addr, register, data)

    def _read(self, data):
        """
        ???
        """
        return self._bus.read_byte_data(self._addr, data)

    def _read_seconds(self):
        """
        ???
        """
        return _bcd_to_int(self._read(self._SECONDS_REGISTER) & 0x7F)   # wipe out the oscillator on bit

    def _read_minutes(self):
        """
        ???
        """
        return _bcd_to_int(self._read(self._MINUTES_REGISTER))

    def _read_hours(self):
        """
        ???
        """
        tmp = self._read(self._HOURS_REGISTER)
        if tmp == 0x64:
            tmp = 0x40
        return _bcd_to_int(tmp & 0x3F)

    def _read_day(self):
        """
        ???
        """
        return _bcd_to_int(self._read(self._DAY_OF_WEEK_REGISTER))

    def _read_date(self):
        """
        ???
        """
        return _bcd_to_int(self._read(self._DAY_OF_MONTH_REGISTER))

    def _read_month(self):
        """
        ???
        """
        return _bcd_to_int(self._read(self._MONTH_REGISTER) & 0x1F)

    def _read_year(self):
        """
        ???
        """
        return _bcd_to_int(self._read(self._YEAR_REGISTER))

    def read_all(self):
        """
        Return a tuple such as (year, month, daynum, dayname, hours, minutes, seconds).
        """
        return (self._read_year(), self._read_month(), self._read_date(),
                self._read_day(), self._read_hours(), self._read_minutes(),
                self._read_seconds())

    def read_str(self):
        """
        Return a string such as 'YY-DD-MMTHH-MM-SS'.
        """
        return '%02d-%02d-%02dT%02d:%02d:%02d' % (self._read_year(),
                self._read_month(), self._read_date(), self._read_hours(),
                self._read_minutes(), self._read_seconds())

    def read_datetime(self):
        """
        Return the datetime.datetime object.
        """
        dtime =  datetime(2000 + self._read_year(),
                self._read_month(), self._read_date(), self._read_hours(),
                self._read_minutes(), self._read_seconds(), 0)
        if (self._utc):
          return _utc2local(dtime)
        else:
          return dtime

    def write_all(self, seconds=None, minutes=None, hours=None, day_of_week=None,
            day_of_month=None, month=None, year=None):
        """
        Direct write each user specified value.
        Range: seconds [0,59], minutes [0,59], hours [0,23],
                 day_of_week [0,7], day_of_month [1-31], month [1-12], year [0-99].
        """
        if seconds is not None:
            if seconds < 0 or seconds > 59:
                raise ValueError('Seconds is out of range [0,59].')
            seconds_reg = _int_to_bcd(seconds)
            self._write(self._SECONDS_REGISTER, seconds_reg)

        if minutes is not None:
            if minutes < 0 or minutes > 59:
                raise ValueError('Minutes is out of range [0,59].')
            self._write(self._MINUTES_REGISTER, _int_to_bcd(minutes))

        if hours is not None:
            if hours < 0 or hours > 23:
                raise ValueError('Hours is out of range [0,23].')
            self._write(self._HOURS_REGISTER, _int_to_bcd(hours)) # not  | 0x40 according to datasheet

        if year is not None:
            if year < 0 or year > 99:
                raise ValueError('Years is out of range [0, 99].')
            self._write(self._YEAR_REGISTER, _int_to_bcd(year))

        if month is not None:
            if month < 1 or month > 12:
                raise ValueError('Month is out of range [1, 12].')
            self._write(self._MONTH_REGISTER, _int_to_bcd(month))

        if day_of_month is not None:
            if day_of_month < 1 or day_of_month > 31:
                raise ValueError('Day_of_month is out of range [1, 31].')
            self._write(self._DAY_OF_MONTH_REGISTER, _int_to_bcd(day_of_month))

        if day_of_week is not None:
            if day_of_week < 1 or day_of_week > 7:
                raise ValueError('Day_of_week is out of range [1, 7].')
            self._write(self._DAY_OF_WEEK_REGISTER, _int_to_bcd(day_of_week))

    def write_datetime(self, dtime):
        """
        Write from a datetime.datetime object.
        """
        if(self._utc):
            dtime = _local2utc(dtime)

        self.write_all(dtime.second, dtime.minute, dtime.hour,
                dtime.isoweekday(), dtime.day, dtime.month, dtime.year % 100)

    def write_system_datetime_now(self):
        """
        shortcut version of "DS3231.write_datetime(datetime.datetime.now())".
        """
        self.write_datetime(datetime.now())

    #######################################################################
    # SDL_DS3231 alarm handling. Recurring alarms are currently unsupported.
    ########################################################################

    def set_alarm_time(self,alarm,dtime):
        """
        Set alarm to given time-point. Note: although this method has a
        full datetime-value as input, only the values of day-of-month,
        hours, minutes and seconds (only alarm1) are used
        """
        if (self._utc):
            dtime = _local2utc(dtime)

        if alarm == 1:
            self._write(self._ALARM1_SEC_REGISTER, _int_to_bcd(dtime.second))
            self._write(self._ALARM1_MIN_REGISTER, _int_to_bcd(dtime.minute))
            self._write(self._ALARM1_HOUR_REGISTER, _int_to_bcd(dtime.hour))
            self._write(self._ALARM1_DATE_REGISTER, _int_to_bcd(dtime.day))
        else:
            self._write(self._ALARM2_MIN_REGISTER, _int_to_bcd(dtime.minute))
            self._write(self._ALARM2_HOUR_REGISTER, _int_to_bcd(dtime.hour))
            self._write(self._ALARM2_DATE_REGISTER, _int_to_bcd(dtime.day))

    def get_alarm_time(self,alarm,convert=True):
        """
        Query the given alarm and construct a valid datetime-object or
        a tuple (day-of-month/day-of-week,hour,min,sec) depending on the
        convert flag.
        """

        # seconds
        is_interval = False
        if alarm == 1:
            buffer = self._read(self._ALARM1_SEC_REGISTER)
            if buffer & 0x80:
                # we fire every second
                if not convert:
                    return (None,None,None,None,None)
                else:
                    return datetime.now()          # not very sensible
            sec = _bcd_to_int(buffer & 0x7F)
            offset = self._ALARM1_OFFSET + 1
        else:
            sec  = 0
            offset = self._ALARM2_OFFSET

        # minutes
        buffer = self._read(offset)
        if buffer & 0x80:
            # alarm when seconds match
            if not convert:
                return (None,None,None,None,sec)
            else:
                return self._next_dt_match(alarm,None,None,None,None,sec)
        min  = _bcd_to_int(buffer & 0x7F)

        # hour
        offset = offset + 1
        buffer = self._read(offset)
        if buffer & 0x80:
            # alarm when minutes match
            if not convert:
                return (None,None,None,min,sec)
            else:
                return self._next_dt_match(alarm,None,None,None,min,sec)
        hour = _bcd_to_int(buffer & 0x7F)

        # day-in-month/day-of-week
        offset = offset + 1
        buffer = self._read(offset)
        if buffer & 0x80:
            # alarm when hour match
            if not convert:
                return (None,None,hour,min,sec)
            else:
                return self._next_dt_match(alarm,None,None,hour,min,sec)
        elif buffer & 0x40:
            # DY/DT (bit 6) is 1
            weekday  = _bcd_to_int(buffer & 0x3F)
            day = None
        else:
            weekday = None
            day  = _bcd_to_int(buffer & 0x3F)

        if not convert:
            return (day,weekday,hour,min,sec)
        else:
            return self._next_dt_match(alarm,day,weekday,hour,min,sec)

    def _next_dt_match(self,alarm,day,weekday,hour,min,sec):
        """
        Calculate the next alarm datetime (in case alarm did not fire yet)
        or the last datetime if the alarm fired.
        The former is exact while the latter is just a best guess - the
        alarm could already have fired way in the past.
        """
        if (self._utc):
            now = datetime.utcnow()
        else:
            now = datetime.now()
        # convert weekday to day of month
        if not weekday == None:
            now = now + timedelta((weekday - now.weekday()+7) % 7)
            day = now.day
        year = now.year
        month = now.month

        enabled,fired = self.get_alarm_state(alarm)

        # first try: assume alarm is in the curren month
        # we try to create a valid datetime object
        try:
            alarm_dtime = datetime(year,month,day,hour,min,sec)
        except ValueError:
            # day-of-month might not be valid for current month!
            # no year roll-over necessary, since this won't happen
            # for December or January
            if fired:
                month = month - 1         # alarm must have been in the past
            else:
                month = month + 1         # alarm date is in the future
            alarm_dtime = datetime(year,month,day,hour,min,sec)

        # check if first alarm-datetime is correct
        # this depends on the state of the alarm
        if now > alarm_dtime and not fired:
            # alarm did not fire yet, but must be in the future
            month = month + 1
            if month > 12:
                month = 1
                year  = year + 1
        elif now < alarm_dtime and fired:
            # alarm fired, but must be in the past
            month = month - 1
            if month == 0:
                month = 12
                year = year - 1
        else:
            # proper alignment of now and alarm_dtime, so return it
            if (self._utc):
                return _utc2local(alarm_dtime)
            else:
                return alarm_dtime

        # second try: we moved the alarm one month, but the day-of-month
        # might not be valid for the new month
        try:
            alarm_dtime = datetime(year,month,day,hour,min,sec)
        except ValueError:
            # again, no year roll-over necessary
            if fired:
                month = month - 1      # alarm is in the past, go back in time
            else:
                month = month + 1      # alarm waiting, i.e. in the future
            alarm_dtime = datetime(year,month,day,hour,min,sec)

        if (self._utc):
          return _utc2local(alarm_dtime)
        else:
          return alarm_dtime

    def get_alarm_state(self,alarm):
        """
        Query if the state of the alarm. Returns a tuple (enabled,fired)
        of two booleans.
        """
        control = self._read(self._CONTROL_REGISTER)
        status  = self._read(self._STATUS_REGISTER)

        return (bool(control & alarm),bool(status & alarm))

    def clear_alarm(self,alarm):
        """
        Clear the given alarm (set A1F or A2F in the status-register to zero)
        """
        status = self._read(self._STATUS_REGISTER)
        status &= ~alarm
        self._write(self._STATUS_REGISTER,status)

    def set_alarm(self,alarm,state):
        """
        Set the given alarm-flag A1IE or A2IE in the control-register to the
        desired state (0 or 1)
        """
        control = self._read(self._CONTROL_REGISTER)
        control = _set_bit(control,alarm-1,state)
        self._write(self._CONTROL_REGISTER,control)

    def dump_value(self,value):
        """
        Dump a value as hex and binary string
        """
        return "0x{0:02X} 0b{0:08b}".format(value,value)

    def dump_register(self,reg):
        """
        Read and return a raw register as binary string
        """
        return self.dump_value(self._read(reg))

    ###########################
    # SDL_DS3231 module onboard temperature sensor
    ###########################

    def get_temp(self):
        """
        ???
        """
        byte_tmsb = self._bus.read_byte_data(self._addr, self._TEMP_MSB_REGISTER)
        byte_tlsb = bin(self._bus.read_byte_data(self._addr,
                                            self._TEMP_LSB_REGISTER))[2:].zfill(8)
        return byte_tmsb + int(byte_tlsb[0]) * 2 ** (-1) + int(byte_tlsb[1]) * 2 ** (-2)

    ###########################
    # AT24C32 non-volatile ram Code
    ###########################

    def set_current_at24c32_address(self, address):
        """
        ???
        """
        addr1 = address / 256
        addr0 = address % 256
        self._bus.write_i2c_block_data(self._at24c32_addr, addr1, [addr0])

    def read_at24c32_byte(self, address):
        """
        ???
        """
        self.set_current_at24c32_address(address)
        return self._bus.read_byte(self._at24c32_addr)

    def write_at24c32_byte(self, address, value):
        """
        ???
        """
        addr1 = address / 256
        addr0 = address % 256
        self._bus.write_i2c_block_data(self._at24c32_addr, addr1, [addr0, value])
        time.sleep(0.20)


#!/usr/bin/env python3
import random, argparse

parser = argparse.ArgumentParser()
parser.add_argument("-r", "--rows", help="how many rows defaults to 2")
parser.add_argument("-c", "--cols", help="how many cols defaults to 2")
args = parser.parse_args()

if args.rows:
    try:
        rows = int(args.rows)

    except:
        print("Arguments must be integer... using 2 as default rows\n")
        rows = 2
else:
    rows = 2

if args.cols:
    try:
        cols = int(args.cols)
    except:
        print("Arguments must be integers... using 2 as default cols\n")
        cols = 2
else:
    cols = 2

name_ls = ["Nate", "Ben", "Laurent", "Gabe", "Jessy", "Imran", "Dionte", "Sakib"]
random.shuffle(name_ls)

string = "FRONT OF CLASS"
#print(f"{string:^50}")
#longest_name_len = max(len(x) for x in name_ls)

for i in range(cols):
    for j in range(rows):
        if(name_ls):
            # 2 per table so pop both names from list then put in front left.
            name1 = name_ls.pop(0)
            name2 = name_ls.pop(0)
            #table = f"{name1:^{longest_name_len}}   {name2:^{longest_name_len}}                           "
            #can't use thing above because it doesn't like text formatting that way
            #11 and 7 just got from experimenting and playing around with names
            table = f"{name1:>11}   {name2:<7}"
            #print(table, end="    ")
    #print("\n")
print("RIP SEATING CHART \nYou guys make me sad")


import warnings
warnings.filterwarnings('ignore')
import numpy as np
import pandas as pd
#import matplotlib as plt
import matplotlib.pyplot as plt

from pyFTS.common import Util
from pyFTS.benchmarks import Measures
from pyFTS.partitioners import Grid, Entropy
from pyFTS.models import hofts
from pyFTS.common import Membership

dataset = pd.read_csv('https://query.data.world/s/2bgegjggydd3venttp3zlosh3wpjqj', sep=';')
dataset['data'] = pd.to_datetime(dataset["data"], format='%Y-%m-%d %H:%M:%S')

train_uv = dataset['glo_avg'].values[:24505]
test_uv = dataset['glo_avg'].values[24505:]

train_mv = dataset.iloc[:24505]
test_mv = dataset.iloc[24505:]

dataset.head()

models = []
fig, ax = plt.subplots(nrows=2, ncols=1,figsize=[20,5])

ax[0].plot(train_uv[:240])
ax[1].plot(train_uv)

from statsmodels.tsa.stattools import acf

fig, ax = plt.subplots(nrows=1, ncols=1,figsize=[15,5])

ax.plot(acf(train_uv,nlags=48))
ax.set_title("Autocorrelation")
ax.set_ylabel("ACF")
ax.set_xlabel("LAG")

from itertools import product

levels = ['VL','L','M','H','VH']
sublevels = [str(k) for k in np.arange(0,7)]
names = []
for combination in product(*[levels, sublevels]):
  names.append(combination[0]+combination[1])

print(names)

fig, ax = plt.subplots(nrows=1, ncols=1,figsize=[15,3])

part = Grid.GridPartitioner(data=train_uv,npart=35, names=names)

part.plot(ax)


import os
from vk import VK
from ya_disk import YaDisk
import json
from datetime import datetime
from tqdm import tqdm
from dotenv import load_dotenv

def main():
    vk_user_id = input('Enter VK user ID (only digits): ')
    num_photos = int(input('Enter the number of photos to save (default is 5): ') or 5)

    load_dotenv()
    token_ya = os.getenv('ya_token')
    token_vk = os.getenv('vk_token')

    try:
        vk = VK(token_vk)
        photos = vk.get_photos(vk_user_id, count=num_photos)
        result = []

        ya_disk = YaDisk(token_ya)
        folder_name = f'VK_Photos_{vk_user_id}'
        ya_disk.create_folder(folder_name)

        for photo in tqdm(photos, desc='Uploading photos', unit='photo', ncols=80):
            likes = photo['likes']['count']
            date = datetime.fromtimestamp(photo['date']).strftime('%Y-%m-%d')
            file_name = f'{likes}_{date}.jpg'
            photo_url = photo['sizes'][-1]['url']

            if ya_disk.upload_photo(folder_name, file_name, photo_url):
                result.append({
                    'file_name': file_name,
                    'size': 'z',
                })

        with open('result.json', 'w') as json_file:
            json.dump(result, json_file, ensure_ascii=False, indent=4)

        print(f'The photos has been successfully uploaded to Yandex.Disk.')

    except Exception as e:
        print(f'An error occurred. {str(e)}')

if __name__ == '__main__':
    main()


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

import os
import tensorflow as tf
from PIL import Image

writer = tf.python_io.TFRecordWriter("train.tfrecords")

images_path = "./snow/"

classes = {'snow'}

for index, name in enumerate(classes):
    for img_name in os.listdir(images_path):
        img_path = images_path + img_name
        img = Image.open(img_path)
        img = img.resize((224, 224))
        img_raw = img.tobytes()
        example = tf.train.Example(features=tf.train.Features(feature={
            'label': tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
            'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
        }))
        writer.write(example.SerializeToString())

writer.close()

for serialized_example in tf.python_io.tf_record_iterator("train.tfrecords"):
    example = tf.train.Example()
    example.ParseFromString(serialized_example)

    image = example.features.feature['image'].bytes_list.value
    label = example.features.feature['label'].int64_list.value

    print("image is : %s" % image)
    print("label is : %s" % label)


from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route

import uvicorn

async def homepage(request):
    return JSONResponse({'hello': 'world'})

async def empty_route(request):
    return JSONResponse({'new route': 'this route does nothing'})

async def contact(request):
    contact_page = """

    Contact Us

send an email to: example@hotmail.com

    """
    return HTMLResponse(content=contact_page)

app = Starlette(debug=True, routes=[
    Route('/', homepage),
    Route('/empty', empty_route),
    Route('/contact', contact)
])

if __name__ == "__main__":
    uvicorn.run(app, host='0.0.0.0', port=8000)


# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2022/10/25 15:03
# @Author  : Hanchiao
# @File    : antColony.py
import numpy as np
from enum import Enum
from itertools import count
import statistics

import matplotlib.pyplot as plt
import sys
import time

def _custom_create(custom_generate, pool):
    initial_loc, accessible = custom_generate()
    return Solution([initial_loc], pool, _access=accessible)

def _custom_route(ant, selectMap, custom_routing, get_fitness):
    _pool = ant.Pool
    _route = ant.Route[:]
    _access = ant.Access[:]
    while _access:
        _route, _access = custom_routing(_route, _access, selectMap.prob_query(_route[-1], _access))
    # print(_route)
    return Solution(_route, _pool, fitness=get_fitness(_route))

def get_best(items_num, opt_fitness,
             get_fitness, display,
             custom_generate=None, custom_move=None,
             forget_rate=0.5, alpha=1, beta=1, poolSize=10,
             generation=None):
    historical_best_fitness = []
    generation_mean_fitness = []
    select_map = SelectProbMap(items_num, get_fitness, _forget=forget_rate, _alpha=alpha, _beta=beta)

    if custom_generate is None:
        def fnCreate(pool):
            pass
    else:
        def fnCreate(pool):
            return _custom_create(custom_generate, pool)

    if custom_move is None:
        def fnMove(candidate):
            pass
    else:
        def fnMove(candidate):
            return _custom_route(candidate, select_map, custom_move, get_fitness)

    strategyLookup = {
        Strategies.Create: lambda pool=1: fnCreate(pool),
        Strategies.Explore: lambda candidate: fnMove(candidate)
    }

    def _get_improvement(strategyLookup, poolSize, iteration=None):
        pool = [strategyLookup[Strategies.Create](i) for i in range(poolSize)]  # 蚁群初始化
        _generation = count(0)

        routes = [strategyLookup[Strategies.Explore](pool[pool_index]) for pool_index in range(poolSize)]  # 探索一条完整解
        best = max(routes)
        generation_mean_fitness.append(best.Fitness.eval())
        historical_best_fitness.append(best.Fitness.eval())
        yield best

        while True:
            generation_num = next(_generation)
            if iteration is not None:
                if generation_num > iteration - 1:
                    break

            routes = [strategyLookup[Strategies.Explore](pool[pool_index]) for pool_index in range(poolSize)]
            local_best = max(routes)
            if best < local_best:
                best = local_best
                yield best

            _w = [routes[i].Fitness.eval() for i in range(poolSize)]
            generation_mean_fitness.append(np.sum(_w) / poolSize)
            historical_best_fitness.append(best.Fitness.eval())

            select_map.update(routes, best)
            # print("{}".format(select_map))

    improvement = None
    for improvement in _get_improvement(strategyLookup, poolSize, generation):
        display(improvement)
        if not opt_fitness > improvement.Fitness:
            historical_best_fitness.append(improvement.Fitness.eval())
            generation_mean_fitness.append(generation_mean_fitness[-1])
            break
    return improvement, generation_mean_fitness, historical_best_fitness

class Solution:
    def __init__(self, route, _pool, fitness=None, _access=None):
        self.Route = route[:]
        self.Fitness = fitness
        self.Pool = _pool
        self.Access = _access

    def __gt__(self, other):
        return self.Fitness > other.Fitness

class SelectProbMap:
    def __init__(self, _size, get_fitness, _forget, _alpha, _beta):
        self.Pheromone = np.ones([_size, _size])
        self.Size = _size
        self.Forget = _forget
        self.Alpha = _alpha
        self.Beta = _beta
        self.fnGetFitness = get_fitness

        self.Probability = np.zeros([_size, _size])
        for _i in range(_size):
            for _j in range(_size):
                self.Probability[_i, _j] = 1

    def prob_query(self, _i, _access):
        _sum = sum(self.Probability[_i, _access])
        probability = self.Probability[_i, _access] / _sum
        return probability

    def update(self, _routes, _best):
        for _i in range(self.Size):
            for _j in range(self.Size):
                if self.Pheromone[_i, _j] > 1e-20:
                    self.Pheromone[_i, _j] *= self.Forget  # 信息素衰减

        # for _r in _routes:
        #     _fit = self.fnGetFitness(_r.Route)
        #     for _i in range(len(_r.Route)-1):
        #         self.Pheromone[_r.Route[_i], _r.Route[_i+1]] += _fit.info()*1e-8
        #         self.Pheromone[_r.Route[_i+1], _r.Route[_i]] = self.Pheromone[_r.Route[_i], _r.Route[_i+1]]

        best_fit = self.fnGetFitness(_best.Route)  # 最优路径信息素增强
        for _i in range(len(_best.Route)-1):
            self.Pheromone[_best.Route[_i], _best.Route[_i+1]] += best_fit.info()
            self.Pheromone[_best.Route[_i+1], _best.Route[_i]] = self.Pheromone[_best.Route[_i], _best.Route[_i+1]]

        for _i in range(self.Size):  # 概率图更新
            for _j in range(_i):
                _fit = self.fnGetFitness([_j])
                _tmp = np.power(self.Pheromone[_i, _j], self.Alpha) * np.power(_fit.eval(), self.Beta)
                self.Probability[_i, _j] = _tmp
                self.Probability[_j, _i] = _tmp

    def __str__(self):
        return "Pheromone: {}".format(self.Pheromone)

class Strategies(Enum):
    Create = 0,
    Explore = 1

class Benchmark:
    @staticmethod
    def run(function, visualization=False):
        timings = []
        optimal_cost = []
        stdout = sys.stdout
        # print("\t{:3}\t{}\t{}".format("No.", "Mean", "Stdev"))
        # pool_size = [0, 5, 15, 50]
        # color = ['salmon', 'sandybrown', 'greenyellow', 'darkturquoise']

        # for i, value in enumerate(neighbor_range):
        for i in range(1):
            startTime = time.time()

            # sys.stdout = None  # avoid the output to be chatty
            best, generation_mean_fitness, historical_best_fitness = function()
            seconds = time.time() - startTime
            optimal_cost.append(best.Fitness.eval())
            sys.stdout = stdout

            if visualization:
                fig = plt.figure()
                ax_1 = fig.add_subplot(111)
                # ax_1.set_aspect(1)
                ax_1.set(ylabel='Collecting value')
                plt.grid(linestyle='--', linewidth=1, alpha=0.3)

                # ax_2 = fig.add_subplot(212)
                # # ax_2.set_aspect(1.2)
                # ax_2.set(xlabel='No. of generation', ylabel='Best so far')
                plt.grid(linestyle='--', linewidth=1, alpha=0.3)
                # fig.tight_layout()
                fig.suptitle('0-1 Bag Problem: Ant Colony Optimization', fontweight="bold")

                x_axis = len(generation_mean_fitness)
                x_axis = list(range(x_axis))

                ax_1.plot(x_axis, generation_mean_fitness, color='b', label="mean value", ls='-')
                ax_1.plot(x_axis, historical_best_fitness, color='g', label="best so far", ls='-')
                # ax_1.plot(x_axis, generation_mean_fitness, color=color[i], label='[{}, {}]'.format(value[0], value[1]), ls='-')
                # ax_2.plot(x_axis, historical_best_fitness, color=color[i], label='[{}, {}]'.format(value[0], value[1]), ls='-')

            timings.append(seconds)
            mean_time = statistics.mean(timings)
            mean_cost = statistics.mean(optimal_cost)
            print("Time Consuming:\t{}\tOptimal Costs:\t{}".format(timings[i], optimal_cost[i]))

            # only display statistics for the first ten runs and then every 10th run after that.
            if i < 10 or i % 10 == 9:
                print("\t{:3}\tMean Time Consuming: {:<3.2f}\tStandard Deviation {:<3.2f}".format(
                    1 + i, mean_time,
                    statistics.stdev(timings, mean_time)
                    if i > 1 else 0))
                print("\t{:3}\tMean Traveling Cost: {:<3.2f}\tStandard Deviation {:<3.2f}".format(
                    1 + i, mean_cost,
                    statistics.stdev(optimal_cost, mean_cost)
                    if i > 1 else 0))

        if visualization:
            ax_1.legend(loc='lower right')
            # ax_2.legend(loc='upper right')
            fig.savefig('../../fig/[tmp-ACO]0-1 Bag Problem.pdf')


import json
import re
import os
import time
import logging
from collections import namedtuple

from paddle.fluid import core
import paddle.fluid as fluid
import numpy as np

Doc = namedtuple("Document", ("id", "title", "content"))
SegDoc = namedtuple("Seg_Document",("id", "title", "content", "seg_content"))

def read_origin_data(filename, *, limit=None):
    """
    read the origin work from multi-lines json
    return the list of json with length of limit

    :param filename: the json filename
    :param limit: only read the limit number of lines of work(if it's None type return )
    :return:
    """
    papers = []
    with open(filename, 'r', encoding='utf-8') as f:
        for id ,line in enumerate(f.readlines()):
            dic = json.loads(line)
            papers.append(dic)
            if limit is not None and id > limit-2:
                break
    return papers

def dict_doc(documents)->dict:
    """
    transfer the origin document list into document dictionary

    :param documents: the list of document
    :return:
    """
    doc_dict = {}
    for doc in documents:
        doc_id = doc.get('id')
        sentences = doc.get('sentences')
        title = sentences[0]
        content = "".join(sentences[1:])
        doc_dict[doc_id] = Doc(doc_id, title, content)
    return doc_dict

def pre_text(text: str) -> str:
    """
    do some dirty work for the string
    1. delete the punctuation
    2. sub the number into <NUM>

    :param text:
    :return:
    """
    punc = re.compile(
        r',|/|：|;|:|\'|`|\[|\]|<|>|\?|:|"|\{|\}|\~|!|@|#|\$|%|\^|&|\(|\)|-|=|\_|\+|，|、|‘|’|【|】|·|！|”|“| |…|（|）|」|「|《|》|。|，|\.|。|;|；|\+',
        re.S)
    res = punc.sub(" ", text)

    # num = re.compile("\d+")
    # res = num.sub("<NUM>", res)

    return res

def time_clock(func):
    def compute_time_clock(*args, **kwargs):
        start = time.time()
        res = func(*args, **kwargs)
        end = time.time()
        print("Implement function %s, using %.2f s"%(func.__name__ ,end-start))
        return res
    return compute_time_clock

@time_clock
def hello_word():
    print("Hello world")
    time.sleep(1)
    return 0

def get_programname(file):
    prgram_filename = os.path.split(file)[1]
    return os.path.splitext(prgram_filename)[0]

def read_json(filename):
    with open(filename, 'r', encoding='utf-8') as f:
        data = json.load(f)
    return data

def normal_leven(str1, str2):
    len_str1 = len(str1) + 1
    len_str2 = len(str2) + 1
    # create matrix
    matrix = [0 for n in range(len_str1 * len_str2)]
    # init x axis
    for i in range(len_str1):
        matrix[i] = i
    # init y axis
    for j in range(0, len(matrix), len_str1):
        if j % len_str1 == 0:
            matrix[j] = j // len_str1

    for i in range(1, len_str1):
        for j in range(1, len_str2):
            if str1[i - 1] == str2[j - 1]:
                cost = 0
            else:
                cost = 1
            matrix[j * len_str1 + i] = min(matrix[(j - 1) * len_str1 + i] + 1,
                                           matrix[j * len_str1 + (i - 1)] + 1,
                                           matrix[(j - 1) * len_str1 + (i - 1)] + cost)

    return matrix[-1]

def time_clock(func):
    def compute_time_clock(*args, **kwargs):
        start = time.time()
        res = func(*args, **kwargs)
        end = time.time()
        print("Implement function %s, using %.2f s"%(func.__name__ ,end-start))
        return res
    return compute_time_clock

@time_clock
def hello_word():
    print("Hello world")
    time.sleep(1)
    return 0

def set_base(file,keyword='result'):
    """

    :param file:
    :param keyword:
    :return:
    """
    base_flle = get_programname(file)
    base_dir = os.path.join(".", keyword, base_flle)
    if not os.path.exists(base_dir):
        os.mkdir(base_dir)
    return base_dir

def json_saver(obj, filename):
    with open(filename, 'w', encoding='utf-8') as f:
        json.dump(obj, f)

@time_clock
def read_train_data(filename) :
    """
    read the train.txt and get the list of all work
    :param filename:
    :return:
    """
    corpus = []  # 存储所有对话的列表
    with open(filename, 'r', encoding='utf-8') as f:
        data = list()  # 存储一个对话的列表
        for line in f:
            if line == '\n':
                corpus.append(data)
                data = list()
                continue
            raw_data = line.strip().split('\t')

            processed_data = []
            for idx, item in enumerate(raw_data):
                try:
                    temp = eval(item)
                except:
                    temp = item
                finally:
                    if idx == 2 and type(temp)==int:
                        temp = str(temp)
                    processed_data.append(temp)
            data.append(processed_data)
    return corpus

def log(filename= "test.log",level=logging.DEBUG):
    #创建logger，如果参数为空则返回root logger
    logger = logging.getLogger("encocder pretrain")
    logger.setLevel(level)  #设置logger日志等级

    #这里进行判断，如果logger.handlers列表为空，则添加，否则，直接去写日志
    if not logger.handlers:
        #创建handler
        fh = logging.FileHandler(filename,encoding="utf-8")
        ch = logging.StreamHandler()
        #设置输出日志格式
        LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
        DATE_FORMAT = "%m/%d/%Y %H:%M:%S %p"
        logger.propagate = False
        formatter = logging.Formatter(
            fmt=LOG_FORMAT,
            datefmt=DATE_FORMAT
            )

        #为handler指定输出格式
        fh.setFormatter(formatter)

        #为logger添加的日志处理器
        logger.addHandler(fh)

    return logger #直接返回logger

class Clock():

    def __init__(self):
        time.time()

def save_model(filename, param_name_list, opt_var_name_list, name=''):
    save_model_file = os.path.join(filename, name + "model_stage_0")
    save_opt_state_file = os.path.join(filename, name + "opt_state_stage_0")

    model_stage_0 = {}
    for name in param_name_list:
        t = np.asarray(fluid.global_scope().find_var(name).get_tensor())
        model_stage_0[name] = t
    np.savez(save_model_file, **model_stage_0)

    opt_state_stage_0 = {}
    for name in opt_var_name_list:
        t_data = np.asarray(fluid.global_scope().find_var(name).get_tensor())
        opt_state_stage_0[name] = t_data
    np.savez(save_opt_state_file, **opt_state_stage_0)
    info_msg = "Finish saving the parameter. "
    return info_msg

def load_model(model_init_file, param_name_list, place, opt_state_init_file='',datatype='float32'):
    """ init model """

    try:
        model_init = np.load(model_init_file)
    except:
        print("load init model failed", model_init_file)
        raise Exception("load init model failed")

    print("load init model")
    loading_msg = []
    for name in param_name_list:
        try:
            t = fluid.global_scope().find_var(name).get_tensor()
            load_param = model_init[str(name)]
            if load_param.shape == np.asarray(t).shape:
                t.set(load_param.astype(datatype), place)
        except AttributeError as e:
            loading_msg.append(str(e) + "%s exist not in this model and cannot be load!"%name)
        except KeyError as e:
            loading_msg.append(str(e) + "%s exist not in this model and cannot be load!"%name)

    return loading_msg

    # load opt state
    if opt_state_init_file != "":
        print("begin to load opt state")
        opt_state_data = np.load(opt_state_init_file)
        for k, v in opt_state_data.items():
            t = fluid.global_scope().find_var(str(k)).get_tensor()
            t.set(v, place)
        print("set opt state finished")

    print("init model parameters finshed")

def write_iterable(filename, iterable_obj):
    with open(filename, 'w', encoding='utf8') as f:
        for i in iterable_obj:
            f.write(str(i)+'\n')

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


#!/usr/bin/env python
from setuptools import setup

VERSION = "0.2"
REPO = "https://github.com/duedil-ltd/python-sloq"
README = "README.rst"

with open(README) as f:
    long_description = f.read()

setup(
    name="sloq",
    version=VERSION,
    description="Rate-limited Queue",
    author="Paul Scott, Duedil Limited",
    author_email="paul@duedil.com",
    url=REPO,
    download_url="%s/tarball/%s" % (REPO, VERSION),
    py_modules=["sloq"],
    test_suite="test_sloq",
    license="MIT",
    long_description=long_description,
    keywords="queue rate limit slow token bucket".split(),
    classifiers=[],
)


import sys
import csv
import json

# Converts the JSON output of a PowerBI query to a CSV file
def extract(input_file, output_file):
    input_json = read_json(input_file)
    data = input_json["results"][0]["result"]["data"]
    dm0 = data["dsr"]["DS"][0]["PH"][0]["DM0"]
    columns_types = dm0[0]["S"]
    columns = map(lambda item: item["GroupKeys"][0]["Source"]["Property"] if item["Kind"] == 1 else item["Value"], data["descriptor"]["Select"])
    value_dicts = data["dsr"]["DS"][0].get("ValueDicts", {})

    reconstruct_arrays(columns_types, dm0)
    expand_values(columns_types, dm0, value_dicts)

    replace_newlines_with(dm0, "")
    write_csv(output_file, columns, dm0)

def read_json(file_name):
    with open(file_name) as json_config_file:
        return json.load(json_config_file)

def write_csv(output_file, columns, dm0):
    with open(output_file, "w") as csvfile:
        wrt = csv.writer(csvfile)
        wrt.writerow(columns)
        for item in dm0:
            wrt.writerow(item["C"])

def reconstruct_arrays(columns_types, dm0):
    # fixes array index by applying
    # "R" bitset to copy previous values
    # "Ø" bitset to set null values
    lenght = len(columns_types)
    for item in dm0:
        currentItem = item["C"]
        if "R" in item or "Ø" in item:
            copyBitset = item.get("R", 0)
            deleteBitSet = item.get("Ø", 0)
            for i in range(lenght):
                if is_bit_set_for_index(i, copyBitset):
                    currentItem.insert(i, prevItem[i])
                elif is_bit_set_for_index(i, deleteBitSet):
                    currentItem.insert(i, None)
        prevItem = currentItem

def is_bit_set_for_index(index, bitset):
    return (bitset >> index) & 1 == 1

# substitute indexes with actual values
def expand_values(columns_types, dm0, value_dicts):
    for (idx, col) in enumerate(columns_types):
        if "DN" in col:
            for item in dm0:
                dataItem = item["C"]
                if isinstance(dataItem[idx], int):
                    valDict = value_dicts[col["DN"]]
                    dataItem[idx] = valDict[dataItem[idx]]

def replace_newlines_with(dm0, replacement):
    for item in dm0:
        elem = item["C"]
        for i in range(len(elem)):
            if isinstance(elem[i], str):
                elem[i] = elem[i].replace("\n", replacement)

def main():
    if len(sys.argv) == 3:
        extract(sys.argv[1], sys.argv[2])
    else:
        sys.exit("Usage: python3 " + sys.argv[0] + " input_file output_file", file=sys.stderr)

if __name__ == "__main__":
    main()


from boto3 import client
from flask import Flask, jsonify, request, make_response

from .utils import get_timestamp
from .constants import FAVOURITE_COMPANIES_TABLE, FAVOURITE_ORG_ID, ORG_ID

app = Flask(__name__)
app.config["JSONIFY_PRETTYPRINT_REGULAR"] = True
client = client("dynamodb", region_name="eu-west-1")

hey = lambda x: x
print(hey)

@app.route("/favourite_companies", methods=["GET"])
def get_all_favourite_companies():
    table = client.scan(TableName=FAVOURITE_COMPANIES_TABLE)
    return jsonify(table["Items"]), 200, {"Content-Type": "application/json"}

@app.route("/favourite_company/<string:org_id>", methods=["GET"])
def get_company(org_id):
    resp = client.query(
        TableName=FAVOURITE_COMPANIES_TABLE,
        KeyConditions={
            ORG_ID: {
                "ComparisonOperator": "EQ",
                "AttributeValueList": [{"S": org_id}],
            }
        },
    )

    if not resp["Items"]:
        return jsonify({"error": f"There is no company with org_id: {org_id}"}), 400

    return jsonify(resp["Items"])

@app.route(
    "/favourite_company/delete/<string:org_id>/<string:favourite_org_id>",
    methods=["DELETE"],
)
def delete_company(org_id, favourite_org_id):
    resp = client.get_item(
        TableName=FAVOURITE_COMPANIES_TABLE,
        Key={
            ORG_ID: {"S": org_id},
            FAVOURITE_ORG_ID: {"S": favourite_org_id},
        },
    )

    if not resp.get("Item"):
        return (
            jsonify(
                {
                    "error": f"There is no company with org_id: {org_id} and favourite_org_id: {favourite_org_id}."
                }
            ),
            400,
        )

    _ = client.delete_item(
        TableName=FAVOURITE_COMPANIES_TABLE,
        Key={
            ORG_ID: {"S": org_id},
            FAVOURITE_ORG_ID: {"S": favourite_org_id},
        },
    )

    return jsonify(
        {
            "success": f"Deleted company with org_id: {org_id} and favourite_org_id: {favourite_org_id}."
        }
    )

@app.route("/favourite_company/create", methods=["POST"])
def create_user():
    org_id = request.json.get(ORG_ID)
    favourite_org_id = request.json.get(FAVOURITE_ORG_ID)
    if not org_id or not favourite_org_id:
        return (
            jsonify(
                {
                    "error": "Please provide org_id and favourite_org_id in the request body."
                }
            ),
            400,
        )

    item = {
        ORG_ID: {"S": org_id},
        FAVOURITE_ORG_ID: {"S": favourite_org_id},
        "date": {"S": get_timestamp()},
    }

    _ = client.put_item(TableName=FAVOURITE_COMPANIES_TABLE, Item=item)

    return jsonify(
        {
            "success": f"Created company with org_id: {org_id} and favourite_org_id: {favourite_org_id}"
        }
    )

@app.errorhandler(404)
def resource_not_found(_):
    return make_response(jsonify(error_handled="Not found!"), 404)


from re import I
import numpy as np
import gym
import random

"""
常に観測値として1を返す環境
環境に対して取るべき行動が周期的に切り替わり、
それに応じて報酬が決定される。
"""

class StaticCyclicEnv0(gym.Env):
    def __init__(self, cycle, cycle_cnt_max, action_num, noise):
        super().__init__()
        self.cycle = cycle
        assert self.cycle % action_num == 0 and self.cycle >= action_num, 'cycleはアクション数の整数倍でなくてはならない'

        self.action_num = action_num
        self.desired_action = -1
        self.cycle_cnt_max = cycle_cnt_max
        self.noise = noise
        self.action_space = gym.spaces.Discrete(self.action_num)
        self.observation_space = gym.spaces.Box(
            low = 1,
            high = 1,
            shape = [1],
            dtype = np.int
        )
        self.reward_range = [0, 1]
        self.reset()

    def reset(self):
        self.step_cnt = 0
        self.cycle_cnt = 0
        self.done = False
        self.info = {
                        'bonus_cnt':0, \
                        'bonus_max': self.cycle_cnt_max * self.action_num, #生涯中でボーナスが発生するタイミングの数
                        'is_bonus': True #ルールが切り替わった直後および最初のステップでTrue
                    }
        return self.observe()

    def step(self, action):
        observation = self.observe()

        ### Step更新
        if(self.step_cnt % (self.cycle // self.action_num) == 0):
            if(random.random()) >= self.noise:
                self.update_action()
            self.info['is_bonus'] = True
        else:
            self.info['is_bonus'] = False

        self.step_cnt += 1

        ### rewardの計算
        if action == self.desired_action:
            reward = 1.0
        else:
            reward = 0.0

        ### 終了処理
        if(self.cycle_cnt >= self.cycle_cnt_max):
            self.done = True
        else:
            self.done = False

        return observation, reward, self.done, self.info

    def observe(self):
        return 1

    def update_action(self):
        self.desired_action += 1
        if(self.desired_action == self.action_num):
            self.desired_action = 0
            self.cycle_cnt += 1


#!/usr/bin/python3

import os
import sys
import argparse
import re

if __name__ == '__main__':

    infile_format = ''
    cmd_opts = []
    id_pat = r''
    alt_id_pat  = r''

    parser = argparse.ArgumentParser(description = """ """)
    parser.add_argument('-i', '--infile',
        help = f'Speciefies path to input file in {infile_format} format.',
        type = str)
    parser.add_argument('-d','--id',
        help = '''Identifier XXX''',
        type = str)
    parser.add_argument('-c', '--command',
        help = f'Command to be executed. One of {cmd_opts}.',
        required = True)
    parser.add_argument('-o', '--option',
        help = 'Set option XXX',
        action = 'store true')

    args = vars(parser.parse_args())

    # argument checks - infile exists
    if not os.path.isfile(args['infile']):
        print(f'File {args["infile"]} not found.')
        sys.exit(0)

    if args['command']:
        if args['command'] not in cmd_opts:
            print(f'Error: {args["command"]} is not a valid Command.')
            sys.exit(0)

    if args['command'] in ['XXX']:
        if not re.match(id_pat, args['id']):
            print(f'Error: {args["id"]} is not a valid GO Id.')
            sys.exit(0)

        # if args['command'] == 'getEntry':
        #     res = getEntry(filename = args['infile'], id = args['id'])

    elif args['command'] in ['XXX']:
        if not re.match(alt_id_pat, args['id']):
            print(f'Error: {args["id"]} is not a valid external identifier.')
            sys.exit(0)

    print(args)


from django_cas_ng import views as cas_views
from django_cas_ng.models import ProxyGrantingTicket, SessionTicket
from django_cas_ng.utils import get_protocol, get_redirect_url, get_cas_client
from django_cas_ng.signals import cas_user_logout
from django.http import JsonResponse, HttpRequest, HttpResponse, HttpResponseRedirect
from django.conf import settings
from django.contrib.auth import authenticate, login as auth_login, logout as auth_logout
from urllib import parse as urllib_parse
from rest_framework.response import Response
from rest_framework_jwt.settings import api_settings
from django.contrib.auth.models import update_last_login
from .models import User

JWT_PAYLOAD_HANDLER = api_settings.JWT_PAYLOAD_HANDLER
JWT_ENCODE_HANDLER = api_settings.JWT_ENCODE_HANDLER

class APILoginView(cas_views.LoginView):

    def successful_login(self, request: HttpRequest, next_page: str) -> HttpResponse:
        """
        This method is called on successful login. Override this method for
        custom post-auth actions (i.e, to add a cookie with a token).

        :param request:
        :param next_page:
        :return:
        """
        try:
            user = User.objects.get(email=f'{request.user.email}@ui.ac.id')
        except User.DoesNotExist:
            user = request.user

        new_next_page = next_page

        if user.email == "":
            new_next_page = settings.SUCCESS_SSO_AUTH_REDIRECT + 'not-login/'
            user.delete()
        elif not user.is_active:
            new_next_page = settings.SUCCESS_SSO_AUTH_REDIRECT + 'not-login/'
        else:
            payload = JWT_PAYLOAD_HANDLER(user)
            jwt_token = JWT_ENCODE_HANDLER(payload)
            update_last_login(None, user)

            new_next_page = settings.SUCCESS_SSO_AUTH_REDIRECT + 'login-sivitas/' + jwt_token

        return HttpResponseRedirect(new_next_page)

class APILogoutView(cas_views.LogoutView):

    def get(self, request: HttpRequest) -> HttpResponse:
        """
        Redirects to CAS logout page

        :param request:
        :return:
        """
        next_page = settings.SUCCESS_SSO_AUTH_REDIRECT

        # try to find the ticket matching current session for logout signal
        try:
            st = SessionTicket.objects.get(session_key=request.session.session_key)
            ticket = st.ticket
        except SessionTicket.DoesNotExist:
            ticket = None
        # send logout signal
        cas_user_logout.send(
            sender="manual",
            user=request.user,
            session=request.session,
            ticket=ticket,
        )

        # clean current session ProxyGrantingTicket and SessionTicket
        ProxyGrantingTicket.objects.filter(session_key=request.session.session_key).delete()
        SessionTicket.objects.filter(session_key=request.session.session_key).delete()
        auth_logout(request)

        next_page = next_page or get_redirect_url(request)
        if settings.CAS_LOGOUT_COMPLETELY:
            client = get_cas_client(request=request)
            return HttpResponseRedirect(client.get_logout_url(next_page))

        # This is in most cases pointless if not CAS_RENEW is set. The user will
        # simply be logged in again on next request requiring authorization.
        return HttpResponseRedirect(next_page)


from collections import Counter
from typing import Counter
def main():
    t = int(input())
    for i in range(t):
        n = int(input())
        nums = list(map(int, input().split()))
        count = Counter(nums)
        print(count)
main()


import requests

url_f = "https://shiqianjiang.cn/home/image/bg"
url_e = ".webp"

headers = {
    'Accept': 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Connection': 'keep-alive',
    'Referer': 'https://shiqianjiang.cn/home/',
    'Sec-Fetch-Dest': 'image',
    'Sec-Fetch-Mode': 'no-cors',
    'Sec-Fetch-Site': 'same-origin',
    'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/103.0.0.0 Mobile Safari/537.36',
    'sec-ch-ua': '".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
    'sec-ch-ua-mobile': '?1',
    'sec-ch-ua-platform': 'Android'
}

for i in range(9):
    a = str(i)
    if (i == 0):
        a = ''
    resp = requests.get(url_f + a + url_e, headers=headers, verify=False)
    with open("shiqianjiang/"+str(i)+".webp", mode="wb") as f:
        f.write(resp.content)

url = "https://shiqianjiang.cn/home/image/home.webp"
resp = requests.get(url, headers=headers, verify=False)
with open("shiqianjiang/"+"home.webp", mode="wb") as f:
        f.write(resp.content)

url = "https://shiqianjiang.cn/image/head.png"
resp = requests.get(url, headers=headers, verify=False)
with open("shiqianjiang/"+"head.webp", mode="wb") as f:
        f.write(resp.content)


import logging
from datetime import datetime

from pymongo import MongoClient, UpdateOne

class UrlRepository:
    def __init__(self):
        mongo_client = MongoClient('mongodb://mongodb:27017/')
        mongo_db = mongo_client['crawler_db']
        self.collection = mongo_db['urls']
        try:
            self.collection.create_index([('url', 1)], unique=True)
        except Exception as e:
            pass

    def upsert_url(self, url, content):
        update_query = {
            '$set': {
                'content': content,
                'last_modified': datetime.now()
            }
        }
        upset_url = UpdateOne({'url': url}, update_query, upsert=True)
        self.collection.bulk_write([upset_url])

    def find_url(self, url):
        query = {'url': url}
        return self.collection.find_one(query)


# stdlib imports
import asyncio
import time

# project imports
import asyncio_cpu
import asyncio_io

if __name__ == "__main__":
    start_time = time.time()
    loop = asyncio.get_event_loop()

    io_start = time.time()
    api_data = loop.run_until_complete(asyncio_io.get_data())
    print(f"\nDone. IO bound time: {round(time.time() - io_start, 2)}\n")

    cpu_start = time.time()
    asyncio.run(asyncio_cpu.process_response(api_data=api_data))
    print(f"\nDone. CPU bound time: {round(time.time() - cpu_start, 2)}")

    print(f"\nTotal time: {round(time.time() - start_time, 2)}")


import pytest
from ui.locators import basic_locators
from base import BaseCase

class Test_Target(BaseCase):

    @pytest.mark.UI
    def test_login(self):
        self.log_in('alena1997999@gmail.com', 'tWz+H@&Gws#Yj7L')
        assert 'Кампании' in self.driver.title

    @pytest.mark.UI
    def test_logout(self):
        self.log_in('alena1997999@gmail.com', 'tWz+H@&Gws#Yj7L')
        self.log_out()
        assert "Рекламная платформа" in self.driver.title

    @pytest.mark.UI
    def test_change_info(self):
        self.log_in('alena1997999@gmail.com', 'tWz+H@&Gws#Yj7L')
        self.change_info("Anna", "89069474448", "anna1234@test.com")
        assert 'Информация успешно сохранена' in self.driver.page_source

    @pytest.mark.UI
    @pytest.mark.parametrize(
        'page, expected',
        [
            pytest.param(
                basic_locators.CHANGE_PAGE1, 'Контактная информация'
            ),
            pytest.param(
                basic_locators.CHANGE_PAGE2, 'Лицевой счет'
            ),
        ],
    )
    def test_change_page(self, page, expected):
        self.log_in('alena1997999@gmail.com', 'tWz+H@&Gws#Yj7L')
        self.click_on_element(page)
        assert expected in self.driver.title


from kivy.uix.screenmanager import ScreenManager, Screen
from tasks.tsks import A

class Scrn_manger:
    sm = ScreenManager()
    name = ""
    sc = Screen(name="tasks")

    # main.right.dodBtn.bind(on_press=main.dod)

    def to_lists(self, sc, a):
        self.sm.current = "lists"
        self.sm.remove_widget(sc)
        a.layout.clear_widgets()
        sc.clear_widgets()
        a.left.layout.clear_widgets()
        a.right.buttons.clear_widgets()
        a.right.add.add.clear_widgets()

    a = True

    def to_tasks(self, name):
        a = A(name)

        if self.a:
            self.a = False
            # a.right.delBtn.bind(on_release=a.right.fn.get_state)

        ly = a.a()

        a.bcbtn.bind(on_release=lambda i: self.to_lists(self.sc, a))
        self.sc.add_widget(ly)
        self.sm.add_widget(self.sc)
        self.sm.current = "tasks"


import csv
import os
import datetime
from datetime import date, datetime, timedelta
import matplotlib.pyplot as plt
from rich.console import Console

console = Console()

current_date = date.today().strftime("%d/%m/%Y")

# Generates unique ID for each new line in each csv file
def generate_id(file_name):
    with open(file_name, "r") as file:
        csvReader = csv.reader(file)
        lines = []
        for line in csvReader:
            if line != []:
                lines.append(line)

        if len(lines) == 1:
            id_number = f"0{str(1)}"
            return id_number
        else:
            last_added = lines[-1]
            id_number = str(int(last_added[0]) + 1)
            if len(id_number) < 2:
                id_number = f"0{str(id_number)}"

    # Getting rid of blank lines
    f = open(file_name, "w+")
    f.truncate()
    f.close()
    with open(file_name, "w", newline="") as newfile:
        write = csv.writer(newfile)
        write.writerows(lines)

    return id_number

# Returns total available stock of a product in inventory
def get_total_stock(product):
    with open("data/inventory.csv", "r") as file:
        total_count = 0
        for line in file.readlines():
            if product in line:
                product_line = line.split(",")
                quantity = int(product_line[5])
                total_count += quantity
    return total_count

# Adds new product to inventory file
def buy_product(name, price, exp_date, quantity):
    id = generate_id("data/inventory.csv")
    new_product = [id, name, current_date, price, exp_date, quantity]
    with open("data/inventory.csv", "a", newline="") as file:
        writer = csv.writer(file)
        writer.writerow(new_product)
        console.print(f"{quantity} pieces of {name} are added to the inventory.", style="#96fdca")

# Updates sold.csv and expired.csv files
def update_csv_file(product, sold_quantity, sell_price, sell_date, file_name):
    id = generate_id(file_name)
    product = {
        "id": id,
        "product_name": product["product_name"],
        "buy_id": product["id"],
        "buy_price": product["buy_price"],
        "buy_date": product["buy_date"],
        "exp_date": product["exp_date"],
        "stock_quantity": product["quantity"],
        "sell_date": sell_date,
        "sell_price": sell_price,
        "sold_quantity": sold_quantity
    }
    with open(file_name, "a+", newline="") as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=[
                                "id","product_name","buy_id","buy_price","buy_date",
                                "exp_date","stock_quantity","sell_date",
                                "sell_price","sold_quantity"])
        writer.writerow(product)

# Updates inventory.csv file
def update_inventory_file(inventory_dict):
    csv_header = inventory_dict[0].keys()
    updated_csv_file = "data/new_inventory.csv"
    with open(updated_csv_file, "w") as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=csv_header)
        writer.writeheader()
        for product in inventory_dict[1:]:
            writer.writerow(product)
    old_csv_file = "data/inventory.csv"
    if (os.path.exists(old_csv_file) and os.path.isfile(old_csv_file)):
        os.remove(old_csv_file)
        os.rename(updated_csv_file, old_csv_file)

# When a product is sold, this function checks if there is enough stock is available for selling
# by checking quantities and expiration dates.
# It automatically removes sold and/or expired stock.
def check_and_update_stock(product_name, sold_quantity, sell_price):
    products_in_stock = []
    with open("data/inventory.csv", "r") as csvfile:
        readCSV = csv.reader(csvfile,delimiter=",")

        for row in readCSV:
            product = dict(id=row[0], product_name=row[1], buy_date=row[2], buy_price=row[3], exp_date=row[4], quantity=row[5])
            products_in_stock.append(product)

        updated_stock = 0
        for product in products_in_stock[1:]:

            if product["product_name"] == product_name:
                exp = datetime.strptime(product["exp_date"], "%d/%m/%Y")
                cur = datetime.strptime(current_date, "%d/%m/%Y")

                if cur < exp:
                    stock = int(product["quantity"])

                    if stock >= sold_quantity and cur < exp:
                        stock = stock + updated_stock
                        stock = stock - sold_quantity
                        product["quantity"] = str(stock)
                        update_csv_file(product, sold_quantity, sell_price, current_date, "data/sold.csv")
                        if stock <= 0: products_in_stock.remove(product)
                        console.print(f"{sold_quantity} pieces of {product_name} are removed from inventory.", style="#FBDBDF")
                        return products_in_stock
                    elif stock < sold_quantity and cur < exp:
                        stock = stock + updated_stock
                        updated_stock = stock - sold_quantity
                        sold_quantity = 0
                        product["quantity"] = str(updated_stock)
                        if updated_stock <= 0: products_in_stock.remove(product)

                elif cur >= exp and int(product["quantity"]) < get_total_stock(product["product_name"]):
                    products_in_stock.remove(product)
                    update_csv_file(product, 0, 0, "--", "data/expired.csv")
                    continue
                else:
                    console.print(f"Unfortunately your {product_name} stock is expired ðŸ¤", style="#fd9796")
                    sold_quantity = 0
                    sell_price = 0
                    sell_date = "--"
                    products_in_stock.remove(product)
                    update_csv_file(product, sold_quantity, sell_price, sell_date, "data/expired.csv")
                    return products_in_stock

# Removes products from inventory.
def sell_product(sold_product_name, sold_quantity, sell_price):
    total_stock = get_total_stock(sold_product_name)

    if total_stock >= sold_quantity:
        update_inventory_file(check_and_update_stock(sold_product_name, sold_quantity, sell_price))

    elif total_stock > 0:
        return console.print(f"You do not have enough stock, you can sell a maximum of {total_stock} ðŸ˜…", style="#fdca96")
    else:
        return console.print(f"You do not have any {sold_product_name} in stock ðŸ˜Ÿ", style="#fd9796")


import numpy as np
import pandas as pd
from sklearn.model_selection import KFold, train_test_split
from sklearn.preprocessing import LabelEncoder,OneHotEncoder
from keras.utils import np_utils
import tensorflow as tf
from MB_nn import MB_nn
from keras.utils.np_utils import to_categorical
from sklearn.metrics import classification_report

# MB_true = {
#             '2':[4],
#             '4': [1, 2, 3, 5],
#             '5':[0,4,6,3],
#             '6':[5,3,35,34],
#             '8':[7,34],
#             '11':[10,34],
#            '14': [13,33,36,35,12,20,32 ],
#            '19':[18,31,20,23],
#            '24': [23,17,31,25,30,22,29,16],
#             '25':[24,29,16],
#            '29': [26,28,25,30,16,24],
#            '30': [16,17,31,15,29,24,32],
#            '33': [14,12,20,32,34],
#            '34': [33,7,10,35,8,11,9],
#            '35':[36,34,6,14],
#     }

# MB_pre = {'2': [4],
        # '4': [1, 2, 3, 5],
        # '5': [0, 3, 4, 6],
        # '6': [34, 3, 35, 4, 5],
        # '8': [34, 7],
        # '11': [34, 10],
        # '14': [32, 33, 35, 36],
        # '19': [18, 20, 23, 31],
        # '24': [16, 17, 23, 25, 29, 30, 31],
        # '25': [24, 16, 29],
        # '29': [24, 25, 26, 28, 30],
        # '30': [17, 24, 31, 29, 15],
        # '33': [32, 34, 14],
        # '34': [33, 35, 6, 7, 8, 9, 10, 11],
        # '35': [34, 36, 6, 14] }

def load_pred_MB(filename):
    MB_pred = pd.read_csv(filename)
    MB_dict = {}
    for col in MB_pred:
        MB_dict[col] = [str(int(i)) for i in list(MB_pred[col]) if not np.isnan(i) ]
    return MB_dict   # return {'2':[1,2,4], '4':[1,5,8]}

# Like 'Alarm.csv' and numerical column names

def load_mb_data(dataset, target, MB_dict):
    df = pd.read_csv(dataset)
    if target in MB_dict:
        MB = MB_dict[target]
    else:
        print('Target Input Error!')
        sys.exit()
    X = df.loc[:, MB]
    encoder = OneHotEncoder(sparse=False)
    X = encoder.fit_transform(X) # X = pd.get_dummy(X)
    y = df[target]
    encoder = LabelEncoder()
    y = encoder.fit_transform(y)
    # Convert integers to dummy variables (i.e. one hot encoded)
    y = np_utils.to_categorical(y)
    return X, y

    # Original dataset "ALARM.csv"
def load_all_data(target,  data_name = 'ALARM.csv'):
    #https://stackoverflow.com/questions/43515877/should-binary-features-be-one-hot-encoded
    data_path = 'DATASET/' + data_name
    df = pd.read_csv( data_path, index_col=False )

    for col in df.columns:
        if df[col].dtype == 'bool':
            df[col] = df[col].map({True: 1, False: 0})
    y = df.pop(target)
    encoder = LabelEncoder()
    y = encoder.fit_transform(y)
    y = np_utils.to_categorical(y)

    encoder = OneHotEncoder(sparse=False)
    X = encoder.fit_transform(df)
    return X, y

# '4' : 'LVEDVOLUME'
# '5' : 'LVFAILURE'
# '6' : 'STROKEVOLUME'
# '14' : 'TPR'
# '24' : 'INTUBATION'
# '29' : 'VENTTUBE'
# '30': 'VENTLUNG'
#  '34' : 'HR'
def main(flag_MB = 0):

    if flag_MB == 1:
        # MB features as input
        MB_dict = load_pred_MB('Pred_MB_MBOR.csv')
        # 'ALARM_SAMPLES.csv': numerical columns and rows
        X, y = load_mb_data('ALARM_SAMPLES.csv' ,'34', MB_dict)
        name = 'MB'
    else:
        # All features as input
        X, y = load_all_data('HR')
        name = 'All'

    input_shape = X.shape[1]
    num_class = len(y[0])
    res = []
    no_epochs = 200

    seed = 15
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state= seed)
    print(X_train)
    print(y_test.shape)
    kf = KFold(n_splits = 10, random_state = seed, shuffle = True )

#https://www.kaggle.com/questions-and-answers/236902

    for fold, (train_idx, val_idx) in enumerate(kf.split(X_train)):

        train_X, val_X = X_train[train_idx], X_train[val_idx]
        train_Y, val_Y = y_train[train_idx], y_train[val_idx]

        MB_model = MB_nn(input_shape , num_class)

        MB_model.assign_data(train_X, train_Y, val_X , val_Y, X_test, y_test)

        initial_weights = MB_model.model.get_weights()
        optim = tf.keras.optimizers.Adam()
        MB_model.train(no_epochs, optim)

        # Choose the best weights on the validation data from 10 fold results
        MB_model.model.set_weights(MB_model.best_weights)

        y_pred = MB_model.model.predict(MB_model.X_test)

        ess = tf.keras.losses.CategoricalCrossentropy()
        Entropy_Loss = ess(y_test, y_pred).numpy()
        res.append([fold, Entropy_Loss])
        print (fold + 1, Entropy_Loss)

        y_pred = np.argmax(y_pred, axis=1)
        y_test_temp = np.argmax(y_test, axis=1)
        report = classification_report(y_test_temp, y_pred, output_dict=True)

        df_classification_report = pd.DataFrame(report).transpose()
        #df_classification_report = df_classification_report.sort_values(by=['f1-score'], ascending=False)
        print(df_classification_report)

        MB_model.model.reset_states()

    df_results = pd.DataFrame(res, columns = ['Run', 'Entropy_Loss'])
    df_results.to_csv(f'{name}.csv', index = False)

    print (df_results['Entropy_Loss'].mean())
    print (df_results['Entropy_Loss'].std())

if __name__ == "__main__":
    main(0)


# A brief script to convert GIF files to RAW with Vector's screen dimensions. Use GIF files that are 184x96 for best results.
# Expected Python Version is 3.9.

import os,sys
#import struct
import array
from PIL import Image
#import Image

SCREEN_WIDTH,SCREEN_HEIGHT = 184,96 #240,240 #180,240
SIZE = (SCREEN_WIDTH,SCREEN_HEIGHT)

def pack16bitRGB(pixel):
#    print(pixel)
    try:
        r,g,b,a = pixel
    except (ValueError,TypeError):
        try:
            a = 0
            r,g,b = pixel
        except (ValueError,TypeError):
            r = g = b = pixel
    #    print(r,g,b,a,"\n")
    word = ( (int(r>>3)<<11) |
             (int(g>>2)<< 5) |
             (int(b>>3)<< 0) )
    return word
    # return ((word>>8)&0xFF) | ((word&0xFF)<<8)

def convert_to_raw(img):
    bitmap = [0x0000]*(SCREEN_WIDTH*SCREEN_HEIGHT)
    for y in range(img.size[1]):
        for x in range(img.size[0]):
            pixel = pack16bitRGB(img.getpixel((x,y)))
            bitmap[(y)*SCREEN_WIDTH + (x)] = pixel
    return bitmap

RAW = 1

def convert_frame_to_data(frame):
    newframe = frame.convert('RGBA')
    newframe = convert_to_raw(newframe)
    data = array.array("H",newframe)
    return data

def extractGifFrames(inGif):
    frame = Image.open(inGif)
    nframes = 0
    with open('%s.raw' % (os.path.basename(inGif),), "wb+") as f:
        while frame:
#            newframe = frame.rotate(90).resize( SIZE, Image.ANTIALIAS).convert('RGBA')
            data = convert_frame_to_data(frame)
            f.write(data.tobytes())
            nframes += 1
            try:
                frame.seek( nframes )
            except EOFError:
                break;
    return True

def convertImages(dirname, images):
    outfilename = '%s/anim.raw' % dirname
    with open(outfilename, "wb+") as f:
        nframes = 0
        for filename in images:
            frame = Image.open(filename)
            data = convert_frame_to_data(frame)
            f.write(data.tobytes())
            nframes += 1

        print('wrote {} frames to {}'.format(nframes, outfilename))

if len(sys.argv) == 1:
    print('error: pass in a .gif file or a folder of sequentail images')
    exit(-1)
elif len(sys.argv) == 2:
    extractGifFrames(sys.argv[1])
else:
    print('got {} images'.format(len(sys.argv)))
    images = sorted(sys.argv[1:])
    convertImages(os.path.dirname(sys.argv[0]), images)


# Importing the ChoiceMC class
import sys
import os
try:
    from ChoiceMC import ChoiceMC, loadResult
except ModuleNotFoundError:
    sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)))
    from ChoiceMC import ChoiceMC, loadResult
import matplotlib.pyplot as plt
import time
import numpy as np

# Setting up the variables to sweep over
g_sweep = np.linspace(0.01, 3, 20)
N=4

time_str = "EntanglementSweep_N" + str(N) + "-"+str(time.gmtime().tm_year)+'-'+str(time.gmtime().tm_mon)+'-'+str(time.gmtime().tm_mday)
path = os.path.join(os.getcwd(), time_str)
try:
    os.mkdir(path)
except FileExistsError:
    pass
os.chdir(path)

# Creating arrays to store the S2 versus g data
entropy = np.zeros((len(g_sweep),3), float)
entropy_out = open("SecondRenyiEntropy_N"+str(N)+'.dat','w')
purity = np.zeros((len(g_sweep),3), float)
purity_out = open("Purity_N"+str(N)+'.dat','w')

if N > 2:
    entropy_RT = np.zeros((len(g_sweep),3), float)
    entropy_RT_out = open("SecondRenyiEntropy_RatioTrick_N"+str(N)+'.dat','w')
    purity_RT = np.zeros((len(g_sweep),3), float)
    purity_RT_out = open("Purity_RatioTrick_N"+str(N)+'.dat','w')
    acceptRatio_RT_dict = {}
    acceptRatio_RT_out = open("AcceptanceRatio_RatioTrick_N"+str(N)+'.dat','w')
    acceptRatioError_RT_dict = {}
    acceptRatioError_RT_out = open("AcceptanceRatioStdError_RatioTrick_N"+str(N)+'.dat','w')

for ig, g in enumerate(g_sweep):
    print("------------------------------------------------")
    print("Starting g = " + str(g))

    # Creating a ChoiceMC object for the current iteration
    PIMC = ChoiceMC(m_max=5, P=9, g=g, MC_steps=100000, N=N, PIGS=True, Nskip=100, Nequilibrate=100, T=0.25)
    # Creating the probability density matrix for each rotor
    PIMC.createFreeRhoMarx()
    # Creating the probability density matrix for nearest neighbour interactions
    PIMC.createRhoVij()
    # Performing MC integration
    PIMC.runMCReplica()
    # Storing and saving the data from the current run
    entropy[ig,:] = [g, PIMC.S2_MC, PIMC.S2_stdError_MC]
    entropy_out.write(str(g) + ' ' + str(PIMC.S2_MC) + ' ' + str(PIMC.S2_stdError_MC) + '\n')
    purity[ig,:] = [g, PIMC.purity_MC, PIMC.purity_stdError_MC]
    purity_out.write(str(g) + ' ' + str(PIMC.purity_MC) + ' ' + str(PIMC.purity_stdError_MC) + '\n')

    if N > 2:
        # Performing MC integration with the ratio trick
        PIMC.runMCReplica(ratioTrick=True)
        # Storing and saving the data from the current run
        entropy_RT[ig,:] = [g, PIMC.S2_MC, PIMC.S2_stdError_MC]
        entropy_RT_out.write(str(g) + ' ' + str(PIMC.S2_MC) + ' ' + str(PIMC.S2_stdError_MC) + '\n')
        purity_RT[ig,:] = [g, PIMC.purity_MC, PIMC.purity_stdError_MC]
        purity_RT_out.write(str(g) + ' ' + str(PIMC.purity_MC) + ' ' + str(PIMC.purity_stdError_MC) + '\n')
        acceptRatio_RT_dict.update({g: PIMC.AR_MC_arr})
        acceptRatioError_RT_dict.update({g: PIMC.AR_stdError_MC_arr})

        acceptRatio_RT_out.write(str(g))
        for AR in PIMC.AR_MC_arr:
            acceptRatio_RT_out.write(' ' + str(AR))
        acceptRatio_RT_out.write('\n')

        acceptRatioError_RT_out.write(str(g))
        for stdAR in PIMC.AR_stdError_MC_arr:
            acceptRatioError_RT_out.write( ' ' + str(stdAR))
        acceptRatioError_RT_out.write('\n')

    # Closing the remaining open plots
    plt.close('all')

if N==2:
    # Loading in ED results
    arrS2_ED = loadResult(os.path.join('ED', 'SecondRenyiEntropy_mMax5.dat'))

if N > 2:
    acceptRatio_RT = np.zeros((len(acceptRatio_RT_dict[g_sweep[0]]), len(g_sweep), 3))
    for ig, g in enumerate(acceptRatio_RT_dict):
        for iPartition, Partition in enumerate(acceptRatio_RT_dict[g]):
            acceptRatio_RT[iPartition,ig,:] = [g, acceptRatio_RT_dict[g][iPartition], acceptRatioError_RT_dict[g][iPartition]]
# Plotting
S2_fig, S2_ax = plt.subplots(1, 1, figsize=(8,5))
S2_ax.errorbar(entropy[:,0], entropy[:,1], entropy[:,2], label='PIGS', fmt='.-', capsize=3, color='k')
if N == 2:
    S2_ax.plot(arrS2_ED[:,0], arrS2_ED[:,1], label='ED', marker='o', color='#d62728')
elif N > 2:
    S2_ax.errorbar(entropy_RT[:,0], entropy_RT[:,1], entropy_RT[:,2], label='PIGS:RT', fmt='.-', capsize=3, color='#1f77b4')
S2_ax.legend()
S2_ax.minorticks_on()
S2_ax.set_xlabel('g')
S2_ax.set_ylabel(r'$S_2$')
S2_ax.annotate('N = ' + str(N), xy=(0.5, 0.95),  xycoords='axes fraction', horizontalalignment='center', verticalalignment='top')
S2_fig.tight_layout()
S2_fig.savefig("SecondRenyiEntropy_N" + str(N) + ".png")

# Plotting
AR_fig, AR_ax = plt.subplots(1, 1, figsize=(8,5))
AR_ax.errorbar(purity[:,0], purity[:,1], purity[:,2], label='PIGS', fmt='.-', capsize=3, color='k')
if N > 2:
    AR_ax.errorbar(purity_RT[:,0], purity_RT[:,1], purity_RT[:,2], label='PIGS:RT', fmt='.-', capsize=3, color='#1f77b4')
AR_ax.minorticks_on()
AR_ax.legend()
AR_ax.set_xlabel('g')
AR_ax.set_ylabel('Purity')
AR_ax.annotate('N = ' + str(N), xy=(0.5, 0.95),  xycoords='axes fraction', horizontalalignment='center', verticalalignment='top')
AR_fig.tight_layout()
AR_fig.savefig("Purity_N" + str(N) + ".png")

# Plotting
AR_fig, AR_ax = plt.subplots(1, 1, figsize=(8,5))
AR_ax.errorbar(purity[:,0], purity[:,1], purity[:,2], label=r'$N_{S}/N_{U}$', fmt='.-', capsize=3)
if N > 2:
    AR_ax.errorbar(purity_RT[:,0], purity_RT[:,1], purity_RT[:,2], label=r'$N_{S}/N_{U}$: RT', fmt='.-', capsize=3)
    for i in range(np.shape(acceptRatio_RT)[0]):
        AR_ax.errorbar(acceptRatio_RT[i,:,0], acceptRatio_RT[i,:,1], acceptRatio_RT[i,:,2], label=r'$N_{'+str(i+1)+'}/N_{'+str(i)+'}$: RT', fmt='.-', capsize=3)
AR_ax.minorticks_on()
AR_ax.legend()
AR_ax.set_xlabel('g')
AR_ax.set_ylabel('Acceptance Ratio')
AR_ax.annotate('N = ' + str(N), xy=(0.5, 0.95),  xycoords='axes fraction', horizontalalignment='center', verticalalignment='top')
AR_fig.tight_layout()
AR_fig.savefig("AcceptanceRatio_N" + str(N) + ".png")

entropy_out.close()
purity_out.close()
if N > 2:
    entropy_RT_out.close()
    purity_RT_out.close()
    acceptRatio_RT_out.close()
    acceptRatioError_RT_out.close()
plt.close('all')