from __future__ import annotations

import json
from typing import Literal, Optional

from htmltools import Tag, TagAttrs, TagAttrValue, TagChild, tags

from .._namespaces import resolve_id_or_none
from ._tag import consolidate_attrs
from ._web_component import web_component

def tooltip(
    trigger: TagChild,
    *args: TagChild | TagAttrs,
    id: Optional[str] = None,
    placement: Literal["auto", "top", "right", "bottom", "left"] = "auto",
    options: Optional[dict[str, object]] = None,
    **kwargs: TagAttrValue,
) -> Tag:
    """
    Add a tooltip to a UI element

    Display additional information when focusing (or hovering over) a UI element.

    Parameters
    ----------
    trigger
        A UI element (i.e., :class:`~htmltools.Tag`) to serve as the tooltips trigger.
        It's good practice for this element to be a keyboard-focusable and interactive
        element (e.g., :func:`~shiny.ui.input_action_button`,
        :func:`~shiny.ui.input_action_link`, etc.) so that the tooltip is accessible to
        keyboard and assistive technology users.
    *args
        Contents to the tooltip's body. Or tag attributes that are supplied to the
        resolved :class:`~htmltools.Tag` object.
    id
        A character string. Required to re-actively respond to the visibility of the
        tooltip (via the `input[id]` value) and/or update the visibility/contents of the
        tooltip.
    placement
        The placement of the tooltip relative to its trigger.
    options
        A list of additional [Bootstrap
        options](https://getbootstrap.com/docs/5.3/components/tooltips/#options).

    Details
    -------

    If `trigger` yields multiple HTML elements (e.g., a :class:`~htmltools.TagList` or
    complex [`shinywidgets`](https://github.com/posit-dev/py-shinywidgets) object), the
    last HTML element is used as the trigger. If the `trigger` should contain all of
    those elements, wrap the object in a :func:`~shiny.ui.tags.div` or
    :func:`~shiny.ui.tags.span`.

    Accessibility of Tooltip Triggers
    ---------------------------------

    Because the user needs to interact with the `trigger` element to see the `tooltip`,
    it's best practice to use an element that is typically accessible via keyboard
    interactions, like a button or a link.

    If you use a non-interactive element, like a `<span>` or text, `tooltip()` will
    automatically add the `tabindex="0"` attribute to the trigger element to make sure
    that users can reach the element with the keyboard. This means that in most cases
    you can use any element you want as the trigger.

    One place where it's important to consider the accessibility of the trigger is when
    using an icon without any accompanying text. In these cases, many icon elements are
    created with the assumption that the icon is decorative, which will make it
    inaccessible to users of assistive technologies.

    When using an icon as the primary trigger, ensure that the icon does not have
    `aria-hidden="true"` or `role="presentation"` attributes. Icon packages typically
    provide a way to specify a title for the icon, as well as a way to specify that the
    icon is not decorative. The title should be a short description of the purpose of
    the trigger, rather than a description of the icon itself.

    For example:

    ```python
    icon_title = "About tooltips"
    def bs_info_icon(title: str):
        # Enhanced from https://rstudio.github.io/bsicons/ via `bsicons::bs_icon("info-circle", title = icon_title)`
        return ui.HTML(f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" class="bi bi-info-circle " style="height:1em;width:1em;fill:currentColor;" aria-hidden="true" role="img" ><title>{title}</title><path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z"></path><path d="m8.93 6.588-2.29.287-.082.38.45.083c.294.07.352.176.288.469l-.738 3.468c-.194.897.105 1.319.808 1.319.545 0 1.178-.252 1.465-.598l.088-.416c-.2.176-.492.246-.686.246-.275 0-.375-.193-.304-.533L8.93 6.588zM9 4.5a1 1 0 1 1-2 0 1 1 0 0 1 2 0z"></path></svg>')

    ui.tooltip(
        bs_info_icon(icon_title),
        "Text shown in the tooltip."
    )
    ```

    ```python
    icon_title = "About tooltips"
    def fa_info_circle(title: str):
        # Enhanced from https://rstudio.github.io/fontawesome/ via `fontawesome::fa("info-circle", a11y = "sem", title = icon_title)`
        return ui.HTML(f'<svg aria-hidden="true" role="img" viewBox="0 0 512 512" style="height:1em;width:1em;vertical-align:-0.125em;margin-left:auto;margin-right:auto;font-size:inherit;fill:currentColor;overflow:visible;position:relative;"><title>{title}</title><path d="M256 512A256 256 0 1 0 256 0a256 256 0 1 0 0 512zM216 336h24V272H216c-13.3 0-24-10.7-24-24s10.7-24 24-24h48c13.3 0 24 10.7 24 24v88h8c13.3 0 24 10.7 24 24s-10.7 24-24 24H216c-13.3 0-24-10.7-24-24s10.7-24 24-24zm40-208a32 32 0 1 1 0 64 32 32 0 1 1 0-64z"/></svg>')
    ui.tooltip(
        fa_info_circle(icon_title),
        "Text shown in the tooltip."
    )
    ```

    See Also
    --------

    * [Bootstrap tooltips documentation](https://getbootstrap.com/docs/5.3/components/tooltips/)
    """

    # * If you're using [bsicons::bs_icon()], provide a `title`.
    # * If you're using [fontawesome::fa()], set `a11y = "sem"` and provide a `title`.

    # Theming/Styling
    # ---------------
    #
    # Like other bslib components, tooltips can be themed by supplying [relevant theming
    # variables](https://rstudio.github.io/bslib/articles/bs5-variables.html#tooltip-bg)
    # to [bs_theme()], which effects styling of every tooltip on the page. To style a
    # _specific_ tooltip differently from other tooltips, utilize the `customClass`
    # option:
    #
    # ```
    # tooltip(
    #     "Trigger", "Tooltip message",
    #     options = {"customClass": "my-pop"}
    # )
    # ```
    #
    # And then add relevant rules to [bs_theme()] via [bs_add_rules()]:
    #
    # ```
    # bs_theme() |> bs_add_rules(".my-pop { max-width: none; }")
    # ```

    attrs, children = consolidate_attrs(*args, **kwargs)

    if len(children) == 0:
        raise RuntimeError("At least one value must be provided to `*args: TagChild`")

    res = web_component(
        "bslib-tooltip",
        {
            "id": resolve_id_or_none(id),
            "placement": placement,
            "options": json.dumps(options) if options else None,
        },
        attrs,
        # Use display:none instead of <template> since shiny.js
        # doesn't bind to the contents of the latter
        tags.template(*children, {"style": "display:none;"}),
        trigger,
    )

    return res


import os
import argparse
from funasr.utils.job_runner import MultiProcessRunnerV3
from funasr.utils.misc import load_scp_as_list, load_scp_as_dict

class MyRunner(MultiProcessRunnerV3):
    def prepare(self, parser):
        assert isinstance(parser, argparse.ArgumentParser)
        parser.add_argument("wav_scp", type=str)
        parser.add_argument("out_dir", type=str)
        args = parser.parse_args()
        # assert args.sr == 8000, "For callhome dataset, the sample rate should be 8000, use --sr 8000."

        wav_scp = load_scp_as_list(args.wav_scp)
        if not os.path.exists(args.out_dir):
            os.makedirs(args.out_dir)

        return wav_scp, None, args

    def post(self, result_list, args):
        count = [0, 0]
        for result in result_list:
            count[0] += result[0]
            count[1] += result[1]
        print("All threads done, {} success, {} failed.".format(count[0], count[1]))

def process(task_args):
    task_id, task_list, _, args = task_args

    count = [0, 0]
    for utt_id, cmd in task_list:
        try:
            wav_path = os.path.join(args.out_dir, "{}.wav".format(utt_id))
            cmd = cmd.replace("|", "> {}".format(wav_path))
            os.system(cmd)
            count[0] += 1
        except:
            print("Failed execute command for {}.".format(utt_id))
            count[1] += 1

    return count

if __name__ == '__main__':
    my_runner = MyRunner(process)
    my_runner.run()


from torchvision import transforms
from torchvision import datasets
from torch.utils.data import DataLoader
from MLPMixerModel import *

batch_size = 64
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))])  # 归一化,均值和方差

train_dataset = datasets.MNIST(root='./dataset/mnist/', train=True, download=True, transform=transform)
train_loader = DataLoader(train_dataset, shuffle=True, batch_size=batch_size)
test_dataset = datasets.MNIST(root='./dataset/mnist/', train=False, download=True, transform=transform)
test_loader = DataLoader(test_dataset, shuffle=False, batch_size=batch_size)

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

model = MLPMixer(in_channels=1, dim=64, num_classes=10, patch_size=4, image_size=28, depth=2, token_dim=128, channel_dim=128)
#                in_channels=1, dim=64, num_classes=10, patch_size=4, image_size=28, depth=1, token_dim=64, channel_dim=128    95%
#                in_channels=1, dim=64, num_classes=10, patch_size=4, image_size=28, depth=2, token_dim=64, channel_dim=128    97%
#                in_channels=1, dim=64, num_classes=10, patch_size=4, image_size=28, depth=3, token_dim=64, channel_dim=128    97%
#                in_channels=1, dim=64, num_classes=10, patch_size=4, image_size=28, depth=2, token_dim=128, channel_dim=512   97%
model.to(device)

criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr = 0.01, momentum=0.5)

def train(epoch):
    running_loss = 0.0
    for batch_index, data in enumerate(train_loader, 0):
        inputs, labels = data
        inputs, labels = inputs.to(device), labels.to(device)

        y_pred = model(inputs)
        loss = criterion(y_pred, labels)
        optimizer.zero_grad()

        loss.backward()
        optimizer.step()

        running_loss += loss.item()
        if batch_index%300 == 299:
            print('[%d, %5d] loss: %.3f' % (epoch + 1, batch_index + 1, running_loss / 300))
            running_loss = 0.0

def test():
    correct = 0
    total = 0
    with torch.no_grad():
        for data in test_loader:
            inputs, labels = data
            inputs, labels = inputs.to(device), labels.to(device)
            outputs = model(inputs)
            _, predicted = torch.max(outputs.data, dim = 1)
            total += labels.size(0)
            correct += (predicted == labels).sum().item()
    print('accuracy on test set: %d %% ' % (100 * correct / total))
    return correct / total

if __name__ == "__main__":
    epoch_list = []
    acc_list = []
    print("使用" + str(device) + "训练")

    for epoch in range(10):
        train(epoch)
        acc = test()
        epoch_list.append(epoch)
        acc_list.append(acc)


"Intersect frames to pull out the foreground."
import numpy as np
from baboon_tracking.mixins.intersected_frames_mixin import IntersectedFramesMixin
from baboon_tracking.mixins.group_shifted_history_frames_mixin import (
    GroupShiftedHistoryFramesMixin,
)

from pipeline import Stage
from pipeline.decorators import stage
from pipeline.stage_result import StageResult

@stage("group_shifted_history_frames")
class IntersectFrames(Stage, IntersectedFramesMixin):
    "Intersect frames to pull out the foreground."

    def __init__(
        self, group_shifted_history_frames: GroupShiftedHistoryFramesMixin
    ) -> None:
        Stage.__init__(self)
        IntersectedFramesMixin.__init__(self)
        self._group_shifted_history_frames = group_shifted_history_frames

    def execute(self) -> StageResult:
        grouped_shifted_history_frames = (
            self._group_shifted_history_frames.grouped_shifted_history_frames
        )
        grouped_quantized_frames = (
            self._group_shifted_history_frames.grouped_quantized_frames
        )

        self.intersected_frames = self._intersect_all_frames(
            grouped_shifted_history_frames, grouped_quantized_frames
        )

        return StageResult(True, True)

    def _intersect_frames(self, frames, q_frames):
        """
        Intersect two consecutive frames to find common background between those two frames
        Returns the single frame produced by intersection
        """
        mask = np.abs(q_frames[0] - q_frames[1]) <= 1
        combined = frames[0].get_frame().copy()
        combined[mask] = 0

        return combined

    def _intersect_all_frames(
        self, grouped_shifted_history_frames, grouped_quantized_frames
    ):
        """
        Takes in two lists of frames, performs intersect on each pair and returns array of intersects
        """
        return [
            self._intersect_frames(z[0], z[1])
            for z in zip(grouped_shifted_history_frames, grouped_quantized_frames)
        ]


