#!/usr/bin/env python3

import csv
import os
import sys

if __name__ == '__main__':
    task_duration_file, prod_tfrm_file, report_file = sys.argv[1:4]
    # task_duration_file = 'TaskDuration.txt'
    # prod_tfrm_file = 'prod_tfrm.csv'
    # report_file = 'report.csv'
    work_dir = os.getcwd()

    with open(os.path.join(work_dir, task_duration_file)) as f_task, \
        open(os.path.join(work_dir, prod_tfrm_file)) as f_prod, \
        open(os.path.join(work_dir, report_file), 'w') as f_report:
        prod_reader = csv.reader(f_prod)
        task_reader = csv.reader(f_task, delimiter='|')
        report_writer = csv.writer(f_report)

        task_duration_lookup = {}
        header = next(task_reader)
        for record in task_reader:
            _key, *_value = record
            task_duration_lookup[_key.strip()] = _value

        report_writer.writerow(header + ['prod_start', 'prod_end', 'duration'])
        for record in prod_reader:
            _key = record[0].split('.', 1)[1]
            if 'simulation' in _key:
                _key = '.'.join((_key.split('.', 1)[0], 'sensitivity'))
            else:
                _key = _key.replace('extract', 'submitBatchToGrid_extract')
            _task_duration = task_duration_lookup[_key]
            report_writer.writerow(record + _task_duration)


import asyncio

from functools import partial

class C:
    def __init__(self, *args, **kwargs):
        self.fn = args[0]
        self.args = args[1:]
        self.kwargs = kwargs

    async def __aenter__(self):
        resp = await self.fn(*self.args, **self.kwargs)

        return resp

    async def __aexit__(self, exc_type, exc, tb):
        pass

class AsyncTask:

    def __init__(self, loop=None):
        self._loop = loop or asyncio.get_event_loop()
        self.pending_tasks = asyncio.PriorityQueue(loop=self._loop)
        self.all_tasks = dict()
        self.running_tasks = dict()

    # --------------------------------------------------------------------------
    # Decorator and helpers
    # --------------------------------------------------------------------------
    def accepts(*types):
        def check_accepts(f):
            assert len(types) == f.func_code.co_argcount

            def new_f(*args, **kwds):
                for (a, t) in zip(args, types):
                    assert isinstance(a, t), \
                        "arg %r does not match %s" % (a, t)
                return f(*args, **kwds)

            new_f.func_name = f.func_name
            return new_f

        return check_accepts

    def task(self):
        """Decorator"""

        def real_decorator(f):

            # Real call to funcion
            def new_f(*args, **kwargs):
                return f(*args, **kwargs)

            # if function is a coro, add some new functions
            if asyncio.iscoroutinefunction(f):
                new_f.delay = partial(self._delay, new_f)
                new_f.wait = partial(C, new_f)

            return new_f
        return real_decorator

    # --------------------------------------------------------------------------
    # Extra functions
    # --------------------------------------------------------------------------
    async def _delay(self, function, *args, **kwargs):
        await self.pending_tasks.put((function, args, kwargs))

    # --------------------------------------------------------------------------
    # Waiting
    # --------------------------------------------------------------------------
    async def wait(self, timeout=10):
        """
        :param timeout: Time in seconds
        :type timeout: int
        """
        TIME_STEP = 0.01

        _current_timeout = timeout

        while (not self.pending_tasks.empty() or self.running_tasks) \
                and _current_timeout > 0:

            await asyncio.sleep(TIME_STEP, loop=self._loop)

            _current_timeout -= TIME_STEP

    # --------------------------------------------------------------------------
    # Stopping
    # --------------------------------------------------------------------------
    async def async_stop(self):

        # Remove all pending coroutines
        while self.pending_tasks.qsize() > 0:
            await self.pending_tasks.get()

        # Cancel all running tasks
        for t in self.all_tasks.values():
            try:
                t.cancel()
            except asyncio.CancelledError:
                pass

    def stop(self):
        for task in self.all_tasks.values():
            task.cancel()

    # --------------------------------------------------------------------------
    # Running Tasks functions
    # --------------------------------------------------------------------------
    def _done_task(self, task_id, future):
        self.all_tasks.pop(task_id)
        self.running_tasks.pop(task_id)

    async def _run(self):
        """Async run"""

        while True:
            fn, args, kwargs = await self.pending_tasks.get()

            # Build task
            task = self._loop.create_task(fn(*args, **kwargs))

            # Build stop task
            done_fn = partial(self._done_task, id(task))

            task.add_done_callback(done_fn)

            self.all_tasks[id(task)] = task
            self.running_tasks[id(task)] = task

    def run(self):
        """Blocking run"""
        # Launch background tasks
        run_task = self._loop.create_task(self._run())

        self.all_tasks[id(run_task)] = run_task

loop = asyncio.get_event_loop()
loop.set_debug(True)

manager = AsyncTask(loop=loop)

# --------------------------------------------------------------------------
# Tasks
# --------------------------------------------------------------------------
@manager.task()
async def task_01(num):
    print("Task 01 starting: {}".format(num))

    await asyncio.sleep(2, loop=loop)

    print("Task 01 stopping")

async def main_async():
    manager.run()

    # await task_01.delay(1)

    # async with task_01.wait() as f:
    async with task_01.wait(1) as f:
        print(f)

    # await asyncio.sleep(10)

    # return
    await manager.wait(5)

    manager.stop()
    # await manager.async_stop()

# --------------------------------------------------------------------------
# Main
# --------------------------------------------------------------------------
def main():
    loop.run_until_complete(main_async())

if __name__ == '__main__':
    main()


from io import BytesIO

from pyrogram.raw.core.primitives import Int, Long, Int128, Int256, Bool, Bytes, String, Double, Vector
from pyrogram.raw.core import TLObject
from pyrogram import raw
from typing import List, Optional, Any

# # # # # # # # # # # # # # # # # # # # # # # #
#               !!! WARNING !!!               #
#          This is a generated file!          #
# All changes made in this file will be lost! #
# # # # # # # # # # # # # # # # # # # # # # # #

class ChatInviteExported(TLObject):  # type: ignore
    """Telegram API type.

    Constructor of :obj:`~pyrogram.raw.base.ExportedChatInvite`.

    Details:
        - Layer: ``151``
        - ID: ``AB4A819``

    Parameters:
        link (``str``):
            N/A

        admin_id (``int`` ``64-bit``):
            N/A

        date (``int`` ``32-bit``):
            N/A

        revoked (``bool``, *optional*):
            N/A

        permanent (``bool``, *optional*):
            N/A

        request_needed (``bool``, *optional*):
            N/A

        start_date (``int`` ``32-bit``, *optional*):
            N/A

        expire_date (``int`` ``32-bit``, *optional*):
            N/A

        usage_limit (``int`` ``32-bit``, *optional*):
            N/A

        usage (``int`` ``32-bit``, *optional*):
            N/A

        requested (``int`` ``32-bit``, *optional*):
            N/A

        title (``str``, *optional*):
            N/A

    Functions:
        This object can be returned by 1 function.

        .. currentmodule:: pyrogram.raw.functions

        .. autosummary::
            :nosignatures:

            messages.ExportChatInvite
    """

    __slots__: List[str] = ["link", "admin_id", "date", "revoked", "permanent", "request_needed", "start_date", "expire_date", "usage_limit", "usage", "requested", "title"]

    ID = 0xab4a819
    QUALNAME = "types.ChatInviteExported"

    def __init__(self, *, link: str, admin_id: int, date: int, revoked: Optional[bool] = None, permanent: Optional[bool] = None, request_needed: Optional[bool] = None, start_date: Optional[int] = None, expire_date: Optional[int] = None, usage_limit: Optional[int] = None, usage: Optional[int] = None, requested: Optional[int] = None, title: Optional[str] = None) -> None:
        self.link = link  # string
        self.admin_id = admin_id  # long
        self.date = date  # int
        self.revoked = revoked  # flags.0?true
        self.permanent = permanent  # flags.5?true
        self.request_needed = request_needed  # flags.6?true
        self.start_date = start_date  # flags.4?int
        self.expire_date = expire_date  # flags.1?int
        self.usage_limit = usage_limit  # flags.2?int
        self.usage = usage  # flags.3?int
        self.requested = requested  # flags.7?int
        self.title = title  # flags.8?string

    @staticmethod
    def read(b: BytesIO, *args: Any) -> "ChatInviteExported":

        flags = Int.read(b)

        revoked = True if flags & (1 << 0) else False
        permanent = True if flags & (1 << 5) else False
        request_needed = True if flags & (1 << 6) else False
        link = String.read(b)

        admin_id = Long.read(b)

        date = Int.read(b)

        start_date = Int.read(b) if flags & (1 << 4) else None
        expire_date = Int.read(b) if flags & (1 << 1) else None
        usage_limit = Int.read(b) if flags & (1 << 2) else None
        usage = Int.read(b) if flags & (1 << 3) else None
        requested = Int.read(b) if flags & (1 << 7) else None
        title = String.read(b) if flags & (1 << 8) else None
        return ChatInviteExported(link=link, admin_id=admin_id, date=date, revoked=revoked, permanent=permanent, request_needed=request_needed, start_date=start_date, expire_date=expire_date, usage_limit=usage_limit, usage=usage, requested=requested, title=title)

    def write(self, *args) -> bytes:
        b = BytesIO()
        b.write(Int(self.ID, False))

        flags = 0
        flags |= (1 << 0) if self.revoked else 0
        flags |= (1 << 5) if self.permanent else 0
        flags |= (1 << 6) if self.request_needed else 0
        flags |= (1 << 4) if self.start_date is not None else 0
        flags |= (1 << 1) if self.expire_date is not None else 0
        flags |= (1 << 2) if self.usage_limit is not None else 0
        flags |= (1 << 3) if self.usage is not None else 0
        flags |= (1 << 7) if self.requested is not None else 0
        flags |= (1 << 8) if self.title is not None else 0
        b.write(Int(flags))

        b.write(String(self.link))

        b.write(Long(self.admin_id))

        b.write(Int(self.date))

        if self.start_date is not None:
            b.write(Int(self.start_date))

        if self.expire_date is not None:
            b.write(Int(self.expire_date))

        if self.usage_limit is not None:
            b.write(Int(self.usage_limit))

        if self.usage is not None:
            b.write(Int(self.usage))

        if self.requested is not None:
            b.write(Int(self.requested))

        if self.title is not None:
            b.write(String(self.title))

        return b.getvalue()


from aqt import mw
import re
from PyQt5 import QtCore, QtGui, QtWidgets
from aqt.qt import *
from aqt.utils import getText
from aqt import gui_hooks

def repl(match):
    return re.sub("<(?:(?![<>]).)*>", "", match.group(0), flags=re.M|re.S)

def replace(show_only):
    quote = "\""
    queries = list()
    for open, close in [("(", ")"), ("[", "]")]:
        open = f"\\\\\\{open}"
        close = f"\\\\\\{close}"
        not_open = f"(?!{open})."
        not_opens = f"(?:{not_open})*"
        query = f"{open}(?P<code>{not_opens}{not_opens}){close}"

        queries.append(query)
    nids = mw.col.find_notes("")
    replaces = []
    for nid in set(nids):
        change = False
        note = mw.col.getNote(nid)
        for i in range(len(note.fields)):
            old_field = field = note.fields[i]
            for query in queries:
                field = re.sub(query, repl, field, flags=re.M|re.S)
            if field != old_field:
                replaces.append((old_field, field))
                change = True
                note.fields[i] = field
        if change and not show_only:
            note.flush()
    print("The changes where:\n" + "\n".join(f"«{orig}»->«{res}»" for (orig, res) in replaces))

def menus(browser):
    action = QtWidgets.QAction(browser)
    action.setObjectName("noHtmlInMathJax")
    browser.form.menuEdit.addAction(action)
    action.setText("Remove all html in mathjax")
    qconnect(action.triggered, replace)

gui_hooks.browser_menus_did_init.append(menus)


