from airflow.models import Variable
import datetime
from .test_utils import create_test_database, db_connect
from dags.rock.rock_content_items import ContentItem
from dags.rock.rock_content_items_connections import ContentItemConnection
import vcr

create_test_database()

def test_run_fetch_and_save_content_item_connections(monkeypatch):
    def mock_get(config, deserialize_json=True, default_var=None):
        if default_var:
            return default_var
        if "_rock_api" in config:
            return "https://rock.apollos.app/api"
        if "_rock_token" in config:
            return "ASZjZWdf3IqrbZX9sedtB4wb"
        if "_rock_config":
            return {
                "CONTENT_MAPPINGS": {
                    "ContentSeriesContentItem": {"ContentChannelTypeId": [6]},
                    "DevotionalContentItem": {"ContentChannelId": [7]},
                    "WeekendContentItem": {"ContentChannelId": [5]},
                },
                "PERSONA_CATEGORY_ID": 186,
                "SERIES_CATEGORY_ORIGIN_IDS": [4, 33],
            }

    monkeypatch.setattr(
        Variable,
        "get",
        mock_get,
    )

    content_item = ContentItem(
        {
            "client": "test",
            "execution_date": datetime.datetime(
                2005, 7, 14, 12, 30, tzinfo=datetime.timezone.utc
            ),
            "do_backfill": True,
        }
    )

    content_item_connection = ContentItemConnection(
        {
            "client": "test",
            "execution_date": datetime.datetime(
                2005, 7, 14, 12, 30, tzinfo=datetime.timezone.utc
            ),
            "do_backfill": True,
        }
    )

    monkeypatch.setattr(
        content_item.pg_hook,
        "get_conn",
        db_connect,
    )

    monkeypatch.setattr(
        content_item_connection.pg_hook,
        "get_conn",
        db_connect,
    )

    with vcr.use_cassette(
        "tests/cassettes/content_item_connections/content_items.yaml"
    ):
        content_item.run_fetch_and_save_content_items()
    with vcr.use_cassette(
        "tests/cassettes/content_item_connections/initial_content_item_connections.yaml"
    ):
        content_item_connection.run_fetch_and_save_content_items_connections()

    conn = db_connect()
    with conn:
        with conn.cursor() as curs:
            # Check for initial parent content item
            curs.execute("SELECT id FROM content_item")

            parent_item_id = curs.fetchone()[0]

            # Check that initial content item connections are correct
            curs.execute(
                """
                SELECT parent_id, origin_id FROM content_item_connection;
                """
            )
            initial_content_item_connections = curs.fetchall()

            assert len(initial_content_item_connections) == 3

            expected = [
                (parent_item_id, "20"),
                (parent_item_id, "18"),
                (parent_item_id, "19"),
            ]

            i = 0
            for connection in initial_content_item_connections:
                assert connection == expected[i]
                i += 1

            # Delete content item connection
            with vcr.use_cassette(
                "tests/cassettes/content_item_connections/delete_content_item_connection.yaml"
            ):
                content_item_connection.run_delete_content_item_connections()

            curs.execute("SELECT parent_id, origin_id FROM content_item_connection;")
            content_item_connections_with_deletion = curs.fetchall()

            assert len(content_item_connections_with_deletion) == 2

            expected = [
                (parent_item_id, "20"),
                (parent_item_id, "18"),
            ]

            i = 0
            for connection in content_item_connections_with_deletion:
                assert connection == expected[i]
                i += 1

    conn.close()


import networkx as nx
import re

def read_file(file):
    first = set()
    second = set()
    G = nx.DiGraph()
    prog = re.compile("Step ([A-Z]) must be finished before step ([A-Z]) can begin.")
    with open(file) as f:
        lines = f.readlines()
    for line in lines:
        r = prog.match(line.strip())
        if not r.group(1) in G:
            G.add_node(r.group(1))
        if not r.group(2) in G:
            G.add_node(r.group(2))
        if not G.has_edge(r.group(1),r.group(2)):
            G.add_edge(r.group(1),r.group(2))
        first.add(r.group(1))
        second.add(r.group(2))
    return (G,first- second)

def duration(step):
    return 60+ord(step)-64

def day7(file):
    G,starter = read_file(file)
    path = list()
    to_visit = sorted(starter,reverse=True)

    while len(to_visit) > 0:
        node = to_visit.pop()
        path.append(node)
        neighbours = G[node]
        for n in neighbours:
            if not n in to_visit and not n in path:
                allCompleted = True
                for u,v in G.in_edges(nbunch=n):
                    if not u in path:
                        allCompleted = False
                if allCompleted:
                    to_visit.append(n)
        to_visit = sorted(to_visit,reverse=True)
    #print("".join(path))
    work_route = "".join(path)

    end_letter = path[-1]
    path = list()
    to_visit = sorted(starter,reverse=True)

    second = 0
    workers = list()
    # Trabajo Actual, segundo que termina
    workers.append(['.',0])
    workers.append(['.',0])
    workers.append(['.',0])
    workers.append(['.',0])
    workers.append(['.',0])
    def full_workers(workers):
        full = True
        for w in workers:
            if w[0] == ".":
                full = False
        return full
    end = False
    while not end:
        if len(to_visit) == 0 or full_workers(workers):
            second += 1
        for i in range(0,len(workers)):
            if workers[i][1] <= second:
                if workers[i][0] != ".":
                    path.append(workers[i][0])
                    neighbours = G[workers[i][0]]
                    for n in neighbours:
                        if not n in to_visit and not n in path:
                            allCompleted = True
                            for u,v in G.in_edges(nbunch=n):
                                if not u in path:
                                    allCompleted = False
                            if allCompleted:
                                to_visit.append(n)
                    to_visit = sorted(to_visit,reverse=True)
                if workers[i][0] == end_letter:
                    #print("Finish point")
                    #print("Seconds: %d" % second)

                    end = True
                if len(to_visit) > 0:
                    node = to_visit.pop()
                    workers[i][1] = second+duration(node)
                    workers[i][0] = node
                else:
                    workers[i][0] = "."
    return work_route,second


import os
from django.core.management.base import BaseCommand, CommandError
from main.settings import BASE_DIR, DEBUG

from costcenter.models import Fund, Source, CostCenter, FundCenter, FinancialStructureManager
from lineitems.models import LineForecast, LineItem

class Command(BaseCommand):
    """
    A class to be used only for development purposes.  It serves to fill in some funds, sources, fund centers and cost centers.  Values fed in the database should match what is expected from the data to be used when running uploadtocsv which also uses test encumbrance report data.
    """

    def handle(self, *args, **options):
        if DEBUG:
            LineForecast.objects.all().delete()
            LineItem.objects.all().delete()
            CostCenter.objects.all().delete()
            Source.objects.all().delete()
            Fund.objects.all().delete()
            FundCenter.objects.all().delete()
            self.set_fund()
            self.set_source()
            self.set_fund_center()
            self.set_cost_center()
        else:
            print("This capability is only available when DEBUG is True")

    def set_fund(self):
        items = [
            {"fund": "C113", "name": "National Procurement", "vote": "1"},
            {"fund": "C116", "name": "Kitchen Procurement", "vote": "5"},
            {"fund": "C523", "name": "Basement Procurement", "vote": "1"},
            {"fund": "CXXX", "name": "Bedroom Procurement", "vote": "1"},
        ]
        for item in items:
            try:
                found = Fund.objects.get(fund=item["fund"])
                if found:
                    print(f"Fund {found} exists")
            except Fund.DoesNotExist:
                new_item = Fund.objects.create(**item)
                print(f"Created fund {new_item}")

    def set_source(self):
        items = [{"source": "Kitchen"}]

        for item in items:
            try:
                found = Source.objects.get(source=item["source"])
                if found:
                    print(f"Source {found} exists")
            except Source.DoesNotExist:
                new_item = Source.objects.create(**item)
                print(f"Created Source {new_item}")

    def set_fund_center(self):
        # Create root FC
        fc = {"fundcenter": "1111AA", "shortname": "root", "parent": None}
        new_item = FundCenter.objects.create(**fc)
        root = FundCenter.objects.filter(fundcenter="1111AA").first()
        print(f"Created Fund Center {root}, sequence {root.sequence}")

        root_children = [
            {"fundcenter": "1111AB", "shortname": "AB", "parent": root},
            {"fundcenter": "1111AC", "shortname": "AC", "parent": root},
        ]
        for item in root_children:
            try:
                found = FundCenter.objects.get(fundcenter=item["fundcenter"])
                if found:
                    print(f"Fund Center {found} exists")
            except FundCenter.DoesNotExist:
                item["sequence"] = FinancialStructureManager().set_parent(fundcenter_parent=root)
                new_item = FundCenter.objects.create(**item)
                print(f"Created Fund Center {new_item}, sequence {new_item.sequence}")

        ab = FundCenter.objects.filter(fundcenter="1111AB").first()
        ab_children = [
            {"fundcenter": "2222BA", "shortname": "BA", "parent": ab},
            {"fundcenter": "2222BB", "shortname": "BB", "parent": ab},
        ]
        for item in ab_children:
            try:
                found = FundCenter.objects.get(fundcenter=item["fundcenter"])
                if found:
                    print(f"Fund Center {found} exists")
            except FundCenter.DoesNotExist:
                item["sequence"] = FinancialStructureManager().set_parent(fundcenter_parent=ab)
                new_item = FundCenter.objects.create(**item)
                print(f"Created Fund Center {new_item}")

    def set_cost_center(self):
        fund = Fund.objects.get(fund="C113")
        source = Source.objects.get(source="Kitchen")
        ab = FundCenter.objects.get(fundcenter="1111AB")
        ac = FundCenter.objects.get(fundcenter="1111AC")
        FSM = FinancialStructureManager()
        items = [
            {
                "costcenter": "8486B1",
                "shortname": "Utensils",
                "fund": fund,
                "source": source,
                "isforecastable": True,
                "isupdatable": True,
                "note": "",
                "parent": ac,
            },
            {
                "costcenter": "8486C1",
                "shortname": "Food and drink",
                "fund": fund,
                "source": source,
                "isforecastable": True,
                "isupdatable": True,
                "note": "A quick and short note for 1234FF",
                "parent": ab,
            },
            {
                "costcenter": "8486C2",
                "shortname": "Basement Stuff",
                "fund": fund,
                "source": source,
                "isforecastable": True,
                "isupdatable": True,
                "note": "",
                "parent": ab,
            },
        ]
        for item in items:
            try:
                found = CostCenter.objects.get(costcenter=item["costcenter"])
                if found:
                    print(f"Cost Center {found} exists")
            except CostCenter.DoesNotExist:
                item["sequence"] = FSM.set_parent(item["parent"], item)
                new_item = CostCenter.objects.create(**item)
                print(f"Created Cost Center {new_item}")


from odoo import models, fields, api
from odoo.exceptions import ValidationError
from odoo.http import request

from odoo.addons.resident_management.enum import STATUS_TYPES, VEHICLE_TYPES, USER_GROUP_CODE, RELATIONSHIP_TYPES

str_bql = USER_GROUP_CODE[2][0]
str_bqt = USER_GROUP_CODE[3][0]