import numpy as np
import warnings
from lime import submodular_pick
from tqdm import tqdm
from lime.lime_tabular import LimeTabularExplainer
import pandas as pd
import joblib

X_train = pd.read_csv("/ml_data/X_train.csv").drop(columns = "Unnamed: 0")
y_train = pd.read_csv("/ml_data/y_train.csv").drop(columns = "Unnamed: 0")
X_test = pd.read_csv("/ml_data/X_test.csv").drop(columns = "Unnamed: 0")
y_test = pd.read_csv("/ml_data/y_test.csv").drop(columns = "Unnamed: 0")
model = joblib.load("/artifact/mlruns/813623427044464195/2cfa40c2107c47cebc0450c6ed0ddbd0/artifacts/model/model.pkl")
# model = joblib.load("/artifact/mlruns/941649382202349625/e311be07056d4a4fa7413ec46830e13b/artifacts/model/model.pkl")
task = "regression"
target = "price"
pipeline_name = "non_remove_outlier median StandardScaler"

all_columns = []
cat_feature = []
for i in range(len(model["preprocessor"].transformers_)):
    if model["preprocessor"].transformers_[i][0] == "num":
        all_columns.extend(model["preprocessor"].transformers[i][2])
    else:
        for j in range(len(model['preprocessor'].transformers_[1][1][0].categories_)):
            all_columns.extend(list(model['preprocessor'].transformers_[1][1][0].categories_[j]))
            cat_feature.append(list(model['preprocessor'].transformers_[1][1][0].categories_[j]))

def load_transform(file_name):
    preprocessor = joblib.load(f'/save_pipeline/{file_name}.pkl')
    return preprocessor

if str(model[1]) ==  'KNeighborsClassifier()':
    pred_model = model[-1].predict_proba
else:
    pred_model = model[-1].predict

def check_type(df)-> list[str]:
    df_columns = list(df.columns)
    numeric = []
    catagories = []
    for values in df.columns:
        if df[values].dtypes == int or df[values].dtypes == float:
            numeric.append(values)
        else:
            catagories.append(values)
    return catagories

def check_file():
    path = Path("/model_interpret")
    return path.is_file()

def _explainer(task, target):
    if task == "classification":
        class_names = y_train[target].unique()
        # Fit the Explainer on the training data set using the LimeTabularExplainer
        explainer = LimeTabularExplainer(load_transform(pipeline_name).fit_transform(X_train), feature_names = all_columns, class_names = class_names, mode = task, categorical_features = cat_feature)
    else:
        explainer = LimeTabularExplainer(load_transform(pipeline_name).fit_transform(X_train), feature_names = all_columns, mode = "regression", class_names = [target], categorical_features = cat_feature)

    return explainer

def return_x(explaination, int_x):
    return explaination.domain_mapper.feature_names[int_x]

def return_x_2(explaination,int_x, i):
    return explaination.explanations[i].domain_mapper.feature_names[int_x]

def _to_csv_local(lime_obj, num):
    cols = ["importance_values_"+i for i in [lime_obj.domain_mapper.feature_names][0]]
    values = lime_obj.domain_mapper.feature_values
    arr = np.array(values).reshape((1, len(cols)))
    df_x = pd.DataFrame(arr , columns = cols)
    return pd.concat([X_test[X_test.index == num], df_x], axis = 1)

def _to_csv_global(lime_obj):
    all_df = []
    for i in range(0,5):
        cols = lime_obj.explanations[i].domain_mapper.feature_names
        values = lime_obj.explanations[i].domain_mapper.feature_values
        arr = np.array(values).reshape((1, len(list(lime_obj.explanations[i].local_exp.values())[0])))
        all_df.append(pd.DataFrame(arr , columns = cols))
    return pd.concat(all_df, ignore_index =True)

def save_to_csv(task, target):
    all_df_local = []
    num = -1
    for i in tqdm(model[0].fit_transform(X_test)):
        num+=1
        explainer = _explainer(task, target)
        explaination = explainer.explain_instance(i, pred_model)
        all_df_local.append(_to_csv_local(explaination, num))

    explainer = _explainer(task, target)
    sp_obj = submodular_pick.SubmodularPick(explainer, model[0].fit_transform(X_train), pred_model, num_features= len(all_columns), num_exps_desired = 5)
    all_df_global = _to_csv_global(sp_obj)
    pd.concat(all_df_local, ignore_index = True).fillna(0).to_csv("/model_interpret/local.csv")
    all_df_global.fillna(0).to_csv("/model_interpret/global.csv")


import sys
from collections import deque
input = sys.stdin.readline

def install():
    que = deque()
    for i in range(R):
        for j in range(C):
            if graph[i][j] == "O":
                que.append((j, i))
            else:
                graph[i][j] = "O"
    return que

def boom(q):
    dx = [-1, 1, 0, 0]
    dy = [0, 0, -1, 1]
    while q:
        x, y = q.popleft()
        graph[y][x] = "."
        for k in range(4):
            nx = x + dx[k]
            ny = y + dy[k]
            if 0 <= ny < R and 0 <= nx < C:
                graph[ny][nx] = "."

if __name__ == "__main__":
    R, C, N = map(int, input().strip().split())
    graph = [list(map(str, input().strip())) for _ in range(R)]
    for n in range(N + 1):
        if n % 3 == 0:
            continue
        elif n % 3 == 1:
            q = install()
        elif n % 3 == 2:
            boom(q)
    for gra in graph:
        answer = ""
        for g in gra:
            answer += g
        print(answer)


from __future__ import unicode_literals

from django import forms
from django.conf import settings
from django.contrib import messages
from django.http.response import HttpResponseRedirect
from django.utils.timezone import now
from django.utils.translation import ugettext_lazy as _
from django.views.generic import FormView

from shuup.addons.manager import get_addons_from_entry_points, get_enabled_addons, set_enabled_addons
from shuup.admin.toolbar import NewActionButton, PostActionButton, Toolbar, URLActionButton
from shuup.utils.django_compat import reverse
from shuup.utils.http import get_client_ip

class AddonEnableDisableForm(forms.Form):
    def __init__(self, **kwargs):
        super(AddonEnableDisableForm, self).__init__(**kwargs)
        self.addons = sorted(get_addons_from_entry_points())
        self._create_fields()

    def _create_fields(self):
        enabled_addons = get_enabled_addons(settings.SHUUP_ENABLED_ADDONS_FILE)
        for addon in sorted(self.addons):
            self.fields[addon] = forms.BooleanField(
                required=False, initial=(addon in enabled_addons), label=_("Enable %s") % addon
            )

    def get_enabled_addons(self):
        return [addon for addon in self.addons if self.cleaned_data.get(addon)]

class AddonListView(FormView):
    template_name = "shuup/admin/addons/list.jinja"
    form_class = AddonEnableDisableForm

    def form_valid(self, form):
        old_enabled_addons = get_enabled_addons(settings.SHUUP_ENABLED_ADDONS_FILE)
        new_enabled_addons = form.get_enabled_addons()
        changes = []
        n_add = len(set(new_enabled_addons) - set(old_enabled_addons))
        n_del = len(set(old_enabled_addons) - set(new_enabled_addons))
        if n_add:
            changes.append(_("%d new addons enabled.") % n_add)
        if n_del:
            changes.append(_("%d previously enabled addons disabled.") % n_del)
        if changes:
            set_enabled_addons(
                settings.SHUUP_ENABLED_ADDONS_FILE,
                new_enabled_addons,
                comment="Written via Shuup admin (user %s; IP %s; time %s)"
                % (self.request.user.pk, get_client_ip(self.request), now().isoformat()),
            )
            messages.success(self.request, " ".join(changes))
            return HttpResponseRedirect(self.request.path + "?reload=1")

        messages.info(self.request, _("No changes were made."))
        return HttpResponseRedirect(self.request.path)

    def get_context_data(self, **kwargs):
        context = super(AddonListView, self).get_context_data(**kwargs)
        context["toolbar"] = Toolbar(
            [
                PostActionButton(
                    icon="fa fa-save",
                    form_id="addon_list",
                    text=_("Save addon changes"),
                    extra_css_class="btn-success",
                ),
                NewActionButton(
                    reverse("shuup_admin:addon.upload"),
                    text=_("Upload new addon"),
                    extra_css_class="btn-info",
                    icon="fa fa-upload",
                ),
            ],
            view=self,
        )
        if self.request.GET.get("reload"):
            context["toolbar"].append(
                URLActionButton(
                    reverse("shuup_admin:addon.reload"),
                    text=_("Reload application"),
                    extra_css_class="btn-warning",
                    icon="fa fa-refresh",
                )
            )

        return context


from util import get_pkg_path_and_version, ant_glob
from os.path import join

def configure(ctx):
    path, version = get_pkg_path_and_version("pidgin-sipe")

    ctx.env.SIPE_PATH = path
    ctx.env.SIPE_VERSION = version

    ctx.env.append_value("DEFINES_SIPE_BUILD", "HAVE_CONFIG_H")
    ctx.env.append_value("INCLUDES_SIPE_BUILD",
                            ["pidgin-sipe_config",
                             join(ctx.env.SIPE_PATH, "src", "api")])

    headers = ["dlfcn", "inttypes", "langinfo", "locale", "memory", "stdint",
               "stdlib", "strings", "string", "sys/sockio", "sys/stat",
               "sys/types", "unistd"]
    for i in headers:
        ctx.check_cc(header_name=i + ".h", mandatory=False,
                      auto_add_header_name=True)

    ctx.define("ENABLE_NLS", 1)
    ctx.define("HAVE_DCGETTEXT", 1)
    ctx.define("HAVE_GETTEXT", 1)
    ctx.define("HAVE_BIND_TEXTDOMAIN_CODESET", 1)
    ctx.define("STDC_HEADERS", 1)
    ctx.define("LOCALEDIR", "")
    ctx.define("PACKAGE_NAME", "pidgin-sipe")
    ctx.define("PACKAGE_STRING", "pidgin-sipe %s" % version)
    ctx.define("PACKAGE_TARNAME", "pidgin-sipe")
    ctx.define("PACKAGE_URL", "http://sipe.sourceforge.net")
    ctx.define("PACKAGE_VERSION", version)
    ctx.define("PACKAGE_BUGREPORT",
                "https://sourceforge.net/tracker/?atid=949931&group_id=194563")
    ctx.define("SIPE_TRANSLATIONS_URL",
                "https://www.transifex.net/projects/p/pidgin-sipe/r/mob/")

    ctx.write_config_header("pidgin-sipe_config/config.h")

def build(ctx):
    ctx.objects(target="libsipe",
                source=ant_glob(ctx, ctx.env.SIPE_PATH, "src", "core", "*.c",
                                exclude=["sip-sec-krb5.c",
                                         "sip-sec-sspi.c",
                                         "sipe-mime.c",
                                         "sipe-win32dep.c",
                                         "sip-sec-ntlm.c",
                                         "*test*.c"]),
                includes=join(ctx.env.SIPE_PATH, "src", "api"),
                use="GLIB NSS SIPE_BUILD XML"
               )


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as tck
import csv
import sys
from mastercurves import MasterCurve
from mastercurves.transforms import Multiply
from sklearn.gaussian_process.kernels import WhiteKernel
import time

# Relative noise level
noise = 0.2

# Read the data
T = [97, 100.6, 101.8, 104.5, 106.7, 109.6, 114.5, 125, 133.8, 144.9]
T.reverse()
ts = [[] for i in range(len(T))]
Js = [[] for i in range(len(T))]
with open("data/tts_plazek.csv") as file:
    reader = csv.reader(file)
    next(reader)
    next(reader)
    for row in reader:
        for k in range(len(T)):
            if not row[2*k] == "":
                ts[k] += [float(row[2*k])]
                Js[k] += [float(row[2*k+1])]
for k in range(len(T)):
    ts[k] = np.log(np.array(ts[k]))
    Js[k] = np.log(np.array(Js[k]) + noise*np.random.randn(len(Js[k]))*np.array(Js[k]))

