import os
import sys
import datetime
import numpy as np
import xarray as xr
import dask
import dask.diagnostics
import cartowik.profiletools as cpf
import hyoga.open

# processed runs
PROC_RUNS = ['alpcyc4.2km.grip.0820', 'alpcyc4.2km.grip.1040.pp',
             'alpcyc4.2km.epica.0970', 'alpcyc4.2km.epica.1220.pp',
             'alpcyc4.2km.md012444.0800', 'alpcyc4.2km.md012444.1060.pp',
             'alpcyc4.1km.epica.1220.pp']

# global attributes
GLOB_ATTRS = dict(
    title='Alpine ice sheet glacial cycle erosion aggregated variables',
    author='Julien Seguinot',
    institution='',
    command='{user}@{host} {time}: {cmdl}\n'.format(
        user=os.getlogin(), host=os.uname()[1], cmdl=' '.join(sys.argv),
        time=datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S UTC')),
    comment="""Aggregated dataset contents:
* Spatial variables aggregated in time over the entire simulation lenght from
120,000 years before present to the present.
* Time-series variables aggregated in space over the entire glaciated area,
defined by a 1-metre ice thickness threshold.
""")

def postprocess_extra(run_path):
    """Postprocess extra dataset for one run."""

    # output file and subtitle
    _, res, rec, *other = os.path.basename(run_path).split('.')
    out_file = 'processed/alpero.{}.{}.{}.agg.nc'.format(
        res, rec[:4], 'pp' if 'pp' in other else 'cp')

    # load output data (in the future combine='by_coords' will be the default)
    print("postprocessing " + out_file + "...")
    boot = hyoga.open.dataset(
        '~/pism/input/boot/'+'alps.srtm.hus12.gou11simi.{}.nc'.format(res))
    print("* reading multi-file extra dataset...")
    ex = hyoga.open.mfdataset(run_path+'/ex.???????.nc', parallel=True)

    # init postprocessed dataset with global attributes
    pp = xr.Dataset(attrs=ex.attrs, coords=dict(lon=ex.lon, lat=ex.lat))
    pp.attrs.update(
        subtitle='{} {} simulation {} precipitation reductions'.format(
            res, rec.upper(), 'with' if 'pp' in other else 'without'),
        **GLOB_ATTRS)
    pp.attrs.update(history=pp.command+pp.history)

    # register intermediate variables (I assumed the K_g numbers given in
    # supplement of Koppes et al., 2015 are for erosion rates in mm/a. An
    # alternative formula with two outliers removed gives 5.3e−12*v_b*2.62
    # (m/a). Now for some reason, these erosion laws seem to not exactly
    # correspond to the blue and pink lines on the summary figure by Cook et
    # al., 2020, which appear to be a factor two or three lower.)
    ex['icy'] = (ex.thk >= 1.0)
    ex['bedlift'] = ex.topg - boot.topg.where(boot.topg > 0, 0)
    ex['sliding'] = ex.velbase_mag.where(ex.icy)
    ex['warmbed'] = ex.icy*(ex.temppabase >= -1e-3)
    ex['coo2020'] = (1.665e-4*ex.sliding**0.6459).assign_attrs(
        ref='Cook et al. (2020)', units='m a-1')
    ex['her2015'] = (2.7e-7*ex.sliding**2.02).assign_attrs(
        ref='Herman et al. (2015)', units='m a-1')
    ex['hum1994'] = (1e-4*ex.sliding).assign_attrs(
        ref='Humphrey and Raymond (1994)', units='m a-1')
    ex['kop2015'] = (5.2e-11*ex.sliding**2.34).assign_attrs(
        ref='Koppes et al. (2015)', units='m a-1')

    # compute grid size
    dt = ex.age[0] - ex.age[1]
    dt = dt*1e3  # convert ka to a
    dx = ex.x[1] - ex.x[0]
    dy = ex.y[1] - ex.y[0]

    # compute glacial cycle integrated variables
    pp['cumu_sliding'] = (dt*ex.sliding.sum(axis=0, min_count=1)).assign_attrs(
        long_name='cumulative basal motion', units='m')
    pp['glacier_time'] = (dt*ex.icy.sum(axis=0)).assign_attrs(
        long_name='total ice cover duration', units='years')
    pp['warmbed_time'] = (dt*ex.warmbed.sum(axis=0)).assign_attrs(
        long_name='temperate-based ice cover duration', units='years')

    # trigger computation to avoid memory errors (18m, 70%, 8GiB)
    # NOTE: time and total mem for single-thread scheduler on altair, with
    # nothing else running. Trigger compute() more often to further reduce mem
    # consumption (maybe at the cost of repeating some operations).
    print("* computing time-integrated variables...")
    with dask.diagnostics.ProgressBar():
        pp = pp.compute()

    # compute timeseries
    pp['glacier_area'] = (dx*dy*ex.icy.sum(axis=(1, 2))).assign_attrs(
        long_name='glacierized area', units='m2')
    pp['volumic_lift'] = (dx*dy*ex.bedlift.sum(axis=(1, 2))).assign_attrs(
        long_name='volumic bedrock uplift', units='m3')
    pp['warmbed_area'] = (dx*dy*ex.warmbed.sum(axis=(1, 2))).assign_attrs(
        long_name='temperate-based ice cover area', units='m2')

    # trigger computation to avoid memory errors (24m, 35%, 4GiB)
    print("* computing space-integrated variables...")
    with dask.diagnostics.ProgressBar():
        pp = pp.compute()

    # compute erosion time-aggregated variables
    for law in ['coo2020', 'her2015', 'hum1994', 'kop2015']:
        pp[law+'_cumu'] = (dt*ex[law].sum(axis=0, min_count=1)).assign_attrs(
            long_name=ex[law].ref+' cumulative glacial erosion potential',
            units='m')

    # trigger computation to avoid memory errors (12m, 35%, 4GiB)
    print("* computing cumulative erosion potential...")
    with dask.diagnostics.ProgressBar():
        pp = pp.compute()

    # compute erosion space-aggregated variables
    # there's still a divide warning here, maybe check what that is
    # NOTE: for the 1-km-run hypsogram on 10-m bands, dask-2021.3.0 splits the
    # chunks even when asked not to. The progress bar only appears after two
    # hours (I nearly gave up), but the actual computations takes only 15 min!
    # In total the script took 194 min on altair.
    x, y = cpf.read_shp_coords(
        '../data/native/profile_rhine.shp', interval=1000)
    for law in ['coo2020', 'her2015', 'hum1994', 'kop2015']:
        pp[law+'_rate'] = (dx*dy*ex[law].sum(axis=(1, 2))).assign_attrs(
            long_name=ex[law].ref+' domain total volumic erosion rate',
            units='m3 year-1')
        pp[law+'_hyps'] = np.exp(np.log(ex[law]).groupby_bins(
            boot.topg, bins=range(0, 4501, 100)).mean(
                dim='stacked_y_x')).assign_attrs(
            long_name=ex[law].ref+' erosion rate geometric mean in band',
            units='m year-1')
        pp[law+'_rhin'] = ex[law].where(ex.icy).interp(
                x=x, y=y, method='linear').assign_attrs(
            long_name=ex[law].ref+' rhine transect erosion rate',
            units='m year-1')

    # trigger computation to avoid memory errors (12m, 40%, 5GiB)
    print("* computing erosion rate variables...")
    with dask.diagnostics.ProgressBar():
        pp = pp.compute()

    # compute glacier cover hypsogram
    pp['glacier_hyps'] = (
        dx*dy*ex.icy.groupby_bins(boot.topg, bins=range(0, 4501, 100)).sum(
            dim='stacked_y_x')).assign_attrs(
        long_name='glacierized area within elevation band', units='m2')

    # trigger computation to avoid memory errors (6m, 40%, 5GiB)
    print("* computing glacier cover hypsogram...")
    with dask.diagnostics.ProgressBar():
        pp = pp.compute()

    # replace intervals (xarray issue #2847)
    pp['topg_bins'] = [b.mid for b in pp.topg_bins.values]
    pp = pp.rename({'topg_bins': 'z'})
    pp['z'] = pp.z.assign_attrs(
        long_name='elevation band midpoints', units='m')
    pp['d'] = pp.d.assign_attrs(
        long_name='distance along transect', units='m')

    # copy grid mapping and pism config
    pp['mapping'] = ex.mapping
    pp['pism_config'] = ex.pism_config

    # export to netcdf
    print("* writing postprocessed file...")
    delayed = pp.to_netcdf(
        out_file, mode='w', compute=False, encoding={var: dict(
            zlib=True, shuffle=True, complevel=1) for var in pp.variables})
    with dask.diagnostics.ProgressBar():
        delayed.compute()

    # close datasets
    boot.close()
    ex.close()
    pp.close()

def main():
    """Main program called during execution."""

    # create directory if missing
    if not os.path.exists('processed'):
        os.makedirs('processed')

    # to use the dask distributed client
    # client = dask.distributed.Client()
    # print(client.dashboard_link)

    # ask dask to not split large chunks (but it still does)
    dask.config.set(**{'array.slicing.split_large_chunks': False})

    # postprocess selected runs
    for run in PROC_RUNS:
        postprocess_extra(os.environ['HOME'] + '/pism/output/e9d2d1f/' + run)

if __name__ == '__main__':
    main()


import torch
from torch import nn

def get_loss(config):
    name = config['loss']['name']
    loss = None
    if name == 'nll_loss':
        loss = nn.NLLLoss(reduction='sum')
    if name == 'CrossEntropy':
        loss = nn.CrossEntropyLoss()
    return loss


import datetime
import os
import subprocess

import boto3
import tqdm

def upload_hook(t):
    def inner(bytes_amount):
        t.update(bytes_amount)

    return inner

def upload(disable_progress=False):
    s3 = boto3.client("s3")

    dbs = {
        "db.sqlite3": "all-data.sqlite3.gz",
        "frontend.sqlite3": "frontend.sqlite3.gz",
    }

    now = datetime.datetime.now(datetime.timezone.utc)
    timestamp = now.strftime("%Y%m%dT%H%M%SZ")

    print("Saving files to s3")

    for disk_name, s3_name in dbs.items():
        subprocess.run(["gzip", "--force", disk_name], check=True)

        gzipped_name = disk_name + ".gz"
        object_name = f"v3/{timestamp}-{s3_name}"
        compressed_size = os.path.getsize(gzipped_name)

        with tqdm.tqdm(
            total=compressed_size,
            unit="B",
            unit_scale=True,
            desc=disk_name,
            disable=disable_progress,
        ) as t:
            s3.upload_file(
                gzipped_name, "kijiji-apartments", object_name, Callback=upload_hook(t),
            )

        os.remove(gzipped_name)

    print("...done")


import wx, os, platform, sys, ctypes

if sys.version_info <= (2, 6):
    import commands as subprocess
else:
    import subprocess

class MainFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        # there needs to be an "Images" directory with one or more jpegs in it in the
        # current working directory for this to work
        self.jpgs = GetJpgList(os.getcwd()) # get all the jpegs in the Images directory
        self.CurrentJpg = 0

        self.MaxImageSize = 300

        b = wx.Button(self, 0, ">")
        b.Bind(wx.EVT_BUTTON, self.DisplayNext)

        bb = wx.Button(self, 0, "<")
        bb.Bind(wx.EVT_BUTTON, self.DisplayPrevious)

        setw = wx.Button(self, 0, "Set as Wallpaper")
        setw.Bind(wx.EVT_BUTTON, self.SetWallpaper)

        cd = wx.Button(self, 0, "Choose Directory")
        cd.Bind(wx.EVT_BUTTON, self.ChooseDir)

        # starting with an EmptyBitmap, the real one will get put there
        # by the call to .DisplayNext()
        self.Image = wx.StaticBitmap(self, bitmap=wx.EmptyBitmap(self.MaxImageSize, self.MaxImageSize-100))

        self.DisplayNext()

        # Using a Sizer to handle the layout: I never  use absolute positioning
        box = wx.BoxSizer(wx.VERTICAL)

        box.Add(cd, 0, wx.CENTER | wx.ALL,10)

        hbox1 = wx.BoxSizer(wx.HORIZONTAL)

        hbox1.Add(bb, 0, wx.RIGHT | wx.ALL,10)

        # adding stretchable space before and after centers the image.
        hbox1.Add((1,1),1)
        hbox1.Add(self.Image, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALL | wx.ADJUST_MINSIZE, 10)
        hbox1.Add((1,1),1)

        hbox1.Add(b, 0, wx.LEFT | wx.ALL,10)

        box.Add(hbox1, flag=wx.EXPAND, border=10)
        box.Add(setw, 0, wx.CENTER | wx.ALL,10)

        self.SetSizerAndFit(box)

        wx.EVT_CLOSE(self, self.OnCloseWindow)

    def SetWallpaper(self, event=None):

        save_location = self.jpgs[self.CurrentJpg-1]

        supported_linux_desktop_envs = ["gnome", "mate", "kde", "lubuntu"]

        # Check OS and environments
        platform_name = platform.system()
        if platform_name.startswith("Lin"):

            # Check desktop environments for linux
            desktop_environment = detect_desktop_environment()
            if desktop_environment and desktop_environment["name"] in supported_linux_desktop_envs:
                os.system(desktop_environment["command"].format(save_location=save_location))
            else:
                #print("Unsupported desktop environment")
                #print("Trying feh")
                os.system("feh --bg-fill '{save_location}'".format(save_location=save_location))
                #print("[SUCCESS] done!!")

        # Windows
        if platform_name.startswith("Win"):
            # Python 3.x
            if sys.version_info >= (3, 0):
                ctypes.windll.user32.SystemParametersInfoW(20, 0, save_location, 3)
            # Python 2.x
            else:
                ctypes.windll.user32.SystemParametersInfoA(20, 0, save_location, 3)

        # OS X/macOS
        if platform_name.startswith("Darwin"):
            if args.display == 0:
                command = """
                        osascript -e 'tell application "System Events"
                            set desktopCount to count of desktops
                            repeat with desktopNumber from 1 to desktopCount
                                tell desktop desktopNumber
                                    set picture to "{save_location}"
                                end tell
                            end repeat
                        end tell'
                        """.format(save_location=save_location)
            else:
                command = """osascript -e 'tell application "System Events"
                                set desktopCount to count of desktops
                                tell desktop {display}
                                    set picture to "{save_location}"
                                end tell
                            end tell'""".format(display=args.display,
                                            save_location=save_location)
            os.system(command)

    def ChooseDir(self, event=None):
        dialog = wx.DirDialog(None, "Choose a directory:",style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
        if dialog.ShowModal() == wx.ID_OK:
            self.jpgs = GetJpgList(dialog.GetPath())
            self.DisplayNext()
        dialog.Destroy()

    def DisplayNext(self, event=None):
        if len(self.jpgs) == 0:
            return 1
        # load the image
        Img = wx.Image(self.jpgs[self.CurrentJpg], wx.BITMAP_TYPE_ANY)

        # scale the image, preserving the aspect ratio
        W = Img.GetWidth()
        H = Img.GetHeight()
        if W > H:
            NewW = self.MaxImageSize
            NewH = self.MaxImageSize * H / W
        else:
            NewH = self.MaxImageSize
            NewW = self.MaxImageSize * W / H
        Img = Img.Scale(NewW,NewH)

        # convert it to a wx.Bitmap, and put it on the wx.StaticBitmap
        self.Image.SetBitmap(wx.BitmapFromImage(Img))

        # You can fit the frame to the image, if you want.
        self.Fit()
        self.Layout()
        self.Refresh()

        self.CurrentJpg += 1
        if self.CurrentJpg > len(self.jpgs) -1:
            self.CurrentJpg = 0

    def DisplayPrevious(self, event=None):
        if len(self.jpgs) == 0:
            return 1
        # load the image
        Img = wx.Image(self.jpgs[self.CurrentJpg - 2], wx.BITMAP_TYPE_ANY)

        # scale the image, preserving the aspect ratio
        W = Img.GetWidth()
        H = Img.GetHeight()
        if W > H:
            NewW = self.MaxImageSize
            NewH = self.MaxImageSize * H / W
        else:
            NewH = self.MaxImageSize
            NewW = self.MaxImageSize * W / H
        Img = Img.Scale(NewW,NewH)

        # convert it to a wx.Bitmap, and put it on the wx.StaticBitmap
        self.Image.SetBitmap(wx.BitmapFromImage(Img))

        # You can fit the frame to the image, if you want.
        self.Fit()
        self.Layout()
        self.Refresh()

        self.CurrentJpg -= 1
        if self.CurrentJpg < 0:
            self.CurrentJpg = len(self.jpgs) -1

    def OnCloseWindow(self, event):
        self.Destroy()

def detect_desktop_environment():
    """Get current Desktop Environment
       http://stackoverflow.com
       /questions/2035657/what-is-my-current-desktop-environment
    :return: environment
    """
    environment = {}
    if os.environ.get("KDE_FULL_SESSION") == "true":
        environment["name"] = "kde"
        environment["command"] = """
                    qdbus org.kde.plasmashell /PlasmaShell org.kde.PlasmaShell.evaluateScript '
                        var allDesktops = desktops();
                        print (allDesktops);
                        for (i=0;i<allDesktops.length;i++) {{
                            d = allDesktops[i];
                            d.wallpaperPlugin = "org.kde.image";
                            d.currentConfigGroup = Array("Wallpaper",
                                                   "org.kde.image",
                                                   "General");
                            d.writeConfig("Image", "file:///{save_location}")
                        }}
                    '
                """
    elif os.environ.get("GNOME_DESKTOP_SESSION_ID"):
        environment["name"] = "gnome"
        environment["command"] = "gsettings set org.gnome.desktop.background picture-uri file://{save_location}"
    elif os.environ.get("DESKTOP_SESSION") == "Lubuntu":
        environment["name"] = "lubuntu"
        environment["command"] = "pcmanfm -w {save_location} --wallpaper-mode=fit"
    elif os.environ.get("DESKTOP_SESSION") == "mate":
        environment["name"] = "mate"
        environment["command"] = "gsettings set org.mate.background picture-filename {save_location}"
    else:
        try:
            info = subprocess.getoutput("xprop -root _DT_SAVE_MODE")
            if ' = "xfce4"' in info:
                environment["name"] = "xfce"
        except (OSError, RuntimeError):
            environment = None
            pass
    return environment

def GetJpgList(dir):
    jpgs = [f for f in os.listdir(dir) if (f[-4:] == ".jpg" or f[-4:] == ".png")]
    # print "JPGS are:", jpgs
    return [os.path.join(dir, f) for f in jpgs]

class App(wx.App):
    def OnInit(self):

        frame = MainFrame(None, -1, "Wallie", wx.DefaultPosition,(550,200))
        self.SetTopWindow(frame)
        frame.Show(True)
        return True

if __name__ == "__main__":
    app = App(False)
    app.MainLoop()


def grader(grade):
    if 2.00 <= grade <= 2.99:
        return "Fail"
    elif 3.00 <= grade <= 3.49:
        return "Poor"
    elif 3.50 <= grade <= 4.49:
        return "Good"
    elif 4.50 <= grade <= 5.49:
        return "Very Good"
    elif 5.50 <= grade <= 6.00:
        return "Excellent"

grade = grader(float(input()))

print(grade)


import Image

def search(maze, tree):
    dirs = (0,1), (0,-1), (1,0), (-1,0)
    wall = (255,) * 4
    w,h = maze.size

    maze.putpixel((1, h-1), wall) # Wall off exit
    maze.putpixel((w-2, 0), wall) # Wall off entrance
    queue = [(1, h-2)]            # Start at exit
    tree[queue[0]] = 'exit'

    while queue:
        pos = queue.pop(0)
        for d in dirs:
            new_pos = pos[0] + d[0], pos[1] + d[1]
            if not tree.has_key(new_pos) and maze.getpixel(new_pos) != wall:
                tree[new_pos] = pos

                queue.append(new_pos)

maze = Image.open('maze.png')
tree = {}
search(maze, tree)
w = maze.size[0]
path = []
pos = (w-2,1)
while pos != 'exit':
    path.append(chr(maze.getpixel(pos)[0]))

    pos = tree[pos]
pos = (w-2,1)
while pos != 'exit':
    maze.putpixel(pos, (0,255,)*2)
    pos = tree[pos]

maze.save('maze-solved.png')

mazezip = ''.join(path[::2])
open('maze.zip','w').write(mazezip)


# 3.12 시간 단위 변환

from datetime import timedelta
a = timedelta(days=2, hours=6)
b = timedelta(hours=4.5)
c = a + b
c.days
# 2
c.seconds
# 37800
c.seconds / 3600
# 10.5
c.total_seconds() / 3600
# 58.5

from datetime import datetime
a = datetime(2012, 9, 23)
print(a + timedelta(days=10))
# 2012-10-03 00:00:00

b = datetime(2012, 12, 21)
d = b - a
d.days
# 89
now = datetime.today()
print(now)
# 2012-12-21 14:54:43.094063
print(now + timedelta(minutes=10))
# 2012-12-21 15:04:43.094063

a = datetime(2012, 3, 1)
b = datetime(2012, 2, 28)
a - b
# datetime.timedelta(2)
(a - b).days
# 2
c = datetime(2013, 3, 1)
d = datetime(2013, 2, 28)
(c - d).days
# 1

# 토론

a = datetime(2012, 9, 23)
a + timedelta(months=1)
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# TypeError: 'months' is an invalid keyword argument for this function

from dateutil.relativedelta import relativedelta
a + relativedelta(months=+1)
# datetime.datetime(2012, 10, 23, 0, 0)
a + relativedelta(months=+4)
# datetime.datetime(2013, 1, 23, 0, 0)

# 두 날짜 사이의 시간
b = datetime(2012, 12, 21)
d = b - a
# datetime.timedelta(89)
d = relativedelta(b, a)
# relativedelta(months=+2, days=+28)
d.months
# 2
d.days
# 28


# -*- coding: utf-8 -*-
from __future__ import absolute_import

import os
import click

from sources.python_weekly import PythonWeekly
from utils.logging import get_logger

logger = get_logger('main.py')

# The hashtable of sources.
source_map = {
    'python_weekly': PythonWeekly,
}

# Registry the group command.
@click.group()
@click.pass_context
def pip_cli(ctx, **kwargs):
    ctx.obj = kwargs

@pip_cli.command(name='run')
@click.option(
    '-s', '--source',
    required=True,
    type=str,
    help='The source of newsletter.',
)
@click.option(
    '-i', '--issue',
    required=False,
    type=int,
    help='The issue number.',
)
@click.option(
    '-t', '--to-issue',
    required=False,
    type=int,
    help='Get from issue 1 up until the given issue.',
)
def run(source: str, issue: int, to_issue):
    """
    Get source with a given issue.
    """

    if source in source_map:
        class_source = source_map[source]

        if not to_issue:
            # Getting 1 issue only.
            class_source(issue_number=issue).run()
        else:
            # Getting the whole issues from 1 to n.
            for i in range(1, to_issue):
                class_source(issue_number=i).run()

    logger.info(f'Getting source: {source} with issue: {issue}')

def main():
    pip_cli()

if __name__ == '__main__':
    pip_cli()


import torch.nn as nn
import torch
import torch.nn.functional as F
from tqdm import tqdm

from attribution_bottleneck.bottleneck.estimator import ReluEstimator
from attribution_bottleneck.attribution.base import AttributionMethod
from attribution_bottleneck.bottleneck.per_sample_bottleneck import PerSampleBottleneck
from attribution_bottleneck.bottleneck.estimator import Estimator
from attribution_bottleneck.utils.misc import resize, replace_layer, to_np

class PerSampleBottleneckReader(AttributionMethod):
    def __init__(self, model, estim: Estimator, beta=10, steps=10, lr=1, batch_size=10,
                 sigma=1, progbar=False):
        self.model = model
        self.original_layer = estim.get_layer()
        self.shape = estim.shape()
        self.beta = beta
        self.batch_size = batch_size
        self.progbar = progbar
        self.device = list(model.parameters())[0].device
        self.lr = lr
        self.train_steps = steps
        self.bottleneck = PerSampleBottleneck(estim.mean(), estim.std(), device=self.device,
                                              sigma=sigma, relu=isinstance(estim, ReluEstimator))
        self.sequential = nn.Sequential(self.original_layer, self.bottleneck)

    def heatmap(self, input_t, target):
        target_t = torch.tensor([target]) if not isinstance(target, torch.Tensor) else target
        self._run_training(input_t, target_t)
        return self._current_heatmap(shape=input_t.shape[2:])

    def _run_training(self, input_t, target_t):
        # Attach layer and train the bottleneck
        replace_layer(self.model, self.original_layer, self.sequential)
        self._train_bottleneck(input_t, target_t)
        replace_layer(self.model, self.sequential, self.original_layer)

    def _current_heatmap(self, shape=None):
        # Read bottleneck
        heatmap = self.bottleneck.buffer_capacity
        heatmap = to_np(heatmap[0])
        heatmap = heatmap.sum(axis=0)  # Sum over channel dim
        heatmap = heatmap - heatmap.min()  # min=0
        heatmap = heatmap / heatmap.max()  # max=0

        if shape is not None:
            heatmap = resize(heatmap, shape)

        return heatmap

    def _train_bottleneck(self, input_t: torch.Tensor, target_t: torch.Tensor):

        assert input_t.shape[0] == 1, "We can only fit on one sample"
        assert target_t.shape[0] == 1, "We can only fit on one label"

        batch = input_t.expand(self.batch_size, -1, -1, -1), target_t.expand(self.batch_size)
        optimizer = torch.optim.Adam(lr=self.lr, params=self.bottleneck.parameters())

        # Reset from previous run or modifications
        self.bottleneck.reset_alpha()

        # Train
        self.model.eval()
        for _ in tqdm(range(self.train_steps), desc="Training Bottleneck",
                      disable=not self.progbar):
            optimizer.zero_grad()
            out = self.model(batch[0])
            loss_t = self.calc_loss(outputs=out, labels=batch[1])
            loss_t.backward()
            optimizer.step(closure=None)

    def calc_loss(self, outputs, labels):
        """ Calculate the combined loss expression for optimization of lambda """
        information_loss = self.bottleneck.buffer_capacity.mean()  # Taking the mean is equivalent of scaling with 1/K
        cross_entropy = F.cross_entropy(outputs, target=labels)
        total = cross_entropy + self.beta * information_loss
        return total


import urllib.request
import urllib.parse

# simple request
with urllib.request.urlopen('https://www.oneoffcoder.com/') as response:
    content = response.read()
    print(content)

# request with query string parameters
query_string_data = {
    'q': 'oneoffcoder',
    'sourceid': 'chrome',
    'ie': 'UTF-8'
}
query_string = urllib.parse.urlencode(query_string_data)
url = f'https://www.google.com/search?{query_string}'
data = urllib.request.urlopen(url)

# request with headers
query_string_data = {
    'q': 'oneoffcoder',
    'sourceid': 'chrome',
    'ie': 'UTF-8'
}
query_string = urllib.parse.urlencode(query_string_data).encode('ascii')

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'}
url = 'https://www.google.com/search'

req = urllib.request.Request(url, data, headers)
with urllib.request.urlopen(req) as response:
    content = response.read()


#!/usr/bin/env python3
import re, copy, argparse, json

class Node(dict):
    def __init__(self, *args, **kwargs):
        super(Node, self).__init__(*args, **kwargs)

    def add_child(self, node):
        if not 'children' in self:
            self['children'] = list()
        self['children'].append(node)

    def get_nodes(self, nodes=None):
        if nodes is None:
            nodes = []
        nodes.append(self)
        if 'children' in self:
            for i in self['children']:
                i.get_nodes(nodes)
        return nodes

def from_tsv(name, input_path, output_path):

    root = Node(entry=name, name=name, layer='root')

    with open(input_path, 'rU') as mapping_file:
        levels = mapping_file.readline().strip().split('\t')
        levels.insert(0, 'root')
        nodes_layer = {key: {} for key in levels}
        nodes_layer['root'] = {'root': root}

        for line in mapping_file:
            entries = line.strip().split('\t')
            # prefix empty cells by parent id
            for index, entry in enumerate(entries):
                if entry == ["", "-"]:
                    unique_entry = levels[index + 1]
                    if index == 0:
                        unique_entry = "root_" + unique_entry
                    else:
                        unique_entry = entries[index - 1] + "_" + unique_entry
                    entries[index] = unique_entry

            for index, entry in enumerate(entries):
                layer = levels[index + 1]
                if entry not in nodes_layer[layer]:
                    parent_layer = levels[index]
                    node = Node(entry=entry, name=entry, layer=layer)
                    if index > 0:
                        nodes_layer[parent_layer][entries[index - 1]].add_child(node)
                    else:
                        nodes_layer[parent_layer]['root'].add_child(node)
                    nodes_layer[layer][entry] = node
    with open(output_path, 'w') as output:
        output.write(json.dumps(root, sort_keys=True, indent=4))

if __name__ == '__main__':
    name = 'Enteropathway Functional Hierarchies'
    #/home/omixer/Downloads/FOAM-onto_rel1.tsv
    ipath ='/home/omixer/Downloads/epathway_output.tsv'
    #/home/omixer/Downloads/FOAM-onto_rel1.json
    opath = '/home/omixer/Downloads/enteropathway.json'
    from_tsv(name, ipath, opath)


# two classes (class1, class2)
# only replace with a directory of yours
# finally .npy files saved in your directory with names train.npy, #test.npy, train_labels.npy, test_labels.npy
import cv2
import glob
import numpy as np
#Train data
train = []
train_labels = []

NO_OF_CLASSES = 30
IMG_SIZE = 128

filepaths = ["../../datasets/texture30/*.jpg"]
files = glob.glob("../../datasets/texture30/*.jpg")

for i in range(len(files)):
    j = int(files[i].split('/')[4][1:4])
    print(str(files[i].split('/')[4])," , ",str(j))
    train_label = np.zeros(NO_OF_CLASSES,'int8')
    train_label[j-1] = 1
        #prvchgint(myFile)
    image = cv2.imread(files[i], 0)
    train.append(cv2.resize(image, (IMG_SIZE, IMG_SIZE)))
    train_labels.append(train_label)
    #print(train_label)

train_labels = np.array(train_labels,dtype='int8') #as mnist
train = np.array(train)
print(train.shape)
# convert (number of images x height x width x number of channels) to (number of images x (height * width *3))
# for example (120 * 40 * 40 * 3)-> (120 * 4800)

train = np.reshape(train,[train.shape[0],train.shape[1]*train.shape[2]])
print(train.shape)

# # save numpy array as .npy formats
np.save('tex45Img',train)
np.save('tex45Imglabels',train_labels)

# #Test data
# test = []
# test_labels = []
# files = glob.glob ("/data/test/class1/*.JPG")
# for myFile in files:
#     image = cv2.imread (myFile)
#     test.append (image)
#     test_labels.append([1., 0.]) # class1
# files = glob.glob ("/data/test/class2/*.JPG")
# for myFile in files:
#     image = cv2.imread (myFile)
#     test.append (image)
#     test_labels.append([0., 1.]) # class2

# test = np.array(test,dtype='float32') #as mnist example
# test_labels = np.array(test_labels,dtype='float64') #as mnist
# test = np.reshape(test,[test.shape[0],test.shape[1]*test.shape[2]*test.shape[3]])

# # save numpy array as .npy formats
# np.save('test',test) # saves test.npy
# np.save('test_labels',test_labels)


'''
Created on 2013-02-28

Problem 9:
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which a^2 + b^2 = c^2

For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

SOLUTION: 31875000
@author: Jason Baker
'''

def isTriple(a, b, c):
    return a**2 + b**2 == c**2

a = 1
b = 0
c = 0

# I had to do some back of the envelope math to get the formulas for this
# Because we're given two equations with three unknowns, it's pretty basic math to isolate for a single variable
# Then it's just a matter of incrementing that variable, and adjusting the others accordingly
while(a + b + c != 1000 and not isTriple(a, b, c)):
    a += 1
    b = int( (1000 * (a - 500))/(a - 1000) )
    c = int( (-1 * a**2 + 1000*a - 500000)/(a - 1000) )

print(a*b*c)


from miasm.core.asmblock import disasmEngine
from miasm.arch.aarch64.arch import mn_aarch64

cb_aarch64_funcs = []

def cb_aarch64_disasm(*args, **kwargs):
    for func in cb_aarch64_funcs:
        func(*args, **kwargs)

class dis_aarch64b(disasmEngine):
    attrib = "b"
    def __init__(self, bs=None, **kwargs):
        super(dis_aarch64b, self).__init__(
            mn_aarch64, self.attrib, bs,
            dis_block_callback = cb_aarch64_disasm,
            **kwargs)

class dis_aarch64l(disasmEngine):
    attrib = "l"
    def __init__(self, bs=None, **kwargs):
        super(dis_aarch64l, self).__init__(
            mn_aarch64, self.attrib, bs,
            dis_block_callback = cb_aarch64_disasm,
            **kwargs)


#!/usr/bin/env python
import keras, tensorflow as tf, numpy as np, gym, sys, copy, argparse
import random
from algs.DQN import create_dqn
from algs.a2c import A2C
from algs.ddpg import DDPG
import time
import logging
import os.path

logger = logging.getLogger("10703")
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(message)s')

logs_dir = "logs"
curr_model_dir = None
models_dir = "models"

a2c_model = "algs/a2c.py"
dqn_model = "algs/DQN.py"

def parse_arguments():
	# Ex. python main.py --env CartPole-v0 --replay-batch 32 --model-name cartpole_dqn_w_mem --deepness deep
    parser = argparse.ArgumentParser(description='Experience Replay Argument Parser')
    parser.add_argument('--env-name',dest='env_name',type=str, default="LunarLander-v2")
    parser.add_argument('--alg', type=str, default="a2c", help="a2c or dqn")
    parser.add_argument('--model-name',dest='model_name',type=str,required=True)

    parser.add_argument('--seed', dest='seed', type=int)
    parser.add_argument('--num-episodes', dest='num_episodes', type=int,
                        default=50000, help="Number of episodes to train on.")
    parser.add_argument('--gamma', type=float, default=0.99)

    parser.add_argument('--memory-size', type=int, default=50000, help="ER buffer initial max size")

    # Utils
    parser.add_argument('--render', dest='render', action="store_true")
    parser.add_argument('--record-video-only',dest='record_video_only',action="store_true")
    parser.add_argument('--test-only', action="store_true")
    parser.add_argument('--train-mod', type=int, default=100, help="Print every x number of episodes")
    parser.add_argument('--test-mod', type=int, default=500, help="Test every x number of episodes")

    # A2C
    parser.add_argument('--actor-model-path', type=str, default=None,
                        help="Path to the actor model file.")
    parser.add_argument('--actor-lr', type=float,
                        default=0.01, help="The actor's learning rate.")
    parser.add_argument('--critic-model-path', type=str, default=None)
    parser.add_argument('--critic-lr', type=float,
                        default=0.01, help="The critic's learning rate.")

    parser.add_argument('--N', dest='N', type=int,
                        default=50, help="The number of steps in N-step A2C.")

    # DQN
    parser.add_argument('--epsilon', dest='epsilon', type=float, default=0.5)
    parser.add_argument('--deepness',dest='deepness',type=str,default=False)
    parser.add_argument('--lr',dest='lr',type=float,default=0.0001)
    parser.add_argument('--alt-learn', dest='alt_learn', action="store_true")

    #DDPG
    parser.add_argument('--tau', type=float, default=0.001)

    # ER
    parser.add_argument('--replay-batch',dest='replay_batch',type=int, default=32)
    parser.add_argument('--hindsight-replay', dest='hindsight_replay',action="store_true")
    parser.add_argument('--combined-replay', dest='combined_replay', action="store_true")
    parser.add_argument('--priority-replay', dest='priority_replay', action="store_true")
    # TODO: add diffeq ultimate replay evolution adaptation

    return parser.parse_args()

def main(args):

    global curr_model_dir

    args = parse_arguments()

    if not os.path.exists(models_dir):
        os.makedirs(models_dir)

    curr_model_dir = f"{models_dir}/{args.model_name}/"
    if not os.path.exists(curr_model_dir):
        os.makedirs(curr_model_dir)

    if not os.path.exists(logs_dir):
        os.makedirs(logs_dir)

    #start logging settings
    os.environ['TZ'] = 'EST+05EDT,M4.1.0,M10.5.0'
    time.tzset()

    time_str = time.strftime('%Y_%m_%d_%H_%M_%S')
    log_file_name = f'{logs_dir}/{args.model_name}_{time_str}.log'
    hdlr = logging.FileHandler(log_file_name)
    hdlr.setFormatter(formatter)
    logger.addHandler(hdlr)
    ch = logging.StreamHandler(sys.stdout)
    ch.setLevel(logging.DEBUG)
    ch.setFormatter(formatter)
    logger.addHandler(ch)

    #end logging settings

    # Setting the session to allow growth, so it doesn't allocate all GPU memory.
    gpu_ops = tf.GPUOptions(allow_growth=True)
    config = tf.ConfigProto(gpu_options=gpu_ops)
    sess = tf.Session(config=config)

    # Setting this as the default tensorflow session.
    keras.backend.tensorflow_backend.set_session(sess)

    #assert(args.env in ["CartPole-v0", "MountainCar-v0", "LunarLander-v2"])
    assert(args.alg in ["a2c", "dqn", "ddpg"])

    logger.info(f"Command line args: {args}")
    logger.info(f"Log saving to {log_file_name}")
    logger.info(f"Alg: {args.alg}")

    if args.hindsight_replay:
        if args.env_name == "MountainCar-v0" or args.env_name == "MountainCarContinuous-v0":
            default_goal = np.array([[0.5]]) # flag at x = 0.5
        elif args.env_name == "LunarLander-v2" or args.env_name == "LunarLanderContinuous-v2":
            default_goal = np.array([[0.0, 0.0]]) # landing pad at x,y = (0,0)
        elif args.env_name == "Pendulum-v0":
            default_goal = np.array([[1.0, 0.0]]) # cos(theta), sin(theta), theta dot
                                                    # theta is normalized between pi and -pi
        else:
            raise ValueError("Hindsight not enabled for this env")

    else:
        default_goal = None

    if args.seed != None:
        time_seed = args.seed
    else:
        #set consistent seed based on time
        time_seed = int(''.join(time_str.split('_'))) % (2 ** 32)

    np.random.seed(time_seed)
    logger.info(f"Numpy random seed {time_seed}")

    env = gym.make(args.env_name)
    num_inputs = env.observation_space.shape[0]

    num_outputs = None
    try:
        num_outputs = env.action_space.n
    except AttributeError:
        pass
    print(f"Num env inputs (state space): {num_inputs}")
    print(f"Num env outputs (actions): {num_outputs}")

    if args.alg == "a2c":
        agent = A2C(env, args.model_name, args.actor_model_path, args.actor_lr,
            args.critic_model_path, args.critic_lr, N=args.N, logger=logger)
    elif args.alg == "dqn":
        agent, use_episodes, num_train_episodes, num_train_steps = create_dqn(logger, args, env, default_goal, curr_model_dir, time_seed)
    elif args.alg == "ddpg":
        agent = DDPG(env, args.critic_lr, args.actor_lr, args.gamma, tau=args.tau,
            batch_size=args.replay_batch, default_goal=default_goal)

    if args.record_video_only:
        agent.test(record_video=True)
        return

    if args.test_only:
        agent.test()
    else:
        if args.alg == "a2c":
            assert not args.priority_replay, "NYI"
            assert not args.combined_replay, "NYI"
            assert not args.hindsight_replay, "NYI"
            agent.train(args.num_episodes, gamma=args.gamma,
                report_interval=args.train_mod, test_interval=args.test_mod, render=args.render)
        elif args.alg == "dqn":
            agent.train(use_episodes, num_train_episodes, num_train_steps,
            rep_batch_size = False if args.replay_batch == 0 else args.replay_batch,
            print_episode_mod=args.train_mod, test_episode_mod=args.test_mod,
            replay_mem_size=args.memory_size, default_goal=default_goal)
        elif args.alg == "ddpg":
            agent.train(env, args.num_episodes, default_goal=default_goal, hindsight_replay=args.hindsight_replay,
                priority_replay=args.priority_replay, combined_replay=args.combined_replay,
                train_mod=args.train_mod, test_mod=args.test_mod)

    logger.info(f"Log saved to {log_file_name}")

if __name__ == '__main__':
	main(sys.argv)


import sqlite3, time
from os import path

# log database SQL
CREATE_LOG   = 'CREATE TABLE conn_log   (id INTEGER PRIMARY KEY AUTOINCREMENT, user INTEGER, proto INTEGER, binary TEXT, dport INTEGER, dest STRING)'
CREATE_STAMP = 'CREATE TABLE conn_stamp (id INTEGER PRIMARY KEY AUTOINCREMENT, log_id INTEGER, outcome INTEGER, time DATE)'
INSERT_LOG   = 'INSERT INTO conn_log (user, proto, binary, dport, dest) VALUES (?, ?, ?, ?, ?)'
INSERT_STAMP = 'INSERT INTO conn_stamp (log_id, outcome, time) VALUES (?, ?, ?)'
EXISTS_LOG   = 'SELECT count(id) FROM conn_log WHERE user=? and proto=? and binary=? and dport=? and dest=?'
LOG_ID       = 'SELECT id FROM conn_log WHERE user=? and proto=? and binary=? and dport=? and dest=?'

# violatoin database SQL
CREATE_VIOLATION = 'CREATE TABLE conn_violation (id INTEGER PRIMARY KEY AUTOINCREMENT, user INTEGER, proto INTEGER, binary TEXT, dport INTEGER, dest STRING)'
CREATE_HANDLED  = 'CREATE TABLE conn_handled   (id INTEGER PRIMARY KEY AUTOINCREMENT, user INTEGER, proto INTEGER, binary TEXT, dport INTEGER, dest STRING)'
EXISTS_VIOLATION = 'SELECT count(id) FROM conn_violation WHERE user = ? AND proto = ? AND binary = ? AND dport = ? AND dest = ?'
EXISTS_HANDLED   = 'SELECT count(id) FROM conn_handled   WHERE user = ? AND proto = ? AND binary = ? AND dport = ? AND dest = ?'
INSERT_VIOLATION = 'INSERT INTO conn_violation (user, proto, binary, dport, dest) VALUES (?, ?, ?, ?, ?)'

class _Logger:
    ''' Log utility class
    '''
    def __init__(self, suffix, log_dir, tables, use_date=True):
        self.suffix   = suffix
        self.log_dir  = log_dir
        self.tables   = tables
        self.use_date = use_date
        self.conn     = self.open_log()

    def open_log(self):
        ''' Create and/or connect to database
        '''
        log_name = '%s/%s.%s' % (self.log_dir, self._next_log(), self.suffix)
        if not self.use_date:
            log_name = '%s/%s.%s' % (self.log_dir, 'log', self.suffix)

        exists = path.exists(log_name)
        conn   = sqlite3.connect(log_name)

        # create the table structure if the log did not exist
        if not exists:
            for table in self.tables:
                conn.execute(table)
                conn.commit()

        return conn

    def _next_log(self):
        self.date = time.localtime()
        return "%s.%s.%s" % (self.date.tm_year, self.date.tm_mon, self.date.tm_mday)

    def _check_rotate(self):
        return time.localtime() != self.date.tm_mday

    def rotate_log(self):
        ''' Rotate the database (based on day of month)
        '''
        if not self.use_date:
            return
        # close the open connection and open a new database
        self.cleanup()
        self.conn = self.open_log()

    def cleanup(self):
        ''' Cleanup database connections on shutdown
        '''
        self.conn.close()

class ConnectionLogger(_Logger):
    ''' Log utility for connection information gathered
        return - rowid for the timestamp entry
    '''
    def __init__(self, log_dir):
        _Logger.__init__(self, 'log', log_dir, ( CREATE_LOG, CREATE_STAMP ))

    def log_conn(self, uid, proto, app, dport, dest, outcome):
        ''' Log a new outgoing connection
        '''
        # hand log rotation
        if self._check_rotate():
            self.rotate_log()

        last_curr = None
        # another instance of an already seen connection
        if self.conn.execute(EXISTS_LOG, (uid, proto, app, dport, dest)).next()[0] > 0:
            last_curr = self.conn.execute(INSERT_STAMP, (self.conn.execute(LOG_ID, (uid, proto, app, dport, dest)).next()[0], outcome, time.asctime()))
        # a never seen connection
        else:
            curr = self.conn.execute(INSERT_LOG, (uid, proto, app, dport, dest))
            last_curr = self.conn.execute(INSERT_STAMP, (curr.lastrowid, outcome, time.asctime()))
        self.conn.commit()

        return last_curr.lastrowid

# SQL REQUIRED
# HANDLED LOGGER REQUIRED
class ViolationLogger(_Logger):
    ''' Log utility for policy violations
    '''
    def __init__(self, log_dir):
        _Logger.__init__(self, 'violation', log_dir, ( CREATE_VIOLATION, ))
        self.handled = HandledLogger(log_dir)

    def exists(self, uid, proto, app, dport, dest):
        ''' Check if a violation was already logged
        '''
        return self.conn.execute(EXISTS_VIOLATION, (uid, proto, app, dport, dest)).next()[0] > 0

    def log_violation(self, uid, proto, app, dport, dest):
        ''' Log a new violation
        '''
        # hand log rotation
        if self._check_rotate():
            self.rotate_log()

        # add violations that have not yet been recorded
        if not self.handled.exists(uid, proto, app, dport, dest) and not self.exists(uid, proto, app, dport, dest):
            self.conn.execute(INSERT_VIOLATION, (uid, proto, app, dport, dest))
        self.conn.commit()

class HandledLogger(_Logger):
    ''' Log utility for handled policy violations
    '''
    def __init__(self, log_dir):
        _Logger.__init__(self, 'handled', log_dir, ( CREATE_HANDLED, ), use_date=False)

    def exists(self, uid, proto, app, dport, dest):
        ''' Check if a violation was already handled
        '''
        return self.conn.execute(EXISTS_HANDLED, (uid, proto, app, dport, dest)).next()[0] > 0


import json
from watson_developer_cloud import ToneAnalyzerV3
import os

api_key = os.environ.get('WATSON_TONE_ANALYZER')

class ToneAnalyzer:

    def __init__(self, tone_analyzer = ToneAnalyzerV3(
        version='2017-09-21',
        iam_apikey=api_key,
        url= 'https://gateway-lon.watsonplatform.net/tone-analyzer/api/v3/tone?version=2017-09-21&sentences=false'
    )):
        self.tone_analyzer = tone_analyzer

    def send_for_analysis(self, text):
        tone_analysis = self.tone_analyzer.tone(
            { 'text': text },
            'application/json',
        ).get_result()
        return json.dumps(tone_analysis)


from bs4 import BeautifulSoup
import requests
import requests.exceptions
from urllib.parse import urlsplit
from collections import deque
import re
from lxml import etree
import pandas as pd
import time

years=range(2002,2020,2)
resDF=pd.DataFrame()
#resDF=pd.read_csv("hiphop_data.csv")
for year in years:
    for year in range(year,year+2):
        print(year)
        baseUrl = "https://www.billboard.com/charts/year-end/"+str(year)+"/hot-r-and-and-b-hip-hop-songs"
        strhtml = requests.get(baseUrl).content

        soup = BeautifulSoup(strhtml, 'lxml')

        songnames = []
        artistnames = []

        data=soup.select("div[class='ye-chart-item__title']")
        for item in data:
            songnames.append(item.get_text().strip())

        data=soup.select("div[class='ye-chart-item__artist']")
        for item in data:
            artistnames.append(item.get_text().strip())

        track = zip(songnames, artistnames, [year] * 100, ["hiphop"] * 100)
        tmp_df = pd.DataFrame(track)
        resDF = pd.concat([resDF, tmp_df])
        time.sleep(5)

resDF.to_csv("hiphop_data.csv",index=False)

resDF=pd.read_csv("hiphop_data.csv")


import sys

def find_aug_path(g):
    q = [(None,'r')]
    visited = {}

    while len(q) != 0:
        arc, node = q.pop(0)
        visited[node] = arc
        if node == 's':
            return visited
        for head in g[node]:
            if not head in visited:
                cap, flow = g[node][head]
                if flow < cap:
                    q.append(((node,head),head))
        for tail in g:
            if node in tail and not tail in visited:
                cap, flow = g[tail][node]
                if flow > 0:
                    q.append(((tail,node),tail))
    return visited

def find_flow(g):

    while 1:
        visited = find_aug_path(g)
        cur = 's'
        if not 's' in visited:
            break

        aug_path = []
        width = 999999
        while 1:
            if visited[cur] == None:
                break
            (head, tail) = visited[cur]
            (cap,flow) = g[head][tail]
            if cur == head:
                cur = tail
                aug_path.insert(0,(-1,(head,tail)))
                width = min(width,flow)
            else:
                cur = head
                aug_path.insert(0,(1,(head,tail)))
                width = min(width, cap-flow)
        print("Corrected augmented path")
        for (mult, (head,tail)) in aug_path:
            print(head+tail, end=" ")
            (cap,flow) = g[head][tail]
            flow += mult * width
            g[head][tail] = (cap,flow)
        print('width:', width)
    print("Final flow:")
    for head in g:
        for tail in g[head]:
            (cap,flow) = g[head][tail]
            print(head+tail, 'u:', cap, "x:", flow )

g = {
    'r' : {'p' : (6,0), 'a' : (9,0), 'q' : (4,0)},
    'p' : {'q' : (2,0), 'b' : (3,0)},
    'q' : {'p' : (1,0), 'b' : (2,0), 'd' : (6,0)},
    'a' : {'c': (8,0), 'd' : (1,0)},
    'b' : {'a' : (1,0), 's' : (8,0)},
    'c' : {'b' : (2,0), 's' : (4,0) , 'q' : (1,0)},
    'd' : {'c' : (1,0), 's' : (6,0)}
}

find_flow(g)


import sys
import modules_list as ml

# Set the system path to get our code modules
sys.path.append('program/')
import check_jvm as cj
import check_service as cs
import check_firewall as cf
import operate_module as om
import super_stdio as super

def main():
    service_list = ["kibana","elasticsearch"] # Service you want to check
    modules_list = ml.modules_list # Modules you have
    modules_missing = []     # Use this list to save those missing modules
    modules = []             # Use this list to save those modules you want to process
    modules_path = "modules/"

    # Check whether your service is up; if not, help the user to open the service
    super.super_print("\n1) Check the service status")
    for service in service_list:

        cs.check_service(service)

    # Check whether the jvm size is enough; if not, send the warning message
    super.super_print("\n2) Check the jvm")
    cj.check_jvm_memory()

    # Check whether the firewall port number is open.
    super.super_print("\n3) Check the firewall")
    cf.check_firewall_port()

    # Check which module the user want to install, then help them to start up modules.
    super.super_print("\n4) Check the module")
    while True:
        modules = om.modules_generate(modules_list)
        super.super_print("The modules you choose is : \033[1;33;40m%s\033[0;37;40m" % (" ".join(modules)))
        for module in modules:
            module_setting = om.check_module(module = module,modules_path = modules_path)
            if module_setting == False:
                modules_missing.append(module)
        om.modules_info(modules = modules, not_exist_module_list = modules_missing)
        add_more = super.super_raw_input("Do you want to add more modules?If 'yes', please enter 'y' or 'yes': ")
        if (add_more != "y") and (add_more != "yes"):
            break

if __name__ == "__main__":
    main()


#!/usr/bin/env python3
data = open("input.txt", "r").read().split("\n\n")

max_calories = sum([int(c) if c else 0 for c in data[0].split('\n')])
max_elf = 0
for elf, calories in enumerate(data[1:], start=1):
  calorie_sum = sum([int(c) if c else 0 for c in calories.split('\n')])
  # print(elf, calorie_sum)
  if calorie_sum > max_calories:
    max_calories = calorie_sum
    max_elf = elf

print(f"Max elf:{max_elf}, max calories: {max_calories}")


import re

from server.service.helper.dict_helper import get_by_path
from server.service.helper.list_helper import format_list_to_string
from server.service.slack.sdk_helper import get_user_info

def format_custom_command_message(
    user_id: str, selected_items: list[str], label: str
) -> str:
    selected_items_msg = format_list_to_string(selected_items)
    return f"Hey ! {format_mention_user(user_id)} choose {selected_items_msg} {label}"

def format_mention_user(user_id: str) -> str:
    if not user_id:
        return "<@user>"
    return f"<@{user_id}>"

def get_user_id_from_mention(text: str) -> str:
    if not text:
        return text
    return re.sub(r"<@(U[A-Z0-9]*)(\|.*)*>", r"\1", text)

def format_new_command_message(
    *,
    command_name: str,
    team_id: str,
    command_description: str,
    pick_list: list[str],
    current_user_id: str,
):
    current_user_name = get_name_from_user(
        get_user_info(team_id=team_id, user_id=current_user_id),
    )
    message = f"{current_user_name} created *{command_name}*."
    if command_description:
        message += f"\n{command_description}"

    message += "\nItems in the pick list are: "

    message += format_pick_list(pick_list, team_id) + "."
    return message

def format_updated_fields_mesage(
    *,
    command_name: str,
    team_id: str,
    fields_updated: dict[str, any],
    current_user_id: str,
) -> str:
    current_user_name = get_name_from_user(
        get_user_info(team_id=team_id, user_id=current_user_id),
    )
    message = f"{current_user_name} updated *{command_name}*."

    messages_for_each_field = [
        ("name", (lambda name: f"Command name changed to {name}.")),
        (
            "added_to_pick_list",
            (
                lambda added_to_pick_list: f"New items added: {format_pick_list(added_to_pick_list, team_id)}."  # noqa E501
            ),
        ),
        (
            "removed_from_pick_list",
            lambda removed_from_pick_list: f"Items removed: {format_pick_list(removed_from_pick_list, team_id)}.",  # noqa E501
        ),
        (
            "description",
            (
                lambda description: f"Command description changed to: {description}."
                if description
                else "Command description removed."
            ),
        ),
        (
            "label",
            (
                lambda label: f'Command message changed to: {format_custom_command_message(None, ["<selected_item>"], label)}'  # noqa E501
            ),
        ),
        (
            "self_exclude",
            (
                lambda self_exclude: f'User running the command is now {"excluded" if self_exclude else "included"} in the pick.'  # noqa E501
            ),
        ),
        (
            "only_active_users",
            (
                lambda only_active_users: "Only active users are now picked."
                if only_active_users
                else "All users are now picked (whether or not they are active)."
            ),
        ),
        ("strategy", (lambda strategy: f"Strategy changed to {strategy}.")),
    ]

    for (field, message_function) in messages_for_each_field:
        if fields_updated.get(field) is not None:
            message += f"\n• {message_function(fields_updated.get(field))}"

    return message

def format_clean_deleted_users_message(*, current_user_id: str, team_id: str):
    current_user_name = get_name_from_user(
        get_user_info(team_id=team_id, user_id=current_user_id),
    )
    return (
        f"{current_user_name} cleaned up the deleted users from the pick lists :broom:"
    )

def format_no_deleted_users_to_clean_message():
    return "No deleted users found. All pick commands are up to date."

def format_pick_list(pick_list: list[str], team_id: str):
    labels = extract_label_from_pick_list(pick_list, team_id=team_id)
    return format_list_to_string(labels)

def is_a_user(item: str) -> bool:
    return item is not None and item.startswith("<@U") and item.endswith(">")

def get_name_from_user(user: dict[str, any]) -> str:
    possible_path_names = [
        "profile.display_name",
        "profile.display_name_normalized",
        "profile.real_name",
        "profile.real_name_normalized",
        "real_name",
    ]

    for possible_path_name in possible_path_names:
        name = get_by_path(user, possible_path_name)
        if name:
            return name.capitalize()

    return None

def extract_label_from_pick_list(pick_list: list[str], *, team_id: str) -> list[str]:
    if pick_list is None:
        return []
    return [
        transform_pick_list_item_to_label(item, team_id=team_id) for item in pick_list
    ]

def transform_pick_list_item_to_label(item: str, *, team_id: str) -> str:
    if is_a_user(item):
        user_name = get_name_from_user(
            get_user_info(team_id=team_id, user_id=get_user_id_from_mention(item))
        )
        return user_name

    return item


from math import floor

f_words_per_line = lambda words_in_lines, num_lines: floor(words_in_lines/num_lines)
f_lines_per_page = lambda lines_in_pages, num_pages: floor(lines_in_pages/num_pages)

def avg_words_per_page(
    words_in_lines,
    lines_in_pages,
    num_lines=5,
    num_pages=5
):
    """
    Args:
        words_in_lines (int): total number of words counted on
                              `num_lines` lines
        lines_in_page (int): total number of lines counted on
                             `num_pages` pages
        num_lines (int): number of lines words were counted for
                         (default 5)
        num_pages (int): number of pages lines were counted for
                         (default 5)
    """
    wpl = f_words_per_line(words_in_lines, num_lines)
    lpp = f_lines_per_page(lines_in_pages, num_pages)
    return wpl * lpp

def words_per_minute(
    lines_read,
    words_per_line,
    minutes_spent=3
):
    """
    Args:
        lines_read (int): number of lines read in `minutes_spent` minutes
        words_per_line (int): words per line in the test book
        minutes_spent (int): how many minutes you've spent reading
    """
    return floor(lines_read * words_per_line / minutes_spent)

def time_per_line(
    desired_wpm,
    words_per_line
):
    """
    Args:
        desired_wpm (int): the target words-per-minute value
                           you wish to achieve
        words_per_line (int): how many words per line your
                              test book contains on average
    Returns:
        seconds
    """
    # words_per_line * 1 min / 3 * desired_wpm
    minute_fraction = words_per_line/(3 * desired_wpm)
    return minute_fraction * 60


# -*-coding:utf-8-*-
"""
# File       : test_meter_profile.py
# Time       ：2021/12/21 14:18
# Author     ：cao jiann
# version    ：python 3.7
"""

import datetime

from common.HESRequest import *
from common.marker import *

class Test_Meter_Relay_Status:

    @smokeTest
    def test_read_relay_status_sync(self, requestMessage, caseData, device):
        """
        使用同步读取的方式去对电表进行读取闸状态
         """
        data = caseData('testData/RelayControlTask/read_RelayControlStatus.json')
        requestData = data['ReadRelayControlStatusSync']['request']
        requestData['payload'][0]['deviceNo'] = device['device_number']
        transactionId = str(device['device_number']) + '_' + time.strftime('%y%m%d%H%M%S', time.localtime())
        requestData['payload'][0]['transactionId'] = transactionId
        response, elapsed = HESRequest().post(url=requestMessage, params=requestData)
        print('Request ---  ', response)
        assert '636F6E6E6563746564' in str(response) or '646973636F6E6E6563746564' in str(response)

    @asyncTest
    def test_read_relay_status_async(self, device, requestMessage, dbConnect, caseData):
        """
        使用异步读取的方式去对电表进行读取闸状态
         """
        count = 0
        print("Step 1 : 生成异步操作读取任务，hes-api异步执行，生成running表")
        data = caseData('testData/RelayControlTask/read_RelayControlStatus.json')
        requestData = data['ReadRelayControlStatusAsync']['request']
        # 设定三分钟异步任务，三分钟后失效
        currentTime = datetime.datetime.now().strftime('%y%m%d%H%M%S')
        endTime = (datetime.datetime.now() + datetime.timedelta(minutes=3)).strftime('%y%m%d%H%M%S')

        requestData['payload'][0]['startTime'] = currentTime
        requestData['payload'][0]['endTime'] = endTime
        requestData['payload'][0]['deviceNo'] = device['device_number']
        transactionId = str(device['device_number']) + '_' + time.strftime('%y%m%d%H%M%S', time.localtime())
        requestData['payload'][0]['transactionId'] = transactionId
        response = requests.post(url=requestMessage, json=requestData)
        assert response.status_code == 200

        print("Step 2 : 生成异步任务后，任务正常执行完毕进入his，进入his表，则认为任务结束")
        # 过期时间到，也会进入his表，这里暂不考虑
        time.sleep(3)
        # 查询生成Core执行的任务的 auto_run_id
        sql_running = "select auto_run_id from H_TASK_RUNNING where NODE_NO='{}' and JOB_TYPE='GET_COMMON_PARAM'".format(
            device['device_number'])
        db_queue = dbConnect.fetchall_dict(sql_running)
        while len(db_queue) == 0 and count < 2:
            time.sleep(3)
            db_queue = dbConnect.fetchall_dict(sql_running)
            print(db_queue)
            print('Waiting for read relay_control Tasks to Create...')
            count = count + 1

        print("Step 3 : 验证读任务执行结束，task正常移入his表")
        # 查询生成Core执行结束后，his表任务状态
        sql_his = "select task_state from H_TASK_RUN_HIS where auto_run_id='{}'".format(db_queue[0]['auto_run_id'])
        db_queue = dbConnect.fetchall_dict(sql_his)
        while len(db_queue) == 0 and count < 50:
            time.sleep(5)
            db_queue = dbConnect.fetchall_dict(sql_his)
            print(db_queue)
            print('Waiting for read relay_control Tasks to finish...')
            count = count + 1
        assert db_queue[0]['task_state'] == 3


from tkinter import colorchooser
from tkinter import ttk
from typing import Optional, Any

import customtkinter as ct
import networkx as nx
import seaborn as sns
from matplotlib import pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

from facadeController import FacadeController

class PlotFrame(ct.CTkToplevel):
    """This class is responsible for creating the plot frame."""

    def __init__(self, *args, **kwargs):
        super().__init__(*args)

        # Set the window
        self.geometry("1300x700+70+50")
        self.resizable(False, False)

        # Set the attributes
        self._facade: FacadeController = FacadeController()
        self.figure: Any = None
        self.options: dict = {}
        self.df: Any = self.facade.get_df()

        # Set the widgets
        self.canvas: Optional[FigureCanvasTkAgg] = None
        self.tree: Optional[ttk.Treeview] = None
        self.entry: Optional[ct.CTkEntry] = None
        self.prop_frame: Optional[ct.CTkFrame] = None
        self.graph_frame: Optional[ct.CTkFrame] = None
        self.option_frame: Optional[ct.CTkFrame] = None
        self.opt_2: ct.StringVar = ct.StringVar(value="Plot Type")
        self.opt_3: ct.StringVar = ct.StringVar(value="Value")
        self.opt_4: ct.StringVar = ct.StringVar(value="Value 2")

        # Set the tabs
        self.tab_view = ct.CTkTabview(self, height=660, width=1260)
        self.tab_view.grid(row=0, column=0, padx=20, pady=20, sticky="nsew")
        self.tab_view.add("DataFrame")
        self.create_tree()

        # Set a condition to create the plot tab (Visualize only)
        if kwargs.get("page_num", 2) == 2:
            self.tab_view.add("Plot")
            self.create_opt_frame()
            self.create_graph_frame()
            self.create_prop_frame()

        # add widgets on tabs
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)
        self.facade.create_btn(self, text="Close", row=1, column=0,
                               command=self.destroy, size=10, y_pad=(0, 10))

    @property
    def facade(self) -> FacadeController:
        """FacadeController"""
        return self._facade

    def create_tree(self) -> None:
        """This method is responsible for creating the treeview."""

        self.init_tree()
        text = ct.StringVar(value="Insert column")
        frame = ct.CTkFrame(self.tab_view.tab("DataFrame"), height=90,
                            width=550)
        frame.grid_columnconfigure(0, weight=1)
        frame.grid(row=1, column=0, padx=20, pady=20, sticky="nsew")

        self.facade.create_text_lbl(frame,
                                    text="Query (Optional)", row=0,
                                    column=0, size=15, y_pad=5)
        self.entry = ct.CTkEntry(frame, width=350)

        self.entry.insert(0, "ISO in ['ISL','SWE'] or 0.05 > GII > 0.01")
        self.entry.grid(row=1, column=0, padx=10, pady=2)

        self.facade.create_opt_menu(frame, variable=text,
                                    command=lambda x: self.entry.insert(ct.END,
                                                                        x),
                                    value=self.facade.get_df().columns, row=1,
                                    column=1, y_pad=10)

        self.facade.create_btn(frame, text="Confirm",
                               row=1, column=2, command=lambda: self.init_tree(
                self.entry.get()),
                               size=15, y_pad=10)
        self.facade.create_btn(frame, text="Reset", row=1, column=3,
                               command=lambda: self.entry.delete(0, ct.END),
                               size=15, y_pad=10)

    def init_tree(self, event: str = None) -> None:
        """This method is responsible for initializing the treeview."""

        style = ttk.Style()
        columns = None
        rows = None

        # set style according to the appearance mode
        if ct.get_appearance_mode() == "Dark":
            style.configure("Treeview", background="Black", foreground="white",
                            fieldbackground="#C47AFF")
            style.map("Treeview", background=[("selected", "#C47AFF")])
        else:
            style.configure("Treeview", background="White", foreground="black",
                            fieldbackground="#E80F88")
            style.map("Treeview", background=[("selected", "#E80F88")])

        # get columns and rows
        try:
            columns = tuple(self.facade.get_df(event).columns)
            rows = tuple(self.facade.get_df(event).values.tolist())
        except SyntaxError:
            self.entry.delete(0, ct.END)
            self.entry.insert(0, "Invalid input, try again")
            self.after(1200, self.update_entry)

        self.tree = ttk.Treeview(self.tab_view.tab("DataFrame"),
                                 columns=columns,
                                 show='headings', height=20)

        scrollbar = ttk.Scrollbar(self.tab_view.tab("DataFrame"),
                                  orient="vertical",
                                  command=self.tree.yview)
        self.tree.configure(yscrollcommand=scrollbar.set)

        # set column headings
        for column in columns:
            self.tree.heading(column, text=column)
            self.tree.column(column, width=98, anchor="center")

        self.tree.grid(row=0, column=0, padx=20, pady=20, sticky="nsew")
        scrollbar.grid(row=0, column=1, sticky="ns")

        # insert values into a tree
        for row in rows:
            self.tree.insert(parent='', index=0, values=row)

    def update_entry(self) -> None:
        """This method is responsible for updating the entry widget."""

        self.entry.delete(0, ct.END)
        self.entry.insert(0, "ISO in ['ISL','SWE'] or 0.05 > GII > 0.01")

    def opt_changed(self, value) -> None:
        """This method is responsible for changing the options in the"""

        self.options[value[0]] = value[1]
        self.reset_widget()
        if self.options["Graph"] == "Distribution":
            self.opt_3.set("Value")
            self.create_dist_opt()

        elif self.options["Graph"] == "Everyday":
            self.opt_3.set("Value")
            self.create_everyday_opt()

        elif self.options["Graph"] == "Correlation":
            self.opt_3.set("Value 1")
            self.opt_4.set("Value 2")
            self.create_corr_opt()

        elif self.options["Graph"] == "Network":
            self.opt_3.set("Value")
            self.create_network_opt()

    def create_dist_opt(self, value=None) -> None:
        """This method is responsible for creating the options for the
        distribution graph."""

        self.plot_dis_graph()
        if value:
            self.options[value[0]] = value[1]
        if self.options.get("Graph") == "Distribution":
            self.facade.create_opt_menu(self.option_frame, variable=self.opt_2,
                                        command=lambda x: self.create_dist_opt(
                                            ("Plot", x)),
                                        value=self.facade.get_dis_plot(),
                                        row=2, column=0)

        if self.options.get("Plot") in self.facade.get_dis_plot():
            self.facade.create_opt_menu(self.option_frame, variable=self.opt_3,
                                        command=lambda x: self.create_dist_opt(
                                            ("Column", x)),
                                        value=self.facade.get_columns()[2:-1],
                                        row=4, column=0)

            self.create_prop_btn(command=self.plot_dis_graph)

    def create_everyday_opt(self, value=None) -> None:
        """This method is responsible for creating the options for the
        everyday graph."""

        self.plot_everyday_graph()
        if value:
            self.options[value[0]] = value[1]
        if self.options.get("Graph") == "Everyday":
            self.facade.create_opt_menu(self.option_frame, variable=self.opt_2,
                                        command=lambda
                                            x: self.create_everyday_opt(
                                            ("Plot", x)),
                                        value=self.facade.get_every_plot(),
                                        row=2, column=0)

        if self.options.get("Plot") in self.facade.get_every_plot():
            self.facade.create_opt_menu(self.option_frame, variable=self.opt_3,
                                        command=lambda
                                            x: self.create_everyday_opt(
                                            ("Column", x)),
                                        value=self.facade.get_columns()[2:-1],
                                        row=4,
                                        column=0)

            self.create_prop_btn(command=self.plot_everyday_graph)

    def create_corr_opt(self, value=None) -> None:
        """This method is responsible for creating the options for the
        correlation graph."""

        self.plot_corr_graph()
        if value:
            self.options[value[0]] = value[1]

        if self.options.get("Graph", "Correlation") == "Correlation":
            self.facade.create_opt_menu(self.option_frame, variable=self.opt_2,
                                        command=lambda
                                            x: self.create_corr_opt(
                                            ("Plot", x)),
                                        value=self.facade.get_corr_plot(),
                                        row=2, column=0)

        if self.options.get("Plot") in self.facade.get_corr_plot():
            self.facade.create_opt_menu(self.option_frame, variable=self.opt_3,
                                        command=lambda
                                            x: self.create_corr_opt(
                                            ("Column1", x)),
                                        value=self.facade.get_columns()[2:-1],
                                        row=4,
                                        column=0)
            self.facade.create_opt_menu(self.option_frame, variable=self.opt_4,
                                        command=lambda
                                            x: self.create_corr_opt(
                                            ("Column2", x)),
                                        value=self.facade.get_columns()[2:-1],
                                        row=6,
                                        column=0)

            self.create_prop_btn(command=self.plot_corr_graph)

    def create_network_opt(self, value=None) -> None:
        """This method is responsible for creating the options for the
        network graph."""

        self.plot_network_graph()
        if value:
            self.options[value[0]] = value[1]
        if self.options.get("Graph") == "Network":
            self.facade.create_opt_menu(self.option_frame, variable=self.opt_2,
                                        command=lambda
                                            x: self.create_network_opt(
                                            ("NPlot", x)),
                                        value=self.facade.get_network_plot(),
                                        row=2, column=0)

        if self.options.get("NPlot") in self.facade.get_network_plot():
            self.facade.create_opt_menu(self.option_frame, variable=self.opt_3,
                                        command=lambda
                                            x: self.create_network_opt(
                                            ("Column1", x)),
                                        value=self.facade.get_columns()[2:-1],
                                        row=4,
                                        column=0)

            layout = self.options.get("NPlot", "spring_layout")
            func = self.facade.get_network_func(layout)
            self.create_prop_btn(command=lambda: self.plot_network_graph(func))

    def plot_dis_graph(self) -> None:
        """This method is responsible for plotting the distribution graph."""

        if self.figure:  # Clear the figure if it is not empty
            self.figure.clf()

        colors = self.get_colors()
        sns.set_style("darkgrid")
        if self.options.get("Plot", "histplot") != "boxplot":
            plot = self.facade.get_plot_func(
                self.options.get("Plot", "histplot"))(
                data=self.df,
                x=self.df[self.options.get("Column", "GII")],
                hue='Human_development',
                palette=colors,
            )
        else:
            plot = self.facade.get_plot_func(
                self.options.get("Plot", "histplot"))(
                data=self.df,
                y=self.df[self.options.get("Column", "GII")],
                hue='Human_development',
                palette=colors,
            )
        plot.set(
            title=f"A graph show the frequency of {self.options.get('Column', 'GII').replace('_', ' ')}")
        self.figure = plot.get_figure()
        plot.set_xlabel(self.options.get('Column', 'GII'))
        plot.set_ylabel("Frequency")

        self.create_canvas()

    def plot_everyday_graph(self) -> None:
        """This method is responsible for plotting the everyday graph."""

        if self.figure:  # Clear the figure if it is not empty
            self.figure.clf()

        sns.set_style("darkgrid")
        fig = plt.figure()
        ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
        colors = self.get_colors()

        if self.options.get("Plot") == "stackbar":  # Plot the stacked bar plot
            # Calculate the average of values in each country
            # (In case that there are duplicated country)

            col_sum = self.df.groupby("ISO")[
                self.options.get("Column", "GII")].mean()

            # Select the top 10 columns based on the average of values
            top_10_cols = col_sum.sort_values(ascending=False)[
                          :10].index.tolist()

            # Create a pivot table with only the top 10 columns
            pivot_table = self.df.pivot_table(index="Human_development",
                                              columns="ISO",
                                              values=self.options.get("Column",
                                                                      "GII"),
                                              aggfunc="sum")[top_10_cols]

            # Plot the stacked bar plot
            ax = pivot_table.plot(kind="bar", stacked=True, figsize=(7, 5),
                                  )

            # Set the title and labels
            ax.set_title(
                f"A graph showing proportion of {self.options.get('Column', 'GII')} of Top 10 Human Development by ISO")
            ax.set_xlabel("Human Development")
            ax.set_ylabel("Proportion")
            ax.legend(loc='best')

        else:  # Plot the pie chart
            ax.pie(self.df.groupby("Human_development")[
                       self.options.get("Column", "GII")].agg("sum"),
                   labels=self.df.Human_development.unique(),
                   autopct='%1.1f%%',
                   startangle=90,
                   colors=colors)
            ax.axis('equal')
            ax.set_title(f'A graph show proportion of {self.options.get("Column", "GII")} of Human Development')

        self.figure = ax.get_figure()
        self.create_canvas()

    def plot_corr_graph(self) -> None:
        """This method is responsible for plotting the correlation graph."""

        if self.figure:  # Clear the figure if it is not empty
            self.figure.clf()

        sns.set_style("darkgrid")
        colors = self.get_colors()
        plot = self.facade.get_plot_func(self.options.get("Plot",
                                                          "scatterplot")) \
            (self.df, x=self.options.get("Column1", "GII"),
             y=self.options.get("Column2", "Rank"),
             hue="Human_development",
             palette=colors, )

        plot.set(title=f"A graph show the correlation of {self.options.get('Column1', 'GII')} and {self.options.get('Column2', 'Rank')}".replace('_', ' '))
        self.figure = plot.get_figure()
        plot.set_xlabel(self.options.get('Column1', 'GII'))
        plot.set_ylabel(self.options.get('Column2', 'Rank'))

        self.create_canvas()

    def plot_network_graph(self, layout=nx.circular_layout) -> None:
        """This method is responsible for plotting the network graph."""

        if self.figure:  # Clear the figure if it is not empty
            self.figure.clf()

        col = self.options.get("Column1", "GII")
        colors = self.get_colors()
        G2 = self.facade.get_network_graph(col, colors[0], colors[1],
                                           colors[2], colors[3])

        node_colors_list = [G2.nodes[n].get('color', 'black') for n in G2.nodes()]
        edge_width_list = [G2.edges[n]['weight'] / 50 for n in G2.edges()]
        edge_labels = nx.get_edge_attributes(G2, "weight")

        pos2 = layout(G2)
        nx.draw_networkx_nodes(G2, pos2, node_size=400,
                               node_color=node_colors_list)
        nx.draw_networkx_edges(G2, pos2, width=edge_width_list)
        nx.draw_networkx_labels(G2, pos2, font_size=15)
        nx.draw_networkx_edge_labels(G2, pos2, edge_labels)
        plt.title(f"Random {col} and Human Development relation of 15 countries")
        self.create_canvas()

    def create_color_chooser(self, frame, row: int = 1, num: int = 0) -> None:
        """This method is responsible for creating the color chooser."""

        color = colorchooser.askcolor()

        if num == 0:  # Set the color of the first color chooser
            self.options["Color"] = color[1]
            self.facade.create_text_lbl(frame,
                                        text=self.facade.get_current_color(
                                            color), size=18, row=row, column=0,
                                        y_pad=0, text_color=color[1])

        else:  # Set the color of the second color chooser
            self.options[f"Color{num}"] = color[1]
            self.facade.create_text_lbl(frame,
                                        text=self.options.get(f"Color{num}",
                                                              ""), size=18,
                                        row=row, column=0, y_pad=0,
                                        text_color=color[1])

    def create_canvas(self) -> None:
        """This method is responsible for creating the canvas."""

        self.figure.set_facecolor("gray")
        self.canvas = FigureCanvasTkAgg(self.figure, self.graph_frame)
        self.canvas.draw()
        self.canvas.get_tk_widget().grid(row=0, column=0, padx=20, pady=20,
                                         sticky="nsew")

    def create_opt_frame(self) -> None:
        """This method is responsible for creating the option frame."""

        self.option_frame = ct.CTkFrame(self.tab_view.tab("Plot"), height=580,
                                        width=200)
        self.option_frame.grid(row=0, column=0, padx=(20, 0), pady=20,
                               sticky="nsw")

        self.facade.create_text_lbl(self.option_frame,
                                    text="Choose the graph type",
                                    size=15, row=0, column=0, x_pad=0,
                                    y_pad=20)
        opt_1 = ct.StringVar(value="Graph Type")
        self.facade.create_opt_menu(self.option_frame, variable=opt_1, row=1,
                                    command=lambda x: self.opt_changed(
                                        ("Graph", x)),
                                    value=["Distribution", "Everyday",
                                           "Correlation", "Network"])

    def create_graph_frame(self) -> None:
        """This method is responsible for creating the graph frame."""

        plt.figure(figsize=(7, 5))
        self.graph_frame = ct.CTkFrame(self.tab_view.tab("Plot"), height=580,
                                       width=800)
        self.graph_frame.grid(row=0, column=1, padx=5, pady=20, sticky="nsew")
        self.plot_dis_graph()

    def create_prop_frame(self) -> None:
        """This method is responsible for creating the property frame."""

        self.prop_frame = ct.CTkFrame(self.tab_view.tab("Plot"), height=580,
                                      width=200)
        self.prop_frame.grid(row=0, column=2, padx=(0, 20), pady=20,
                             sticky="nse")

    def create_prop_btn(self, command=None) -> None:
        """This method is responsible for creating the property button."""

        self.facade.create_btn(self.prop_frame, text="Pick color 1", row=0,
                               column=0, size=15,
                               command=lambda: self.create_color_chooser(
                                   self.prop_frame, row=1, num=1))
        self.facade.create_btn(self.prop_frame, text="Pick color 2", row=2,
                               column=0, size=15,
                               command=lambda: self.create_color_chooser(
                                   self.prop_frame, row=3, num=2))
        self.facade.create_btn(self.prop_frame, text="Pick color 3", row=4,
                               column=0, size=15,
                               command=lambda: self.create_color_chooser(
                                   self.prop_frame, row=5, num=3))
        self.facade.create_btn(self.prop_frame, text="Pick color 4", row=6,
                               column=0, size=15,
                               command=lambda: self.create_color_chooser(
                                   self.prop_frame, row=7, num=4))

        self.facade.create_btn(self.prop_frame, text="Plot", row=9,
                               column=0, command=command,
                               size=15)

    def get_colors(self) -> list:
        """This method is responsible for getting the colors."""

        color1 = self.options.get("Color1", "#39B5E0")
        color2 = self.options.get("Color2", "#FB2576")
        color3 = self.options.get("Color3", "#C9F4AA")
        color4 = self.options.get("Color4", "#F5EA5A")
        return [color1, color2, color3, color4]

    def reset_widget(self) -> None:
        """This method is responsible for resetting the widget."""

        for widget in self.option_frame.winfo_children()[2:]:
            widget.destroy()
        for widget in self.prop_frame.winfo_children():
            widget.destroy()


import logging

import numpy as np

from cn.ivker.ogeek.util.Dict import Dict

logger = logging.getLogger("Array")

class Array:
    @staticmethod
    def extract(data, selected):
        """
        按selected从data中取出相应的数据
        :param data:
        :param selected:
        :return:
        """
        if len(selected) > 0:
            return data[selected]
        else:
            return np.asarray([])

    @staticmethod
    def merge(x, y, choseX):
        """
        合并x,y数组,choseX为合并x的索引
        :param x:
        :param y:
        :param choseX:
        :return:
        """
        if x.shape < 1:
            return y
        r_size = x.shape[0] + y.shape[0]
        c_size = x.shape[1]
        res = np.zeros((r_size, c_size))
        current_x = 0
        current_y = 0

        # 当前位置选择x/y
        check = 0
        for idx in range(r_size):
            if idx == choseX[check]:
                res[idx] = (x[current_x])
                check += 1
                current_x += 1
            else:
                res[idx] = (y[current_y])
                current_y += 1
        return res

    @staticmethod
    def crossingIndex(a, b, a_dict=None, b_dict=None):
        """
        返回a,b两个数组的交集,差集所对应的数组下标
        :param a:
        :param b:
        :param a_dict:
        :param b_dict:
        :return:
        """
        if a_dict is None:
            a_dict = Dict.array2IndexDict(a)
        if b_dict is None:
            b_dict = Dict.array2IndexDict(b)
        # int:交集,diff:差集
        a_int_index = []
        a_diff_index = []
        b_int_index = []
        b_diff_index = []
        inter = set(a_dict) & set(b_dict)

        for i in a_dict:
            if i in inter:
                a_int_index.extend(a_dict[i])
            else:
                a_diff_index.extend(a_dict[i])
        for i in b_dict:
            if i in inter:
                b_int_index.extend(b_dict[i])
            else:
                b_diff_index.extend(b_dict[i])

        logger.info("集合A(%d条记录) 和 集合B(%d条记录) 的交集为 %d 条记录" % (len(a_dict), len(b_dict), len(inter)))
        a_int_index.sort()
        b_int_index.sort()
        return [np.asarray(a_int_index), np.asarray(a_diff_index),  np.asarray(b_int_index),
                np.asarray(b_diff_index)]


import urllib.request
from bs4 import BeautifulSoup
import json
import PostScrape

extractedRecords = []

def crawlPagesUpTo(pages):
    if int(pages) < 1:
        print("Invalid integer.")
        return
    headers = {'user-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.3'}
    print('Extracting from a total of %d links.'%(int(pages) * 25))
    for page in range(int(pages)):
        curr = "https://old.reddit.com/top/"
        if page != 0:
            curr += "?count=" + str(page * 25) + "&after=t3_" + str(getRecords()[-1]['tag'])
        request = urllib.request.Request(curr, headers=headers)
        html = urllib.request.urlopen(request).read()
        soup = BeautifulSoup(html, 'html.parser')
        siteTable = soup.find("div", attrs={'id':'siteTable'})
        links = siteTable.find_all("a", class_="title")
        commentLinks = siteTable.find_all("a", class_="bylink comments may-blank")
        for index in range(25):
            PostScrape.resetDetails()
            title = links[index].text
            url = links[index]['href']
            comments = commentLinks[index].text
            commenturl = commentLinks[index]['href']
            tag = commenturl.split('/')[6]
            ad = False
            print('%d. Extracting data from %s.'%((index + 1), commenturl))
            PostScrape.scrapeDetails(commenturl)
            postDetails = PostScrape.getDetails()
            if not url.startswith('http'):
                url = "https://old.reddit.com" + url
            if not commenturl.startswith('http'):
                commenturl = "https://old.reddit.com" + commenturl
            if url.startswith('https://alb.reddit.com') or url.startswith('http://alb.reddit.com'):
                ad = True
            record = {
                'title':title,
                'url':url,
                'comments':comments,
                'commenturl':commenturl,
                'tag':tag,
                'ad':ad,
                'postDetails':postDetails
            }
            extractedRecords.append(record)
        print('Finished extracting from a total of %d links.'%(int(pages) * 25))

def resetRecords():
    extractedRecords.clear()

def dumpRecords():
    with open('dumpCrawl.json', 'w') as outfile:
        json.dump(extractedRecords, outfile)

def getRecords():
    return extractedRecords

if __name__ == "__main__":
    crawlPagesUpTo(input("How many pages would you like to crawl? "))
    dumpRecords()


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

from flask import Flask
from validator import MyRequestValidator
from core import db, oauth
from views import yoloapi

def create_app(settings_override=None):
    """ Method for creating and initializing application.

        :param settings_override: Dictionary of settings to override.
    """
    app = Flask(__name__)

    # Update configuration.
    app.config.from_object('settings')
    app.config.from_pyfile('settings.cfg', silent=True)
    app.config.from_object(settings_override)

    # Initialize extensions on the application.
    db.init_app(app)
    oauth.init_app(app)
    oauth._validator = MyRequestValidator()

    # Register views on the application.
    app.register_blueprint(yoloapi)

    return app

if __name__ == '__main__':

    # Enable Flask-OAuthlib logging for this application.
    import logging
    logger = logging.getLogger('flask_oauthlib')
    logger.addHandler(logging.StreamHandler())
    logger.setLevel(logging.DEBUG)

    # Create app and SQL schemas in database, then run the application.
    app = create_app()
    db.create_all(app=app)
    app.run(ssl_context='adhoc')


from collections import deque

def bfs():
    while q1:
        x, y, cnt = q1.popleft()
        visited[x][y]=cnt

        for i in range(4):
            nx=x+dx[i]
            ny=y+dy[i]

            if 0<=nx<r and 0<=ny<c:
                if graph[nx][ny] == "X": continue  # 돌이면
                elif visited[nx][ny]==-1 and graph[nx][ny]!="D":
                    visited[nx][ny]=visited[x][y]+1
                    graph[nx][ny]="*"
                    q1.append((nx,ny,cnt+1))

def bfs2(x,y):
    global result
    visited[x][y] = 0

    while q2:
        x, y = q2.popleft()

        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]

            if 0<=nx<r and 0<=ny<c:
                if graph[nx][ny] == "X": continue
                elif graph[nx][ny]=="D": #비버 굴 도착
                    result=visited[x][y]+1
                    return #리턴 해주기
                else:
                    # "." , "*" 일때
                    if visited[nx][ny] != -1 and visited[nx][ny] <= visited[x][y] + 1:
                        continue
                    visited[nx][ny]=visited[x][y]+1
                    q2.append((nx,ny))