import tensorflow as tf
import numpy as np

# Custom truncation layer
def floating_to_fixed_tf(array, bit_length, fraction_len):
    """
    :param array: number or numpy array
    :param integer_length: the signed bit is included
    :param fraction_len:
    :return:
    """
    maximum_value = 2 ** (bit_length - 1) - 1
    minimum_value = -maximum_value - 1
    # normalize array to have no fraction
    array = array * 2 ** fraction_len
    # apply round
    array = tf.round(array)
    # saturation
    array = tf.clip_by_value(array, minimum_value, maximum_value)
    # re-normalize to fixed point
    array = array / 2 ** fraction_len
    return array

# Used for converting weights
def floating_to_fixed_np(array, bit_len, fraction_len):
    """
        :param array: number or numpy array
        :param integer_length: the signed bit is included
        :param fraction_len:
        :return:
        """
    assert isinstance(bit_len, int) and isinstance(fraction_len, int)
    maximum_value = 2 ** (bit_len - 1) - 1
    minimum_value = -maximum_value - 1
    # normalize array to have no fraction
    array *= 2 ** fraction_len
    # apply round
    array = np.round(array)
    # saturation
    array = np.clip(array, minimum_value, maximum_value)
    # re-normalize to fixed point
    array /= 2 ** fraction_len
    return array

def calculate_fxp_quantization(array):
    """

    :param array:
    :return: bit_length, fraction_length
    """
    maximum_value = np.max(np.abs(array))
    if maximum_value < 1:
        return 8, 7
    required_bits = int(np.ceil(np.log2(maximum_value)))
    if required_bits < 7:
        bit_len = 8
    elif required_bits < 16:
        bit_len = 16
    else:
        raise ValueError('Array contains large number which is not suitable for fixed point')
    fraction_len = bit_len - 1 - required_bits
    return bit_len, fraction_len

def truncate_weights(model, verbose=True):
    # truncate weights
    for i in range(len(model.layers)):
        layer = model.get_layer(index=i)
        weights = layer.get_weights()
        if len(weights) != 0:
            if verbose:
                print(layer.name)
            fxp_weights = []
            for weight in weights:
                bit_len, fraction_len = calculate_fxp_quantization(weight)
                if verbose:
                    print(bit_len, fraction_len)
                weight = floating_to_fixed_np(weight, bit_len, fraction_len)
                fxp_weights.append(weight)
            layer.set_weights(fxp_weights)


import cadquery as cq

from cq_cam import JobV2

tolerance = 0.1
rotary_enc_od = 6
rotary_enc_id = 4.5

inner_shaft_id = 6
inner_shaft_od = 8

outer_shaft_id = 8
outer_shaft_od = 10

tool_diameter = 1
rotary_enc_coupler_thickness = 4

rotary_enc_coupler = (
    cq.Workplane()
    .circle((inner_shaft_od + 5) / 2)
    .extrude(rotary_enc_coupler_thickness)
    .faces('>Z').workplane()
    .circle(inner_shaft_od / 2)
    .circle(inner_shaft_id / 2)
    .cutBlind(-rotary_enc_coupler_thickness / 2)
    .faces('>Z').workplane()
    .sketch()
    .segment((-1, -1), (1, -1), "cseg", True)
    .arc((0, 0), rotary_enc_od/2, 350, 200, "carc", True)
    .constrain("carc", "FixedPoint", None)
    .constrain("carc", "Radius", rotary_enc_od/2 + tolerance)
    .constrain("carc", "cseg", "Coincident", None)
    .constrain("cseg", "carc", "Coincident", None)
    .constrain("cseg", "Orientation", (1, 0))
    .constrain("carc", "cseg", "Distance", (0.5, 0.5, 4.5 + tolerance))
    # Actual
    .arc((0, 0), rotary_enc_od/2, 350, 200, "arc")
    .arc((-1, 0), tool_diameter/2, 180, 90, "db1")
    .arc((1, 0), tool_diameter/2, 270, 90, "db2")
    .segment((-1, -1), (1, -1), "seg")
    .constrain("arc", "FixedPoint", None)
    .constrain("arc", "Radius", rotary_enc_od/2 + tolerance)
    .constrain("db1", "Radius", tool_diameter/2)
    .constrain("db2", "Radius", tool_diameter/2)
    .constrain("db1", "cseg", "Distance", (0.5, 0, 0))
    .constrain("db2", "cseg", "Distance", (0.5, 1, 0))
    .constrain("cseg", "seg", "Distance", (0.5, 0.5, 0))
    .constrain("seg", "Orientation", (1, 0))
    .constrain("arc", "db1", "Coincident", None)
    .constrain("db1", "seg", "Coincident", None)
    .constrain("seg", "db2", "Coincident", None)
    .constrain("db2", "arc", "Coincident", None)
    .solve()
    .assemble()
    .finalize()
    .cutBlind(-rotary_enc_coupler_thickness)
)

cam = (
    JobV2(
        top=rotary_enc_coupler.faces('>Z').workplane().plane,
        feed=300,
        tool_diameter=1.0,
        plunge_feed=100)
    .profile(rotary_enc_coupler.faces('<Z'), inner_offset=-1, stepdown=1)
    .profile(rotary_enc_coupler.faces('<Z[1]'), outer_offset=-1, stepdown=1)
    .profile(rotary_enc_coupler.faces('<Z'), outer_offset=1, stepdown=1)
)

cam.save_gcode('pushpull.nc')
show_object(rotary_enc_coupler)
cam.show(show_object)

#cq.exporters.export(rotary_enc_coupler, 'pp.stl')


#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import codecs
import sys
import os

from ansible import constants as C
from ansible.cli import CLI
from ansible.errors import AnsibleError, AnsibleOptionsError, AnsibleUndefinedVariable, AnsibleParserError
from ansible.executor.playbook_executor import PlaybookExecutor
from ansible.playbook.block import Block
from ansible.playbook.play_context import PlayContext
from ansible.module_utils._text import to_text
from ansible.template import Templar, AnsibleEnvironment
from ansible.utils.listify import listify_lookup_plugin_terms
from ansible.plugins.loader import action_loader, connection_loader, filter_loader, lookup_loader, module_loader, test_loader
from jinja2 import FileSystemLoader
from jinja2.exceptions import TemplateError

try:
    from __main__ import display
except ImportError:
    from ansible.utils.display import Display
    display = Display()

class SharedPluginLoaderObj:
    def __init__(self):
        self.action_loader = action_loader
        self.connection_loader = connection_loader
        self.filter_loader = filter_loader
        self.test_loader = test_loader
        self.lookup_loader = lookup_loader
        self.module_loader = module_loader

class PlaybookDocutizer(CLI):

    exclude_actions = ['meta', 'setup']

    def parse(self):
        parser = CLI.base_parser(
            usage="%prog [options] playbook.yml",
            desc="Creates documentation from Ansible playbooks.",
        )

        parser.add_option('-i', '--inventory', dest='inventory', action="append",
                          help='specify inventory host path or comma separated host list')
        parser.add_option('-l', '--limit', default=C.DEFAULT_SUBSET, dest='subset',
                          help='further limit selected hosts to an additional pattern')
        parser.add_option('--output', '-o', dest='output',
                          help='output file', default='output.txt')
        parser.add_option('--template-path', dest='template_path',
                          help='path to template files', default='templates')
        parser.add_option('--template-master', dest='template_master',
                          help='path to template master file', default='master.j2')
        parser.add_option('--showdetails', dest='showdetails', action='store_true',
                          help='show JSON details for task')
        parser.add_option('--showtasks', dest='showtasks', action='store_true',
                          help='show tasks in table of contents')

        self.parser = parser
        super(PlaybookDocutizer, self).parse()

        if len(self.args) == 0:
            raise AnsibleOptionsError('You must specify a playbook file to run')

        display.verbosity = self.options.verbosity
        self.options.vault_ids = []
        self.options.vault_password_files = []
        self.options.ask_vault_pass = False
        # NOTE(iwalker): need to set liststasks to True so pbex.run() does not actually run the playbook
        self.options.listtasks = True
        self.options.listhosts = False
        self.options.listtags = False
        self.options.syntax = False
        self.options.become = False
        self.options.become_method = 'sudo'
        self.options.become_user = 'root'
        self.options.check = False
        self.options.diff = False

        if self.options.showdetails:
            display.warning('--showdetails option may expose passwords and other secure data!')

    def run(self):
        super(PlaybookDocutizer, self).run()

        for playbook in self.args:
            if not os.path.exists(playbook):
                raise AnsibleError("the playbook: %s could not be found" % playbook)
            if not (os.path.isfile(playbook)):
                raise AnsibleError("the playbook: %s does not appear to be a file" % playbook)

        self._shared_loader_obj = SharedPluginLoaderObj()

        self._loader, self._inventory, self._variable_manager = self._play_prereqs(self.options)

        pbex = PlaybookExecutor(playbooks=self.args, inventory=self._inventory, variable_manager=self._variable_manager, loader=self._loader, options=self.options, passwords={})
        results = pbex.run()

        if isinstance(results, list):
            for p in results:
                plays = []
                for idx, play in enumerate(p['plays']):
                    display.display('Processing play %d: %s' % (idx+1, play.name))

                    if play._included_path is not None:
                        self._loader.set_basedir(play._included_path)
                    else:
                        pb_dir = os.path.realpath(os.path.dirname(p['playbook']))
                        self._loader.set_basedir(pb_dir)

                    hosts = CLI.get_host_list(self._inventory, self.options.subset)
                    # TODO(iwalker): do we really need a host? can this just run with localhost?
                    if len(hosts) == 0:
                        raise AnsibleError('No hosts were specified')

                    # NOTE(iwalker): can loop through all hosts and evaluate the tasks, but since we are not doing any
                    # conditional evaluation, we'll just pick the first host and use that.
                    host = hosts[0]
                    display.v('Processing against host: %s' % (host.get_name()))

                    self._all_vars = self._variable_manager.get_vars(play=play, host=host)
                    play_context = PlayContext(play=play, options=self.options)

                    processed_blocks = []
                    for block in play.compile():
                        block = block.filter_tagged_tasks(play_context, self._all_vars)
                        if not block.has_tasks():
                            continue
                        processed_blocks.append(self._process_block(block))

                    tasks = []
                    for block in processed_blocks:
                        if len(block) == 0:
                            continue
                        for task in block:
                            tasks.append(task)

                    processed_handlers = []
                    for block in play.compile_roles_handlers():
                        processed_handlers.extend(self._process_block(block))

                    play_info = {
                        'filename': p['playbook'],
                        'hosts': hosts,
                        'name': play.name,
                        'roles': play.roles,
                        'tasks': tasks,
                        'handlers': processed_handlers,
                        'become': play.become,
                        'remote_user': play.remote_user,
                    }

                    plays.append(play_info)

                env = AnsibleEnvironment(trim_blocks=True,
                                         extensions=['jinja2.ext.loopcontrols'],
                                         loader=FileSystemLoader(self.options.template_path))

                display.display('Rendering template containing %d plays' % (len(plays)))
                template = env.get_template(self.options.template_master)
                output = template.render(plays=plays,
                                         options=self.options)

                display.display('Saving output to %s' % (self.options.output))
                with codecs.open(self.options.output, mode='w', encoding='utf-8') as f:
                    f.write(output)

    def _process_block(self, b):
        results = []
        for task in b.block:
            if isinstance(task, Block):
                results.extend(self._process_block(task))
            else:
                if task.action in self.exclude_actions:
                    continue
                results.append(self._process_task(task))
        return results

    def _process_task(self, t):
        task_data = self._task_data_for_template(t)
        task_data['eval'] = self._post_validate_task(t)
        task_data['loop_eval'] = self._process_task_loops(t)
        return task_data

    def _post_validate_task(self, task):
        if task.loop_with or task.loop:
            return None

        templar = Templar(loader=self._loader, shared_loader_obj=self._shared_loader_obj, variables=self._all_vars)
        try:
            tmp_task = task.copy(exclude_parent=True, exclude_tasks=True)
            tmp_task._parent = task._parent
            tmp_task.post_validate(templar=templar)
        except AnsibleParserError as e:
            display.warning(e)
            return None

        if tmp_task.args == task.args:
            return None

        return self._task_data_for_template(tmp_task)

    def _process_task_loops(self, task):
        results = []
        if task.loop_with or task.loop:
            task_vars = self._variable_manager.get_vars(task=task)
            templar = Templar(loader=self._loader, shared_loader_obj=self._shared_loader_obj, variables=self._all_vars)
            items = self._get_loop_items(task, templar, task_vars)
            if items:
                for item in items:
                    try:
                        task_vars['item'] = item
                        templar.set_available_variables(task_vars)
                        tmp_task = task.copy(exclude_parent=True, exclude_tasks=True)
                        tmp_task._parent = task._parent
                        tmp_task.post_validate(templar=templar)
                        results.append(self._task_data_for_template(tmp_task))
                        del task_vars['item']
                    except AnsibleParserError:
                        display.warning('%s caused an AnsibleParseError; ignoring' % (tmp_task))
                        results.clear()
        return results

    def _get_loop_items(self, task, templar, task_vars):
        items = None
        if task.loop_with:
            if task.loop_with in self._shared_loader_obj.lookup_loader:
                try:
                    loop_terms = listify_lookup_plugin_terms(terms=task.loop, templar=templar, loader=self._loader, fail_on_undefined=True, convert_bare=False)
                    mylookup = self._shared_loader_obj.lookup_loader.get(task.loop_with, loader=self._loader, templar=templar)
                    items = mylookup.run(terms=loop_terms, variables=task_vars, wantlist=True)
                except AnsibleUndefinedVariable as e:
                    display.warning('Processing %s caused an AnsibleUndefinedVariable error: %s' % (task, e))
        elif task.loop:
            items = templar.template(task.loop)
        return items

    def _task_data_for_template(self, task):
        result = {'name': task.name,
                  'role': task._role,
                  'has_loop': (task.loop or task.loop_with),
                  'loop': task.loop,
                  'loop_with': task.loop_with,
                  'action': task.action,
                  'args': task.args,
                  'when': task.when,
                  'notify': task.notify,
                  'register': task.register,
                  'validate': str(task.validate),
                  'ds': task.get_ds()}
        return result

