import os
import random
import shutil

from django.views.decorators.csrf import csrf_exempt
from django.http import (
    HttpResponse,
    HttpResponseForbidden,
    HttpResponseServerError
)
from django.conf import settings

from license_protected_downloads.models import APIKeyStore, APILog
from license_protected_downloads.common import safe_path_join

def upload_target_path(path, key, public):
    """Quick path handling function.

    Checks that the generated path doesn't end up outside the target directory,
    so you can't set path to start with "/" and upload to anywhere.
    """
    if public:
        base_path = os.path.join(settings.SERVED_PATHS[0])
    else:
        base_path = os.path.join(settings.UPLOAD_PATH, key)
    return safe_path_join(base_path, path)

@csrf_exempt
def file_server_post(request, path):
    """ Handle post requests.

    All post requests must be accompanied by a valid key. If not, the upload
    will be ignored.

    Files are stored in a private directory that can not be accessed via the
    web interface unless you have the same key.
    """
    if not ("key" in request.POST and
            APIKeyStore.objects.filter(key=request.POST["key"])):
        APILog.mark(request, 'INVALID_KEY')
        return HttpResponseServerError("Invalid key")

    api_key = APIKeyStore.objects.get(key=request.POST["key"])

    if 'file' not in request.FILES or not path:
        APILog.mark(request, 'INVALID_ARGUMENTS', api_key)
        return HttpResponseServerError("Invalid call")

    APILog.mark(request, 'FILE_UPLOAD', api_key)

    path = upload_target_path(
        path, request.POST["key"], public=api_key.public)

    # Create directory if required
    dirname = os.path.dirname(path)
    if not os.path.isdir(dirname):
        os.makedirs(dirname)

    with open(path, "wb") as destination:
        for chunk in request.FILES["file"].chunks():
            destination.write(chunk)

    return HttpResponse("OK")

def api_request_key(request):
    APILog.mark(request, 'REQUEST_KEY')
    if("key" in request.GET and
       request.GET["key"] == settings.MASTER_API_KEY and
       settings.MASTER_API_KEY):

        # Generate a new, random key.
        key = "%030x" % random.randrange(256 ** 15)
        while APIKeyStore.objects.filter(key=key):
            key = "%030x" % random.randrange(256 ** 15)

        # Look for a hint of sanity in the value given to public, but don't
        # care about it too much.
        yes = ["", "y", "yes", "true", "1"]
        if "public" in request.GET:
            public = request.GET["public"].lower() in yes
        else:
            public = False

        api_key = APIKeyStore(key=key, public=public)
        api_key.save()
        return HttpResponse(key)

    return HttpResponseForbidden()

def api_delete_key(request):
    APILog.mark(request, 'DELETE_KEY')
    if "key" not in request.GET:
        return HttpResponseServerError("Invalid key")

    key = request.GET["key"]
    api_key = APIKeyStore.objects.filter(key=key)

    if not api_key:
        return HttpResponseServerError("Invalid key")

    # Delete key from database and all files associated with it
    api_key.delete()
    shutil.rmtree(os.path.join(settings.UPLOAD_PATH, key))

    return HttpResponse("OK")

def api_push_to_server(request):
    # TODO: Upload files from this machine to another linaro-licence-protection
    # node.
    """
    Something like:

    if request.GET["target"] in settings.REMOTE_SERVERS:
        remote_server = settings.REMOTE_SERVERS[request.GET["target"]]

        remote_server should contain:
        {
            "key": "...",
            "url": "...",
        }

        now just POST files from this machine to the specified URL/KEY.

        Possibly add some magic to POST endpoint (file_server_post) to allow
        (some users??) uploads to a public path:

        POST snapshots.linaro.org/path/to/file?key="key"&public=true

    """
    pass


from typing import List
from collections import defaultdict, deque

# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def verticalOrder(self, root: TreeNode) -> List[List[int]]:
        nodes = defaultdict(list)
        q = deque([(root, 0)])
        while len(q) > 0:
            node, col = q.popleft()
            if not node:
                continue
            nodes[col].append(node.val)
            q.append((node.left, col - 1))
            q.append((node.right, col + 1))

        return [nodes[x] for x in sorted(nodes.keys())]

Python/Binary Tree Vertical Order Traversal.py


import logging
import subprocess as sp

import numpy as np

from t2v.config.root import RootConfig

class VideoInput:
    def __init__(self, cfg: RootConfig, video_path: str):
        """
        VideoInput reads frames from a given video file which can be used as init images for individual frame generation
        """
        command = ["ffmpeg",
                   '-i', video_path,
                   '-f', 'image2pipe',
                   '-pix_fmt', 'rgb24',
                   '-vcodec', 'rawvideo', '-']
        self.pipe = sp.Popen(command, stdout=sp.PIPE, bufsize=10 ** 8)
        w, h = get_vid_dims(video_path)
        if w != cfg.width or h != cfg.height:
            raise RuntimeError(f"Actual width {w} and height {h} of the video file {video_path} are not matching "
                               f"the scenario width {cfg.width} and height {cfg.height}. Crop the video or make sure the settings are correct.")
        self.fps = get_vid_fps(video_path)
        self.w = w
        self.h = h
        self.video_path = video_path

    def next_frame(self):
        raw_image = self.pipe.stdout.read(self.w * self.h * 3)
        # transform the byte read into a numpy array
        image = np.frombuffer(raw_image, dtype='uint8')
        image = image.reshape((self.h, self.w, 3))
        # throw away the data in the pipe's buffer.
        self.pipe.stdout.flush()
        return image

    def skip_frame(self, n=1):
        logging.info(f"Skipping {n} frames of video file {self.video_path}")
        for i in range(0, n):
            # scroll the stdin buffer
            self.pipe.stdout.read(self.w * self.h * 3)
            # throw away the data in the pipe's buffer.
            self.pipe.stdout.flush()

def get_vid_dims(video_path):
    """
    Returns width,height as tuple for a given video file.
    Requires ffprobe in PATH

    :param video_path: path to video file
    :return: w,h tuple
    """
    line = ffprobe_params(video_path, "width,height")
    w, h = line.split("x")
    return int(w), int(h)

def ffprobe_params(video_path, param):
    command = ["ffprobe",
               '-v', 'error',
               '-select_streams', 'v',
               '-show_entries', f'stream={param}',
               '-of', 'csv=p=0:s=x',
               video_path]
    pipe = sp.Popen(command, stdout=sp.PIPE, bufsize=10 ** 8)
    line = pipe.stdout.readline(1024).decode("utf-8")
    return line

def get_vid_fps(video_path):
    """
    Returns frame rate of a given video file.
    Requires ffprobe in PATH

    :param video_path: path to video file
    :return: fps number
    """
    line = ffprobe_params(video_path, "r_frame_rate")
    num, den = line.split("/")
    return int(num)/int(den)


import numpy as np
from utils.text_analyzer import TextStats
from utils.text_analyzer import compute_score
from utils.dictionary import Dictionary
from itertools import permutations as permutations

def crack_transposition(stats: TextStats, dictionary: Dictionary, verbose: bool=False):
    """
    Method to crack table transpositions. It assumes that ciphertext was obtained by writing plaintext to rows of given
    size and then read by columns. If there are empty spaces in the table, they are filled by fake letters. This cracker
    identifies the correct plaintext by counting identical letters at it's end, and by comparing it to the dictionary.
    All possible table sizes are evaluated, as it doesn't take much time and size detection function that was used to
    get one table size can be confused if the filler uses random characters instead of all Xs.
    :param stats: TextStats object of the text to be cracked
    :param dictionary: Dictionary constructed from the language of the plaintext
    :param verbose: True to print some outputs, eg other candidates for plaintext
    :return: plaintext, cipher name ("transposition_table"), parameters (table height)
    """
    solutions = []

    # this doesn't work all that well, and it is found later on anyway
    #    likely_size = guess_table_size(stats)
    # if likely_size:
    #    solution = read_from_table(stats, likely_size)
    #    language_score = compute_score(solution, dictionary)
    #    solutions.append((language_score, solution, "guessed"+str(likely_size)))

    # try all possible table dimensions
    for size in range(2, stats.N):
        # table size must be divisible by the dimensions
        if not stats.N % size:
            solution = read_from_table(stats, size)
            # how many characters repeat at the end of the plaintext
            tail_score = count_tailing_letters(solution)
            language_score = compute_score(solution, dictionary)
            solutions.append((tail_score, language_score, solution, "transposition_table", [size]))

    # sort by language, but use tail_score for primary sorting
    solutions.sort(key=lambda s: s[1], reverse=True)
    solutions.sort(key=lambda s: s[0], reverse=True)

    if verbose:
        for i in range(0, len(solutions)):
            print(solutions[i][0], solutions[i][3], solutions[i][2])

    return solutions[0][2], solutions[0][3], solutions[0][4]

def read_from_table(stats: TextStats, table_width: int):
    # take the string from stats, feed it to an array and then read it along different axis to get plaintext
    m = int(stats.N/table_width)
    f = np.array(list(stats.text))
    cipher_table = np.transpose(np.reshape(f, (m, table_width)))
    table = np.reshape(cipher_table, (1, stats.N)).tolist()
    return "".join(table[0])

def crack_transposition_with_column_scrambling(stats: TextStats, dictionary: Dictionary, verbose: bool=False):
    """
    Method to crack table transpositions with column shuffling. The cipher is the same as table transposition above, but
    the columns are shuffled before message is read. The method will try all permutations of columns for tables with 7
    or less columns. Larger tables are skipped, because it would take ages to compute.
    :param stats: TextStats object of the text to be cracked
    :param dictionary: Dictionary constructed from the language of the plaintext
    :param verbose: True to print some outputs, eg other candidates for plaintext
    :return: plaintext, cipher name ("transposition_table_shuffled"), parameters: [table height, permutation]
    """
    solutions = []
    for size in range(2, stats.N):
        if not stats.N % size:
            n_cols = int(stats.N / size)
            if n_cols > 7:
                if verbose:
                    print("Can't guess with table size", size, "- too many column permutations, skipping")
                continue
            from math import factorial
            if verbose:
                print(n_cols, factorial(n_cols))

            # prepare a table to be shuffled
            cipher_table = generate_table(stats, size)
            # create permutations of [0, 1, ..., n_cols]
            for permutation in permutations(list(range(0, n_cols))):
                # apply permutation and read message
                shuffled_table = cipher_table[:, permutation]
                table = np.reshape(shuffled_table, (1, stats.N)).tolist()
                solution = "".join(table[0])
                language_score = compute_score(solution, dictionary)
                solutions.append((language_score, solution, size, permutation))

    solutions.sort(key=lambda s: s[0], reverse=True)
    if verbose:
        for i in range(0, min(len(solutions), 50)):
            print(solutions[i][0], solutions[i][1], solutions[i][2], solutions[i][3])

    return solutions[0][1], "transposition_table_shuffled", [solutions[0][2], solutions[0][3]]

def generate_table(stats: TextStats, table_width: int):
    m = int(stats.N/table_width)
    f = np.array(list(stats.text))
    cipher_table = np.transpose(np.reshape(f, (m, table_width)))
    return cipher_table

def guess_table_size(text: TextStats, filler: int="X"):
    """
    Finds how often filler characters appear. This can be used to detect table size, but turned out to be useless.
    :param text: ciphertext
    :param filler: character to work with
    :return: Likely table size
    """
    last_filler_pos = 0
    distances = {}
    best_size = -1
    best_size_value = -1
    for i in range(0, text.N):
        c = text.text[i]
        if c == filler:
            distance = i - last_filler_pos
            if distance not in distances:
                distances[distance] = 1
            else:
                distances[distance] += 1
            last_filler_pos = i
            if distances[distance] > best_size_value:
                best_size = distance
                best_size_value = distances[distance]

    if text.N % best_size:
        return False
    return best_size

def count_tailing_letters(text: str):
    if len(text) == 0:
        return 0
    last_char = text[-1]
    i = 1
    for i in range(1, len(text)):
        if text[-i] != last_char:
            break
    return i-1


