from flask import request
from flask_restplus import Resource

from app.api.users import namespace, user_info_namespace

from app.api.users.models import user, user_response, user_info, user_info_response

@namespace.route('')
class UserList(Resource):

    @namespace.doc('list_users')
    # @namespace.marshal_list_with(user_full_info)
    def get(self):
        """
        Get all users.
        """
        from app.api import container
        return container.facades.users().get_all()

    @namespace.doc('add_user')
    @namespace.response(200, 'Success', user_response)
    def post(self):
        """
        Create a new user.
        """
        data = request.json
        from app.api import container
        # return container.services.users().create(data)
        return container.facades.users.create_user(data)

@namespace.route('/<id>')
@namespace.doc(params={'election_id': {'description': 'The election id'}})
@namespace.param('id', 'The user identifier')
@namespace.response(404, 'User not found')
class User(Resource):

    @namespace.doc('get_user')
    def get(self, id):
        """
        Get a user by id.
        """
        from app.api import container
        return container.services.users().get_one(id)

    @namespace.doc('update_user')
    @namespace.expect(user)
    def put(self, id):
        """
        Update existing user.
        """
        data = request.json
        from app.api import container
        return container.services.users().update(id, data)

    def delete(self, id):
        """
        Delete existing user.
        """
        from app.api import container
        return container.services.users().delete(id)

# USER INFO

@user_info_namespace.route('')
class UserInfoList(Resource):

    @user_info_namespace.doc('add user info')
    @user_info_namespace.expect(user_info)
    @user_info_namespace.response(200, 'Success', user_info_response)
    def post(self):
        """
        Add user info
        """
        data = request.json
        from app.api import container
        return container.services.user_info().create(data)

@user_info_namespace.route('/<id>')
@user_info_namespace.param('id', 'The user identifier')
@user_info_namespace.response(404, 'User info not found')
class UserInfo(Resource):

    @user_info_namespace.marshal_list_with(user_info_response)
    @user_info_namespace.response(200, 'Success', user_info_response)
    @user_info_namespace.doc('get_user_info')
    def get(self, id):
        """
        Get a user info by user id.
        """
        from app.api import container
        return container.services.user_info().get_one(id)

    @user_info_namespace.doc('update_user_info')
    @user_info_namespace.expect(user_info)
    def put(self, id):
        """
        Update existing user info by user id.
        """
        data = request.json
        from app.api import container
        return container.services.user_info().update(id, data)

    def delete(self, id):
        """
        Delete existing user info by user id.
        """
        from app.api import container
        return container.services.user_info().delete(id)


#!/usr/bin/env python

import cgi
import sqlite3

class HtmlResponse:

    def __init__(self):
        pass

    def build_response(self, insert_data):
        html_text = """Content-type: text/html

<!DOCTYPE html>

        <meta charset="utf-8">
        <title>Check create group | BioLabCorp</title>
        <link rel="stylesheet" href="../html/css/check_create_group.css">

            <form action="done_create_group.py" method="POST">

                    <input type="text" class="groupName" name="groupName" value="%s" readonly>

                    <input type="password" class="roomKeyNumber" name="keyNumber" value="%s" readonly>

                    <button type="submit" class="back" onclick="location.href='../html/create_group.html'">< Back</button>
                    <button type="submit" class="create" onclick="location.href='done_create_group.py'">Create</button>

            </form>

"""
        return html_text % insert_data

    def build_response_error(self, insert_data):
        html_text = """Content type: text/html

        <!DOCTYPE html>

                <meta http-equiv="refresh" content="0; url=../html/%s.html">

        """

        return html_text % insert_data

class DataBase:

    def __init__(self):
        # フルパスじゃないと上手くいかない...?
        # self.db_path = "/home/hal/Biolabcorp/Test/cgi-bin/db/test_db.sqlite"
        self.db_path = "./cgi-bin/db/test_db.sqlite"

    # DB の全データを読み込んで返す
    def read_db(self):
        conn = sqlite3.connect(self.db_path)
        cur = conn.cursor()

        ret = []
        try:
            for row in cur.execute("SELECT * FROM group_table"):
                ret.append(row)
        except:
            pass

        conn.close()
        return ret

    # 既に登録されていないか確認
    def check_group(self, group_name):
        if group_name == "":
            return -1

        num_group = 0
        for row in self.read_db():
            if row[1] == group_name:
                num_group = 1

        return num_group

if __name__ == "__main__":
    form = cgi.FieldStorage()
    group_name = form.getvalue("groupName", "")
    key_number = form.getvalue("keyNumber", "")

    html_response = HtmlResponse()
    data_base = DataBase()

    error_code = data_base.check_group(group_name)
    # 0: エラーなし / 1: group_name 重複 / -1:入力なし
    if error_code == -1:
        print(html_response.build_response_error("create_group"))

    if error_code == 0:
        print(html_response.build_response((group_name, key_number)))

    if error_code == 1:
        print(html_response.build_response_error("create_group_error"))


import uuid
import time
from app import app

TOKEN_LIFE_TIME = 86400

tokens = []

def create():
  token = {
    "id": str(uuid.uuid1()),
    "created": time.time(),
    "lifetime": TOKEN_LIFE_TIME
  }
  tokens.append( token )
  return token

def is_token_valid( token ):
  tok = next((x for x in tokens if x['id'] == token['id']), None)
  app.logger.error( tok )
  app.logger.error( tokens )
  return tok is not None and (time.time() < tok['created'] + TOKEN_LIFE_TIME)


# Adeptus Astartes Wargear Data

wargear = [
    {   # Astartes Shotgun
        "name":"Astartes Shotgun",
        "points":0,
    },
    {   # Auspex
        "name":"Auspex",
        "points":1,
    },
    {   # Auto Bolt Rifle
        "name":"Auto Bolt Rifle",
        "points":0,
    },
    {   # Auxiliary Grenade Launcher
        "name":"Auxiliary Grenade Launcher",
        "points":0,
    },
    {   # Bolt Carbine
        "name":"Bolt Carbine",
        "points":0,
    },
    {   # Bolt Pistol
        "name":"Bolt Pistol",
        "points":0,
    },
    {   # Bolt Rifle
        "name":"Bolt Rifle",
        "points":0,
    },
    {   # Boltgun
        "name":"Boltgun",
        "points":0,
    },
    {   # Camo Cloak
        "name":"Camo Cloak",
        "points":1,
    },
    {   # Chainsword
        "name":"Chainsword",
        "points":0,
    },
    {   # Combat Knife
        "name":"Combat Knife",
        "points":0,
    },
    {   # Combi-flamer
        "name":"Combi-flamer",
        "points":3,
    },
    {   # Combi-grav
        "name":"Combi-grav",
        "points":2,
    },
    {   # Combi-melta
        "name":"Combi-melta",
        "points":3,
    },
    {   # Combi-plasma
        "name":"Combi-plasma",
        "points":4,
    },
    {   # Flamer
        "name":"Flamer",
        "points":3,
    },
    {   # Frag Grenade
        "name":"Frag Grenade",
        "points":0,
    },
    {   # Grapnel Launcher
        "name":"Grapnel Launcher",
        "points":1,
    },
    {   # Grav-chute
        "name":"Grav-chute",
        "points":1,
    },
    {   # Grav-gun
        "name":"Grav-gun",
        "points":2,
    },
    {   # Grav-pistol
        "name":"Grav-pistol",
        "points":1,
    },
    {   # Heavy Bolt Pistol
        "name":"Heavy Bolt Pistol",
        "points":0,
    },
    {   # Heavy Bolter
        "name":"Heavy Bolter",
        "points":3,
    },
    {   # Krak Grenade
        "name":"Krak Grenade",
        "points":0,
    },
    {   # Meltagun
        "name":"Meltagun",
        "points":3,
    },
    {   # Missle Launcher
        "name":"Missle Launcher",
        "points":5,
    },
    {   # Plasma Gun
        "name":"Plasma Gun",
        "points":3,
    },
    {   # Plasma Pistol
        "name":"Plasma Pistol",
        "points":1,
    },
    {   # Power Fist
        "name":"Power Fist",
        "points":4,
    },
    {   # Power Sword
        "name":"Power Sword",
        "points":2,
    },
    {   # Shock Grenade
        "name":"Shock Grenade",
        "points":0,
    },
    {   # Sniper Rifle
        "name":"Sniper Rifle",
        "points":1,
    },
    {   # Stalker Bolt Rifle
        "name":"Stalker Bolt Rifle",
        "points":0,
    },
],


N, M = map(int, input().split())

def mySelect(n, m):
    if m == 1:
        myList = []
        for i in range(1, n+1):
            myList.append([i])
        return myList
    else:
        myList = mySelect(n, m-1)
        copyList = []
        for j in myList:
            indx = j[0]
            for k in range(1, n+1):
                if k != indx and k > indx:
                    copyList.append([indx, k])


"""microfview.video module

Provides FMFCapture class for FlyMovieFormat videos.
"""

import logging
import os.path

import cv2
import numpy as np

from . import CaptureBase, SeekError

def decode_4cc(capture):
    prop = getattr(cv2,'CAP_PROP_FOURCC',6) #keep compat with OpenCV < 3.0
    fourcc = int(capture.get(prop))
    if fourcc > 0:
        return "%c%c%c%c" % (chr(fourcc & 255),
                             chr((fourcc >> 8) & 255),
                             chr((fourcc >> 16) & 255),
                             chr((fourcc >> 24) & 255))
    else:
        return "????"

class VideoDeviceReadError(Exception):
    pass