if __name__ == "__main__":
    try:
        cli = PlaybookDocutizer([to_text(a, errors='surrogate_or_strict') for a in sys.argv])
        cli.parse()
        exit_code = cli.run()
    except AnsibleOptionsError as e:
        cli.parser.print_help()
        display.error(to_text(e), wrap_text=False)
        exit_code = 5
    except AnsibleParserError as e:
        display.error(to_text(e), wrap_text=False)
        exit_code = 4
    except AnsibleError as e:
        display.error(to_text(e), wrap_text=False)
        exit_code = 7
    except TemplateError as e:
        display.error(to_text(e), wrap_text=False)
        exit_code = 6
    except KeyboardInterrupt:
        display.error('User interrupted execution')
        exit_code = 99
    sys.exit(exit_code)


"""
This script extracts images of different types and saves them.
This is for Figure 1 in the paper
"""

#global module imports
from scipy import misc
import matplotlib.pyplot as plt
import matplotlib.cm as cm

#local imports
from pbtspot import set_default,globalconstants
from pbtspot.adaboost.generateimage import image_generate,vid_utils

set_default.set_default_constants();

def init_set(glob_const):
    #set constants
    glob_const.particle_shape='round'
    glob_const.image_type='A'
    glob_const.savefile();

def gen_img_type(shape,imagetype, SNRval,filename,islowres=False):

    glob_const=globalconstants.GlobalConstants();
    init_set(glob_const);

    glob_const.particle_shape=shape;
    glob_const.image_type=imagetype;
    glob_const.savefile();

    img_gen=image_generate.GenerateImage(SNRval);
    if islowres==False:
        img_gen.generate_img();
        full_img_matrix=img_gen.noisy_imgarr;
        part_img_matrix=full_img_matrix[0:150,0:130];
    else:
        img_gen.lowres_image();
        full_img_matrix=img_gen.noisy_lowres;
        part_img_matrix=full_img_matrix[0:37,0:32];

    #filename='scripts/Paper_Figures/Figure1/A_round_SNR2.png';

    plt.imshow(part_img_matrix,cmap=cm.Greys_r,interpolation='none');
    plt.axis('off')
    #plt.show()
    #also tried scipy.misc.imsave but this provides more
    #flexibility and more options
    plt.savefig(filename,dpi=300);
    plt.show();
    return;

def imggen_script():

    #filename='scripts/Paper_Figures/Figure1/A_round_SNR2.png';
    #gen_img_type(shape='round',imagetype='A',SNRval=2.,\
    #             filename=filename);
    #filename='scripts/Paper_Figures/Figure1/A_round_SNR0p5.png';
    #gen_img_type(shape='round',imagetype='A',SNRval=0.5,\
    #             filename=filename);
    #filename='scripts/Paper_Figures/Figure1/A_elongated_SNR2.png';
    #gen_img_type(shape='elongated',imagetype='A',SNRval=2,\
    #             filename=filename);
    #filename='scripts/Paper_Figures/Figure1/B_elongated_SNR2.png';
    #gen_img_type(shape='elongated',imagetype='B',SNRval=2,\
    #             filename=filename);
    filename='scripts/Paper_Figures/Figure1/A_round_SNR2_lowres.png';
    gen_img_type(shape='round',imagetype='A',SNRval=2,\
                 filename=filename,islowres=True);
    filename='scripts/Paper_Figures/Figure1/A_round_SNR0p5_lowres.png';
    gen_img_type(shape='round',imagetype='A',SNRval=0.5,\
                 filename=filename,islowres=True);

    return;

if __name__=="__main__":
    imggen_script()


from flask import Flask
from flask_restful import Resource, Api, reqparse
from flask_cors import CORS
from util.database_io import get_available_data

# Flask initialisieren
app = Flask(__name__)
cors = CORS(app, resources={r"/users/*": {"origins": "*"}})
api = Api(app)

'''
    Diese Klasse nimmt einen GET-Request entgegen, parsed parameter
    und führt die Datenabfrage durch. Wenn Daten verfügbar sind
    wird mit dem Status-Code 200 geantwortet, wenn nicht 202.
    So kann am Frontend unterschieden werden on es ein Lade-Symbol
    anzeigen soll oder nicht.
'''
class Users(Resource):
    def get(self, username):
        # Limit parameter mit dem RequestParser auslesen
        parser = reqparse.RequestParser()
        parser.add_argument('limit', type=int)
        args = parser.parse_args()
        limit = args['limit']

        response, is_available = get_available_data(username, limit)
        if is_available:
            return response
        else:
            return response, 202

# Ressourcen/URI's zuordnen
api.add_resource(Users, '/users/<string:username>')

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')


'''
Problem
-------------------
You are given an array of integers stones where stones[i] is the weight of the ith stone.

We are playing a game with the stones. On each turn, we choose the heaviest two stones and smash them together. Suppose the heaviest two stones have weights x and y with x <= y. The result of this smash is:

If x == y, both stones are destroyed, and
If x != y, the stone of weight x is destroyed, and the stone of weight y has new weight y - x.
At the end of the game, there is at most one stone left.

Return the smallest possible weight of the left stone. If there are no stones left, return 0.

Constraints
-------------------
* 1 <= stones.length <= 30
* 1 <= stones[i] <= 1000

Thinking
-------------------
* heapy the array O(N)
'''
from typing import List
import heapq

def lastStoneWeight(stones: List[int]) -> int:
    stones = [-x for x in stones]
    heapq.heapify(stones)
    while stones:
        if len(stones) == 1:
            return -stones[0]

        largest = -heapq.heappop(stones)
        second = -heapq.heappop(stones)

        after_smash = largest - second
        if after_smash != 0:
            heapq.heappush(stones, -after_smash)

    return 0

if __name__ == "__main__":
    print(lastStoneWeight([2, 7, 4, 1, 8, 1]))


class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None

    def __str__(self):
        return str(self.data)

class BST:
    def __init__(self):
        self.root = None

    def insert(self, data):
        new_node = Node(data)
        if self.root is None:
            self.root = new_node
        else:
            current = self.root
            while True:
                if data < current.data:
                    if current.left is None:
                        current.left = new_node
                        break
                    else:
                        current = current.left
                elif data >= current.data:
                    if current.right is None:
                        current.right = new_node
                        break
                    else:
                        current = current.right
        return self.root

    def printTree(self, node, level=0):
        if node != None:
            self.printTree(node.right, level + 1)
            print("     " * level, node)
            self.printTree(node.left, level + 1)

    def descending(self, node):
        if node is None:
            return
        if node.right is not None:
            self.descending(node.right)
        print(node.data, end=" ")
        if node.left is not None:
            self.descending(node.left)

    def ascending(self, node):
        if node is None:
            return
        if node.left is not None:
            self.ascending(node.left)
        print(node.data, end=" ")
        if node.right is not None:
            self.ascending(node.right)

T = BST()
inp = [int(i) for i in input("Enter Input : ").split()]
for i in inp:
    root = T.insert(i)
T.printTree(root)
print("--------------------------------------------------")
print("Descending: ", end="")
T.descending(root)
print()
print("Ascending: ", end="")
T.ascending(root)
print()


from django.views.generic.edit import CreateView
from cadastros.forms.user import UsuarioForm
from django.urls import reverse_lazy

class UsuarioCreate(CreateView):
    template_name = "cadastros/form.html"
    form_class = UsuarioForm
    success_url = reverse_lazy('login')

    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)

        context['titulo'] = "Registrar-se"
        context['botao'] = "Cadastrar-se"

        return context


while True:
    n = int(input())
    if n == 0: break
    while True:
        if n < 10:
            print(n)
            break
        print(n, end=' ')
        ls = list(str(n))
        n = 1
        for i in range(len(ls)): n *= int(ls[i])


#!/usr/local/bin/python3.6

import glob

#save only "passed" and AF>0.2, DP>50 variants

def main():
    for name in glob.glob("./raw_variants.vcf"): #input is the vcf file
        inputfile = str(name)
        outputfile = str(name)[:-4]+"_HQ_10K.txt"
        with open(inputfile,'r') as f:
            data_in = f.read().rstrip().split('\n') # Read file and create list split by newline
        print(len(data_in))

        header_line_number, header = find_header(data_in)
        print("header_line_number:", len(header))
        print("Total input SNPs:", len(data_in)-len(header))
        #passed=filter_PASS(data_in,header_line_number, passed)

        AF_filtered=filter_AF(data_in,header_line_number)
        print("High quality SNPs:",len(AF_filtered))

        mix_AF=find_AF(AF_filtered)

        mix1=find_mix(mix_AF)
        #print=("Pblood AF> 0.2: ", len(mix1))

        with open(outputfile,'w') as f:
            for line in header:
                f.write(str(line)+'\n')
            for line in mix_AF:
                f.write(str(line)+'\n')

# append header lines to the header list.
def find_header(data_in):
    header=[]
    for i in range(len(data_in)):
        if '#CHROM' not in data_in[i][0:6]:
            header.append(data_in[i])
        else:
            header.append(data_in[i])
            header_line_number = i
            break
    print(len(header))
    return header_line_number, header

# append lines with quality "PASS" to the filtered list.
def filter_PASS(data_in,header_line_number):
    passed=[]
    for line in data_in[(header_line_number + 1):(len(data_in))]:
        field=line.split('\t')
        if field[6]=="PASS":
            passed.append(line)
    return passed