"""
Finone API Controller

Processes XML response from Mortech, parses, and stores into database.
Returns response object for user consumption as JSON.
"""
import requests
import xmltodict

from requests import ConnectionError, HTTPError, RequestException, Timeout

from finone import db, app
from finone.constants import (
    AUTH_EMAIL, AUTH_ID, AUTH_LICENSEKEY, AUTH_NAME,
    PROPERTY_TYPE_MAP, LOAN_PURPOSE_MAP,
    PRODUCT_FIXED, PRODUCT_ARM,
    PRODUCT_5_ARM, PRODUCT_7_ARM, PRODUCT_15_FIXED, PRODUCT_30_FIXED,
    DEFAULT_TARGET_PRICE, DEFAULT_LOCKIN,
    ServiceConstants as sc)
from finone.factories import lender_factory, request_factory
from finone.utils import underscoreize
from sqlalchemy.exc import ProgrammingError

class ApiRequest(object):
    """
    Creates and formats a request to the service.
    """

    valid_args = ['state', 'county', 'loan_amount', 'zipcode',
                  'appraised_value', 'property_type', 'loan_purpose']

    def __init__(self, params):
        """
        Builds service request object.
        :param params: data from the submitted form
        """
        self.session = self.create_session()
        params = underscoreize(params)
        self.status_code = ''
        # TODO: Figure out what options would possibly be and where to use them
        self.options = {}
        for key, value in params.items():
            if key in self.valid_args:
                setattr(self, key, value)
            else:
                self.options.update({key: value})

    def _get_property_type(self):
        return PROPERTY_TYPE_MAP.get(self.options.get('property_type'))

    def _get_loan_purpose(self):
        return LOAN_PURPOSE_MAP.get(self.options.get('loan_purpose'))

    def build_request(self):
        return {
            sc.LICENSEKEY: AUTH_LICENSEKEY,
            sc.THIRD_PARTY_NAME: AUTH_NAME,
            sc.CUSTOMER_ID: AUTH_ID,
            sc.EMAIL_ADDRESS: AUTH_EMAIL,
            sc.REQUEST_ID: 1,
            sc.PROPERTY_STATE: self.state,
            sc.PROPERTY_COUNTY: self.county,
            sc.LOAN_AMOUNT: self.loan_amount,
            sc.PROPERTY_TYPE: self._get_property_type(),
            sc.LOAN_PURPOSE: self._get_loan_purpose(),
            sc.LOAN_PRODUCT1: PRODUCT_30_FIXED,
            sc.LOAN_PRODUCT2: PRODUCT_15_FIXED,
            sc.LOAN_PRODUCT3: PRODUCT_5_ARM,
            sc.LOAN_PRODUCT4: PRODUCT_7_ARM,
            sc.APPRAISED_VALUE: self.appraised_value,
            sc.TARGET_PRICE: DEFAULT_TARGET_PRICE,
            sc.LOCKIN_DAYS: DEFAULT_LOCKIN
        }

    @staticmethod
    def create_session():
        """Return an HTTP session."""
        session = requests.Session()
        return session

    def send_request(self):
        """Sends a request to mortech with data."""
        app.logger.info("Sending request...")
        # session = self.create_session()
        try:
            data = self.build_request()
            res = self.session.get(app.config['MORTECH_ENDPOINT'], params=data)
        except (HTTPError, ConnectionError, Timeout, RequestException, Exception) as exc:
            app.logger.exception("RQS-REQUEST-EXCEPTION: %s", exc)
            self.status_code = '500'
            raise
        else:
            app.logger.info("Request successful: {0}".format(res.status_code))
            self.status_code = res.status_code
            parser = ApiResponse(res.content, request_factory())
            return parser.get_results()

class ApiResponse(object):
    """
    Manages API responses from Mortech by:

    * Parsing response
    * Storing XML response
    * Storing parsed lenders to database

    Accepts and parses XML responses from the API.

    :param response: XML response from Mortech API
    :param request: An instance of :class:`Request`
    """

    def __init__(self, response, request):
        self.xml = response
        self.data = self.get_data()
        self.request = request

    def is_valid(self):
        return "results" in self.data['mortech']

    def parse(self, data):
        """
        Parses XML to dict. Returns process logs during process.
        To hide these dense logs, move the parsing function to a separate
        method.
        """
        if app.debug:
            self.save_xml()
        return xmltodict.parse(data)

    def get_data(self):
        """Parses elements of an XML document and returns a list of quotes."""
        return self.parse(self.xml)

    def result_count(self):
        count = 0
        for result in self.data['mortech']['results']:
            count += int(result['@size'])
        service.logger.info("Result count: %s", count)
        return count

    @staticmethod
    def _get_fees(fee_list):
        """Accepts list and returns dictionary of fees."""
        fees = {}
        for fee in fee_list:
            fees.update({
                fee.get('@description'): fee.get('@feeamount')
            })
        return fees

    @staticmethod
    def _get_amortization(obj):
        """Evaluates presence of intial arm tem to determine amortization."""
        if obj:
            return PRODUCT_ARM
        else:
            return PRODUCT_FIXED

    def _get_lenders(self):
        """Return a list of RateQuote instances."""
        lenders = []
        data = self.parse(self.xml)
        results = data['mortech']['results']
        app.logger.info(data.get('mortech').keys())

        for product in results:
            app.logger.info("==> BEGIN: Processing rate quotes for %s products ...",
                            product.get('@product_name'))
            for lender in product['quote']:
                lenders.append(lender_factory(lender))
            app.logger.info("==> END: Processing for %s", product.get('@product_name'))
        return lenders

    def bulk_store(self):
        # TODO: Move to ModelManager
        lenders = self._get_lenders()
        app.logger.info("==> BEGIN: Saving lenders ...")
        try:
            db.session.add_all(lenders)
            db.session.commit()
        except (ProgrammingError, Exception) as exc:
            app.logger.info("BULK STORE EXCEPTION: {}".format(exc))
            raise exc
        else:
            app.logger.info("==> END: %s Lenders saved successfully!", len(lenders))

    def save_xml(self):
        """Debug. Stores parsed data to tmp/data."""
        with open('response.xml', 'w+') as doc:
            doc.write(self.xml)

    def get_results(self):
        data = self.get_data()
        self.bulk_store()
        app.logger.info("RESULTS DATA: %s", dict(data['mortech']['header']))

        return to_list(data['mortech']['results'])

def to_list(data):
    # Move to Utils
    if not isinstance(data, list):
        return [data]


import sys
import datetime

configFile = open(sys.argv[1], 'r')
symbolList = []

for line in configFile:
    symbol = line.split(';', 1)
    symbol[1] = symbol[1][:-1] #remove trailing \n
    symbol[0] = symbol[0].replace('\\n', '\n'); #remove escaping \\n
    symbol[1] = symbol[1].replace('\\n', '\n'); #remove escaping \\n
    symbolList.append(symbol)

XMONTH = datetime.datetime.now().strftime("%m")
XYEAR = datetime.datetime.now().strftime("%Y")
XNUMBER = 0

for line in sys.stdin:
    for symbol in symbolList:
        line = line.replace(symbol[0], symbol[1])

    line = line.replace("XMONTH", XMONTH)
    line = line.replace("XYEAR", XYEAR)
    if (line.find("XNUMBER") != -1):
        line = line.replace("XNUMBER", str(XNUMBER))
        XNUMBER = XNUMBER + 1

    sys.stdout.write(line)


# 더 효율적인 로직으로 풀어보기
from collections import defaultdict,deque; import copy
n, q = map(int, input().split())
n2 = 2**n
g = [ list(map(int,input().split())) for _ in range(n2)]
qrr = list( map(int, input().split()) )
visit = [ [False]*n2 for _ in range( n2)]

# 2L × 2L --> 회전 90도
dir = [(-1,0),(1,0),(0,-1),(0,1)]
summ = 0
group = 0

def play( sy, sx, sizee ):
    global g
    start_val = g[sy+sizee-1][sx]; end_val = g[sy][sx]
    y = sy; x = sx; nrr = [0]*sizee; srr = []; srr2 = []
    nrr[0] = g[sy+sizee-1][sx+sizee-1]
    if sx + sizee + 1 >= n2: srr2 = g[sy+sizee-1][sx:]; srr = g[sy][ sx : ]
    else:srr2 = g[sy+sizee-1][sx: sx + sizee ]; srr = g[sy][ sx : sx+sizee]
    for i in range(4):
        if i == 2 : y = sy+sizee-1; x = sx
        if i%2 == 0:# 시작이 가로인 경우
            ssy = sy+sizee-1; ssx = sx+sizee-1
            if i>0 : ssx = sx
            for j in range(sizee):
                nrr[j] = g[ ssy - j][ssx]
                if i == 0: g[ssy - j][ssx] = srr[ sizee-1-j]
                else: g[ssy - j][ssx] = srr2[ sizee-1-j]
        else: #시작이 세로인 경우 1 3
            if i == 3 : nrr[sizee-1] = end_val; nrr[0] = start_val
            ssx = sx; ssy = sy
            if i == 1 : ssy = sy + sizee-1
            for j in range(sizee) : g[ssy][ssx + j] = nrr[j]

