import random
import typing as t

INITIAL_CAPACITY = 10 ** 2
MULTIPLIER = 4
EXPANSION_ON = 0.5

class Node:

    def __init__(self, value):
        self.value = value
        self.next = None

class HashTable:

    def __init__(self):
        self.capacity = INITIAL_CAPACITY
        self.size = 0
        self.storage = [Node(None)] * self.capacity
        self.expand_times = 0

    def hash(self, value) -> int:
        return hash(value) % self.capacity

    def add(self, value) -> t.Optional[int]:
        self.size += 1
        index = self.hash(value)
        node = self.storage[index]

        if node.value is None:
            self.storage[index] = Node(value)
            return index

        parent = node
        while node is not None:
            parent = node
            node = node.next
        parent.next = Node(value)
        return index

    def find(self, value) -> t.Optional[int]:
        index = self.hash(value) % self.capacity
        node = self.storage[index]
        while node.value != value:
            node = node.next
            if node.value is None:
                return None
        return index

    def delete(self, value) -> bool:
        index = self.hash(value) % self.capacity
        node = self.storage[index]
        parent = None

        while node.value != value:
            node = node.next
            if node.value is None:
                return False

        self.size -= 1
        if parent is None:
            self.storage[index] = node.next
        else:
            parent.next = node.next
        return True

    def print_table(self):
        for index in range(self.capacity):
            print('['+str(index)+']:', end=' ')
            node = self.storage[index]
            while node is not None:
                if node.value is not None:
                    print(str(node.value), end=' ')
                node = node.next
            print()

    def check(self) -> bool:
        # вернет True, если надо расширяться, False - если нет
        return True if self.size / self.capacity > EXPANSION_ON else False

    def expand(self):
        self.storage += [Node(None)] * self.capacity
        self.capacity *= 2