# append lines with AF>20%, DP>50 to the filtered list.
def filter_AF(data_in,header_line_number):
    AF_filtered=[]
    for line in data_in[(header_line_number + 1):(len(data_in))]:
        Max_AF_allele = 0
        field=line.split('\t')
        for sample in field[9:len(field)]:
            reads=sample.split(':')
            AD=reads[1]
            DP=reads[2]
            if DP != "." and float(DP) > 50:
                AD_allele=AD.split(",")
                for j in range(1,len(AD_allele)): # first AD_allele is WT
                    AF_allele=float(AD_allele[j])/float(DP)
                    if float(AF_allele) > Max_AF_allele:
                        Max_AF_allele = float(AF_allele)
        if float(Max_AF_allele) > 0.20:
            AF_filtered.append(line)

    return AF_filtered

# append AF to the end
def find_AF(AF_filtered):
    mix_AF=[]
    for line in AF_filtered:
        AF_list=[]
        field=line.split('\t')
        for sample in field[9:len(field)]:
            reads=sample.split(':')
            AD=reads[1]
            DP=reads[2]
            AD_allele=AD.split(",")
            if len(AD_allele) == 2 and "." not in DP:
                if float(DP) != 0:
                    AF=float(AD_allele[1])/float(DP)
            elif len(AD_allele) > 2:
                AF ="multiAD"
            else:
                AF=""
            AF_list.append(str(AF))
        line=line+'\t'+('\t').join(AF_list)
        mix_AF.append(line)
    return mix_AF

# select line with Pblood AF >0.2
def find_mix(mix_AF):
    mix1=[]
    multiAD=0
    noAF = 0
    for line in mix_AF:
        field=line.split('\t')
        mother=field[9]
        father=field[10]
        p_blood=field[11]
        p_tumor=field[12]
        brother=field[13]
        pblood_AF=field[16]
        if "0/0" not in p_blood:
            if pblood_AF=="multiAD":
                multiAD += 1
            elif pblood_AF=="":
                noAF += 1
            elif float(pblood_AF)>0.2:
                mix1.append("\t".join(field[0:14]))
    print("in p-blood:", len(mix1),len(mix1)/102482)
    print("Multi Alleles in p-blood:", multiAD)
    print("No AF in p-blood:", noAF)
    return mix1

main()


def change_basic_syntax(node, ccfg):
        sourcecode = node.sourceCode
        nodes = ccfg.nodes

        #ensure appropriate whitespace, simple syntax changes
        explicitChildren = set([token for token in sourcecode if token in node.neighbors])

        for i, token in enumerate(sourcecode):
                if token == 'in':
                        if sourcecode[i - 1] == ")":
                                sourcecode[i - 1] = ""
                                if sourcecode[i - 5] == "(":
                                        sourcecode[i - 5] = ""
                        sourcecode[i] = " := range "
                elif token == "println":
                        sourcecode[i] = "fmt.Println"
                elif token == 'if':
                        sourcecode[i] = 'if '
                elif token == 'for':
                        sourcecode[i] = 'for '
                elif token == 'float':
                        sourcecode[i] = 'float64'
                elif token == 'map' and sourcecode[i - 2] == ':': #map delcaration
                        sourcecode[i] = "make(map"
                        sourcecode[i + 1] = "["
                        sourcecode[i + 3] = ']'
                        sourcecode[i + 5] = ")"

# Links node to buffer function
# Params:
#       sourcecode - Parsed Digon source code by line (list of string lists)
#       i - current index in source code
#       param - parameter sent to channel
#
def overwrite_dest(sourcecode, i, param):
        sourcecode[i - 1] = "channel"
        sourcecode[  i  ] = '<'
        sourcecode[i + 1] = '-'
        sourcecode[i + 2] = param
        sourcecode[i + 3] = "" #replace (
        sourcecode[i + 4] = "" #replace )

# Links node to buffer function
# Params:
#       sourcecode - Parsed Digon source code by line (list of string lists)
#       i - current index in source code
#       destTypes - channel type for each destination
#       destinations - list of destination links in current function
#       leftindex - index of lvalue string
#       param - parameter sent to channel
#
def overwrite_node_link(sourcecode, i, destTypes, destinations, leftindex, param):
        fname = sourcecode[i + 2] #node name
        sourcecode[i - 1] = fname
        if sourcecode[i - 1] == "length":
                sourcecode[i - 1] = "len"
        sourcecode[i] = '('
        if destTypes.get(fname) != None:
                param += ', ' + fname + "Channel"

        sourcecode[i + 1] = param
        sourcecode[i + 2] = ')'
        i += 3
        while sourcecode[i] != ')':
                sourcecode[i] = ""
                i += 1
        sourcecode[i] = ""
        i += 1

        if sourcecode[i] == '=': #check if assignment or pass-on node
                if sourcecode[i + 3] != '(': #assignment
                        sourcecode[leftindex] = sourcecode[i + 2] + " = " + sourcecode[leftindex]
                        sourcecode[i] = ""
                        sourcecode[i + 1] = ""
                        sourcecode[i + 2] = ""
                        i += 3
                else:
                        sourcecode[leftindex] = "go " + sourcecode[leftindex]
                        while sourcecode[i] != ")":
                                if sourcecode[i + 1] == "(":
                                        if destinations.get(sourcecode[i]) == None:
                                                destinations[sourcecode[i]] = []

                                        sourcecode[leftindex] = fname + "Channel := make(chan " \
                                        + destTypes[fname] + ")\n" + sourcecode[leftindex]

                                        destinations[sourcecode[i]].append(fname + "Channel")
                                sourcecode[i] = ""
                                i += 1
                        sourcecode[i] = ""

# Resolves dest and other node linking to go function calls
# Params:
#       i - current index in source code
#       token - current token (at sourcecode[i])
#       sourcecode - Parsed Digon source code by line (list of string lists)
#       destTypes - channel type for each destination
#       destinations - list of destination links in current function
#
# WARNING: this function relies on semicolons in sourcecode
#
def process_function(i, token, sourcecode, destTypes, destinations):
        leftindex = i - 1 #index of lval
        j = 1
        param = ""

        #Determine full parameters for node (could be expression, tuple)
        while sourcecode[i - j] != ';' and sourcecode[i - j] != '}':
                param = sourcecode[i - j] + param
                sourcecode[i - j] = ""
                j += 1

        if sourcecode[i + 2] == 'dest':
                overwrite_dest(sourcecode, i, param)
        else:
                overwrite_node_link(sourcecode, i, destTypes, destinations,\
                        leftindex, param)

# Handles current token in node source code
# Params:
#       i - current index in source code
#       token - current token (at sourcecode[i])
#       sourcecode - Parsed Digon source code by line (list of string lists)
#       ccfg - full colored control flow graph
#       destinations - list of destination links in current function
#
# WARNING: this function relies on semicolons in sourcecode
#
def process_token(i, token, sourcecode, ccfg, destinations):
        destTypes = ccfg.channels

        if token == '=' and sourcecode[i + 1] == ">":
                process_function(i, token, sourcecode, destTypes, destinations)
        else:
                pass

# Writes buffer function (end result of concurrent nodes' destinations)
# Params:
#       sourcecode - Parsed Digon source code by line (list of string lists)
#       destinations - list of destination links in current function
#
# WARNING: this function relies on semicolons in sourcecode
#
def write_buffer_function(sourcecode, destinations):
        for dest in destinations: #
                funcode = "\n" + dest + "("
                for i, channel in enumerate(destinations[dest]):
                        funcode += channel
                        if i != len(destinations[dest]) - 1:
                                funcode += ", "
                funcode += ")\n"
                sourcecode.append(funcode)

# Changes Resolves node linking to Go function calls as appropriate
# Params:
#       sourcecode - Parsed Digon source code by line (list of string lists)
#       ccfg - full colored control flow graph
#
def transpile_function_calls(node, ccfg):
        sourcecode = node.sourceCode
        nodes = ccfg.nodes
        destinations = {}

        for i, token in enumerate(sourcecode):
               process_token(i, token, sourcecode, ccfg, destinations)

        #Writes function call to buffer function if needed
        write_buffer_function(sourcecode, destinations)

################################## INTERFACE ###################################
# Transpiles Digon source to equivalent Go source
# Params:
#       sourcecode - Parsed Digon source code by line (list of string lists)
#
# Returns:
#       transpiled - Transpiled code in same form
#
def transpile_to_go(node, ccfg):
        change_basic_syntax(node, ccfg)
        transpile_function_calls(node, ccfg)


import os
from sys import platform
import time
import datetime
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from GAN.cGANGenerator import Generator
from GAN.cGANDiscriminator import Discriminator
from GAN.cGANLoss import discriminator_loss, generator_loss
from GAN.data_preprocess import load_image_train, load_image_test_y, load_image_test

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

# GPU Setting
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"

config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
tf.compat.v1.enable_eager_execution(config=config)
layers = tf.keras.layers
cross_entropy = tf.keras.losses.BinaryCrossentropy(from_logits=True)

"""
Discriminator loss:
The discriminator loss function takes 2 inputs; real images, generated images
real_loss is a sigmoid cross entropy loss of the real images and an array of ones(since the real images)
generated_loss is a sigmoid cross entropy loss of the generated images and an array of zeros(since the fake images)
Then the total_loss is the sum of real_loss and the generated_loss

Generator loss:
It is a sigmoid cross entropy loss of the generated images and an array of ones.
The paper also includes L2 loss between the generated image and the target image.
This allows the generated image to become structurally similar to the target image.
The formula to calculate the total generator loss = gan_loss + LAMBDA * l2_loss, where LAMBDA = 100.
This value was decided by the authors of the paper.
"""

def generated_image(model, test_input, tar, t=0):
    """Dispaly  the results of Generator"""
    prediction = model(test_input)
    # plt.figure(figsize=(15, 15))
    display_list = [np.squeeze(test_input[:, :, :, 0]),
                    np.squeeze(tar[:, :, :, 0]),
                    np.squeeze(prediction[:, :, :, 0])]

    title = ['Input Y', 'Target H', 'Prediction H']

    for i in range(3):
        plt.subplot(1, 3, i+1)
        plt.title(title[i])
        plt.imshow(display_list[i])
        plt.axis("off")

    plt.savefig(os.path.join("generated_img", "img_" + str(t) + ".png"))

def train_step(input_image, target, l2_weight=100):
    with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
        gen_output = generator(input_image)                      # input -> generated_target
        disc_real_output = discriminator(target)  # [input, target] -> disc output
        disc_generated_output = discriminator(gen_output)  # [input, generated_target] -> disc output

        # calculate loss
        gen_loss = generator_loss(disc_generated_output,
                                  gen_output,
                                  target,
                                  l2_weight=l2_weight)   # gen loss
        disc_loss = discriminator_loss(disc_real_output,
                                       disc_generated_output)  # disc loss

    # gradient
    generator_gradient = gen_tape.gradient(gen_loss,
                                           generator.trainable_variables)
    discriminator_gradient = disc_tape.gradient(disc_loss,
                                                discriminator.trainable_variables)

    # apply gradient
    generator_optimizer.apply_gradients(zip(generator_gradient,
                                            generator.trainable_variables))
    discriminator_optimizer.apply_gradients(zip(discriminator_gradient,
                                                discriminator.trainable_variables))

    return (gen_loss, disc_loss)

def train(epochs, l2_weight=100):
    nm = []
    ep = []
    start_time = datetime.datetime.now()

    for epoch in range(epochs):
        print("-----\nEPOCH:", epoch)
        # train
        for (bi, (target, input_image)) in enumerate(load_image_train(path, batch_size=batch)):
            elapsed_time = datetime.datetime.now() - start_time
            (gen_loss, disc_loss) = train_step(input_image,
                                               target,
                                               l2_weight=l2_weight)

            print("B/E:", bi, '/' , epoch,
                  ", Generator loss:", gen_loss.numpy(),
                  ", Discriminator loss:", disc_loss.numpy(),
                  ', time:',  elapsed_time)
        ep.append(epoch + 1)

        (realim, inpuim) = load_image_test_y(path)
        prediction = generator(inpuim)

        ## Calculate test NMSE score...
        error_ = np.sum((realim - prediction) ** 2, axis=None)
        real_ = np.sum(realim ** 2, axis=None)
        nmse_dB = 10 * np.log10(error_ / real_)
        nm.append(nmse_dB)

        plt.figure()
        plt.plot(ep,nm,'^-r')
        plt.grid(True)
        plt.xlabel('Epoch')
        plt.ylabel('NMSE')

        if (is_not_linux):
            plt.show()

    return (nm, ep)