# Build a master curve
mc = MasterCurve()
mc.add_data(ts, Js, T)
mc.set_gp_kernel(mc.kernel)

# Add transformations
mc.add_htransform(Multiply())

# Superpose
start = time.time()
mc.superpose()
stop = time.time()
print(f"Time: {stop-start} s")

# Compare to the WLF form from Plazek
a = mc.hparams[0]
T = np.array(T)
a_mod = 10**(-np.log10(a) + np.log10(a[-1]) + 1.13)
da = np.array(mc.huncertainties[0])
da_mod = 10**(1.13)*a[-1]*da/(np.array(a)**2)
fig, ax = plt.subplots(1,1)
ax.errorbar(T, a_mod, yerr=da_mod, ls='none', marker='o', mfc='k', mec='k')
Tv = np.linspace(90,150)
da_cum = [da_mod[-1]]
for i in range(1,len(a_mod)):
    da_cum += [a_mod[-i-1]*np.sqrt((da_mod[-i-1]/a_mod[-i-1])**2 + (da_cum[i-1]/a_mod[-i])**2)]
da_cum.reverse()
#plt.rcParams.update({'font.size': 12})
ax.semilogy(Tv, 10**(-10.7*(Tv - 100)/(29.9 + Tv - 100)), 'b')
ax.set_xlim([90,150])
ax.set_ylim([1E-8,1E2])
ax.tick_params(which="both",direction="in",top=True,right=True)
ax.xaxis.set_minor_locator(tck.LinearLocator(numticks=61))
ax.yaxis.set_major_locator(tck.LogLocator(base=10.0, numticks=12))
ax.yaxis.set_minor_locator(tck.LogLocator(base=10.0, subs=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1], numticks=12))
ax.set_yticklabels(["", r"$10^{-8}$", "", r"$10^{-6}$", "", r"$10^{-4}$", "", r"$10^{-2}$", "", r"$10^{0}$", "", r"$10^{2}$"])

# Plot
mc.change_ref(100, a_ref=10**(1.13)*a[-1])
fig1, ax1, fig2, ax2, fig3, ax3 = mc.plot(log=True, colormap=plt.cm.viridis_r)
ax2.tick_params(which="both",direction="in",right=True,top=True)
ax3.tick_params(which="both",direction="in",right=True,top=True)
ax3.xaxis.set_major_locator(tck.LogLocator(base=10.0, numticks=14))
ax3.xaxis.set_minor_locator(tck.LogLocator(base=10.0, subs=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1],numticks=14))
ax3.set_xticklabels(["", "", "", r"$10^{0}$", "", r"$10^{2}$", "", r"$10^{4}$", "", r"$10^{6}$", "", r"$10^{8}$", "", r"$10^{10}$"])

plt.show()


from logging.config import dictConfig

def init_logger(log_file, level='DEBUG'):
    conf = {
        'version': 1,
        'formatters': {
            'f': {
                'format': '%(asctime)s, %(levelname)s, %(name)s, %(message)s',
                'datefmt': '%Y-%m-%dT%H:%M:%S'
            }
        },
        'handlers': {
            'console': {
                'class': 'logging.StreamHandler',
                'formatter': 'f',
                'level': level,
                'stream': 'ext://sys.stdout'
            },
            'file': {
                'class': 'logging.handlers.TimedRotatingFileHandler',
                'level': level,
                'formatter': 'f',
                'filename': log_file,
                'backupCount': 15,
                'when': 'midnight',
                'interval': 1,
                'utc': True
            }
        },
        'root': {
            'handlers': ['console', 'file'],
            'level': level,
        }
    }

    dictConfig(conf)


from asyncio.windows_events import NULL
from cmath import nan
import SimpleITK as sitk
import numpy as np
import numpy.ma as ma
from matplotlib import pyplot
import matplotlib.pyplot as plt
import os
import sys
import seaborn as sns
import pandas as pd
import statsmodels.tsa.stattools as sts
from datetime import datetime

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

def DataRoot(root):
    '''
    Returns the root directory of the data if on
    server or local
    '''
    if root == 1:
        # server
        root = "D:\\data\\"
    elif root == 2:
        # local
        root = "E:\\"

    return root

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

def CD(DataRoot, Extract, Norm, tag):
    '''
    Checks if folders exist and creates them if not
    '''
    root_dir = DataRoot + "Aaron\ProstateMRL\Data\Paper1\\"

    if not os.path.exists(root_dir + Norm):
        os.makedirs(root_dir + Norm)

    root_dir = os.path.join(root_dir, Norm)

    # Delta folder
    if not os.path.exists(root_dir + "\\Delta"):
        os.makedirs(root_dir + "\\Delta")

    # Longitudinal folder
    if not os.path.exists(root_dir + "\\Longitudinal"):
        os.makedirs(root_dir + "\\Longitudinal")
        os.makedirs(root_dir + "\\Longitudinal\\DM")
        os.makedirs(root_dir + "\\Longitudinal\\DM\\csvs")
        os.makedirs(root_dir + "\\Longitudinal\\DM\\Figs")
        os.makedirs(root_dir + "\\Longitudinal\\ClusterLabels")
        os.makedirs(root_dir + "\\Longitudinal\\ClusterPlots")

    # Features folder
    if not os.path.exists(root_dir + "\\Features"):
        os.makedirs(root_dir + "\\Features")
    if Extract == "No":
        if not os.path.exists(root_dir + "\\Features\\Longitudinal_All_fts_" + tag + ".csv"):
            df_fts = pd.read_csv(root_dir + "\\Features\\Longitudinal_All_fts_Baseline.csv")
            df_fts.to_csv(root_dir + "\\Features\\Longitudinal_All_fts_" + tag + ".csv")
            df_l = pd.read_csv(root_dir + "\\Features\\Longitudinal_Limbus_fts_Baseline.csv")
            df_l.to_csv(root_dir + "\\Features\\Longitudinal_Limbus_fts_" + tag + ".csv")
#####################################################

def SABRPats():
    '''
    Returns array of patIDs for SABR
    '''
    array = ['653', '713', '752', '826', '1088', '1089', '1118', '1303', '1307', '1464', '1029',
 '1302', '1431', '1481', '1540', '1553', '1601', '1642', '829', '955']

    return array

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

def NormArray():
    """
    Returns array of normalisation factors
    """
    Norms = ["Raw", "HM-FS", "HM-TP", "HM-FSTP", "Med-Pros", "Med-Glute", "Med-Psoas"]
    return Norms

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

def GetNorm(image_name):
    '''
    Input: image file name
    Output: Normalisation method
    '''
    n = image_name

    Norm = n.split("_")[2]
    Norm = Norm.replace(".nii", "")
    Norm = Norm.replace("v2", "")
    if Norm == "image":
        Norm = "Raw"

    return Norm

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

def GetRegion(mask_name):
    '''
    Input: Mask name
    Output: Mask region
    '''
    n = mask_name

    if "pros" in n:
        Region = "Prostate"
    elif "glute" in n:
        Region = "Glute"
    elif "psoas" in n:
        Region = "Psoas"
    else:
        Region = "-"

    return Region

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

def FixPatID(patID, treatment_group):
    '''
    Patient IDs are not consistent across the data
    '''
    if "new" in treatment_group:
        newID = str(patID)
    else:
        if len(str(patID)) == 3:
            newID = "0000" + str(patID)
        elif len(str(patID)) == 4:
            newID = "000" + str(patID)
        else:
            newID = str(patID)

    return newID

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

def FixDate(date):
    '''
    Reformats date to YYYYMMDD
    '''
    date_string = str(date)
    if len(date_string) != 8:
        date_string = date_string[:-2]
    date = datetime.strptime(date_string, "%Y%m%d").date()

    return date
####################################################

def GetNiftiPaths(patient_path, treatment):
    """
    Returns the paths to the nifti files
    """
    mask_path = os.path.join(patient_path, "Masks\\")

    if treatment == "20fractions":
        mask_labels = ["_shrunk_pros.nii", "_glute2.nii", "_psoas.nii"] # set glute2 for 20fractions
    else:
        mask_labels = ["_shrunk_pros.nii", "_glute.nii", "_psoas.nii"]

    image_roots = ["RawImages\\", "HM-TP\\", "HM-FS\\", "HM-FSTP\\","Norm-Pros\\", "Norm-Glute\\", "Norm-Psoas\\", "Med-Pros\\", "Med-Glute\\", "Med-Psoas\\"]
    #image_roots = ["BaseImages\\", "HM-TP\\","Norm-Psoas\\", "Med-Psoas\\"]

    image_labels = ["Raw", "HM-TP", "HM-FS",  "HM-FSTP", "Norm-Pros","Norm-Glute", "Norm-Psoas", "Med-Pros", "Med-Glute", "Med-Psoas"]
    #image_labels = ["Raw", "HM-TP", "Norm-Psoas", "Med-Psoas"]

    image_paths = []

    for b in image_roots:
        image_paths.append(os.path.join(patient_path, b))

    return mask_path, mask_labels, image_paths, image_labels

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

def GetNiftiPathsProsSens(patient_path, treatment):
    """
    """
    mask_path = os.path.join(patient_path, "Masks\\")

    masks = os.listdir(mask_path)
    mask_labels = []
    for x in masks:
        if "shrunk_pros" in x[:-7]:
            mask_labels.append(x)

    image_roots = ["RawImages\\", "HM-TP\\", "HM-FS\\", "HM-FSTP\\","Norm-Pros\\", "Norm-Glute\\", "Norm-Psoas\\", "Med-Pros\\", "Med-Glute\\", "Med-Psoas\\"]
    #image_roots = ["BaseImages\\", "HM-TP\\","Norm-Psoas\\", "Med-Psoas\\"]

    image_labels = ["Raw", "HM-TP", "HM-FS",  "HM-FSTP", "Norm-Pros","Norm-Glute", "Norm-Psoas", "Med-Pros", "Med-Glute", "Med-Psoas"]
    #image_labels = ["Raw", "HM-TP", "Norm-Psoas", "Med-Psoas"]

    image_paths = []

    for b in image_roots:
        image_paths.append(os.path.join(patient_path, b))

    return mask_path, mask_labels, image_paths, image_labels

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

def GetImageFile(image_path, patient, scan, image_label):
    """
    Returns the path to the image file
    """
    label = image_label
    #if image_label.__contains__("Raw"):
    #    image_label == "image"
    #if label.__contains__("Norm"):# or label.__contains__("Med"):
    #    label = label + "_v2"

    file_name = patient + "_" + scan + "_" + label + ".nii"
    file_path = os.path.join(image_path, file_name)

    return file_path, file_name

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

def ClusterFtSelection(Cluster_ft_df):
    '''
    Input - df filtered for norm, patient, cluster
    Output - performs cross-correlation within clustered fts and returns ft most strongly correlated with the rest, if more than 2 fts present
    '''
    fts = Cluster_ft_df.FeatureName.unique()
    num_fts = len(fts)

    if num_fts > 2:
        vals = {} # stores fts and values
        ccfs = {} # stores cc values for each feature
        mean_ccfs = {} # stores the mean cc value for every feature

        for f in fts:
            ft_df = Cluster_ft_df[Cluster_ft_df["FeatureName"] == f]
            ft_vals = ft_df.FeatureChange.values
            vals[f] = ft_vals

        for v in vals:
            ft_1 = vals[v]
            ccfs[v] = v
            ccfs_vals = []

            for u in vals:
                ft_2 = vals[u]
                corr = sts.ccf(ft_1, ft_2)[0] # cross correlation value, index [0] for for 0 lag in csc function
                ccfs_vals.append(corr)

            mean_ccfs[v] = np.array(ccfs_vals).mean() # get mean across all cc values for each ft

        ft_selected = max(mean_ccfs, key=mean_ccfs.get) # get max mean cc value and return the feature

    else:
        ft_selected = np.nan

    return ft_selected

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