class tb_vehicle(models.Model):
    _name = 'tb_vehicle'
    _description = 'Phương tiện'

    license_plates = fields.Char(string='Biển số xe', copy=False)
    vehicle_color = fields.Char(string='Màu xe', copy=False)
    vehicle_brand = fields.Char(string='Nhãn hiệu xe', copy=False)

    image = fields.Image(string='Ảnh phương tiện', copy=False)
    image_citizen_identification_font = fields.Image(string='CMND/CCCD mặt trước', copy=False)
    image_citizen_identification_back = fields.Image(string='CMND/CCCD mặt sau', copy=False)
    image_vehicle_registration_certificate_font = fields.Image(string='Đăng ký xe mặt trước', copy=False)
    image_vehicle_registration_certificate_back = fields.Image(string='Đăng ký xe mặt sau', copy=False)

    name = fields.Char(string='Tên chủ xe', required=True, copy=False)
    date_of_birth = fields.Char(string='Tên chủ xe', copy=False)
    phone = fields.Char(string='Điện thoại', copy=False)
    citizen_identification = fields.Char(string='CMND / CCCD')
    relationship_type = fields.Selection(string='Quan hệ với chủ hộ', selection=RELATIONSHIP_TYPES,
                                         default=RELATIONSHIP_TYPES[0][0])

    vehicle_type = fields.Selection(string='Loại xe', selection=VEHICLE_TYPES, default=VEHICLE_TYPES[0][0])
    note = fields.Text(string='Ghi chú', copy=False, help='')
    status = fields.Selection(string='Trạng thái', selection=STATUS_TYPES, default=STATUS_TYPES[0][0])

    blockhouse_id = fields.Many2one(comodel_name='tb_blockhouse', string="Dự án",
                                    domain=lambda self: self._domain_blockhouse_id(),
                                    ondelete="cascade")
    building_id = fields.Many2one(comodel_name='tb_building', string="Toà nhà",
                                  domain="[('is_active', '=', True), ('blockhouse_id', '!=', None), ('blockhouse_id', '=', blockhouse_id)]",
                                  ondelete="cascade")
    building_house_id = fields.Many2one(comodel_name='tb_building_house', string="Căn hộ",
                                        domain="[('is_active', '=', True), ('building_id', '!=', None), ('building_id', '=', building_id)]",
                                        ondelete="cascade")
    user_id = fields.Many2one(comodel_name='res.users', string="Chủ hộ",
                              domain=lambda self: self._domain_user_id(),
                              ondelete="cascade")

    def set_status_active(self):
        for item in self:
            item.status = 'ACTIVE'

    def set_status_reject(self):
        for item in self:
            item.status = 'REJECT'

    @api.model
    def _domain_user_id(self):
        user = request.env.user
        if user and user.id != 1 and user.id != 2:
            user_ids = (self.env['tb_users_blockhouse_res_groups_rel'].sudo()
                        .search([('building_id', 'in', self.building_house_id)])).user_id.ids
            return ["&", ("active", "=", True), ("id", "in", user_ids)]
        else:
            return [("active", "=", True)]

    @api.model
    def _domain_blockhouse_id(self):
        user = request.env.user
        bqt_bh_id = []  # ban quan tri - blockhouse - id
        bqt_bd_id = []  # ban quan tri - building - id
        bql_bh_id = []  # ban quan ly - blockhouse - id
        bql_bd_id = []  # ban quan ly - building - id
        if user and user.id != 1 and user.id != 2:
            for item in user.tb_users_blockhouse_res_groups_rel_ids:
                if item.group_id.name and str_bqt in item.user_group_code:
                    bqt_bh_id.append(int(item.blockhouse_id.id))
                    bqt_bd_id.append(int(item.building_id.id))
                if item.group_id.name and str_bql in item.user_group_code:
                    bql_bh_id.append(int(item.blockhouse_id.id))
                    bql_bd_id.append(int(item.building_id.id))
            return ["&", ("is_active", "=", True), ("id", "in", list(set(bqt_bh_id + bql_bh_id)))]
        else:
            return [("is_active", "=", True)]

    @api.model
    def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True):
        user = request.env.user
        bqt_bh_id = []  # ban quan tri - blockhouse - id
        bqt_bd_id = []  # ban quan tri - building - id
        bql_bh_id = []  # ban quan ly - blockhouse - id
        bql_bd_id = []  # ban quan ly - building - id
        if user and user.id != 1 and user.id != 2:
            for item in user.tb_users_blockhouse_res_groups_rel_ids:
                if item.group_id.name and str_bqt in item.user_group_code:
                    bqt_bh_id.append(int(item.blockhouse_id.id))
                    bqt_bd_id.append(int(item.building_id.id))
                if item.group_id.name and str_bql in item.user_group_code:
                    bql_bh_id.append(int(item.blockhouse_id.id))
                    bql_bd_id.append(int(item.building_id.id))
            domain.append(('building_id', 'in', list(set(bqt_bd_id + bql_bd_id))))
            domain.append(('blockhouse_id', 'in', list(set(bqt_bh_id + bql_bh_id))))
        res = super(tb_vehicle, self).read_group(domain, fields, groupby, offset=offset, limit=limit,
                                                 orderby=orderby, lazy=lazy)
        return res

    @api.model
    def search_read(self, domain=None, fields=None, offset=0, limit=10, order=None):
        user = request.env.user
        bqt_bh_id = []  # ban quan tri - blockhouse - id
        bqt_bd_id = []  # ban quan tri - building - id
        bql_bh_id = []  # ban quan ly - blockhouse - id
        bql_bd_id = []  # ban quan ly - building - id
        if user and user.id != 1 and user.id != 2:
            for item in user.tb_users_blockhouse_res_groups_rel_ids:
                if item.group_id.name and str_bqt in item.user_group_code:
                    bqt_bh_id.append(int(item.blockhouse_id.id))
                    bqt_bd_id.append(int(item.building_id.id))
                if item.group_id.name and str_bql in item.user_group_code:
                    bql_bh_id.append(int(item.blockhouse_id.id))
                    bql_bd_id.append(int(item.building_id.id))
            domain.append(('building_id', 'in', list(set(bqt_bd_id + bql_bd_id))))
            domain.append(('blockhouse_id', 'in', list(set(bqt_bh_id + bql_bh_id))))
        res = super(tb_vehicle, self).search_read(domain, fields, offset, limit, order)
        return res

    def open_edit_form(self):
        per_name = 'perm_write_vehicle'
        error_messenger = 'Bạn chưa được phân quyền này!'
        can_do = self.check_permission(per_name, raise_exception=False)
        if can_do:
            return {
                'type': 'ir.actions.act_window',
                'name': 'Sửa phương tiện',
                'res_model': 'tb_vehicle',
                'res_id': self.id,
                'view_type': 'form',
                'view_mode': 'form',
                'view_id': self.env.ref('apartment_service_support.view_tb_vehicle_form').id,
                'context': {'form_view_initial_mode': 'edit'},
                'target': 'current',
            }
        raise ValidationError(error_messenger)

    def open_edit_approve_form(self):
        per_name = 'perm_approve_vehicle'
        error_messenger = 'Bạn chưa được phân quyền này!'
        can_do = self.check_permission(per_name, raise_exception=False)
        if can_do:
            return {
                'type': 'ir.actions.act_window',
                'name': 'Sửa phương tiện',
                'res_model': 'tb_vehicle',
                'res_id': self.id,
                'view_type': 'form',
                'view_mode': 'form',
                'view_id': self.env.ref('apartment_service_support.view_tb_vehicle_approve_form').id,
                'context': {'form_view_initial_mode': 'edit'},
                'target': 'current',
            }
        raise ValidationError(error_messenger)

    def confirm_delete(self):
        per_name = 'perm_delete_vehicle'
        error_messenger = 'Bạn chưa được phân quyền này!'
        can_do = self.check_permission(per_name, raise_exception=False)
        if can_do:
            message = """Bạn có chắc muốn xóa bản ghi này?"""
            value = self.env['dialog.box.confirm'].sudo().create({'message': message})
            return {
                'type': 'ir.actions.act_window',
                'name': 'Xóa bản ghi',
                'res_model': 'dialog.box.confirm',
                'view_type': 'form',
                'view_mode': 'form',
                'target': 'new',
                'res_id': value.id
            }
        raise ValidationError(error_messenger)

    def del_record(self):
        for record in self:
            record.unlink()
            pass


import argparse
import os
import importlib.util
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import json
from collections import defaultdict

import utils
import transformers

parser = argparse.ArgumentParser()
parser.add_argument('--task')
parser.add_argument('--model')
parser.add_argument('--dataset')
parser.add_argument('--k', default='0')
parser.add_argument('--mode', default='all')
parser.add_argument('--prompt', default='qa')
parser.add_argument('--debug', action='store_true')
parser.add_argument('--repeats', default=1, type=int)
parser.add_argument('--output', default='plot.png')
parser.add_argument('--device', default='cpu')
args = parser.parse_args()

os.environ["DEVICE"] = args.device

# Check if submission module is present.  If it is not, then main() will not be executed.
use_submission = importlib.util.find_spec('submission') is not None
if use_submission:
    import submission

def plot():
    dataset = 'xsum'
    data = defaultdict(lambda: defaultdict(list))
    model = 'med'
    mode = 'lora16'
    x_vals = set()
    for k in [0,1,8,128]:
        fn = '_'.join([model, dataset, str(k), mode])
        id_ = '_'.join([model, dataset, mode])
        with open(f'submission/results/ft/{fn}.json', 'r') as f:
            score = json.load(f)['metric']
            data[id_]['x'].append(k)
            x_vals.add(k)
            data[id_]['y'].append(score)

    prompt_mode = 'tldr'
    for k in [0,1,4]:
        fn = '_'.join([model, dataset, str(k), prompt_mode])
        id_ = '_'.join([model, dataset, prompt_mode])
        with open(f'submission/results/icl/{fn}.json', 'r') as f:
            score = json.load(f)['metric']
            data[id_]['x'].append(k)
            x_vals.add(k)
            data[id_]['y'].append(score)

    for k, v in data.items():
        plt.plot(v['x'], v['y'], label=k)
    if max(x_vals) > 4:
        plt.xscale('symlog')

    ax = plt.gca()
    ax.xaxis.set_major_formatter(mticker.ScalarFormatter())
    ax.xaxis.set_ticks(sorted(x_vals))
    plt.legend()
    plt.title(dataset)
    plt.ylabel(utils.metric_for_dataset(dataset))
    plt.xlabel('Number of support examples')
    plt.savefig(args.output, bbox_inches='tight')

# Download all models and datasets required by the grader
def cache():

    models = [
        {'name': 'bert-tiny', 'type':  transformers.AutoModelForSequenceClassification, 'num_labels': 5},
        {'name': 'bert-tiny', 'type':  transformers.AutoModelForCausalLM},
        {'name': 'med', 'type':  transformers.AutoModelForCausalLM}
    ]

    for model in models:
        if 'num_labels' in model:
            utils.get_model_and_tokenizer(model['name'], model['type'], num_labels = model['num_labels'])
        else:
            utils.get_model_and_tokenizer(model['name'], model['type'])

    datasets = [
        {'name': 'amazon', 'n_train': 1, 'n_val': 125},
        {'name': 'xsum', 'n_train': 8, 'n_val': 125}
    ]

    for dataset in datasets:
        utils.get_dataset(dataset=dataset['name'], n_train=dataset['n_train'], n_val=dataset['n_val'])

def run():
    ks = [int(k) for k in args.k.split(',')]
    if args.task == 'run_ft':
        submission.run_ft(args.model.split(','), args.dataset.split(','), ks, args.mode.split(','), args.debug, args.repeats)
    elif args.task == 'run_icl':
        submission.run_icl(args.model.split(','), args.dataset.split(','), ks, args.prompt.split(','), args.debug, args.repeats)
    elif args.task == 'plot_ft':
        submission.plot_ft(args.model.split(','), args.dataset.split(','), ks, args.mode.split(','), args.output)
    elif args.task == 'plot_icl':
        assert ',' not in args.dataset, "Only one dataset at a time for plotting"
        submission.plot_icl(args.model.split(','), args.dataset, ks, args.prompt.split(','), args.output)
    elif args.task == 'plot':
        plot()
    elif args.task == 'cache':
        cache()

if __name__ == '__main__':
    run()


import wordgenerator

NUMBER_OF_WORDS = int(input("How many random words do you want? "))
NUMBER_OF_LETTERS = int(input("How many letters do you want the random words to have? "))

user_options = list()
for i in range(0 , NUMBER_OF_LETTERS):
    user_choice = input("What letter " + str(i + 1) + " do you want? Enter 'v' for vowels, 'c' for consonants, 'l' for any letter: ")
    user_options.append(user_choice)

for i in range(0, NUMBER_OF_WORDS):
    word = wordgenerator.assemble_word(user_options)
    print(word)


import sys
sys.stdin = open('input/19236-1.txt') # 33, 43, 76, 39

import copy

def find_fish(x, info):
    for i in range(4):
        for j in range(4):
            if info[i][j][0] == x:
                return i, j
    return -1, -1