is_not_linux = (platform != 'linux')

# data path
path = "../Data_Generation_matlab/Gan_Data/Comb_3_12_25_rev3.mat"

# Set hyper params...
# beta_1_list = [0.5, 0.6, 0.7, 0.8, 0.9]
# l2_weight_list = [0.0, 1.0, 10.0, 50.0, 100.0]
# lr_gen_list = [1e-3, 5e-4, 2e-4, 1e-4, 5e-5, 1e-5]
# lr_dis_list = [1e-3, 5e-4, 2e-4, 1e-4, 5e-5, 1e-5]

beta_1_list = [0.5]
l2_weight_list = [100.0]
lr_gen_list = [2e-4]
lr_dis_list = [2e-5]

# batch = 1 produces good results on U-NET
BATCH_SIZE = [2, 4, 10]
snr = 10
epochs = 25 # The best performance
NMSE_SAVE_OPT = True
MODEL_SAVE_OPT = True

bs_index = 0

for beta_1 in beta_1_list:
    for l2_weight in l2_weight_list:
        for lr_gen in lr_gen_list:
            for lr_dis in lr_dis_list:

                # model
                generator = Generator()
                discriminator = Discriminator()

                batch = BATCH_SIZE[bs_index]

                # optimizer
                # generator_optimizer = tf.compat.v1.train.AdamOptimizer(2e-4, beta1=0.5) ##
                # discriminator_optimizer = tf.compat.v1.train.RMSPropOptimizer(2e-5) ##
                # discriminator_optimizer = tf.compat.v1.train.AdamOptimizer(2e-4, beta1=0.5) # Which is unused...
                discriminator_optimizer = tf.keras.optimizers.Adam(learning_rate=lr_dis,
                                                                   beta_1=beta_1)
                generator_optimizer = tf.keras.optimizers.Adam(learning_rate=lr_gen,
                                                               beta_1=beta_1)

                # train
                (nm, ep) = train(epochs=epochs,
                                 l2_weight=l2_weight)

                fig_nmse = plt.figure(figsize=(10, 10))
                plt.plot(ep, nm, '^-r')

                for (x, y) in zip(ep, nm):
                    if (x > 9):
                        plt.text(x=x,
                                 y=y + 0.5,
                                 s=("%.3f" % y),
                                 fontsize=9,
                                 color='black',
                                 horizontalalignment='center',
                                 verticalalignment='bottom',
                                 rotation=90)

                plt.xlabel('Epoch, %s' % (path[35:]))
                plt.ylabel('NMSE(dB)')
                plt.title("[lr_gen : %.6f][lr_dis : %.6f][beta1 : %.3f][l2_weight : %.6f]"
                          % (lr_gen, lr_dis, beta_1, l2_weight))
                plt.grid(True)

                if (is_not_linux):
                    plt.show()

                timestr = time.strftime("%Y%m%d_%H%M%S")
                fig_nmse.savefig("fig_temp/nmse_score_%s_%02dBS" % (timestr, batch))

                fname = "nmse/nmse_dB_%.5f_%.5f_%.2f_%.2f_ext_%02d" % (lr_gen, lr_dis, beta_1, l2_weight, batch)

                if (NMSE_SAVE_OPT):
                    nm_np = np.array(nm)
                    np.save(fname, nm_np)

                MODEL_SAVE_COND = ((lr_gen == 2e-4) and
                                  (lr_dis == 2e-5) and
                                  (beta_1 == 0.5) and
                                  (l2_weight == 100.0))

                if (MODEL_SAVE_OPT and MODEL_SAVE_COND):
                    generator.save("Models/Gen_%.5f_%.5f_%.2f_%.2f_%ddB_ext_%02d"
                                   % (lr_gen, lr_dis, beta_1, l2_weight, snr, batch))
                    # discriminator.save("Models/Dis_%.5f_%.5f_%.2f_%.2f"
                    #                    % (lr_gen, lr_dis, beta_1, l2_weight))

                bs_index = bs_index + 1


from django.shortcuts import render
from django.http import JsonResponse
from rest_framework.decorators import api_view
from rest_framework.response import Response
import nmap
import json

scanner = nmap.PortScanner()
nm = nmap.PortScanner()
nmScan = nmap.PortScanner()

ip_addr = '127.0.0.1'
ip_baseline = '192.168.68.0'

@api_view(['GET'])
def getOS(request, pk):

    pingScan = []
    osinfo = []
    flag = []
    st = ""

    if pk == 'undefined':
         pk = ip_addr

    info = scanner.scan(pk, arguments="-O")['scan'][pk]['osmatch'][0]
    info["IP_Address"] = pk

    nmScan.scan(pk, '21-443')
    for host in nmScan.all_hosts():
     print('Host : %s (%s)' % (host, nmScan[host].hostname()))
     print('State : %s' % nmScan[host].state())
     for proto in nmScan[host].all_protocols():
        #  print('----------')
        #  print('Protocol : %s' % proto)

         lport = nmScan[host][proto].keys()
         sorted(lport)
         for port in lport:
            # print ('port : %sstate : %s' % (port, nmScan[host][proto][port]['state']))
            st = st + ('%s : %s, ' % (port, nmScan[host][proto][port]['state']))

    info["Ports"] = st

    if not pingScan:
        nm.scan(hosts=ip_baseline+'/24', arguments='-n -sP -PE -PA21,23,80,3389')
        hosts_list = [(x, nm[x]['status']['state']) for x in nm.all_hosts()]
        for host, status in hosts_list:
            print('{0}:{1}'.format(host, status))
            pingScan.append(('{0}:{1},  '.format(host, status)).upper())
            # info = scanner.scan(host, arguments="-O")['scan'][host]['osmatch'][0]
            # info["IP_Address"] = host
            # if not flag:
            #     osinfo.append(info)

    if not flag:
        info["pinged"] = pingScan
        osinfo.append(info)

    flag.append("True")

    return Response(osinfo)


from django.shortcuts import render_to_response
from core.utils import *
import urllib, urllib2
import libxml2, csv, os
#from sets import Set
from django.db import connection
from datetime import datetime

monthName = ['', 'January', 'February', 'March', 'April',
             'May', 'June', 'July', 'August', 'September',
             'October', 'November', 'December']

def index(request):
    return viewNormal(request)

def viewNormal(request):
    data = getData()
    data['c5report_active'] = 1
    return render_to_response('c5report.html', data)

def viewMobile(request):
    data = getData()
    return render_to_response('c5report.mobile.html', data)

# Not used at the moment
def viewMobileManifest(request):
    response = render_to_response('c5report.mobile.manifest')
    response['Content-Type'] = 'text/cache-manifest'
    return response

def viewText(request):
    data = getData()
    response = render_to_response('c5report.txt', data)
    response['Content-Type'] = 'text/plain'
    response['Content-Description'] = 'C5 Report generated by GStat 2.0'
    return response

def viewXml(request):
    data = getData()
    response = render_to_response('c5report.xml', data)
    response['Content-Type'] = 'application/xml'
    response['Content-Description'] = 'C5 Report generated by GStat 2.0'
    return response

def prettyPrintNumber(number):
    len_number = len(number)
    key = 9; value = 'B';
    if len_number > key:
        return number[0:len_number-key] + '.' + number[len_number-key:len_number-key+1] + value
    key = 6; value = 'M';
    if len_number > key:
        return number[0:len_number-key] + '.' + number[len_number-key:len_number-key+1] + value
    key = 3; value = 'K';
    if len_number > key:
        return number[0:len_number-key] + '.' + number[len_number-key:len_number-key+1] + value
    return number

def getJobsPerMonth(startMonth, startYear, endMonth, endYear):
    urlquery = 'http://www3.egee.cesga.es/gridsite/accounting/CESGA/csv_export_extend.php'
    params = {'startYear'   : startYear,
              'startMonth'  : startMonth,
              'endYear'     : endYear,
              'endMonth'    : endMonth,
              'yrange'      : 'REGION',
              'xrange'      : 'VO',
              'option'      : 'REGION',
              'optval'      : '',
              'type'        : 'Production',
              'dteamVO'     : '0',
              'query'       : 'njobs',
              'voGroup'     : 'lhc',
              'dataGroup'   : 'Production',
              'newCateg'    : '0',
              'isextend'    : '0'}
    response = urllib2.urlopen(urlquery, urllib.urlencode(params)).read()
    rows = csv.reader(response.split(os.linesep), delimiter=',', quoting=False)

    # Skip initial comments
    for i in range(0,5):
        rows.next()

    # Get the total number of jobs
    for row in rows:
        if row[0] == 'Total':
            return row[5]

    return False