class OpenCVCapture(CaptureBase):

    def __init__(self, identifier, is_file, **prop_config):
        """class for using opencv VideoCapture objects

        Args:
          identifier (str): identifier as per cv2.VideoCapture
          is_file (bool): true if this device backs a video file (mp4, etc)
        """
        super(OpenCVCapture, self).__init__()

        self._log = logging.getLogger('microfview.capture.OpenCVCapture')

        self._log.info('opening %s (file: %s)' % (identifier, is_file))

        self._identifier = identifier
        self._capture = cv2.VideoCapture(identifier)
        if not self._capture.isOpened():
            raise Exception("Unable to open %s" % identifier)

        self._frame_timestamp = 0.0
        self._frame_number = -1

        self._log.info('%s format: %s' % ("file" if is_file else "device",
                                          decode_4cc(self._capture)))

        #CaptureBase attributes
        self.fps = self._capture.get(getattr(cv2,"CAP_PROP_FPS",5))
        self.frame_width = self._capture.get(getattr(cv2,"CAP_PROP_FRAME_WIDTH",3))
        self.frame_height = self._capture.get(getattr(cv2,"CAP_PROP_FRAME_HEIGHT",4))
        self.is_video_file = is_file
        if is_file:
            self.frame_count = self._capture.get(getattr(cv2,"CAP_PROP_FRAME_COUNT",7))
            self.filename = os.path.abspath(identifier)
            self.supports_seeking = True
            self._log.warn("seeking is not reliable on some videos in opencv")
        self.noncritical_errors = VideoDeviceReadError,

        if np.isnan(self.fps):
            self._log.warn("unknown FPS")

    def grab_next_frame_blocking(self):
        """returns next frame."""
        #opencv post increments, so get these first
        ms = self._capture.get(getattr(cv2,"CAP_PROP_POS_MSEC",0))
        fn = self._capture.get(getattr(cv2,"CAP_PROP_POS_FRAMES",1))

        frame_timestamp = ms/1000.
        frame_number = int(fn)

        flag, frame = self._capture.read()

        if not flag:
            if self.is_video_file:
                #there is not true way for opencv to tell us we are at
                #the end of file - so add many scary heuristics
                if frame_number >= self.frame_count:
                    raise EOFError("File ended at frame: %d" % frame_number)
                #the frame didn't advance - truncated file
                fn2 = self._capture.get(getattr(cv2,"CAP_PROP_POS_FRAMES",1))
                if (fn > 0) and (fn == fn2):
                    raise EOFError("Truncated file at at frame: %d" % frame_number)
            raise VideoDeviceReadError

        self._frame_timestamp = frame_timestamp
        self._frame_number = frame_number

        return frame

    def seek_frame(self, n):
        """Seeks to the given frame in the files.

        Seeking on random videos is generally risky, so we do some sanity checking.
        Seeking to n=0 reopens the file (if this wraps a video file)

        :param n: framenumber
        :return: np.array
        """
        if self.is_video_file:
            if n == 0:
                self._capture.release()
                self._capture = cv2.VideoCapture(self._identifier)
            else:
                self._capture.set(getattr(cv2,"CAP_PROP_POS_FRAMES",1), n)
        else:
            raise ValueError("Seeking not available on video devices")

    def get_last_timestamp(self):
        """returns the timestamp of the last frame."""
        return self._frame_timestamp

    def get_last_framenumber(self):
        """returns the framenumber of the last frame."""
        return self._frame_number


from django.shortcuts import render
from django.shortcuts import render_to_response
# Create your views here.
from django.http import HttpResponse
from django.core.paginator import Paginator,PageNotAnInteger
from order.models import *

def index(request):
    pagenum = request.GET.get('pid')
    products = Product.objects.all()
    p = Paginator(products,5)
    try:
        page = p.page(pagenum)
    except PageNotAnInteger:
        page = p.page(1)

    return render(request,'index.html',{'user':request.user, 'p':p, 'page':page})

def query(request):
    pagenum = request.GET.get('pid')
    products = []
    try:
        products = Product.objects.filter(name__contains=request.POST['productname'])
    except:
        products = Product.objects.all()
    p = Paginator(products, 5)
    try:
        page = p.page(pagenum)
    except PageNotAnInteger:
        page = p.page(1)

    return render(request,'index.html', {'user': request.user, 'p': p, 'page': page})

def order(request):
    print(request.user)
    try:
        produce = Product.objects.get(id=request.GET.get('pid'))
        user = UserProfile.objects.get(username=request.user)
        orderobj = Order.objects.create(product=produce,user=user)
    except:
        orderobj = None

    return render(request,'order.html',{'order':orderobj})


from typing import List, Optional, Callable

from app_config import deployment_config
from simulation.simulation_error import SimulationError

# Singleton module
LogLine = str
JoinedLogLines = str  # One string
ErrorDescription = str
ErrorCheckFunction = Callable[[JoinedLogLines], ErrorDescription]

LINES_TO_ANALYZE = 10
UNKNOWN_ERROR_LAST_LINES_LOG = 3

def analyze_log(model_name: str, log_lines: List[LogLine]) -> Optional[SimulationError]:
    """
    Analyzes given lines of log in search of errors.
    @param model_name: Name of the Hydrus model
    @param log_lines: Lines of Hydrus simulation log to analyze
    @return: Simulation error if such took place
    """

    if deployment_config.LOCAL_DEBUG_MODE:
        whole_log = f"LOG FOR HYDRUS MODEL {model_name}:\n{''.join(log_lines)}"
        print(whole_log)

    joined_log_lines: JoinedLogLines = ''.join(log_lines[-LINES_TO_ANALYZE:])

    basic_error_checks: List[ErrorCheckFunction] = [_check_no_folder_error,
                                                    _check_convergence_error,
                                                    _check_initial_time_step_error,
                                                    _check_basic_info_error]

    for error_check_function in basic_error_checks:
        error = error_check_function(joined_log_lines)
        if error:
            return SimulationError(model_name, error)

    error = _check_no_file_error(joined_log_lines, model_name=model_name)
    if error:
        return SimulationError(model_name, error)

    error = _check_for_unknown_error(joined_log_lines, log_lines=log_lines)
    return SimulationError(model_name, error) if error else None

# Wrong path
def _check_no_folder_error(log_to_analyze: JoinedLogLines) -> Optional[ErrorDescription]:
    error_msg_content = "Folder with input data of the specified project does not exist"
    if error_msg_content in log_to_analyze:
        return "Problem with path to project - project might not exist or path may be too long or incorrect." \
               "Please, avoid uppercase letters and special symbols (except from '-' and '.') in the name of the model."
    return None

# Missing input file (ex. ATMOSPH.IN)
def _check_no_file_error(log_to_analyze: JoinedLogLines, model_name: str) -> Optional[ErrorDescription]:
    error_msg_content = "Open file error in file"
    if error_msg_content in log_to_analyze:
        not_found_file = log_to_analyze.split(f"{model_name}")[1].split('\n')[0][1:]
        return f"Problem occurred while opening file {not_found_file} - make sure it was provided inside the .zip " \
               f"file containing Hydrus model. "
    return None

# Delete values in one row of ATMOSPH.IN but leave number at the beginning of the row (tAtm)
def _check_convergence_error(log_to_analyze: JoinedLogLines) -> Optional[ErrorDescription]:
    error_msg_numerical_content = "Numerical solution"
    error_msg_convergence_content = "not converged"
    if error_msg_numerical_content in log_to_analyze and error_msg_convergence_content in log_to_analyze:
        return "Numerical solution cannot converge - check if input data is correct."
    return None

# Delete every row in ATMOSPH.IN but leave column names and last line (empty input)
def _check_initial_time_step_error(log_to_analyze: JoinedLogLines) -> Optional[ErrorDescription]:
    error_msg_content = "The first time-variable BC record is at time smaller than tInit+dtInit"
    if error_msg_content in log_to_analyze:
        return "Too big initial time step - consider lowering it. This error may occur as well if ATMOSPH.IN is empty."
    return None

# Delete line under 'Wat   lChem lTemp' in Selector.in
def _check_basic_info_error(log_to_analyze: JoinedLogLines) -> Optional[ErrorDescription]:
    error_msg_content = "Error when reading from an input file Selector.in BasicInformations"
    if error_msg_content in log_to_analyze:
        return "An error occurred while reading basic model information from file Selector.in. " \
               "Please check correctness of data stored in this file."
    return None

# Check if simulation ended successfully, if not return unknown error
def _check_for_unknown_error(log_to_analyze: JoinedLogLines, log_lines: List[LogLine]) -> Optional[ErrorDescription]:
    successful_simulation_docker = "Calculation complete"
    successful_simulation_local = "successfully"
    time_word = "time"

    if time_word in log_to_analyze and \
            (successful_simulation_docker in log_to_analyze or successful_simulation_local in log_to_analyze):
        return None

    log_without_backtrace = create_log_without_backtrace(log_lines)
    unknown_error_log_line_count = min(UNKNOWN_ERROR_LAST_LINES_LOG, len(log_without_backtrace))
    return f"Unknown error, last log lines: {'<br>'.join(log_without_backtrace[-unknown_error_log_line_count:])}"

def create_log_without_backtrace(log_lines: List[LogLine]) -> List[LogLine]:
    fortran_message_before_backtrace = "Fortran runtime error"
    for i, line in enumerate(log_lines):
        if line.startswith(fortran_message_before_backtrace):
            return log_lines[:i + 1]        # inclusive
    return log_lines


import dhooks
import json

def init():
    global config
    global hook

    config = json.load(open("config.json"))["discord"]
    hook = dhooks.Webhook(config["webhook"])

def send(author:str, msg:str):
    global hook

    message_content = f"<{author}> {msg}"

    hook.send(message_content)


from Test_Case_1.Necessary_Packages.Necessary_Packages_Import import *

def handle_state_field():
    # State field checking
    try:
        state_field = WebDriverWait(driver, 10, poll_frequency=1).until(
            EC.visibility_of_element_located((By.CSS_SELECTOR, "input#state")))
        state_field.send_keys("Calcutta")
        print("State entered successfully!")
    except Exception as e:
        print("State field exception: ", type(e).__name__)


bl_info = {
    "name" : "EPIC GS data Processor",
    "author" : "David C. King",
    "version" : (2, 79, 0),
    "location" : "View3D > Tools > Create",
    "description" : "Process EPIC.GS data files into full animations: 1 object per cell",
    "tracker_url" : "https://github.com/meekrob/c-blenderi/issues",
    "warning" : "",
    "wiki_url" : "https://github.com/meekrob/c-blenderi/wiki",
    "category" : "3D View"
}

import bpy
from bpy.props import StringProperty
from bpy.props import CollectionProperty
import sys

from mathutils import Vector

from epic_gs_data_addon import trace_lineage

def find_cell_or_parent(cellname):
    while bpy.data.objects.find(cellname) < 0:
        cellname = cellname[:-1]
        if not cellname:
            return -1

    return bpy.data.objects.find(cellname)

# mball elements #######
##object as returned by mball.elements.new:
##------------------------
##e.g.
#mball= bpy.data.metaballs.new("name")
#el = mball.elements.new()

def mball_el_translate_x(el, x):
    mball_translate(el,Vector((x,0,0)))

def mball_el_translate_y(el, y):
    mball_translate(el,Vector((0,y,0)))

def mball_el_translate_z(el, z):
    mball_translate(el,Vector((0,0,z)))

def mball_el_translate(el, vec):
    el.co = el.co + vec

# mball elements #######

def swap_obj_locations(obj1, obj2):
    loc = obj1.location.copy()
    obj1.location = obj2.location
    obj2.location = loc

def set_active_object(obj, context=bpy.context):
    context.scene.objects.active = obj

def select_only_and_make_active(obj,  context=bpy.context):
    set_active_object(obj, context)
    select_only(obj)

def select_by_prefix(prefix):
    for obj in bpy.context.scene.objects:
        if obj.name.startswith(prefix): obj.select = True

def assign_material_to_selected(material):
    assign_material_to_objects(material, bpy.context.selected_objects)

def assign_material_to_objects(material, objects):
    for obj in objects:
        obj.active_material = material

def assign_parent_to_selected(parent):
    assign_parent_to_objects(parent, bpy.context.selected_objects)

def assign_parent_to_objects(parent, objects):
    for obj in objects:
        obj.parent = parent

def scale_value(inval):
    MINV = 20000
    MAXV = 90000
    margin = MAXV - MINV
    percent_max = (inval - MINV) / margin
    return percent_max

def select_only(obj):
    bpy.ops.object.select_all(action='DESELECT')
    obj.select = True
    return obj

def dup_obj_return_new(obj):
    select_only(obj)
    bpy.ops.object.duplicate() # the duplicate becomes the only selected object
    new_obj = bpy.context.selected_objects[0]
    return new_obj