def bfs(x, y, info, total):
    global answer
    total += info[x][y][0] + 1
    direction = info[x][y][1]
    info[x][y] = [-1, -1]

    for i in range(16):
        fx, fy = find_fish(i, info)
        if (fx, fy) == (-1, -1):
            continue

        for _ in range(8):
            dx, dy = fx + delta[info[fx][fy][1]][0], fy + delta[info[fx][fy][1]][1]
            if not (0 <= dx < 4 and 0 <= dy < 4) or (dx == x and dy == y):
                info[fx][fy][1] = (info[fx][fy][1] + 1) % 8
                continue
            info[fx][fy], info[dx][dy] = info[dx][dy], info[fx][fy]
            break

    for j in range(1, 4):
        move = False
        ni, nj = x + delta[direction][0] * j, y + delta[direction][1] * j
        if 0 <= ni < 4 and 0 <= nj < 4 and info[ni][nj][0] != -1:
            bfs(ni, nj, copy.deepcopy(info), total)
            move = True
    if not move:
        answer = max(total, answer)

info = [[0] * 4 for _ in range(4)]
for i in range(4):
    temp = list(map(int, input().split()))
    for j in range(4):
        info[i][j] = [temp[2 * j] - 1, temp[2 * j + 1] - 1]

delta = ((-1, 0), (-1, -1), (0, -1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 1))
answer = 0
bfs(0, 0, info, 0)
print(answer)


import glob
import json

def LoadTweets(directory):
    directory = directory +"/*json"
    files = glob.glob(directory)[:100]
    twts = [a for fl in files for a in json.load(open(fl))]
    twts.sort(key=lambda x: x['id'] if 'id' in x else int(x['id_str']) if 'id_str' in x else 0)
    twts = [a for a in twts if 'id' in a or 'str_id' in a]
    twts = [a for a in twts
            if (('id' in a and a['id'] >= 656971539691257900) or
                ('id_str' in a and int(a['id_str']) >= 656971539691257900))]
    res = []
    prev_id = 0
    for tw in twts:
        if 'id' not in tw: tw['id'] = int(tw['id_str'])
        if 'id_str' not in tw: tw['id_str'] = str(tw['id'])
        if tw['id'] != prev_id: res.append(tw)
        prev_id = tw['id']

    return res

def LoadUsers(directory):
    directory = directory + "/*txt"
    usrfiles = glob.glob(directory)
    users = [(a.strip("@"),fl) # Removing the @
             for fl in usrfiles
             for nms in open(fl)
             for a in nms.strip().split(",")]
    usrset = set([a[0] for a in users])
    return usrset, users


from argparse import ArgumentParser, RawTextHelpFormatter
from glob import glob
from subprocess import check_call
import os
from shutil import rmtree

def compile_clm():

    # Define and parse command line arguments
    # ---------------------------------------

    dsc = "Compile CLM on Piz Daint. A case will be created in a subfolder of your ${SCRATCH}.\n"\
          " WARNING: tool has to be run from the default Prg-Env-cray environment"
    parser = ArgumentParser(description=dsc, formatter_class=RawTextHelpFormatter)
    parser.add_argument('cesm_trunk', help="path to the CESM directory")
    parser.add_argument('--clm_version', choices=['4.0', '4.5'], default='4.0', help="CLM version")
    parser.add_argument('-c', '--compiler', help="compiler to use (default: pgi)", default='pgi')
    parser.add_argument('-v', '--compiler_version', help="switch to this version of the compiler\n"\
                        "This is not recommended by CSCS")
    parser.add_argument('-d', '--debug', help="compile in debug mode (default: false)",
                        action='store_true')
    parser.add_argument('--src_mod', action='append',
                        help="path to additionnal/modified sources (e.g. oasis interface)\n"\
                        "has to be a folder containing src.xxx subfolders, can be specified several times")
    parser.add_argument('-o', '--output', help="output executable file path (default: ./cesm.exe)",
                        default='./cesm.exe')
    parser.add_argument('--no_exe', help="do not execute build_cesm.bash, leave it to any suited modification before actual compilation.",
                        action='store_false', dest='execute')
    opts = parser.parse_args()

    # Init some variables
    # -------------------

    CESM_TRUNK = opts.cesm_trunk
    EXP = 'clm{:s}_bld'.format(opts.clm_version)
    CASEDIR = os.path.join(os.environ['SCRATCH'], EXP)
    if os.path.exists(CASEDIR):
        rmtree(CASEDIR)
    RES = '1.9x2.5_gx1v6'
    COMP = 'ITEST'
    MACH = 'daint'
    if opts.clm_version == '4.5':
        COMP += 'CLM45'

    out_exe = os.path.abspath(opts.output)
    sourcemods = [os.path.abspath(src_dir) for src_dir in opts.src_mod]

    create_case_fmt = '{:s}/scripts/create_newcase -res {:s} -compset {:s} -mach {:s} -compiler pgi_oas -case {:s}'
    create_case_cmd = create_case_fmt.format(CESM_TRUNK, RES, COMP, MACH, CASEDIR)

    # Build compiling script
    # ----------------------

    with open('build_cesm.bash', mode='w') as script:
        script.write('#!/bin/bash\n')
        script.write('\n')
        script.write('# ----------------------------------------------\n')
        script.write('# Modules\n')
        script.write('# ----------------------------------------------\n')
        script.write('\n')
        if opts.compiler == 'pgi':
            script.write('module switch PrgEnv-cray PrgEnv-pgi\n')
            if opts.compiler_version is not None:
                script.write('module switch pgi pgi/{:s}\n'.format(opts.compiler_version))
        elif opts.compiler == 'intel':
            script.write('module switch PrgEnv-cray PrgEnv-intel\n')
            if opts.compiler_version is not None:
                script.write('module switch intel intel/{:s}\n'.format(opts.compiler_version))
        elif opts.compiler == 'cray' and opts.compiler_version is not None:
            script.write('module switch cce cce/{:s}\n'.format(opts.compiler_version))
        script.write('\n')
        script.write('module load cray-netcdf\n')
        script.write('module load daint-gpu\n')
        script.write('\n')
        script.write('module list\n')
        script.write('\n')
        script.write('# ----------------------------------------------\n')
        script.write('# Create case\n')
        script.write('# ----------------------------------------------\n')
        script.write('\n')
        script.write('{:s}\n'.format(create_case_cmd))
        script.write('\n')
        script.write('# ----------------------------------------------\n')
        script.write('# Setup case\n')
        script.write('# ----------------------------------------------\n')
        script.write('\n')
        script.write('cd {:s}\n'.format(CASEDIR))
        script.write('\n')
        script.write('switch off river routing\n')
        script.write('./xmlchange RTM_MODE="NULL"\n')
        script.write('\n')
        script.write('set transient CO2\n')
        script.write('./xmlchange CCSM_BGC=CO2A,CLM_CO2_TYPE=diagnostic\n')
        if opts.debug:
            script.write('# activate debug mode\n')
            script.write('./xmlchange -file env_build.xml -id DEBUG -val "TRUE"\n')
        script.write('\n')
        script.write('./cesm_setup\n')
        script.write('\n')
        script.write('# ----------------------------------------------\n')
        script.write('# Add source additions/modifications\n')
        script.write('# ----------------------------------------------\n')
        script.write('\n')
        for src_dir in sourcemods:
            print(src_dir)
            for comp in glob('{:s}/src.*'.format(src_dir)):
                print(comp)
                script.write('rsync -avrL {:s} SourceMods\n'.format(comp))
        script.write('\n')
        script.write('# ----------------------------------------------\n')
        script.write('# Build\n')
        script.write('# ----------------------------------------------\n')
        script.write('\n')
        script.write('{:s}.build\n'.format(EXP))
        script.write('rsync -avr bld/cesm.exe {:s}\n'.format(out_exe))

    os.chmod('build_cesm.bash', 0o755)

    # Execute compiling script
    # ------------------------

    if opts.execute:
        check_call(['./build_cesm.bash'])


""" Initializes Pickly Files"""
import pickle
import json
import requests
import urllib.request

def guiinit(sub):

    #Gets information from Reddit
    r = urllib.request.urlopen(r'http://www.reddit.com/r/' + sub + '/new/.json', timeout=60).read().decode("utf-8")
    data = json.loads(r)

    #Creates ists to hold data
    titlelist = []
    urllist = []
    permalinklist = []

    #Creats Files
    title = open("title.obj", 'wb')
    url = open("url.obj", 'wb')
    permalink = open("perma.obj", 'wb')

    #Appends Data from Reddit API to lists
    for i in range(0,20):
        titlelist.append(data['data']['children'][i]['data']['title'])  #Gets title
        urllist.append(data['data']['children'][i]['data']['url'])  #Gets URL
        permalinklist.append("http://www.reddit.com" + data['data']['children'][i]['data']['permalink'])  #Gets Comments

    #Dumps lists to files
    pickle.dump(titlelist, title)
    pickle.dump(urllist, url)
    pickle.dump(permalinklist, permalink)

    #Closes files
    title.close()
    url.close()
    permalink.close()


import sys

#f= open(sys.argv[1], 'r')
# for line in f:
 #    print(line.strip())
#
#
#f2= open(sys.argv[2],'r')
# for line in f2:
#     print(line.strip())
f=open(sys.argv[1])
#create an empty dictionary
flydict= dict()
for line in f:
    fields= line.strip().split('\t')
    #assign gene name and protein id to the dictionary
    flydict[fields[0]]= fields[1]

f2=open(sys.argv[2],'r')
for line in f2:
    fields=line.strip().split('\t')
    print(fields)
    gene_name=fields[8]
    if gene_name in flydict:
        protein_id= flydict[gene_name]
        fields[8]=protein_id
        print(gene_name, protein_id)
        print(fields)
    else:
        if len(sys.argv)==4:
            fields[8]=sys.argv[3]
            print(fields)
        else:
            continue


import rospy
from geometry_msgs.msg import Point
import VariableManager

class MasterRecorder:

    def __init__(self):
        self.recording = VariableManager.recording
        self.file = None
        self.data_to_write = VariableManager.master_data

    def main(self):

        if(not self.recording and VariableManager.recording):
            self.clear_recorded_data()
        self.recording = VariableManager.recording
        if(self.recording):
            self.record_data()

    def clear_recorded_data(self):
        self.file = open("recorded_data.txt", "w")
        self.file.write("")
        self.file.close()

    def record_data(self):
        try:
            self.file = open("recorded_data.txt", "a")
        except:
            self.file = open("recorded_data.txt", "w")
        self.data_to_write = VariableManager.master_data
        self.file.write(f"{self.data_to_write.x},{self.data_to_write.y},{self.data_to_write.z}\n")
        self.file.close()


from typing import Any
from random import randint, random
import pygame as py

from engine.game_engine import Pygame
from engine.color import Color
from engine.game_objects import IGameObject
from engine.game_objects.modules import IAnimationModule, ICollisionModule
from engine.image import SpriteSheet
from engine.ui.text import TypeWriterText
from engine.map import LoopingMap

from src.player.player import Player

class UI_NPC_Text(TypeWriterText):
    def __init__(self, game: Pygame = None):
        super().__init__("We need to push forward! We need to push forward!", (200, 200, 200), game.fonts["8bit"], (135, 590), 10, game=game)

class UI_NPC_Text_Box(IGameObject):
    def __init__(self, game: Pygame = None):
        super().__init__("ui", game)
        self.pos = (120, 577)
        self.add_layer(f"{self.game.game_dir}\\data\\images\\ui\\ui_npc_text_box.png")
        self.rect = self.get_layer_image(0).get_rect(topleft=self.pos)