def getData():
    # Initialization
    cursor = connection.cursor()
    # SAM DB API
    urlquery = 'http://lcg-sam.cern.ch:8080/reports/lcg_deployment_C5.xsql'
    response = urllib2.urlopen(urlquery).read()
    doc = libxml2.parseDoc(response)
    # Summary OSes
    #urlquery2 = 'http://horat.web.cern.ch/horat/summary-oses.xml'
    #response2 = urllib2.urlopen(urlquery2).read()
    #doc2 = libxml2.parseDoc(response2)
    # Sites in GStat 2.0
    site_list = get_sites('GRID', 'ALL')
    # Month names for easy access. E.g.: monthName[1] == 'January'
    monthName = ['', 'January', 'February', 'March', 'April',
                 'May', 'June', 'July', 'August',
                 'September', 'October', 'November', 'December']

    # 1.a Total number of Sites
    # Extracted from GStat 2.0 or SAM API
    # GStat 2.0 way: total_number_sites = len(site_list)
    total_number_sites = doc.xpathEval("/LCG_DEPLOYMENT/TOTAL_NUM_EGEE_SITES_CERT_PROD/VALUE/TOTAL_SITES")[0].content

    # 1.b Status (ok, degraded, down, maintenance, not available)
    # Extracted from SAM API
    # http://lcg-sam.cern.ch:8080/reports/lcg_deployment_C5.xsql
    sites_ok = doc.xpathEval("/LCG_DEPLOYMENT/TOTAL_EGEE_SITES_STATUS_CERT_PROD/VALUE[NAME='ok']/TOTAL")[0].content
    sites_degraded = doc.xpathEval("/LCG_DEPLOYMENT/TOTAL_EGEE_SITES_STATUS_CERT_PROD/VALUE[NAME='degraded']/TOTAL")[0].content
    sites_degraded += doc.xpathEval("/LCG_DEPLOYMENT/TOTAL_EGEE_SITES_STATUS_CERT_PROD/VALUE[NAME='degrated80']/TOTAL")[0].content
    sites_down = doc.xpathEval("/LCG_DEPLOYMENT/TOTAL_EGEE_SITES_STATUS_CERT_PROD/VALUE[NAME='down']/TOTAL")[0].content
    sites_maint = doc.xpathEval("/LCG_DEPLOYMENT/TOTAL_EGEE_SITES_STATUS_CERT_PROD/VALUE[NAME='maint']/TOTAL")[0].content
    sites_na = doc.xpathEval("/LCG_DEPLOYMENT/TOTAL_EGEE_SITES_STATUS_CERT_PROD/VALUE[NAME='na']/TOTAL")[0].content

    # 2. Numer of sites per software (glite 3.2 and glite 3.1)
    # Extracted from SAM API
    # http://lcg-sam.cern.ch:8080/reports/lcg_deployment_C5.xsql
    sites_glite32 = doc.xpathEval("/LCG_DEPLOYMENT/GLITE_VERSION_IN_SITES/VALUES[VERSION='3.2.0']/NUMBER_OF_SITES")[0].content
    sites_glite31 = doc.xpathEval("/LCG_DEPLOYMENT/GLITE_VERSION_IN_SITES/VALUES[VERSION='3.1.0']/NUMBER_OF_SITES")[0].content

    # 3.a Number of jobs executed last month
    # 3.b Number of jobs executed since the beginning of this month
    # Extracted from accounting portal: http://www3.egee.cesga.es/
    now = datetime.now()
    thisYear = now.year
    thisMonth = now.month
    jobs_this_month = getJobsPerMonth(thisMonth, thisYear, thisMonth, thisYear)
    jobs_this_month = prettyPrintNumber(jobs_this_month)
    jobs_this_month_monthName = monthName[thisMonth]
    jobs_this_month_monthNumber = thisMonth
    jobs_this_month_yearNumber = thisYear

    if thisMonth == 1:
        lastYear = thisYear - 1
        lastMonth = 12
    else:
        lastYear = thisYear
        lastMonth = thisMonth - 1
    jobs_last_month = getJobsPerMonth(lastMonth, lastYear, lastMonth, lastYear)
    jobs_last_month = prettyPrintNumber(jobs_last_month)
    jobs_last_month_monthName = monthName[lastMonth]
    jobs_last_month_monthNumber = lastMonth
    jobs_last_month_yearNumber = lastYear

    # 4. Number of CREAM CEs unique hosts deployed
    cursor.execute("SELECT count(DISTINCT ce.hostname) FROM glue_gluece ce WHERE ce.implementationname = 'CREAM';")
    creamces_unique_hosts = int(cursor.fetchall()[0][0])

    # 5. Number of LCG-CEs unique hosts deployed
    cursor.execute("SELECT count(DISTINCT ce.hostname) FROM glue_gluece ce WHERE ce.implementationname = 'LCG-CE';")
    lcgces_unique_hosts = int(cursor.fetchall()[0][0])

    # 6. - Number of sites supporting CREAM CEs
    cursor.execute("SELECT COUNT(DISTINCT cluster.gluesite_fk) FROM glue_gluece ce, glue_gluecluster cluster, glue_gluesite site WHERE ce.implementationname='CREAM' and ce.gluecluster_fk = cluster.uniqueid and cluster.gluesite_fk = site.uniqueid;")
    sites_creamces = int(cursor.fetchall()[0][0])

    # 7. - Number of sites supporting LCG-CEs
    cursor = connection.cursor()
    cursor.execute("SELECT COUNT(DISTINCT cluster.gluesite_fk) FROM glue_gluece ce, glue_gluecluster cluster, glue_gluesite site WHERE ce.implementationname='LCG-CE' and ce.gluecluster_fk = cluster.uniqueid and cluster.gluesite_fk = site.uniqueid;")
    sites_lcgces = int(cursor.fetchall()[0][0])

    # 8 Number of sites supporting MPI
    # Before, extracted from SAM API:
    # http://lcg-sam.cern.ch:8080/reports/lcg_deployment_C5.xsql
    #ces_mpi = doc.xpathEval("/LCG_DEPLOYMENT/LCG_CEs_and_CREAM_CEs_SUPPORTING_MPI/VALUE/TOTAL_CES")[0].content
    # Extracted from GStat 2.0
    cursor.execute("SELECT COUNT(*) FROM glue_gluemultivalued mv, glue_gluecluster cluster, glue_gluesite site WHERE mv.attribute = 'GlueHostApplicationSoftwareRunTimeEnvironment' and mv.value = 'MPI-START' and mv.uniqueid = cluster.uniqueid and cluster.gluesite_fk = site.uniqueid;")
    sites_mpi = int(cursor.fetchall()[0][0])

    # 9 Number of logical CPUs supporting MPI
    cursor.execute("SELECT SUM(subcluster.logicalcpus) FROM glue_gluemultivalued mv, glue_gluesubcluster subcluster WHERE mv.attribute = 'GlueHostApplicationSoftwareRunTimeEnvironment' and mv.value = 'MPI-START' and mv.uniqueid = subcluster.uniqueid;")
    logical_cpus_mpi = int(cursor.fetchall()[0][0])

    # 10. Installed Capacity per OS
    # Check with: ldapsearch -LLL -x -h lcg-bdii -p 2170 -b o=grid '(&(objectClass=GlueSubCluster)(GlueHostOperatingSystemName=*AIX*))'
    temp = {}
    cursor.execute("SELECT operatingsystemname, operatingsystemrelease, logicalcpus, benchmarksi00 FROM glue_gluesubcluster;")
    normalOS = ['AIX', 'Debian', 'Ubuntu', 'SUSE LINUX']
    rhelOS = ['Scientific Linux', 'Scientific Linux CERN', 'ScientificCERNSLC',
              'CentOS', 'ScientificSL', 'ScientificFermiLTS',
              'RedHatEnterpriseAS', 'RedHatEnterpriseWS', 'ScientificSLF']

    for row in cursor.fetchall():
        os = row[0]
        ver = row[1]
        if ver.find('.') != -1:
            ver = ver[0:ver.find('.')]
        logicalcpus = int(row[2])
        si2000 = logicalcpus * int(row[3])

        if os in normalOS:
            key = os + ' ' + ver
        elif os in rhelOS and ver != '':
            key = 'RHEL ' + ver + ' Compat'
        else:
            key = 'Unknown'

        if temp.has_key(key):
            tempkey = temp[key]
            tempkey[0] += 1
            tempkey[1] += logicalcpus
            tempkey[2] += si2000
        else:
            temp[key] = [1, logicalcpus, si2000]

    ic_per_os = []
    for key, value in temp.items():
        temp = [key]
        temp.extend(value)
        ic_per_os.append(temp)
    ic_per_os.sort(lambda x, y: cmp(x[0],y[0]))

    mySubClusters = 0
    myLogicalCPUs = 0
    mySI2000 = 0
    for temp in ic_per_os:
        mySubClusters += temp[1]
        myLogicalCPUs += temp[2]
        mySI2000 += temp[3]
    ic_per_os.append(['Total', mySubClusters, myLogicalCPUs, mySI2000])

    # 11. Installed Capacity per CE Type
    cursor.execute("SELECT SUM(a.logicalcpus) total_cpus FROM (SELECT subcluster.logicalcpus FROM glue_gluece ce, glue_gluecluster cluster, glue_gluesubcluster subcluster WHERE ce.implementationname = 'CREAM' and ce.gluecluster_fk = cluster.uniqueid and subcluster.gluecluster_fk = cluster.uniqueid GROUP BY subcluster.uniqueid) a;")
    temp = cursor.fetchall()[0]
    ic_per_ce = [['CREAM', int(temp[0])]]
    cursor.execute("SELECT SUM(a.logicalcpus) total_cpus FROM (SELECT subcluster.logicalcpus FROM glue_gluece ce, glue_gluecluster cluster, glue_gluesubcluster subcluster WHERE ce.implementationname = 'LCG-CE' and ce.gluecluster_fk = cluster.uniqueid and subcluster.gluecluster_fk = cluster.uniqueid GROUP BY subcluster.uniqueid) a;")
    temp = cursor.fetchall()[0]
    ic_per_ce.append(['LCG-CE', int(temp[0])])

    return {'date': datetime.utcnow().strftime("%Y-%m-%d %H:%MZ"),
            'total_number_sites': total_number_sites,
            'sites_ok': sites_ok,
            'sites_degraded': sites_degraded,
            'sites_down': sites_down,
            'sites_maint': sites_maint,
            'sites_na': sites_na,
            'sites_glite32': sites_glite32,
            'sites_glite31': sites_glite31,
            'jobs_this_month': jobs_this_month,
            'jobs_this_month_monthName': jobs_this_month_monthName,
            'jobs_this_month_monthNumber': jobs_this_month_monthNumber,
            'jobs_this_month_yearNumber': jobs_this_month_yearNumber,
            'jobs_last_month': jobs_last_month,
            'jobs_last_month_monthName': jobs_last_month_monthName,
            'jobs_last_month_monthNumber': jobs_last_month_monthNumber,
            'jobs_last_month_yearNumber': jobs_last_month_yearNumber,
            'creamces_unique_hosts': creamces_unique_hosts,
            'lcgces_unique_hosts': lcgces_unique_hosts,
            'sites_creamces': sites_creamces,
            'sites_lcgces': sites_lcgces,
            'sites_mpi': sites_mpi,
            'logical_cpus_mpi': logical_cpus_mpi,
            'ic_per_os': ic_per_os,
            'ic_per_ce': ic_per_ce}


import os
from typing import Union, Type

from telegram import Update, Bot, InlineKeyboardMarkup, ForceReply, ParseMode
from telegram.ext import CallbackContext

from tg.markup.keyboards import build_vote_keyboard
from models.phrase import Phrase, LongPhrase
from models.proposal import Proposal, LongProposal
from tg.decorators import log_update

curators_chat_id = os.environ.get("MOD_CHAT_ID", "")
SIMILARITY_DISCARD_THRESHOLD = int(
    os.getenv("PHRASE_DISMISSAL_SIMILARITY_THRESHOLD", 90)
)

proposal_t = Union[Type[Proposal], Type[LongProposal]]
phrase_t = Union[Type[Phrase], Type[LongPhrase]]

def _notify_proposal_to_curators(
    bot: Bot,
    proposal: Proposal,
    submitted_by: str,
    most_similar: Phrase,
    similarity_ratio: int,
) -> None:
    curators_reply_markup = InlineKeyboardMarkup(
        build_vote_keyboard(proposal.id, proposal.kind)
    )
    curators_message_text = (
        f"{submitted_by} dice que deberiamos añadir la siguiente {most_similar.name} a la lista:"
        f"\n'*{proposal.text}*'"
        f"\n\nLa mas parecida es: '*{most_similar}*' ({similarity_ratio}%)."
    )

    bot.send_message(
        curators_chat_id,
        curators_message_text,
        reply_markup=curators_reply_markup,
        parse_mode=ParseMode.MARKDOWN,
        disable_web_page_preview=True,
    )

def submit_handling(
    bot: Bot, update: Update, proposal_class: proposal_t, phrase_class: phrase_t
):
    submitted_by = update.effective_user.name
    proposal = proposal_class.from_update(update)
    if not proposal.text:
        return update.effective_message.reply_text(
            f"¿Qué *{phrase_class.name}* quieres proponer, {Phrase.get_random_phrase()}?\n"
            "Si no quieres proponer nada, puedes usar /cancelar.",
            reply_markup=ForceReply(selective=True),
            parse_mode=ParseMode.MARKDOWN,
        )

    # Fuzzy search. If we have one similar, discard.
    most_similar, similarity_ratio = phrase_class.get_most_similar(proposal.text)
    if SIMILARITY_DISCARD_THRESHOLD < similarity_ratio:
        text = f"Esa ya la tengo, {Phrase.get_random_phrase()}, {Phrase.get_random_phrase()}."
        if similarity_ratio != 100:
            text += f'\nSe parece demasiado a "<b>{most_similar}</b>", {Phrase.get_random_phrase()}.'
        return update.effective_message.reply_text(text, parse_mode=ParseMode.HTML)

    proposal.save()
    _notify_proposal_to_curators(
        bot, proposal, submitted_by, most_similar, similarity_ratio
    )

    update.effective_message.reply_text(
        f"Tu aportación será valorada por un consejo de cuñaos expertos y te avisaré una vez haya sido evaluada, "
        f"{Phrase.get_random_phrase()}.",
        quote=True,
    )

@log_update
def handle_submit(update: Update, context: CallbackContext):
    if len(update.effective_message.text.split(" ")) > 5:
        return update.effective_message.reply_text(
            f"¿Estás seguro de que esto es una frase corta, {Phrase.get_random_phrase()}?\n"
            f"Mejor prueba con /proponerfrase {Phrase.get_random_phrase()}.",
            quote=True,
        )
    submit_handling(context.bot, update, Proposal, Phrase)