if __name__ == '__main__':
    r,c=map(int,input().split())
    graph=[]
    dx=[-1,1,0,0]
    dy=[0,0,-1,1]

    for i in range(r):
        graph.append(list(input().rstrip()))

    visited=[[-1 for _ in range(c)] for _ in range(r)]
    water=[]

    q1=deque()
    q2=deque()
    for i in range(r):
        for j in range(c):
            if graph[i][j]=="*":
                cnt=0
                q1.append((i,j,cnt))
            elif graph[i][j]=="S":
                cnt2=0
                p,q=i,j
                q2.append((i,j))

    result=-1
    bfs()
    bfs2(p,q)

    if result==-1:
        print("KAKTUS")
    else:
        print(result)


from models.RainbowModel import RainbowModel
import tensorflow as tf
import tensorflow.keras as keras
import utils.settings as settings

class ResNetModel(RainbowModel):

    def _create_model(self):
        n_feature_maps = 64

        window_size = settings.data_dimension_dict["window_size"]
        n_features = settings.data_dimension_dict["n_features"]
        n_classes = settings.data_dimension_dict["n_classes"]

        input_layer = keras.layers.Input((window_size, n_features))
        x = self._preprocessing_layer(input_layer)

        # BLOCK 1

        conv_x = keras.layers.Conv1D(
            filters=n_feature_maps, kernel_size=8, padding="same"
        )(x)
        conv_x = keras.layers.BatchNormalization()(conv_x)
        conv_x = keras.layers.Activation("relu")(conv_x)

        conv_y = keras.layers.Conv1D(
            filters=n_feature_maps, kernel_size=5, padding="same"
        )(conv_x)
        conv_y = keras.layers.BatchNormalization()(conv_y)
        conv_y = keras.layers.Activation("relu")(conv_y)

        conv_z = keras.layers.Conv1D(
            filters=n_feature_maps, kernel_size=3, padding="same"
        )(conv_y)
        conv_z = keras.layers.BatchNormalization()(conv_z)

        # expand channels for the sum
        shortcut_y = keras.layers.Conv1D(
            filters=n_feature_maps, kernel_size=1, padding="same"
        )(x)
        shortcut_y = keras.layers.BatchNormalization()(shortcut_y)

        output_block_1 = keras.layers.add([shortcut_y, conv_z])
        output_block_1 = keras.layers.Activation("relu")(output_block_1)

        # BLOCK 2

        conv_x = keras.layers.Conv1D(
            filters=n_feature_maps * 2, kernel_size=8, padding="same"
        )(output_block_1)
        conv_x = keras.layers.BatchNormalization()(conv_x)
        conv_x = keras.layers.Activation("relu")(conv_x)

        conv_y = keras.layers.Conv1D(
            filters=n_feature_maps * 2, kernel_size=5, padding="same"
        )(conv_x)
        conv_y = keras.layers.BatchNormalization()(conv_y)
        conv_y = keras.layers.Activation("relu")(conv_y)

        conv_z = keras.layers.Conv1D(
            filters=n_feature_maps * 2, kernel_size=3, padding="same"
        )(conv_y)
        conv_z = keras.layers.BatchNormalization()(conv_z)

        # expand channels for the sum
        shortcut_y = keras.layers.Conv1D(
            filters=n_feature_maps * 2, kernel_size=1, padding="same"
        )(output_block_1)
        shortcut_y = keras.layers.BatchNormalization()(shortcut_y)

        output_block_2 = keras.layers.add([shortcut_y, conv_z])
        output_block_2 = keras.layers.Activation("relu")(output_block_2)

        # BLOCK 3

        conv_x = keras.layers.Conv1D(
            filters=n_feature_maps * 2, kernel_size=8, padding="same"
        )(output_block_2)
        conv_x = keras.layers.BatchNormalization()(conv_x)
        conv_x = keras.layers.Activation("relu")(conv_x)

        conv_y = keras.layers.Conv1D(
            filters=n_feature_maps * 2, kernel_size=5, padding="same"
        )(conv_x)
        conv_y = keras.layers.BatchNormalization()(conv_y)
        conv_y = keras.layers.Activation("relu")(conv_y)

        conv_z = keras.layers.Conv1D(
            filters=n_feature_maps * 2, kernel_size=3, padding="same"
        )(conv_y)
        conv_z = keras.layers.BatchNormalization()(conv_z)

        # no need to expand channels because they are equal
        shortcut_y = keras.layers.BatchNormalization()(output_block_2)

        output_block_3 = keras.layers.add([shortcut_y, conv_z])
        output_block_3 = keras.layers.Activation("relu")(output_block_3)

        # FINAL

        gap_layer = keras.layers.GlobalAveragePooling1D()(output_block_3)

        output_layer = keras.layers.Dense(n_classes, activation="softmax")(
            gap_layer
        )

        model = keras.models.Model(inputs=input_layer, outputs=output_layer)

        model.compile(
            loss=tf.keras.losses.CategoricalCrossentropy(),
            optimizer=keras.optimizers.Adam(),
            metrics=["accuracy"]
            )

        self.model = model
        return model