class UI_NPC_Box(IGameObject):
    def __init__(self, game: Pygame = None):
        super().__init__("ui", game)
        self.pos = (10, 665)
        # self.add_layer(f"{self.game.game_dir}/data/images/npc_board.png")
        self.add_layer(py.Surface((100, 100), flags=py.SRCALPHA))
        self.add_layer(py.Surface((100, 100), flags=py.SRCALPHA))
        self.add_layer(py.Surface((100, 100), flags=py.SRCALPHA))
        # self.add_layer(f"{self.game.game_dir}/data/images/npc_box.png")
        self.npc = ""
        self.npcs = {
            "red": py.image.load(f"{self.game.game_dir}/data/images/ui/npcs/red_npc.png")
        }
        self.anim_atlas = SpriteSheet(f"{self.game.game_dir}/data/images/ui/noise_atlas.png", 100, 100).get_image_array()
        self.rect = self.get_layer_image(1).get_rect(bottomleft=self.pos)

        self.set_module("animation", IAnimationModule(self, False))
        self.get_module("animation").add_animation_by_dict(
            "noise",
            {
                "layer": 1,
                "frames": [self.anim_atlas[0], self.anim_atlas[1], self.anim_atlas[2], self.anim_atlas[3],
                           self.anim_atlas[4], self.anim_atlas[5], self.anim_atlas[6], self.anim_atlas[7]],
                "frame_time": 40,
                "loop": False,
                "callback": self.noise_anim_ended
            }
        )

        self.get_module("animation").add_animation_by_dict(
            "noise_reversed",
            {
                "layer": 1,
                "frames": [self.anim_atlas[7], self.anim_atlas[6], self.anim_atlas[5], self.anim_atlas[4],
                           self.anim_atlas[3], self.anim_atlas[2], self.anim_atlas[1], self.anim_atlas[1]],
                "frame_time": 40,
                "loop": False,
                "callback": self.noise__reversed_anim_ended
            }
        )
        self.text = UI_NPC_Text(self.game)
        self.border = UI_NPC_Text_Box(self.game)
        self.game.objects.add("ui_npc_text", self.text)
        self.game.objects.add("ui_npc_text_box", self.border)
        self.get_module("animation").should_animate = True
        self.get_module("animation").play("noise")

    def noise_anim_ended(self):
        self.npc = "red"
        if self.npc != "":
            self.set_layer(self.npcs[self.npc], layer_id=0)
            self.set_layer(self.anim_atlas[8], layer_id=1)

    def noise__reversed_anim_ended(self):
        self.npc = ""
        self.set_layer(py.Surface((100, 100), py.SRCALPHA), layer_id=0)
        self.set_layer(py.Surface((100, 100), py.SRCALPHA), layer_id=1)

class UI_BG(IGameObject):
    def __init__(self, game=None):
        super().__init__("ui", game)
        self.pos = (0, 675)
        self.add_layer(f"{self.game.game_dir}/data/images/ui_bg.png")
        self.rect = self.get_layer_image(0).get_rect(bottomleft=self.pos)

# Rock Object #
class Rock(IGameObject):
    def __init__(self, game: Pygame):
        super(Rock, self).__init__('env', game)
        self.set_module("collision", ICollisionModule(self, False))
        self.pos = (self.game.screen.get_width(), randint(0, self.game.screen.get_height()))
        self.vel = (randint(-2,-1), randint(-1, 1) * (random() * 0.5))
        spr = py.image.load(f"{self.game.game_dir}/data/images/rock.png").convert_alpha()
        scale = max(25, random() * 48)
        spr = py.transform.scale(spr, (scale, scale))
        spr = py.transform.rotate(spr, randint(-25, 25))
        self.add_layer(spr)
        self.rect = self.get_layer_image(self.primary_layer).get_rect(center=self.pos)
        self.mask = py.mask.from_surface(self.get_layer_image(0))

    def update(self, *args, **kwargs) -> None:
        self.pos = (self.pos[0] + self.vel[0], self.pos[1] + self.vel[1])
        self.rect = self.get_layer_image(0).get_rect(center=self.pos)
        super(Rock, self).update()

class RockSpawner(IGameObject):
    def __init__(self, group: str = "handler", game: Pygame = None):
        super().__init__(group, game)
        self.spawn_timing = 2000
        self.last_spawn_time = self.game.time

    def update(self, *args: Any, **kwargs: Any) -> None:
        if self.last_spawn_time + self.spawn_timing <= self.game.time:
            self.game.objects.add(f"rock_{randint(0, 999999)}", Rock(self.game))
            self.last_spawn_time = self.game.time
        return super().update(*args, **kwargs)

# MINIMAL RUNNING EXAMPLE #
# Main Game Engine Object #
class Game(Pygame):
    def __init__(self):
        super(Game, self).__init__(1200, 675, "Space Game", fps_target=60)
        self.add_group("handler")
        self.add_group("map")
        self.add_group("player")
        self.add_group("player_projectile")
        self.add_group("env")
        self.add_group("enemy")
        self.add_group('ui')
        self.fonts["8bit"] = self.get_font("8-BIT WONDER.ttf", 17)
        # SETUP GAME AXIS CONTROLS (IControlModule().get_axis("move_left"))
        self.axis = {'move_left': {py.K_a: -1, py.K_d: 1}, 'move_up': {py.K_w: -1, py.K_s: 1}}
        py.event.set_grab(True)
        py.mouse.set_pos((100, self.screen.get_height() / 2))
        py.mouse.set_visible(False)
        self.load_data()
        self.start_game_loop()

    def load_data(self):
        super(Game, self).load_data() # Required to set the base dir of the game for easy access in objects without recalculating where to split the path (self.game_dir)
        self.objects['map'] = LoopingMap(self, "data/images/background.png", [50, 0]) # Looping map uses the images size to calculate its looping. You may need to rescale your image to fit your game area (Window scaling will handle it after as long as it covers the initial screen). Supports vertical or horizontal but not both (Hopefully in future revisions)
        self.objects["player"] = Player((0, self.screen.get_height() / 2), self) # Adds a GameObject to the ObjectHandler so that update and draw calls are triggered correctly
        self.objects.add("rock_spawner", RockSpawner(game=self))
        self.objects["ui_bg"] = UI_BG(self)
        self.objects['ui_npc_box'] = UI_NPC_Box(self)
        # self.objects['ui_npc_text'] = UI_NPC_Text(self)

    def draw(self):
        self.screen.fill(Color(1, 1, 1, 1).RGB) # self.screen.fill((255, 255, 255)). Color class is used mostly for storing colors to easily recall but may get more features later
        super(Game, self).draw() # Required to call the draw function for registered objects
        # UNCOMMENT FOR RECT DEBUGGING
        if self.debug:
            for group in self.groups.keys():
                for sprite in self.groups[group]:
                    self.debug.debug_collision(sprite)

        super(Game, self).render_display() # Required to call the render update of the display (py.display.flip())

    def update(self):
        super(Game, self).update()
        for event in py.event.get():
            # COMMENT TO REMOVE MANUAL WINDOW RESIZING SUPPORT
            if event.type == py.VIDEORESIZE:
                self.windowSize = (event.w, event.h)
                py.display._resize_event(event)
            if event.type == py.QUIT:
                self.quit()
            if event.type == py.KEYDOWN:
                # UNCOMMENT TO TOGGLE DEBUGGING
                if event.key == py.K_c: self.debug.set_debug(not self.debug._debug)
                if event.key == py.K_p:
                    npc_ui = self.objects.get("ui_npc_box")
                    if npc_ui.npc == "":
                        npc_ui.get_module("animation").play("noise")
                    else:
                        self.objects["ui_npc_box"].get_module("animation").play("noise_reversed")
                if event.key == py.K_ESCAPE:
                    self.quit()

if __name__ == '__main__':
    g = Game()


from tkinter import *

window = Tk() #need a way of keeping the window on the screen
window.title("First GUI Program")
window.minsize(width = 500, height = 300)
window.config(padx=20,pady=20) #adds more space for our widgets

#Label
my_label = Label(text="taco",font =("Papyrus",20))
my_label["text"] = "cat"
my_label.config(text="tacocat")
my_label.config(padx=50,pady=50)
my_label.grid(column=0,row=0) #packs our label onto the screen

#button
def button_clicked():
    my_label["text"] = input.get()

button = Button(text="Click me",command=button_clicked)
button.grid(column=1,row=1)

#Entry
input = Entry(width = 10)
input.grid(column=3,row=2)
print(input.get())

button = Button(text="No Click me",command=button_clicked)
button.grid(column=2,row=0)

#keeps our window on the screen
window.mainloop()


import typing
from typing import (
    Union,
    Optional,
    List,
)
import asyncio
import logging
from datetime import datetime

from hikari import ActionRowComponent, Embed, MessageCreateEvent, embeds
from hikari import ButtonStyle
from hikari.impl.special_endpoints import MessageActionRowBuilder, LinkButtonBuilder
from hikari.events import InteractionCreateEvent
import lightbulb
import lightbulb.utils as lightbulb_utils
from lightbulb import commands, context
from lightbulb import OptionModifier as OM
from lightbulb.context import Context
import hikari
from matplotlib.style import available
from numpy import full, isin
from fuzzywuzzy import fuzz

from utils import Colors, Human, Paginator, crumble
from core import getLogger, Inu

log = getLogger(__name__)

plugin = lightbulb.Plugin("Voice commands")

@plugin.command
@lightbulb.add_checks(
    lightbulb.has_role_permissions(hikari.Permissions.MOVE_MEMBERS),
    lightbulb.bot_has_role_permissions(hikari.Permissions.MOVE_MEMBERS),
    lightbulb.guild_only,
)
# @lightbulb.option(
#     "member",
#     "a person who is in the current voice channel. normally you",
#     type=hikari.Member,
#     default=None,
# )
@lightbulb.option(
    "from-voice-channel",
    "the voice channel where move peaple of",
    type=hikari.GuildChannel,
    default=None,
)
@lightbulb.option(
    "voice-channel",
    "the voice channel where you want to move to",
    type=hikari.GuildChannel,
)
@lightbulb.command(
    "move-all",
    "moves all members from a current voice channel into another",
    aliases=["move"]
)
@lightbulb.implements(commands.SlashCommand, commands.PrefixCommand)
async def move_all(ctx: Context):
    target_channel: hikari.InteractionChannel = ctx.options["voice-channel"]
    if not target_channel.type == hikari.ChannelType.GUILD_VOICE:
        await ctx.respond(f"{target_channel} is not a voice channel", flags=hikari.MessageFlag.EPHEMERAL)
        return None

    if not ctx.options["from-voice-channel"]:
        member = ctx.member
        states = ctx.bot.cache.get_voice_states_view_for_guild(ctx.guild_id)
        voice_state = [state for state in states.values() if state.user_id == member.id]

        if not voice_state:
            await ctx.respond(f"{member.display_name} needs to be in a voice channel")
            return None

        channel_id = voice_state[0].channel_id
        user_ids = [state.user_id for state in states.values() if state.channel_id == channel_id]
    else:
        user_ids = [
            state.user_id for state in ctx.bot.cache.get_voice_states_view_for_guild(ctx.guild_id).values()
            if state.channel_id == ctx.options["from-voice-channel"].id
        ]

    tasks = [
        asyncio.create_task(
            ctx.bot.rest.edit_member(
                guild=ctx.guild_id,
                user=user_id,
                voice_channel=target_channel.id
            )
        )
        for user_id in user_ids
    ]
    await asyncio.wait(tasks, return_when=asyncio.ALL_COMPLETED)
    await ctx.respond(
        f"Moved {Human.list_([f'<@{user_id}>' for user_id in user_ids], with_a_or_an=False)} to `{target_channel.name}`"
    )

@move_all.autocomplete("voice-channel")
async def tag_name_auto_complete(
    option: hikari.AutocompleteInteractionOption,
    interaction: hikari.AutocompleteInteraction
) -> List[str]:
    vcs = []
    guild = interaction.get_guild()
    if not guild:
        return []
    for ch in guild.get_channels().values():
        if not isinstance(ch, hikari.GuildVoiceChannel):
            continue
        if lightbulb_utils.permissions_in(ch, interaction.member) & hikari.Permissions.CONNECT:
            vcs.append(f"{ch.id} | {ch.name}")
    return vcs[:24]

def load(bot: Inu):
    bot.add_plugin(plugin)


import time
from collections import OrderedDict
from collections.abc import Callable, Sequence
from typing import Any, NamedTuple

DB = "timeseries"
TABLE_RAW = "paii_raw"
TIME_FIELD = "paii_time"

class Field(NamedTuple):
    json_key: str
    store_flag: bool
    db_name: str
    data_type: str
    convert: Callable[[dict[str, Any], str], Any] | None = None

# conversions
def fahrenheit2celsius(data: dict[str, Any], key: str) -> float:
    f = data[key]
    return round((f - 32) * 5 / 9, 2)

def get_response_date(data: dict[str, Any], key: str) -> int:
    return data.get(key, time.time())