@log_update
def handle_submit_long(update: Update, context: CallbackContext):
    if "submitlong" in update.effective_message.text:
        update.effective_message.reply_text(
            f"Te aviso, {Phrase.get_random_phrase()}, submitlong va a dejar de funcionar, si quieres ir de americano, "
            f"vas a tener que usar /submitphrase, {Phrase.get_random_phrase()}",
            quote=True,
        )
    submit_handling(context.bot, update, LongProposal, LongPhrase)


"""
Code to make a more informative plot of a mooring extraction, focused
on physical properties.
"""

# setup
import netCDF4 as nc
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.dates as mdates
from datetime import datetime
import seawater as sw

import os; import sys
sys.path.append(os.path.abspath('../alpha'))
import Lfun
Ldir = Lfun.Lstart(gridname='sj0', tag='v0')

sys.path.append(os.path.abspath('../plotting'))
import pfun

# choose the mooring extraction to plot
indir0 = Ldir['LOo'] + 'moor/'
if False:
    item = Lfun.choose_item(indir0)
    indir = indir0 + item + '/'
    infile = Lfun.choose_item(indir, tag='.nc')
else:
    indir = indir0 + 'sj0_v0_lo8nest_2019.09.01_2019.10.01/'
    infile = 'lost_tripod_hourly.nc'
fn = indir + infile

ds = nc.Dataset(fn)
lon = ds['lon_rho'][:]
lat = ds['lat_rho'][:]

dsg = nc.Dataset(Ldir['grid'] + 'grid.nc')

# get fields
ubar = ds['ubar'][:]
vbar = ds['vbar'][:]
u = ds['u'][:]
v = ds['v'][:]
salt = ds['salt'][:]
temp = ds['temp'][:]
pdens = sw.dens0(salt,temp)
z_w = ds['z_w'][:]
z_rho = ds['z_rho'][:]
ot = ds['ocean_time'][:]
t_md = Lfun.modtime_to_mdate_vec(ot)
t_dt = []
for t in ot:
    t_dt.append(Lfun.modtime_to_datetime(t))

def rot_vec(u,v,theta):
    ur = u*np.cos(theta) + v*np.sin(theta)
    vr = v*np.cos(theta) - u*np.sin(theta)
    return ur, vr

def de_mean(u, v):
    up = u - np.nanmean(u)
    vp = v - np.nanmean(v)
    return up, vp

# find angle of principal axes
up, vp = de_mean(ubar, vbar)
# we mask out high speeds because they introduced a funny direction
spd_lim = 0.5
spd = np.sqrt(up**2 + vp**2)
up[spd>spd_lim] = np.nan
vp[spd>spd_lim] = np.nan
up, vp = de_mean(up, vp)
theta = 0.5 * np.arctan2(2*np.nanmean(up*vp),(np.nanvar(up)-np.nanvar(vp)))

# and rotate
ubar_r, vbar_r = rot_vec(ubar, vbar, theta)
u_r, v_r = rot_vec(u,v,theta)

# plotting
plt.close('all')
fig = plt.figure(figsize=(18,10))

# map
ax = fig.add_subplot(3,4,4)
ax.plot(lon,lat,'*r')
pad = .1
ax.axis([lon-pad, lon+pad, lat-pad, lat+pad])
pfun.add_coast(ax)
pfun.dar(ax)

# depth averaged velocity scatterplot
V = 1
cmap = 'Spectral_r'
cmap = 'jet'
ax = fig.add_subplot(3,4,8)
ax.axhline(); ax.axvline()
ax.plot(ubar,vbar,'ob', alpha=.2, markersize=3)
ax.plot(ubar_r, vbar_r,'o', color='gold', alpha=.5, markersize=3)
ax.axis([-V,V,-V,V])
ax.grid(True)
ax.set_aspect('equal')
ax.axhline(); ax.axvline()

ax = plt.subplot2grid((2,4), (0,0), colspan=3)
T = (t_md)*np.ones((31,1))
tt = t_md - t_md[0]
cs = ax.pcolormesh(t_md, z_w.T, u_r.T - ubar_r, cmap=cmap, vmin=-V, vmax=V)
fig.colorbar(cs, ax=ax)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m-%d'))
ax.text(.05, .9, 'Along Channel (m/s Ebb Positive)', transform=ax.transAxes)
ax.set_ylim(top=5)

ax = plt.subplot2grid((2,4), (1,0), colspan=3)
T = (t_md)*np.ones((31,1))
tt = t_md - t_md[0]
cs = ax.pcolormesh(t_md, z_w.T, v_r.T -vbar_r, cmap=cmap, vmin=-V, vmax=V)
fig.colorbar(cs, ax=ax)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m-%d'))
ax.text(.05, .9, 'Cross Channel (m/s NE Positive)', transform=ax.transAxes)
ax.set_ylim(top=5)
#fig.autofmt_xdate() 3 rotates labels

plt.show()


import numpy as np
import matplotlib.pyplot as plt

# a = np.random.randn(100, 5)
# soft = np.exp(a)
# soft = soft/soft.sum(axis=1, keepdims=True)
# print(soft.sum())

def sigmoid(a):
    return 1 / (1+np.exp(-a))

def forward(X, W1, b1, W2, b2):
    Z1 = sigmoid(W1.dot(X) + b1)
    out = np.exp(W2.dot(Z1) + b2)
    out = out/out.sum(axis=1, keepdims= True)
    return out

def classification(T, Y):
    count= 0
    for i in range(len(Y)):
        if Y[i] == T[i]:
            count+=1
    return count/len(Y)

N = 500
X1 = np.random.randn(N, 2) + np.array([0, -2])
X2 = np.random.randn(N, 2) + np.array([2, 2])
X3 = np.random.randn(N, 2) + np.array([-2, 2])
X = np.vstack((X1, X2, X3))
X = X.T
print(X.shape)
Y = np.array([0] * N + [1] * N + [2] * N).T
plt.scatter(X[0,:], X[1,:], c=Y, s=100, alpha=0.5)
plt.show()

#dimesnions is 2
D = 2
#hidden layer perceptrons 3
M = 3
#outputlayer is 3
K = 3

W1 = np.random.randn(M, D)
b1 = np.random.randn(M,1)
W2 = np.random.randn(K, M)
b2 = np.random.randn(K,1)

Yhat = forward(X, W1, b1, W2, b2)
print(np.shape(Yhat))
Yhat = np.argmax(Yhat, axis = 0)
print(np.shape(Yhat))
print("Classification rate for randomly chosen weights:",classification(Y, Yhat))


#!/usr/bin/env python
# -*- coding: utf-8 -*-
from typing import Optional

from sqlalchemy.orm import Session

from app.models.repo import PaginatedRepoModel
from app.models.repo import RepoModel
from app.models.repo import RepoWrModel
from app.schemas.repo import Repo

class RepoRepository:
    def __init__(self, db: Session) -> None:
        self.db = db

    def get_repo(self, repo_id: str) -> Optional[Repo]:
        return self.db.query(Repo).filter(Repo.id == repo_id).first()

    def get_repos(
        self, page=0, per_page=20, search_id: Optional[str] = None
    ) -> PaginatedRepoModel:
        filters = []
        if search_id:
            filters.append((Repo.search_id == search_id))
        query = (
            self.db.query(Repo) if not filters else self.db.query(Repo).filter(*filters)
        )

        repos_len = len(query.all())
        if per_page:
            query = query.limit(per_page)
        if page:
            query = query.offset(page * per_page)
        return PaginatedRepoModel(
            pagination=dict(total_count=repos_len, page=page + 1, per_page=per_page),
            items=[RepoModel.from_orm(r) for r in query.all()],
        )

    def create_repo(self, model: RepoWrModel, commit=True) -> RepoWrModel:
        db_item = Repo(**model.dict())
        self.db.add(db_item)
        if commit:
            self.db.commit()
            self.db.refresh(db_item)
        return model


import importlib
import inspect
import os
from collections import OrderedDict

from rest_framework import serializers, views

from drf_typescript_generator.globals import (
    CHOICES_TRANSFORM_FUNCTIONS_BY_TYPE, DEFAULT_TYPE, MAPPING, SPECIAL_FIELD_TYPES
)

def _is_api_view(member):
    """ Returns whether the `member` is drf api view """
    return inspect.isclass(member) and views.APIView in inspect.getmro(member)

def _is_serializer_class(member):
    """ Returns whether the `member` is drf serializer class or not """
    return inspect.isclass(member) and serializers.BaseSerializer in inspect.getmro(member)

def _to_camelcase(s):
    parts = s.split('_')
    return parts[0] + ''.join([part.capitalize() for part in parts[1:]])

def _check_for_nullable(field, typescript_type):
    if field.allow_null:
        typescript_type += ' | null'
    return typescript_type

def _get_project_name():
    return os.environ['DJANGO_SETTINGS_MODULE'].split('.')[0]

def _get_typescript_name(field, field_name, options={}):
    if options.get('preserve_case', False):
        typescript_field_name = field_name
    else:
        typescript_field_name = _to_camelcase(field_name)
    if not field.read_only and not field.required:
        typescript_field_name += '?'
    return typescript_field_name

def _get_method_return_value_type(field, field_name, serializer_instance):
    """
    For given method field function looks for return type of corresponding
    method in type annotations.
    """
    method_name = field.method_name if field.method_name else f'get_{field_name}'
    method = getattr(serializer_instance, method_name)
    method_signature = inspect.signature(method)
    return MAPPING.get(method_signature.return_annotation, DEFAULT_TYPE), False

def _get_choice_selection_fields_type(field):
    """
    Returns composite typescript type for choice selection field
    by enumerating its choices. Also takes into account the
    allow_blank argument.
    """
    def transform_choice(v):
        return str(CHOICES_TRANSFORM_FUNCTIONS_BY_TYPE[type(v)](v))

    typescript_type = ' | '.join([transform_choice(choice) for choice in field.choices.keys()])
    is_list = type(field) == serializers.MultipleChoiceField
    if field.allow_blank:
        typescript_type += ' | ""'

    return typescript_type, is_list

def _handle_special_field_type(field, field_name, serializer_instance):
    """
    This function is used for fields that require custom logic for
    deriving its type. Custom function based on field type is called
    in sake of finding typescript type for this field.
    """
    if type(field) == serializers.SerializerMethodField:
        return _get_method_return_value_type(field, field_name, serializer_instance)
    elif type(field) in [serializers.ChoiceField, serializers.MultipleChoiceField]:
        return _get_choice_selection_fields_type(field)
    else:
        raise NotImplementedError(
            f'Handling of {type(field).__name__} special field is not implemented'
        )

def _handle_nonspecial_field_type(field):
    """
    Function determines typescript type for field that does not
    require special logic to derive its type e.g. field in
    String fields category, Numeric fields category, ...
    """
    # core type is type of child in listfield / nested serializer with many=True
    is_list = hasattr(field, 'child')
    field_type = type(field.child) if is_list else type(field)

    if _is_serializer_class(field_type):
        typescript_type = field_type.__name__
    else:
        typescript_type = MAPPING.get(field_type, DEFAULT_TYPE)
    return typescript_type, is_list

def _get_typescript_type(field, field_name, serializer_instance):
    """
    Returns typescript type for given field based on global mapping.
    Supports also method fields, list like fields and nested serializers.
    Types derivation is not recursive (for nested serializer type
    {serializer.__name__} is returned).
    If mapping for field type was not found default type is returned.
    """
    if type(field) in SPECIAL_FIELD_TYPES:
        typescript_type, is_list = _handle_special_field_type(field, field_name, serializer_instance)
    else:
        typescript_type, is_list = _handle_nonspecial_field_type(field)

    typescript_type = _check_for_nullable(field, typescript_type)

    if is_list and '|' in typescript_type:
        # composite type array needs to be in parenthesis e.g. (number | null)[]
        typescript_type = f'({typescript_type})'

    return typescript_type + ('[]' if is_list else '')