def ClusterFtSelection2(Cluster_ft_df):
    '''
    Input - df filtered for norm, patient, cluster
    Output - performs cross-correlation within clustered fts and returns ft most strongly correlated with the rest, if more than 2 fts present
    '''
    fts = Cluster_ft_df.Feature.unique()
    num_fts = len(fts)

    if num_fts > 2:
        vals = {} # stores fts and values
        ccfs = {} # stores cc values for each feature
        mean_ccfs = {} # stores the mean cc value for every feature
        num_sel = np.rint(len(fts) * 0.2)

        for f in fts:
            ft_df = Cluster_ft_df[Cluster_ft_df["Feature"] == f]
            ft_vals = ft_df.FeatureChange.values
            vals[f] = ft_vals

        for v in vals:
            ft_1 = vals[v]
            ccfs[v] = v
            ccfs_vals = []

            for u in vals:
                ft_2 = vals[u]
                corr = sts.ccf(ft_1, ft_2)[0] # cross correlation value, index [0] for for 0 lag in csc function
                ccfs_vals.append(corr)

            mean_ccfs[v] = np.array(ccfs_vals).mean() # get mean across all cc values for each ft

        s_mean_ccfs = sorted(mean_ccfs.items(), key=lambda x:x[1], reverse=True)
        sorted_temp = s_mean_ccfs[0:int(num_sel)]
        ft_selected = [seq[0] for seq in sorted_temp]

    else:
        ft_selected = 0

    return ft_selected

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

def ICC_Class(icc_val):
    '''
    Classifies the icc value into poor, moderate, good, excellent
    '''

    if icc_val < 0.5:
        icc_class = "Poor"
    elif icc_val >= 0.5 and icc_val < 0.75:
        icc_class = "Moderate"
    elif icc_val >= 0.75 and icc_val < 0.9:
        icc_class = "Good"
    elif icc_val >= 0.9 and icc_val < 1:
        icc_class = "Excellent"
    else:
        icc_class = np.nan

    return icc_class

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

def CompareFeatureLists(root, Norm, stages, tag):
    '''
    Given feature lists from different methods, returns the features that are in common
    '''
    fts_1 = pd.read_csv(root + "Aaron\ProstateMRL\Data\Paper1\\" + Norm + "\\Features\\" + stages[0] + "_" + tag + ".csv")
    fts_2 = pd.read_csv(root + "Aaron\ProstateMRL\Data\Paper1\\" + Norm + "\\Features\\" + stages[1] + "_" + tag + ".csv")

    fts_1 = set(fts_1["Feature"].unique())
    fts_2 = set(fts_2["Feature"].unique())

    common_fts = fts_1.intersection(fts_2)

    return common_fts

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

def ModelSummary(root, Norm, tag):
    dir = root + "Aaron\ProstateMRL\Data\Paper1\\" + Norm + "\\Features\\"
    out = open(root + "Aaron\ProstateMRL\Data\Paper1\\NormSummary\\" + Norm + "_" + tag + ".txt", "w")

    out.write("Model Summary - " + Norm)
    out.write("\n")
    out.write("-------------------------\n")
    out.write("ICC Reduction\n")
    out.write("Features Before: 105\n")

    L_ICC = pd.read_csv(dir + "Longitudinal_FeaturesRemoved_ICC_" + tag + ".csv")
    L_ICC_fts = len(L_ICC["Feature"].unique())
    out.write("Longitudinal Features Removed: " + str(L_ICC_fts) + "\n")
    out.write("\n")
    D_ICC = pd.read_csv(dir + "Delta_FeaturesRemoved_ICC_" + tag + ".csv")
    D_ICC_fts = len(D_ICC["Feature"].unique())
    out.write("Delta Features Removed: " + str(D_ICC_fts) + "\n")

    out.write("-------------------------\n")
    out.write("Volume Reduction\n")
    out.write("\n")
    out.write("Longitudinal Features Before: " + str(105 - L_ICC_fts) + "\n")
    L_Vol = pd.read_csv(dir + "Longitudinal_FeaturesRemoved_Volume_" + tag + ".csv")
    L_Vol_fts = len(L_Vol["Feature"].unique())
    out.write("Longitudinal Features Removed: " + str(L_Vol_fts)+ "\n")

    out.write("\n")
    out.write("Delta Features Before: " + str(105 - D_ICC_fts)+ "\n")
    D_Vol = pd.read_csv(dir + "Delta_FeaturesRemoved_Volume_" + tag + ".csv")
    D_Vol_fts = len(D_Vol["Feature"].unique())
    out.write("Delta Features Removed: " + str(D_Vol_fts) + "\n")

    out.write("-------------------------\n")
    L_both = CompareFeatureLists(root, Norm, ["Longitudinal_FeaturesRemoved_ICC", "Longitudinal_FeaturesRemoved_Volume"], tag)
    out.write("Longitudinal - Number of features both Vol & ICC redudant: " + str(len(L_both)) + "\n")
    D_both = CompareFeatureLists(root, Norm, ["Delta_FeaturesRemoved_ICC", "Delta_FeaturesRemoved_Volume"], tag)
    out.write("Delta - Number of features both Vol & ICC redudant: " + str(len(D_both)) + "\n")

    out.write("-------------------------\n")

    out.write("Feature Selection\n")
    out.write("\n")
    out.write("Longitudinal Features Before: " + str(105 - L_ICC_fts - L_Vol_fts) + "\n")
    L_Select = pd.read_csv(dir + "Longitudinal_SelectedFeatures_" + tag + ".csv")
    L_Select_fts = L_Select["Feature"].unique()
    out.write("Longitudinal Features Selected: " + str(len(L_Select_fts)) + "\n")

    out.write("\n")
    out.write("Delta Features Before: " + str(105 - D_ICC_fts - D_Vol_fts)+ "\n")
    D_Select = pd.read_csv(dir + "Delta_SelectedFeatures_" + tag + ".csv")
    D_Select_fts = D_Select["Feature"].unique()
    out.write("Delta Features Selected: " + str(len(D_Select_fts))+ "\n")

    # check if any features are selected in both longitudinal and delta
    out.write("\n")
    Selected_both = set(L_Select_fts).intersection(set(D_Select_fts))
    out.write("Features Selected in Both Longitudinal and Delta: " + str(len(Selected_both))+ "\n")
    out.write("-------------------------")

    out.close()

    # print out contents of file
    with open(root + "Aaron\ProstateMRL\Data\Paper1\\NormSummary\\" + Norm + "_" + tag + ".txt", "r") as f:
        print(f.read())

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


from PIL import Image, ImageFilter
import numpy as np

import torchvision.transforms as transforms

def random_blur(img, p = 0.7):
    a = np.random.uniform(size=1)
    if (a> p).all(): return img.filter(ImageFilter.GaussianBlur(radius=0.2))
    return img

data_transforms = {
    'train': transforms.Compose([
        transforms.transforms.Resize((224,224)),
        #transforms.transforms.Lambda(random_blur),
        transforms.RandomHorizontalFlip(),
        transforms.RandomRotation(45),
        transforms.RandomVerticalFlip(0.2),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]),
    'val': transforms.Compose([
        transforms.transforms.Resize((224,224)),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]),
}


from sys import argv

num_files = int(argv[1])
avg = 0
worked = True
for i in range(num_files):
    file_name = "client" + str(i + 1) + "/client" + str(i + 1) + ".out"
    f = open(file_name,'r')
    lines = f.readlines()
    count = -1
    tot = 0
    tot_time = 0
    recv_time = None
    onb_time = (60 * float(lines[0][-11:-9]) + float(lines[0][-8:]))

    lines = lines[1:-1]
    for line in lines:
        if line == "\n" or line=="":
            continue
        if  "Sending" in line :
            continue
        count = count + 1
        if count % 4 == 0:
            csec = float(line[-8:])
            cmin = float(line[-11:-9])
            recv_time = (60 * cmin + csec)
        elif count % 4 == 1:
            csec = float(line[-8:])
            cmin = float(line[-11:-9])
            sent_time = 60 * cmin + csec
            if onb_time > sent_time:
                continue
            time = recv_time - sent_time
            tot = tot + 1
            tot_time = tot_time + time
    if count!= 4*num_files-1:
        print(f'ERROR IN {file_name}')
        print(f'count is {count} instead of {6*num_files}')
        worked = False
    avg = avg + (tot_time/tot)

if worked:
    print("All data has been received")
print("Average latency = " + str(1000 * avg/num_files) + " ms")


"""This Python code calculates the factorial of a number given by the user."""

import sys

def Factorial(num):
    """This function calculates the factorial of a number."""

    mul = 1
    for i in range(0,num):
        if num == 0: #If the input which was given by the user is Zero,It returns One.
            return 1
        else:
            mul = mul*(num-i) #Else it will calculate the factorial of the number.
    return mul

def get_input():
    """This function gets an integer input from the user.If it was not an integer it gives three chances to the user to give a right input."""

    for i in range(3,0,-1):
        num = input("enter a number:")
        if num.isnumeric():  #checks if input is numeric using .isnumeric().
            num = int(num)
            return num
        else:
            print("enter integer only")
            print(f'{i-1} chances are left' if (i-1)>1 else f'{i-1} chance is left')
        continue

if __name__ == '__main__':
    """ This function is used to overcome the problem while importing this file """

    num = get_input()
    if type(num) is type(None):
        print("Try again!")
        sys.exit()
    else:
        fact=Factorial(num)
        print(f'The factorial of {num} is {fact}')


'''
Estación del año: Al ingresar un valor del 1 al 12
se le dirá al usuario a qué estación del año pertenece el valor
ingresado, correspondiendo 1 al mes de Enero y así sucesivamente.
'''
mes = int(input('Ingrese un mes del año (1-12): '))
estacion = None
if mes == 1 or mes == 2 or mes == 3:
    estacion = 'Verano'
elif mes == 4 or mes == 5 or mes == 6:
    estacion = 'Otoño'
elif mes == 7 or mes == 8 or mes == 9:
    estacion = 'Invierno'
elif mes == 10 or mes == 11 or mes == 12:
    estacion = 'Primavera'
else:
    estacion = 'Error, no hay número para ese mes'
print(f' Para el mes {mes} la estación es: {estacion}')


import snake as game
import argparse
from keras.models import Sequential
from keras.layers import Dense, Activation, Flatten, Convolution2D
from keras.optimizers import Adam
import numpy as np
import skimage as skimage
import random
import json
from random import sample as rsample
import time

INPUT_SHAPE = (80, 80, 2)  # Shape of the image imported into the NN
NB_ACTIONS = 5  # NB_ACTIONS is the number of actions the player can do
BATCH = 100
GAME_INPUT = [0, 1, 2, 3, 4]
EPSILON = 1
EPSILON_DECAY = 0.99
FINAL_EPSILON = 0.3
LEARNING_RATE = 1e-4
GAMMA = 0.7
NB_FRAMES = 1

# Building a NN model for interpretating the image from the game
# The architecture is borrowed from
# https://github.com/matthiasplappert/keras-rl/blob/master/examples/dqn_atari.py
# First wanted to try the DeepMind Atari model but
# comments recommended the one below instead
# Performance is supposed to be a lot better.

def build_model():
    model = Sequential()

    model.add(Convolution2D(16, (8, 8), strides=(4, 4), input_shape=INPUT_SHAPE))
    model.add(Activation("relu"))
    model.add(Convolution2D(32, (4, 4), strides=(2, 2)))
    model.add(Activation("relu"))
    model.add(Flatten())
    model.add(Dense(256))
    model.add(Dense(NB_ACTIONS))

    # I chosed to use a Adam optimizer, I have used it before with good results
    adam = Adam(lr=LEARNING_RATE)
    model.compile(loss="mean_squared_error", optimizer=adam)
    print(model.summary())
    return model

def experience_replay(batch_size):
    memory = []
    while True:
        experience = (
            yield rsample(memory, batch_size) if batch_size <= len(memory) else None
        )
        memory.append(experience)

def stack_image(game_image):
    # Make image black and white
    x_t = skimage.color.rgb2gray(game_image)
    # Resize the image to 80x80 pixels
    x_t = skimage.transform.resize(x_t, (80, 80))
    # Change the intensity of colors, maximizing the intensities.
    x_t = skimage.exposure.rescale_intensity(x_t, out_range=(0, 255))
    # Stacking 2 images for the agent to get understanding of speed
    s_t = np.stack((x_t, x_t), axis=2)
    # Reshape to make keras like it
    s_t = s_t.reshape(1, s_t.shape[0], s_t.shape[1], s_t.shape[2])
    return s_t