def missing_int(data: dict[str, Any], key: str) -> int:
    return data.get(key, -1)

fields = [
    # inserted by the sceduler
    Field(TIME_FIELD, True, TIME_FIELD, "TIMESTAMPTZ NOT NULL"),
    # from the device
    Field("SensorId", False, "SensorId", "VARCHAR"),  # eg "84:f3:eb:7b:c8:ee"
    Field("DateTime", False, "DateTime", "VARCHAR"),  # eg "2020/08/23T10:44:39z"
    Field("Geo", True, "geo", "VARCHAR"),  # eg "PurpleAir-c8ee"
    Field("Mem", False, "Mem", "VARCHAR"),  # eg 18936
    Field("memfrag", False, "memfrag", "VARCHAR"),  # eg 8
    Field("memfb", False, "memfb", "VARCHAR"),  # eg 17568
    Field("memcs", False, "memcs", "VARCHAR"),  # eg 896
    Field("Id", False, "Id", "VARCHAR"),  # eg 4177
    Field("lat", True, "lat", "DOUBLE PRECISION"),  # eg -37.8484
    Field("lon", True, "lon", "DOUBLE PRECISION"),  # eg 145.177399
    Field("Adc", True, "adc", "DOUBLE PRECISION"),  # eg 0.05
    Field("loggingrate", False, "loggingrate", "VARCHAR"),  # eg 15
    Field("place", True, "place", "VARCHAR"),  # eg "outside"
    Field("version", False, "version", "VARCHAR"),  # eg "6.01"
    Field("uptime", False, "uptime", "VARCHAR"),  # eg 242296
    Field("rssi", False, "rssi", "VARCHAR"),  # eg -59
    Field("period", True, "period", "INTEGER"),  # eg 120
    Field("httpsuccess", False, "httpsuccess", "VARCHAR"),  # eg 12169
    Field("httpsends", False, "httpsends", "VARCHAR"),  # eg 12182
    Field("hardwareversion", True, "hardwareversion", "VARCHAR"),  # eg "2.0"
    Field(
        "hardwarediscovered",
        False,
        "hardwarediscovered",
        "VARCHAR",
    ),  # eg "2.0+BME280+PMSX003-B+PMSX003-A"
    Field(
        "current_temp_f",
        True,
        "current_temp_c",
        "DOUBLE PRECISION",
        fahrenheit2celsius,
    ),  # eg 52
    Field("current_humidity", True, "current_humidity", "DOUBLE PRECISION"),  # eg 55
    Field(
        "current_dewpoint_f",
        True,
        "current_dewpoint_c",
        "DOUBLE PRECISION",
        fahrenheit2celsius,
    ),  # eg 36
    Field("pressure", True, "pressure", "DOUBLE PRECISION"),  # eg 1005.28
    Field("p25aqic_b", False, "p25aqic_b", "VARCHAR"),  # eg "rgb(0,228,0)"
    Field("pm2.5_aqi_b", False, "pm2.5_aqi_b", "VARCHAR"),  # eg_aqi_b": 5
    Field("pm1_0_cf_1_b", True, "pm1_0_cf_1_b", "DOUBLE PRECISION"),  # eg 0.39
    Field("p_0_3_um_b", False, "p_0_3_um_b", "VARCHAR"),  # eg 261.79
    Field("pm2_5_cf_1_b", True, "pm2_5_cf_1_b", "DOUBLE PRECISION"),  # eg 1.3 **** µg/m3
    Field("p_0_5_um_b", False, "p_0_5_um_b", "VARCHAR"),  # eg 72.35
    Field("pm10_0_cf_1_b", True, "pm10_0_cf_1_b", "DOUBLE PRECISION"),  # eg 1.72
    Field("p_1_0_um_b", False, "p_1_0_um_b", "VARCHAR"),  # eg 13.05
    Field("pm1_0_atm_b", False, "pm1_0_atm_b", "VARCHAR"),  # eg 0.39
    Field("p_2_5_um_b", False, "p_2_5_um_b", "VARCHAR"),  # eg 2.42
    Field("pm2_5_atm_b", False, "pm2_5_atm_b", "VARCHAR"),  # eg 1.3
    Field("p_5_0_um_b", False, "p_5_0_um_b", "VARCHAR"),  # eg 0.7
    Field("pm10_0_atm_b", False, "pm10_0_atm_b", "VARCHAR"),  # eg 1.72
    Field("p_10_0_um_b", False, "p_10_0_um_b", "VARCHAR"),  # eg 0.0
    Field("p25aqic", False, "p25aqic", "VARCHAR"),  # eg "rgb(0,228,0)"
    Field("pm2.5_aqi", False, "pm2.5_aqi", "VARCHAR"),  # eg_aqi": 1
    Field("pm1_0_cf_1", True, "pm1_0_cf_1", "DOUBLE PRECISION"),  # eg 0.14
    Field("p_0_3_um", False, "p_0_3_um", "VARCHAR"),  # eg 163.63
    Field("pm2_5_cf_1", True, "pm2_5_cf_1", "DOUBLE PRECISION"),  # eg 0.33  **** µg/m3
    Field("p_0_5_um", False, "p_0_5_um", "VARCHAR"),  # eg 45.77
    Field("pm10_0_cf_1", True, "pm10_0_cf_1", "DOUBLE PRECISION"),  # eg 0.42
    Field("p_1_0_um", False, "p_1_0_um", "VARCHAR"),  # eg 7.79
    Field("pm1_0_atm", False, "pm1_0_atm", "VARCHAR"),  # eg 0.14
    Field("p_2_5_um", False, "p_2_5_um", "VARCHAR"),  # eg 0.56
    Field("pm2_5_atm", False, "pm2_5_atm", "VARCHAR"),  # eg 0.33
    Field("p_5_0_um", False, "p_5_0_um", "VARCHAR"),  # eg 0.18
    Field("pm10_0_atm", False, "pm10_0_atm", "VARCHAR"),  # eg 0.42
    Field("p_10_0_um", False, "p_10_0_um", "VARCHAR"),  # eg 0.0
    Field("pa_latency", False, "pa_latency", "VARCHAR"),  # eg 631
    Field("response", False, "response", "VARCHAR"),  # eg 201
    Field(
        "response_date",
        True,
        "response_date",
        "INTEGER",
        get_response_date,
    ),  # eg 1598179477
    Field("latency", True, "latency", "INTEGER", missing_int),  # eg 1459
    Field("key1_response", False, "key1_response", "VARCHAR"),  # eg 200
    Field("key1_response_date", False, "key1_response_date", "VARCHAR"),  # eg 1598179467
    Field("key1_count", False, "key1_count", "VARCHAR"),  # eg 79205
    Field("ts_latency", False, "ts_latency", "VARCHAR"),  # eg 1198
    Field("key2_response", False, "key2_response", "VARCHAR"),  # eg 200
    Field("key2_response_date", False, "key2_response_date", "VARCHAR"),  # eg 1598179470
    Field("key2_count", False, "key2_count", "VARCHAR"),  # eg 79212
    Field("ts_s_latency", False, "ts_s_latency", "VARCHAR"),  # eg 1141
    Field("key1_response_b", False, "key1_response_b", "VARCHAR"),  # eg 200
    Field(
        "key1_response_date_b",
        False,
        "key1_response_date_b",
        "VARCHAR",
    ),  # eg 1598179472
    Field("key1_count_b", False, "key1_count_b", "VARCHAR"),  # eg 79213
    Field("ts_latency_b", False, "ts_latency_b", "VARCHAR"),  # eg 1133
    Field("key2_response_b", False, "key2_response_b", "VARCHAR"),  # eg 200
    Field(
        "key2_response_date_b",
        False,
        "key2_response_date_b",
        "VARCHAR",
    ),  # eg 1598179474
    Field("key2_count_b", False, "key2_count_b", "VARCHAR"),  # eg 79217
    Field("ts_s_latency_b", False, "ts_s_latency_b", "VARCHAR"),  # eg 1136
    Field("wlstate", False, "wlstate", "VARCHAR"),  # eg "Connected"
    Field("status_0", True, "status_0", "INTEGER"),  # eg 2
    Field("status_1", True, "status_1", "INTEGER"),  # eg 2
    Field("status_2", True, "status_2", "INTEGER"),  # eg 2
    Field("status_3", True, "status_3", "INTEGER"),  # eg 2
    Field("status_4", True, "status_4", "INTEGER"),  # eg 2
    Field("status_5", True, "status_5", "INTEGER"),  # eg 2
    Field("status_6", True, "status_6", "INTEGER", missing_int),  # eg 2
    Field("status_7", True, "status_7", "INTEGER"),  # eg 0
    Field("status_8", True, "status_8", "INTEGER"),  # eg 2
    Field("status_9", True, "status_9", "INTEGER"),  # eg 2
    Field("ssid", False, "ssid", "VARCHAR"),  # eg "apocalypse
]

def gen_stored(fields: list[Field] = fields):
    yield from (f for f in fields if f.store_flag)

def compose_create(
    table_name: str,
    time_field: str,
    fields: list[Field] = fields,
) -> str:
    fdesc = ",\n".join([f"{f.db_name} {f.data_type}" for f in gen_stored()])
    sql = f"""CREATE TABLE IF NOT EXISTS {table_name} (
{fdesc},
PRIMARY KEY({time_field})
);
    """
    return sql

def compose_insert(field_names: Sequence, table_name: str) -> str:
    """compose parameterized insert SQL

    Args:
        field_names (Sequence): database table field names
        table_name (str): database table name.

    Returns:
        str: insert SQL.
    """
    fields = ", ".join(field_names)
    placeholders = ", ".join([f"${i+1}" for i in range(len(field_names))])
    sql = f"INSERT INTO {table_name} ({fields}) values ({placeholders})"
    return sql

def convert_data(data: dict[str, Any], fields: list[Field] = fields) -> OrderedDict:
    """return filtered and ordered device data

    Args:
        data (Dict[str, Any]): raw dictionary directly from device.
        fields (List[Field], optional): fields specification

    Returns:
        OrderedDict[str, Any]: {db_key: converted_value, ...} items() will return
        in the same order as SQL commands assuming they are all based on the same
        field list.
    """

    missing = []

    def convert(data: dict[str, Any], field: Field) -> Any:
        if field.convert:
            # custom converstion function, takes care of missing data
            return field.convert(data, field.json_key)
        if field.json_key in data:
            return data[field.json_key]
        else:
            missing.append(field.json_key)
            return None

    missing.clear()
    result = OrderedDict({f.db_name: convert(data, f) for f in gen_stored()})
    # if missing:
    #     print(f"fields were missing: {missing}")
    return result


import os
import time
import subprocess
import shlex
import os
from delete_big_files import deleteBigFilesFor1000experiment

os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"

train_dir = '/home/ubuntu/robust_transfer_learning/'

sample_size_to_number_of_seeds_epochs_and_log_freq = {
    400  : (3, 150, 5),
    1600 : (2, 150, 5),
    6400 : (1, 150, 5),
    25600: (1, 150, 5),
    -1   : (1, 150, 5),
}

target_ds_list = ['food101']
eps_levels = [0, 0.05, 0.25, 1]
num_unfrozen_blocks_list = [3]

polling_delay_seconds = 1
concurrent_commands = 4
commands_to_run = []

def poll_process(process):
    time.sleep(polling_delay_seconds)
    return process.poll()

for t in target_ds_list:
    for ub in num_unfrozen_blocks_list:
        for n, tup in sample_size_to_number_of_seeds_epochs_and_log_freq.items():
            num_seeds, ne, li = tup
            seed_list = [20000000 + 100000*(i) for i in range(num_seeds)]
            for s in seed_list:
                for e in eps_levels:
                    command = f'python train.py -e {e} -t {t} -ub {ub} -n {n} -s {s} -ne {ne} -li {li} -d True'
                    commands_to_run.append(command)

for start_idx in range(0, len(commands_to_run), concurrent_commands):
    os.chdir(train_dir)
    processes = []
    rng = range(start_idx, min(len(commands_to_run), start_idx + concurrent_commands))
    print(rng)
    for i in rng:
        os.environ["CUDA_VISIBLE_DEVICES"] = str(i%4)
        processes.append(subprocess.Popen(shlex.split(commands_to_run[i])))
        print(f'Starting command: {commands_to_run[i]}')

    for process in processes:
        while poll_process(process) is None:
            pass

    deleteBigFilesFor1000experiment()