def get_nested_serializers(serializer):
    """
    Finds nested serializers in given serializer. Returns
    ordered dictionary with keys being name of the nested
    serializers and values nested serializer classes.
    """
    serializer_instance = serializer()
    fields = serializer_instance.get_fields()
    nested_serializers = {}
    for field in fields.values():
        is_list = hasattr(field, 'child')
        field_type = type(field.child) if is_list else type(field)
        if _is_serializer_class(field_type):
            nested_serializers[field_type.__name__] = field_type

    return OrderedDict(sorted(nested_serializers.items()))

def get_serializer_fields(serializer, options={}):
    """
    Determines a typescript type for every field in the serializer.
    Returns ordered dictionary with keys being transformed field names to
    typescript names (including `?` if field is optional) and values
    being typescript types.
    """
    serializer_instance = serializer()
    fields = serializer_instance.get_fields()
    typescript_fields = {}
    for field_name, field in fields.items():
        typescript_field_name = _get_typescript_name(field, field_name, options)
        typescript_type = _get_typescript_type(field, field_name, serializer_instance)
        typescript_fields[typescript_field_name] = typescript_type

    return OrderedDict(sorted(typescript_fields.items()))

def get_app_api_views(app_name):
    """ Returns all api views classes found in {app_name}.urls module """
    try:
        urls_module = importlib.import_module('.urls', package=app_name)
        return inspect.getmembers(urls_module, _is_api_view)
    except ImportError:
        return []

def get_project_api_views():
    """ Returns all api views classes found in project urls module """
    return get_app_api_views(_get_project_name())

def get_module_serializers(module):
    """ Returns all serializer classes found in given module """
    try:
        urls_module = importlib.import_module(module)
        return inspect.getmembers(urls_module, _is_serializer_class)
    except ImportError:
        return []

def export_serializer(serializer_name, fields, options):
    def format_field(field, indent):
        formatted = f'{indent}{field[0]}: {field[1]}'
        if options['semicolons']:
            formatted += ';'
        return formatted

    indent = '\t' * options['tabs'] if options['tabs'] is not None else ' ' * options['spaces']
    attributes = '\n'.join([format_field(field, indent) for field in fields.items()])

    if options['format'] == 'type':
        template = 'export type {} = {{\n{}\n}}\n\n'
    else:
        template = 'export interface {} {{\n{}\n}}\n\n'

    return template.format(serializer_name, attributes)


import sys

reading = lambda :sys.stdin.readline().strip()

A, B = reading().split()
len_A = len(A)
len_B = len(B)
_min = 50

for i in range(len_B - len_A + 1):
    cnt = 0
    for j in range(len_A):
        if A[j] != B[i + j]:
            cnt += 1
    _min = min(cnt, _min)

print(_min)


from django.db import models
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django_filters.rest_framework import DjangoFilterBackend
from djoser.views import UserViewSet
from rest_framework import exceptions, status, viewsets
from rest_framework.decorators import action
from rest_framework.permissions import (IsAuthenticated,
                                        IsAuthenticatedOrReadOnly)
from rest_framework.response import Response
from foodgram.pagination import CustomPagination
from recipes.models import (Favorite, Ingredient, Recipe, RecipeIngredient,
                            ShoppingCart, Tag)
from users.models import Follow, User

from api.filters import IngredientFilter, RecipiesFilter
from api.permissions import RecipePermission
from api.serializers import (FavoriteSerializer, FollowSerializer,
                             IngredientSerializer,
                             RecipeCreateUpdateSerializer,
                             RecipeListSerializer, RecipeSerializer,
                             ShoppingCartSerializer, TagSerializer,
                             UserCreateSerializer, UserWithRecipesSerializer)

class UserViewSet(UserViewSet):
    """User ViewSet."""

    permission_classes = (IsAuthenticated,)
    pagination_class = CustomPagination

    @action(
        detail=False,
        methods=('GET',),
        serializer_class=UserWithRecipesSerializer,
        permission_classes=(IsAuthenticated, )
    )
    def subscriptions(self, request):
        queryset = User.objects.filter(subscribers__user=request.user)
        paginated_queryset = self.paginate_queryset(queryset)
        serializer = self.get_serializer(paginated_queryset, many=True)
        return self.get_paginated_response(serializer.data)

    action_serializer = UserCreateSerializer

    @action(
        detail=True,
        methods=('POST', 'DELETE'),
        serializer_class=UserWithRecipesSerializer
    )
    def subscribe(self, request, id=None):
        user = self.request.user
        author = get_object_or_404(User, pk=id)
        if self.request.method == 'POST':
            context = {'request': request}
            data = {
                'user': user.id,
                'author': author.pk
            }
            serializer = FollowSerializer(data=data, context=context)
            serializer.is_valid(raise_exception=True)
            serializer.save()
            return Response(
                serializer.data,
                status=status.HTTP_201_CREATED
            )
        subscription = get_object_or_404(
            Follow,
            user=user,
            author=author,
        )
        subscription.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

class RecipeViewSet(viewsets.ModelViewSet):
    """Recipe ViewSet."""

    queryset = Recipe.objects.all()
    permission_classes = (RecipePermission,)
    pagination_class = CustomPagination
    filter_backends = (DjangoFilterBackend,)
    filterset_class = RecipiesFilter

    def get_serializer_class(self):
        if self.action in ('create', 'partial_update'):
            return RecipeCreateUpdateSerializer
        return RecipeSerializer

    def favorite_logic(self, user, recipe):
        serializer = FavoriteSerializer(
            data={'user': user.id, 'recipe': recipe.id}
        )
        serializer.is_valid(raise_exception=True)
        serializer.save()
        favorite_serializer = RecipeListSerializer(recipe)
        return favorite_serializer.data

    @action(detail=True, methods=('POST', 'DELETE'))
    def favorite(self, request, pk=None):
        user = self.request.user
        recipe = get_object_or_404(Recipe, pk=pk)
        if self.request.method == 'POST':
            favorite_data = self.favorite_logic(user, recipe)
            return Response(
                favorite_data,
                status=status.HTTP_201_CREATED
            )
        favorite = Favorite.objects.filter(user=user, recipe=recipe)
        if not favorite:
            raise exceptions.ValidationError(
                'The recipe is not in list of favorites!'
            )
        favorite.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

    def shopping_cart_logic(self, user, recipe):
        serializer = ShoppingCartSerializer(
            data={'user': user.id, 'recipe': recipe.id}
        )
        serializer.is_valid(raise_exception=True)
        serializer.save()
        shopping_cart_serializer = RecipeListSerializer(recipe)
        return shopping_cart_serializer.data

    @action(detail=True, methods=('POST', 'DELETE'))
    def shopping_cart(self, request, pk=None):
        user = self.request.user
        recipe = get_object_or_404(Recipe, pk=pk)
        if self.request.method == 'POST':
            shopping_cart_data = self.shopping_cart_logic(user, recipe)
            return Response(
                shopping_cart_data,
                status=status.HTTP_201_CREATED
            )
        shopping_cart = ShoppingCart.objects.filter(user=user, recipe=recipe)
        if not shopping_cart:
            raise exceptions.ValidationError(
                'The recipe is not in list of shopping_cart!'
            )
        shopping_cart.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

    @action(
        detail=False,
        methods=('GET',),
        permission_classes=(IsAuthenticated,)
    )
    def download_shopping_cart(self, request):
        shopping_cart = ShoppingCart.objects.filter(user=self.request.user)
        recipes = [item.recipe.id for item in shopping_cart]
        ingredients = (
            RecipeIngredient.objects.filter(recipe_id__in=recipes)
            .values("product_id__name", "product_id__measurement_unit")
            .annotate(models.Sum("amount"))
        )
        purchase_list_text = 'Purchase list:\n\n'
        for item in ingredients:
            purchase_list_text += (
                f'{item["product_id__name"]}, {item["amount__sum"]} '
                f'{item["product_id__measurement_unit"]}\n'
            )
        response = HttpResponse(purchase_list_text, content_type='text/plain')
        response['content-disposition'] = (
            'attachment; filename=purchase_list.txt'
        )
        return response

class IngredientViewSet(viewsets.ModelViewSet):
    """Ingredient ViewSet."""

    queryset = Ingredient.objects.all()
    serializer_class = IngredientSerializer
    permission_class = (RecipePermission,)
    pagination_class = None
    filter_backends = (DjangoFilterBackend,)
    filterset_class = IngredientFilter

class TagViewSet(viewsets.ModelViewSet):
    """Tag ViewSet."""

    queryset = Tag.objects.all()
    serializer_class = TagSerializer
    permission_class = (IsAuthenticatedOrReadOnly)
    pagination_class = None


import pprint
from hail.typecheck import typecheck_method, sequenceof
from hail.utils.java import jiterable_to_list
from hail.expr.types import dtype, hail_type

class tblockmatrix(object):
    __slots__ = 'element_type', 'shape', 'is_row_vector', 'block_size'

    @staticmethod
    def _from_java(jtbm):
        return tblockmatrix(
            dtype(jtbm.elementType().toString()),
            jiterable_to_list(jtbm.shape()),
            jtbm.isRowVector(),
            jtbm.blockSize())

    @staticmethod
    def _from_json(json):
        return tblockmatrix(element_type=dtype(json['element_type']),
                            shape=json['shape'],
                            is_row_vector=json['is_row_vector'],
                            block_size=json['block_size'])

    @typecheck_method(element_type=hail_type, shape=sequenceof(int), is_row_vector=bool, block_size=int)
    def __init__(self, element_type, shape, is_row_vector, block_size):
        self.element_type = element_type
        self.shape = shape
        self.is_row_vector = is_row_vector
        self.block_size = block_size

    def to_dict(self):
        return dict(element_type=str(self.element_type),
                    shape=self.shape,
                    is_row_vector=self.is_row_vector,
                    block_size=self.block_size)

    def __eq__(self, other):
        return isinstance(other, tblockmatrix) and \
            self.element_type == other.element_type and \
            self.shape == other.shape and \
            self.is_row_vector == other.is_row_vector and \
            self.block_size == other.block_size

    def __hash__(self):
        return 43 + hash(str(self))

    def __repr__(self):
        return f'tblockmatrix(element_type={self.element_type!r}, shape={self.shape!r}, ' \
            f'is_row_vector={self.is_row_vector!r}, block_size={self.block_size!r})'

    def __str__(self):
        return f'blockmatrix {{element_type: {self.element_type}, shape: {self.shape}, ' \
            f'is_row_vector: {self.is_row_vector}, block_size: {self.block_size})'

    def pretty(self, indent=0, increment=4):
        b = []
        b.append(' ' * indent)
        b.append('blockmatrix {\n')
        indent += increment

        b.append(' ' * indent)
        b.append('element_type: ')
        self.element_type._pretty(b, indent, increment)
        b.append(',\n')

        b.append(' ' * indent)
        b.append(f'shape: [{self.shape}],\n')

        b.append(' ' * indent)
        b.append('is_row_vector: ')
        self.is_row_vector._pretty(b, indent, increment)
        b.append(',\n')

        b.append(' ' * indent)
        b.append('block_size: ')
        self.block_size._pretty(b, indent, increment)
        b.append(',\n')

        indent -= increment
        b.append(' ' * indent)
        b.append('}')

        return ''.join(b)

_old_printer = pprint.PrettyPrinter

class PrettyPrinter(pprint.PrettyPrinter):
    def _format(self, object, stream, indent, allowance, context, level):
        if isinstance(object, tblockmatrix):
            stream.write(object.pretty(self._indent_per_level))
        else:
            return _old_printer._format(self, object, stream, indent, allowance, context, level)

pprint.PrettyPrinter = PrettyPrinter  # monkey-patch pprint


#!/usr/bin/env python3

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

fpath = '/media/findux/DATA/R/data/ETOPO1_Ice_g_geotiff.tif'
res = 0.05

img = cv2.imread(fpath)

height, width = img.shape[:2]

h = int(height * res)
w = int(width * res)

img_small = cv2.resize(img, (w, h))
cv2.imshow('Resized', img_small)
while True:
    if cv2.waitKey(0) & 0xFF == ord('q'):
        break