def nn_loadOld_weights(model):
    print("Now we load weight")
    model.load_weights("model.h5")
    print("Weight load successfully")
    print("Let the training begin!")
    train_network(model)

def train_network(model):
    epsilon = EPSILON
    game_state = game.Game()  # Starting up a game
    game_state.set_start_state()
    game_image, score, game_lost = game_state.run(
        0
    )  # The game is started but no action is performed
    s_t = stack_image(game_image)
    terminal = False
    t = 0
    exp_replay = experience_replay(BATCH)
    exp_replay.__next__()  # Start experience replay coroutine
    epsilon = epsilon * EPSILON_DECAY if epsilon > FINAL_EPSILON else FINAL_EPSILON
    while True:
        loss = 0
        Q_sa = 0
        action_index = 4
        r_t = 0
        a_t = "no nothing"
        if terminal:
            game_state.set_start_state()
        if t % NB_FRAMES == 0:
            if random.random() <= epsilon:
                action_index = random.randrange(NB_ACTIONS)
            else:
                action_index = np.argmax(model.predict(s_t))
            a_t = GAME_INPUT[action_index]
        # run the selected action and observed next state and reward
        x_t1_colored, r_t, terminal = game_state.run(a_t)
        s_t1 = stack_image(x_t1_colored)
        experience = (s_t, a_t, r_t, s_t1)
        batch = exp_replay.send(experience)
        s_t1 = stack_image(x_t1_colored)
        if batch:
            inputs = np.zeros((BATCH, s_t.shape[1], s_t.shape[2], s_t.shape[3]))
            targets = np.zeros((BATCH, NB_ACTIONS))
            i = 0
            for s, a, r, s_pred in batch:
                inputs[i : i + 1] = s
                if r < 0:
                    targets[i, a] = r
                else:
                    Q_sa = model.predict(s_pred)
                    targets[i, a] = r + GAMMA * np.max(Q_sa)
                i += 1
            loss += model.train_on_batch(inputs, targets)
            # Exploration vs Exploitation

        t += 1
        # save progress every 10000 iterations
        if t % 1000 == 0:
            print("Now we save model")
            model.save_weights("model.h5", overwrite=True)
            with open("model.json", "w") as outfile:
                json.dump(model.to_json(), outfile)

        if t % 500 == 0:
            print(
                "TIMESTEP",
                t,
                "/ EPSILON",
                epsilon,
                "/ ACTION",
                action_index,
                "/ REWARD",
                r_t,
                "/ Q_MAX ",
                np.max(Q_sa),
                "/ Loss ",
                loss,
            )

    print("Episode finished!")
    print("************************")

def nn_playGame(model):
    print("Now we load weight")
    model.load_weights("model.h5")
    print("Weight load successfully")
    print("Let the game begin!")
    game_state = game.Game()  # Starting up a game
    game_state.set_start_state()
    game_image, score, game_lost = game_state.run(
        4
    )  # The game is started but no action is performed
    s_t = stack_image(game_image)
    s_t1 = s_t
    a_t = 4
    while True:

        if game_lost:
            print("Game lost")
            time.sleep(2)
            print("Game is restarting")
            game_state.set_start_state()

        action_index = np.argmax(model.predict(s_t1))
        a_t = GAME_INPUT[action_index]
        x_t1_colored, _, terminal = game_state.run(a_t)
        s_t1 = stack_image(x_t1_colored)
        game_lost = terminal

def playGame(args):
    model = build_model()
    if args["mode"] == "Run":
        nn_playGame(model)
    elif args["mode"] == "Re-train":
        nn_loadOld_weights(model)
    elif args["mode"] == "Train":
        train_network(model)
    else:
        print("*** Not valid argument ***")
        print("Run argument for running game with a trained weights")
        print("Re-train argument for continue training model")
        print("Train to train train from scratch")
        print("*********************************")

def main():
    parser = argparse.ArgumentParser(
        description="How you would like your program to run"
    )
    parser.add_argument("-m", "--mode", help="Train / Run / Re-train", required=True)
    args = vars(parser.parse_args())
    playGame(args)

if __name__ == "__main__":
    main()


'''
752. Open the Lock
Medium

2458

85

Add to List

Share
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'. The wheels can rotate freely and wrap around: for example we can turn '9' to be '0', or '0' to be '9'. Each move consists of turning one wheel one slot.

The lock initially starts at '0000', a string representing the state of the 4 wheels.

You are given a list of deadends dead ends, meaning if the lock displays any of these codes, the wheels of the lock will stop turning and you will be unable to open it.

Given a target representing the value of the wheels that will unlock the lock, return the minimum total number of turns required to open the lock, or -1 if it is impossible.

Example 1:

Input: deadends = ["0201","0101","0102","1212","2002"], target = "0202"
Output: 6
Explanation:
A sequence of valid moves would be "0000" -> "1000" -> "1100" -> "1200" -> "1201" -> "1202" -> "0202".
Note that a sequence like "0000" -> "0001" -> "0002" -> "0102" -> "0202" would be invalid,
because the wheels of the lock become stuck after the display becomes the dead end "0102".
Example 2:

Input: deadends = ["8888"], target = "0009"
Output: 1
Explanation: We can turn the last wheel in reverse to move from "0000" -> "0009".
Example 3:

Input: deadends = ["8887","8889","8878","8898","8788","8988","7888","9888"], target = "8888"
Output: -1
Explanation: We cannot reach the target without getting stuck.

Constraints:

1 <= deadends.length <= 500
deadends[i].length == 4
target.length == 4
target will not be in the list deadends.
target and deadends[i] consist of digits only.

'''
class Solution:
    def openLock(self, deadends: List[str], target: str) -> int:

        ## number of vertex V= 10^4
        ## number of edges = 8*V
        ## time complexity = O(E+V)

        if target in deadends or "0000" in deadends:
            return -1
        if target == "0000":
            return 0

        ## we don't need to move '0'

        switching_index = []
        for i in range(4):
            if target[i] == "0":
                continue
            switching_index.append(i)

        def get_neighbors(ss,switching_index):
            for i in switching_index:
                for dx in [-1,1]:
                    si = (int(ss[i]) + dx) % 10
                    # print(ss[:i] + str(si) + ss[i+1:])
                    yield ss[:i] + str(si) + ss[i+1:]

        queue = collections.deque()
        queue.append(target)
        visited = set([target])
        height = 1
        init_state = "0000"
        while queue:
            d = len(queue)
            for _ in range(d):
                ss = queue.popleft()
                for nbd in get_neighbors(ss,switching_index):
                    if nbd == init_state:
                        return height
                    if nbd in visited or nbd in deadends:
                        continue

                    visited.add(nbd)
                    queue.append(nbd)
            height += 1

        return -1


from base64 import b64encode, b64decode

flag = input("Enter Flag Format (flag{....}):")
ts = int(input("How many times (10,20,30):"))
flagencode = b64encode(flag.encode())
flagencode1 = b64encode(flagencode).decode()
output = str(flagencode1)
n = 1
while n != ts:
   fe = b64encode(output.encode())
   fe2= b64encode(fe).decode()
   output = output.replace(output,str(fe2))
   n += 1
print(output)


from aiogram import Bot, Dispatcher, executor, types
import redis

bot = Bot(token='5578408826:AAEQKMhIMK4M-p91wYwmwc8V5_tMeSUvReE')
dp = Dispatcher(bot)

redis_db = redis.from_url("redis://:p3327b36ac14079b707aaabe97f366db94d64870302e88e78071de4f2476a86dc@ec2-52-30-48-197.eu-west-1.compute.amazonaws.com:23249")

@dp.message_handler(commands=['start'])
async def welcome_reply(message: types.Message):
  print(message)
  await bot.send_animation(message.from_user.id, 'https://media0.giphy.com/media/3oEjI67Egb8G9jqs3m/giphy.gif')
  chat_id = message.from_user.id
  redis_db.hset(chat_id, 'user', message.from_user.full_name)
  user = redis_db.hget(chat_id, 'user')
  print(f'redis user: {user}')
if __name__ == '__main__':

    executor.start_polling(dp)


class Solution:
    def goodDaysToRobBank(self, security: List[int], time: int) -> List[int]:
        increasing = [1 for i in security]
        decreasing = [1 for i in security]

        res = []

        for i in range(1, len(increasing)):
            increasing[i] = increasing[i-1] + 1 if security[i-1] >= security[i] else 1

        for i in range(len(decreasing) - 2, -1 , -1):
            decreasing[i] = decreasing[i+1] + 1 if security[i+1] >= security[i] else 1

        for i in range(len(security)):
            if increasing[i] > time and decreasing[i] > time:
                res.append(i)

        return res

'''

[5,3,3,3,5,6,2]
inc
 1 2 3 4 1 1 2

 dec
 1 5 4 3 2 1 1

'''

2023/Feb/21st, at 10 27 - find good days to rob the bank.py


class Solution(object):
    def findLadders(self, beginWord, endWord, wordList):
        """
        :type beginWord: str
        :type endWord: str
        :type wordList: List[str]
        :rtype: List[List[str]]
        """
        if endWord not in wordList:
            return []

        reached, unreached = set([beginWord]), set(wordList)
        if beginWord in unreached:
            unreached.remove(beginWord)
        records = {beginWord: {'parents': set([]), 'kids': set([])}}
        willadd = set([])
        while endWord not in reached:
            for word in reached:
                for i in xrange(len(word)):
                    for c in 'abcdefghijklmnopqrstuvwxyz':
                        if c != word[i]:
                            tmp = word[:i]+c+word[i+1:]
                            if tmp in unreached:
                                willadd.add(tmp)
                                records[word]['kids'].add(tmp)
                                if tmp not in records:
                                    records[tmp] = {'parents': set([word]), 'kids':set([])}
                                else:
                                    records[tmp]['parents'].add(word)
                self.clean(records, word)
                if not records:
                    return []
            unreached.difference_update(willadd)
            reached, willadd = willadd, set([])

        for word in reached:
            if word != endWord:
                self.clean(records, word)

        ans = [[beginWord]]
        while True:
            for i in xrange(len(ans)):
                kids = list(records[ans[i][-1]]['kids'])
                if not kids:
                    return ans
                ans[i].append(kids[0])
                for kid in kids[1:]:
                    ans.append(ans[i][:-1]+[kid])

    def clean(self, records, word):
        if word in records and not records[word]['kids']:
            parents = records[word]['parents']
            del records[word]
            for p in parents:
                records[p]['kids'].remove(word)
                self.clean(records, p)

if __name__ == "__main__":
    from leetcodelib import test, update_testfile, run_testfile
    arguments = [("hit", "cog", ["hot","dot","dog","lot","log","cog"]),
                 ("a", "c", ["a", "b", "c"]),
                 ("hot", "dog", ["hot", "dog"]),
                 ("hot", "dog", ["hot","cog","dog","tot","hog","hop","pot","dot"])
                 ]
    answers = [
               [["hit","hot","dot","dog","cog"],["hit","hot","lot","log","cog"]],
               [["a", "c"]],
               [],
               [["hot","dot","dog"],["hot","hog","dog"]]
               ]
    test(Solution().findLadders, arguments, answers, inds=None, mode='1D')

    #testfile = __file__.replace('.py', '.yaml')
    #arg_names = ""
    #update_testfile(testfile, arg_names, arguments, answers)
    #run_testfile(testfile, Solution().)


import random
from faker import Faker
import time
from faker_vehicle import VehicleProvider
from app import db
from app import ScooterAllinfo, FONLoc, ReservedScooterStatus, ScooterQueryRecord, ScooterOrderRecord
from app import CarAllinfo, RealtimeCarDetails, RealtimeCarOrderRecord, ReservedCarStatus, CarQueryRecord, CarOrderRecord

from sqlalchemy import and_, func

fake = Faker()
fake.add_provider(VehicleProvider)