import os

from flask import Flask
from flask_jwt_extended import JWTManager
from flask_login import LoginManager

from .auth import ldap_handler
from .db import database
from .db.models import *
from .messages import messages
from .mocks import fake_ldap_handler

configuration_switch = {
    "default": "backend.config.DevConfig",  # Development configuration (fake LDAP)
    "staging": "backend.config.StagingConfig",  # Staging configuration (should be as close as possible to prod)
    "production": "backend.config.ProductionConfig",  # Production configuration
}

ENV = os.environ.get("ENV", "default")

# SET UP =====================================

LOGIN_MANAGER = LoginManager()
LOGIN_MANAGER.login_view = "auth.login"
LOGIN_MANAGER.login_message = messages.LOGIN_MANAGER_MESSAGE

LDAP = fake_ldap_handler.FAKE_LDAP if ENV == "default" else ldap_handler.LDAP
DB = database.DB
JWT_MANAGER = JWTManager()

# ===================================================

def create_app(test_configuration=None, test_db=None):
    """Application factory method"""
    app = Flask(__name__, static_folder="../build/static", template_folder="../build")

    # Configure the application
    if test_configuration:
        app.config.update(test_configuration)
    else:
        app.config.from_object(configuration_switch[ENV])

    # Register extensions ################################
    # |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  #
    ######################################################
    LDAP.init_app(app)
    LOGIN_MANAGER.init_app(app)
    JWT_MANAGER.init_app(app)

    if test_configuration:
        test_db.init_app(app)
    else:
        DB.init_app(app)

    ############################################################

    from .views import auth, index, home, configuration, plugin

    app.register_blueprint(auth.bp)
    app.register_blueprint(index.bp)
    app.register_blueprint(home.bp)
    app.register_blueprint(configuration.bp)
    app.register_blueprint(plugin.bp)

    return app


from django.conf.urls import url

from .views import *

urlpatterns = [
    # 课程列表
    url(r'^list/$', CourseListView.as_view(), name='list'),
    # 课程详情
    url(r'^detail/(?P<course_id>\d+)/$', DetailView.as_view(), name='detail'),
    # 视频信息
    url(r'^info/(?P<course_id>\d+)/$', InfoView.as_view(), name='info'),
    # 课程评论
    url(r'^comment/(?P<course_id>\d+)/$', CommentView.as_view(), name='comment'),
    # 添加评论
    url(r'^addcomment/$', AddComment.as_view(), name='addcomment'),
    #video
    url(r'^video/(?P<video_id>\d+)/$', VideoView.as_view(), name='video'),
]


from django.shortcuts import render, redirect
from django.utils import timezone
from .forms import ActivateSertificateForm

from programs.models import Category
from .models import Sertificate

# Create your views here.
def activate_sertificate(request):
    if request.method == "POST":
        form = ActivateSertificateForm(request.POST)
        if form.is_valid():
            sert = form.save(commit=False)
            try:
                sertificate = Sertificate.objects.get(number = sert.number)
                if not sertificate.is_active:
                    sertificate.user = request.user
                    sertificate.activation_date = timezone.now()
                    sertificate.is_active = True
                    sertificate.save()
                    return redirect('success/')
                else:
                    return redirect('outdated/')
            except Exception as e:
                return redirect('unsuccess/')

    else:
        form = ActivateSertificateForm()
        categories = Category.objects.all();
    return render(request, 'activation/check_sertificate.html', {'form': form, 'categories': categories })

def return_success_message(request):
    categories = Category.objects.all();
    return render(request, 'activation/success.html', { 'categories': categories })

def return_unsuccess_message(request):
    categories = Category.objects.all();
    return render(request, 'activation/unsuccess.html', { 'categories': categories })

def return_outdated_message(request):
    categories = Category.objects.all();
    return render(request, 'activation/outdated.html', { 'categories': categories })


from   __future__ import absolute_import, division, print_function, unicode_literals

from   _GTW                   import GTW
from   _MOM                   import MOM
from   _TFL                   import TFL

import _GTW._OMP._PAP

from   _MOM._Graph.Spec       import Attr, Child, ET, IS_A, Role, Skip

import _MOM._Graph.Command
import _MOM._Graph.Entity

from   _TFL._D2               import Cardinal_Direction as CD
from   _TFL.I18N              import _, _T

import _TFL._Meta.Once_Property

def graph (app_type) :
    """Class/association graph displaying PAP partial object model"""
    ag_p    = hasattr (GTW.OMP.PAP, "Adhoc_Group")
    ass_p   = hasattr (GTW.OMP.PAP, "Association")
    le_p    = hasattr (GTW.OMP.PAP, "Legal_Entity")
    shp_off = CD.S
    result  = MOM.Graph.Spec.Graph \
        ( app_type
        , ET.PAP.Subject_has_Property
            ( Role.left
                ( Child.PAP.Person
                    ( offset      = CD.N
                    )
                , offset = CD.W
                )
            , Role.right
                ( Child.PAP.Address
                    ( ET.PAP.Address_Position
                        ( offset      = CD.S if (ag_p or le_p) else CD.W
                        )
                    , offset      = shp_off + 3 * CD.W
                    )
                , Child.PAP.Email
                    ( offset      = shp_off + 2 * CD.W
                    )
                , Child.PAP.Phone
                    ( offset      = shp_off + 1 * CD.W
                    )
                , Child.PAP.Url
                    ( offset      = shp_off
                    )
                , offset = CD.E
                )
            )
        , desc  = _T
            ("Class/association graph displaying PAP partial object model")
        , title = _T ("PAP graph")
        )
    if hasattr (GTW.OMP.PAP, "Group") :
        g_args = ()
        if hasattr (GTW.OMP.PAP, "Person_in_Group") :
            g_args = \
                ( ET.PAP.Person_in_Group
                    ( Role.left  ()
                    , Role.right ()
                    , offset      = CD.N
                    )
                ,
                )
        result ["PAP.Subject"]._add \
            ( Child.PAP.Group
                ( * g_args
                , offset      = CD.W
                )
            )
        if le_p :
            result ["PAP.Group"]._add \
                ( Child.PAP.Legal_Entity
                    ( offset      = CD.W
                    , source_side = "E"
                    , target_side = "W"
                    )
                )
            if ass_p :
                result ["PAP.Legal_Entity"]._add \
                    ( Child.PAP.Association
                        ( offset      = CD.N
                        )
                    )
            if hasattr (GTW.OMP.PAP, "Company") :
                result ["PAP.Legal_Entity"]._add \
                    ( Child.PAP.Company
                        ( offset      = CD.S
                        )
                    )
        if ag_p :
            result ["PAP.Group"]._add \
                ( Child.PAP.Adhoc_Group
                    ( offset      = CD.W + CD.S *
                        (-1 if not ass_p else (2 if le_p else 0))
                    , source_side = "E"
                    , target_side = "W"
                    )
                )
    if hasattr (GTW.OMP.PAP, "IM_Handle") :
        result ["PAP.Property"]._add \
            ( Child.PAP.IM_Handle
                ( offset      = shp_off + CD.E
                )
            )
    if hasattr (GTW.OMP.PAP, "Nickname") :
        result ["PAP.Property"]._add \
            ( Child.PAP.Nickname
                ( offset      = CD.E
                , source_side = "W"
                , target_side = "E"
                )
            )
    if hasattr (GTW.OMP.PAP, "Person_has_Account") :
        result ["PAP.Person"]._add \
            ( ET.PAP.Person_has_Account
                ( Role.left  (guide_offset = 1.0)
                , Role.right
                    ( offset  = CD.E
                    )
                , offset      = CD.E
                )
            )
    return result
# end def graph

class Command (MOM.Graph.Command) :

    @TFL.Meta.Class_and_Instance_Once_Property
    def PNS (self) :
        return GTW.OMP.PAP
    # end def PNS

    def import_all (self) :
        import _GTW._OMP._Auth
        self.__super.import_all  ()
        GTW.OMP.Auth._Import_All ()
    # end def import_all

# end class Command

if __name__ != "__main__" :
    GTW.OMP.PAP._Export_Module ()
else :
    Command () ()
### __END__ GTW.OMP.PAP.graph


from functools import wraps

def vowel_filter(function):
    vowels = ["a", "e", "i", "o", "u", "y"]
    found_vowels = []

    @wraps(function)
    def wrapper():
        for ch in function():
            if ch.lower() in vowels:
                found_vowels.append(ch)

        return found_vowels

    return wrapper

@vowel_filter
def get_letters():
    return ["a", "b", "c", "d", "e"]

print(get_letters())


from pathlib import Path

from vesper.tests.test_case import TestCase
from vesper.util.preference_manager import PreferenceManager
import vesper.tests.test_utils as test_utils

_DATA_DIR_PATH = Path(test_utils.get_test_data_dir_path(__file__))
_PREFERENCE_FILE_PATH = _DATA_DIR_PATH / 'Preferences.yaml'
_EMPTY_PREFERENCE_FILE_PATH = _DATA_DIR_PATH / 'Empty Preferences.yaml'
_NON_MAPPING_PREFERENCE_FILE_PATH = \
    _DATA_DIR_PATH / 'Non Mapping Preferences.yaml'
_MALFORMED_PREFERENCE_FILE_PATH = _DATA_DIR_PATH / 'Malformed Preferences.yaml'

class PreferenceManagerTests(TestCase):

    def test_get(self):

        preferences = _get_preferences(_PREFERENCE_FILE_PATH)

        cases = (
            ('one', 1),
            ('category_a.two', 2),
            ('category_a.three', 'three'),
            ('category_a.category_b.forty_five', 45),
            ('category_a.category_b.fifty six', 56),
            ('category_a.category_b', {'forty_five': 45, 'fifty six': 56})
        )

        for p in preferences:
            for name, value in cases:
                self.assertTrue(name in p)
                self.assertEqual(p[name], value)
                self.assertEqual(p.get(name), value)

    def test_get_of_nonexistent_preferences(self):

        preferences = _get_preferences(_PREFERENCE_FILE_PATH)

        cases = (
            'bobo',
            'category_a.bobo'
        )

        for p in preferences:
            for name in cases:
                self.assertFalse(name in p)
                self.assertRaises(KeyError, p.__getitem__, name)
                self.assertIsNone(p.get(name))
                self.assertEqual(p.get(name, 10), 10)

    def test_empty_preference_file(self):
        self._test_bad_preference_file(_EMPTY_PREFERENCE_FILE_PATH)

    def _test_bad_preference_file(self, file_path):
        preferences = _get_preferences(file_path)
        for p in preferences:
            self.assertEqual(len(p), 0)

    def test_malformed_preference_file(self):
        self._test_bad_preference_file(_MALFORMED_PREFERENCE_FILE_PATH)

    def test_non_mapping_preference_file(self):
        self._test_bad_preference_file(_NON_MAPPING_PREFERENCE_FILE_PATH)

    def test_nonexistent_preference_file(self):
        manager = PreferenceManager()
        manager.load_preferences_from_file('nonexistent')
        self.assertEqual(len(manager.preferences), 0)

def _get_preferences(file_path):

    # Create preferences from file.
    manager = PreferenceManager.create_for_file(file_path)
    preferences_a = manager.preferences

    # Create preferences from YAML.
    with open(file_path) as file_:
        yaml = file_.read()
    manager = PreferenceManager.create_for_yaml(yaml)
    preferences_b = manager.preferences

    return (preferences_a, preferences_b)


import os
from celery import Celery
from celery.schedules import crontab

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings')

app = Celery('backend')
app.config_from_object('django.conf:settings', namespace='CELERY')

app.conf.beat_schedule = {
   'check_mail_everyday': {
       'task': 'emailService.task.check_dates_task',
       'schedule': crontab(hour=1, minute=00),

   }
}

app.autodiscover_tasks()