if __name__ == '__main__':
    hash_table = HashTable()
    hash_table.print_table()
    for _ in range(INITIAL_CAPACITY // MULTIPLIER):
        hash_table.add(random.choice(range(INITIAL_CAPACITY * MULTIPLIER)))

    hash_table.print_table()


"""
Turning radar PPIs into Cartesian grids.

@title: radar_grids
@name: radar_grids
@author: Valentin Louf <valentin.louf@bom.gov.au>
@institution: Monash University and the Australian Bureau of Meteorology
@date: 12/03/2021

.. autosummary::
    :toctree: generated/

    chunks
    buffer
    main
"""
# Python Standard Library
import os
import sys
import glob
import time
import argparse
import warnings
import traceback

from concurrent.futures import TimeoutError

import pandas as pd
from pebble import ProcessPool, ProcessExpired

import radar_grids

def chunks(l, n):
    """
    Yield successive n-sized chunks from l.
    From http://stackoverflow.com/a/312464
    """
    for i in range(0, len(l), n):
        yield l[i : i + n]

def buffer(infile: str) -> None:
    """
    It calls the production line and manages it. Buffer function that is used
    to catch any problem with the processing line without screwing the whole
    multiprocessing stuff.

    Parameters:
    ===========
    infile: str
        Name of the input radar file.
    """
    radar_grids.标准映射(infile, OUTPATH, prefix="502", na_standard=True)

    return None

def main(start: pd.Timestamp, end: pd.Timestamp) -> None:
    """
    Look for files in the subdirectory structure and spawn the multiprocessing
    pools to process these files.

    Parameters:
    ===========
    start: pd.Timestamp
        Start date for processing.
    end: pd.Timestamp
        End date for processing.
    """
    drange = pd.date_range(start, end)
    for date in drange:
        st = time.time()
        datestr = date.strftime("%Y%m%d")
        input_dir = os.path.join(INPATH, str(date.year), datestr, "*.nc")
        flist = sorted(glob.glob(input_dir))
        if len(flist) == 0:
            print(f"No file found for {input_dir}.")
            continue
        print(f"Found {len(flist)} files for {datestr}.")

        for flist_chunk in chunks(flist, 32):
            with ProcessPool() as pool:
                future = pool.map(buffer, flist_chunk, timeout=120)
                iterator = future.result()

                while True:
                    try:
                        _ = next(iterator)
                    except StopIteration:
                        break
                    except TimeoutError as error:
                        print("function took longer than %d seconds" % error.args[1])
                    except ProcessExpired as error:
                        print("%s. Exit code: %d" % (error, error.exitcode))
                    except Exception:
                        traceback.print_exc()

        ed = time.time()
        print(f"Processed in {ed - st:0.1f}s.")

    return None

if __name__ == "__main__":
    """
    Global variables definition.
    """
    # Parse arguments
    parser_description = "Processing of radar data from level 1a to level 1b."
    parser = argparse.ArgumentParser(description=parser_description)

    parser.add_argument(
        "-i",
        "--input-dir",
        dest="indir",
        default="/scratch/kl02/vhl548/opol/v2020/ppi/",
        type=str,
        help="Input directory.",
    )
    parser.add_argument(
        "-o",
        "--output-dir",
        dest="outdir",
        default="/scratch/kl02/vhl548/opol/v2021/gridded/",
        type=str,
        help="Output directory.",
    )
    parser.add_argument(
        "-s",
        "--start-date",
        dest="start_date",
        default=None,
        type=str,
        help="Start date for radar processing.",
        required=True,
    )
    parser.add_argument(
        "-e",
        "--end-date",
        dest="end_date",
        default=None,
        type=str,
        help="End date for radar processing.",
        required=True,
    )

    args = parser.parse_args()
    INPATH = args.indir
    OUTPATH = args.outdir
    START_DATE = args.start_date
    END_DATE = args.end_date
    print(f"The input directory is {INPATH}\nThe output directory is {OUTPATH}.")

    try:
        start = pd.Timestamp(START_DATE)
        end = pd.Timestamp(END_DATE)
        if start > end:
            parser.error("End date older than start date.")
    except ValueError:
        parser.error("Invalid dates.")
        sys.exit()

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        main(start, end)


# -*- coding: utf-8 -*-
from PyQt5 import QtGui,QtCore,QtWidgets
import math
import codescene
import time
import ui.CodeUIItem as CodeUIItem

class CodeView(QtWidgets.QGraphicsView):
	def __init__(self, *args):
		super(CodeView, self).__init__(*args)
		from UIManager import UIManager
		self.setScene(UIManager.instance().getScene())
		self.setViewportUpdateMode(QtWidgets.QGraphicsView.FullViewportUpdate)
		self.setCacheMode(QtWidgets.QGraphicsView.CacheNone)
		#self.setDragMode(QtWidgets.QGraphicsView.RubberBandDrag)
		self.setTransformationAnchor(QtWidgets.QGraphicsView.AnchorUnderMouse)
		self.setMouseTracking(True)
		self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
		self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
		self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
		self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
		self.setAcceptDrops(True)

		self.mousePressPnt = None
		self.mouseCurPnt = None
		self.isFrameSelectMode = False
		self.isMousePressed = False

		self.updateTimer = QtCore.QTimer()
		self.updateTimer.setInterval(70)
		# self.connect(self.updateTimer, QtCore.SIGNAL('timeout()'), self, QtCore.SLOT('updateView()'))
		self.updateTimer.timeout.connect(self.updateView)
		self.centerPnt = QtCore.QPointF()
		self.scale(0.6,0.6)
		self.brushRadius = 8
		self.setBackgroundBrush(QtGui.QBrush(QtGui.QColor(50,50,50)))
		self.hudFont = QtGui.QFont('tahoma', 8)
		self.hudFontMetric = QtGui.QFontMetrics(self.hudFont)

	@QtCore.pyqtSlot()
	def updateView(self):
		scene = self.scene()
		if scene:
			scene.acquireLock()
			pos = scene.getSelectedCenter()
			self.centerPnt = self.centerPnt * 0.97 + pos * 0.03
			self.centerOn(self.centerPnt)
			scene.releaseLock()

	def keyPressEvent(self, event):
		from UIManager import UIManager
		mainUI = UIManager.instance().getMainUI()
		self.setCursor(QtCore.Qt.ArrowCursor)
		# if event.modifiers() == QtCore.Qt.AltModifier:
		if event.key() == QtCore.Qt.Key_Up:
			mainUI.goToUp()
		elif event.key() == QtCore.Qt.Key_Down:
			mainUI.goToDown()
		elif event.key() == QtCore.Qt.Key_Left:
			mainUI.goToLeft()
		elif event.key() == QtCore.Qt.Key_Right:
			mainUI.goToRight()
		elif event.modifiers() == QtCore.Qt.AltModifier:
			if event.key() == QtCore.Qt.Key_1:
				mainUI.showScheme([1, True])
			elif event.key() == QtCore.Qt.Key_2:
				mainUI.showScheme([2, True])
			elif event.key() == QtCore.Qt.Key_3:
				mainUI.showScheme([3, True])
			elif event.key() == QtCore.Qt.Key_4:
				mainUI.showScheme([4, True])
			elif event.key() == QtCore.Qt.Key_5:
				mainUI.showScheme([5, True])
			elif event.key() == QtCore.Qt.Key_6:
				mainUI.showScheme([6, True])
			elif event.key() == QtCore.Qt.Key_7:
				mainUI.showScheme([7, True])
			elif event.key() == QtCore.Qt.Key_8:
				mainUI.showScheme([8, True])
			elif event.key() == QtCore.Qt.Key_9:
				mainUI.showScheme([9, True])
		elif event.modifiers() == QtCore.Qt.ControlModifier:
			if event.key() == QtCore.Qt.Key_1:
				mainUI.toggleSelectedEdgeToScheme([1, True])
			elif event.key() == QtCore.Qt.Key_2:
				mainUI.toggleSelectedEdgeToScheme([2, True])
			elif event.key() == QtCore.Qt.Key_3:
				mainUI.toggleSelectedEdgeToScheme([3, True])
			elif event.key() == QtCore.Qt.Key_4:
				mainUI.toggleSelectedEdgeToScheme([4, True])
			elif event.key() == QtCore.Qt.Key_5:
				mainUI.toggleSelectedEdgeToScheme([5, True])
			elif event.key() == QtCore.Qt.Key_6:
				mainUI.toggleSelectedEdgeToScheme([6, True])
			elif event.key() == QtCore.Qt.Key_7:
				mainUI.toggleSelectedEdgeToScheme([7, True])
			elif event.key() == QtCore.Qt.Key_8:
				mainUI.toggleSelectedEdgeToScheme([8, True])
			elif event.key() == QtCore.Qt.Key_9:
				mainUI.toggleSelectedEdgeToScheme([9, True])
		else:
			super(CodeView, self).keyPressEvent(event)
		self.viewport().update()

	def keyReleaseEvent(self, event):
		self.setCursor(QtCore.Qt.ArrowCursor)
		if event.key() == QtCore.Qt.Key_Control or event.key() == QtCore.Qt.Key_Shift:
			pass
		else:
			super(CodeView, self).keyReleaseEvent(event)

	def mousePressEvent(self, event):
		self.mouseCurPnt = self.mousePressPnt = event.pos()
		self.isMousePressed = True
		item = self.itemAt(self.mousePressPnt)
		self.isFrameSelectMode = (not item)

		modifiers = QtWidgets.QApplication.keyboardModifiers()
		if modifiers == QtCore.Qt.ControlModifier or modifiers == QtCore.Qt.ShiftModifier:
			if item:
				item.setSelected(not item.isSelected())
		else:
			super(CodeView, self).mousePressEvent(event)

	def mouseMoveEvent(self, event):
		self.mouseCurPnt = event.pos()
		if self.isFrameSelectMode:
			pass
		super(CodeView,self).mouseMoveEvent(event)
		#self.invalidateScene(self.scene().sceneRect())
		#self.update()
		self.viewport().update()

	def mouseReleaseEvent(self, event):
		self.mouseCurPnt = event.pos()
		self.isMousePressed = False
		if self.isFrameSelectMode:
			topLeftX = min(self.mousePressPnt.x(), self.mouseCurPnt.x())
			topLeftY = min(self.mousePressPnt.y(), self.mouseCurPnt.y())
			width = abs(self.mousePressPnt.x()-self.mouseCurPnt.x())
			height= abs(self.mousePressPnt.y()-self.mouseCurPnt.y())
			self.isFrameSelectMode = False
			itemList = self.items(topLeftX, topLeftY, width, height)

			modifiers = QtWidgets.QApplication.keyboardModifiers()
			if modifiers == QtCore.Qt.ShiftModifier:
				for item in itemList:
					item.setSelected(False)
			else:
				if modifiers != QtCore.Qt.ControlModifier:
					self.scene().clearSelection()
				for item in itemList:
					item.setSelected(True)

		super(CodeView, self).mouseReleaseEvent(event)

	def wheelEvent(self, event):
		posScene = self.mapToScene(event.pos())
		factor = 1.001 ** event.delta()
		self.scale(factor, factor)

		posMouse = self.mapFromScene(posScene)
		mov = posMouse - event.pos()
		self.horizontalScrollBar().setValue(mov.x() + self.horizontalScrollBar().value())
		self.verticalScrollBar().setValue(mov.x() + self.verticalScrollBar().value())

		self.centerPnt = self.mapToScene(self.viewport().rect().center())

	def drawForeground(self, painter, rectF):
		super(CodeView, self).drawForeground(painter, rectF)

		if self.isFrameSelectMode:
			if self.mousePressPnt and self.mouseCurPnt:
				topLeftX = min(self.mousePressPnt.x(), self.mouseCurPnt.x())
				topLeftY = min(self.mousePressPnt.y(), self.mouseCurPnt.y())
				width = abs(self.mousePressPnt.x()-self.mouseCurPnt.x())
				height= abs(self.mousePressPnt.y()-self.mouseCurPnt.y())

				painter.setPen(QtGui.QPen(QtGui.QColor(100,164,230),1.0))
				painter.setBrush(QtGui.QBrush(QtGui.QColor(100,164,230,100)))
				painter.setTransform(QtGui.QTransform())
				painter.drawRect(topLeftX, topLeftY, width, height)
		self.drawScheme(painter, rectF)
		self.drawLegend(painter, rectF)

	def drawScheme(self, painter, rectF):
		from UIManager import UIManager
		scene = UIManager.instance().getScene()
		schemeList = scene.getCurrentSchemeList()
		nScheme = len(schemeList)
		if not nScheme:
			return
		painter.setTransform(QtGui.QTransform())
		painter.setFont(self.hudFont)
		colorList = scene.getCurrentSchemeColorList()
		cw = 10
		y  = 10

		maxWidth = 0
		for ithScheme, schemeName in enumerate(schemeList):
			schemeSize = self.hudFontMetric.size(QtCore.Qt.TextSingleLine, schemeName)
			maxWidth = max(maxWidth, schemeSize.width())

		painter.setCompositionMode(QtGui.QPainter.CompositionMode_Multiply)
		painter.setPen(QtCore.Qt.NoPen)
		painter.setBrush(QtGui.QColor(0,0,0,150))
		painter.drawRect(5,5, 80 + maxWidth, nScheme * cw + (nScheme-1)*2 + 10)

		painter.setCompositionMode(QtGui.QPainter.CompositionMode_Source)
		for ithScheme, schemeName in enumerate(schemeList):
			painter.setPen(QtCore.Qt.NoPen)
			painter.setBrush(QtGui.QBrush(colorList[ithScheme]))
			painter.drawRect(QtCore.QRect(10,y+5,20,2))

			painter.setPen(QtGui.QPen(QtGui.QColor(255,157,38,255),1))
			painter.drawText(39, y+cw, 'Alt + %s' % (ithScheme+1,))
			painter.setPen(QtGui.QPen(QtGui.QColor(255,255,255,255),1))
			painter.drawText(QtCore.QRect(80,y-1,maxWidth,cw+3), QtCore.Qt.AlignRight | QtCore.Qt.AlignTop, schemeName)
			y += cw + 2

	def drawLegend(self, painter, rectF):
		painter.setTransform(QtGui.QTransform())
		painter.setFont(self.hudFont)

		from UIManager import UIManager
		scene = UIManager.instance().getScene()
		itemDict = scene.getItemDict()
		classNameDict = {}
		for uname, item in itemDict.items():
			if item.isSelected() or item.isConnectedToFocusNode:
				cname = item.getClassName()
				if not cname:
					cname = '[global function]'
				classNameDict[cname] = item.getColor()

		maxWidth = 0
		cw = 10
		nClasses = len(classNameDict)
		if not nClasses:
			return

		for className in classNameDict.keys():
			classSize = self.hudFontMetric.size(QtCore.Qt.TextSingleLine, className)
			maxWidth = max(maxWidth, classSize.width())

		painter.setCompositionMode(QtGui.QPainter.CompositionMode_Multiply)
		painter.setPen(QtCore.Qt.NoPen)
		painter.setBrush(QtGui.QColor(0,0,0,150))
		interiorHeight = nClasses * cw + (nClasses-1)*2
		painter.drawRect(5,self.height()-15-interiorHeight, 22 + maxWidth, interiorHeight + 10)

		painter.setCompositionMode(QtGui.QPainter.CompositionMode_Source)
		y = self.height() - 20
		for cname, clr in classNameDict.items():
			painter.setPen(QtCore.Qt.NoPen)
			painter.setBrush(QtGui.QBrush(clr))
			painter.drawRect(QtCore.QRect(10,y,cw,cw))

			painter.setPen(QtGui.QPen(QtGui.QColor(255,255,255,255),1))
			painter.drawText(cw+12, y+cw, cname)
			y -= cw + 2

	def drawComment(self, painter, rectF):
		from UIManager import UIManager
		scene = UIManager.instance().getScene()

		for uname, itemData in scene.itemDataDict.items():
			item = scene.itemDict.get(uname, None)
			if not item:
				continue

	def paintEvent(self, QPaintEvent):
		scene = self.scene()
		scene.acquireLock()
		t0 = time.time()
		QtWidgets.QGraphicsView.paintEvent(self, QPaintEvent)
		scene.releaseLock()


#coding=utf-8
__author__='silver'
import os
from os.path import splitext
from os.path import isfile
from os import listdir
from os import stat
from os import stat_result
from os import walk
from os import stat_float_times
#from re import re
from datetime import datetime
from dateutil import tz

"""

Function: util.filemanager.getdocumentlist(
                                            dir, # path to the directory
                                            recursive=True, # if it should recursive at the dir
                                            detail=True, # if need time and other detailed infos(in unix time format)
                                            )
Return Value: [{path, filename, [t_create, t_modify]},..]

"""

def getdocumentlist(path, recursive=True, detail=True):
    path=os.path.normpath(path)
    path=os.path.relpath(path)  #convert to rel path
    document_list=[]
    # have the list
    if recursive:
        for curdir, curdirlist, curfilelist in walk(path):
            for filename in curfilelist:
                    document_list.append({
                            "filename":splitext(filename)[0],
                            "path":curdir+"/"+filename
                        })
    else:
        for filename in listdir(path):  #have a loop test
            if os.path.isfile(path+'/'+filename):
                document_list.append({
                        "filename":splitext(filename)[0],
                        "path":path+'/'+filename          # just use relative path
                    })
    # find the detailed time if need
    if detail:
        # state save
        _stat=os.stat_float_times()
        os.stat_float_times(False)
        for x in range(len(document_list)):
            document_list[x].update(getdocumentdetail(document_list[x].get('path')))
        #state restore
        os.stat_float_times(_stat)
    return document_list

def getdocumentdetail(path):
    # os.stat_float_times(False) should be executed before all calls
    m=stat(path)
    strt=datetime.fromtimestamp(m.st_mtime, tz=tz.gettz('Asia/Shanghai')).strftime(r"%b.%d, %Y %Z %H:%M:%S")
    return {"path":path, "t_create":m.st_ctime, "t_modify":m.st_mtime, 'last_update':strt}
    # os.stat_float_times(True)  should be executed after all calls

if __name__=="__main__":
    print(getdocumentlist('.', detail=False))
    print('---')
    print(getdocumentlist('../controller'))


from typing import Tuple, Union
import datetime
from .spyglass_helpers import fetch_sessions, fetch_subjects, fetch_electrode_groups, fetch_interval_lists
import threading
import numpy as np

class RtcsharePlugin:
    def initialize(context):
        context.register_service('spyglass-explorer', SpyglassExplorerService)

# The lock is needed because datajoint database access is not thread-safe
lock = threading.Lock()

class SpyglassExplorerService:
    def handle_query(query: dict, *, dir: str, user_id: Union[str, None]=None) -> Tuple[dict, bytes]:
        # todo: authenticate user
        type0 = query['type']
        if type0 == 'get_subjects':
            with lock:
                a = fetch_subjects(
                    limit = query['limit'],
                    subject_id=query.get('subject_id', None),
                    age=query.get('age', None),
                    description=query.get('description', None),
                    genotype=query.get('genotype', None),
                    sex=query.get('sex', None),
                    species=query.get('species', None)
                )
                subjects = []
                for s in a:
                    subjects.append(s.key)
                return convert_to_json_serializable({'subjects': subjects}), b''
        elif type0 == 'get_sessions':
            with lock:
                a = fetch_sessions(
                    limit=query['limit'] + 1, # include one more so we can tell if there are additional sessions
                    session_id=query.get('session_id', None),
                    subject_id=query.get('subject_id', None),
                    experiment_description=query.get('experiment_description', None),
                    institution_name=query.get('institution_name', None),
                    lab_name=query.get('lab_name', None),
                    nwb_file_name=query.get('nwb_file_name', None),
                    session_description=query.get('session_description', None),
                    # session_start_time_min=query.get('session_start_time_min', None),
                    # session_start_time_max=query.get('session_start_time_max', None),
                    # timestamps_reference_time_min=query.get('timestamps_reference_time_min', None),
                    # timestamps_reference_time_max=query.get('timestamps_reference_time_max', None)
                )
                sessions = []
                has_more = False
                for s in a:
                    if len(sessions) >= query['limit']:
                        has_more = True
                        break
                    sessions.append(s.key)
                return convert_to_json_serializable({'sessions': sessions, 'has_more': has_more}), b''
        elif type0 == 'get_electrode_groups':
            with lock:
                a = fetch_electrode_groups(
                    limit=query['limit'] + 1, # include one more so we can tell if there are additional electrode groups
                    nwb_file_name=query['nwb_file_name']
                )
                electrode_groups = []
                has_more = False
                for eg in a:
                    if len(electrode_groups) >= query['limit']:
                        has_more = True
                        break
                    electrode_groups.append(eg.key)
                return convert_to_json_serializable({'electrode_groups': electrode_groups, 'has_more': has_more}), b''
        elif type0 == 'get_interval_lists':
            with lock:
                a = fetch_interval_lists(
                    limit=query['limit'] + 1, # include one more so we can tell if there are additional interval lists
                    nwb_file_name=query['nwb_file_name']
                )
                interval_lists = []
                has_more = False
                for il in a:
                    if len(interval_lists) >= query['limit']:
                        has_more = True
                        break
                    interval_lists.append(il.key)
                return convert_to_json_serializable({'interval_lists': interval_lists, 'has_more': has_more}), b''
        else:
            raise Exception(f'Unexpected query type: {type0}')

def convert_to_json_serializable(obj):
    if isinstance(obj, (datetime.date, datetime.datetime)):
        return obj.timestamp()
    elif isinstance(obj, np.ndarray):
        return obj.tolist()
    elif isinstance(obj, dict):
        ret = {}
        for key, val in obj.items():
            ret[key] = convert_to_json_serializable(val)
        return ret
    elif isinstance(obj, list):
        ret = []
        for val in obj:
            ret.append(convert_to_json_serializable(val))
        return ret
    else:
        return obj


#!/usr/bin/python3
"""
lsadgfshgjhdfhdgseasrdtfjyfjthrgaestr\
    dgfesasrgdhftrgserawestrdytfrte
"""
from models.base import Base

class Rectangle(Base):
    """xdfgchvjklljhgfhjkl"""
    def __init__(self, width, height, x=0, y=0, id=None):
        """yuitueryetawtrtyueyryet"""
        super().__init__(id)
        self.width = width
        self.height = height
        self.x = x
        self.y = y

    @property
    def width(self):
        """getter"""
        return self.__width

    @width.setter
    def width(self, value):
        """setter"""
        if not isinstance(value, int):
            raise TypeError("width must be an integer")
        if value <= 0:
            raise ValueError("width must be > 0")
        self.__width = value

    @property
    def height(self):
        """height getter"""
        return self.__height

    @height.setter
    def height(self, value):
        """height setter"""
        if not isinstance(value, int):
            raise TypeError("height must be an integer")
        if value <= 0:
            raise ValueError("height must be > 0")
        self.__height = value

    @property
    def x(self):
        """x getter"""
        return self.__x

    @x.setter
    def x(self, value):
        """x setter"""
        if not isinstance(value, int):
            raise TypeError("x must be an integer")
        if value < 0:
            raise ValueError("x must be >= 0")
        self.__x = value

    @property
    def y(self):
        """x getter"""
        return self.__y

    @y.setter
    def y(self, value):
        """y setter"""
        if not isinstance(value, int):
            raise TypeError("y must be an integer")
        if value < 0:
            raise ValueError("y must be >= 0")
        self.__y = value

    def area(self):
        """return the area of rectangle"""
        return self.__height * self.__width

    def display(self):
        """display the rectangle"""
        for _ in range(self.__y):
            print()
        for _ in range(self.__height):
            print(" " * self.__x + "#" * self.__width)

    def __str__(self):
        """return rectangle representation"""
        return f"[Rectangle] ({self.id}) {self.x}/{self.y}"\
            f" - {self.width}/{self.height}"

    def update(self, *args, **kwargs):
        """update method"""
        for i, kargs in kwargs.items():
            if i == "id":
                self.id = kargs
            elif i == "width":
                self.__width = kargs
            elif i == "height":
                self.__height = kargs
            elif i == "x":
                self.__x = kargs
            elif i == "y":
                self.__y = kargs
        for i, arg in enumerate(args):
            if i == 0:
                self.id = arg
            elif i == 1:
                self.__width = arg
            elif i == 2:
                self.__height = arg
            elif i == 3:
                self.__x = arg
            elif i == 4:
                self.__y = arg

    def to_dictionary(self):
        """to dictionary"""
        dictionary = {
            'x': self.x,
            'y': self.y,
            'id': self.id,
            'height': self.height,
            'width': self.width,
        }
        return dictionary


#import your algorithms here
from pip._vendor.distlib.compat import raw_input
from md5 import MD5_State
from RSA import xRsa
from VG import VG
from des import des
class Decryption_Runner:
    def __init__(self, algorithm=''):
        self.algorithm = algorithm

    def run(self):
        if(self.algorithm == 'RSA'):
            t = xRsa()
            print("Running RSA Decryption")
            #cipher = raw_input("Cipher text: ")
            plaintext = t.decrptyMsg(14638872678012233450065572822)
            # Print the string content after encryption and decryption
            print("decrypted text:" + plaintext)
        elif(self.algorithm == 'MD5'):
            print("unsupported")
        elif(self.algorithm == 'DES'):
            print("Running DES Decryption")
            desObj = des()
            desObj.decryption("key.txt", "file(txt).des")  # thie will generate file.txt at the same directory
        elif (self.algorithm == 'VG'):
            print("Running VG Decryption")
            obj = VG()
            cipher_text = input("Cipher_text: ")
            key = input("Key: ")
            obj.create_table(key)
            deciper_text = obj.decipher(cipher_text, key)
            print(deciper_text)
        else:
            print("end")


from kivy.uix.screenmanager import ScreenManager, SlideTransition

import decks.starting
import decks.summer
import decks.test
import decks.terraforming

import galaxy_gen

import numpy as np

stats = {
    'male_baby_count':0,'male_baby_approval':0,
    'female_baby_count':0,'female_baby_approval':0,
    'male_child_count':0,'male_child_approval':0,
    'female_child_count':0,'female_child_approval':0,
    'male_teen_count':0,'male_teen_approval':0,
    'female_teen_count':0,'female_teen_approval':0,

    'male_settler_count':45,'male_settler_approval':50,
    'female_settler_count':45,'female_settler_approval':50,
    'male_farmer_count':5,'male_farmer_approval':50,
    'female_farmer_count':5,'female_farmer_approval':50,

    'habitat_modules':20,'habitat_condition':100,'habitat_space_used':10,

    'greenhouse_modules':10,'greenhouse_condition':100,'greenhouse_space_used':2,'greenhouse_fertility':100,

    'food_reserves':500,'food_storage':1000,'food_production':200,'food_consumption':100,

    'atmosphere_percentage':0,
    'ocean_percentage':0,'arable_percentage':0,

    'year':0,
    'day':1,
    'hour':0,

    'current_card_id':''
}

#galaxy_stats = galaxy_gen.stats_gen()
#print(galaxy_stats)

sm = ScreenManager(transition=SlideTransition())

main_deck = decks.starting.deck.copy()

all_unused_cards = decks.terraforming.deck.copy() | decks.summer.deck.copy()

# Create an empty dictionary that will hold all_used_cards.
all_used_cards = {}

# Create the 'on deck' deck, where cards go once selected, but before actually added to the main deck.
to_be_added = {}


## Lowercase item types a through z have priorities 1 through 26.
## Uppercase item types A through Z have priorities 27 through 52.

f = open("C:/Data/Inputs/Advent of Code 2022/advent_of_code_day_3_input.txt", "r")
#print(f.read())

g = f.readlines()

totalPriority = 0

for line in g:
    print("Rucksack compartment lengths: " + str((len(line.strip()))/2))
    rucksack_comp_1 = line.strip()[:len(line.strip()) // 2]
    rucksack_comp_2 = line.strip()[len(line.strip()) // 2:]
    print(rucksack_comp_1)
    print(rucksack_comp_2)
    common_character = ''.join(
        set(rucksack_comp_1).intersection(rucksack_comp_2)
    )

    letterPriorityNumber = ord(common_character)

    if (letterPriorityNumber >= 97):
        actualPriority = letterPriorityNumber - 96
    if (letterPriorityNumber <= 90):
        actualPriority = letterPriorityNumber - 38
    print(actualPriority)
    print(common_character)

    totalPriority = actualPriority + totalPriority
    print(totalPriority)

    # - 96 - 38
    # a = 97 z = 122 A = 65 Z = 90


# -*- coding: utf-8 -*-
"""
Created on Sun Nov  3 22:44:22 2019

@author: lssya
"""
import sys
import time
import numpy as np
from matplotlib.patches import Rectangle
import point
import My_Map

class AStar:

#%%  构造函数

    def __init__(self, Map_size, obstacle_point, p_start, p_end):
        self.Map_size = Map_size
        self.obstacle_point = obstacle_point
        self.p_start = p_start
        self.p_end = p_end
        self.open_set = []
        self.close_set = []

#%% 节点到起点的移动代价，对应g(n)

    def GCost(self, p):
        h_diagonal = min(np.abs(self.p_start.x - p.x -1), np.abs(self.p_start.y - p.y - 1))
        h_stright = np.abs(self.p_start.x - p.x - 1) + np.abs(self.p_start.y - p.y - 1)
        # Distance to start point
        return h_stright + (np.sqrt(2) - 2) * h_diagonal

# 节点到终点的启发函数，对应h(n)
    def HCost(self, p):
        h_diagonal = min(np.abs(self.p_end.x - p.x - 1), np.abs(self.p_end.y - p.y - 1))
        h_stright = np.abs(self.p_end.x - p.x - 1) + np.abs(self.p_end.y - p.y - 1)
        # Distance to start point
        return h_stright + (np.sqrt(2) - 2) * h_diagonal

# 代价总和，即对应f(n)
    def TotalCost(self, p):
        return self.GCost(p) + self.HCost(p)

#%% 判断点是否有效，不在地图内部或者障碍物所在点都是无效的

    def IsValidPoint(self, x, y):
        if x < 0 or y < 0:
            return False
        if x >= self.Map_size[0] or y >= self.Map_size[1]:
            return False
        for p in self.obstacle_point:
            if x==p.x and y==p.y:
                return False
        return True

#%% 判断点是否在某个集合中,open_set 或 close_set?

    def IsInPointList(self, p, point_list):
        for pt in point_list:
            if pt.x == p.x and pt.y == p.y:
                return True
        return False

    def IsInOpenList(self, p):
        return self.IsInPointList(p, self.open_set)

    def IsInCloseList(self, p):
        return self.IsInPointList(p, self.close_set)

#%% 判断点是否是起点，终点

    def IsStartPoint(self, p):
        return p.x == self.p_start.x and p.y == self.p_start.y

    def IsEndPoint(self, p):
        return p.x == self.p_end.x and p.y == self.p_end.y

#%% 将当前状态保存到图片中，图片以当前时间命名

    def SaveImage(self, plt):
        exec_time = int(round(time.time() * 1000))
        filename = './' + str(exec_time) + '.png'
        plt.savefig(filename)

#%% 针对每一个节点进行处理：如果是没有处理过的节点，则计算优先级设置父节点，并且添加到open_set中。

    def ProcessPoint(self, x, y, parent):
        if not self.IsValidPoint(x, y):
            return # Do nothing for invalid point
        p = point.Point(x, y)
        if self.IsInCloseList(p):
            return # Do nothing for visited point
        print('Process Point [', p.x, ',', p.y, ']', ', cost: ', p.cost)
        if not self.IsInOpenList(p):
            p.parent = parent
            p.cost = self.TotalCost(p)
            self.open_set.append(p)

#%% 从open_set中找到优先级最高的节点，返回其索引

    def SelectPointInOpenList(self):
        index = 0
        selected_index = -1
        min_cost = sys.maxsize
        for p in self.open_set:
            cost = self.TotalCost(p)
            if cost < min_cost:
                min_cost = cost
                selected_index = index
            index += 1
        return selected_index

#%% 从终点往回沿着parent构造结果路径。然后从起点开始绘制结果，结果使用绿色方块，最后保存一个图片。

    def BuildPath(self, p, ax, plt, start_time):
        path = []
        while True:
            path.insert(0, p) # Insert first
            if self.IsStartPoint(p):
                break
            else:
                p = p.parent
        for p in path:
            rec = Rectangle((p.x, p.y), 1, 1, color='g')
            ax.add_patch(rec)
            plt.draw()

        self.SaveImage(plt)
        end_time = time.time()
        print('===== Algorithm finish in %.4f seconds' %(end_time-start_time))

#%% 算法实现

    def RunAndSaveImage(self, ax, plt, ):
        start_time = time.time()

        # 初始化起点以及起点的优先值，并加入open_set
        self.p_start.cost = 0
        self.open_set.append(self.p_start)

        while True:
            index = self.SelectPointInOpenList() # 返回open_set中优先级最高的索引
            if index < 0:
                print('No path found, algorithm failed!!!')
                return
            p = self.open_set[index] # 返回open_set中优先级最高的点
            rec = Rectangle((p.x, p.y), 1, 1, color='c') # 绘制p点
            ax.add_patch(rec)

            if self.IsEndPoint(p): # 判断是否为终点，如果是的话，绘制找到的最短路径
                return self.BuildPath(p, ax, plt, start_time)

            del self.open_set[index] #将该p点从open_set中删除，加入close_set中
            self.close_set.append(p)

            # Process all neighbors 处理以p为父节点的所有相邻点（8个）
            x = p.x
            y = p.y
            self.ProcessPoint(x-1, y+1, p)
            self.ProcessPoint(x-1, y, p)
            self.ProcessPoint(x-1, y-1, p)
            self.ProcessPoint(x, y-1, p)
            self.ProcessPoint(x+1, y-1, p)
            self.ProcessPoint(x+1, y, p)
            self.ProcessPoint(x+1, y+1, p)
            self.ProcessPoint(x, y+1, p)


from Views.evento_v import EventoV

def bar():
    print("=" * 32)

class SocialV(EventoV):
    def __init__(self):
        super().__init__()

    def mensagem(self, message: str):
        print(message)

    def capturar(self, message) -> str:
        while True:
            try:
                escolha = int(input(message))
            except ValueError:
                self.mensagem("Erro: A opção escolhida deve ser um número"
                              " inteiro.")
            else:
                break
        return escolha

    def mostrar_data(self, data):
        print("\nData: ", data)

    def mostrar_titulo(self, titulo):
        print("\nTítulo: ", titulo)

    def mostrar_descricao(self, descricao):
        print("\nDescrição: ", descricao)

    def mostrar_local(self, local):
        print("\nLocal: ", local)

    def mostrar_tudo(self, socical):
        print("Título: ", socical.titulo)
        print("Data: ", socical.data[0:2], "/", socical.data[2:])
        print("Local: ", socical.local)
        print("Detalhes: ", socical.descricao)

    def incluir_evento(self) -> tuple:
        while True:
            dia = input("\nInforme o dia: ").zfill(2)
            if not dia.isdigit() or int(dia) < 1 or int(dia) > 31:
                print("\nError: Dia inválido!Tente novamente")
            else:
                break
        while True:
            mes = input("\nAgora, informe o mês: ").zfill(2)
            if not mes.isdigit() or int(mes) < 1 or int(mes) > 12:
                print("\nError: Mês inválido!Tente novamente")
            else:
                break
        data = dia + mes
        titulo = input("\nInforme o tipo de atividade: ")
        local = input("\nInforme o local: ")
        descricao = input("\nPor fim, informe a descrição: ")
        return data, titulo, local, descricao

    def alterar_evento(self) -> tuple:
        titulo = input("\nInforme o novo tipo de atividade: ")
        local = input("\nInforme o novo local: ")
        descricao = input("\nPor fim, informe a nova descrição: ")
        return titulo, local, descricao

    def menu(self, chave: str) -> int:
        bar()
        print("MENU EVENTOS SOCIAIS DIA: ", chave[0:2], "/", chave[2:])
        bar()
        opcoes = ("1 - Visualizar eventos sociais", "2 - Editar eventos"
                  "sociais", "3 - Excluir eventos sociais",
                  "0 - Voltar ao menu do calendário")
        for opcao in opcoes:
            print(opcao)
        return self.capturar("Selecione uma das opções: ")


from flask import Flask, render_template
#from conexion import añadiralist
import sys
import pyodbc

#abrir la conexion con la base de datos
conn= pyodbc.connect(r"Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ= c:\Users\cuc\Documents\base\TallerDB.accdb;")
#abrir el cursor
cursor=conn.cursor()
app = Flask(__name__)
@app.route('/')
def index():
    cursor.execute("SELECT * FROM Estudiantes")
    Estudiantes=cursor.fetchall()
    cursor.execute("SELECT * FROM cursos")
    cursos=cursor.fetchall()
    return f"esta es la tabla de estudiantes
 {Estudiantes}
 esta es la tabla de cursos
{cursos}"
if __name__=='__main__':
    app.run()
cursor.close
#cerrar la conexion
conn.close


from custom_code.models import GladeCatalog
from django.core.management.base import BaseCommand

class Command(BaseCommand):

    help = 'Ingests the GLADE+ catalog into the database'

    def add_arguments(self, parser):
        parser.add_argument('--filename', help='Ingest catalog information from this file')

    def handle(self, *args, **options):

        filename = options['filename']

        with open(filename, 'r') as f:

            lines = f.readlines()
            column_names = ['pgc_no', 'gwgc_name', 'hyperleda_name', 'twomass_name',
                            'wisexscos_name', 'sdss_dr16q_name', 'object_type_flag',
                            'ra', 'dec']

            column_types = ['int', 'str', 'str', 'str', 'str', 'str', 'str', 'float', 'float']

            mag_dict_keys = ['B', 'B_err', 'B_flag', 'B_abs', 'J', 'J_err', 'H', 'H_err',
                             'K', 'K_err', 'W1', 'W1_err', 'W2', 'W2_err', 'W1_flag', 'B_J',
                             'B_J_err']

            column_names_two = ['z_helio', 'z_cmb', 'z_flag', 'v_err', 'z_err', 'd_l', 'd_l_err',
                                'dist_flag', 'm_star', 'm_star_err', 'm_star_flag', 'merger_rate',
                                'merger_rate_err']

            column_types_two = ['float', 'float', 'int', 'float', 'float', 'float', 'float', 'int',
                                'float', 'float', 'int', 'float', 'float']

            for line in lines:
                ### Insert into database
                x = line.split()
                param_dict = {}

                for i in range(len(column_names)):
                    if x[i+1] != 'null':

                        if column_types[i] == 'int':
                            param_dict[column_names[i]] = int(x[i+1])

                        elif column_types[i] == 'float':
                            param_dict[column_names[i]] = float(x[i+1])

                        else:
                            param_dict[column_names[i]] = x[i+1]

                mag_dict = {}

                for i in range(len(mag_dict_keys)):
                    if x[i+len(column_names)+1] != 'null' and 'flag' not in mag_dict_keys[i]:

                        mag_dict[mag_dict_keys[i]] = float(x[i+len(column_names)+1])

                    elif x[i+len(column_names)+1] != 'null':

                        mag_dict[mag_dict_keys[i]] = int(x[i+len(column_names)+1])

                param_dict['mag'] = mag_dict

                for i in range(len(column_names_two)):
                    if x[i+len(column_names)+len(mag_dict_keys)+1] != 'null':

                        if column_types_two[i] == 'int':
                            param_dict[column_names_two[i]] = int(x[i+len(column_names)+len(mag_dict_keys)+1])

                        elif column_types_two[i] == 'float':
                            param_dict[column_names_two[i]] = float(x[i+len(column_names)+len(mag_dict_keys)+1])

                        else:
                            param_dict[column_names_two[i]] = x[i+len(column_names)+len(mag_dict_keys)+1]

                g = GladeCatalog(**param_dict)
                g.save()


#!/usr/bin/env python3
import subprocess

with open('api-resources.txt', 'w') as f:
    subprocess.run(['kubectl','api-resources'], stdout=f)

file1 = open('api-resources.txt', 'r')
Lines = file1.readlines()

for line in Lines:
    final = line.strip().split()[1].split(',')[0]
    if ('v1' not in final) and ('v1beta1' not in final) and ('v1alpha1' not in final):
        output = subprocess.getoutput('kubectl explain '+final)
        if output == 'the server doesn\'t have a resource type "'+final+'"':
            print('INVALID STRING');
        else:
            items = output.split()
            if items[len(items)-2].strip() == 'DESCRIPTION:' and items[len(items)-1].strip() == '<empty>':
                print("-------------------->",final, "-----------> X")
            else:
                print(final, "------> T")


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

from setuptools import setup, find_packages
import glob
import ntpath

def get_module_name(module_path):
    """
    Return the module name of the module path
    """
    return ntpath.split(module_path)[1].split(".")[0]

def snake_to_camel(word):
    """
    Convert a word from snake_case to CamelCase
    """
    return ''.join(x.capitalize() or '_' for x in word.split('_'))

setup(
    display_name='Scheduler',
    name='fn_scheduler',
    version='2.1.1',
    license='MIT',
    author='IBM SOAR',
    url='https://ibm.com/mysupport',
    description="Functions to allow Rules/Playbooks to be scheduled",
    long_description="""This package of functions allows an enterprise to schedule a rule or playbook to run in the future associated with a incident, task, artifact, and datatable.

        Functions available include:

            1) Scheduling a rule/playbook

            2) Listing scheduled jobs

            3) Pause and resume scheduled job

            4) Removing a scheduled job""",
    install_requires=[
        'resilient_circuits>=46.0.0',
        'pytz',
        "APScheduler < 3.9;python_version < '3.6'",
        "APScheduler >= 3.9;python_version >= '3.6'",
        'SQLAlchemy[postgresql]>=1.3.8',
        'python-dateutil>=2.8.1'
    ],
    extras_require={
        'apphost': ['py-postgresql']
    },
    packages=find_packages(),
    include_package_data=True,
    platforms='any',
    classifiers=[
        'Programming Language :: Python',
    ],
    entry_points={
        "resilient.circuits.components": [
            # When setup.py is executed, loop through the .py files in the components directory and create the entry points.
            "{}FunctionComponent = fn_scheduler.components.{}:FunctionComponent".format(snake_to_camel(get_module_name(filename)), get_module_name(filename)) for filename in glob.glob("./fn_scheduler/components/[a-zA-Z]*.py")
        ],
        "resilient.circuits.configsection": ["gen_config = fn_scheduler.util.config:config_section_data"],
        "resilient.circuits.customize": ["customize = fn_scheduler.util.customize:customization_data"],
        "resilient.circuits.selftest": ["selftest = fn_scheduler.util.selftest:selftest_function"]
    }
)


#!/usr/bin/env python3

import argparse
import json
import os
import subprocess
import glob

# Parse Arguements and print
parser = argparse.ArgumentParser()

parser.add_argument('--json', type=str, required=True)
parser.add_argument('--dir', type=str, required=True)
parser.add_argument('--mixtape', default=False, type=bool, required=False)

args = vars(parser.parse_args())

json_file = args['json']
flac_dir =  args['dir']
mixtape_bool = args['mixtape']
print('\n')
print(flac_dir)
print('\t' + json_file.split('/')[-1])
print('\t' + str(mixtape_bool))
print('\n')

with open(json_file) as f:
   data = json.load(f)

# Core fuction
def mod_song_meta_data(data: dict, file: str, mixtape_bool: bool):
    file_name = file.split('/')[-1]
    track_number = file_name.split(' - ')[0]
    print('\t' + data['Songs'][track_number]['ARTIST'])

    #Album
    tag_album = data['ALBUM']
    cmd_album = [
                'metaflac',
                '--remove-tag=ALBUM',
                '--set-tag=ALBUM=' + tag_album,
                file
                ]

    #AlbumArtist
    tag_alb_art = data['ALBUMARTIST']
    cmd_alb_art = [
                'metaflac',
                '--remove-tag=ALBUMARTIST',
                '--set-tag=ALBUMARTIST=' + tag_alb_art,
                file
                ]

    #MUSICBRAINZ_ALBUMARTIST
    tag_muscbrnz_alb_art = data['MUSICBRAINZ_ALBUMARTIST']
    cmd_muscbrnz_alb_art = [
                'metaflac',
                '--remove-tag=MUSICBRAINZ_ALBUMARTIST',
                '--set-tag=MUSICBRAINZ_ALBUMARTIST=' + tag_muscbrnz_alb_art,
                file
                ]

    #ARTIST
    if mixtape_bool:
        tag_art = data['Songs'][track_number]['ARTIST']
    else:
        tag_art = data['ARTIST']
    cmd_art = [
                'metaflac',
                '--remove-tag=ARTIST',
                '--set-tag=ARTIST=' + tag_art,
                file
                ]

    #TRACKNUMBER
    tag_trknum = str(data['Songs'][track_number]['TRACKNUMBER'])
    cmd_trknum = [
                'metaflac',
                '--remove-tag=TRACKNUMBER',
                '--set-tag=TRACKNUMBER=' + tag_trknum,
                file
                ]

    #TITLE
    tag_title = data['Songs'][track_number]['TITLE']
    cmd_title = [
                'metaflac',
                '--remove-tag=TITLE',
                '--set-tag=TITLE=' + tag_title,
                file
                ]

    #GENRE
    tag_genre = data['GENRE']
    cmd_genre = [
                'metaflac',
                '--remove-tag=GENRE',
                '--set-tag=GENRE=' + tag_genre,
                file
                ]

    # Run the commands to process the files
    out_album = subprocess.run(cmd_album,
                            capture_output=True)
    out_alb_art = subprocess.run(cmd_alb_art,
                                capture_output=True)
    out_muscbrnz_alb_art = subprocess.run(cmd_muscbrnz_alb_art,
                                        capture_output=True)
    out_art = subprocess.run(cmd_art,
                            capture_output=True)
    out_trknum = subprocess.run(cmd_trknum,
                                capture_output=True)
    out_title = subprocess.run(cmd_title,
                            capture_output=True)
    out_genre = subprocess.run(cmd_genre,
                            capture_output=True)

    #Change file Name
    file_path = file.split('/')
    file_path.pop()

    file_name_new = track_number + ' - ' + tag_title + '.flac'
    file_new = '/'.join(file_path) + '/' + file_name_new

    os.rename(file, file_new)

    return file_new #Could add a dictionary of subprocess outs.

# Loop through files
new_files = []
for i in glob.glob(flac_dir + '*.flac'):
    file_name = i.split('/')[-1]
    print(file_name)
    new_files.append(mod_song_meta_data(data, i, mixtape_bool))
    print('\t' + new_files[-1].split('/')[-1])

# Command line example check that the renameing worked. Could fold into a check.
#ls /Users/jcorwin/Desktop/MusicTemp/MixTapes/Lisa\ MixTape\ 01/

#metaflac --list /Users/jcorwin/Desktop/MusicTemp/MixTapes/Lisa\ MixTape\ 01/15\ -\ Uncle\ John\'s\ Band.flac | grep comment


# Author: kk.Fang(fkfkbill@gmail.com)

from .base import PrivilegeReq
from auth.user import *
from ..const import PRIVILEGE
from utils.schema_utils import *
from models.sqlalchemy import *
from restful_api.modules import *

@as_view(group="auth")
class PrivilegeHandler(PrivilegeReq):

    def get(self):
        """权限列表"""
        params = self.get_query_args(Schema({
            scm_optional("type", default=PRIVILEGE.ALL_PRIVILEGE_TYPE): And(
                scm_dot_split_int,
                scm_subset_of_choices(PRIVILEGE.ALL_PRIVILEGE_TYPE)
            ),
            scm_optional("current_user", default=False): scm_bool,
            **self.gen_p(per_page=99)
        }))
        privilege_type: list = params.pop("type")
        current_user = params.pop("current_user")
        p = self.pop_p(params)

        if current_user:
            if self.is_admin():
                # admin用户拥有任何权限
                privilege_ids = PRIVILEGE.get_all_privilege_id()
            else:
                with make_session() as session:
                    privilege_ids = [
                        i[0]
                        for i in session.query(RolePrivilege.privilege_id).
                        join(UserRole, RolePrivilege.role_id == UserRole.role_id).
                        filter(UserRole.login_user == self.current_user)
                    ]
            privilege_dicts = [
                PRIVILEGE.privilege_to_dict(PRIVILEGE.get_privilege_by_id(i))
                for i in privilege_ids if PRIVILEGE.get_privilege_by_id(i)
            ]
            privilege_dicts = [
                i["name"]
                for i in privilege_dicts
                if i["type"] in privilege_type
            ]

        else:
            privilege_dicts = [
                PRIVILEGE.privilege_to_dict(i)
                for i in PRIVILEGE.get_privilege_by_type(privilege_type)
            ]

        items, p = self.paginate(privilege_dicts, **p)
        self.resp([i for i in items], **p)

    get.argument = {
        "querystring": {
            "//type": "2,3",
            "//current_user": "1",
            "page": "1",
            "per_page": "10"
        },
        "json": {}
    }


import click

from utils.raster import GeoTiffConverter
from utils.data import RasterData

@click.command()
@click.argument('datasets', type=lambda s: s.split(','))
def convert_to_zarr(datasets):
    """
    Convert GeoTIFFs to Zarr.
    """
    groups = {'global': ['recent'],#['historic', 'recent'],
              'scenarios': ['crop_I', 'crop_MG', 'crop_MGI', 'grass_part', 'grass_full', 'rewilding',
                            'degradation_ForestToGrass', 'degradation_ForestToCrop', 'degradation_NoDeforestation'],
              'experimental': ['stocks', 'concentration']}

    for dataset in datasets:
        print(dataset)
        for group in groups[dataset]:
            print(group)
            # Create an instance of a GeoTiffData Data Class with all data information
            geotiff_data = RasterData(dataset, group)
            # Save GeoTIFFs as Zarr
            geotiff_converter = GeoTiffConverter(geotiff_obj=geotiff_data)
            geotiff_converter.convert_to_zarr()

if __name__ == '__main__':
    convert_to_zarr()


from __future__ import print_function, absolute_import
import re
from llvm.core import (Type, Builder, LINKAGE_INTERNAL, inline_function,
                       Constant, ICMP_EQ)
from numba import typing, types, cgutils
from numba.targets.base import BaseContext
from .cudadrv import nvvm
from . import nvvmutils

# -----------------------------------------------------------------------------
# Typing

class CUDATypingContext(typing.BaseContext):
    def init(self):
        from . import cudadecl, cudamath

        self.install(cudadecl.registry)
        self.install(cudamath.registry)

# -----------------------------------------------------------------------------
# Implementation

VALID_CHARS = re.compile(r'[^a-z0-9]', re.I)

class CUDATargetContext(BaseContext):
    implement_powi_as_math_call = True

    def init(self):
        from . import cudaimpl, libdevice

        self.insert_func_defn(cudaimpl.registry.functions)
        self.insert_func_defn(libdevice.registry.functions)

    def mangler(self, name, argtypes):
        def repl(m):
            ch = m.group(0)
            return "_%X_" % ord(ch)

        qualified = name + '.' + '.'.join(str(a) for a in argtypes)
        mangled = VALID_CHARS.sub(repl, qualified)
        return mangled

    def prepare_cuda_kernel(self, func, argtypes):
        # Adapt to CUDA LLVM
        module = func.module
        func.linkage = LINKAGE_INTERNAL
        wrapper = self.generate_kernel_wrapper(func, argtypes)
        func.delete()
        del func

        nvvm.set_cuda_kernel(wrapper)
        nvvm.fix_data_layout(module)

        return wrapper

    def generate_kernel_wrapper(self, func, argtypes):
        module = func.module
        argtys = self.get_arguments(func.type.pointee)
        fnty = Type.function(Type.void(), argtys)
        wrapfn = module.add_function(fnty, name="cudaPy_" + func.name)
        builder = Builder.new(wrapfn.append_basic_block(''))

        # Define error handling variables
        def define_error_gv(postfix):
            gv = module.add_global_variable(Type.int(),
                                            name=wrapfn.name + postfix)
            gv.initializer = Constant.null(gv.type.pointee)
            return gv

        gv_exc = define_error_gv("__errcode__")
        gv_tid = []
        gv_ctaid = []
        for i in 'xyz':
            gv_tid.append(define_error_gv("__tid%s__" % i))
            gv_ctaid.append(define_error_gv("__ctaid%s__" % i))

        callargs = []
        for at, av in zip(argtypes, wrapfn.args):
            av = self.get_argument_value(builder, at, av)
            callargs.append(av)

        status, _ = self.call_function(builder, func, types.void, argtypes,
                                       callargs)

        # Check error status
        with cgutils.if_likely(builder, status.ok):
            builder.ret_void()

        with cgutils.ifthen(builder, builder.not_(status.exc)):
            # User exception raised
            old = Constant.null(gv_exc.type.pointee)

            # Use atomic cmpxchg to prevent rewriting the error status
            # Only the first error is recorded
            xchg = builder.atomic_cmpxchg(gv_exc, old, status.code,
                                          "monotonic")
            changed = builder.icmp(ICMP_EQ, xchg, old)

            # If the xchange is successful, save the thread ID.
            sreg = nvvmutils.SRegBuilder(builder)
            with cgutils.ifthen(builder, changed):
                for dim, ptr, in zip("xyz", gv_tid):
                    val = sreg.tid(dim)
                    builder.store(val, ptr)

                for dim, ptr, in zip("xyz", gv_ctaid):
                    val = sreg.ctaid(dim)
                    builder.store(val, ptr)

        builder.ret_void()

        # force inline
        inline_function(status.code)

        module.verify()
        return wrapfn

    def link_dependencies(self, module, depends):
        for lib in depends:
            module.link_in(lib, preserve=True)

    def make_constant_array(self, builder, typ, ary):
        """
        Return dummy value.

        XXX: We should be able to move cuda.const.array_like into here.
        """

        a = self.make_array(typ)(self, builder)
        return a._getvalue()

    def insert_string_const_addrspace(self, builder, string):
        """
        Insert a constant string in the constant addresspace and return a
        generic i8 pointer to the data.

        This function attempts to deduplicate.
        """
        lmod = builder.basic_block.function.module
        text = Constant.stringz(string)
        name = "__conststring__.%s" % string
        charty = Type.int(8)

        for gv in lmod.global_variables:
            if gv.name == name and gv.type.pointee == text.type:
                break
        else:
            gl = lmod.add_global_variable(text.type, name=name,
                                          addrspace=nvvm.ADDRSPACE_CONSTANT)
            gl.linkage = LINKAGE_INTERNAL
            gl.global_constant = True
            gl.initializer = text

            constcharptrty = Type.pointer(charty, nvvm.ADDRSPACE_CONSTANT)
            charptr = builder.bitcast(gl, constcharptrty)

        conv = nvvmutils.insert_addrspace_conv(lmod, charty,
                                               nvvm.ADDRSPACE_CONSTANT)
        return builder.call(conv, [charptr])


import logging
import typing

import scenario
from scenario.reflex import qualname
import scenario.test
import scenario.text

class LoadConfigFile(scenario.test.Step):

    def __init__(
            self,
            input_path,  # type: scenario.Path
            root_key="",  # type: str
    ):  # type: (...) -> None
        scenario.test.Step.__init__(self)

        self.input_path = input_path  # type: scenario.Path
        self.root_key = root_key  # type: str

    def step(self):  # type: (...) -> None
        self.STEP(f"Load '{self.input_path}'")

        _root_key_spec = f" from key {self.root_key!r}" if self.root_key else " at the root level"  # type:str
        if self.ACTION(f"Load the '{self.input_path}' configuration file{_root_key_spec}."):
            scenario.conf.loadfile(self.input_path, root=self.root_key)

class SaveConfigFile(scenario.test.Step):

    def __init__(
            self,
            output_path,  # type: scenario.Path
            root_key="",  # type: str
    ):  # type: (...) -> None
        scenario.test.Step.__init__(self)

        self.output_path = output_path  # type: scenario.Path
        self.root_key = root_key  # type: str

    def step(self):  # type: (...) -> None
        self.STEP(f"Save '{self.output_path}'")

        _root_key_spec = f" from key {self.root_key!r}" if self.root_key else " from the root level"  # type:str
        if self.ACTION(f"Save the configuration database{_root_key_spec} to file '{self.output_path}'"):
            scenario.conf.savefile(self.output_path, root=self.root_key)

class ShowConfigValue(scenario.test.Step):

    def __init__(
            self,
            key,  # type: str
    ):  # type: (...) -> None
        scenario.test.Step.__init__(self)

        self.key = key  # type: str

    def step(self):  # type: (...) -> None
        self.STEP(f"Show {self.key}")

        if self.ACTION(f"Show configuration key {self.key!r}."):
            scenario.conf.show(logging.INFO)

class StoreConfigValue(scenario.test.Step):

    def __init__(
            self,
            key,  # type: str
            value,  # type: typing.Any
    ):  # type: (...) -> None
        scenario.test.Step.__init__(self)

        self.key = key  # type: str
        self.value = value  # type: typing.Any

    def step(self):  # type: (...) -> None
        self.STEP(f"Set {self.key}={self.value!r}")

        if self.ACTION(f"Store {self.key}={self.value!r} in the configuration database."):
            scenario.conf.set(self.key, self.value)

class RemoveConfigValue(scenario.test.Step):

    def __init__(
            self,
            key,  # type: str
    ):  # type: (...) -> None
        scenario.test.Step.__init__(self)

        self.key = key  # type: str

    def step(self):  # type: (...) -> None
        self.STEP(f"Unset {self.key}")

        if self.ACTION(f"Remove {self.key!r} from the configuration database."):
            scenario.conf.remove(self.key)

class CheckDictConfigNode(scenario.test.Step):

    def __init__(
            self,
            key,  # type: str
            field_names,  # type: typing.List[str]
    ):  # type: (...) -> None
        scenario.test.Step.__init__(self)

        self.key = key  # type: str
        self.field_names = field_names  # type: typing.List[str]

    def step(self):  # type: (...) -> None
        self.STEP(f"Check {self.key!r} section node")

        _data = None  # type: typing.Any
        if self.ACTION(f"Retrieve configuration data for key {self.key!r}."):
            _data = scenario.conf.get(self.key)
            self.evidence(f"Data: {scenario.debug.saferepr(_data)}")
        if self.RESULT("The data retrieved is a dictionary."):
            self.assertisinstance(
                _data, dict,
                evidence=f"{self.key!r} data type",
            )
        _fields_txt = scenario.text.Countable("field", self.field_names)  # type: scenario.text.Countable
        if self.RESULT(f"It contains {len(_fields_txt)} {_fields_txt}{_fields_txt.ifany(':', '.')}"):
            self.assertlen(
                _data, len(self.field_names),
                evidence=f"{self.key!r} number of fields",
            )
        for _field_name in self.field_names:  # type: str
            if self.RESULT(f"- {_field_name!r}"):
                self.assertin(
                    _field_name, _data,
                    evidence=f"{self.key!r} field {_field_name!r}",
                )

class CheckListConfigNode(scenario.test.Step):

    def __init__(
            self,
            key,  # type: str
            length,  # type: int
    ):  # type: (...) -> None
        scenario.test.Step.__init__(self)

        self.key = key  # type: str
        self.length = length  # type: int

    def step(self):  # type: (...) -> None
        self.STEP(f"Check {self.key!r} list node")

        _data = None  # type: typing.Any
        if self.ACTION(f"Retrieve configuration data for key {self.key!r}."):
            _data = scenario.conf.get(self.key)
            self.evidence(f"Data: {scenario.debug.saferepr(_data)}")
        if self.RESULT("The data retrieved is a list."):
            self.assertisinstance(
                _data, list,
                evidence=f"{self.key!r} data type",
            )
        _items_txt = scenario.text.Countable("item", self.length)  # type: scenario.text.Countable
        if self.RESULT(f"It contains {len(_items_txt)} {_items_txt}."):
            self.assertlen(
                _data, self.length,
                evidence=f"{self.key!r} number of items",
            )

class CheckConfigValue(scenario.test.Step):

    def __init__(
            self,
            key,  # type: str
            read_as,  # type: typing.Optional[type]
            expected_type,  # type: type
            expected_value=None,  # type: typing.Any
            origin=None,  # type: str
    ):  # type: (...) -> None
        scenario.test.Step.__init__(self)

        self.key = key  # type: str
        self.read_as = read_as  # type: typing.Optional[type]
        self.expected_type = expected_type  # type: type
        self.expected_value = expected_value  # type: typing.Optional[typing.Any]
        self.origin = origin  # type: typing.Optional[str]

        self.value_read = None  # type: typing.Any

    def step(self):  # type: (...) -> None
        self.STEP(f"Read {self.key!r}{f' as `{qualname(self.read_as)}`' if self.read_as is not None else ''}")

        if self.read_as is None:
            self.ACTION(f"Read {self.key!r} without specifying the type.")
        else:
            self.ACTION(f"Read {self.key!r} as a `{qualname(self.read_as)}` value.")

        _value_error = None  # type: typing.Optional[ValueError]
        if self.doexecute():
            try:
                if self.read_as is None:
                    self.value_read = scenario.conf.get(self.key)
                else:
                    self.value_read = scenario.conf.get(self.key, type=self.read_as)
                self.evidence(f"Value read: {scenario.debug.saferepr(self.value_read)}")
            except ValueError as _err:
                _value_error = _err

        if self.expected_type is not ValueError:
            if self.RESULT(f"The type of the value read is `{qualname(self.expected_type)}`."):
                self.assertisinstance(
                    self.value_read, self.expected_type,
                    evidence=f"Value type{f' read as `{qualname(self.read_as)}`' if self.read_as is not None else ''}",
                )
            if self.expected_value is not None:
                if self.RESULT(f"The value read is {self.expected_value!r}."):
                    self.assertequal(
                        self.value_read, self.expected_value,
                        evidence="Value",
                    )
        else:
            if self.RESULT("A `ValueError` exception is raised."):
                self.assertisnotnone(
                    _value_error,
                    evidence="Value error",
                )
            if self.origin is not None:
                if self.RESULT("The exception gives the origin of the bad configuration value."):
                    self.assertin(
                        self.origin, str(_value_error),
                        evidence="Configuration value origin",
                    )


#!/usr/bin/python
import ecto #ecto core library
from ecto.opts import scheduler_options, run_plasm, cell_options
from srv_ecto_vision import features3d
import ecto_ros
from ecto_ros import ecto_sensor_msgs
import ecto_opencv
from ecto_opencv.highgui import imshow, FPSDrawer
from ecto_opencv.features2d import DrawKeypoints
import argparse

#short names
ImageSub = ecto_sensor_msgs.Subscriber_Image
InfoSub = ecto_sensor_msgs.Subscriber_CameraInfo

debug = True

def do_ecto():
  parser = argparse.ArgumentParser(description="Stereo Feature Extractor")

  # add cell options
  feature_extractor_factory = cell_options(parser, features3d.FeatureExtractor, prefix="fe")

  # add scheduler options
  group = parser.add_argument_group("ecto scheduler options")
  scheduler_options(group)

  options = parser.parse_args()

  # setup ros subscribers
  subscriptions = dict(image_left  = ImageSub(topic_name="image_left",  queue_size=0),
                       #info_left   = InfoSub (topic_name="camera_info_left", queue_size=0),
                       image_right = ImageSub(topic_name="image_right", queue_size=0),
                       #info_right  = InfoSub (topic_name="camera_info_right", queue_size=0)
                       )
  sync = ecto_ros.Synchronizer("Synchronizer", subs=subscriptions)

  # setup cells
  image2mat_left = ecto_ros.Image2Mat()
  image2mat_right = ecto_ros.Image2Mat()
  feature_extractor_left  = feature_extractor_factory(options)
  feature_extractor_right = feature_extractor_factory(options)

  match_mask_creator = features3d.StereoMatchMaskCreator()
  descriptor_matcher = features3d.DescriptorMatcherKnn()
  matches_filter = features3d.MatchesFilter(verbose=True)

  #setup the processing graph
  graph = [
      sync["image_left"] >> image2mat_left["image"],
      image2mat_left["image"] >> feature_extractor_left["image"],
      sync["image_right"] >> image2mat_right["image"],
      image2mat_right["image"] >> feature_extractor_right["image"],

      feature_extractor_left["key_points"] >> match_mask_creator["key_points_left"],
      feature_extractor_right["key_points"] >> match_mask_creator["key_points_right"],

      feature_extractor_left["descriptors"] >> descriptor_matcher["train_descriptors"],
      feature_extractor_right["descriptors"] >> descriptor_matcher["test_descriptors"],

      descriptor_matcher["knn_matches"] >> matches_filter["knn_matches"],
      match_mask_creator["match_mask"] >> matches_filter["match_mask"],

      match_mask_creator["match_mask"] >> imshow(name="match mask")["image"],
      ]

  #instantiate a plasm
  plasm = ecto.Plasm()
  plasm.connect(graph)

  sched = ecto.schedulers.Singlethreaded(plasm)

  run_plasm(options, plasm, locals=vars())

if __name__ == '__main__':
  import sys
  # init ros (does topic remapping)
  ecto_ros.init(sys.argv, "stereo_feature_extraction")
  do_ecto()


import time
import torch
import torch.nn as nn
from data import dataset
from torch.utils.data import DataLoader
from training import average, current_prog
from conv_lstm import Conv_LSTM
import training as tu
import scipy.ndimage as ndimage
import matplotlib.pyplot as plt
from setup import device
from setup import Configs
import setup

def test(val_loader, model, criterion, log_every=1):
    batch_time = average('Time', ':6.3f')
    losses = average('Loss', ':.4e')
    acc = average('Acc', ':6.4f')
    f1 = average('F1', ':6.4f')
    prec = average('Prec', ':6.4f')
    rec = average('Recall', ':6.4f')
    progress = current_prog(
        len(val_loader),
        [batch_time, losses, acc, f1, prec, rec],
        prefix='Test: ')

    #model.eval()
    model.train()

    correct = 0
    error = 0
    precision = 0.
    recall = 0.
    with torch.no_grad():
        end = time.time()
        for batch_no, (test_samples, gt_test) in enumerate(val_loader):

            test_samples = [t.to(device) for t in test_samples]
            gt_test = gt_test.squeeze(1).long().to(device)
            output = model(test_samples)
            loss = criterion(output, gt_test)
            losses.update(loss.item(), gt_test.size(0))
            f, (p, r) = f1_score(output, gt_test.float())
            f1.update(f)
            prec.update(p)
            rec.update(r)
            acc.update(pixel_accuracy(output, gt_test), gt_test.size(0))
            batch_time.update(time.time() - end)
            end = time.time()

            if batch_no % log_every == 0:
                progress.display(batch_no)

        return acc.avg

def pixel_accuracy(prediction:torch.Tensor, target:torch.Tensor):
    out = (prediction[:, 1, :, :] > 0.).float()
    return (out == target).float().mean().item()

def f1_score(output, target, epsilon=1e-7):
    probas = (output[:, 1, :, :] > 0.).float()
    TP = (probas * target).sum(dim=1)
    precision = TP / (probas.sum(dim=1) + epsilon)
    recall = TP / (target.sum(dim=1) + epsilon)
    f1 = 2 * precision * recall / (precision + recall + epsilon)
    f1 = f1.clamp(min=epsilon, max=1-epsilon)
    return f1.mean().item(), (precision.mean().item(), recall.mean().item())

configs = Configs()
print("Loading stored model")
model = Conv_LSTM(configs.hidden_dims, decoder_out_channels=2, lstm_nlayers=len(configs.hidden_dims),
                       vgg_decoder_config=configs.decoder_config)
tu.load_model_checkpoint(model, 'train-results/model.torch', inference=False, map_location=device)
model.to(device)
print("Model loaded")

tu_test_dataset = dataset(setup.test_dir, setup.test_sub,setup.test_labels, shuffle=False)
tu_test_dataloader = DataLoader(tu_test_dataset, batch_size=configs.test_batch, shuffle=True, num_workers=2)

criterion = nn.CrossEntropyLoss(weight=torch.FloatTensor([0.02, 1.02])).to(device)

test(tu_test_dataloader, model, criterion, log_every=1)


import jax.numpy as jnp
import pytest

from moll.metrics import euclidean

from .._online_add import (
    _add_point,
    _finalize_updates,
    _needless_point_idx,
    update_points,
)

# Test finalize_updates

@pytest.mark.parametrize(
    "array, expected",
    [
        ([-1], [-1]),
        ([2], [2]),
        ([-1, -1, -1], [-1, -1, -1]),
        ([3, 3, 3], [-1, -1, 3]),
        ([1, 2, 3], [1, 2, 3]),
        ([1, 1, 2, 2, 3, 3], [-1, 1, -1, 2, -1, 3]),
        ([1, 2, 2, 3, 3, 3], [1, -1, 2, -1, -1, 3]),
        ([3, 2, 1], [3, 2, 1]),
        ([3, 3, -1, 2, 2, -1, 1], [-1, 3, -1, -1, 2, -1, 1]),
    ],
)
def test_finalize_updates(array, expected):
    array = jnp.array(array)
    expected = jnp.array(expected)
    output_array = _finalize_updates(array)
    assert jnp.all(output_array == expected)

# Test needless point search

@pytest.mark.parametrize(
    "array, expected",
    [
        ([0, 0.1, 1], 1),
        ([0, 0, 1], 0),
        ([0, 0, 0, 1], 0),
        ([(0.1, 0.1), (0, 0), (1, 1)], 0),
    ],
)
@pytest.mark.parametrize(
    "dist_fn",
    [
        euclidean,
        lambda x, y: euclidean(x, y) - 10,  # negative distance is ok
    ],
)
def test_find_needless_point(array, expected, dist_fn):
    array = jnp.array(array)
    # exp potential is used to treat negative distances
    idx = _needless_point_idx(array, dist_fn, lambda d: jnp.exp(-d))
    assert idx == expected

# Test add points

@pytest.fixture
def X():
    return jnp.array(
        [
            [0, 0],
            [1, 1],
            [2, 2],
            [3, 3],
            [4, 4],
        ],
        dtype=jnp.float32,
    )

@pytest.mark.parametrize(
    "similarity_fn",
    [
        euclidean,
        lambda x, y: euclidean(x, y) + 10,
        lambda x, y: euclidean(x, y) - 10,
    ],
)
def test_add_point(X, similarity_fn):
    x = jnp.array([4.3, 4.3])
    X_copy = X.copy()
    X_updated, updated_idx = _add_point(
        x=x,
        X=X,
        similarity_fn=similarity_fn,
        potential_fn=lambda d: jnp.exp(-d),
        k_neighbors=5,
        n_valid_points=5,
        threshold=-jnp.inf,
    )
    assert updated_idx >= 0
    assert updated_idx == 4
    assert (X_copy[:4] == X_updated[:4]).all()
    assert (X_updated[4] == x).all()

@pytest.mark.parametrize(
    "xs, acc_mask",
    [
        (
            [[4.3, 4.3], [4.2, 4.2], [4.1, 4.1]],
            [True, False, False],
        ),
        (
            [[4.1, 4.1], [4.2, 4.2], [4.3, 4.3]],
            [False, False, True],
        ),
    ],
)
def test_update_points(X, xs, acc_mask):
    xs = jnp.array(xs)
    acc_mask = jnp.array(acc_mask)
    X_copy = X.copy()

    X_updated, updated_idxs, acceptance_mask, n_appended, n_updated = update_points(
        X=X,
        xs=xs,
        similarity_fn=euclidean,
        potential_fn=lambda d: d**-1,
        k_neighbors=5,
        n_valid=5,
        threshold=0.0,
    )

    assert (acceptance_mask == acc_mask).all()
    assert (X_copy[:3] == X_updated[:3]).all()
    idx = jnp.argmax(acc_mask)
    assert (X_updated[4] == xs[idx]).all()


import os
from setuptools import find_packages, setup

here = os.path.abspath(os.path.dirname(__file__))

with open(os.path.join("pycharge", "version.txt")) as file_handler:
    __version__ = file_handler.read().strip()

with open(os.path.join(here, "README.md"), encoding="utf-8") as f:
    long_description = f.read()

DESCRIPTION = 'ML Library'

# Setting up
setup(
    name="python-charge",
    version=__version__,
    author="Ashutosh Mishra",
    url="https://github.com/ASH1998/pYcharge",
    author_email="pythonicdevil@gmail.com",
    description=DESCRIPTION,
    license="MIT",
    long_description_content_type="text/markdown",
    long_description=long_description,
    packages=[package for package in find_packages() if package.startswith("pycharge")],
    install_requires=[],
    python_requires=">=3.8",
    keywords=['python', 'machine learning'],
    classifiers=[
        "Development Status :: 1 - Planning",
        "Intended Audience :: Developers",
        "Programming Language :: Python :: 3",
        "Operating System :: Unix",
        "Operating System :: MacOS :: MacOS X",
        "Operating System :: Microsoft :: Windows",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.7",
        "Programming Language :: Python :: 3.8",
        "Programming Language :: Python :: 3.9",
    ]
)


# https://app.codility.com/demo/results/trainingM6UF8C-NRK/

from bisect import bisect_left, bisect_right

def solution(A):
    n = len(A)

    # O(N)
    # 양수, 음수로 분리
    positive, negative = [], []
    for i in range(n):
        # base case: 0인 경우 -> 절대값이 0 -> 절대값의 최소
        if A[i] == 0:
            return 0
        elif A[i] > 0:
            positive.append(A[i])
        else:
            negative.append(A[i])

    # O(N * log(N))
    min_abs = int(1e9) * 2
    if positive:
        positive.sort()
        # min_abs case 1) 절대값이 가장 작은 수 * 2
        min_abs = min(min_abs, positive[0] * 2)
        # min_abs case 2) 절대값이 가장 작은 두 수의 합
        if len(positive) >= 2:
            min_abs = min(min_abs, positive[0] + positive[1])
    if negative:
        negative.sort()
        # min_abs case 1)
        min_abs = min(min_abs, (-negative[-1]) * 2)
        # min_abs case 2)
        if len(negative) >= 2:
            min_abs = min(min_abs, -(negative[-1] + negative[-2]))

    # base case: 양수 혹은 음수만 있는 경우 위에서 정답 발생
    if len(positive) == 0 or len(negative) == 0:
        return min_abs

    # O(N * logN)
    # min_abs case 3) 부호 반대에서 절대값이 가장 비슷한 수
    positive_n = len(positive)
    for neg in negative:
        target = -neg
        # 1) bisect_left -> 동일한 값은 bl_idx + 1 이상에서 발생
        bl_idx = bisect_left(positive, target)
        # base case: 절대값이 반대인 수가 있으면 최소값 0 리턴
        if (bl_idx < positive_n - 1) and positive[bl_idx + 1] == target:
            min_abs = 0
            break
        if bl_idx > 0:
            min_abs = min(min_abs, target - positive[bl_idx - 1])
        if bl_idx < positive_n:
            min_abs = min(min_abs, abs(positive[bl_idx] - target)) # bl_idx 번째는 뭐가 큰지 모름
        # 2) bisect_right -> 동일한 값은 이미 위에서 체크
        br_idx = bisect_right(positive, target)
        if br_idx > 0:
            min_abs = min(min_abs, target - positive[br_idx - 1])
        if br_idx < positive_n:
            min_abs = min(min_abs, abs(positive[br_idx] - target)) # br_idx 번째는 뭐가 큰기 모름

    return min_abs


import math
import time
import data_handler
from stop_words import get_stop_words

stop_words = get_stop_words('english')  #import and declare the stopwords
#Storing these variables in RAM for better performance
training_data = None
pos_words_dict = None
neg_words_dict = None
positive_review_count = None
negative_review_count = None
prob_positive = None
prob_negative = None
test_pos_reviews = None
test_neg_reviews = None

def predict_input(text):
	"""
	This function will attempt to predict whether a review that is written by the user is positive or negative.
	:param text: the input from the user
	:return: a set with the results.
	"""
	train()  #this will attempt to load from file
	list_of_words = data_handler.get_words_from_input(text)
	pos_prediction = make_class_prediction(list_of_words, pos_words_dict, prob_positive, positive_review_count)
	neg_prediction = make_class_prediction(list_of_words, neg_words_dict, prob_negative, negative_review_count)
	predicted_result = decide_outcome(pos_prediction, neg_prediction)
	if predicted_result == 1:
		predicted_result = "positive"
	else:
		predicted_result = "negative"

	results = []
	results.append("It is predicted to be: " + predicted_result)
	results.append("pos_prediction: " + (str(pos_prediction)))
	results.append("neg_prediction: " + (str(neg_prediction)))
	return results

def make_class_prediction(text, review_wordcount_dict, review_probability, number_of_reviews, use_stop_words = False):
	"""
	This will calculate the positive or negative review's value by looking at the frequency of the words in the training
	data
	:param text: the review as a list
	:param review_wordcount_dict: the frequency of the words as a dictionary where the keys are the words
	:param review_probability: the prior probability
	:param number_of_reviews: the total number of positive or negative reviews
	:return: the predicted value
	"""
	prediction = 1
	text_WC_dict = data_handler.count_text(text)
	divide_with_this = (sum(review_wordcount_dict.values()) + number_of_reviews)
	# to improve performance, this calculation is done once here and called divide_with_this
	# summing up all the values for every word makes the prediction very slow.
	if not use_stop_words:
		for word in text_WC_dict:
			prediction += math.log(
				text_WC_dict.get(word, 0) * ((review_wordcount_dict.get(word, 0) + 1) / divide_with_this))
	else:

		for word in text_WC_dict:
			if word not in stop_words:
				prediction += math.log(
					text_WC_dict.get(word, 0) * ((review_wordcount_dict.get(word, 0) + 1) / divide_with_this))
	return prediction * review_probability

def classify(text, use_stop_words = False):
	# P(xi|H) the likelyhood or the probability of predictor (a word) given hypothesis (positive review), adding 1 to
	# smooth the value so we dont multiply the prediction by 0 if the word didn't exist in the training data
	negative_prediction = make_class_prediction(text, neg_words_dict, prob_negative, negative_review_count,
	                                            use_stop_words = use_stop_words)
	positive_prediction = make_class_prediction(text, pos_words_dict, prob_positive, positive_review_count,
	                                            use_stop_words = use_stop_words)

	return decide_outcome(positive_prediction, negative_prediction)

def decide_outcome(positive_prediction, negative_prediction):
	"""
	Make the final decision if it is positive or negative
	:param positive_prediction:
	:param negative_prediction:
	:return: -1 if negative 1 if positive
	"""
	if negative_prediction > positive_prediction:
		return -1
	else:
		return 1

def train():
	"""
	This will attempt to load the classifier and declare the variables,
	if it is unable to do so it will load the data and save it as classifier.trained
	:return:
	"""
	global pos_words_dict, neg_words_dict, positive_review_count, negative_review_count, prob_positive, prob_negative, test_pos_reviews, test_neg_reviews
	start_time = time.time()
	if pos_words_dict == None:  # check if a variable is missing, if it is they all are and has to be added.

		try:
			data = data_handler.load_object("classifier.trained")

			pos_words_dict = data["pos_words_dict"]
			neg_words_dict = data["neg_words_dict"]
			positive_review_count = data["positive_review_count"]
			negative_review_count = data["negative_review_count"]
			prob_positive = data["prob_positive"]
			prob_negative = data["prob_negative"]
		except Exception as e:
			print(
				"Couldn't load test data from the classifier.trained file. Processing the training data now, this may take a while...")
			data = process_training_data()
			data_handler.save_object(data, "classifier.trained")

	else:
		pass
	final_time = time.time() - start_time
	print("It took: "f'{final_time:.2f}'" seconds to load the classifier\n")  # TODO REMOVE BEFORE SUBMITTING??
	return {
		"pos_words_dict":pos_words_dict,
		"neg_words_dict":neg_words_dict,
		"positive_review_count":positive_review_count,
		"negative_review_count":negative_review_count,
		"prob_positive":prob_positive,
		"prob_negative":prob_negative}

def process_training_data():
	"""
	This will process the training data and prepare it for the classifier
	:return: pos_words_dict, neg_words_dict, positive_review_count,
			negative_review_count, prob_positive, prob_negative
	"""
	global pos_words_dict, neg_words_dict, positive_review_count, negative_review_count, prob_positive, prob_negative
	training_data = data_handler.get_training_words()  # This will have training data ready, prepared for counting etc.

	pos_words_dict = data_handler.count_text(training_data[0])
	neg_words_dict = data_handler.count_text(training_data[1])
	positive_review_count = training_data[2]
	negative_review_count = training_data[3]
	prob_positive = positive_review_count / (positive_review_count + negative_review_count)
	prob_negative = negative_review_count / (positive_review_count + negative_review_count)
	print("P(y) or the prior positive probability is: ", prob_positive)

	return {
		"pos_words_dict":pos_words_dict,
		"neg_words_dict":neg_words_dict,
		"positive_review_count":positive_review_count,
		"negative_review_count":negative_review_count,
		"prob_positive":prob_positive,
		"prob_negative":prob_negative}

def load_test_dataset():
	"""
	This will load the test dataset from test.data if possible, else it will process it and create that file.
	:return:
	"""
	#test reviews
	global test_pos_reviews, test_neg_reviews
	if test_pos_reviews == None or test_neg_reviews == None:

		try:
			test_data = data_handler.load_object("test.dataset")
			test_pos_reviews = test_data["pos_reviews"]
			test_neg_reviews = test_data["neg_reviews"]

		except Exception:
			print(
				"Couldn't load test data from the test.dataset file. Processing the test data now, this may take a while...")
			test_data = data_handler.get_test_data()
			test_pos_reviews = test_data["pos_reviews"]
			test_neg_reviews = test_data["neg_reviews"]
			data_handler.save_object(test_data, "test.dataset")

	return test_pos_reviews, test_neg_reviews

def predict_test_reviews(use_stop_words = False):
	"""
	Predicts all the test reviews
	:return: a dict with the results, keys are:
	predicted_positive
	predicted_negative
	correct_predictions
	incorrect_predictions
	"""
	load_test_dataset()
	predicted_positive = 0
	predicted_negative = 0
	correct_predictions = 0
	incorrect_predictions = 0
	counter = 0
	pos_test_reviews = []
	neg_test_reviews = []
	for review in test_pos_reviews:
		pos_test_reviews.append(data_handler.count_text(test_pos_reviews[review]))

	for review in test_neg_reviews:
		neg_test_reviews.append(data_handler.count_text(test_neg_reviews[review]))

	while pos_test_reviews.__len__() > counter:
		current_review = pos_test_reviews[counter]
		classificiation = classify(current_review, use_stop_words = use_stop_words)
		if classificiation == -1:
			predicted_negative += 1
			incorrect_predictions += 1
		else:
			predicted_positive += 1
			correct_predictions += 1
		counter += 1
	counter = 0

	while neg_test_reviews.__len__() > counter:
		current_review = neg_test_reviews[counter]
		classificiation = classify(current_review, use_stop_words = use_stop_words)
		if classificiation == -1:
			predicted_negative += 1
			correct_predictions += 1
		else:
			predicted_positive += 1
			incorrect_predictions += 1
		counter += 1

	results = {}
	results["predicted_positive"] = predicted_positive
	results["predicted_negative"] = predicted_negative
	results["correct_predictions"] = correct_predictions
	results["incorrect_predictions"] = incorrect_predictions
	return results


from collections import defaultdict
from aocd.models import Puzzle

class Intcomp():
    def __init__(self, code: list):
        self.code = None
        self.orig_code = None
        self.load(code)
        self.ip = 0
        self.rel_base = 0
        self.num_of_operands = [0, 3, 3, 1, 1, 2, 2, 3, 3, 1]
        self.out = []

    def load(self, code: list):
        self.orig_code = code
        self.code = defaultdict(int, enumerate(code))

    def set(self, code: list = None, pointer: int = None, base: int = None):
        if code is not None:
            self.load(code)
        self.ip = pointer if pointer is not None else self.ip
        self.rel_base = base if base is not None else self.rel_base

    def reset(self):
        self.set(code=self.orig_code, pointer=0, base=0)
        self.out = []

    def run(self, inp: tuple = None, halt_after=-1):
        self.out = []
        if inp is None:
            inp = (0,)
        inp_i = 0
        while self.code[self.ip] != 99:
            if 0 < halt_after <= len(self.out):
                return self.out
            modes = [int(x) for x in f"{self.code[self.ip]:0>5}"[:3]][::-1]
            instruction = int(f"{self.code[self.ip]:0>5}"[3:])
            operands = [0 for _ in range(self.num_of_operands[instruction])]
            adjust = [0 for _ in range(self.num_of_operands[instruction])]
            for m in range(self.num_of_operands[instruction]):
                if modes[m] == 0:
                    operands[m] = self.code[self.code[self.ip + m + 1]]
                elif modes[m] == 1:
                    operands[m] = self.code[self.ip + m + 1]
                elif modes[m] == 2:
                    adjust[m] = self.rel_base
                    operands[m] = self.code[self.rel_base + self.code[self.ip + m + 1]]

            if instruction == 1:  # ADD
                self.code[adjust[2] + self.code[self.ip + 3]] = operands[0] + operands[1]
            elif instruction == 2:  # MULTIPLY
                self.code[adjust[2] + self.code[self.ip + 3]] = operands[0] * operands[1]
            elif instruction == 3:  # INPUT
                self.code[adjust[0] + self.code[self.ip + 1]] = inp[inp_i]
                inp_i = (inp_i + 1) % len(inp)
            elif instruction == 4:  # OUTPUT
                self.out.append(operands[0])
            elif instruction == 5:  # JUMP IF TRUE
                self.ip = (operands[1] - 3) if operands[0] != 0 else self.ip
            elif instruction == 6:  # JUMP IF FALSE
                self.ip = (operands[1] - 3) if operands[0] == 0 else self.ip
            elif instruction == 7:  # TRUE IS LESS THAN
                self.code[adjust[2] + self.code[self.ip + 3]] = int(operands[0] < operands[1])
            elif instruction == 8:  # TRUE IF EQUAL
                self.code[adjust[2] + self.code[self.ip + 3]] = int(operands[0] == operands[1])
            elif instruction == 9:  # INCREASE RELATIVE BASE
                self.rel_base += operands[0]
            self.ip += self.num_of_operands[instruction] + 1

if __name__ == "__main__":
    puzzle = Puzzle(year=2019, day=9)
    in9 = [int(x) for x in puzzle.input_data.split(',')]

    prog = Intcomp(code=in9)
    prog.run(1)
    print(prog.out, prog.out[-1])
    # puzzle.answer_a = proga.out[-1]

    prog.reset()
    prog.run(2)
    print(prog.out, prog.out[-1])
    # puzzle.answer_b = prog.out[-1]


import tkinter as tk
from tkinter import messagebox

vAT = ["salutat", "ambulat", "vocat", "rogat", "postulat"]
vET = ["sedet", "habet", "videt", "tinet", "retinet"]
vIT = ["scribit", "surgit", "ducit", "emit", "inquit"]
vIR = ["est", "revenit", "aperit", "offert"]

n1 = ["via","familia", "lingua", "puella", "stella", "terram", "turba"]
n2 = ["coquus", "servus", "dominus", "libertus", "vinum", "portum", "argentarius" ]
n3 = ["mater", "pater", "clamor", "hospes", "dies", "mortem", "omnes"]

def verbconjugate(*args):
	v1 = verb.get()[0:-1] + "o"
	v2 = verb.get()[0:-1] + "s"
	v3 = verb.get()[0:-1] + "t"
	output1.config()
	output1.insert(tk.END, v1)
	output2.insert(tk.END, v2)
	output3.insert(tk.END, v3)
	print("")
	# if len(verb.get())-2 == "e" and verb not in vIR:
	# 	v1 = verb.get()[0:-2] + "o"
	# 	v2 = verb.get()[0:-2] + "s"
	# 	output1.config(text=verb)
	# 	output1.insert(v1)
	# 	output2.insert(v2)
	# 	print("")

#use for loop to replace end with verb using list,
#where you insert list[1] in place of ending

root = tk.Tk()
root.geometry("600x300")
root.Title = "Latin Word Conjugator"

verb = tk.Entry()
verb.config()
verb.grid(row = 0, column = 2)

conjugate = tk.Button()
conjugate.config(height = 4, width = 10, command = verbconjugate, text = "Conjugate")
conjugate.grid(row = 1, column = 2)

output1 = tk.Text()
output1.config(height = 2, width = 7)
output1.grid(row = 0)

output2 = tk.Text()
output2.config(height = 2, width = 7)
output2.grid(row = 1)

output3 = tk.Text()
output3.config(height = 2, width = 7)
output3.grid(row = 2)

output4 = tk.Text()
output4.config(height = 2, width = 7)
output4.grid(row = 3)

root.mainloop()
print("DONE")


'''
  SSA code that simulates a birth-death process.
'''

import numpy as np
from numpy.random import randn, rand

def SSA(xinit, nsteps, a=10.0, mu=1.0):
    '''
        Using SSA to exactly simulate the death/birth process starting
        from xinit and for nsteps or up to time T.

        a and mu are parameters of the propensities.

        Returns
        =======
        path : array-like, the path generated.
        tpath: stochastic time steps
    '''

    path = np.zeros(nsteps)
    tpath= np.zeros(nsteps)

    path[0] = xinit # initial population

    u = rand(2,nsteps) # pre-pick all the uniform variates we need

    for i in xrange(1,nsteps):

        # The propensities will be normalized
        tot_prop = path[i-1]*mu+a
        prob = path[i-1]*mu/tot_prop # probability of death

        if(u[0,i]<prob):
            # Death
            path[i] = path[i-1]-1
        else:
            # Birth
            path[i] = path[i-1]+1

        # Time stayed at current state
        tpath[i] = -np.log(u[1,i])*1/tot_prop

    tpath = np.cumsum(tpath)
    return path, tpath


import math
elements = input().split()
command = input()

count = 0
match_element = ''

while not command == "end":
    indexes = [int(index) for index in command.split()]
    count += 1
    flag = True
    if indexes[0] == indexes[1] or indexes[0] not in range(0, len(elements)) or indexes[1] not in range(0, len(elements)):
        elements.insert(math.ceil(len(elements) / 2), f"-{count}a")
        elements.insert(math.ceil(len(elements) / 2), f"-{count}a")
        print("Invalid input! Adding additional elements to the board")
    else:
        if elements[indexes[0]] == elements[indexes[1]]:
            match_element = elements[indexes[0]]
            elements.remove(match_element)
            elements.remove(match_element)
            print(f"Congrats! You have found matching elements - {match_element}!")
            if len(elements) == 0:
                print(f"You have won in {count} turns!")
                break
        else:
            print("Try again!")

    command = input()

if command == 'end':
    print("Sorry you lose :(")
    print(' '.join(elements))


import json
from flask import Blueprint, render_template, redirect, request
from ym_backend import controller

product = Blueprint('product',__name__)
ProductCtrl = controller.Product(__name__)

@product.route('/')
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def index():
  return render_template('./index.html')

# 注册
@product.route('/addProduct', methods=['POST'])
def addProduct():
  name = request.form['name']
  pic = request.form['pic']
  params = {
    'name': name,
    'pic': pic
  }
  return ProductCtrl.addProduct(params)

# 查询用户信息
@product.route('/queryProduct', methods=['POST', 'GET'])
def queryProduct():
  if request.method == 'POST':
    name = request.form['name']
    params = {
      'name': name
    }
    return ProductCtrl.queryProduct(params)
  elif request.method == 'GET':
    return ProductCtrl.queryProductAll()

# 修改用户信息
@product.route('/modifyProduct', methods=['POST'])
def modifyProduct():
  name = request.form['name']
  pic = request.form['pic']
  params = {
    'name': name,
    'pic': pic
  }
  return ProductCtrl.modifyProduct(params)

# 删除用户信息
@product.route('/delProduct', methods=['POST'])
def delProduct():
  id = request.form['id']
  return ProductCtrl.delProduct({'id': id})


import numpy as np
from scipy.sparse import vstack
from sklearn.preprocessing import OneHotEncoder

FOLDS_COUNT = 5

def read_dataset(n=None):
    xs = []
    ys = []
    i = 0
    with open("dataset.txt") as dataset_file:
        for line in dataset_file:
            if ':' in line:
                current_movie = line.split(":")[0]
            else:
                user_id, raw_mark, _ = line.split(",")
                xs.append((user_id, current_movie))
                ys.append(int(raw_mark))
            if n is not None:
                i += 1
                if i >= n:
                    break
    encoder = OneHotEncoder(categories='auto', sparse=True)
    encoder.fit(xs)
    return encoder.transform(xs), np.array(ys).reshape(-1, 1), encoder

def split_data_to_fold(xs, ys, fold_index):
    fold_size = xs.shape[0] // FOLDS_COUNT

    train_xs = vstack([xs[:fold_index * fold_size, :], xs[fold_size * (fold_index + 1):, :]])
    train_ys = np.vstack([ys[:fold_index * fold_size, :], ys[fold_size * (fold_index + 1):, :]])

    test_xs = xs[fold_index * fold_size:(fold_index + 1) * fold_size]
    test_ys = ys[fold_index * fold_size:(fold_index + 1) * fold_size]

    return train_xs, train_ys, test_xs, test_ys
    # return xs[:fold_size * 4], ys[:fold_size * 4], xs[fold_size * 4:], ys[fold_size * 4:]

def kek(xs, ys, fold_index):
    fold_size = xs.shape[0] // FOLDS_COUNT
    return xs[:, fold_size * 4], ys[:, fold_size * 4], xs[:, fold_size * 4:], ys[:, fold_size * 4:]


import glob
from setuptools import setup, find_packages
from setuptools.extension import Extension
from setupext import casacorebuild_ext

PKGNAME = "pyrap.quanta"
EXTNAME = "_quanta"
casalibs = [] # casa_casa is added by default

casaextension = Extension(name="%s.%s" % (PKGNAME, EXTNAME),
                          sources = glob.glob('src/*.cc'),
                          depends = glob.glob('src/*.h'),
                          libraries = casalibs)
setup(name = PKGNAME,
      version = 'trunk',
      description = 'Python bindings to casacore Quanta',
      author = 'Malte Marquarding',
      author_email = 'Malte.Marquarding@csiro.au',
      url = 'http://code.google.com/p/pyrap',
      keywords = ['quanta','casacore', 'unit conversion'],
      long_description = '''
This is a python module to do unit conversion using the casacore Quanta c++ library.
''',
      packages = find_packages(),
      namespace_packages = ["pyrap"],
      license = 'GPL',
      zip_safe = 0,
      ext_modules =[ casaextension ],
      cmdclass={'build_ext': casacorebuild_ext})


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

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('primo', '0017_bodypart_expand_in_tree'),
    ]

    operations = [
        migrations.AddField(
            model_name='taxon',
            name='tree_root',
            field=models.BooleanField(default=False),
        ),
    ]