from flask import Flask, request, jsonify
from flask_cors import CORS, cross_origin
import base64
import replicate
import io
import openai
import os

app = Flask(__name__)
cors = CORS(app)

openai.api_key = os.getenv('OPENAIKEY')

@app.route("/process", methods=['POST'])
@cross_origin()
def process():
    picture_data = request.json['picture']
    picture_bytes = base64.b64decode(picture_data.split(',')[1])
    image_file = io.BytesIO(picture_bytes)
    output = replicate.run(
                "salesforce/blip:2e1dddc8621f72155f24cf2e0adbde548458d3cab9f00c0139eea840d0ac4746",
                input={
                    "image": image_file,
                    "task": "visual_question_answering",
                    "question": "What item is this?"
                    }
            )

    remove_caption = output.split(':')[1]
    prompt = create_prompt(remove_caption)
    answer = get_response(prompt)
    return jsonify({"answer": answer})

def create_prompt(message):
    return ("Please provide a a super short, succinct, useful explaination while avoiding generalizations and summaries. How do I recycle: "
                + message
            )

def get_response(message):
    response = openai.ChatCompletion.create(
        model = 'gpt-3.5-turbo',
        temperature = 1,
        messages = [
                {"role": "user", "content": message}
                ]
    )
    rv = response.choices[0]["message"]["content"]
    return rv

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