DIVISIONS_ONLY = True

class OBJECT_OT_epic(bpy.types.Operator):
    """Epic processing button"""
    bl_label = "Process file"
    bl_idname = "object.epic"
    bl_options = {'REGISTER'}

    run_this_shit = False

    def execute(self, context):
        if context.scene.render.engine != 'CYCLES':
            context.scene.render.engine = 'CYCLES'

        if context.scene.epic_gs_filename == '':
            self.report({'ERROR_INVALID_INPUT'}, "Need to specify a file")
            return {'CANCELLED'}

        self.report(
            {'INFO'},
            """Processing context.scene.epic_gs_filename: %s,\n context.scene.embryo_parent: %s,\n context.scene.default_cell_template: %s,\n context.scene.default_cell_material: %s""" %
            (context.scene.epic_gs_filename, context.scene.embryo_parent, context.scene.default_cell_template, context.scene.default_cell_material)
        )
        infilename = context.scene.epic_gs_filename
        # key the cell divisions only
        key_divisions_only = context.scene.key_divisions_only

        # set up the scene
        if not context.scene.default_cell_template:
            # add an object
            bpy.ops.mesh.primitive_uv_sphere_add(size=1, view_align=False, enter_editmode=False, location=(0, 0, 0), layers=layers_tuple())
            context.scene.default_cell_template = context.object

        cell_template = bpy.data.objects.get( context.scene.default_cell_template.name )

        if context.scene.E_cell_template:
            e_cell_template = bpy.data.objects.get( context.scene.E_cell_template.name )
        else:
            e_cell_template = cell_template

        if context.scene.MS_cell_template:
            ms_cell_template = bpy.data.objects.get( context.scene.MS_cell_template.name )
        else:
            ms_cell_template = cell_template

        if context.scene.P_cell_template:
            p_cell_template = bpy.data.objects.get( context.scene.P_cell_template.name )
        else:
            p_cell_template = p_cell_template

        # process data
        bpy.context.scene.frame_start = 1
        bpy.context.scene.frame_end = 2000
        min_time, max_time, big_data = trace_lineage.load_gs_epic_file(infilename)
        for celltype in trace_lineage.ALL_END_PNTS:
            current_cell_type_char = ''
            cell_type_char = celltype[0]
            print("Doing", celltype, file=sys.stderr)
            print("==============", file=sys.stderr)
            ### E cells
            if celltype.startswith('E'):
                new_cell = dup_obj_return_new(e_cell_template)
                new_mat = bpy.context.scene.e_cell_material
            ### MS cells
            elif celltype.startswith('MS'):
                new_cell = dup_obj_return_new(ms_cell_template)
                new_mat = bpy.context.scene.ms_cell_material
            ### P --> Z cells
            elif celltype.startswith('P') or celltype.startswith('Z'):
                new_cell = dup_obj_return_new(p_cell_template)
                new_mat = bpy.context.scene.p_cell_material
            else:
                new_cell = dup_obj_return_new(cell_template)
                new_mat = bpy.context.scene.default_cell_material

            new_cell.name = celltype
            new_cell.parent = context.scene.embryo_parent
            new_cell.active_material = new_mat
            do_thing(new_cell, celltype, min_time, max_time, big_data, key_divisions_only)

        # set active object to parent
        if context.scene.embryo_parent:
           set_active_object( context.scene.embryo_parent, context )
           select_only( context.scene.embryo_parent )

        return {'FINISHED'}
import time
NTICKS = 4

def do_thing(object, end_cell, min_time, max_time, big_data, key_divisions_only = True):
    last_cell = ''
    timer = time.time()
    for timepnt, row, found_cell in trace_lineage.search_gs_epic_file([end_cell], min_time, max_time, big_data):
        frame = timepnt *12
        print("%s: at cell division" % end_cell, str(timepnt) + ",", last_cell, "==>", found_cell, "in %.4f seconds" % (time.time() - timer), file=sys.stderr)

        synchronize = False
        if frame % 512 == 0:
            synchronize = True
        if timepnt == max_time:
            synchronize = True

        # cell divisions determined by "found_cell" being different from the last iteration, since it traverses a lineage tree in the search
        in_a_cell_division = False
        if found_cell != last_cell:
            print("%s: =========== AT CELL DIVISION ===========" % end_cell, file=sys.stderr)
            ticker = NTICKS
            timer = time.time()
            in_a_cell_division = True

        # always insert keyframes at synchronization points
        if synchronize:
            print("%s: =========== key_divisions_only %s, in_a_cell_division %s. SYNCHRONIZING ==========="
                        % (end_cell, key_divisions_only, in_a_cell_division),
                        file=sys.stderr
            )
            pass
        # skip intermediate keyframes
        elif key_divisions_only and not in_a_cell_division:
            print("%s: =========== key_divisions_only %s, in_a_cell_division %s. continuing ==========="
                        % (end_cell, key_divisions_only, in_a_cell_division),
                        file=sys.stderr
            )
            continue

        last_cell = found_cell

        if not row: continue

        bpy.context.scene.frame_current = frame
        object.location = (row['x']/100,row['y']/100,row['z']/10)
        size = row['size']/200
        object.scale = (size,size,size)
        object.keyframe_insert('scale')
        object.keyframe_insert('location')
        # do something with the material
        curMat = object.active_material
        #if 'ecdf' in row:
        if False:
            if False:
                curMat.node_tree.nodes['ColorRamp'].inputs[0].default_value = row['ecdf']
            else:
                curMat.node_tree.nodes['ColorRamp'].inputs[0].default_value = scale_value(row['value'])
            curMat.node_tree.nodes['ColorRamp'].inputs[0].keyframe_insert("default_value", frame = frame )

class OBJECT_OT_custompath(bpy.types.Operator):
    #bl_label = "Select epic.gs data file"
    bl_label = "Process EPIC.GS file"
    bl_idname = "object.custom_path"
    __doc__ = ""

    bl_options = {'REGISTER'}

    filename_ext = "*.csv"
    filter_glob = StringProperty(
        name = "Filename",
        description = "File to Process",
        default=filename_ext, options={'HIDDEN'}
    )

    filepath = StringProperty(
        name="File Path",
        description="Filepath used for importing txt files",
        maxlen= 1024,
        default= "")

    def execute(self, context):
        print("FILEPATH %s"%self.properties.filepath)#display the file name and current path
        context.scene.epic_gs_filename = self.properties.filepath
        return {'FINISHED'}

    def invoke(self, context, event):
        context.window_manager.fileselect_add(self)
        return {'RUNNING_MODAL'}

class file_processing_panel(bpy.types.Panel):
    """File processing panel"""
    bl_label = "Epic.gs.data processer"
    bl_idname = "OBJECT_PT_file_process"
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
    bl_category = "Create"
    bl_context = "objectmode"

    def draw(self, context):
        #obj = bpy.context.scene.objects.get("embryo_parent")
        scene = bpy.context.scene

        layout = self.layout
        row = layout.row()
        row.label(text="Open and process an epic.gs.data file:")
        row = layout.row(align=True)
        row.prop( context.scene, "epic_gs_filename", text="")
        p = row.operator( OBJECT_OT_custompath.bl_idname, text="", icon = "FILESEL")
        p.filepath = context.scene.epic_gs_filename

        row = layout.row()
        row.label(text="Setup:")

        row = layout.row()
        row.label(text="Smooth animation")
        row.prop(bpy.context.scene, "key_divisions_only", text="")

        # empty parent for everything
        row = layout.row()
        row.label(text="Embryo parent")
        row.prop_search(bpy.context.scene, "embryo_parent", bpy.context.scene, "objects", text="")

        # cell type object templates
        row = layout.row()
        row.label(text="Object template for cell types:")

        # default
        row = layout.row()
        split = row.split()
        col = split.column()
        col.label(text="Mesh template")
        col.prop_search(bpy.context.scene, "default_cell_template", bpy.context.scene, "objects", text="")
        col = split.column()
        col.label(text="Material template")
        col.prop_search(scene, "default_cell_material", bpy.data, "materials", text="")

        # E cell row
        row = layout.row()
        split = row.split()
        col = split.column()
        col.label(text="E cell:")
        col.prop(bpy.context.scene, "E_cell_template", text="")
        col = split.column()
        col.label(text="E cell material")
        col.prop_search(scene, "e_cell_material", bpy.data, "materials", text="")

        # MS cell row
        row = layout.row()
        split = row.split()
        col = split.column()
        col.label(text="MS cell:")
        col.prop_search(bpy.context.scene, "MS_cell_template", bpy.context.scene, "objects", text="")
        col = split.column()
        col.label(text="MS cell material")
        col.prop_search(scene, "ms_cell_material", bpy.data, "materials", text="")

        # P/Z cell row
        row = layout.row()
        split = row.split()
        col = split.column()
        col.label(text="P/Z cells:")
        col.prop_search(bpy.context.scene, "P_cell_template", bpy.context.scene, "objects", text="")
        col = split.column()
        col.label(text="P/Z cell material")
        col.prop_search(scene, "p_cell_material", bpy.data, "materials", text="")

        # Functions
        row = layout.row()
        row.label(text="Operations:")
        row = layout.row()
        p = row.operator(OBJECT_OT_epic.bl_idname)

def process_epic_gs_button(self, context):
    self.layout.operator('file.select_all_toggle',
        OBJECT_OT_custompath.bl_idname,
        icon = "FILESEL"
    )

def layers_tuple(selected=0):
    layer_list = [False] * 20
    layer_list[0] = True
    return tuple(layer_list)