def firestorm(sizee):
    for se in range(n2 //sizee):
        y = se * sizee
        for ga in range(n2 // sizee):
            x = ga*sizee
            sy = y; sx = x
            for i in range( sizee // 2): #8 -> 6
                play(sy,sx, sizee -2*i )
                sy += 1;  sx+= 1

for qq in qrr:
    firestorm( 2**qq )

    summ = 0
    before = copy.deepcopy(g)
    for i in range(n2):
        for j in range(n2):
            cnt = 0
            if before[i][j] == 0: continue
            for d in dir:
                ny, nx = i+d[0], j+d[1]
                if ny< 0 or ny>=n2 or nx<0 or nx>=n2 : continue
                if before[ny][nx]>0: cnt += 1
            if cnt < 3 : g[i][j] -= 1
            summ += g[i][j]

def bfs(i,j):
    global dir,visit
    q = deque(); q.append( (i, j) ) ; visit[i][j] = True; cnt = 1
    while q:
        y,x = q.popleft()
        for d in dir:
            ny, nx = y+d[0], x+d[1]
            if ny<0 or ny>=n2 or nx<0 or nx>=n2: continue
            if 0 < g[ny][nx] and visit[ny][nx] == False:
                cnt+=1; visit[ny][nx] = True; q.append((ny,nx))
    return 0 if cnt <= 1 else cnt

for i in range(n2):
    for j in range(n2):
        if visit[i][j] == False and g[i][j] > 0 : group = max(bfs(i, j), group)

print( summ )
print( group )

#남아있는 얼음 중 가장 큰 덩어리가 차지하는 칸의 개수

b20058_마법상어와파이어스톰.py


from typing import Optional

from fastapi import APIRouter, Form

from Deployment.ConsumerServices.GeneralService import DownloadImageFromURLServiceTask, ParseImageFromBase64ServiceTask
from Deployment.ConsumerServices.RecaptchaService import Captcha1RecognizeServiceTask
from Deployment.server_config import IS_MOCK
from Utils.DAG import DAG
from Utils.Exceptions import InputParameterAbsentException, InputParameterAbnormalException
from Utils.InterfaceUtils import base_interface_result_decorator, DictInterfaceResult

router = APIRouter()

@router.post('/captcha1_recognize')
@base_interface_result_decorator()
async def general_ocr(
        image_url: Optional[str] = Form(None),
        image_base64: Optional[str] = Form(None),
):
    dag = DAG()
    # 如果mock数据过长，可以写在其他文件，然后这边引用
    mock_result = {
        'text': '',
    }
    with DictInterfaceResult(mock_result) as to_return_result:
        if IS_MOCK:
            return to_return_result
        if not (image_url or image_base64):
            raise InputParameterAbsentException('至少有一个参数image_url或者image_base64')
        if image_url and image_base64:
            raise InputParameterAbnormalException('只能有一个image_url或者image_base64')
        if image_url:
            download_image_task = DownloadImageFromURLServiceTask(_dag=dag)
            download_image_task.add_dependency_from_value('_image_url', image_url)
            image_info = (await download_image_task).service_result['image_info']
        elif image_base64:
            decode_image_task = ParseImageFromBase64ServiceTask(_dag=dag)
            decode_image_task.add_dependency_from_value('_base64_string', image_base64)
            image_info = (await decode_image_task).service_result['image_info']
        captcha1_recognize_task = Captcha1RecognizeServiceTask(_dag=dag)
        captcha1_recognize_task.add_dependency_from_value('_image_info', image_info)
        recognize_result = (await captcha1_recognize_task).service_result['text']
        to_return_result.add_sub_result('text', recognize_result)
        return to_return_result


from contextlib import asynccontextmanager

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

def create_app() -> "FastAPI":
    from prisma import Prisma

    from app.api.v1 import api
    from app.core import settings

    @asynccontextmanager
    async def lifespan(_app: FastAPI):
        prisma = Prisma(auto_register=True)
        await prisma.connect()
        yield
        await prisma.disconnect()

    app = FastAPI(
        lifespan=lifespan,
        title=settings.PROJECT_NAME,
        openapi_url=f"{settings.BASE_URL}/openapi.json",
        version=settings.VERSION,
        redoc_url=f"{settings.BASE_URL}/redoc",
        docs_url=f"{settings.BASE_URL}/docs",
    )
    app.add_middleware(
        CORSMiddleware,
        allow_origins=["*"],
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )
    app.include_router(api.router, prefix=settings.API_V1_STR)

    return app


import logging
import numpy

import matplotlib
# Force matplotlib to not use any Xwindows backend.
matplotlib.use('Agg')

from matplotlib import pyplot as plt

def plot_pixels(file_name, candidate_data_single_band,
                reference_data_single_band, limits=None, fit_line=None):

    logging.info('Display: Creating pixel plot - {}'.format(file_name))
    fig = plt.figure()
    plt.hexbin(
        candidate_data_single_band, reference_data_single_band, mincnt=1)
    if not limits:
        min_value = 0
        _, ymax = plt.gca().get_ylim()
        _, xmax = plt.gca().get_xlim()
        max_value = max([ymax, xmax])
        limits = [min_value, max_value]
    plt.plot(limits, limits, 'k-')
    if fit_line:
        start = limits[0] * fit_line.gain + fit_line.offset
        end = limits[1] * fit_line.gain + fit_line.offset
        plt.plot(limits, [start, end], 'g-')
    plt.xlim(limits)
    plt.ylim(limits)
    plt.xlabel('Candidate DNs')
    plt.ylabel('Reference DNs')
    fig.savefig(file_name, bbox_inches='tight')
    plt.close(fig)

def plot_histograms(file_name, candidate_data_multiple_bands,
                    reference_data_multiple_bands=None,
                    # Default is for Blue-Green-Red-NIR:
                    colour_order=['b', 'g', 'r', 'y'],
                    x_limits=None, y_limits=None):
    logging.info('Display: Creating histogram plot - {}'.format(file_name))
    fig = plt.figure()
    plt.hold(True)
    for colour, c_band in zip(colour_order, candidate_data_multiple_bands):
        c_bh, c_bins = numpy.histogram(c_band, bins=256)
        plt.plot(c_bins[:-1], c_bh, color=colour, linestyle='-', linewidth=2)
    if reference_data_multiple_bands:
        for colour, r_band in zip(colour_order, reference_data_multiple_bands):
            r_bh, r_bins = numpy.histogram(r_band, bins=256)
            plt.plot(
                r_bins[:-1], r_bh, color=colour, linestyle='--', linewidth=2)
    plt.xlabel('DN')
    plt.ylabel('Number of pixels')
    if x_limits:
        plt.xlim(x_limits)
    if y_limits:
        plt.ylim(y_limits)
    fig.savefig(file_name, bbox_inches='tight')
    plt.close(fig)


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

import pytest
#set up a browser
@pytest.fixture
def selenium(base_url):
    selenium = webdriver.Chrome('chromedriver')
    selenium.get(base_url)
    selenium.maximize_window()
    selenium.set_page_load_timeout('10')
    return selenium

def test_search(selenium, variables):
    step_1(selenium, variables['input_text'])
    step_2(selenium, variables['input_text'])
    step_3(selenium)

#input term in search field and submit
def step_1(selenium, input_text):
    search_bar = selenium.find_element_by_id('q')
    search_bar.send_keys(input_text)
    search_bar.send_keys(Keys.ENTER)

#verify text is found
def step_2(selenium, input_text):
    element = WebDriverWait(selenium, 10).until(
        EC.presence_of_element_located((By.ID, "col-annot"))
    )
    assert 'Výsledek vyhledávání výrazu ' + input_text in element.text
    assert element.is_displayed() is True

#teardown
def step_3(selenium):
    selenium.quit()


# -*- coding: utf-8 -*-
import config
import telebot
from room import Room
from functools import partial
from random import shuffle

bot = telebot.TeleBot(config.token)
imagi_room = Room

@bot.message_handler(commands=['start'])
def start(message):
    # kb = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
    # kb.add('Новая игровая', 'Выбрать существующую игровую')
    text = '\n'.join(['/1 Новая игровая', '/2 Выбрать существующую игровую'])
    selected = bot.send_message(message.chat.id, text=text)
    bot.register_next_step_handler(selected, get_select)

def get_select(message):
    if message.text == '/1':
        new_room(message)
    elif message.text == '/2':
        choosing_room(message)
    else:
        start(message)

def new_room(message):
    imagi_room.new(chat_id=message.chat.id)
    photo = bot.send_message(message.chat.id, 'Отправьте фото с подписью')
    bot.register_next_step_handler(photo, add_master_photo)

def choosing_room(message):
    try:
        rooms = []
        for room_id in imagi_room.get_room_ids():
            room = imagi_room.get_room_by_id(room_id)
            if room['status'] == 'wait':
                rooms.append(' '.join(['/' + str(room_id), room['master_name']]))
        if not rooms:
            bot.send_message(message.chat.id, text='Нет игровых /start')
        rooms_str = '\n'.join(rooms)
        message = bot.send_message(message.chat.id, text=rooms_str)
        bot.register_next_step_handler(message, room_selected)
    except Exception as e:
        print(e)

def room_selected(message):
    try:
        room_id = int(message.text.strip('/'))
        room = imagi_room.get_room_by_id(room_id)
        photo_mess = bot.send_message(message.chat.id, text='Отправьте фото ассоциирующееся с "{}"'.format(room['img_mess']))
        bot.register_next_step_handler(photo_mess, partial(add_player_photo, room_id))
    except Exception as e:
        raise e

def add_master_photo(message):
    if message.content_type != 'photo':
        bot.send_message(message.chat.id, 'Это не фото!')
        new_room(message)

    elif not message.caption or not message.caption.strip():
        bot.send_message(message.chat.id, 'Подпись та забыли!')
        new_room(message)

    else:
        user_name = ' '.join([message.chat.first_name, message.chat.last_name])
        imagi_room.add_master_photo(message.chat.id,
                                    message.photo[-1].file_id,
                                    message.caption,
                                    user_name
                                    )
        bot.send_message(message.chat.id, text='Ваша игровая создана')

def add_player_photo(room_id, message):
    if message.content_type == 'photo':
        user_name = ' '.join([message.chat.first_name, message.chat.last_name])
        imagi_room.add_player(room_id, message.chat.id, user_name, message.photo[-1].file_id)
        if len(imagi_room.get_room_by_id(room_id)['players']) > 0:
            in_game(room_id, user_name)

    else:
        bot.send_message(message.chat.id, 'Это не фото!')
        message.text = str(room_id)
        room_selected(message)

def in_game(room_id, user_name):
    room = imagi_room.get_room_by_id(room_id)
    message = bot.send_message(room_id,
                               text='В Вашу игровую зашел {}. Всего в игровой {} игроков. Начать игру?/{}'
                               .format(user_name, len(room['players']), room_id))
    stat = 'Игра началась'
    bot.register_next_step_handler(message, partial(run_game, room_id, stat))

def run_game(room_id, stat, message):
    if message.text.strip('/') == str(room_id):
        imagi_room.change_status(room_id, 'in_game')
        room = imagi_room.get_room_by_id(room_id)
        photo_ids = [room['master_photo_id']] + [room['players'][player_id]['photo_id'] for player_id in list(room['players'])]
        shuffle(photo_ids)
        for chat_id in list(room['players']):
            bot.send_message(chat_id, text=stat)
            for i, photo_id in enumerate(photo_ids):
                bot.send_photo(chat_id, photo=photo_id, caption='/{}'.format(i), )
        bot.register_next_step_handler(message, partial(check, room_id, photo_ids))

def check(room_id, photo_ids, message):
    room = imagi_room.get_room_by_id(room_id)
    if message.text.strip('/') in [str(i) for i, _ in enumerate(photo_ids)]:
        photo_id = photo_ids[int(message.text.strip('/'))]
        if room['master_photo_id'] == photo_id:
            bot.send_message(message.chat.id, text='Правильно! Молодец!')
        else:
            bot.send_message(message.chat.id, text='Не угадал')
            bot.send_photo(message.chat.id, photo=room['master_photo_id'], caption='Верная картинка')
    else:
        message.text = str(room_id)
        stat = 'Выберите фото'
        bot.register_next_step_handler(message, partial(run_game, room_id, stat))

if __name__ == '__main__':
    bot.polling(none_stop=True)


import glob
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

def plot_figure(data, x, y, filename, figsize=(24, 9), log_scale=False):
    sns.set(font_scale=1.5)
    plt.style.use("seaborn-poster")

    fig, ax = plt.subplots(figsize=figsize, dpi=300)
    g = sns.barplot(x=x, y=y, hue="Method", data=data, errwidth=2, ax=ax, palette=colors)
    if log_scale:
        g.set_yscale("log")
    plt.legend(loc='upper left', bbox_to_anchor=(1.0, 1.0))
    plt.tight_layout()
    plt.savefig(f"./bandits/figures/{filename}.png")
    plt.show()

def read_data(dataset_name):
    *_, filename = sorted(glob.glob(f"./bandits/results/{dataset_name}_results*.csv"))
    df = pd.read_csv(filename)
    if dataset_name=="mnist":
        linear_df = df[(df["Method"]=="Lin-KF") | (df["Method"]=="Lin")].copy()
        linear_df["Model"] = "MLP2"
        df = df.append(linear_df)
        linear_df["Model"] = "LeNet5"
        df = df.append(linear_df)

    by = ["Rank"] if dataset_name=="tabular" else ["Rank", "AltRank"]

    data_up = df.sort_values(by=by).copy()
    data_down = df.sort_values(by=by).copy()

    data_up["Reward"] = data_up["Reward"] + data_up["Std"]
    data_down["Reward"] = data_down["Reward"] - data_down["Std"]
    data = pd.concat([data_up, data_down])
    return data

def plot_subspace_figure(df, filename=None):
    df = df.reset_index().drop(columns=["index"])
    plt.style.use("seaborn-darkgrid")
    fig, ax = plt.subplots(figsize=(12, 8))
    sns.lineplot(x="Subspace Dim", y="Reward", hue="Method", marker="o", data=df)
    lines, labels = ax.get_legend_handles_labels()
    for line, method in zip(lines, labels):
        data = df[df["Method"]==method]
        color = line.get_c()
        y_lower_bound =  data["Reward"] -  data["Std"]
        y_upper_bound = data["Reward"] + data["Std"]
        ax.fill_between(data["Subspace Dim"],  y_lower_bound, y_upper_bound, color=color, alpha=0.3)

    ax.set_ylabel("Reward", fontsize=16)
    plt.setp(ax.get_xticklabels(), fontsize=16)
    plt.setp(ax.get_yticklabels(), fontsize=16)
    ax.set_xlabel("Subspace Dimension(d)", fontsize=16)
    dataset = df.iloc[0]["Dataset"]
    ax.set_title(f"{dataset.title()} - Subspace Dim vs. Reward", fontsize=18)
    legend = ax.legend(loc="lower right", prop={'size': 16},frameon=1)
    frame = legend.get_frame()
    frame.set_color('white')
    frame.set_alpha(0.6)

    file_path = "./bandits/figures/"
    file_path = file_path + f"{dataset}_sub_reward.png" if filename is None else file_path + f"{filename}.png"
    plt.savefig(file_path)

method_ordering = {"EKF-Sub-SVD": 0,
                   "EKF-Sub-RND": 1,
                   "EKF-Sub-Diag-SVD": 2,
                   "EKF-Sub-Diag-RND": 3,
                   "EKF-Orig-Full": 4,
                   "EKF-Orig-Diag": 5,
                   "NL-Lim": 6,
                   "NL-Unlim": 7,
                   "Lin": 8,
                   "Lin-KF": 9,
                   "Lin-Wide": 9,
                   "Lim2": 10,
                   "NeuralTS": 11}

colors = {k : sns.color_palette("Paired")[v]
          if k!="Lin-KF" else  sns.color_palette("tab20")[8]
          for k,v in method_ordering.items()}

dataset_info = {
    "mnist": {
        "elements": ["EKF-Sub-SVD", "EKF-Sub-RND", "EKF-Sub-Diag-SVD",
                    "EKF-Sub-Diag-RND", "EKF-Orig-Diag", "NL-Lim",
                    "NL-Unlim", "Lin"],
        "x": "Model",
    },

    "tabular": {
        "elements": ["EKF-Sub-SVD", "EKF-Sub-RND", "EKF-Sub-Diag-SVD",
                    "EKF-Sub-Diag-RND", "EKF-Orig-Diag", "NL-Lim",
                    "NL-Unlim", "Lin"],
        "x": "Dataset"
    },

    "movielens": {
        "elements": ["EKF-Sub-SVD", "EKF-Sub-RND", "EKF-Sub-Diag-SVD",
                    "EKF-Sub-Diag-RND", "EKF-Orig-Diag", "NL-Lim",
                    "NL-Unlim", "Lin"],
        "x": "Model"
        },
}

plot_configs = [
    {"metric": "Reward", "log_scale":False},
    {"metric": "Time", "log_scale":True},

]

def main():
    # Create reward / runnnig time experiments
    print("Plotting reward / running time")
    for dataset_name in dataset_info:
        print(dataset_name)
        info = dataset_info[dataset_name]
        methods = info["elements"]
        x = info["x"]

        df = read_data(dataset_name)
        df = df[df["Method"].isin(methods)]

        for config in plot_configs:
            metric = config["metric"]
            use_log_scale = config["log_scale"]

            filename = f"{dataset_name}_{metric.lower()}"
            plot_figure(df, x, metric, filename, log_scale=use_log_scale)

    # Plot subspace-dim v.s. reward
    print("Plotting subspace dim v.s. reward")
    *_, filename = sorted(glob.glob(f"./bandits/results/tabular_subspace_results*.csv"))
    tabular_sub_df = pd.read_csv(filename)

    datasets = ["shuttle", "adult", "covertype"]
    for dataset_name in datasets:
        print(dataset_name)
        subdf = tabular_sub_df.query("Dataset == @dataset_name")
        plot_subspace_figure(subdf)

if __name__ == "__main__":
    main()


import numpy as np
import lime
from lime.io import _LOG_EXPORT_DICT

# Outputs
file_address = 'manga_spaxel.txt'
cube_plot_address = 'cube_manga_plot.png'
spectrum_plot_address = 'spectrum_manga_spaxel.png'
line_plot_address = 'Fe3_4658A_manga_spaxel.png'
line_bands_file = f'manga_line_bands.txt'
lines_log_file = f'manga_lines_log.txt'
cfg_file = 'manga.toml'

wave_array, flux_array, err_array = np.loadtxt(file_address, unpack=True)
pixel_mask = np.isnan(err_array)

# Parameters
redshift = 0.0475
norm_flux = 1e-17

cfg = lime.load_cfg(cfg_file)
spec = lime.Spectrum(wave_array, flux_array, err_array, redshift=redshift, norm_flux=norm_flux, pixel_mask=pixel_mask)

# spec.fit.frame(line_bands_file, cfg, id_conf_prefix='38-35')
#
# # spec.plot.spectrum(include_fits=True)
#
# log_orig = lime.load_log(lines_log_file)

extension = 'xlsx'
test_log_file = f'test_lines_log.{extension}'
spec.fit.frame(line_bands_file, cfg, id_conf_prefix='38-35')
spec.save_log(test_log_file, page='NewLinesLog')

log_orig = lime.load_log(lines_log_file)
log_test = lime.load_log(test_log_file)

for line in spec.log.index:
    for param in spec.log.columns:

        # String
        if _LOG_EXPORT_DICT[param].startswith('<U'):
            if log_orig.loc[line, param] is np.nan:
                assert log_orig.loc[line, param] is log_test.loc[line, param]
            else:
                assert log_orig.loc[line, param] == log_test.loc[line, param]

        # Float
        else:
            if param not in ['eqw', 'eqw_err']:
                # print('\n', param, log_orig.loc[line, param], log_test.loc[line, param])
                assert np.allclose(log_orig.loc[line, param], log_test.loc[line, param], rtol=0.05,
                                   equal_nan=True)
            else:
                assert np.allclose(log_orig.loc[line, param], log_test.loc[line, param], rtol=0.15,
                                   equal_nan=True)

# for ext in ['txt', 'fits', 'csv', 'xlsx', 'asdf']:
#
#     spec.save_log(f'test_frame.{ext}')
#     log_test = lime.load_log(f'test_frame.{ext}')
#
#     for line in log_orig.index:
#
#         for param in log_orig.columns:
#
#             # String
#             if _LOG_EXPORT_DICT[param].startswith('<U'):
#                 if log_orig.loc[line, param] is np.nan:
#                     check_A = log_orig.loc[line, param] is log_test.loc[line, param]
#                 else:
#                     check_A = log_orig.loc[line, param] == log_test.loc[line, param]
#
#             # Float
#             else:
#                 if param != 'eqw_err':
#                     check_A = np.allclose(log_orig.loc[line, param], log_test.loc[line, param], rtol=0.02, equal_nan=True)
#                 else:
#                     check_A = np.allclose(log_orig.loc[line, param], log_test.loc[line, param], rtol=0.15, equal_nan=True)
#
#             if check_A is False:
#                 print(line, param, ext)
#                 print(f'-- Fail): {log_orig.loc[line, param]} != {log_test.loc[line, param]}')
#                 # raise 'Coso'


import curses
import io
import time

from rich.console import Console
from rich.table import Table
from rich.text import Text
from rich import box

def main():

    # initialize curses
    screen = curses.initscr()
    curses.noecho() # turn off key echoing
    curses.cbreak() # respond to keys immediately (don't wait for Enter)
    # screen.keypad(True) # enable arrow keys

    # # set up initial position of character
    # row, col = 10, 10
    #
    # while True:
    #     # draw the character at its current position
    #     screen.addch(row, col, '@')
    #
    #     # get user input
    #     key = screen.getch()
    #
    #     # move the character based on input
    #     if key == curses.KEY_UP:
    #         row -= 1
    #     elif key == curses.KEY_DOWN:
    #         row += 1
    #     elif key == curses.KEY_LEFT:
    #         col -= 1
    #     elif key == curses.KEY_RIGHT:
    #         col += 1
    #     elif key == ord('q'):
    #         break
    #
    #     # clear the screen and move the cursor back to the top left
    #     screen.clear()
    #     screen.move(0, 0)

    for i in range(10):
        console = Console(file=io.StringIO())
        table = Table(
            title="My 4x4 Table",
            style="bright_white",
            caption_style="bright_white",
            box=box.ROUNDED,
            padding=0,
            show_header=False,
            show_footer=False,
            show_lines=True
        )

        # Add four columns with a minimum width of 14 characters
        for i in range(4):
            table.add_column(justify="center", min_width=14)

        # Add four rows to the table
        for i in range(4):
            row = table.add_row("'   _____ __ __\n  / ___// // /\n / __ \\/ // /_\n/ /_/ /__  __/\n\\____/  /_/   \n              \n'", "", "", "")
            # for cell in row.cells:
            #     cell.min_height = 5
        # Print the table
        console.print(table)
        output = console.file.getvalue()
        screen.addstr(f"size of output: {len(output)}")
        screen.addstr(output)
        screen.refresh()
        time.sleep(5)

    # console.print(table)
    #
    # # print("\n\n======\n")
    # screen.addstr("\n\n======\n")
    # with console.capture() as capture:
    #     console.print(table)
    # capture_str = capture.get()
    # # print("capture string length: ", len(capture_str))
    # screen.addstr("capture string length: ", len(capture_str))
    # # for i in range(10):
    # #     print("a")
    # # print("capture string type: ", type(capture_str))
    # # print("num returns: ", len(capture_str.split("\n")))
    # # print("printing line by line seprarated by ==========")
    # for line in capture_str.split("\n"):
    #     # print(line)
    #     screen.addstr(line)
    #     # print("==========")

    # # clean up curses before exiting
    curses.nocbreak()
    # screen.keypad(False)
    curses.echo()
    curses.endwin()

if __name__ == "__main__":
    main()


import random
from typing import List, Dict
"""
This file can be a nice home for your move logic, and to write helper functions.

We have started this for you, with a function to help remove the 'neck' direction
from the list of possible moves!
"""

def avoid_my_neck(my_head: Dict[str, int], my_body: List[dict], possible_moves: List[str]) -> List[str]:
    """
    my_head: Dictionary of x/y coordinates of the Battlesnake head.
            e.g. {"x": 0, "y": 0}
    my_body: List of dictionaries of x/y coordinates for every segment of a Battlesnake.
            e.g. [ {"x": 0, "y": 0}, {"x": 1, "y": 0}, {"x": 2, "y": 0} ]
    possible_moves: List of strings. Moves to pick from.
            e.g. ["up", "down", "left", "right"]

    return: The list of remaining possible_moves, with the 'neck' direction removed
    """
    my_neck = my_body[
        1]  # The segment of body right after the head is the 'neck'

    if my_neck["x"] < my_head["x"]:  # my neck is left of my head
        possible_moves.remove("left")
    elif my_neck["x"] > my_head["x"]:  # my neck is right of my head
        possible_moves.remove("right")
    elif my_neck["y"] < my_head["y"]:  # my neck is below my head
        possible_moves.remove("down")
    elif my_neck["y"] > my_head["y"]:  # my neck is above my head
        possible_moves.remove("up")

    return possible_moves

def SnakeSafe(data, x, y):
    for asmr in data['board']['hazards']:
        if asmr['x'] == x and asmr['y'] == y:
            return False
    for bmi in data['board']['snakes']:
        for _ in bmi['body']:
            if _['x'] == x and _['y'] == y:
                return False
    return True

def avoid_myself(my_head: Dict[str, int], data, my_body: List[dict],
                 possible_moves: List[str]) -> List[str]:
    print("AVOID MYSELF")
    for _ in data['board']['snakes']:
        for n in _['body']:
            if n['x'] == my_head['x'] - 1 and n['y'] == my_head[
                    'y'] and 'left' in possible_moves:
                possible_moves.remove('left')
                print(possible_moves)
                print(my_head['x'] - 1)
                print(n['x'])
            elif n['x'] == my_head['x'] + 1 and n['y'] == my_head[
                    'y'] and 'right' in possible_moves:
                possible_moves.remove('right')
                print(possible_moves)
                print(my_head['x'] + 1)
                print(n['x'])
            elif n['y'] == my_head['y'] - 1 and n['x'] == my_head[
                    'x'] and 'down' in possible_moves:
                possible_moves.remove('down')
                print(possible_moves)
                print(my_head['y'] - 1)
                print(n['y'])
            elif n['y'] == my_head['y'] + 1 and n['x'] == my_head[
                    'x'] and 'up' in possible_moves:
                possible_moves.remove('up')
                print(possible_moves)
                print(my_head['y'] + 1)
                print(n['y'])
    return possible_moves

def D(x1, y1, x2, y2):
    return abs(x1 - x2) + abs(y1 - y2)

def SnakeFood(data):
    x, y = 0, 0
    hx = data["you"]["head"]["x"]
    hy = data["you"]["head"]["y"]
    foods = data["board"]["food"]
    min_d = None
    for food in foods:
        dx = food["x"]
        dy = food["y"]
        dist = D(hx, hy, dx, dy)
        if min_d is None or dist < min_d:
            min_d = dist
            x, y = dx, dy
    return x, y

def SnakeNeck(data):
    headx = data["you"]["body"][0]["x"]
    heady = data["you"]["body"][0]["y"]
    neckx = data["you"]["body"][1]["x"]
    necky = data["you"]["body"][1]["y"]
    if headx < neckx:
        return "right"
    if headx > neckx:
        return "left"
    if heady < necky:
        return "up"
    if heady > necky:
        return "down"

def food(my_head, data, my_body, possible_moves):
    mov = None
    if len(data['board']['food']) > 0:
        if 'right' in avoid_myself(
                my_head, data, my_body,
                possible_moves) and SnakeFood(data)[0] > my_head['x']:
            mov = 'right'
        elif 'left' in avoid_myself(
                my_head, data, my_body,
                possible_moves) and SnakeFood(data)[0] < my_head['x']:
            mov = 'left'
        elif 'down' in avoid_myself(
                my_head, data, my_body,
                possible_moves) and SnakeFood(data)[1] < my_head['y']:
            mov = 'down'
        elif 'up' in avoid_myself(
                my_head, data, my_body,
                possible_moves) and SnakeFood(data)[1] > my_head['y']:
            mov = 'up'
        else:
            mov = choose_move(data)
    else:
        mov = choose_move(data)
    return mov

def avoid_borders(data, possible_moves):
    my_head = data['you']['head']
    print('border code')
    if my_head['x'] == data['board']['width'] - 1:
        possible_moves.remove('right')
        print(possible_moves)
    if my_head['x'] == 0:
        possible_moves.remove('left')
        print(possible_moves)
    if my_head['y'] == data['board']['height'] - 1:
        possible_moves.remove('up')
        print(possible_moves)
    if my_head['y'] == 0:
        possible_moves.remove('down')
        print(possible_moves)

    return possible_moves

def choose_move(data: dict) -> str:
    """
    data: Dictionary of all Game Board data as received from the Battlesnake Engine.
    For a full example of 'data', see https://docs.battlesnake.com/references/api/sample-move-request

    return: A String, the single move to make. One of "up", "down", "left" or "right".

    Use the information in 'data' to decide your next move. The 'data' variable can be interacted
    with as a Python Dictionary, and contains all of the information about the Battlesnake board
    for each move of the game.

    """
    my_head = data["you"][
        "head"]  # A dictionary of x/y coordinates like {"x": 0, "y": 0}
    my_body = data["you"][
        "body"]  # A list of x/y coordinate dictionaries like [ {"x": 0, "y": 0}, {"x": 1, "y": 0}, {"x": 2, "y": 0} ]

    # TODO: uncomment the lines below so you can see what this data looks like in your output!
    # print(
    #     f"~~~ Turn: {data['turn']}  Game Mode: {data['game']['ruleset']['name']} ~~~"
    # )
    # print(f"All board data this turn: {data}")
    # print(f"My Battlesnakes head this turn is: {my_head}")
    # print(f"My Battlesnakes body this turn is: {my_body}")

    possible_moves = ["up", "down", "left", "right"]

    # Don't allow your Battlesnake to move back in on it's own neck
    possible_moves = avoid_my_neck(my_head, my_body, possible_moves)
    possible_moves = avoid_borders(data, possible_moves)
    possible_moves = avoid_myself(my_head, data, my_body, possible_moves)

    # TODO: Using information from 'data', find the edges of the board and don't let your Battlesnake move beyond them
    # board_height = ?
    # board_width = ?

    # TODO Using information from 'data', don't let your Battlesnake pick a move that would hit its own body

    # TODO: Using information from 'data', don't let your Battlesnake pick a move that would collide with another Battlesnake

    # TODO: Using information from 'data', make your Battlesnake move towards a piece of food on the board

    # Choose a random direction from the remaining possible_moves to move in, and then return that move
    move = random.choice(possible_moves)
    print(my_head)
    # TODO: Explore new strategies for picking a move that are better than random

    print(
        f"{data['game']['id']} MOVE {data['turn']}: {move} picked from all valid options in {possible_moves}"
    )

    return move


from typing import Optional, Union

import cv2
import numpy as np
from sklearn.cluster import KMeans

def postprocess(image: np.ndarray, threshold: Optional[Union[str, int]] = None) -> np.ndarray:
    """Apply postprocessing on alpha prediction to make it appear better.
    Currently, the postprocessing steps are thresholding to make border sharper/crisper and
    Gaussian blurring to make the border edges smoother.

    Arguments:
        image {np.ndarray} -- Image

    Keyword Arguments:
        threshold {Union[str, int]} -- Threshold used: int or "kmeans" (default: {"kmeans"})

    Returns:
        np.ndarray -- Processed image
    """

    print("Applying postprocessing...")

    ori_image_shape = image.shape

    # print(
    #     f">>>>>>>>>> image: min: {image.min()}, max: {image.max()}, dtype: {image.dtype}, shape: {image.shape}")

    if threshold:
        if threshold == "kmeans":
            kmeans = KMeans(n_clusters=2, random_state=0,
                            n_jobs=-1).fit(image.reshape(-1, 1))
            threshold = kmeans.cluster_centers_.ravel().mean()
        threshold = 128

        # image[image < threshold] = 0
        # image[image >= threshold] = 255
        _, image = cv2.threshold(image, threshold, 255, cv2.THRESH_BINARY)

    image = cv2.GaussianBlur(image, (5, 5), 0)
    print(
        f">>>>>>>>>> image: min: {image.min()}, max: {image.max()}, dtype: {image.dtype}, shape: {image.shape}")

    # image = (image * 255).astype("uint8")
    # _, image = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY)
    # image = image / np.float32(255)

    if len(ori_image_shape) == 3 and image.ndim == 2:
        image = np.expand_dims(image, axis=-1)

    assert image.shape == ori_image_shape, "Image shape changed!"

    return image


# -*- coding: utf-8 -*-
"""
Created on Sun Sep  6 08:37:49 2020

@author: USER
"""

import json
import numpy as np

f = open("C:\Ripplage\価格データ.json", "r")
data = json.load(f)
#dataは1時間足のロー測データ5999個分

#移動平均、BBの計算期間
term=20

#スクイーズを定義する期間
squeeze_term=15

#ARを取得する期間
average_range_term=5

#最小標準偏差がでてから何期間までsqueezeが持続しているとみなすか
squeeze_flag=4

#ARの何倍の実線の長さでsigma2をブレイクした際1期間で直ちにトレンド発生とみなすか
quick_trend_change_rate=2.5

#一つの期間に対して標準偏差を作る関数。term, i, data[リスト]を入れ、標準偏差と移動平均を返す。
def std_in_term (term, i, data):

    close_prices=[]
    count=1

    while count<=term:
        close_prices.append(data[i-(term)+count]['close_price'])
        count+=1

    ave=sum(close_prices)/len(close_prices)
    std=np.std(close_prices)

    return ave, std

#特定の期間average_range_termでのARを返す
def get_average_range (average_range_term, i, data):

    range=[]
    count=1

    while count<=term:
        single_range=abs(data[i-average_range_term+count]['open_price']-data[i-average_range_term+count]['close_price'])
        range.append(single_range)
        count+=1

    average_range=sum(range)/len(range)

    return average_range

#スクイーズがおきているかどうか判定する関数
def check_squeeze(std_list, squeeze_term, squeeze_flag):

    std_list_for_squeeze=[]
    count=0

    while count<squeeze_term:
        std=std_list[-(squeeze_term-count)]
        std_list_for_squeeze.append(std)
        count+=1

    minimum_std=min(std_list_for_squeeze)

    std_list_in_squeeze_flag=[]
    count2=1

    while count2<=squeeze_flag:
        std_list_in_squeeze_flag.append(std_list_for_squeeze[-count2])
        count2+=1

    if minimum_std in std_list_in_squeeze_flag:
        return True
    else:
        return False

#所与の終値がARのquick_trend_change_rate倍の長さで2シグマをブレイクしているか

def check_quick_trend_change(average_range, std, ave, quick_trend_change_rate, data, i):

    #まず、所与のローソクの長さをcandle_lengthに入れる
    candle_length=data[i]['close_price']-data[i]['open_price']

    if candle_length>quick_trend_change_rate*average_range:
        pass

    else:
        return False

    if candle_length>=0:

        if data[i]['close_price']>ave+2*std:
            return True
        else:
            return False
    else:

        if data[i]['close_price']<ave-2*std:
            return True

        else:
            return False

def serial_candle (i, data, std_list, ave_list):

    current_open_price=data[i]['open_price']
    previous_open_price=data[i]['open_price']

    current_close_price=data[i]['close_price']
    previous_close_price=data[i]['close_price']

    two_close_price_relation=current_close_price-previous_close_price
    two_open_price_relation=current_open_price-previous_open_price

    if current_close_price>ave_list[-1]:

        #シグマ2を二本連続でブレイクし、始値と終値が両方切りあがっているか
        previous_sigma2=ave_list[-2]+2*std_list[-2]
        current_sigma2=ave_list[-1]+2*std_list[-1]

        if previous_close_price>previous_sigma2 \
        and current_close_price>current_sigma2 \
        and two_close_price_relation>0 \
        and two_open_price_relation>0:

            return True
        else:
            return False

    else:
        previous_sigma2=ave_list[-2]-2*std_list[-2]
        current_sigma2=ave_list[-1]-2*std_list[-1]

        if previous_close_price<previous_sigma2 \
        and current_close_price<current_sigma2 \
        and two_close_price_relation<0 \
        and two_open_price_relation<0:

            return True

        else:
            return False

def check_expansion (std_list, average_range, average_range_term):

    recent_sigmas=[]

    count=-(average_range_term)

    while count<0:
        recent_sigmas.append(std_list[count])
        count+=1

    max_recent_sigma=max(recent_sigmas)

    if 2*max_recent_sigma> 3*average_range:

        return True

    else:
        return False

def check_serial_candle(data, i, position):

    current_candle=data[i]['close_price']-data[i]['open_price']
    previous_candle=data[i-1]['close_price']-data[i-1]['open_price']

    if position=='sell':

        if current_candle>0 and previous_candle>0:
            return True
        else:
            pass
    else:

        if current_candle<0 and previous_candle<0:
            return True
        else:
            pass

def check_contrary_candle (data, i, position):
    current_candle=data[i]['close_price']-data[i]['open_price']

    if position=='sell' and current_candle>0:
        return True
    elif position=='buy' and current_candle<0:
        return True
    else:
        return False

def check_big_contrary (average_range, data, i, position):
    latest_candle_range=data[i]['close_price']-data[i]['close_price']

    if position=='long':

        if latest_candle_range<0 and abs(latest_candle_range)>average_range:

            return True
        else:
            pass
    else:
        if latest_candle_range>0 and abs(latest_candle_range)>average_range:

            return True
        else:
            pass

position='OFF'
i=term
contrary_candle=0

while i<5999:

    if position=='OFF':

        #stdリストを作る。
        std_list=[]

        #aveリストを作る。
        ave_list=[]

        count1=1

        while count1<=term:

            ave, std=std_in_term(term, i-term+count1, data)
            std_list.append(std)
            ave_list.append(ave)
            count1+=1

        #term中のclose_priceのリストを作る
        close_price_list=[]
        count2=1

        while count2<=term:

            close_price=data[i-term+count2]['close_price']
            close_price_list.append(close_price)
            count2+=1

        average_range=get_average_range(average_range_term, i, data)
        squeeze=check_squeeze(std_list, squeeze_term, squeeze_flag)
        expansion=check_expansion (std_list, average_range, average_range_term)

        if squeeze and not expansion:

            quick_trend_change=check_quick_trend_change(average_range,
                                                        std,
                                                        ave,
                                                        quick_trend_change_rate,
                                                        data,
                                                        i)

            serial_candles=serial_candle (i, data, std_list, ave_list)

            if quick_trend_change or serial_candles:
                #エントリーのコード
                print(str(i)+'回目でエントリーです')

                position='sell'#か'buy'

            else:
                pass
        else:
            pass

    else:
        pass
        """

        if check_contrary_candle (data, i, position):
            contrary_candle+=1
        else:
            pass

        average_range=get_average_range(average_range_term, i, data)

        if contrary_candle>2\
        or check_serial_candle(data, i, position)\
        or check_big_contrary(average_range, data, i, position):

            #手じまいのコードを実装
            print(str(i)+'回目でカバーです')
            position=='OFF'
        else:
            pass

        """

    i+=1


# -*- coding: utf-8 -*-
# @Author: solicucu

import sys
sys.path.append('..')
import os
import argparse
import logging
import time
import openpyxl as xl
import torch
import torch.nn as nn

from config import cfg
from torch.backends import cudnn
from utils import setup_logger, R1_mAP
from data import make_data_loader
from model import build_model, CDNetwork, CNetwork
from optims import make_optimizer, make_lr_scheduler, make_loss
from utils.utils import count_parameters, infer_count_parameters
from utils.metrics import *
from utils.flop_benchmark import get_model_infos

"""
Args:
	res: a list of elem like [scores, feats]
	labels: ground truth of the imgs class
	loss_fn: type of loss function to compute the loss
Returns:
	loss: summation of each loss
	acc: avg of each loss
"""
def compute_loss_acc(res, labels, loss_fn):

	loss, acc = 0. , 0.
	num = len(res)

	for scores, feats in res:
		ls = loss_fn(scores, feats, labels)
		loss += ls

		ac = (scores.max(1)[1] == labels).float().mean()
		acc += ac

	return loss, acc / num

def parse_config():

	# create the parser
	parser = argparse.ArgumentParser(description = "CDNet training")

	parser.add_argument("--config_file", default = '', help = "path to specify config file", type = str)

	#remainder parameters in a list
	parser.add_argument("opts", default = None, help = 'modify some value for the config file in command line', nargs = argparse.REMAINDER)

	args = parser.parse_args()

	if args.config_file != "":
		# use config file to update the default config value
		cfg.merge_from_file(args.config_file)
	# note that opts is a list
	cfg.merge_from_list(args.opts)
	# cfg.freeze() if use this, the cfg can not be revised

	output_dir = cfg.OUTPUT.DIRS
	if output_dir != "":
		if not os.path.exists(output_dir):
			os.makedirs(output_dir)
	else:
		print("ERROR:please specify an output path")
		exit(1)

	# config the logger
	logger = setup_logger("CDNet", output_dir, 0, cfg.OUTPUT.LOG_NAME)

	use_gpu = cfg.MODEL.DEVICE == "cuda"
	if use_gpu:
		logger.info("Train with GPU: {}".format(cfg.MODEL.DEVICE_IDS))
	else:
		logger.info("Train with CPU")

	#print the all arguments
	logger.info(args)
	#read the config file
	if args.config_file != "":
		logger.info("load configuration file {}".format(args.config_file))

	# print the configuration
	logger.info("running with config:\n{}".format(cfg))

	if use_gpu:
		os.environ["CUDA_VISIBLE_DEVICES"] = cfg.MODEL.DEVICE_IDS

	#this setup will facilitate the training
	cudnn.benchmark = True

def train():

	# 1、make dataloader
	# prepare train,val img_info list, elem is tuple;
	train_loader, val_loader, num_query, num_class = make_data_loader(cfg)

	# 2、make model
	model = build_model(cfg, num_class)

	# 3、 make optimizer
	optimizer = make_optimizer(cfg, model)

	# 4、 make lr_scheduler
	scheduler = make_lr_scheduler(cfg, optimizer)

	# 5、make loss
	loss_fn = make_loss(cfg, num_class)

	# get parameters
	device = cfg.MODEL.DEVICE
	use_gpu = device == "cuda"
	pretrained = cfg.MODEL.PRETRAIN_PATH != ""
	parallel = cfg.MODEL.PARALLEL

	log_period = cfg.OUTPUT.LOG_PERIOD
	ckpt_period = cfg.OUTPUT.CKPT_PERIOD
	eval_period = cfg.OUTPUT.EVAL_PERIOD
	output_dir = cfg.OUTPUT.DIRS
	ckpt_save_path = output_dir + cfg.OUTPUT.CKPT_DIRS

	epochs = cfg.SOLVER.MAX_EPOCHS
	batch_size = cfg.SOLVER.BATCH_SIZE
	grad_clip = cfg.SOLVER.GRAD_CLIP

	batch_num = len(train_loader)
	log_iters = batch_num // log_period

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

	# create *_result.xlsx
	# save the result for analyze
	name = (cfg.OUTPUT.LOG_NAME).split(".")[0] + ".xlsx"
	result_path = cfg.OUTPUT.DIRS + name

	wb = xl.Workbook()
	sheet = wb.worksheets[0]
	titles = ['size/M','speed/ms','final_planes', 'acc', 'mAP', 'r1', 'r5', 'r10', 'loss',
			  'acc', 'mAP', 'r1', 'r5', 'r10', 'loss','acc', 'mAP', 'r1', 'r5', 'r10', 'loss']
	sheet.append(titles)
	check_epochs = [40, 80, 120, 160, 200, 240, 280, 320, 360, epochs]
	values = []

	logger = logging.getLogger("CDNet.train")
	size = count_parameters(model)
	values.append(format(size, '.2f'))
	values.append(model.final_planes)

	logger.info("the param number of the model is {:.2f} M".format(size))
	infer_size = infer_count_parameters(model)
	logger.info("the infer param number of the model is {:.2f}M".format(infer_size))

	shape = [1, 3]
	shape.extend(cfg.DATA.IMAGE_SIZE)

	# if cfg.MODEL.NAME == 'cdnet' :
	# 	infer_model = CDNetwork(num_class, cfg)
	# elif cfg.MODEL.NAME == 'cnet':
	# 	infer_model = CNetwork(num_class, cfg)
	# else:
	# 	infer_model = model

	# for scaling experiment
	flops, _ = get_model_infos(model, shape)
	logger.info("the total flops number of the model is {:.2f} M".format(flops))

	logger.info("Starting Training CDNetwork")

	best_mAP, best_r1 = 0., 0.
	is_best = False
	avg_loss, avg_acc = RunningAverageMeter(),RunningAverageMeter()
	avg_time, global_avg_time = AverageMeter(), AverageMeter()

	if parallel:
		model = nn.DataParallel(model)

	if use_gpu:
		model = model.to(device)

	for epoch in range(epochs):

		scheduler.step()
		lr = scheduler.get_lr()[0]
		# if save epoch_num k, then run k+1 epoch next
		if pretrained and epoch < model.start_epoch:
			continue

		# rest the record
		model.train()
		avg_loss.reset()
		avg_acc.reset()
		avg_time.reset()

		for i, batch in enumerate(train_loader):

			t0 = time.time()
			imgs, labels = batch

			if use_gpu:
				imgs = imgs.to(device)
				labels = labels.to(device)

			res = model(imgs)

			loss, acc = compute_loss_acc(res, labels, loss_fn)
			loss.backward()

			if grad_clip != 0:
				nn.utils.clip_grad_norm_(model.parameters(), grad_clip)

			optimizer.step()
			optimizer.zero_grad()

			t1 = time.time()
			avg_time.update((t1 - t0) / batch_size)
			avg_loss.update(loss)
			avg_acc.update(acc)

			# log info
			if (i+1) % log_iters == 0:
				logger.info("epoch {}: {}/{} with loss is {:.5f} and acc is {:.3f}".format(
					epoch+1, i+1, batch_num, avg_loss.avg, avg_acc.avg))

		logger.info("end epochs {}/{} with lr: {:.5f} and avg_time is: {:.3f} ms".format(epoch+1, epochs, lr, avg_time.avg * 1000))
		global_avg_time.update(avg_time.avg)

		# test the model
		if (epoch + 1) % eval_period == 0 or (epoch + 1) in check_epochs:

			model.eval()
			metrics = R1_mAP(num_query, use_gpu = use_gpu)

			with torch.no_grad():
				for vi, batch in enumerate(val_loader):

					imgs, labels, camids = batch
					if use_gpu:
						imgs = imgs.to(device)

					feats = model(imgs)
					metrics.update((feats, labels, camids))

				#compute cmc and mAP
				cmc, mAP = metrics.compute()
				logger.info("validation results at epoch {}".format(epoch + 1))
				logger.info("mAP:{:2%}".format(mAP))
				for r in [1,5,10]:
					logger.info("CMC curve, Rank-{:<3}:{:.2%}".format(r, cmc[r-1]))

				# determine whether current model is the best
				if mAP > best_mAP:
					is_best = True
					best_mAP = mAP
					logger.info("Get a new best mAP")
				if cmc[0] > best_r1:
					is_best = True
					best_r1 = cmc[0]
					logger.info("Get a new best r1")

				# add the result to sheet
				if (epoch + 1) in check_epochs:
					val = [avg_acc.avg, mAP, cmc[0], cmc[4], cmc[9]]
					change = [format(v * 100, '.2f') for v in val]
					change.append(format(avg_loss.avg, '.3f'))
					values.extend(change)

		# whether to save the model
		if (epoch + 1) % ckpt_period == 0 or is_best:
			torch.save(model.state_dict(), ckpt_save_path + "checkpoint_{}.pth".format(epoch + 1))
			logger.info("checkpoint {} was saved".format(epoch + 1))

			if is_best:
				torch.save(model.state_dict(), ckpt_save_path + "best_ckpt.pth")
				logger.info("best_checkpoint was saved")
				is_best = False

	values.insert(1, format(global_avg_time.avg * 1000, '.2f'))
	values.append(format(infer_size, '.2f'))
	sheet.append(values)
	wb.save(result_path)
	logger.info("best_mAP:{:.2%}, best_r1:{:.2%}".format(best_mAP, best_r1))
	logger.info("Ending training CDNetwork")

def main():

	parse_config()
	train()

if __name__ == "__main__":

	main()


from typing import Iterable

from autowsgr.constants.image_templates import IMG
from autowsgr.constants.positions import FLEET_POSITION
from autowsgr.controller.run_timer import Timer
from autowsgr.game.game_operation import MoveTeam
from autowsgr.ocr.ship_name import recognize_ship
from autowsgr.utils.api_image import convert_position
from autowsgr.utils.operator import unorder_equal

def count_ship(fleet):
    res = sum(have_ship(fleet[i]) for i in range(1, min(len(fleet), 7)))
    return res

def have_ship(ship):
    return ship is not None and ship != ""

class Ship:
    pass

class Fleet:
    def __init__(self, timer: Timer, fleet_id=None):
        self.timer = timer
        self.fleet_id = fleet_id
        self.flag_ship = None
        self.ships = [None] * 7

    def exist(self, name):
        return name in self.ships

    def empty(self):
        return self.count() == 0

    def count(self):
        return 0 if self.ships is None else count_ship(self.ships)

    def __eq__(self, other: object):
        if not isinstance(other, (Iterable, Fleet)):
            raise ValueError()
        ships = other
        if isinstance(other, Fleet):
            if other.fleet_id != self.fleet_id:
                return False
            ships = other.ships
        for i in range(1, 7):
            if have_ship(ships[i]) != have_ship(self.ships[i]) or (have_ship(ships[i]) and ships[i] != self.ships[i]):
                return False
        return True

    def detect(self):
        """在对应的战斗准备页面检查舰船"""
        assert self.timer.wait_image(IMG.identify_images["fight_prepare_page"][0]) != False
        if self.fleet_id is not None:
            MoveTeam(self.timer, self.fleet_id)
        ships = recognize_ship(self.timer.get_screen()[433:459], self.timer.ship_names)
        self.ships = [None] * 7
        for rk, ship in enumerate(ships):
            self.ships[rk + 1] = ship[0]

    def change_ship(self, position, ship_name, search_method="word"):
        self.ships[position] = ship_name
        self.timer.Android.click(*FLEET_POSITION[position], delay=0)
        res = self.timer.wait_images(
            IMG.choose_ship_image[1:3] + [IMG.choose_ship_image[4]],
            after_get_delay=0.4,
            gap=0,
            timeout=16,
        )
        if res == None:
            raise TimeoutError("选择舰船时点击超时")
        if ship_name is None:
            self.timer.Android.click(83, 167, delay=0)
        else:
            if res == 1:
                self.timer.Android.click(839, 113)
            if search_method == "word":
                self.timer.Android.click(700, 30, delay=0)
                self.timer.wait_image(IMG.choose_ship_image[3], gap=0, after_get_delay=0.1)
                self.timer.Android.text(ship_name)
                self.timer.Android.click(1219 * 0.75, 667 * 0.75, delay=1)

            ships = recognize_ship(self.timer.get_screen()[:, :1048], self.timer.ship_names)
            for ship in ships:
                if ship[0] == ship_name:
                    center = (ship[1][1][0] + 20, ship[1][1][1])
                    center = convert_position(*center, (1280, 720), "this_to_960")
                    self.timer.Android.click(*center)
                    break

        self.timer.wait_pages("fight_prepare_page", gap=0)

    def _set_ships(self, ships, search_method="word"):
        ok = [None] + [False] * 6
        if self.ships is None:
            self.detect()
        for i in range(1, 7):
            ship = self.ships[i]
            if not have_ship(ship):
                continue
            if ship in ships:
                ok[i] = True
        for ship in ships:
            if ship in self.ships or not have_ship(ship):
                continue
            position = ok.index(False)
            self.change_ship(position, ship, search_method=search_method)
            ok[position] = True

        for i in range(1, 7):
            if ok[7 - i] == False and self.ships[7 - i] != None:
                self.change_ship(7 - i, None)
                self.ships[7 - i :] = self.ships[8 - i :]

    def reorder(self, ships):
        assert unorder_equal(ships, self.ships, skip=[None, ""])
        for i in range(1, 7):
            ship = ships[i]
            if not have_ship(ship):
                return
            if self.ships[i] != ship:
                self.circular_move(self.ships.index(ship), i)

    def circular_move(self, p1, p2):
        if p1 > p2:
            self.ships = self.ships[:p2] + self.ships[p1 : p1 + 1] + self.ships[p2:p1] + self.ships[p1 + 1 :]
        else:
            self.ships = self.ships[:p1] + self.ships[p1 + 1 : p2 + 1] + self.ships[p1 : p1 + 1] + self.ships[p2 + 1 :]
        assert len(self.ships) == 7
        p1 = FLEET_POSITION[p1]
        p2 = FLEET_POSITION[p2]
        self.timer.Android.swipe(*p1, *p2)

    def legal(self, ships):
        ok = False
        if len(ships) <= 7:
            ships += [None] * 7
        for i in range(1, 7):
            if ships[i] is None:
                ok = True
            if ok and ships[i] is not None:
                return False
        return True

    def set_ship(self, ships, flag_ship=None, order=False, search_method="word"):
        """设置指定位置的舰队, 1-index
        Args:
            ships (list(str)): 代表舰船 [0号位留空, 1号位, 2号位, ...]
            flag_ship: 如果不为 None, 则代表旗舰名称
            order (bool): 是否按照 ships 给定的顺序 (优先级高于旗舰指定)
        """
        assert self.legal(ships)
        assert flag_ship is None or flag_ship in ships
        self.detect()
        self._set_ships(ships, search_method=search_method)
        if order:
            self.reorder(ships)
        elif flag_ship is not None:
            position = self.ships.index(flag_ship)
            if position != 1:
                self.circular_move(position, 1)

    def reset(self):
        self.__init__(self.timer, self.fleet_id)


from django.db import models

class CRUDManager(models.Manager):
    def update_product(self, data, pk):
        product = self.get(pk=pk)
        product.name = data['name']
        product.brand = data['brand']
        product.description = data['description']
        product.price = data['price']
        product.sub_category_id = data['sub_category']
        product.save()


from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse

import markdown, pygments
from comments.forms import CommentForm

from .models import Post, Category

def index(request):
    '''
    首页
    '''
    post_list = Post.objects.all()
    return render(request, 'blog/index.html', context={
        'post_list': post_list
    })

def detail(request, pk):
    '''
    详情页
    '''
    post = get_object_or_404(Post, pk=pk)
    post.body = markdown.markdown(post.body,
                                  extensions=[
                                      'markdown.extensions.extra',
                                      'markdown.extensions.codehilite', # 语法高亮拓展
                                      'markdown.extensions.toc', # 自动生成目录
                                  ])
    post.increase_visiting()

    comment_form = CommentForm()
    comment_list = post.comment_set.all()

    # 将文章、表单、以及文章下的评论列表作为模板变量传给 detail.html 模板，以便渲染相应数据。
    return render(request, 'blog/detail.html', context={
        'post': post,
        'comment_form': comment_form,
        'comment_list': comment_list,
    })

def archives(request, year, month):
    '''
    归档
    '''
    post_list = Post.objects.filter(created_time__year=year, created_time__month=month)
    return render(request, 'blog/index.html', context={'post_list': post_list})

def category(request, pk):
    '''
    分类
    '''
    cat = get_object_or_404(Category, pk=pk)
    post_list = Post.objects.filter(category=cat)
    return render(request, 'blog/index.html', context={'post_list': post_list})


from collections import deque
def solution(land):
    land = deque(land)
    while len(land)>1:
        tmp=land.popleft()
        for i in range(4):
            tmp1=[]
            for j in range(4):
                if i==j:
                    continue
                else:
                    tmp1.append(land[0][i]+tmp[j])
            land[0][i]=max(tmp1)

    return max(list(land[0]))
land = [[1,2,3,5],[5,6,7,8],[4,3,2,1]]
print(solution(land))

코로나 기간 알고/프로그래머스/땅따먹기.py


import time
import numpy as np
import numpy.matlib as matlib
from scipy.optimize import dual_annealing

class maxpro_design:
    def __init__(self):
        self.s = 2.0
        self.n = 8
        self.p = 2
        self.random_seed = np.random.RandomState(100)
        self.no_local_search = True
        self.L_BFGS_B_eps = 1e-4
        self.L_BFGS_B_maxiter = 10
        self.dual_annealing_maxiter = 2000
        self.x = None

    def objective(self, xin):
        obj = 0.0
        n = self.n
        p = self.p
        x = xin.reshape((n,p))
        for idx in range(0, n-1): # 0 1 2 ... n-2
            for jdx in range(idx+1, n): # idx+1 idx+2 ... n-1
                obj += 1.0/np.prod(np.abs(x[idx,:] - x[jdx,:])**self.s)
        return obj

    def solve(self):
        res = dual_annealing(
            self.objective,
            bounds = [(0.0, 1.0)]*(self.n*self.p),
            maxiter = self.dual_annealing_maxiter,
            local_search_options = {
                'method': 'L-BFGS-B',
                'bounds': [(0.0, 1.0)]*(self.n*self.p),
                'options': {
                    'eps': self.L_BFGS_B_eps,
                    'maxiter': self.L_BFGS_B_maxiter,
                },
            },
            seed = self.random_seed,
            no_local_search = self.no_local_search,
        )
        self.x = res.x.reshape((self.n, self.p))
        return self.x

if __name__ == '__main__':

    start_time = time.time()

    mpd = maxpro_design()
    mpd.n = 8
    mpd.p = 2
    mpd.s = 2.0
    x = mpd.solve()

    print('Execution time: {:} seconds'.format(time.time() - start_time))

    from matplotlib import pyplot as plt
    fig = plt.figure()
    for idx in range(0, mpd.n):
        plt.plot(x[idx,0],x[idx,1],'ks')
    plt.show()


import urlparse
import ujson as json
import sys
import time
import itertools
import getopt

from shutil import copyfile

import boto
import warc

from boto.s3.key import Key
from gzip import GzipFile
from mrjob.job import MRJob
from mrjob.launch import _READ_ARGS_FROM_SYS_ARGV
from mrjob.step import MRStep
from mrjob.protocol import RawValueProtocol, JSONProtocol, TextValueProtocol

class MRCalculateLinkGraph(MRJob):
    HADOOP_INPUT_FORMAT = 'org.apache.hadoop.mapred.lib.NLineInputFormat'
    INPUT_PROTOCOL = RawValueProtocol

    def map_linkgraph(self, _, commoncrawl_file):
        if '\t' in commoncrawl_file:
            commoncrawl_file = commoncrawl_file.split('\t')[1]

        try:
            conn = boto.connect_s3(anon=True)
            bucket = conn.get_bucket('aws-publicdatasets', validate=True)
            key = Key(bucket, commoncrawl_file)

            import tempfile
            fp = tempfile.NamedTemporaryFile('w+b')
            content = key.read(4096)
            while content:
                fp.write(content)
                content = key.read(4096)
            fp.seek(0)

            f = warc.WARCFile(fileobj=GzipFile(fileobj=fp, mode='rb'))
            for record in f:
                if record['content-type'] != 'application/json':
                    continue

                payload = record.payload.read()

                try:
                    page_info = json.loads(payload)
                    page_url = page_info['Envelope']['WARC-Header-Metadata']['WARC-Target-URI']
                    page_domain = urlparse.urlparse(page_url).netloc
                    links = page_info['Envelope']['Payload-Metadata']['HTTP-Response-Metadata']['HTML-Metadata']['Links']
                    domains = set(filter(None, [urlparse.urlparse(url['url']).netloc for url in links]))

                    if len(domains) > 0:
                        yield page_domain, list(domains)
                except (KeyError, UnicodeDecodeError):
                    pass

            f.close()
        except:
            pass

    def reduce_linkgraph(self, domain, links):
        links = list(set(itertools.chain(*links)))
        yield domain, {'id': domain, 'state': 0, 'outgoing': links}

    def map_normalize(self, domain, data):
        yield data['id'], data

        for outgoing in data['outgoing']:
            yield outgoing, None

    def reduce_normalize(self, domain, data):
        self.increment_counter('linkgraph', 'size', 1)
        try:
            node = list(filter(lambda x: x is not None, data))[0]
        except IndexError:
            node = {'id': domain, 'state': 0, 'outgoing': []}

        yield domain, node

    def steps(self):
        return [MRStep(mapper=self.map_linkgraph, reducer=self.reduce_linkgraph),
                MRStep(mapper=self.map_normalize, reducer=self.reduce_normalize)]

if __name__ == '__main__':
    MRCalculateLinkGraph().run()


import os
import boto3
import subprocess
import threading
from flask import request

class Videofunc:
    def __init__(self):
        self.local = os.path.dirname(__file__)
        self.dir = os.path.dirname(self.local)
        self.strlocal = self.dir + "/videostreaming/downloadedvideo.mp4"
        self.s3c = boto3.client("s3")  # 비디오 다운용
        self.s3r = boto3.resource("s3")  # 비디오 업용
        self.bucket_name = "mobles3"
        self.bucket = self.s3r.Bucket(self.bucket_name)

    def video_init(self):
        return self.s3c, self.s3r, self.bucket, self.bucket_name

    def folder_make(self, mode, NorC, file):
        if mode == "down":
            if not os.path.exists(self.dir + "/videostreaming"):  # 없으면 만들어
                os.makedirs(self.dir + "/videostreaming")
            for file_name in os.listdir(self.dir + "/videostreaming"):
                file_path = os.path.join(self.dir + "/videostreaming", file_name)
                if os.path.isfile(file_path):
                    os.remove(file_path)  # 파일 삭제

        elif mode == "up":
            if not os.path.exists(self.dir + "/videoupload"):  # 없으면 만들어
                os.makedirs(self.dir + "/videoupload")
            file.save(self.dir + "/videoupload/" + file.filename)

            if NorC == "normal":
                self.normal_upload(file)
            elif NorC == "crash":
                self.crash_upload(file)

            file_path = self.dir + "/videoupload/" + file.filename
            os.remove(file_path)

    def incord_thread(self):
        # 입력 파일 경로
        input_file = self.dir + "/videostreaming/downloadedvideo.mp4"

        # 출력 파일 경로
        output_file = self.dir + "/videostreaming/streamingvideo.mp4"

        # ffmpeg 명령어
        command = f"ffmpeg -i {input_file} -c:v libx264 -preset veryfast -crf 22 -c:a copy {output_file}"

        # ffmpeg 실행
        subprocess.call(command, shell=True)

    def incord(self):
        # 쓰레드 생성
        t = threading.Thread(
            target=self.incord_thread
        )  # target=self.incord_thread 여기다 () 달아버리면 쓰레드 정상 작동 안됨
        t.start()

    def normal_download(self, strvideodate):
        self.bucket.download_file(
            "normalvideo/" + strvideodate,
            self.strlocal,
        )

    def crash_download(self, strvideodate):
        self.bucket.download_file(
            "crashvideo/" + strvideodate,
            self.strlocal,
        )

    def normal_upload(self, file):
        # 파일 ec2에서 s3로 업로드하기
        local_file = self.dir + "/videoupload/" + file.filename
        obj_file = "normalvideo/" + file.filename  # S3 에 올라갈 파일명
        self.bucket.upload_file(local_file, obj_file)

    def crash_upload(self, file):
        # 파일 ec2에서 s3로 업로드하기
        local_file = self.dir + "/videoupload/" + file.filename
        obj_file = "crashvideo/" + file.filename  # S3 에 올라갈 파일명
        self.bucket.upload_file(local_file, obj_file)

    def generate_presigned_url(self, mode, object_name, strvideodate, expiration=3600):
        if mode == "normal":
            response = self.s3c.generate_presigned_url(
                "get_object",
                Params={"Bucket": "mobles3", "Key": "normalvideo/" + strvideodate},
                ExpiresIn=expiration,
            )
        elif mode == "crash":
            response = self.s3c.generate_presigned_url(
                "get_object",
                Params={"Bucket": "mobles3", "Key": "crashvideo/" + strvideodate},
                ExpiresIn=expiration,
            )
        return response


from scipy.stats import kstest
from create_plots import *

def plot_power(raw_df,dir,name):
    for d in [1]:
        df = raw_df.sort_values(['alp'])
        # for alp in [0.00, 0.02, 0.04, 0.06, 0.08, 0.10]:
        for n in [1000,5000,10000]:
            subset_3 = df[df['n']==n]
            a,b,e = calc_error_bars(subset_3['alp=0.05'],alpha=0.05,num_samples=100)
            plt.plot('alp','alp=0.05',data=subset_3,linestyle='--', marker='o',label=r'$n'+f'={n}$')
            plt.fill_between(subset_3['alp'], a, b, alpha=0.1)
        plt.hlines(0.05, 0, 0.1)
        plt.legend(prop={'size': 10})
        plt.xticks(rotation=90)
        plt.xticks([0.0,0.005,0.01,2*1e-2,4*1e-2,6*1e-2,8*1e-2,1e-1])
        plt.xlabel(r'$\beta_{XY}$')
        plt.ylabel(r'Power $\alpha=0.05$')
        plt.savefig(f'{dir}/{name}_hde.png',bbox_inches = 'tight',pad_inches = 0.05)
        plt.clf()

def calc_power(vec, level=.05):
    n = vec.shape[0]
    pow = np.sum(vec<=level)/n
    return pow

bench_res_dir = '1d_cont_pow'
benchmark_data = pd.read_csv('hdm_bench_syntehtic_cont.csv')
if not os.path.exists(bench_res_dir):
    os.makedirs(bench_res_dir)
# print(benchmark_data)
bench_extract_cols = list(str(el) for el in range(1,101))

pow_and_calib = []
for i,row in benchmark_data.iterrows():
    data_row = []
    p_vals = row[bench_extract_cols].values.astype(float).squeeze()
    for lvl in [0.001,0.01,0.025,0.05,0.1]:
        pow = calc_power(p_vals,lvl)
        data_row.append(pow)
    stat,p_val_ks_test = kstest(p_vals,'uniform')
    data_row.append(stat)
    data_row.append(p_val_ks_test)
    pow_and_calib.append(data_row)

df_calib_pow = pd.DataFrame(pow_and_calib,columns=['p_a=0.001','p_a=0.01' ,'p_a=0.025' , 'p_a=0.05' ,'p_a=0.1','KS stat','KS pval'])
big_df = pd.concat([benchmark_data,df_calib_pow],axis=1)
big_df.to_csv(f"{bench_res_dir}/pow_and_calib.csv")
# subset = big_df[big_df['null']==False]
# plot_power(subset,bench_res_dir,'pow_plot')


from aiogram import Router, F, Bot
from aiogram.filters import Command
from aiogram.types import Message, ReplyKeyboardRemove
from aiogram.fsm.context import FSMContext
from aiogram.fsm.state import StatesGroup, State

from filters.user_admin import UserAdminFilter
from filters.custom import bot_is_admin

from keyboards.for_choose_channel import choose_channel_kb
from keyboards.for_start import return_to_main_kb
from keyboards.for_start import admin_start_kb

from config_reader import config

from database import Database

router = Router()

database = Database()

class SelectingChannels(StatesGroup):
    selecting_main_channel = State()
    selecting_reserve_channel = State()

class SelectingOffer(StatesGroup):
    selecting_offer = State()

@router.message(
    UserAdminFilter(user_role=["superadmin"]),
    F.text.lower() == "настроить каналы"
)
async def cmd_setup_channels(message: Message, state: FSMContext):
    await message.answer(
        "Начнем настройку каналов.\n"
        "Вам необходимо будет сначала указать основной канал, на который будут приходить подписчики "
        "(чтобы затем его сделать закрытым с входом по заявкам), а также резервный канал, который "
        "сможет сохранить публичную ссылку вашего канала, чтобы ее не занял кто-то другой.",
        reply_markup=choose_channel_kb(channel_type="main")
    )
    await state.set_state(SelectingChannels.selecting_main_channel)

@router.message(
    UserAdminFilter(user_role=["superadmin"]),
    SelectingChannels.selecting_main_channel,
    F.chat_shared
)
async def main_channel_chosen(message: Message, state: FSMContext, bot: Bot):
    await state.update_data(channel_chosen=message.chat_shared.chat_id)
    try:
        if database.add_channel(message.chat_shared.chat_id, "main"):
            if not await bot_is_admin(bot, chat_id=message.chat_shared.chat_id):
                await message.answer(
                    text=f"Бот не является администратором в этом канале.\n"
                         "Повторите установку",
                    reply_markup=return_to_main_kb()
                )
                await state.clear()
            else:
                await message.answer(
                    text=f"Канал успешно выбран в качестве основного. Теперь установите резервный канал:",
                    reply_markup=choose_channel_kb(channel_type="reserve")
                )
                await state.set_state(SelectingChannels.selecting_reserve_channel)
        else:
            await message.answer(
                text=f"Установить основной канал не получилось, попробуйте снова.",
                reply_markup=choose_channel_kb(channel_type="main")
            )
            await state.set_state(SelectingChannels.selecting_main_channel)

    except Exception as e:
        print(e)

@router.message(
    UserAdminFilter(user_role=["superadmin"]),
    SelectingChannels.selecting_reserve_channel,
    F.chat_shared
)
async def reserve_channel_chosen(message: Message, state: FSMContext, bot: Bot):
    await state.update_data(channel_chosen=message.chat_shared.chat_id)
    try:
        if database.add_channel(message.chat_shared.chat_id, "reserve"):
            if not await bot_is_admin(bot, chat_id=message.chat_shared.chat_id):
                await message.answer(
                    text=f"Бот не является администратором в этом резервном канале.\n"
                         "Повторите установку",
                    reply_markup=return_to_main_kb()
                )
                await state.clear()
            else:
                await message.answer(
                    text=f"Канал успешно выбран в качестве резервного."
                         "Теперь можно настроить оффер (описание, которое "
                         "будут видеть юзеры в момент подачи заявки на вступление в канал).\n\n"
                         "Чтобы настроить оффер, пришлите мне его в ответном сообщении.\n"
                         "Например, можно написать так:\n\n"
                         "Чтобы получить крутой файл по программированию с нуля, подавай заявку"
                         " на вступление в канал! Жми кнопку ⬇ ⬇ ⬇",
                    reply_markup=return_to_main_kb()
                )

                await state.set_state(SelectingOffer.selecting_offer)
        else:
            await message.answer(
                text=f"Установить резервный канал не получилось, попробуйте снова.",
                reply_markup=choose_channel_kb(channel_type="reserve")
            )
            await state.set_state(SelectingChannels.selecting_reserve_channel)

    except Exception as e:
        print(e)

@router.message(
    UserAdminFilter(user_role=["superadmin", "admin"]),
    SelectingOffer.selecting_offer
)
async def offer_sent(message: Message, state: FSMContext, bot: Bot):
    await state.update_data(offer=message.text)
    reserve_channel_id = database.get_channel_id_by_type("reserve")
    try:
        result = await bot.set_chat_description(chat_id=reserve_channel_id, description=message.text)
        if result:
            await message.answer(
                text=f"Оффер успешно установлен в описании резервного канала!",
                reply_markup=admin_start_kb(admin_type="admin")
            )

            await state.clear()
        else:
            await message.answer(
                text=f"Оффер установить не получилось!\n"
                     "Убедитесь, что символов в описании не больше 255 и повторите попытку.",
                reply_markup=return_to_main_kb()
            )
    except Exception as e:
        print(e)

@router.message(
    UserAdminFilter(user_role=["superadmin", "admin"]),
    F.text.lower() == "настроить оффер"
)
async def cmd_set_offer(message: Message, state: FSMContext, bot: Bot):
    await message.answer(
        text=f"Чтобы настроить оффер (описание, которое "
             "будут видеть юзеры в момент подачи заявки на вступление в канал),"
             " пришлите мне его в ответном сообщении. "
             "Например, можно написать так:\n\n"
             "Чтобы получить крутой файл по программированию с нуля, подавай заявку"
             " на вступление в канал! Жми кнопку ⬇ ⬇ ⬇",
        reply_markup=return_to_main_kb()
    )

    await state.set_state(SelectingOffer.selecting_offer)


import json

from nextcord.ext import commands

import discordlists

with open('config.txt') as f:
    config = [g.strip('\r\n ') for g in f.readlines()]

class DiscordListsPost(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.api = discordlists.Client(self.bot)  # Create a Client instance
        self.api.set_auth(config[1], config[2])  # Set authorisation token for a bot list
        self.api.start_loop()  # Posts the server count automatically every 30 minutes

    @commands.command()
    async def post(self, ctx: commands.Context):
        """
        Manually posts guild count using discordlists.py (BotBlock)
        """
        try:
            result = await self.api.post_count()
        except Exception as e:
            await ctx.send("Request failed: `{}`".format(e))
            return

        await ctx.send("```json\n{}\n```".format(json.dumps(result)[:1980]))
        await ctx.send("Successfully manually posted server count ({:,}) to {:,} lists."
                       "\nFailed to post server count to {:,} lists.".format(self.api.server_count,
                                                                             len(result["success"].keys()),
                                                                             len(result["failure"].keys())))

def setup(bot):
    bot.add_cog(DiscordListsPost(bot))