import openpyxl as xl
import xlwings as xw
from Worksheet import Worksheet,QPreviewItem
from Workcell import *
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from copy import copy
#import time
import datetime
##################################################
#       class for PS sheet handling
##################################################
UP = 0
DOWN = 1
nn = 0
class PSsheet(Worksheet):
    def __init__(self,sheet = None,sheet_wr = None):
        super(PSsheet,self).__init__(sheet,sheet_wr)
        self._preview_model_list = []
        self._extended_preview_model_list = None
        self.init_ps_sheet()
        self.init_ps_model()
    def __del__(self):
        super(PSsheet,self).__del__()
        del self._preview_model
        del self._extended_preview_model_list

    def init_ps_sheet(self):
        if self._xmlname != None:
            self._status = self.search_header_by_value(u'Status(POR,INIT,PREV)')
            self._subject_matter = self.search_header_by_value(u'Subject Matter/\nFunctional Area')
            self._container_name = self.search_header_by_value(u'Container Name\nTechnical Specification')
    def init_ps_model(self):
        self._preview_model = QStandardItemModel()
        self._preview_model.setColumnCount(4)
        self._preview_model.setHeaderData(0,Qt.Horizontal,'status')
        self._preview_model.setHeaderData(1,Qt.Horizontal,'subject matter')
        self._preview_model.setHeaderData(2,Qt.Horizontal,'container name')
        self._preview_model.setHeaderData(3,Qt.Horizontal,'xmlname')
        self._extended_preview_model = None
    def update_model(self):
        super(PSsheet,self).update_model()
        self._preview_model_list = []
        self.init_ps_model()
        self.init_ps_sheet()
        if self._xmlname != None:
            try:
                for xml_name in self.xml_names():
                    item_status = QPreviewItem(self._status.get_item_by_xmlname(xml_name))
                    item_subject_matter = QPreviewItem(self._subject_matter.get_item_by_xmlname(xml_name))
                    item_container_name = QPreviewItem(self._container_name.get_item_by_xmlname(xml_name))
                    item_xml_name = QPreviewItem(xml_name)
                    self._preview_model.appendRow((item_status,item_subject_matter,item_container_name,item_xml_name))
                    self._preview_model_list.append((item_status.value,item_subject_matter.value,item_container_name.value,item_xml_name.value))
            except:
                return 'error'
    def status(self):
        cells = list(self._worksheet.iter_cols(min_col=self._status.col,min_row=self._status.row+1,max_col=self._status.col,max_row=self.max_row).next())
        while cells[-1].value == None:
            cells.pop()
        return map(lambda x:Status(x,self._worksheet_wr),cells)
    def cell(self,row,col):
        #return self._worksheet_wr.range(row,col)
        return self._worksheet.cell(row=row,column=col)

    def auto_fit(self,cols):
        for col in cols:
            for i in range(1):
                self._worksheet_wr.api.Columns(col).AutoFit()

    def add_row(self,start_pos,offset,orientation):
        loop = offset
        while loop > 0:
            self._worksheet_wr.api.Rows[start_pos].Insert(-4121)
            loop -= 1

    def delete_row(self,start_pos,offset):
        self._worksheet_wr.range('%d:%d'%(start_pos,start_pos+offset-1)).api.Delete()
    def lock_row(self,row,status):
        self._worksheet_wr.api.Rows[row-1].Locked = True
    def lock_sheet(self):
        self._worksheet_wr.api.Protect()
    def lock_sheet_status(self):
        return self._worksheet_wr.api.ProtectContents
    def unlock_sheet(self):
        self._worksheet_wr.api.Unprotect()
    def unlock_all_cells(self):
        self._worksheet_wr.api.Cells.Locked = False

    def extended_preview_model(self):
        if self._extended_preview_model == None:
            self._extended_preview_model = QStandardItemModel()
            self._extended_preview_model.setColumnCount(self._worksheet.max_column)
            for row in self._worksheet.rows:
                item_row = []
                for cell in row:
                    try:
                        if cell.value == None:
                            item = QStandardItem('')
                        else:
                            item = QStandardItem(cell.value)
                    except:
                        item = QStandardItem('')
                    item_row.append(item)
                self._extended_preview_model.appendRow(item_row)
        return self._extended_preview_model
    @property
    def extended_preview_model_list(self):
        if self._extended_preview_model_list == None:
            self._extended_preview_model_list = []
            for row in self._worksheet.iter_rows(min_row=self.min_row,max_row=self.max_row,min_col=self.min_col,max_col=self.max_col):
                item_row = []
                for cell in row:
                    #item_row.append(cell.value if cell.value is not None else '')
                    try:
                        #item_row.append('' if cell.value is None else str(cell.value) if type(cell.value) == long else '%s-%s-%s'%((cell+datetime.delta(days=1)).timetuple().tm_year,(cell+datetime.delta(days=1)).timetuple().tm_mon,(cell+datetime.delta(days=1)).timetuple().tm_mday) if type(cell) is datetime.datetime else (cell.value))
                        item_row.append('' if cell.value is None else str('%s-%s-%s'%((cell.value+datetime.timedelta(days=1)).timetuple().tm_year,(cell.value+datetime.timedelta(days=1)).timetuple().tm_mon,(cell.value+datetime.timedelta(days=1)).timetuple().tm_mday)) if type(cell.value) is datetime.datetime else str(cell.value))
                    except:
                        item_row.append('')
                self._extended_preview_model_list.append(item_row)
        return self._extended_preview_model_list

    @property
    def preview_model(self):
        return self._preview_model_list


class Solution:
    def splitListToParts(self, root: ListNode, k: int) -> List[ListNode]:
        totalLen = 0
        cur = root
        while cur:
            totalLen += 1
            cur = cur.next
        length = totalLen // k
        mod = totalLen % 3
        res = []
        cur = root
        for i in range(k):
            res.append(cur)
            size = length + (1 if m > 0 else 0)
            if cur:
                for j in range(size):
                    cur = cur.next
                tmp = cur.next
                cur.next = None
                cur = tmp
        return res

temp/toy/python/725. Split Linked List in Parts.py


import numpy as np
import netCDF4 as nc4
import net_radiation
import atmospheric_parameters
import wind_shear_velocity
import datetime

# Using TerraClimate
# 2.5 arcminute (1/24 degree) resolution: ~5 km N-S

# Import step
# ... load files here or with a CLI
years = range(1958, 2019)
months_zero_indexed = range(12)
TerraClimateDir = '/media/andy/data1/TerraClimate/'

def extract_data(varnc, varname, varmonth_zero_indexed=None):
    if varmonth_zero_indexed is None:
        var = varnc.variables[varname][:]
    else:
        var = varnc.variables[varname][varmonth_zero_indexed]
    fv = var.fill_value
    var = var.data
    var[var == fv] = np.nan
    return var

# Get lats and lons from one file
srad_nc = nc4.Dataset(TerraClimateDir+'TerraClimate_srad_1958.nc')
lats = extract_data(srad_nc, 'lat')
lons = extract_data(srad_nc, 'lon')
#LONS, LATS = np.meshgrid (lons, lats)

# Shear velocity of winds: tool to compute from velocity
ustar_interp = wind_shear_velocity.create_lookup_table_one_step()

# Elevation
elevation_nc = nc4.Dataset(TerraClimateDir+'Gebco_2020_2_point_5_arcminute.nc')
elevation = extract_data(elevation_nc, 'value')
elevation = elevation[::-1]

# Heat capacity of air
specific_heat_capacity_of_air = 1.005 # approx. constant at 1 atm
                                      # Humidity minor impact below 40C or so
                                      # But this is an approximation!
cp = specific_heat_capacity_of_air # Easier

# Water density
rho_w = 1000.

# Latent heat of vaporization for water
Lv = 2.5E6
DeltaH_vap = Lv # to make me happier

# Ratio of molecular weight of water vapor to dry air
epsilon = 0.622