# registration
def register():
    print("-register-", file=sys.stderr)

    """
    # While trying to activate as an add-on
    # AttributeError: '_RestrictContext' object has no attribute 'scene'
    if bpy.context.scene.objects.find("embryo_parent") < 0:
        bpy.ops.object.empty_add(
            type='PLAIN_AXES',
            view_align=False,
            location=(0, 0, 0),
            layers=layers_tuple()
        )
        bpy.context.object.name = "embryo_parent"
    """
    bpy.types.Scene.key_divisions_only = bpy.props.BoolProperty(
        name = "Key Divisions Only",
        default = True,
        description = "Only insert keyframes at divisions (smoother animation)"
    )

    bpy.types.Scene.epic_gs_filename = bpy.props.StringProperty(
        name = "Epic filename",
        default = "",
        description = "Epic gs name"
    )

    bpy.types.Scene.embryo_parent = bpy.props.PointerProperty(
        type=bpy.types.Object,
        name = "Embryo object parent",
        description = "All added objects are parented to this object",
        poll = empty_check_function
    )

    bpy.types.Scene.default_cell_template = bpy.props.PointerProperty(
        type=bpy.types.Object,
        name = "Default object to use for all cells",
        description = "This object will be cloned to produce any cells",
        poll = mesh_check_function
    )

    bpy.types.Scene.C_cell_template = bpy.props.PointerProperty(
        type=bpy.types.Object,
        name = "C Cell Object template",
        description = "This object will be cloned to produce all of the C cells in the data file",
        poll = mesh_check_function

    )
    bpy.types.Scene.D_cell_template = bpy.props.PointerProperty(
        type=bpy.types.Object,
        name = "D Cell Object template",
        description = "This object will be cloned to produce all of the D cells in the data file",
        poll = mesh_check_function

    )
    bpy.types.Scene.E_cell_template = bpy.props.PointerProperty(
        type=bpy.types.Object,
        name = "E Cell Object template",
        description = "This object will be cloned to produce all of the E cells in the data file",
        poll = mesh_check_function
    )
    bpy.types.Scene.MS_cell_template = bpy.props.PointerProperty(
        type=bpy.types.Object,
        name = "MS Cell Object template",
        description = "This object will be cloned to produce all of the MS cells in the data file",
        poll = mesh_check_function
    )
    bpy.types.Scene.P_cell_template = bpy.props.PointerProperty(
        type=bpy.types.Object,
        name = "P-->Z Cell Object template",
        description = "This object will be cloned to produce all of the P and Z cells in the data file",
        poll = mesh_check_function

    )
    bpy.types.Scene.default_cell_material = bpy.props.PointerProperty(
        type=bpy.types.Material,
        name = "Default cell material",
        description = "This material will be assigned to all cells",
        poll = material_check_function
    )

    bpy.types.Scene.e_cell_material = bpy.props.PointerProperty(
        type=bpy.types.Material,
        name = "E cell material",
        description = "This material will be assigned to all E cells",
        poll = material_check_function
    )

    bpy.types.Scene.ms_cell_material = bpy.props.PointerProperty(
        type=bpy.types.Material,
        name = "E cell material",
        description = "This material will be assigned to all E cells",
        poll = material_check_function
    )

    bpy.types.Scene.p_cell_material = bpy.props.PointerProperty(
        type=bpy.types.Material,
        name = "P/Z cell material",
        description = "This material will be assigned to all P and Z cells",
        poll = material_check_function
    )

    bpy.utils.register_module(__name__)
    #bpy.utils.register_class(OBJECT_OT_epic)
    #bpy.utils.register_class(OBJECT_OT_custompath)
    #bpy.utils.register_class(file_processing_panel)
    bpy.types.INFO_MT_mesh_add.append(process_epic_gs_button)

def mesh_check_function(self, obj):
    return obj.type == 'MESH'

def empty_check_function(self, obj):
    return obj.type == 'EMPTY'

def material_check_function(self, obj):
    return obj.type in ['SURFACE', 'WIRE', 'VOLUME', 'HALO'] # possible "material" types

def unregister():
    bpy.utils.unregister_module(__name__)
    #bpy.utils.unregister_class(OBJECT_OT_epic)
    #bpy.utils.unregister_class(OBJECT_OT_custompath)
    #bpy.utils.unregister_class(file_processing_panel)
    bpy.types.INFO_MT_mesh_add.remove(process_epic_gs_button)

    del bpy.types.Scene.key_divisions_only

    del bpy.types.Scene.epic_gs_filename
    del bpy.types.Scene.embryo_parent

    del bpy.types.Scene.default_cell_template
    del bpy.types.Scene.default_cell_material

    del bpy.types.Scene.C_cell_template
    del bpy.types.Scene.D_cell_template

    del bpy.types.Scene.E_cell_template
    del bpy.types.Scene.e_cell_material

    del bpy.types.Scene.MS_cell_template
    del bpy.types.Scene.ms_cell_material

    del bpy.types.Scene.P_cell_template
    del bpy.types.Scene.p_cell_material

if __name__ == "__main__":
    register()


import numpy as np
import glob
from PIL import Image
from sklearn.model_selection import train_test_split

#  ФУНКЦИЯ ДЛЯ ПРЕОБРАЗОВАНИЯ ФОТОГРАФИЙ ПОД НУЖНЫЙ ФОРМАТ
def get_images(path):
    image_paths = glob.glob(path)

    for image_file in image_paths:
        image = Image.open(image_file).resize((220, 220))
        image = np.asarray(image.convert('RGB')) / 255.0

        yield image

def get_image_data(path):
    images_data = []

    images = get_images(path)
    for img in images:
        images_data.append(img)

    return images_data

#  ФУНКЦИЯ ДЛЯ ПРЕОБРАЗОВАНИЯ ФАЙЛОВ К ФОТО ПОД НУЖНЫЙ ФОРМАТ
def get_txt(path):
    txt_data = []
    txt_path = glob.glob(path)

    for txt_file in txt_path:
        with open(txt_file, 'r', encoding='utf-8') as f:
            tmp = f.readline().split(' ')

            bndbox = [None] * 5
            bndbox[0] = int(tmp[0])
            bndbox[1] = int(tmp[1])
            bndbox[2] = int(tmp[2])
            bndbox[3] = int(tmp[3])
            bndbox[4] = int(tmp[4])

            txt_data.append(bndbox)

    return txt_data

def get_txt_index(path, index):
    txt_data = []
    txt_path = glob.glob(path)

    for txt_file in txt_path:
        with open(txt_file, 'r', encoding='utf-8') as f:
            tmp = f.readline().split(' ')
            tmp = int(tmp[index])

            if tmp == 2 and index == 0:
                tmp = 0
            elif index == 1 and index == 3:
                img = Image.open(os.path.splitext(txt_file)[0] + '.jpg')
                w, _ = img.size
                tmp = tmp / w
            elif index == 2 and index == 4:
                img = Image.open(os.path.splitext(txt_file)[0] + '.jpg')
                _, h = img.size
                tmp = tmp / h

            txt_data.append(tmp)

    return txt_data

# Получение всех данных
def get_data(path_train_jpg, path_train_txt, path_valid_jpg, path_valid_txt):
    images = get_image_data(path_train_jpg)
    txt = get_txt(path_train_txt)

    txt_class = get_txt_index(path_train_txt, 0)
    txt_xmin = get_txt_index(path_train_txt, 1)
    txt_ymin = get_txt_index(path_train_txt, 2)
    txt_xmax = get_txt_index(path_train_txt, 3)
    txt_ymax = get_txt_index(path_train_txt, 4)

    x_train, x_test, _, _ = train_test_split(images, txt, test_size=0.1)

    _, _, y_train_class, y_test_class = train_test_split(images, txt_class, test_size=0.1)
    _, _, y_train_xmin, y_test_xmin = train_test_split(images, txt_xmin, test_size=0.1)
    _, _, y_train_ymin, y_test_ymin = train_test_split(images, txt_ymin, test_size=0.1)
    _, _, y_train_xmax, y_test_xmax = train_test_split(images, txt_xmax, test_size=0.1)
    _, _, y_train_ymax, y_test_ymax = train_test_split(images, txt_ymax, test_size=0.1)

    images_valid = get_image_data('cats_dogs_dataset/valid/*.jpg')
    txt_class_valid = get_txt_index('cats_dogs_dataset/valid/*.txt', 0)
    txt_xmin_valid = get_txt_index('cats_dogs_dataset/valid/*.txt', 1)
    txt_ymin_valid = get_txt_index('cats_dogs_dataset/valid/*.txt', 2)
    txt_xmax_valid = get_txt_index('cats_dogs_dataset/valid/*.txt', 3)
    txt_ymax_valid = get_txt_index('cats_dogs_dataset/valid/*.txt', 4)

    x_train = np.array(x_train)
    x_test = np.array(x_test)

    y_train_class = np.array(y_train_class)
    y_train_xmin = np.array(y_train_xmin)
    y_train_ymin = np.array(y_train_ymin)
    y_train_xmax = np.array(y_train_xmax)
    y_train_ymax = np.array(y_train_ymax)

    y_test_class = np.array(y_test_class)
    y_test_xmin = np.array(y_test_xmin)
    y_test_ymin = np.array(y_test_ymin)
    y_test_xmax = np.array(y_test_xmax)
    y_test_ymax = np.array(y_test_ymax)

    x_valid = np.array(images_valid)
    txt_class_valid = np.array(txt_class_valid)
    txt_xmin_valid = np.array(txt_xmin_valid)
    txt_ymin_valid = np.array(txt_ymin_valid)
    txt_xmax_valid = np.array(txt_xmax_valid)
    txt_ymax_valid = np.array(txt_ymax_valid)

    y_train_cat = {'class': y_train_class,
                   'out': [y_train_xmin, y_train_ymin, y_train_xmax, y_train_ymax]}

    y_test_cat = {'class': y_test_class,
                  'out': [y_test_xmin, y_test_ymin, y_test_xmax, y_test_ymax]}

    y_valid_cat = {'class': txt_class_valid,
                   'out': [txt_xmin_valid, txt_ymin_valid, txt_xmax_valid, txt_ymax_valid]}

    return x_train, y_train_cat, x_test, y_test_cat, x_valid, y_valid_cat

if __name__ == '__main__':
    images_data = []

    images = get_images('cats_dogs_dataset/train/*.jpg')
    for img in images:
        images_data.append(img)

    print(len(images_data))


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        l = 0
        tmp = head
        while tmp :
            tmp = tmp.next
            l+=1
        ind = 0
        t = l - n
        if t == 0 :
            return head.next
        tmp = head
        while ind < t :
            ind+=1
            last = tmp
            tmp = tmp.next
        last.next = tmp.next
        return head

19. Remove Nth Node From End of List/solve.py


import unittest
from Converter.Output import Output
from Converter import Rules
import bs4

class OutputTest(unittest.TestCase):
    def setUp(self):
        html = 'ladyfafa'
        attributes_to_save, contents_to_save, tags, alls = Rules.get_separated_list_of_values_saved(
            [Rules.HasAttribute(does=True, condition='href')])
        self.soup = bs4.BeautifulSoup(html, 'html.parser')
        expression = '