import seqio
import torch
import tensorflow as tf
from polytax.data import dataset, listops, tasks  # pylint: disable=unused-import
from polytax.data.dataset import IterableDataset, MapDataset

""" BUILD TASKS AND DATASETS """

def build_seqio_dataset(task, sequence_length, split, seed=1, pack=False):
    dataset = seqio.get_dataset(
        task.name,
        task_feature_lengths=sequence_length,
        dataset_split=split,
        use_cached=False,
        shuffle=True,
        seed=seed,
        feature_converter=seqio.EncDecFeatureConverter(pack=pack),
    )
    return dataset

def build_dataset(dataset, batch_size, GLOBAL_RANK, NUM_SHARDS, use_iterable_ds=True, drop_remainder=False):
    """Builds an iterable dataset."""
    dataset = dataset.shard(num_shards=NUM_SHARDS, index=GLOBAL_RANK)
    print("building dataset.")
    if use_iterable_ds:
        tf_dataset = dataset.batch(batch_size, drop_remainder=drop_remainder).prefetch(tf.data.AUTOTUNE)
        dataset = IterableDataset(tf_dataset.as_numpy_iterator())
    else:
        dataset = dataset.prefetch(tf.data.AUTOTUNE).as_numpy_iterator()
        dataset = MapDataset(dataset)
    return tf_dataset, dataset

def maptensorto(maptensor, device):
    toreturn = {}
    for key, value in maptensor.items():
        toreturn[key] = value.to(device)
    return toreturn

def slicetensorto(slicetensor, slice_idx, num_slices, device):
    toreturn = {}
    for key, tensor in slicetensor.items():
        start_idx = slice_idx * tensor.shape[0] // num_slices
        end_idx = (slice_idx + 1) * tensor.shape[0] // num_slices
        toreturn[key] = tensor[start_idx:end_idx].to(device)
    return toreturn