# Days in month, for weighting
days_in_month = [31, 28.25, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

# Evaporation array
E = np.zeros(elevation.shape)

years = years[:13] # 1970 and before: before so much GW
for year in years:
    print(year)
    # Incoming solar radiation (monthly average)
    srad_nc = nc4.Dataset(TerraClimateDir+'TerraClimate_srad_'+str(year)+'.nc')
    # Maximum daily temperature (monthly average)
    tmax_nc = nc4.Dataset(TerraClimateDir+'TerraClimate_tmax_'+str(year)+'.nc')
    # Minimum daily temperature (monthly average)
    tmin_nc = nc4.Dataset(TerraClimateDir+'TerraClimate_tmin_'+str(year)+'.nc')
    # Wind speed (monthly average)
    ws_nc = nc4.Dataset(TerraClimateDir+'TerraClimate_ws_'+str(year)+'.nc')
    # Vapor pressure (monthly average)
    vap_nc = nc4.Dataset(TerraClimateDir+'TerraClimate_vap_'+str(year)+'.nc')

    # Now compute for each month
    for month_zi in months_zero_indexed:
        print(month_zi+1)
        # Data
        srad = extract_data(srad_nc, 'srad', month_zi)
        tmax = extract_data(tmax_nc, 'tmax', month_zi)
        tmin = extract_data(tmin_nc, 'tmin', month_zi)
        ws = extract_data(ws_nc, 'ws', month_zi)
        vap = extract_data(vap_nc, 'vap', month_zi) * 1000. # kPa to Pa
        # Average radiation on the midday of the month; could be more precise
        date = datetime.date(year, month_zi+1, int(np.round(days_in_month[month_zi]/2.)))
        #elevation = 2000. # placeholder
        #julian_day = 205 # placeholder
        #vap = .03*101325 # placeholder
        albedo = 0.06

        # Calculations:
        # Net Radiation
        Rn = net_radiation.computeNetRadiation(elevation, date, lats, len(lons),
                                                tmax, tmin, vap, srad, albedo)

        # Shear velocity of winds
        ustar = ustar_interp(ws)

        # Vapor-pressure deficit
        # We don't have max and min humidity
        VPD = atmospheric_parameters.compute_vpd( (tmax+tmin)/2., vap )

        # Atmospheric pressure
        P = atmospheric_parameters.compute_atmospheric_pressure(elevation)

        # Atmospheric density (ignoring temperature + humidity effects)
        rho_a = atmospheric_parameters.compute_atmospheric_density(elevation,
                                        (tmax + tmin)/2.)

        # Clausius-Clayperon phase-change slope
        Delta = ( atmospheric_parameters.compute_Delta_e_sat( tmax )
                  + atmospheric_parameters.compute_Delta_e_sat( tmin ) ) / 2.

        _E = (Rn + cp*rho_a*ustar**2/(Delta*ws) * VPD) \
             / ( rho_w*Lv  + P*cp*rho_w/epsilon )
        _E[_E<0] = 0 # ignore condensation; I think it's spurious (Antarctica?)
        E += _E*days_in_month[month_zi]

E /= (365.25*len(years))

# Export
from netCDF4 import Dataset
import numpy
import time

#path to the file you want to open or create
location_string="evaporation_002p5_arcmin.nc"

# Create nc file
rootgrp = Dataset(location_string,"w",format="NETCDF4")

# Dimensions
lat=rootgrp.createDimension("lat", len(lats))
lon=rootgrp.createDimension("lon", len(lons))
value = rootgrp.createDimension("evaporation", None)

# Values
latitudes = rootgrp.createVariable("lat", "f4", ("lat",))
longitudes = rootgrp.createVariable("lon", "f4", ("lon",))
values = rootgrp.createVariable("value", "f4" , ("lat", "lon",))
latitudes[:] = lats
longitudes[:] = lons
values[:] = E

# Units
latitudes.units = "degrees north"
longitudes.units = "degrees east"
values.units = "metres per second"

# Metadata
rootgrp.description = "Evaporation derived from TerraClimate data products (see https://github.com/umn-earth-surface/TerraClimate-potential-open-water-evaporation)."
rootgrp.history = "created" + time.ctime(time.time())
values.Long_Name = "Open-water evaporation"

# Save
rootgrp.close()


from gensim.models.doc2vec import Doc2Vec
import pickle

def get_most_similar_docs(test_data, model_path):
   # Load the Doc2Vec model
   model = Doc2Vec.load(model_path)

   # Split the test_data string into a list of words
   test_data_words = test_data.split()

   # Infer the vector for the test document
   inferred_vector = model.infer_vector(test_data_words)

   with open('./homework_6/data/tagged_data.pkl', 'rb') as file:
        loaded_tagged_data = pickle.load(file)
   # Get the 5 most similar documents based on the inferred vector
   sims = model.dv.most_similar([inferred_vector], topn=5)
   idx = [sims[i][0] for i in range(5)]

   # Print the most similar documents
   print('Test Document: «{}»\n'.format(' '.join(test_data_words)))
   print(u'SIMILAR DOCS PER MODEL %s:\n' % model)
   for label, index in [('1', 0), ('2', 1), ('3', 2), ('4', 3), ('5', 4)]:
       print(u'%s %s: «%s»\n' % (label, sims[index], ' '.join(loaded_tagged_data[int(sims[index][0])].words)))
   return idx

test_data = 'exotic yellow spice note meet lean lime pith light crisp nose old vine expression historic winery . meyer lemon rind juice show brightly palate grippy chalkiness complement rich lemon curd flavor'
get_most_similar_docs(test_data, './homework_6/models/doc2vec.model')


from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField
from wtforms.validators import DataRequired, Length, ValidationError
from urllib.parse import urlparse
from app.models import Business

def business_name_exists(form, field):
    business_name = field.data
    business = Business.query.filter(Business.name == business_name).first()
    if business:
        raise ValidationError("Business with this name already exists.")

def valid_zip_code(form, field):
    if not field.data.isdigit() or len(field.data) != 5:
        raise ValidationError("Invalid ZIP Code.")

def valid_phone_number(form, field):
    if not field.data.isdigit() or len(field.data) != 10:
        raise ValidationError("Invalid phone number.")

# def valid_url(form, field):
#     try:
#         result = urlparse(field.data)
#         if not all([result.scheme, result.netloc]):
#             raise ValueError()
#     except ValueError:
#         raise ValidationError("Invalid URL.")

class BusinessForm(FlaskForm):
    # class Meta:
    #     csrf = False
    name = StringField('Business Name', validators=[
        Length(min=1, max=50),
        DataRequired(),
        # business_name_exists
    ])

    address = StringField('Address', validators=[
        Length(min=1, max=255),
        DataRequired()
    ])

    city = StringField('City', validators=[
        Length(min=1, max=50),
        DataRequired()
    ])

    state = StringField('State', validators=[
        Length(min=1, max=25),
        DataRequired()
    ])

    zip_code = StringField('Zip Code', validators=[
        Length(min=1, max=10),
        DataRequired(),
        # valid_zip_code
    ])

    phone_number = StringField('Phone Number', validators=[
        Length(min=1, max=30),
        DataRequired(),
        # valid_phone_number
    ])

    category_id = IntegerField('Category ID', validators=[DataRequired()])

    owner_id = IntegerField('Owner ID', validators=[DataRequired()])

    website = StringField('Website', validators=[
        Length(min=1, max=255),
        DataRequired(),
        # valid_url
    ])

    about = StringField('About', validators=[
        Length(min=1, max=500),
        DataRequired()
    ])

    type = StringField('Type', validators=[
        Length(min=1, max=255),
        DataRequired()
    ])


import numpy as np
import matplotlib.pyplot as plt
from scipy import sparse

def output_result(path, matrix):
    f = open(path, "w+")
    f.write(str(len(matrix)) + '\n')

    for row in matrix:
        f.write(str(row)[1:-1])
        f.write('\n')
    f.close()

def read_buildings(path_to_buildings):
    '''
    :param path_to_buildings: a path to text file that consists coordinates of the buildings
    :return: list of (x1,y1,x2,y2)
    '''
    file = open(path_to_buildings)
    buildings = []
    for line in file:
        sp = line.split(" ")

        x1, y2, x2, y1 = float(sp[0]), 1 - float(sp[1]), float(sp[2]), 1 - float(sp[3])
        buildings.append((x1, y1, x2, y2))

    return buildings

def isInsideBuildings(xx, yy, buildings):
    '''
    :param coord: (x,y) - coords to check
    :param building: (x1,y1,x2,y2) - building
    :return: Boolean
    '''

    answer = False
    for building in buildings:
        (x1, y1, x2, y2) = building

        if x1 <= xx <= x2 and y1 <= yy <= y2:
            answer = True
    return not answer

def get_cond_check_func(buildings):
    '''
    Given building construct function that verify whether point (x,y) is outside these buildings
    :param buildings:
    :return: lambda (x,y) -> ...
    '''
    return lambda x, y: isInsideBuildings(x, y, buildings)

class ConvectionDiffusion:
    def __init__(self, max_t, l1, l2, k, N, cond_func, eps):
        self.max_t = max_t
        self.l1 = l1
        self.l2 = l2
        self.k = k
        self.N = N
        self.h = 1.0 / N
        self.eps = eps
        self.cond_func = cond_func
        self.tau = 1 / (4 * k * N * N)
        self.U = np.zeros((N + 1, N + 1))
        self.coeffs = [1 - 4 * self.tau * k / (self.h * self.h),
                       self.tau * (k / (self.h * self.h) - l1 / (2 * self.h)),
                       self.tau * (k / (self.h * self.h) + l1 / (2 * self.h)),
                       self.tau * (k / (self.h * self.h) - l2 / (2 * self.h)),
                       self.tau * (k / (self.h * self.h) + l2 / (2 * self.h))]

    def check_correctness(self, x, y):
        return 0 <= x < self.N and 0 < y < self.N

    def iteration(self):
        '''
        One iteration of the simple iteration methods
        :return: error
        '''
        dx = [0, 1, -1, 0, 0]
        dy = [0, 0, 0, 1, -1]
        new_U = np.zeros((N + 1, N + 1))

        for i in range(self.N + 1):
            for j in range(self.N + 1):

                new_U[i, j] = self.U[i, j]
                if not self.cond_func(i / N, j / N):
                    continue
                else:
                    new_U[i, j] *= self.coeffs[0]

                for f in range(1, 5):
                    x = i + dx[f]
                    y = j + dy[f]
                    if self.cond_func(x / N, y / N) and self.check_correctness(x, y):
                        new_U[i, j] += self.U[x, y] * self.coeffs[f]
                    else:
                        new_U[i, j] += self.U[i, j] * self.coeffs[f]
        old_U = self.U
        self.U = new_U
        return np.max(np.abs((old_U / 100 - new_U / 100)))

    def init_matrix(self):
        self.U[:, 1] = 100

    def solve(self):
        '''
        :return: U and image
        '''
        self.init_matrix()
        for f in range(0, self.max_t):
            error = self.iteration()
            print(error)
            if error < self.eps:
                break
        fig = plt.imshow(self.U / 100)
        plt.colorbar(fig)
        plt.show()
        return self.U / 100

    def optimized_solve(self):
        vars = (self.N + 1) * (self.N + 1)
        dx = [0, 1, -1, 0, 0]
        dy = [0, 0, 0, 1, -1]
        A = sparse.lil_matrix((vars, vars))
        b = np.zeros(vars)
        frames = []
        for i in range(vars):
            y = i // (self.N + 1)
            x = i % (self.N + 1)

            A[i, i] = self.coeffs[0]
            if not self.cond_func(x / (self.N + 1), y / (self.N + 1)) or not self.check_correctness(x, y):
                continue
            if x == 0:
                A[i, i] = 1
                continue
            for j in range(1, 5):
                xx = x + dx[j]
                yy = y + dy[j]
                if xx == 0:
                    b[i] += self.coeffs[j]
                    continue
                if not self.cond_func(xx / (self.N + 1), yy / (self.N + 1)) or not self.check_correctness(xx, yy):
                    A[i, i] += self.coeffs[j]
                else:
                    A[i, yy * (self.N + 1) + xx] = self.coeffs[j]

        A = sparse.csc_matrix(A)
        x = np.zeros(vars)
        for i in range(self.N + 1):
            x[i * (self.N + 1)] = 0

        for f in range(self.max_t):
            x_new = A @ x + b
            error = np.max(np.abs((x_new - x) / np.maximum(1, x)))
            if f % 100 == 0:
                frames.append(x_new.reshape((self.N + 1, self.N + 1)))
                print(error)
            if error < self.eps:
                x = x_new
                break
            x = x_new
        answer = x.reshape((self.N + 1, self.N + 1))
        fig = plt.imshow(answer)
        plt.colorbar(fig)
        plt.show()
        return answer, frames

def animate(frames):
    fig = plt.figure()
    ims = []
    from matplotlib import animation
    i = 0
    for frame in frames:
        print(frame.shape)
        im = plt.imshow(frame, animated=True)
        ims.append([im])
        i += 1

    plt.colorbar(im)

    Writer = animation.writers['ffmpeg']
    writer = Writer(fps=60, metadata=dict(artist='Me'), bitrate=260)
    ani = animation.ArtistAnimation(fig, ims, interval=17, blit=True,
                                    repeat_delay=1000)
    # ani.save('diffusion.html')
    ani.save('diffusion.mp4', writer=writer)

    plt.show()

if __name__ == "__main__":
    max_t = 100000
    l_1 = 1
    l_2 = 0.0
    k = 0.5
    N = 300
    eps = 1e-6

    buildings = read_buildings("buildings.txt")
    cond_func = get_cond_check_func(buildings)
    solver = ConvectionDiffusion(max_t, l_1, l_2, k, N, cond_func, eps)
    u, frames = solver.optimized_solve()
    animate(frames)
    output_result("output.txt", u)


import wild_pokemon, wondertrade, pokemon

def index_species(symbols, rom, proj, ps):
    """ Creates an index for all species
    Parameters:
    symbols : The symbol table
    rom : The rom
    proj : The pymap project
    ps : The Pstring parser

    Returns: A list of lists containing IndexEntry instances """
    species_index = [[] for _ in proj.constants.values("species")]

    wild_pokemon.index_wild_pokemon(species_index, symbols, rom, proj, ps, wild_pokemon_table_symbol="wild_pokemon")
    wondertrade.index_wondertrade(species_index, symbols, rom)
    for i, species in enumerate(proj.constants.values("species")):
        for entry in species_index[i]:
            print("Species", pokemon.species_to_name(i, rom, proj, ps, symbols), entry.__dict__)


import os
import numpy as np
import torch
import transforms3d
def plane2pose(plane_parameters):
    r3 = plane_parameters[:3]
    r2 = np.zeros_like(r3)
    r2[0], r2[1], r2[2] = (-r3[1], r3[0], 0) if r3[2] * r3[2] <= 0.5 else (-r3[2], 0, r3[0])
    r1 = np.cross(r2, r3)
    pose = np.zeros([4, 4], dtype=np.float32)
    pose[0, :3] = r1
    pose[1, :3] = r2
    pose[2, :3] = r3
    pose[2, 3] = plane_parameters[3]
    pose[3, 3] = 1
    return pose
def plane2euler(plane_parameters, axes='sxyz'):
    pose = plane2pose(plane_parameters)
    T, R, Z, S = transforms3d.affines.decompose(pose)
    euler = transforms3d.euler.mat2euler(R, axes=axes)
    return T, euler


from telegram import Update
from telegram.ext import (
    Updater,
    CallbackContext,
    run_async,
    CommandHandler,
)
from utils import Config
from pkgutil import walk_packages
from types import ModuleType
from typing import Dict

from utils import get_filter

submodules: Dict[str, ModuleType] = {
    module_name: loader.find_module(module_name).load_module(module_name)
    for loader, module_name, is_package in walk_packages(__path__)
}

def describe():
    return "列出所有的指令, 需注意列出的指令在当前的环境内不一定可用"

def run(update: Update, context: CallbackContext):
    update.effective_message.reply_text(
        text="所有指令如下:\n"
        + "\n".join(
            [f"/{command}: {description}"
             for command, description in commands_list]
        ),
        quote=True
    )

commands_list = tuple(
    (name, module.describe())
    for name, module in submodules.items()
) + (
    (__name__, describe()),
)

def register(updater: Updater):
    for module in submodules.values():
        module.register(updater)

    dp = updater.dispatcher
    dp.add_handler(CommandHandler(
        __name__, run, filters=get_filter(Config.watchers), run_async=True))
    # dp.add_handler(CommandHandler(__name__, run, filters=Filters.all)) # DEBUG

    # * Unavailable until all commands are implemented (or at least their describe methods return a string with len > 3)
    updater.bot.set_my_commands(commands_list)