[Contents]
'
        self.output = Output(expression, attributes=attributes_to_save[0], alls=[Rules.AllContents()])

    def test_make_tag(self):
        self.assertEqual(str(self.output.make_tag(self.soup.find('a'))),
                         '
ladyfafa
')


import random
import discord
from discord.ext import commands

class Nuke(commands.Cog):
    def __init__(self, client):
        self.client = client

    def get_channel_members(self, ctx):
        # Guild text channel
        if isinstance(ctx.channel, discord.TextChannel):
            return ctx.channel.members

        # Direct Message
        elif isinstance(ctx.channel, discord.DMChannel):
            return [ctx.channel.recipient, ctx.message.author]

        # Groupchat
        elif isinstance(ctx.channel, discord.GroupChannel):
            return ctx.channel.recipients

    @commands.command(
        brief="Lists the amount of members in the current channel.", aliases=["mc"]
    )
    async def membercount(self, ctx):
        members = self.get_channel_members(ctx)

        await ctx.message.edit(content=f"{len(members)} members")

    @commands.command(brief="Mass ping users in the current channel.", aliases=["mp"])
    async def massping(
        self, ctx, mention_limit: int = 20, message_count: int = 3, members=None
    ):
        if members is None:
            members = self.get_channel_members(ctx)

        await ctx.message.delete()

        pings = []
        for member in members:
            pings.append(member.mention)

        # Send pings
        for i in range(message_count):
            try:
                # Randomly shuffle members
                random.shuffle(pings)

                message = "".join(pings[:mention_limit])
                await ctx.channel.send(message, delete_after=0.00005)

            # Automod mention limits
            except discord.errors.HTTPException:
                return await self.massping(
                    ctx, mention_limit // 2, message_count, members
                )

    @commands.command(brief="Spam a message automatically", aliases=["s"])
    async def spam(self, ctx, amount, *, message):
        await ctx.message.edit(content=message)
        for i in range(0, int(amount)):  # amount - 1
            await ctx.channel.send(message)

async def setup(client):
    await client.add_cog(Nuke(client))


import os
import glob
import math
import sys
from io import BytesIO
import threading
from boto3.session import Session
import numpy as np
from mlchain import logger
from mlchain.config import object_storage_config
from mlchain.base.exceptions import MlChainError
from .base import MLStorage

cv2 = None

def import_cv2():
    if cv2 is None:
        import cv2 as cv
        return cv
    return cv2

class ProgressPercentage:
    def __init__(self, filename, filesize):
        self._filename = filename
        self._size = filesize
        self._seen_so_far = 0
        self._lock = threading.Lock()

    def __call__(self, bytes_amount):
        def convertSize(size):
            if (size == 0):
                return '0B'
            size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
            i_name = int(math.floor(math.log(size, 1024)))
            chunk = math.pow(1024, i_name)
            size = round(size / chunk, 2)
            return '%.2f %s' % (size, size_name[i_name])

        # To simplify, assume this is hooked up to a single filename
        with self._lock:
            self._seen_so_far += bytes_amount
            percentage = (self._seen_so_far / self._size) * 100
            sys.stdout.write(
                "\r%s  %s / %s  (%.2f%%)        " % (self._filename, convertSize(self._seen_so_far),
                                                     convertSize(self._size), percentage))
            sys.stdout.flush()

class ObjectStorage(MLStorage):
    def __init__(self, bucket=None, client=None, url=None,
                 access_key=None, secret_key=None, provider=None, ping=False):
        MLStorage.__init__(self)
        self.bucket = bucket or object_storage_config.BUCKET
        self._client = client
        self.url = url or object_storage_config.URL
        self.access_key = access_key or object_storage_config.ACCESS_KEY
        self.secret_key = secret_key or object_storage_config.SECRET_KEY
        self.provider = provider or object_storage_config.PROVIDER or 's3'
        if ping:
            try:
                self.client.list_buckets()
            except Exception:
                MlChainError("Can't connect to Object Storage", code="S000", status_code=500)
        if self.bucket:
            try:
                self.client.create_bucket(Bucket=self.bucket)
            except Exception:
                pass

    @property
    def client(self):
        if self._client is None:
            session = Session(
                aws_access_key_id=self.access_key,
                aws_secret_access_key=self.secret_key)
            self._client = session.client(self.provider, endpoint_url=self.url)
        return self._client

    def download_file(self, key, local_path, bucket_name=None, use_basename=False):
        """
        Download a file from S3.
        Args:
            key: `str`. S3 key that will point to the file.
            local_path: `str`. the path to download to.
            bucket_name: `str`. Name of the bucket in which to store the file.
            use_basename: `bool`. whether or not to use the basename of the key.
        """
        if not bucket_name:
            bucket_name = self.bucket
        if bucket_name is None:
            raise MlChainError("bucket can't be None", code="S000", status_code=500)
        local_path = os.path.abspath(local_path)
        if use_basename:
            local_path = os.path.join(local_path, os.path.basename(key))
        dir_name = os.path.dirname(local_path)
        if not os.path.exists(dir_name):
            os.makedirs(dir_name)
        try:
            content_length = self.client.head_object(Bucket=bucket_name, Key=key)["ContentLength"]
            progress = ProgressPercentage(key, content_length)
            self.client.download_file(bucket_name, key, local_path, Callback=progress)
            return True
        except Exception as ex:
            logger.error(str(ex))
            raise MlChainError(str(ex), code="S001", status_code=500)

    def download_bytes(self, key, bucket_name=None, use_basename=True):
        """
        Download a file from S3.
        Args:
            key: `str`. S3 key that will point to the file.
            local_path: `str`. the path to download to.
            bucket_name: `str`. Name of the bucket in which to store the file.
            use_basename: `bool`. whether or not to use the basename of the key.
        """
        if not bucket_name:
            bucket_name = self.bucket
        if bucket_name is None:
            raise MlChainError("bucket can't be None", code="S000", status_code=500)
        try:
            buffer = BytesIO()
            self.client.download_fileobj(bucket_name, key, buffer)
            return buffer.getvalue()
        except Exception as ex:
            logger.error(str(ex))
            raise MlChainError(str(ex), code="S002", status_code=500)

    def download_cv2(self, key, bucket_name=None):
        """
        Download a file from S3.
        Args:
            key: `str`. S3 key that will point to the file.
            local_path: `str`. the path to download to.
            bucket_name: `str`. Name of the bucket in which to store the file.
            use_basename: `bool`. whether or not to use the basename of the key.
        """
        if not bucket_name:
            bucket_name = self.bucket
        if bucket_name is None:
            raise MlChainError("bucket can't be None", code="S000", status_code=500)
        try:
            buffer = BytesIO()
            self.client.download_fileobj(bucket_name, key, buffer)
            cv2 = import_cv2()
            return cv2.imdecode(
                np.asarray(bytearray(buffer.getvalue()), dtype="uint8"),
                cv2.IMREAD_COLOR)
        except Exception as ex:
            logger.error(str(ex))
            raise MlChainError(str(ex), code="S003", status_code=500)

    def download_dir(self, prefix, dir_path, bucket_name=None):
        bucket_name = bucket_name or self.bucket
        if bucket_name is None:
            raise MlChainError("bucket can't be None", code="S000", status_code=500)
        if prefix.endswith('/'):
            prefix = prefix[:-1]
        if len(prefix) > 0:
            prefix = '{0}/'.format(prefix)
        try:
            results = self.list(bucket_name=bucket_name, prefix=prefix)
            for file in results['keys']:
                self.download_file(prefix + file, os.path.join(dir_path, file),
                                   bucket_name=bucket_name)
            for dir in results['prefixes']:
                self.download_dir(prefix + dir, os.path.join(dir_path, dir),
                                  bucket_name=bucket_name)
        except Exception as ex:
            logger.error(str(ex))
            raise MlChainError(str(ex), code="S004", status_code=500)

    def upload_file(self, filename,
                    key,
                    bucket_name=None,
                    overwrite=False,
                    encrypt=False,
                    acl=None,
                    use_basename=False):
        """
        Uploads a local file to S3.
        Args:
            filename: `str`. name of the file to upload.
            key: `str`. S3 key that will point to the file.
            bucket_name: `str`. Name of the bucket in which to store the file.
            overwrite: `bool`. A flag to decide whether or not to overwrite the key
                if it already exists. If replace is False and the key exists, an
                error will be raised.
            encrypt: `bool`. If True, the file will be encrypted on the server-side
                by S3 and will be stored in an encrypted form while at rest in S3.
            acl: `str`. ACL to use for uploading, e.g. "public-read".
            use_basename: `bool`. whether or not to use the basename of the filename.
        """
        if not bucket_name:
            bucket_name = self.bucket
        if bucket_name is None:
            raise MlChainError("bucket can't be None", code="S000", status_code=500)
        if use_basename:
            key = os.path.join(key, os.path.basename(filename))
        try:
            self.client.upload_file(filename, bucket_name, key)
        except Exception as ex:
            logger.error(str(ex))
            raise MlChainError(str(ex), code="S005", status_code=500)

    def upload_bytes(self, bytes_data,
                     key,
                     bucket_name=None,
                     overwrite=False,
                     encrypt=False,
                     acl=None):
        """
        Uploads bytes to S3
        This is provided as a convenience to drop a string in S3. It uses the
        boto infrastructure to ship a file to s3.
        Args:
            bytes_data: `bytes`. bytes to set as content for the key.
            key: `str`. S3 key that will point to the file.
            bucket_name: `str`. Name of the bucket in which to store the file.
            overwrite: `bool`. A flag to decide whether or not to overwrite the key
                if it already exists.
            encrypt: `bool`. If True, the file will be encrypted on the server-side
                by S3 and will be stored in an encrypted form while at rest in S3.
            acl: `str`. ACL to use for uploading, e.g. "public-read".
        """
        if not bucket_name:
            bucket_name = self.bucket
        if bucket_name is None:
            raise MlChainError("bucket can't be None", code="S000", status_code=500)
        filelike_buffer = BytesIO(bytes_data)
        try:
            self.client.upload_fileobj(filelike_buffer, bucket_name, key)
        except Exception as ex:
            logger.info(str(ex))
            raise MlChainError(str(ex), code="S006", status_code=500)

    def upload_cv2(self, img,
                   key,
                   bucket_name=None,
                   overwrite=False,
                   quality=95,
                   img_fmt='.jpg',
                   acl=None):
        """
        Uploads bytes to S3
        This is provided as a convenience to drop a string in S3. It uses the
        boto infrastructure to ship a file to s3.
        Args:
            bytes_data: `bytes`. bytes to set as content for the key.
            key: `str`. S3 key that will point to the file.
            bucket_name: `str`. Name of the bucket in which to store the file.
            overwrite: `bool`. A flag to decide whether or not to overwrite the key
                if it already exists.
            encrypt: `bool`. If True, the file will be encrypted on the server-side
                by S3 and will be stored in an encrypted form while at rest in S3.
            acl: `str`. ACL to use for uploading, e.g. "public-read".
        """
        if not bucket_name:
            bucket_name = self.bucket
        if bucket_name is None:
            raise MlChainError("bucket can't be None", code="S000", status_code=500)
        try:
            jpg_formats = ['.JPG', '.JPEG']
            png_formats = ['.PNG']
            encode_params = None
            img_fmt = '.{0}'.format(img_fmt.strip('.')).upper()
            cv2 = import_cv2()
            if img_fmt in jpg_formats:
                encode_params = [cv2.IMWRITE_JPEG_QUALITY, quality]
            elif img_fmt in png_formats:
                encode_params = [cv2.IMWRITE_PNG_COMPRESSION, quality]
            ret, buf = cv2.imencode(img_fmt, img, encode_params)

            filelike_buffer = BytesIO(buf.tostring())

            self.client.upload_fileobj(filelike_buffer, bucket_name, key)
        except Exception as ex:
            logger.info(str(ex))
            raise MlChainError(str(ex), code="S007", status_code=500)

    def upload_dir(self, dir_path, prefix, bucket_name=None):
        bucket_name = bucket_name or self.bucket
        if bucket_name is None:
            raise MlChainError("bucket can't be None", code="S000", status_code=500)
        try:
            for file in glob.glob(os.path.join(dir_path, '*'), recursive=True):
                file_name = file[len(dir_path):].replace('\\', '/')
                if file_name.startswith('/'):
                    file_name = file_name[1:]
                key = os.path.join(prefix, file_name).replace('\\', '/')
                if os.path.isfile(file):
                    self.upload_file(file, key, bucket_name)
                else:
                    self.upload_dir(file, key, bucket_name)
        except Exception as ex:
            logger.info(str(ex))
            raise MlChainError(str(ex), code="S008", status_code=500)

    def list(self,
             bucket_name=None,
             prefix='',
             delimiter='/',
             page_size=None,
             max_items=None,
             keys=True,
             prefixes=True):
        """
        Lists prefixes and contents in a bucket under prefix.
        Args:
            bucket_name: `str`. the name of the bucket
            prefix: `str`. a key prefix
            delimiter: `str`. the delimiter marks key hierarchy.
            page_size: `str`. pagination size
            max_items: `int`. maximum items to return
            keys: `bool`. if it should include keys
            prefixes: `boll`. if it should include prefixes
        """
        if not bucket_name:
            bucket_name = self.bucket
        if bucket_name is None:
            raise MlChainError("bucket can't be None", code="S000", status_code=500)
        config = {
            'PageSize': page_size,
            'MaxItems': max_items,
        }
        try:
            paginator = self.client.get_paginator('list_objects_v2')

            response = paginator.paginate(Bucket=bucket_name,
                                          Prefix=prefix,
                                          Delimiter=delimiter,
                                          PaginationConfig=config)

            def get_keys(contents):
                list_keys = []
                for cont in contents:
                    list_keys.append(cont['Key'][len(prefix):])

                return list_keys

            def get_prefixes(page_prefixes):
                list_prefixes = []
                for pref in page_prefixes:
                    list_prefixes.append(pref['Prefix'][len(prefix): -1])
                return list_prefixes

            results = {
                'keys': [],
                'prefixes': []
            }
            for page in response:
                if prefixes:
                    results['prefixes'] += get_prefixes(page.get('CommonPrefixes', []))
                if keys:
                    results['keys'] += get_keys(page.get('Contents', []))

            return results
        except Exception as ex:
            logger.info(str(ex))
            raise MlChainError(str(ex), code="S009", status_code=500)

    def listdir(self, path, bucket=None):
        bucket = bucket or self.bucket
        if bucket is None:
            raise MlChainError("bucket can't be None", code="S000", status_code=500)
        try:
            results = self.list(bucket_name=bucket, prefix=path)
            return {'files': results['keys'], 'dirs': results['prefixes']}
        except Exception as ex:
            logger.info(str(ex))
            raise MlChainError(str(ex), code="S010", status_code=500)

    def delete(self, path, bucket=None):
        bucket = bucket or self.bucket
        if bucket is None:
            raise MlChainError("bucket can't be None", code="S000", status_code=500)
        try:
            self.client.remove_object(bucket, path)
        except Exception as ex:
            logger.info(str(ex))
            raise MlChainError(str(ex), code="S011", status_code=500)


#!/usr/bin/env python

import os
import io
import json

from flask import Flask, jsonify
from flask_restful import Resource, Api, request
from flask import send_file, make_response
from flask_cors import CORS

app = Flask(__name__)
api = Api(app)
CORS(app)

image_directory = '/media/anders/Media/Pictures/Post processed/'
small_thumbnail_directory = '/media/anders/Media/Pictures/Post processed/thumbnails/small/'
big_thumbnail_directory = '/media/anders/Media/Pictures/Post processed/thumbnails/big/'
image_data_filepath = 'image_info.json'

thumbnails = {} # {name : binary_data}
                # thumbnails are saved to memory as they are small and need to be accessed often.
                # Note: Raw images are not loaded to memory.

image_info = {} # {image_name : int(nbr of likes)}

@app.route('/images/<string:image_name>')
def get_image(image_name):
    image_binary = thumbnails[image_name]
    return make_image_response(image_binary, image_name)

@app.route('/images/raw/<string:image_name>')
def get_raw_image(image_name):
    return send_file(image_directory + image_name)

# Returns the filenames in the default original image directory
# sorted by rating on client side
@app.route('/images/filenames.json')
def get_filenames():
    return jsonify(image_info)

def make_image_response(image_binary, image_name):
    response = make_response(image_binary)
    response.headers.set('Content-Type', 'image/jpeg')
    response.headers.set(
        'Content-Disposition', '', filename='%s.jpg' % image_name) # '' argument represent "not as attachment"
    return response

def read_images(directory_list):
    img_dict = {}
    for directory in directory_list:
        filenames = read_filenames(directory)
        for filename in filenames:
            print('READING FILE: ' + str(filename))
            img_dict[filename] = open(directory + filename, "rb").read()
    return img_dict

def read_image_info(directory):
    image_info = load_image_info()
    for filename in os.listdir(directory):
        if filename not in image_info.keys():
            if filename.endswith('.png') or filename.endswith('.jpg'):
                image_info[filename] = 0
    return image_info

def read_filenames(directory):
    filenames = []
    for filename in os.listdir(directory):
        if filename.endswith('.png') or filename.endswith('.jpg'):
            filenames.append(filename)
    return filenames

def save_image_data(image_info):
    with open(image_data_filepath, 'w') as outfile:
        json.dump(image_info, outfile)

def load_image_info():
    try:
        with open(image_data_filepath) as file:
            return json.load(file)
    except IOError:
        print ("File does not exist:", image_data_filepath)
        return {}

class ImageRating(Resource):
    def get(self, image_name):
        if image_name in image_info:
            return image_info[image_name]
        return 0

    def put(self, image_name):
        put_data =  int(request.form['data'])
        if image_name in image_info.keys():
            image_info[image_name] += put_data
        elif put_data > 0:
            image_info[image_name] = put_data
        save_image_data(image_info) #TODO: optimize save to file to happen less
        return image_info[image_name]

api.add_resource(ImageRating, '/ratings/<string:image_name>')

if __name__ == '__main__':
    # ORDER IMPORTANT
    image_info = read_image_info(image_directory)
    thumbnails = read_images([small_thumbnail_directory, big_thumbnail_directory])
    app.run(debug=False)


import os

import requests as requests

from nsfw_model.nsfw_detector import predict
model = predict.load_model('./nsfw_mobilenet2.224x224.h5')

# predict nsfw from image url
def predict_nsfw_from_url(url):
    img_data = requests.get(url).content
    with open('eval.jpg', 'wb') as handler:
        handler.write(img_data)
    pred = predict.classify(model,'eval.jpg')
    os.remove('eval.jpg')
    return pred


def solution(n, costs):
    def find_parent(parent, n):
        if parent[n] == n:
            return parent[n]
        return find_parent(parent, parent[n])

    def union_parent(parent, a, b):
        a = find_parent(parent, a)
        b = find_parent(parent, b)
        # 최대한 부모노드가 적은숫자를 가진걸로 통일
        parent[max(a, b)] = min(a, b)

    answer = 0
    costs.sort(key=lambda x: x[2])  # 비용 기준으로 오름차순 정렬
    parent = [i for i in range(n)]

    cnt = 0
    for node_a, node_b, cost in costs:
        if find_parent(parent, node_a) != find_parent(parent, node_b):
            union_parent(parent, node_a, node_b)
            answer += cost
            cnt += 1
        if cnt >= n - 1:
            break

    return answer

def solution2(n, costs):
    answer = 0

    def get_parent(n, parent):
        if n == parent[n]:
            return n
        return get_parent(parent[n], parent)

    costs.sort(key=lambda c: c[2])
    parent = [i for i in range(n)]

    for n1, n2, cost in costs:
        a, b = get_parent(n1, parent), get_parent(n2, parent)
        if a != b:
            parent[max(a, b)] = min(a, b)
            answer += cost
    return answer

if __name__ == '__main__':
    print(solution(4, [[0, 1, 1], [0, 2, 2], [1, 2, 5], [1, 3, 1], [2, 3, 8]]))
    print(solution(5, [[0, 1, 5], [1, 2, 3], [2, 3, 3], [3, 1, 2], [3, 0, 4], [2, 4, 6], [4, 0, 7]]))

    print(solution2(4, [[0, 1, 1], [0, 2, 2], [1, 2, 5], [1, 3, 1], [2, 3, 8]]))
    print(solution2(5, [[0, 1, 5], [1, 2, 3], [2, 3, 3], [3, 1, 2], [3, 0, 4], [2, 4, 6], [4, 0, 7]]))


import boto3
dynamodb = boto3.resources('dynamodb')
table = dynamodb.Table('DemodataBase')
table.update_item(
    Key={
        'ID': '004',
        'phone': '9113266608'
    },
    UpdateExpression='name = :string1',
    ExpressionAttributeValues={
        ':string1': 'ashok kumar'
    }
)
response = table.get_item(
    Key={
        'ID': '004',
        'phone': '9113266608'
    }
)
item = response['Item']
print(item)


import collections
import json
from json.decoder import JSONDecodeError
from data.offer_reply import ReplyStatus, OfferReply
from static_info import DEFAULT_FILENAME

class JsonDatabase:
    """
    бд в которй хранятся все данные о заявках в формате json
    """

    def __init__(self, filename=DEFAULT_FILENAME):
        self.__filename = filename
        self.__data = collections.defaultdict(list)
        self.reply_to_approve_queue = []

        with open(self.__filename, 'r') as json_file:
            try:
                self.__data.update(json.load(json_file))
            except JSONDecodeError:
                pass

        self.__clear_data_from_deleted()
        self.__fill_reply_queue_from_bd(self.reply_to_approve_queue)

    def get_reply(self, reply_address: OfferReply.Address):
        try:
            reply_data = self.__data[str(reply_address.chat_id)][reply_address.user_reply_ind]
            return OfferReply.from_bd_row(**reply_data, row_ind=reply_address.user_reply_ind)
        except:
            return None

    def get_replies(self, chat_id):
        if str(chat_id) not in self.__data:
            return []
        return self.__data[str(chat_id)]

    def write_reply(self, reply: OfferReply):
        if reply.__user_reply_ind is None:
            reply.__user_reply_ind = self.__add(reply.__chat_id, reply.__status, reply.__credit_ind, reply.__data)
        else:
            self.__update(reply.__chat_id, reply.__status, reply.__credit_ind, reply.__user_reply_ind, reply.__data)

    def __update(self, chat_id, status: ReplyStatus, credit_ind: int, credit_number: int, data):
        self.__data[str(chat_id)][credit_number] = \
            {"credit_data": data, "credit_ind": credit_ind, "status": status,
             "chat_id": chat_id}

    def __add(self, chat_id, status: ReplyStatus, credit_ind: int, data):
        self.__data[str(chat_id)].append(
            {"credit_data": data, "credit_ind": credit_ind, "status": status,
             "chat_id": chat_id})
        return len(self.__data[str(chat_id)]) - 1

    def flush(self):
        with open(self.__filename, 'w+') as outfile:
            json.dump(self.__data, outfile)

    def __fill_reply_queue_from_bd(self, queue: list):
        for chat_id, replies in self.__data.items():
            for ind, reply_data in enumerate(replies):
                if reply_data["status"] == ReplyStatus.Pending:
                    cr = OfferReply.from_bd_row(**reply_data, row_ind=ind)
                    queue.append(cr.get_address())

    def __clear_data_from_deleted(self):
        new_data = collections.defaultdict(list)
        for chat_id, replies in self.__data.items():
            new_data[chat_id] = [reply_data for reply_data in replies if reply_data["status"] != ReplyStatus.Removed]
        self.__data = new_data

    def filter_reply_queue(self):
        self.reply_to_approve_queue = [addr for addr in self.reply_to_approve_queue if
                                       not (self.get_reply(addr).is_deleted() or self.get_reply(addr).is_cancelled())]

    def get_approved_replies(self):
        ret_replies = []
        for chat_id, replies in self.__data.items():
            for ind, reply_data in enumerate(replies):
                if reply_data["status"] == ReplyStatus.Approved:
                    cr = OfferReply.from_bd_row(**reply_data, row_ind=ind)
                    ret_replies.append(cr.get_address())
        return ret_replies

    def get_rejected_replies(self):
        ret_replies = []
        for chat_id, replies in self.__data.items():
            for ind, reply_data in enumerate(replies):
                if reply_data["status"] == ReplyStatus.Rejected:
                    cr = OfferReply.from_bd_row(**reply_data, row_ind=ind)
                    ret_replies.append(cr.get_address())
        return ret_replies

    def get_cancelled_replies(self):
        ret_replies = []
        for chat_id, replies in self.__data.items():
            for ind, reply_data in enumerate(replies):
                if reply_data["status"] == ReplyStatus.Cancelled:
                    cr = OfferReply.from_bd_row(**reply_data, row_ind=ind)
                    ret_replies.append(cr.get_address())
        return ret_replies


def is_odd(n) :
    if n < 0 :
        print("Negative number entered")
        exit()
    if n % 2 == 1 :
        return True
    else :
        return False

x = int(input("Enter an integer"))
print(is_odd(x))


# Determines input read criterion
from sys import stdin
JUDGE = 0
FILE = 1
INPUT_SRC = FILE
# INPUT_SRC = JUDGE
if INPUT_SRC == FILE:
 inputSrc = open("729.txt", "r")
else:
 inputSrc = stdin

from itertools import permutations
import operator
num_datasets=int(inputSrc.readline().strip())
inputSrc.readline().strip()
for _ in range(num_datasets):
  inp=list(map(int,inputSrc.readline().strip().split()))
  n,h=inp[0],inp[1]
  s=""
  for i in range(n):
    if i<n-h:
      s+="0"
    else:
      s+="1"
  perm =  [''.join(p) for p in permutations(s)]
  # print ()
  dct={}
  for out in set(perm):
     binary_2_dec=int(out, 2)
     dct[out]=binary_2_dec

  sorted_d = sorted(dct.items(), key=operator.itemgetter(1))
  # print (sorted_d)
  for d in sorted_d:
    print (d[0])


import socketio

from .component_role import ComponentRole

class NodeRole(ComponentRole):
    """
    Role definition of a node component.
    """

    def __init__(self):
        super().__init__(
            "Node",
            runtime_aspects=ComponentRole.RuntimeAspects(
                runtime_app_type=socketio.WSGIApp
            ),
            networking_aspects=ComponentRole.NetworkingAspects(
                has_client=True, has_server=True
            ),
        )


import numpy
"""
This file is interface to read correlation data provided in the data file
and interpolate and provide correlation value for any given distance of
seperation and the redshift.
"""

r_value,corr_value=numpy.load('/n/home10/vikas/myproduct/theorycorr/data/corr_cross.npy')
r_value_upper,corr_value_upper=numpy.load('/n/home10/vikas/myproduct/theorycorr/data/corr_cross_upper.npy')
r_value_lower,corr_value_lower=numpy.load('/n/home10/vikas/myproduct/theorycorr/data/corr_cross_lower.npy')
omg_b,omg_c,omg_l,h=0.04,0.22,0.7,0.72

def SQR(a):
    return a*a

def growth(redshift):
    omega_matter=omg_b+omg_c
    hubble=h
    theta_cmb = 2.728/2.7
    omega_lambda=omg_l
    omhh = omega_matter*SQR(hubble)

    z_equality = 25000.0*omhh/SQR(SQR(theta_cmb))
    omega_curv = 1.0-omega_matter-omega_lambda
    omega_denom = omega_lambda+SQR(1.0+redshift)*(omega_curv+omega_matter*(1.0+redshift))
    omega_lambda_z = omega_lambda/omega_denom
    omega_matter_z = omega_matter*SQR(1.0+redshift)*(1.0+redshift)/omega_denom
    growth_k0 = z_equality/(1.0+redshift)*2.5*omega_matter_z/(pow(omega_matter_z,4.0/7.0)\
                    -omega_lambda_z+(1.0+omega_matter_z/2.0)*(1.0+omega_lambda_z/70.0))
    growth_to_z0 = z_equality*2.5*omega_matter/(pow(omega_matter,4.0/7.0)-omega_lambda + \
                (1.0+omega_matter/2.0)*(1.0+omega_lambda/70.0))

    growth_to_z0 = growth_k0/growth_to_z0
    return growth_to_z0

def get_corr(r,zqso):
    # Unit of r is Mpc but unit of r_value is h^-1 Mpc
    if numpy.isscalar(r) or len(r.shape)==1:
        ret_corr=numpy.interp(r,r_value/h,corr_value,right=0.0)
        err_upper=numpy.interp(r,r_value_upper/h,corr_value_upper,right=0.0)-ret_corr
        err_lower=ret_corr-numpy.interp(r,r_value_lower/h,corr_value_lower,right=0.0)
        growth_factor=(growth(zqso)/growth(2.4))**2
        if numpy.isscalar(growth_factor):
    	    return growth_factor*ret_corr,err_upper,err_lower
        GROWTH_FACTOR,RET_CORR=numpy.meshgrid(growth_factor,ret_corr)
        GROWTH_FACTOR,ERR_UPPER=numpy.meshgrid(growth_factor,err_upper)
        GROWTH_FACTOR,ERR_LOWER=numpy.meshgrid(growth_factor,err_lower)
        return GROWTH_FACTOR*RET_CORR,ERR_UPPER,ERR_LOWER
    elif len(r.shape)==2 and len(zqso.shape)==2:
        RET_CORR = numpy.interp(r.flatten(),r_value/h,corr_value,right=0.0).reshape(r.shape)
        ERR_UPPER = numpy.interp(r.flatten(),r_value_upper/h,corr_value_upper,right=0.0).reshape(r.shape) - RET_CORR
        ERR_LOWER = RET_CORR - numpy.interp(r.flatten(),r_value_lower/h,corr_value_lower,right=0.0).reshape(r.shape)
        growth_value = growth(zqso[0,:])
        return growth_value*RET_CORR,ERR_UPPER,ERR_LOWER


import typer
from farcaster.client import Warpcast
from farcaster.models import ApiCast, ApiUser
from typing import Optional, Set
from rich import print
from rich.panel import Panel
from rich.padding import Padding
from rich.progress import Progress, SpinnerColumn, TextColumn

cast_link_prefix = "farcaster://casts/"
user_link_prefix = "farcaster://profiles/"

def main(mnemonic: str, watch_all: bool = False, skip_existing: bool = False):
    with Progress(
        SpinnerColumn(),
        TextColumn("[progress.description]{task.description}"),
        transient=True,
    ) as progress:
        progress.add_task(description="Logging in...", total=None)
        progress.add_task(description="Reticulating splines...", total=None)

        client = Warpcast(mnemonic=mnemonic)

        following: Set[int] = set()
        if not watch_all:
            following_users = client.get_all_following().users
            following = set([user.fid for user in following_users])
    print("Ready!")
    with Progress(
        SpinnerColumn(),
        TextColumn("[progress.description]{task.description}"),
        transient=True,
    ) as progress:
        progress.add_task(description="Listening for new casts...", total=None)
        for cast in client.stream_casts(skip_existing=skip_existing):
            if cast:
                if not watch_all:
                    if cast.author.fid not in following:
                        continue
                if cast.text.startswith("recast:farcaster://casts/"):
                    recaster = cast.author
                    cast_hash = cast.text.lstrip("recast:farcaster://casts/")
                    cast = client.get_cast(cast_hash).cast
                else:
                    recaster = None
                print_cast(cast, recaster)

def print_cast(cast: ApiCast, recaster: Optional[ApiUser] = None) -> None:
    if recaster:
        print(
            f"🔁 [red bold link={user_link_prefix + str(recaster.fid)}]{recaster.username}[/red bold link] recasted:"
        )
    parent = ""
    if cast.parent_hash:
        parent = f" replying to [purple link={cast_link_prefix + str(cast.parent_hash)}](parent)[/purple link]"

    print(
        Padding(
            f"[green bold link={user_link_prefix + str(cast.author.fid)}]{cast.author.username}[/green bold link]{parent} | [purple link={cast_link_prefix + str(cast.hash)}](cast)[/purple link]",
            (0, 1),
        )
    )
    print(Panel(cast.text))
    print()

def start():
    typer.run(main)


import cv2
import numpy as np
import time
from VehicleCounter import VideoPreprocessor, VehicleCounter
import yaml

def read_configs(filename):
    with open(filename, 'r') as stream:
        configs = yaml.load(stream, Loader = yaml.FullLoader)
    return configs

if __name__ == '__main__':
    configs = read_configs('configs.yaml')
    vidcap = cv2.VideoCapture(configs['video_input'])

    #Get video frame data
    width  = int(vidcap.get(3))
    height = int(vidcap.get(4))
    original_fps = vidcap.get(cv2.CAP_PROP_FPS)

    output_filename = "outputs/" + configs['video_input'].split('.')[0] + "_" + str(int(time.time())) + ".mp4"
    fourcc = cv2.VideoWriter_fourcc(*'MP4V')
    writer = cv2.VideoWriter(output_filename, fourcc, original_fps, (width,height))

    count_frame = 0
    vid_proprocessor = VideoPreprocessor(height, width, configs["stabilizerPercentageCropFromTop"], np.array(configs["roiPolyPoints"]))
    vehicle_counter = VehicleCounter(configs['yoloModelName'],
                                       configs['yoloSize'],
                                       configs['trackerModel_filename'],
                                       configs['tracker_max_cosine_distance'],
                                       configs['tracker_maxAge'],
                                       np.array(configs['exit_lines']))

    with open('model_data/names.txt', 'r') as f:
        classes = f.read().splitlines()

    while True:
        t1 = time.time()

        ret, frame = vidcap.read()
        #Break loop when video ends
        if ret != True:
            print("BREAK")
            cv2.destroyAllWindows()
            break

        ## Stabilize frame
        inv_transformation, transformation_mat, processed_frame, original_frame = vid_proprocessor.preprocess_img(frame, width, height)
        classIds, boxes, trackIds, exit_records, unsure_records = vehicle_counter.trackSouthwardVehicles(processed_frame,
                                                                                confThreshold = configs["confThreshold"],
                                                                                nmsThreshold = configs["nmsThreshold"])

        for (classId, box, trackId) in zip(classIds, boxes, trackIds):
            #Transform to original
            top_left = (box[0], box[1])
            bottom_right = (box[0] + box[2], box[1] + box[3])
            top_left = VideoPreprocessor.transform2OriginalCoord(top_left, transformation_mat)
            bottom_right = VideoPreprocessor.transform2OriginalCoord(bottom_right, transformation_mat)

            if trackId in exit_records:
                color = (0, 255 , 0)
            elif trackId in unsure_records:
                color = (0, 0, 255)
            else:
                color = (0, 255, 255)

            cv2.rectangle(original_frame, (bottom_right[0], bottom_right[1]),(top_left[0], top_left[1]),
                  color=color, thickness=2)
            text =  str(trackId) + " " + classes[classId]
            cv2.putText(original_frame, text, (top_left[0], top_left[1] - 5), cv2.FONT_HERSHEY_SIMPLEX, 1,
                color=color, thickness=2)

        vehicles_exited_text = "Vehicles Exited (South): " + str(len(exit_records))
        cv2.rectangle(original_frame, (93, 45),(970, 117),
                  color=(0,0,0), thickness=-1)
        cv2.putText(original_frame, vehicles_exited_text, (100, 100), cv2.FONT_HERSHEY_SIMPLEX, 2,
            color=(0, 0, 255), thickness=3)

        writer.write(frame)
        original_frame = cv2.resize(original_frame, (1920//2, 1080//2))

        cv2.imshow("output", original_frame)
        key = cv2.waitKey(1)
        if ret != True or key==27:
            print("BREAK")
            cv2.destroyAllWindows()
            break

        #previous_frame = frame
        print(1/(time.time()-t1))
        count_frame+=1

    writer.release()


class ListNode:
    def __init__(self, val):
        self.val = val
        self.next = None
        self.prev = None

class DoublyLinkedList:
    def __init__(self):
        self.tail = None

    def append(self, val):
        new_node = ListNode(val)
        if not self.tail:
            self.tail = new_node
            return

        new_node.prev = self.tail
        self.tail.next = new_node
        self.tail = new_node

class BrowserHistory(DoublyLinkedList):

    def __init__(self, homepage: str):
        super().__init__()
        self.append(homepage)
        self.curr = self.tail

    def visit(self, url: str) -> None:
        self.tail = self.curr
        self.append(url)
        self.curr = self.tail

    def back(self, steps: int) -> str:
        i = 0
        curr = self.curr
        while curr and curr.prev:
            if i == steps:
                break
            curr = curr.prev
            i += 1

        # Curr is now the node of history backward
        self.curr = curr
        return curr.val

    def forward(self, steps: int) -> str:
        i = 0
        curr = self.curr
        while curr and curr.next:
            if i == steps:
                break
            curr = curr.next
            i += 1

        # Curr is now the node of history forward
        self.curr = curr
        return curr.val

# Your BrowserHistory object will be instantiated and called as such:
# obj = BrowserHistory(homepage)
# obj.visit(url)
# param_2 = obj.back(steps)
# param_3 = obj.forward(steps)


import numpy as np

class GVF:

    def __init__(self, actions, init_q, gamma, alpha, learning_method, rng):

        self.actions = actions
        self.init_q = init_q
        self.q = dict()
        self.gamma = gamma
        self.alpha = alpha
        self.start_alpha = alpha
        self.learning_method = learning_method
        self.rng = np.random.RandomState(rng)

    def _get_q(self, s, a):
        sa = tuple(list(s) + [a])
        if sa in self.q:
            return self.q[sa]
        else:
            self._set_init_q(s)
            return self.init_q

    def get_q(self, s, a=None):
        if a is not None:
            return self._get_q(s, a)
        else:
            return np.asarray([self._get_q(s, a) for a in self.actions], dtype=np.float32)

    def _set_q(self, s, a, q):
        sa = tuple(list(s) + [a])
        if sa not in self.q:
            self._set_init_q(s)
        self.q[sa] = np.float32(q)

    def _set_init_q(self, s):
        s = list(s)
        for a in self.actions:
            self.q[tuple(s + [a])] = np.float32(self.init_q)

    def get_max_action(self, s, stochastic=True):
        values = self.get_q(s)
        if stochastic:
            actions = np.where(values == values.max())[0]
            return self.rng.choice(actions)
        else:
            return np.argmax(values)

    def learn(self, s, a, r, s2, term):

        if term:
            q2 = 0.
        elif self.learning_method == 'max':
            q2 = np.max(self.get_q(s2))
        elif self.learning_method == 'mean':
            q2 = np.mean(self.get_q(s2))
        else:
            raise ValueError('Learning method is not known.')
        delta = r + self.gamma * q2 - self._get_q(s, a)
        self._set_q(s, a, self._get_q(s, a) + self.alpha * delta)


#!/usr/bin/env python3

import re
import random
import numpy as np
import torch
from transformers import BertForMaskedLM, BertTokenizer

class Escritor():
    def __init__(self):
        """
        Escritor is a class in charge of handling BETO model and using it as LM
        to generate new lines of text.

        It uses the previous sentence as context for the new sentence.

        Parameters that are adjustable according to different kind of texts:
        - pie_forzado: initial string to be used when creating the first line.
        - min_words_line: minimum amount of words that are in each line.
        - max_words_line: maximum amount of words that are in each line.
        - max_tokens: amount of retrieved tokens from which to choose everytime BETO is called.
        - score: threshold used to accept or reject a sentence using BETO's Language Model.
        """
        # Initialize BETO
        self.tokenizer = BertTokenizer.from_pretrained("pytorch/", do_lower_case=False)
        self.model = BertForMaskedLM.from_pretrained("pytorch/")
        self.model.eval()

        # Default parameters to generate different kind of texts
        self.pie_forzado = 'la casa'
        self.min_words_line = 5
        self.max_words_line = 12
        self.max_tokens = 20  # amount of possible tokens from which to choose
        self.score = 0  # if score is given, then it is used a threshold to accept or reject sentences

        # Initializing inner variables
        self.initial_context = ''
        self.actual_context = ''
        self.previous_sentence = False
        self.text = []

    def beto_sentence(self, sentence=False):
        if not sentence:  # creating initial context in beto format
            sentence = '[CLS] ' + self.pie_forzado + self.beto_added_sentence()

        else:  # formatting sentence in beto format
            sentence = '[CLS] ' + sentence + ' [SEP]'

        return sentence

    def beto_added_sentence(self):
        masks_to_add = self.random_masks() * '[MASK] '
        return ' ' + masks_to_add + '[SEP]'

    def random_masks(self):
        return random.randint(self.min_words_line, self.max_words_line)

    def generate_text(self, N, given_context=False):
        n = 0
        while n < N:  # generating an N line's text
            if given_context:  # checking if previous context has been passed from other class
                self.previous_sentence = True
                self.actual_context = self.beto_sentence(given_context) + self.beto_added_sentence()
            # using the previous sentence as context for the new sentence
            if self.previous_sentence:
                #print(self.actual_context)
                new_sentence = self.generate_sentence(self.actual_context)
                #self.actual_context = self.beto_sentence(new_sentence) + self.beto_added_sentence()
                self.text.append(new_sentence)

            else:  # there is no previous sentence
                self.initial_context = self.beto_sentence()
                first_sentence = self.generate_sentence(self.initial_context)
                self.text.append(first_sentence)
                self.previous_sentence = self.beto_sentence(first_sentence)
                #self.actual_context = self.previous_sentence + self.beto_added_sentence()

            self.actual_context = self.beto_sentence(self.text[-1]) + self.beto_added_sentence()
            n += 1

        return self.text

    def generate_sentence(self, actual_context):
        # use score to decide if sentence is "good" or "bad" given the LM
        if self.score:
            score = self.score + 1
            # if generated sentence is greater than threshold, create another one
            while score >= self.score:
                context = actual_context[:]
                sentence = self.fill_masked_indexes(context)
                # getting the sentence's score
                score = self.score_sentence(sentence)
                #print(sentence, score)
        else:
            context = actual_context[:]
            sentence = self.fill_masked_indexes(context)

        sentence = self.clean_sentence(sentence)

        return sentence

    def fill_masked_indexes(self, sentence):
        initial_sentence = sentence[:]
        masked_indxs = [index for index, word in enumerate(initial_sentence.split()) if re.match(r'\[MASK\]', word)]
        while masked_indxs:
            sentence, masked_indxs = self.generate_word(sentence, masked_indxs)

        return sentence

    def generate_word(self, text, masked_indxs):
        # converting text to BERTO format
        tokens = self.tokenizer.tokenize(text)
        indexed_tokens = self.tokenizer.convert_tokens_to_ids(tokens)
        tokens_tensor = torch.tensor([indexed_tokens])

        # getting LM predictions for [MASK] index
        predictions = self.model(tokens_tensor)[0]
        focus_masked_indx = masked_indxs.pop(0)
        idxs = torch.argsort(predictions[0,focus_masked_indx], descending=True)

        # randomly choosing a word considering first max_tokens possible tokens
        max_tokens = self.max_tokens
        chosen_word = self.retrieve_token(idxs, max_tokens)

        # assigning the new token
        new_text = text.split()
        new_text[focus_masked_indx] = chosen_word
        new_text = ' '.join(new_text)

        return new_text, masked_indxs

    def retrieve_token(self, idxs, max_tokens):
        chosen = False
        while not chosen:
            try:
                # retrieving only the first max_tokens
                predicted_token = self.tokenizer.convert_ids_to_tokens(idxs[:max_tokens])
                #print('MASK:',predicted_token)

                # filtering retrieved tokens and choosing one token
                predicted_token_filtered = self.filter_tokens(predicted_token)
                chosen_word = random.choice(predicted_token_filtered)  # randomly choosing
                chosen = True

            except IndexError:
                max_tokens *= 2

        return chosen_word

    def filter_tokens(self, tokens):
        tokens = [token for token in tokens if token != '[UNK]']
        #tokens = [token for token in tokens if token not in punctuation]
        tokens = [token for token in tokens if token.count('#') == 0]
        tokens = [token for token in tokens if token.isalpha()]
        #tokens = [token for token in tokens if token in palabras_todas and len(token) > 1]

        if len([token for token in tokens if len(token) > 1]) > 0:
            tokens = [token for token in tokens if len(token) > 1]

        return tokens

    def score_sentence(self, sentence):
        # function adapted from:
        # https://www.scribendi.ai/comparing-bert-and-gpt-2-as-language-models-to-score-the-grammatical-correctness-of-a-sentence/
        tokenize_input = self.tokenizer.tokenize(sentence)
        tokenize_input = ["[CLS]"]+tokenize_input+["[SEP]"]
        tensor_input = torch.tensor([self.tokenizer.convert_tokens_to_ids(tokenize_input)])
        with torch.no_grad():
            loss = self.model(tensor_input, labels=tensor_input)[0]

        return np.exp(loss.detach().numpy())

    def clean_sentence(self, sentence):
        sentence = re.findall(r'\[\w{3}\] (.*?) (?=\[SEP\])', sentence)

        return sentence[-1]

    def print_text(self, text):
        print('TEXT')
        for i, line in enumerate(text):
            #print(line, self.score_sentence(line))
            print(line)

if __name__ == '__main__':
    # BETO --> https://github.com/dccuchile/beto
    # How to use it --> https://colab.research.google.com/drive/1uRwg4UmPgYIqGYY4gW_Nsw9782GFJbPt#scrollTo=9KXo6-ahoJoM

    # palabras del español --> https://github.com/JorgeDuenasLerin/diccionario-espanol-txt

    escritor = Escritor()  # instantiates Escritor class
    #escritor.score = 200  # setting a score threshold
    text = escritor.generate_text(4)
    escritor.print_text(text)