def fake_longnla():
    # for i in range(cnt):
    loc1 = FONLoc(
        loc=1,
        longnla=str("(25.016695, 121.543692)")
    )
    db.session.add(loc1)
    loc2 = FONLoc(
        loc=2,
        longnla=str("(25.021295, 121.539112)")
    )
    db.session.add(loc2)
    loc3 = FONLoc(
        loc=3,
        longnla=str("(25.019808, 121.541526)")
    )
    db.session.add(loc3)
    db.session.commit()

def fake_scooter(cnt=10):

    for i in range(cnt):
        Scooter = ScooterAllinfo(bmid=10021 + random.randint(1, 2),
                                 sub_type='Escooter',
                                 brand_id='Gogoro',
                                 model_id='VIVA',
                                 price=random.randint(1, 3),
                                 location=random.randint(1, 3),
                                 UsedorNot=False)
        db.session.add(Scooter)
    db.session.commit()

def fake_scooter_status():
    time_slot = 744  # a month
    data_created = int(time.time())
    loc_list = []
    bm_list = []
    all_list = db.session.query(ScooterAllinfo).all()
    cnt_bm = db.session.query(ScooterAllinfo).group_by(
        ScooterAllinfo.bmid).count()  # having(func.count('*')
    bm_list_v = db.session.query(
        ScooterAllinfo.bmid).group_by(ScooterAllinfo.bmid).all()
    cnt_loc = db.session.query(ScooterAllinfo).group_by(
        ScooterAllinfo.location).count()
    loc_list_v = db.session.query(ScooterAllinfo).group_by(
        ScooterAllinfo.location).all()
    for i in loc_list_v:
        loc_list.append(i.location)
    for i in bm_list_v:
        bm_list.append(i.bmid)

    for i in range(data_created, data_created + 3600*time_slot, 3600):
        for j in loc_list:
            for k in bm_list:
                csta1 = ReservedScooterStatus(
                    date=i,
                    location=j,
                    # db.session.query(ScooterAllinfo.location).order_by(func.random()).limit(1),
                    bmid=k,
                    # db.session.query(ScooterAllinfo.bmid).\
                    # filter(ScooterAllinfo.location == '1').order_by(
                    #     func.random()).limit(1),
                    ScooterAmount=db.session.query(ScooterAllinfo.bmid).\
                    filter(and_(ScooterAllinfo.bmid == k, ScooterAllinfo.location == j)).count())
                db.session.add(csta1)

    db.session.commit()

def fake_car(cnt=10):

    for i in range(cnt):
        car = CarAllinfo(bmid=1000021 + random.randint(1, 2),
                         sub_type=fake.vehicle_category(),
                         brand_id=fake.vehicle_make(),
                         model_id=fake.vehicle_model(),
                         price=random.randint(2, 7),
                         location=random.randint(1, 3),
                         UsedorNot=False)
        db.session.add(car)
    db.session.commit()

def fake_car_status():
    time_slot = 744  # a month
    data_created = int(time.time())
    loc_list = []
    bm_list = []
    all_list = db.session.query(CarAllinfo).all()
    cnt_bm = db.session.query(CarAllinfo).group_by(
        CarAllinfo.bmid).count()  # having(func.count('*')
    bm_list_v = db.session.query(
        CarAllinfo.bmid).group_by(CarAllinfo.bmid).all()
    cnt_loc = db.session.query(CarAllinfo).group_by(
        CarAllinfo.location).count()
    loc_list_v = db.session.query(CarAllinfo).group_by(
        CarAllinfo.location).all()
    for i in loc_list_v:
        loc_list.append(i.location)
    for i in bm_list_v:
        bm_list.append(i.bmid)

    for i in range(data_created, data_created + 3600*time_slot, 3600):
        for j in loc_list:
            for k in bm_list:
                csta1 = ReservedCarStatus(
                    date=i,
                    location=j,
                    # db.session.query(CarAllinfo.location).order_by(func.random()).limit(1),
                    bmid=k,
                    # db.session.query(CarAllinfo.bmid).\
                    # filter(CarAllinfo.location == '1').order_by(
                    #     func.random()).limit(1),
                    carAmount=db.session.query(CarAllinfo.bmid).\
                    filter(and_(CarAllinfo.bmid == k, CarAllinfo.location == j)).count())
                db.session.add(csta1)

    db.session.commit()


import pytest

from httmock import urlmatch, HTTMock

from util.config import URLSchemeAndHostname
from util.config.validator import ValidatorContext
from util.config.validators import ConfigValidationException
from util.config.validators.validate_bitbucket_trigger import BitbucketTriggerValidator

from test.fixtures import *

@pytest.mark.parametrize(
    "unvalidated_config",
    [
        (ValidatorContext({})),
        (ValidatorContext({"BITBUCKET_TRIGGER_CONFIG": {}})),
        (ValidatorContext({"BITBUCKET_TRIGGER_CONFIG": {"CONSUMER_KEY": "foo"}})),
        (ValidatorContext({"BITBUCKET_TRIGGER_CONFIG": {"CONSUMER_SECRET": "foo"}})),
    ],
)
def test_validate_invalid_bitbucket_trigger_config(unvalidated_config, app):
    validator = BitbucketTriggerValidator()

    with pytest.raises(ConfigValidationException):
        validator.validate(unvalidated_config)

def test_validate_bitbucket_trigger(app):
    url_hit = [False]

    @urlmatch(netloc=r"bitbucket.org")
    def handler(url, request):
        url_hit[0] = True
        return {
            "status_code": 200,
            "content": "oauth_token=foo&oauth_token_secret=bar",
        }

    with HTTMock(handler):
        validator = BitbucketTriggerValidator()

        url_scheme_and_hostname = URLSchemeAndHostname("http", "localhost:5000")
        unvalidated_config = ValidatorContext(
            {"BITBUCKET_TRIGGER_CONFIG": {"CONSUMER_KEY": "foo", "CONSUMER_SECRET": "bar",},},
            url_scheme_and_hostname=url_scheme_and_hostname,
        )

        validator.validate(unvalidated_config)

        assert url_hit[0]


import logging

from sugar_network import db
from sugar_network.model.user import User
from sugar_network.model.post import Post
from sugar_network.model.report import Report
from sugar_network.model.context import Context as _Context
from sugar_network.toolkit.router import ACL, File
from sugar_network.toolkit.coroutine import this

_logger = logging.getLogger('client.model')

class Context(_Context):

    @db.indexed_property(db.List, prefix='P', default=[],
            acl=ACL.READ | ACL.LOCAL)
    def pins(self, value):
        return value + this.injector.pins(self.guid)

class Volume(db.Volume):

    def __init__(self, root, resources=None):
        if resources is None:
            resources = [User, Context, Post, Report]
        db.Volume.__init__(self, root, resources)
        for directory in self.values():
            directory.metadata['author'].acl |= ACL.LOCAL

def dump_volume(volume):
    for resource, directory in volume.items():
        if not directory.has_seqno:
            continue

        for doc in directory:
            if not doc['seqno'] or doc['state'] != 'active':
                continue

            dump = {}
            op = dump['op'] = {}
            props = dump['content'] = {}
            keys = []
            postfix = []

            for name, prop in doc.metadata.items():
                meta = doc.meta(name)
                if meta is None or 'seqno' not in meta:
                    continue
                if isinstance(prop, db.Aggregated):
                    for agg in doc.repr(name):
                        aggop = {
                            'method': 'POST',
                            'path': [resource, doc.guid, name, agg['key']],
                            }
                        aggvalue = agg['value']
                        if isinstance(aggvalue, File):
                            aggvalue.meta['op'] = aggop
                            postfix.append(aggvalue)
                        else:
                            postfix.append({'op': aggop, 'content': aggvalue})
                elif prop.acl & (ACL.WRITE | ACL.CREATE):
                    if isinstance(prop, db.Blob):
                        blob = volume.blobs.get(doc[name])
                        if blob is not None:
                            blob.meta['op'] = {
                                'method': 'PUT',
                                'path': [resource, doc.guid, name],
                                }
                            postfix.append(blob)
                    else:
                        if isinstance(prop, db.Reference):
                            keys.append(name)
                        props[name] = doc[name]

            if 'seqno' in doc.meta('guid'):
                keys.append('guid')
                props['guid'] = doc.guid
                op['method'] = 'POST'
                op['path'] = [resource]
            else:
                op['method'] = 'PUT'
                op['path'] = [resource, doc.guid]

            if keys:
                dump['keys'] = keys

            yield dump
            for dump in postfix:
                yield dump


from socket import *
import os

def parsing(host):
    # 운영체제가 윈도우일 경우
    if os.name == 'nt':
        socket_protocol = IPPROTO_IP
    else:
        socket_protocol = IPPROTO_ICMP
    sock = socket(AF_INET, SOCK_RAW, socket_protocol)
    sock.bind((host, 0))

    sock.setsockopt(IPPROTO_IP, IP_HDRINCL, 1) # IP_HDRINCL: IP 헤더 포함 옵션, 1: True

    # 윈도우의 Promiscuous Mode 활성화
    if os.name == 'nt':
        sock.ioctl(SIO_RCVALL, RCVALL_ON)

    data = sock.recvfrom(65535)
    print(data[0])

    # 윈도우의 Promiscuous Mode 종료
    if os.name == 'nt':
        sock.ioctl(SIO_RCVALL, RCVALL_OFF)

    sock.close()

if __name__ == "__main__":
    host = "#" # 본인 IP
    print(f"Listening at [{host}]")
    parsing(host)


"""
This example script demonstrates some simple usage of gt_pg package.
"""

import gt_pg

def main():
    # prepare data for simple graph:
    # prepare names of properties of certain types
    vertex_properties = {
        "uri": "string",
        "label": "string",
    }
    edge_properties = {
        "uri": "string",
        "label": "string",
    }

    # prepare values for uris, the keys for vertices and edges
    v_id_prop = "uri"
    e_id_prop = "uri"
    v_uris = ["http://example.com/1", "http://example.com/2", "http://example.com/3"]
    e_uris = ["http://example.com/relA", "http://example.com/relB"]

    # initialize the graph
    g = gt_pg.Graph()
    g.init_graph(
        v_props=vertex_properties,
        e_props=edge_properties,
    )
    g.set_vertex_id_prop(v_id_prop)
    g.set_edge_id_prop(e_id_prop)

    # specify properties for vertices, vertices will be created
    g.append_vprop_val(v_uris[0], "label", "1")
    g.append_vprop_val(v_uris[2], "label", "3")

    # create relation between vertices, non-existing vertices will be created
    g.create_relation(v_uris[0], v_uris[1], e_uris[0])
    g.create_relation(v_uris[2], v_uris[1], e_uris[1])

    # specify property for edge
    g.append_eprop_val(v_uris[2], v_uris[1], "label", "relA")

    # save graph
    g.save('quickstart.graphml')

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
import os
import time
import json
import gevent
import datetime
import traceback
import const as ct
import pandas as pd
from pathlib import Path
from cstock import CStock
from gevent.pool import Pool
from functools import partial
from datetime import datetime
from rstock import RIndexStock
from base.clog import getLogger
from ccalendar import CCalendar
from index_info import IndexInfo
from cstock_info import CStockInfo
from combination import Combination
from cindex import CIndex, TdxFgIndex
from industry_info import IndustryInfo
from datamanager.margin  import Margin
from datamanager.emotion import Emotion
from algotrade.model.follow_trend import FollowTrendModel
from datamanager.hgt import StockConnect
from rindustry import RIndexIndustryInfo
from cpython.cval import CValuation
from futu.common.constant import SubType
from combination_info import CombinationInfo
from datamanager.sexchange import StockExchange
from base.cdate import transfer_date_string_to_int
from datamanager.bull_stock_ratio import BullStockRatio
from base.cdate import get_day_nday_ago, get_dates_array
from algotrade.broker.futu.subscriber import Subscriber, StockQuoteHandler, TickerHandler
from common import is_trading_time, add_prifix, add_index_prefix, kill_process, concurrent_run, process_concurrent_run, get_latest_data_date
pd.options.mode.chained_assignment = None #default='warn'
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
class DataManager:
    def __init__(self, dbinfo = ct.DB_INFO, redis_host = None):
        self.dbinfo = dbinfo
        self.logger = getLogger(__name__)
        self.index_objs = dict()
        self.stock_objs = dict()
        self.updating_date = None
        self.combination_objs = dict()
        self.cal_client = CCalendar(dbinfo = dbinfo, redis_host = redis_host)
        self.index_info_client = IndexInfo()
        self.cvaluation_client = CValuation()
        self.comb_info_client = CombinationInfo(dbinfo, redis_host)
        self.stock_info_client = CStockInfo(dbinfo, redis_host)
        self.rindex_stock_data_client = RIndexStock(dbinfo, redis_host)
        self.industry_info_client = IndustryInfo("TDX", dbinfo, redis_host)
        self.sw_industry_info_client = IndustryInfo("SW", dbinfo, redis_host)
        self.rindustry_info_client = RIndexIndustryInfo(dbinfo, redis_host)
        self.subscriber = Subscriber()
        self.quote_handler  = StockQuoteHandler()
        self.ticker_handler = TickerHandler()
        self.connect_client = StockConnect(market_from = ct.SH_MARKET_SYMBOL, market_to = ct.HK_MARKET_SYMBOL, dbinfo = dbinfo, redis_host = redis_host)
        self.margin_client = Margin(dbinfo = dbinfo, redis_host = redis_host)
        self.emotion_client = Emotion(dbinfo = dbinfo, redis_host = redis_host)
        self.sh_exchange_client = StockExchange(ct.SH_MARKET_SYMBOL)
        self.sz_exchange_client = StockExchange(ct.SZ_MARKET_SYMBOL)

    def is_collecting_time(self):
        now_time = datetime.now()
        _date = now_time.strftime('%Y-%m-%d')
        y,m,d = time.strptime(_date, "%Y-%m-%d")[0:3]
        aft_open_hour,aft_open_minute,aft_open_second = (17,30,00)
        aft_open_time = datetime(y,m,d,aft_open_hour,aft_open_minute,aft_open_second)
        aft_close_hour,aft_close_minute,aft_close_second = (23,59,59)
        aft_close_time = datetime(y,m,d,aft_close_hour,aft_close_minute,aft_close_second)
        #self.logger.info("collecting now time. open_time:%s < now_time:%s < close_time:%s" % (aft_open_time, now_time, aft_close_time))
        return aft_open_time < now_time < aft_close_time

    def is_morning_time(self, now_time = datetime.now()):
        _date = now_time.strftime('%Y-%m-%d')
        y,m,d = time.strptime(_date, "%Y-%m-%d")[0:3]
        mor_open_hour,mor_open_minute,mor_open_second = (0,0,0)
        mor_open_time = datetime(y,m,d,mor_open_hour,mor_open_minute,mor_open_second)
        mor_close_hour,mor_close_minute,mor_close_second = (6,30,0)
        mor_close_time = datetime(y,m,d,mor_close_hour,mor_close_minute,mor_close_second)
        return mor_open_time < now_time < mor_close_time

    def collect_combination_runtime_data(self):
        def _combination_run(code_id):
            self.combination_objs[code_id].run()
            return (code_id, True)
        todo_iplist = list(self.combination_objs.keys())
        return concurrent_run(_combination_run, todo_iplist, num = 10)

    def collect_stock_runtime_data(self):
        if self.ticker_handler.empty(): return
        datas = self.ticker_handler.getQueue()
        while not datas.empty():
            df = datas.get()
            df = df.set_index('time')
            df.index = pd.to_datetime(df.index)
            for code_str in set(df.code):
                code_id = code_str.split('.')[1]
                self.stock_objs[code_id].run(df.loc[df.code == code_str])

    def init_real_stock_info(self):
        concerned_list = self.comb_info_client.get_concerned_list()
        prefix_concerned_list = [add_prifix(code) for code in concerned_list]
        ret = self.subscriber.subscribe(prefix_concerned_list, SubType.TICKER, self.ticker_handler)
        if 0 == ret:
            for code in concerned_list:
                if code not in self.stock_objs:
                    self.stock_objs[code] = CStock(code, self.dbinfo, should_create_influxdb = True, should_create_mysqldb = True)
        return ret

    def init_index_info(self):
        index_list = ct.INDEX_DICT.keys()
        prefix_index_list = [add_index_prefix(code) for code in index_list]
        ret = self.subscriber.subscribe(prefix_index_list, SubType.QUOTE, self.quote_handler)
        if 0 != ret:
            self.logger.error("subscribe for index list failed")
            return ret
        for code in index_list:
            if code not in self.index_objs:
                self.index_objs[code] = CIndex(code, should_create_influxdb = True, should_create_mysqldb = True)
        return 0

    def collect_index_runtime_data(self):
        if self.quote_handler.empty(): return
        datas = self.quote_handler.getQueue()
        while not datas.empty():
            df = datas.get()
            df['time'] = df.data_date + ' ' + df.data_time
            df = df.drop(['data_date', 'data_time'], axis = 1)
            df = df.set_index('time')
            df.index = pd.to_datetime(df.index)
            for code_str in set(df.code):
                code_id = code_str.split('.')[1]
                self.index_objs[code_id].run(df.loc[df.code == code_str])

    def run(self, sleep_time):
        while True:
            try:
                self.logger.debug("enter run")
                if self.cal_client.is_trading_day():
                    if is_trading_time():
                        t_sleep_time = 1
                        if not self.subscriber.status():
                            self.subscriber.start()
                            if 0 == self.init_index_info() and 0 == self.init_real_stock_info():
                                self.init_combination_info()
                            else:
                                self.logger.debug("enter stop subscriber")
                                self.subscriber.stop()
                        else:
                            self.collect_stock_runtime_data()
                            self.collect_combination_runtime_data()
                            self.collect_index_runtime_data()
                    else:
                        t_sleep_time = sleep_time
                        if self.subscriber.status():
                            self.subscriber.stop()
                else:
                    t_sleep_time = sleep_time
            except Exception as e:
                #traceback.print_exc()
                self.logger.error(e)
            gevent.sleep(t_sleep_time)

    def set_update_info(self, step_length, exec_date, cdate = None, filename = ct.STEPFILE):
        step_info = dict()
        if cdate is None: cdate = 'none'
        step_info[cdate] = dict()
        step_info[cdate]['step'] = step_length
        step_info[cdate]['date'] = exec_date
        with open(filename, 'w') as f:
            json.dump(step_info, f)
        self.logger.info("finish step :%s" %  step_length)

    def get_update_info(self, cdate = None, exec_date = None, filename = ct.STEPFILE):
        if cdate is None: cdate = 'none'
        if not os.path.exists(filename): return (0, exec_date)
        with open(filename, 'r') as f: step_info = json.load(f)
        if cdate not in step_info: return (0, exec_date)
        return (step_info[cdate]['step'], step_info[cdate]['date'])

    def bootstrap(self, cdate = None, exec_date = datetime.now().strftime('%Y-%m-%d'), ndays = 10):
        finished_step, exec_date = self.get_update_info(cdate, exec_date)
        self.logger.info("enter updating.%s" % finished_step)
        if finished_step < 1:
            if not self.cal_client.init():
                self.logger.error("cal client init failed")
                return False
            self.set_update_info(1, exec_date, cdate)

        if finished_step < 2:
            if not self.init_stock_meta():
                self.logger.error("stock info init failed")
                return False
            self.set_update_info(2, exec_date, cdate)

        if finished_step < 3:
            self.cvaluation_client = CValuation(needUpdate = True)
            self.set_update_info(3, exec_date, cdate)

        if finished_step < 4:
            if not self.index_info_client.update():
                self.logger.error("index info init failed")
                return False
            self.set_update_info(4, exec_date, cdate)

        if finished_step < 5:
            if not self.comb_info_client.update():
                self.logger.error("comb info init failed")
                return False
            self.set_update_info(5, exec_date, cdate)

        if finished_step < 6:
            if not self.industry_info_client.update() or not self.sw_industry_info_client.update():
                self.logger.error("industry info init failed")
                return False
            self.set_update_info(6, exec_date, cdate)

        if finished_step < 7:
            if not self.init_tdx_index_info(cdate):
                self.logger.error("init tdx index info failed")
                return False
            self.set_update_info(7, exec_date, cdate)

        if finished_step < 8:
            if not self.sh_exchange_client.update(exec_date, num = ndays):
                self.logger.error("sh exchange update failed")
                return False
            self.set_update_info(8, exec_date, cdate)

        if finished_step < 9:
            if not self.sz_exchange_client.update(exec_date, num = ndays):
                self.logger.error("sz exchange update failed")
                return False
            self.set_update_info(9, exec_date, cdate)

        if finished_step < 10:
            if not self.init_index_components_info(exec_date):
                self.logger.error("init index components info failed")
                return False
            self.set_update_info(10, exec_date, cdate)

        if finished_step < 11:
            if not self.init_industry_info(cdate):
                self.logger.error("init industry info failed")
                return False
            self.set_update_info(11, exec_date, cdate)

        if finished_step < 12:
            if not self.rindustry_info_client.update(exec_date, num = ndays):
                self.logger.error("init %s rindustry info failed" % exec_date)
                return False
            self.set_update_info(12, exec_date, cdate)

        if finished_step < 13:
            if not self.init_stock_info(cdate):
                self.logger.error("init stock info set failed")
                return False
            self.set_update_info(13, exec_date, cdate)

        if finished_step < 14:
            if not self.init_base_float_profit():
                self.logger.error("init base float profit for all stock")
                return False
            self.set_update_info(14, exec_date, cdate)

        if finished_step < 15:
            if not self.init_valuation_info(cdate):
                self.logger.error("init stock valuation info failed")
                return False
            self.set_update_info(15, exec_date, cdate)

        if finished_step < 16:
            if not self.init_rvaluation_info(cdate):
                self.logger.error("init r stock valuation info failed")
                return False
            self.set_update_info(16, exec_date, cdate)

        if finished_step < 17:
            if not self.init_rindex_valuation_info(cdate):
                self.logger.error("init r index valuation info failed")
                return False
            self.set_update_info(17, exec_date, cdate)

        if finished_step < 18:
            if not self.rindex_stock_data_client.update(exec_date, num = ndays):
                self.logger.error("rstock data set failed")
                return False
            self.set_update_info(18, exec_date, cdate)

        if finished_step < 19:
            if not self.set_bull_stock_ratio(exec_date, num = ndays):
                self.logger.error("bull ratio set failed")
                return False
            self.set_update_info(19, exec_date, cdate)

        if finished_step < 20:
            if not self.init_yesterday_hk_info(exec_date, num = ndays):
                self.logger.error("init yesterday hk info failed")
                return False
            self.set_update_info(20, exec_date, cdate)

        if finished_step < 21:
            if not self.set_stock_pools(cdate):
                self.logger.error("choose stocks for model")
                return False
            self.set_update_info(21, exec_date, cdate)

        self.logger.info("updating succeed")
        return True

    def clear_network_env(self):
        kill_process("google-chrome")
        kill_process("renderer")
        kill_process("Xvfb")
        kill_process("zygote")
        kill_process("defunct")
        kill_process("show-component-extension-options")

    def update(self, sleep_time):
        succeed = False
        while True:
            self.logger.debug("enter daily update process. %s" % datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
            try:
                if self.cal_client.is_trading_day():
                    #self.logger.info("is trading day. %s, succeed:%s" % (datetime.now().strftime('%Y-%m-%d %H:%M:%S'), succeed))
                    if self.is_collecting_time():
                        self.logger.debug("enter collecting time. %s, succeed:%s" % (datetime.now().strftime('%Y-%m-%d %H:%M:%S'), succeed))
                        if not succeed:
                            self.clear_network_env()
                            mdate = datetime.now().strftime('%Y-%m-%d')
                            ndate = get_latest_data_date()
                            if ndate is not None:
                                if ndate >= transfer_date_string_to_int(mdate):
                                    if self.updating_date is None: self.updating_date = mdate
                                    succeed = self.bootstrap(cdate = self.updating_date, exec_date = self.updating_date)
                                    if succeed: self.updating_date = None
                                else:
                                    self.logger.debug("%s is older for %s" % (ndate, mdate))
                    else:
                        succeed = False
                gevent.sleep(sleep_time)
            except Exception as e:
                time.sleep(1)
                self.logger.error(e)
                #traceback.print_exc()

    def init_combination_info(self):
        trading_info = self.comb_info_client.get()
        for _, code_id in trading_info['code'].iteritems():
            if str(code_id) not in self.combination_objs:
                self.combination_objs[str(code_id)] = Combination(code_id, self.dbinfo)

    def init_base_float_profit(self):
        def _set_base_float_profit(code_id):
            if CStock(code_id).set_base_floating_profit():
                self.logger.info("%s set base float profit success" % code_id)
                return (code_id, True)
            else:
                self.logger.error("%s set base float profit failed" % code_id)
                return (code_id, False)
        df = self.stock_info_client.get()
        if df.empty: return False
        failed_list = df.code.tolist()
        return process_concurrent_run(_set_base_float_profit, failed_list, num = 8)

    def init_rindex_valuation_info(self, cdate):
        for code in ct.INDEX_DICT:
            if not self.cvaluation_client.set_index_valuation(code, cdate):
                self.logger.error("{} set {} data for rvaluation failed".format(code, mdate))
                return False
        return True

    def init_rvaluation_info(self, cdate = None):
        def cget(mdate, code):
            return code, CStock(code).get_val_data(mdate)
        df = self.stock_info_client.get()
        code_list = df.code.tolist()
        try:
            obj_pool = Pool(5000)
            all_df = pd.DataFrame()
            cfunc = partial(cget, cdate)
            for code_data in obj_pool.imap_unordered(cfunc, code_list):
                if code_data[1] is not None and not code_data[1].empty:
                    tem_df = code_data[1]
                    tem_df['code'] = code_data[0]
                    all_df = all_df.append(tem_df)
            obj_pool.join(timeout = 5)
            obj_pool.kill()
            all_df = all_df.reset_index(drop = True)
            file_name = "{}.csv".format(cdate)
            file_path = Path(ct.RVALUATION_DIR) / file_name
            all_df.to_csv(file_path, index=False, header=True, mode='w', encoding='utf8')
            return True
        except Exception as e:
            self.logger.error(e)
            return False

    def init_valuation_info(self, cdate = None):
        df = self.stock_info_client.get()
        code_list = df['code'].tolist()
        time2market_list = df['timeToMarket'].tolist()
        code2timedict = dict(zip(code_list, time2market_list))
        cfun = partial(self.cvaluation_client.set_stock_valuation, code2timedict, cdate)
        return process_concurrent_run(cfun, code_list, num = 15, black_list = list())

    def init_stock_info(self, cdate = None):
        def _set_stock_info(mdate, bonus_info, index_info, stock_info, code_id):
            try:
                if CStock(code_id).set_k_data(bonus_info, index_info, stock_info, mdate):
                    self.logger.info("%s set k data success for date:%s", code_id, mdate)
                    return (code_id, True)
                else:
                    self.logger.error("%s set k data failed for date:%s", code_id, mdate)
                    return (code_id, False)
            except Exception as e:
                self.logger.error("%s set k data for date %s exception:%s", code_id, mdate, e)
                return (code_id, False)

        #get stock bonus info
        bonus_info = pd.read_csv("/data/tdx/base/bonus.csv", sep = ',',
                      dtype = {'code' : str, 'market': int, 'type': int, 'money': float,
                            'price': float, 'count': float, 'rate': float, 'date': int})

        index_info = CIndex('000001').get_k_data()
        if index_info is None or index_info.empty: return False
        stock_info = self.stock_info_client.get()
        if stock_info.empty: return False
        failed_list = stock_info.code.tolist()
        if cdate is None:
            cfunc = partial(_set_stock_info, cdate, bonus_info, index_info, stock_info)
            return process_concurrent_run(cfunc, failed_list, num = 6)
        else:
            cfunc = partial(_set_stock_info, cdate, bonus_info, index_info, stock_info)
            succeed = True
            if not process_concurrent_run(cfunc, failed_list, num = 6):
                succeed = False
            return succeed
            #start_date = get_day_nday_ago(cdate, num = 4, dformat = "%Y-%m-%d")
            #for mdate in get_dates_array(start_date, cdate, asending = True):
            #    if self.cal_client.is_trading_day(mdate):
            #        self.logger.info("start recording stock info: %s", mdate)
            #        cfunc = partial(_set_stock_info, mdate, bonus_info, index_info)
            #        if not process_concurrent_run(cfunc, failed_list, num = 500):
            #            self.logger.error("compute stock info for %s failed", mdate)
            #            return False
            #return True

    def init_industry_info(self, cdate, num = 1):
        def _set_industry_info(cdate, code_id):
            return (code_id, CIndex(code_id).set_k_data(cdate))
        df = self.industry_info_client.get_data()
        if cdate is None:
            cfunc = partial(_set_industry_info, cdate)
            return concurrent_run(cfunc, df.code.tolist(), num = 5)
        else:
            succeed = True
            start_date = get_day_nday_ago(cdate, num = num, dformat = "%Y-%m-%d")
            for mdate in get_dates_array(start_date, cdate, asending = True):
                if self.cal_client.is_trading_day(mdate):
                    cfunc = partial(_set_industry_info, mdate)
                    if not concurrent_run(cfunc, df.code.tolist(), num = 5):
                        succeed = False
            return succeed

    def init_yesterday_hk_info(self, cdate, num):
        succeed = True
        for data in ((ct.SH_MARKET_SYMBOL, ct.HK_MARKET_SYMBOL), (ct.SZ_MARKET_SYMBOL, ct.HK_MARKET_SYMBOL)):
            if not self.connect_client.set_market(data[0], data[1]):
                self.logger.error("connect_client for %s failed" % data)
                succeed = False
                continue
            if not self.connect_client.update(cdate, num = num):
                succeed = False
            self.connect_client.close()
            self.connect_client.quit()
        return succeed

    def get_concerned_index_codes(self):
        index_codes = list(ct.INDEX_DICT.keys())
        #添加MSCI板块
        index_codes.append('880883')
        return index_codes

    def init_index_components_info(self, cdate = None):
        if cdate is None: cdate = datetime.now().strftime('%Y-%m-%d')
        def _set_index_info(code_id):
            if code_id in self.index_objs:
                _obj = self.index_objs[code_id]
            else:
                _obj = CIndex(code_id) if code_id in list(ct.INDEX_DICT.keys()) else TdxFgIndex(code_id)
            return (code_id, _obj.set_components_data(cdate))
        index_codes = self.get_concerned_index_codes()
        return concurrent_run(_set_index_info, index_codes, num = 10)

    def set_stock_pools(self, mdate = None):
        if mdate is None: mdate = datetime.now().strftime('%Y-%m-%d')
        model = FollowTrendModel(should_create_mysqldb = True)
        return model.set_stock_pool(mdate)

    def set_bull_stock_ratio(self, cdate, num = 10):
        def _set_bull_stock_ratio(code_id):
            obj = BullStockRatio(code_id)
            #obj.mysql_client.delete(obj.get_table_name())
            return (code_id, obj.update(cdate, num))
        index_codes = self.get_concerned_index_codes()
        return concurrent_run(_set_bull_stock_ratio, index_codes)

    def init_stock_meta(self, num = 10):
        def create_stock_obj(code):
            try:
                CStock(code, should_create_influxdb = True, should_create_mysqldb = True)
                return (code, True)
            except Exception as e:
                self.logger.error(e)
                return (code, False)
        if self.stock_info_client.init():
            df = self.stock_info_client.get()
            return concurrent_run(create_stock_obj, df.code.tolist(), num = num)
        return False

    def init_tdx_index_info(self, cdate = None, num = 1):
        def _set_index_info(cdate, code_id):
            try:
                if code_id in self.index_objs:
                    _obj = self.index_objs[code_id]
                else:
                    _obj = CIndex(code_id) if code_id in list(ct.TDX_INDEX_DICT.keys()) else TdxFgIndex(code_id)
                return (code_id, _obj.set_k_data(cdate))
            except Exception as e:
                self.logger.error(e)
                return (code_id, False)
        #index_code_list = self.get_concerned_index_codes()
        index_code_list = list(ct.TDX_INDEX_DICT.keys())
        if cdate is None:
            cfunc = partial(_set_index_info, cdate)
            return concurrent_run(cfunc, index_code_list, num = 5)
        else:
            succeed = True
            start_date = get_day_nday_ago(cdate, num = num, dformat = "%Y-%m-%d")
            for mdate in get_dates_array(start_date, cdate, asending = True):
                if self.cal_client.is_trading_day(mdate):
                    cfunc = partial(_set_index_info, mdate)
                    if not concurrent_run(cfunc, index_code_list, num = 5):
                        succeed = False
            return succeed

if __name__ == '__main__':
    #from cmysql import CMySQL
    #mysql_client = CMySQL(dbinfo = ct.DB_INFO)
    #import sys
    #sys.exit(0)
    #for code in IndustryInfo().get().code.tolist():
    #    print(code)
    #    mysql_client.delete_db('i%s' % code)
    #for code in CStockInfo().get().code.tolist():
    #    print(code)
    #    mysql_client.delete_db('s%s' % code)
    #mdate = datetime.now().strftime('%Y-%m-%d')
    dm = DataManager()
    mdate = '2021-04-28'
    #dm.logger.info("start compute!")
    #dm.init_rindex_valuation_info(mdate)
    #dm.init_rvaluation_info(mdate)
    #dm.init_valuation_info(mdate)
    #dm.set_bull_stock_ratio(mdate, num = 10)
    #dm.clear_network_env()
    #dm.init_base_float_profit()
    #dm.init_stock_info(cdate = mdate)
    #dm.init_stock_info()
    #dm.init_base_float_profit()
    dm.bootstrap(cdate = mdate, exec_date = mdate)
    #dm.set_stock_pools(mdate = '2021-03-02')
    #dm.init_yesterday_hk_info('2019-09-21', num = 10)
    #dm.set_stock_pools(mdate = '2019-10-08')
    #dm.logger.info("end compute!")
    #CValuation(needUpdate = True)


jogadores = []
jogador = {}
cod = 0
while True:
    gols = []
    total_de_gols = []
    jogador['Código'] = cod
    jogador['Nome'] = input('Nome do jogador: ').strip().capitalize()
    partidas = int(input(f'Quantas partidas {jogador["Nome"]} jogou? '))
    jogador['Partidas'] = partidas
    for i in range(1, partidas + 1):
        gol = int(input(f'      Quantos gols na partida {i}? '))
        gols.append(gol)
    jogador['Gols'] = gols
    jogador['Total'] = sum(gols) # Total de gols
    jogadores.append(jogador.copy())
    cod += 1
    cont = input('Quer continuar? [S/N]: ').upper().strip()
    if cont == 'N':
        break
string = ''
print('=-=' * 33)
print('{:<30}{:<30}{:<30}{:<30}'.format('Código', 'Nome', 'Gols', 'Total'))
for jog in jogadores:
    string += '{:<30}{:<30}{:<30}{:<30}\n'.format(str(jog['Código']), str(jog['Nome']), str(jog['Gols']), str(jog['Total']))
print(string)
print('=-=' * 33)
while True:
    mostrar = int(input('Mostrar dados de qual jogador? (Digite o código) 999 para sair: '))
    print('=-=' * 12)
    for jogador in jogadores:
        if jogador["Código"] == mostrar:
            print(f'--LEVANTAMENTO DO JOGADOR {jogador["Nome"]}:')
            for g in range(len(jogador['Gols'])):
                print(f'No jogo {g + 1} fez {jogador["Gols"][g]} gols.')
            break
    else:
        print(f'Não existe jogador com o código {mostrar}.')
    print('=-=' * 12)
    if mostrar == 999:
        print('Volte sempre!')
        break


cards=('武则天','李白','兰陵王','姜子牙','貂蝉','安琪拉')
package=[]
import random

while 1 :
	print('\n  充值让你变得更强！\n  请选择指令：\n  1.抽卡\n  2.查看背包\n  3.整理背包\n  4.离开')
	a=input()

	if a==1:
		b=input('输入抽卡次数:')
		if b<=0:
			print('请输入正确的次数')
			continue
		else:
			i=1
			for i in range(1,b):
				i=i+1
				c=random.choice(cards)
				package.append(c)
	print(package)