# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals

from django import forms as djangoforms
from django.forms import fields as djangofields

from django_sorcery import fields
from django_sorcery.db.meta import model_info
from django_sorcery.field_mapping import ModelFieldMapper
from django_sorcery.forms import SQLAModelFormOptions

from .base import TestCase
from .models import AllKindsOfFields, Owner, Vehicle, db

class TestFieldMapping(TestCase):
    def test_get_default_kwargs(self):
        class Meta:
            model = Owner
            session = db.session
            widgets = {"test": "widget"}
            labels = {"test": "label"}
            help_texts = {"test": "help_text"}
            error_messages = {"test": "error_message"}
            field_classes = {"test": "field_class"}
            localized_fields = ("test",)

        opts = SQLAModelFormOptions(Meta)
        mapper = ModelFieldMapper(opts)

        self.assertDictEqual({}, mapper.get_default_kwargs("none"))

        self.assertDictEqual(
            {
                "error_messages": "error_message",
                "form_class": "field_class",
                "help_text": "help_text",
                "label": "label",
                "localize": True,
                "widget": "widget",
            },
            mapper.get_default_kwargs("test"),
        )

        mapper.opts.localized_fields = "__all__"
        self.assertDictEqual(
            {
                "error_messages": "error_message",
                "form_class": "field_class",
                "help_text": "help_text",
                "label": "label",
                "localize": True,
                "widget": "widget",
            },
            mapper.get_default_kwargs("test"),
        )

    def test_get_fields(self):
        class Meta:
            model = Owner
            session = db

        mapper = ModelFieldMapper(SQLAModelFormOptions(Meta))

        fields = mapper.get_fields()

        self.assertListEqual(sorted(list(fields.keys())), ["first_name", "last_name", "vehicles"])

    def test_get_fields_with_include(self):
        class Meta:
            model = Owner
            fields = ["first_name"]
            session = db

        mapper = ModelFieldMapper(SQLAModelFormOptions(Meta))

        fields = mapper.get_fields()

        self.assertListEqual(list(fields.keys()), ["first_name"])

    def test_get_fields_with_exclude(self):
        class Meta:
            model = Owner
            exclude = ["first_name", "vehicles"]
            session = db

        mapper = ModelFieldMapper(SQLAModelFormOptions(Meta))

        fields = mapper.get_fields()

        self.assertListEqual(list(fields.keys()), ["last_name"])

    def test_get_fields_formfield_callback(self):

        info = model_info(Owner)
        self.called_with = []

        def callback(attr, **kwargs):
            self.called_with.append(attr)
            return djangofields.CharField()

        class Meta:
            model = Owner
            fields = ["first_name", "vehicles"]

        mapper = ModelFieldMapper(SQLAModelFormOptions(Meta), formfield_callback=callback)

        fields = mapper.get_fields()

        self.assertListEqual(list(fields.keys()), ["first_name", "vehicles"])
        self.assertEqual(self.called_with, [info.properties["first_name"], info.relationships["vehicles"]])

    def test_get_fields_no_private(self):
        class Meta:
            model = Vehicle
            session = db

        mapper = ModelFieldMapper(SQLAModelFormOptions(Meta))

        fields = mapper.get_fields()

        self.assertEqual(
            list(sorted(fields.keys())), ["created_at", "is_used", "name", "options", "owner", "paint", "parts", "type"]
        )
        self.assertNotIn(Vehicle._owner_id.key, fields)

class TestTypeMap(TestCase):
    def setUp(self):
        super(TestTypeMap, self).setUp()

        class Meta:
            model = AllKindsOfFields
            session = db

        self.mapper = ModelFieldMapper(SQLAModelFormOptions(Meta))
        self.fields = self.mapper.get_fields()

    def test_boolean_field_not_nullable(self):
        self.assertIn("boolean_notnull", self.fields)
        field = self.fields["boolean_notnull"]
        self.assertIsInstance(field, djangofields.BooleanField)

    def test_boolean_field(self):
        self.assertIn("boolean", self.fields)
        field = self.fields["boolean"]
        self.assertIsInstance(field, djangofields.NullBooleanField)

    def test_enum_field(self):
        self.assertIn("enum", self.fields)
        field = self.fields["enum"]
        self.assertIsInstance(field, fields.EnumField)

    def test_enum_choice_field(self):
        self.assertIn("enum_choice", self.fields)
        field = self.fields["enum_choice"]
        self.assertIsInstance(field, djangofields.TypedChoiceField)

    def test_bigint_field(self):
        self.assertIn("bigint", self.fields)
        field = self.fields["bigint"]
        self.assertIsInstance(field, djangofields.IntegerField)

    def test_biginteger_field(self):
        self.assertIn("biginteger", self.fields)
        field = self.fields["biginteger"]
        self.assertIsInstance(field, djangofields.IntegerField)

    def test_decimal_field(self):
        self.assertIn("decimal", self.fields)
        field = self.fields["decimal"]
        self.assertIsInstance(field, djangofields.DecimalField)

    def test_float_field(self):
        self.assertIn("float", self.fields)
        field = self.fields["float"]
        self.assertIsInstance(field, djangofields.FloatField)

    def test_int_field(self):
        self.assertIn("int", self.fields)
        field = self.fields["int"]
        self.assertIsInstance(field, djangofields.IntegerField)

    def test_integer_field(self):
        self.assertIn("integer", self.fields)
        field = self.fields["integer"]
        self.assertIsInstance(field, djangofields.IntegerField)

    def test_numeric_field(self):
        self.assertIn("numeric", self.fields)
        field = self.fields["numeric"]
        self.assertIsInstance(field, djangofields.DecimalField)

    def test_real_field(self):
        self.assertIn("real", self.fields)
        field = self.fields["real"]
        self.assertIsInstance(field, djangofields.FloatField)

    def test_smallint_field(self):
        self.assertIn("smallint", self.fields)
        field = self.fields["smallint"]
        self.assertIsInstance(field, djangofields.IntegerField)

    def test_smallinteger_field(self):
        self.assertIn("smallinteger", self.fields)
        field = self.fields["smallinteger"]
        self.assertIsInstance(field, djangofields.IntegerField)

    def test_char_field(self):
        self.assertIn("char", self.fields)
        field = self.fields["char"]
        self.assertIsInstance(field, djangofields.CharField)

    # def test_clob_field(self):
    # self.assertIn("clob", self.fields)
    # field = self.fields["clob"]
    # self.assertIsInstance(field, djangofields.CharField)
    # self.assertIsInstance(field.widget, djangoforms.Textarea)

    def test_nchar_field(self):
        self.assertIn("nchar", self.fields)
        field = self.fields["nchar"]
        self.assertIsInstance(field, djangofields.CharField)

    # def test_nvarchar_field(self):
    # self.assertIn("nvarchar", self.fields)
    # field = self.fields["nvarchar"]
    # self.assertIsInstance(field, djangofields.CharField)

    def test_string_field(self):
        self.assertIn("string", self.fields)
        field = self.fields["string"]
        self.assertIsInstance(field, djangofields.CharField)

    def test_text_field(self):
        self.assertIn("text", self.fields)
        field = self.fields["text"]
        self.assertIsInstance(field, djangofields.CharField)
        self.assertIsInstance(field.widget, djangoforms.Textarea)

    def test_unicode_field(self):
        self.assertIn("unicode", self.fields)
        field = self.fields["unicode"]
        self.assertIsInstance(field, djangofields.CharField)

    def test_unicodetext_field(self):
        self.assertIn("unicodetext", self.fields)
        field = self.fields["unicodetext"]
        self.assertIsInstance(field, djangofields.CharField)
        self.assertIsInstance(field.widget, djangoforms.Textarea)

    def test_varchar_field(self):
        self.assertIn("varchar", self.fields)
        field = self.fields["varchar"]
        self.assertIsInstance(field, djangofields.CharField)

    def test_date_field(self):
        self.assertIn("date", self.fields)
        field = self.fields["date"]
        self.assertIsInstance(field, djangofields.DateField)

    def test_datetime_field(self):
        self.assertIn("datetime", self.fields)
        field = self.fields["datetime"]
        self.assertIsInstance(field, djangofields.DateTimeField)

    def test_interval_field(self):
        self.assertIn("interval", self.fields)
        field = self.fields["interval"]
        self.assertIsInstance(field, djangofields.DurationField)

    def test_time_field(self):
        self.assertIn("time", self.fields)
        field = self.fields["time"]
        self.assertIsInstance(field, djangofields.TimeField)

    def test_timestamp_field(self):
        self.assertIn("timestamp", self.fields)
        field = self.fields["timestamp"]
        self.assertIsInstance(field, djangofields.DateTimeField)

    def test_binary_field(self):
        self.assertNotIn("binary", self.fields)

    def test_blob_field(self):
        self.assertNotIn("blob", self.fields)

    def test_largebinary_field(self):
        self.assertNotIn("largebinary", self.fields)

    def test_varbinary_field(self):
        self.assertNotIn("varbinary", self.fields)


import scrapy

class SouthUistEstateSpider(scrapy.Spider):
    name = 'lochs'
    start_urls = [
        'http://www.southuistfishing.com/permits-boats/machair-loch-permits/',
    ]

    def parse(self, response):
        for loch in response.css('table:nth-child(7n) tr'):
            yield {
                'name': loch.css('td:nth-child(1n)::text').get(),
                'description': 'blah blah',
                'lat_lng': loch.css('td:nth-child(2n)::text').get(),
            }


# @Time  : 2019-5-15 09:04:37
# @功能  ：集成测试用的各种模块/方法
import re
import time
from lxml import etree
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys

def load_modes(Browser,StandardData):
    """
    载入模块,传入Browser对象和StandardData变量
    :param Browser:
    :param StandardData:
    :return:
    """
    global browser
    browser = Browser
    global standarddata
    standarddata=StandardData

def open_url(url):
    """
    打开url
    :param url:
    :return:
    """
    browser.get(url)

def input_data(data):
    """
    输入测试数据;有2种方法定位相应的元素,自动选择
    :param data:
    :return:
    """
    html=browser.page_source
    selector=etree.HTML(html)
    for k,v in data.items():
        print ("    "+k+" --> "+v)
        # 1.通过框旁边的提示符
        try:
            # 找到含有该文本的标签,取其中for属性的值
            K=selector.xpath('//label[contains(string(),"'+k+'")]/@for')
            # print (k)
            # 利用上面找到的for属性的值 找到id属性为它的输入框,输入数据
            input=browser.find_element_by_xpath("//input[@id='"+K[0]+"']")
            ActionChains(browser).double_click(input).perform()
            input.send_keys(Keys.BACKSPACE)
            input.send_keys(v)

        # 2.通过框里面的placeholder提示符
        except :
            input=browser.find_element_by_xpath("//input[@placeholder='"+k+"']")
            ActionChains(browser).double_click(input).perform()
            input.send_keys(Keys.BACKSPACE)
            input.send_keys(v)

def click(words):
    """
    点击按钮操作;有两种方法定位相应的元素,自动选择
    :param words: 按钮关键字
    :return:
    """
    try:
        browser.find_element_by_xpath("//input[@value='"+words+"']").click()
    except:
        button="//button[contains(string(),'" + words + "')]"
        browser.find_element_by_xpath(button).click()
    time.sleep(1)

def judge(judge_words):
    """
    根据给定关键字判断测试结果,匹配到关键字则返回False;
    :param judge_words: 用来判断测试结果的关键字
    :return:
    """
    ret=""
    for k,v in judge_words.items():
        if browser.page_source.find(k) != -1:
            ret=ret+v+";"
    if ret=="":
        ret="成功"
    print("    judge result:"+ret)
    return ret

def confim(input_data,key):
    """
    判断传入json数据的结构是否符合预期要求
    :param input_data: 需要检查的数据
    :param key: 数据在原始json中的key值
    :return:True为符合要求
    """
    key=re.sub("[\\d.]+","",key)
    if type(input_data)==type(standarddata[key]):
        return True
    else:
        return False


from flask import Flask, jsonify, request
from flask_cors import CORS
from collections import OrderedDict

app = Flask(__name__)
app.config["JSON_SORT_KEYS"] = False
CORS(app)

from testerDAO import TesterDAO

@app.route("/", methods=["GET"])
def index():
    return "Tester Matching App"

@app.route("/countries", methods=["GET"])
def get_countries():
    dao = TesterDAO()
    return jsonify(dao.get_countries())

@app.route("/devices", methods=["GET"])
def get_devices():
    dao = TesterDAO()
    return jsonify(dao.get_devices())

@app.route("/search", methods=["GET"])
def search():
    dao = TesterDAO()
    country_params = request.args.get('country')
    device_params = request.args.get('device')

    if country_params == 'all':
        country_filter = None
    else:
        country_filter = country_params.split('|')
    if device_params == 'all':
        device_filter = None
    else:
        device_filter = device_params.split('|')

    return jsonify(dao.search(country_filter=country_filter,
                              device_filter=device_filter))


from appium_config import DriverClient
import time
import log
import wechataction
import openurl
from testrunner import TestAllCase
import HTMLTestRunner

Log=log.Log()
driver = DriverClient().getDriver()
Wechat=wechataction.WechatAction(driver)
Page=openurl.pageAction(driver)

class Running():
    def run(self):
        Wechat.wechatLogin()
        Page.opentestPage()
        Log.info("开始测试")
        start = time.time()
        start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(start))
        Log.info("当前时间是%s" % start_time)
        now = time.strftime("%Y-%m-%d %H_%M_%S", time.localtime())
        resultpath = "C:\\python\\MobileUItest\\result\\report\\" + now + "_result.html"
        fp = open(resultpath, 'wb')
        runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u'h5测试报告', description=u'用例执行情况：')
        runner.run(TestAllCase().testsuit())
        fp.close()
        # Runner = TestAllCase()
        # Runner.testsuit()
        end = time.time()
        end_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(start))
        Log.info("当前时间是%s" % end_time)
        Log.info("耗时是%s" % (end - start))
        Log.info("测试完毕")

def main():
    running=Running()
    running.run()
if __name__ == '__main__':
    main()


import os
import random

import pytest

import dropboxer
from dropboxer.models import (
    TransferPath,
    TransferRule,
    TransferRulePath,
    transaction,
    )

@pytest.mark.parametrize('type', ['source', 'sink'])
@pytest.mark.path
@pytest.mark.create
def test_create_path(sandbox, fake, type):
    name = fake.word()
    path = os.path.join(sandbox, name)
    with transaction() as session:
        transfer_path = TransferPath.create(
            session,
            name=name,
            path=path,
            type=type)
        id = transfer_path.id

    # fetch it by its id
    with transaction() as session:
        transfer_path = session.query(TransferPath).get(id)
        assert transfer_path.id == id
    # fetch it by its path
    with transaction() as session:
        transfer_path = session.query(TransferPath) \
                .filter(TransferPath.path == path) \
                .one()
        assert transfer_path.id == id

@pytest.mark.path
@pytest.mark.create
@pytest.mark.invalid
def test_create_path_that_already_exists(sandbox, fake):
    name = fake.word()
    path = os.path.join(sandbox, name)

    # create a path
    with transaction() as session:
        transfer_path = TransferPath.create(
            session,
            name=name,
            path=path,
            type='source')

    # try to create the same path
    with transaction() as session:
        invalid_transfer_path = TransferPath.create(
            session,
            name=name,
            path=path,
            type='source')
        assert invalid_transfer_path is False

@pytest.mark.path
@pytest.mark.delete
def test_delete_path(sandbox, fake):
    name = fake.word()
    path = os.path.join(sandbox, name)
    with transaction() as session:
        transfer_path = TransferPath.create(
            session,
            name=name,
            path=path,
            type='source')
        id_ = transfer_path.id

    with transaction() as session:
        assert TransferPath.delete(session, id_) is True

    with transaction() as session:
        assert session.query(TransferPath).get(id_) is None

@pytest.mark.path
@pytest.mark.delete
@pytest.mark.invalid
def test_delete_nonexistent_path():
    id_ = random.randint(1000, 2000)
    with transaction() as session:
        assert TransferPath.delete(session, id_) is False

@pytest.mark.rule_path
@pytest.mark.create
def test_create_rule_path(sandbox, fake):
    with transaction() as session:
        source_name = fake.word()
        source_path = os.path.join(sandbox, source_name)
        source = TransferPath.create(
            session,
            name=source_name,
            path=source_path,
            type='source')

        sink_name = fake.word()
        sink_path = os.path.join(sandbox, sink_name)
        sink = TransferPath.create(
            session,
            name=sink_name,
            path=sink_path,
            type='sink')

        rule_path = TransferRulePath.create(
            session,
            source,
            sink)
        rule_path_id = rule_path.id

    with transaction() as session:
        rule_path = session.query(TransferRulePath).get(rule_path_id)
        assert rule_path.source.path == source_path
        assert rule_path.sink.path == sink_path

@pytest.mark.rule_path
@pytest.mark.create
@pytest.mark.invalid
def test_create_rule_path_that_already_exists(sandbox, fake):
    with transaction() as session:
        source_name = fake.word()
        source_path = os.path.join(sandbox, source_name)
        source = TransferPath.create(
            session,
            name=source_name,
            path=source_path,
            type='source')

        sink_name = fake.word()
        sink_path = os.path.join(sandbox, sink_name)
        sink = TransferPath.create(
            session,
            name=sink_name,
            path=sink_path,
            type='sink')

        rule_path = TransferRulePath.create(session, source, sink)
        _rule_path = TransferRulePath.create(session, source, sink)
        assert _rule_path is False

@pytest.mark.rule
@pytest.mark.create
def test_create_rule(sandbox, fake):
    with transaction() as session:
        rule_name = fake.word()
        rule = TransferRule.create(session, name=rule_name)
        rule_id = rule.id

    with transaction() as session:
        rule = session.query(TransferRule).get(rule_id)
        assert rule.name == rule_name


import sys
import json
from pathlib import Path

class WatchlistMgmt():

    def __init__(self, watchlist):
        """
        Parameters
        ----------
        watchlist- file holding watchlist data - str
        """
        self.watchlist = watchlist

    def watchlist_read(self):
        """
        Read in watchlist data
        Returns - list
        """
        try:
            f = open(Path(self.watchlist), 'rb')
        except IOError:
            print("Could not read file:", self.watchlist)
            sys.exit()

        with f:
            data = json.load(f)

        return data

    def watchlist_add(self, symbol):
        """
        Creates watchlist if dosent exist, Adds symbol to watchlist.
        Parameters
        ----------
        symbol - str

        Returns
        -------
        Returns success or failure message
        """
        data = symbol

        # Create watchlist file if not exist
        if Path(self.watchlist).is_file() == False:
            with open(Path(self.watchlist), mode='w', encoding='utf-8') as f:
                json.dump([], f)

        # Add symbol to portfolio file
        try:
            with open(Path(self.watchlist)) as f:
                load_data = json.load(f)

                load_data['watchlist'].append(data)
                json.dump(load_data, sys.stdout, indent=2)

                with open(Path(self.watchlist), mode='w', encoding='utf-8') as f:
                    json.dump(load_data, f, sort_keys=True, indent=2, ensure_ascii=False)

                return symbol + ' added to your watchlist'
        except Exception as e:
            return str(e)

    def watchlist_remove(self, symbol):
        """
        Removes symbol and transaction to watchlist.
        Parameters
        ----------
        symbol - str

        Returns
        -------
        Returns success or failure message
        """
        data = symbol

        # remove symbol from portfolio file
        try:
            with open(Path(self.watchlist)) as f:
                load_data = json.load(f)

                load_data['watchlist'].remove(data)
                json.dump(load_data, sys.stdout, indent=2)

                with open(Path(self.watchlist), mode='w', encoding='utf-8') as f:
                    json.dump(load_data, f, sort_keys=True, indent=2, ensure_ascii=False)

                return symbol + ' added to your watchlist'
        except Exception as e:
            return str(e)


infix = input('Exp with spaces:   ').split()
postfix = ''
operators = ['^','*','/','+','-']
parenthesis = ['(',')']
priorityDict = {'^':5,'*':4,'/':3,'+':2,'-':1}

class Stack:
    def __init__(self,si):
        self.size = si
        self.top = -1
        self.stack = ['']*self.size

    def push(self,data):
        if self.top >= self.size-1:
            raise Exception('Stack Full')
        else:
            self.top += 1
            self.stack[self.top] = data
            # print('one push',st.stack)

    def pop(self):
        if self.top <0:
            raise Exception('Stack Empty')
        else:
            self.stack[self.top] = ''
            self.top -= 1

    def empty(self,one):
        isEmpty = False
        if self.top == -1 or st.stack[0] == '(':
            isEmpty = True

        return isEmpty

st = Stack(len(infix))

for one in infix:
    if one.isalpha():
        print(1)
        postfix += one
        print('1 Done')

    elif one == '(':
        print(2)
        st.push(one)

    elif one in operators and (st.empty(one) == True or priorityDict[st.stack[st.top]] < priorityDict[one]):
        print(3)
        st.push(one)

    elif one in operators and priorityDict[st.stack[st.top]] > priorityDict[one]:
        print(4)
        while st.top != -1:
            if st.stack[st.top] >= one:
                postfix += st.stack[st.top]
                st.pop()
            else:
                break
        st.push(one)

    elif one == ')':
        print(5)
        while st.stack[st.top] != '(':
            postfix += st.stack[st.top]
            st.pop()
        if st.stack[st.top] == '(':
            st.pop()
print(6)
if st.top != -1:
    print(f"top {st.top}")
    print(7)
    while st.top != -1:
        # print('whats popping')
        postfix += st.stack[st.top]
        st.pop()
    print('7 Done')
print(8)
print(postfix)
print(st.stack)


from dinosaur import Dinosaur
from robot import Robot

class Battlefield:
    def __init__(self):
        self.robot = Robot("Jerry")
        self.dinosaur = Dinosaur("Fredrick",40)

    def run_game(self):

        self.display_welcome()
        self.battle_phase()
        self.display_winner()

    def display_welcome(self):
        print(f"\n\nHello today you will be witnessing a battle between {self.robot.name} and {self.dinosaur.name}")

    def battle_phase(self):
        while self.robot.health > 0 and self.dinosaur.health > 0:
            self.robot.attack(self.dinosaur)
            if self.dinosaur.health <= 0:
                print(f"\n{self.dinosaur.name} is dead.")
                break
            self.dinosaur.attack(self.robot)
            if self.robot.health <= 0:
                print(f"\n{self.robot.name} is dead.")
                break

    def display_winner(self):
        if self.robot.health > self.dinosaur.health:
            print(f"{self.robot.name} is the winner!!!")
            print("Humanity's creation made them extinct!!! Again....")
        else:
            print(f"{self.dinosaur.name} is the winner!!!")
            print("Welcome to the dark ages...")


import sys
import re

for line in sys.stdin:
    line = line.rstrip()
    result = re.search(r"[01]+", line)
    if not result:
        break
    even, noteven = 0, 0
    for ind, itm in enumerate(line):
        if re.match(r"1", itm):
            if ind % 2 == 0:
                even += 1
            else:
                noteven += 1
    if (noteven - even) % 3 == 0:
        print(line)

    if re.match(r"^(0|(1(01*0)*1))*$", line):
        print(f"got: {line}")
    if re.fullmatch(r"(0|(1(01*0)*1))*", line):
        print(f"got: {line}")


import numpy as np
import random
import os

from PIL import Image
from sklearn.model_selection import train_test_split

generic_path = '../../model/dataset'
image_counter = 0

def rename_data():
    global generic_path, image_counter

    for i in range(1, 6):
        path_i = generic_path + '/' + str(i)
        for entry in os.scandir(path_i):
            source = path_i + '/' + entry.name
            destination = path_i + '/' + 'image_class' + str(i) + '_' + str(image_counter) + '.jpg'
            os.rename(source, destination)
            image_counter += 1

def get_image_entries(directory_path):
    entries = []
    for entry in os.scandir(directory_path):
        entries.append(entry)

    return entries

def get_image_entries_main():
    global generic_path

    entries = []
    for i in range(1, 6):
        path_i = generic_path + '/' + str(i)
        entries_i = get_image_entries(path_i)
        entries += entries_i

    return entries

def get_data(x):
    global generic_path

    X = []
    Y = []
    for entry in x:
        if '_class1_' in entry.name:
            label = 0
        elif '_class2_' in entry.name:
            label = 1
        elif '_class3_' in entry.name:
            label = 2
        elif '_class4_' in entry.name:
            label = 3
        elif '_class5_' in entry.name:
            label = 4
        image_path = generic_path + '/' + str(label+1) + '/' + entry.name
        image = Image.open(image_path).convert('RGB')
        image_array = np.array(image)
        X.append(image_array)
        Y.append(label)

    return X, Y

def get_train_test_split():
    x_entries = get_image_entries_main()
    random.shuffle(x_entries)
    X, Y = get_data(x_entries)
    X = np.array(X)
    Y = np.array(Y)

    return train_test_split(X, Y, test_size=.33, random_state=42)

# rename_data()


import pytest

from convex.common import Instrument
from convex.exchanges import ExchangeID

def test_from_string():
    s = 'BTCUSD@GDAX'
    inst = Instrument.from_string(s)
    assert inst.base_currency == 'BTC'
    assert inst.quote_currency == 'USD'
    assert inst.exchange_id == ExchangeID.GDAX

def test_from_string_invalid_base():
    s = 'UNKUSD@GDAX'
    with pytest.raises(ValueError):
        Instrument.from_string(s)


# This file is part of Fail2Ban.
#
# Fail2Ban is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Fail2Ban is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Fail2Ban; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# Author: Cyril Jaquier
#
# $Revision$

__author__ = "Cyril Jaquier"
__version__ = "$Revision$"
__date__ = "$Date$"
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
__license__ = "GPL"

from failmanager import FailManagerEmpty
from filter import FileFilter
from mytime import MyTime

import time, logging, os

# Gets the instance of the logger.
logSys = logging.getLogger("fail2ban.filter")

##
# Log reader class.
#
# This class reads a log file and detects login failures or anything else
# that matches a given regular expression. This class is instanciated by
# a Jail object.

class FilterPoll(FileFilter):

	##
	# Constructor.
	#
	# Initialize the filter object with default values.
	# @param jail the jail object

	def __init__(self, jail):
		FileFilter.__init__(self, jail)
		self.__modified = False
		## The time of the last modification of the file.
		self.__lastModTime = dict()
		self.__file404Cnt = dict()
		logSys.debug("Created FilterPoll")

	##
	# Add a log file path
	#
	# @param path log file path

	def addLogPath(self, path, tail = False):
		if self.containsLogPath(path):
			logSys.error(path + " already exists")
		else:
			self.__lastModTime[path] = 0
			self.__file404Cnt[path] = 0
			FileFilter.addLogPath(self, path, tail)
			logSys.info("Added logfile = %s" % path)

	##
	# Delete a log path
	#
	# @param path the log file to delete

	def delLogPath(self, path):
		if not self.containsLogPath(path):
			logSys.error(path + " is not monitored")
		else:
			del self.__lastModTime[path]
			del self.__file404Cnt[path]
			FileFilter.delLogPath(self, path)
			logSys.info("Removed logfile = %s" % path)

	##
	# Main loop.
	#
	# This function is the main loop of the thread. It checks if the
	# file has been modified and looks for failures.
	# @return True when the thread exits nicely

	def run(self):
		self.setActive(True)
		while self._isActive():
			if not self.getIdle():
				# Get file modification
				for container in self.getLogPath():
					if self.isModified(container.getFileName()):
						self.getFailures(container.getFileName())
						self.__modified = True

				if self.__modified:
					try:
						while True:
							ticket = self.failManager.toBan()
							self.jail.putFailTicket(ticket)
					except FailManagerEmpty:
						self.failManager.cleanup(MyTime.time())
					self.dateDetector.sortTemplate()
					self.__modified = False
				time.sleep(self.getSleepTime())
			else:
				time.sleep(self.getSleepTime())
		logSys.debug(self.jail.getName() + ": filter terminated")
		return True

	##
	# Checks if the log file has been modified.
	#
	# Checks if the log file has been modified using os.stat().
	# @return True if log file has been modified

	def isModified(self, filename):
		try:
			logStats = os.stat(filename)
			self.__file404Cnt[filename] = 0
			if self.__lastModTime[filename] == logStats.st_mtime:
				return False
			else:
				logSys.debug(filename + " has been modified")
				self.__lastModTime[filename] = logStats.st_mtime
				return True
		except OSError:
			logSys.error("Unable to get stat on " + filename)
			self.__file404Cnt[filename] = self.__file404Cnt[filename] + 1
			if self.__file404Cnt[filename] > 2:
				logSys.warn("Too much read error. Set the jail idle")
				self.jail.setIdle(True)
				self.__file404Cnt[filename] = 0
			return False


from dagster_dbt import load_assets_from_dbt_project, DbtCliClientResource
from dagster import Definitions, file_relative_path

DBT_PROJECT_PATH = file_relative_path(__file__, "../brazillian_ecom")
DBT_PROFILES = file_relative_path(__file__, "../brazillian_ecom")

dbt_assets = load_assets_from_dbt_project(
    profiles_dir=DBT_PROFILES,
    project_dir=DBT_PROJECT_PATH,
    key_prefix=["ecommerce"]
)

resources = {
    "dbt": DbtCliClientResource(
        profiles_dir=DBT_PROFILES,
        project_dir=DBT_PROJECT_PATH
    )
}

defs = Definitions(assets=dbt_assets, resources=resources)


# -*- coding: utf-8 -*-
from apps.events import makers
from apps.events.models import Lecture
from apps.events.factories import LectureFactory
from apps.common_app.tests.helpers import TestConditionBase

class TestLectureIsWithPublishedEvent(TestConditionBase):
    model = Lecture
    queryset_method_name = 'filter_by_with_published_event'
    instance_property_name = 'is_with_published_event'

    def create_instance(self):
        return LectureFactory()

    def test_should_be_true__if_event_is_published(self):
        msg = 'lecture should be with published event if event is published'

        makers.event.make_published(self.instance.event)

        self.assertConditionTrue(msg=msg)

    def test_should_be_false_if_event_is_not_published(self):
        msg = 'lecture should be without published event if event is not published'

        makers.event.make_not_published(self.instance.event)

        self.assertConditionFalse(msg=msg)

    def test_make_with_published_event(self):
        msg = "make_with_published_event should make lecture with published event"

        # make lecture.event not published by hand
        makers.event.make_not_published(self.instance.event)

        makers.lecture.make_with_published_event(self.instance) # BANG!

        self.assertConditionTrue(msg=msg)

    def test_make_not_with_published_event(self):
        msg = "make_not_with_published_event should make lecture without published event"

        # make lecture.event published by hand
        makers.event.make_published(self.instance.event)

        makers.lecture.make_not_with_published_event(self.instance) # BANG!

        self.assertConditionFalse(msg=msg)


from tkinter import *
from tkinter import messagebox
import tkinter as tk

window = Tk()
window.title("TIC-TAC-TOE")
window.resizable(0,0)
window.geometry("450x640")

clicked = True
count = 0

def btn_click(btn):
    global clicked, count
    if btn["text"] == " " and clicked == True:
        btn["text"] = "X"
        btn["bg"] = "powderblue"
        clicked = False
        count += 1
        win()

    elif btn["text"] == " " and clicked == False:
        btn["text"] = "O"
        btn["bg"] = "green"
        clicked = True
        count += 1
        win()

    else:
        messagebox.showerror("TIC-TAC-TOE", "Button already clicked")

def reset():
    global clicked, count
    li = [b1,b2,b3,b4,b5,b6,b7,b8,b9]
    for i in li:
        i["bg"] = "white"
        clicked = True
        count = 0
        i["text"] = " "

# reset button

reset_btn = Button(window, text="RESET", width=20, height=8, command=reset)
reset_btn.place(x=150,y=510)

# Player Names Entries

def Player_Names():
    global player1, player2
    player1 = Entry(window, width=20, font=("Arial Bold", 20), fg="powderblue")
    player2 = Entry(window, width=20, font=("Arial Bold", 20), fg="green")
    player1.place(x=100 ,y=0)
    player2.place(x=100, y=50)
    player1.focus()

# Check if someone wins

def win():
    global winner
    winner = False
    if b1["text"] == "X" and b2["text"] == "X" and b3["text"] == "X" or b4["text"] == "X" and b5["text"] == "X" and b6["text"] == "X" or b7["text"] == "X" and b8["text"] == "X" and b9["text"] == "X":
        if player1.get() != "":
            messagebox.showinfo("TIC-TAC-TOE", "{} Wins".format(player1.get()))
            reset()
            winner = True

        else:
            messagebox.showinfo("TIC-TAC-TOE", "X Wins")
            reset()
            winner = True

    elif b1["text"] == "X" and b5["text"] == "X" and b9["text"] == "X" or b3["text"] == "X" and b5["text"] == "X" and b7["text"] == "X":
        if player1.get() != "":
            messagebox.showinfo("TIC-TAC-TOE", "{} Wins".format(player1.get()))
            reset()
            winner = True

        else:
            messagebox.showinfo("TIC-TAC-TOE", "X Wins")
            reset()
            winner = True

    elif b1["text"] == "X" and b4["text"] == "X" and b7["text"] == "X" or b2["text"] == "X" and b5["text"] == "X" and b8["text"] == "X" or b3["text"] == "X" and b6["text"] == "X" and b9["text"] == "X":
        if player1.get() != "":
            messagebox.showinfo("TIC-TAC-TOE", "{} Wins".format(player1.get()))
            reset()
            winner = True

        else:
            messagebox.showinfo("TIC-TAC-TOE", "X Wins")
            reset()
            winner = True

    if b1["text"] == "O" and b2["text"] == "O" and b3["text"] == "O" or b4["text"] == "O" and b5["text"] == "O" and b6["text"] == "O" or b7["text"] == "O" and b8["text"] == "O" and b9["text"] == "O":
        if player1.get() != "":
            messagebox.showinfo("TIC-TAC-TOE", "{} Wins".format(player2.get()))
            reset()
            winner = True

        else:
            messagebox.showinfo("TIC-TAC-TOE", "O Wins")
            reset()
            winner = True

    elif b1["text"] == "O" and b5["text"] == "O" and b9["text"] == "O" or b3["text"] == "O" and b5["text"] == "O" and b7["text"] == "O":
        if player1.get() != "":
            messagebox.showinfo("TIC-TAC-TOE", "{} Wins".format(player2.get()))
            reset()
            winner = True

        else:
            messagebox.showinfo("TIC-TAC-TOE", "O Wins")
            reset()
            winner = True

    elif b1["text"] == "O" and b4["text"] == "O" and b7["text"] == "O" or b2["text"] == "O" and b5["text"] == "O" and b8["text"] == "O" or b3["text"] == "O" and b6["text"] == "O" and b9["text"] == "O":
        if player1.get() != "":
            messagebox.showinfo("TIC-TAC-TOE", "{} Wins".format(player2.get()))
            reset()
            winner = True

        else:
            messagebox.showinfo("TIC-TAC-TOE", "O Wins")
            reset()
            winner = True

    elif count == 9 and winner == False:
        messagebox.showinfo("TIC-TAC-TOE", "This is a TIE")
        reset()

# Player 1 Labels

player1_lbl = Label(window, text="Player 1", font=("Arial Bold", 15), fg="powderblue")
player2_lbl = Label(window, text="Player 2", font=("Arial Bold", 15), fg="green")
player1_lbl.place(x=0,y=0)
player2_lbl.place(x=0,y=50)

# Buttons

def Buttons():
    global b1,b2,b3,b4,b5,b6,b7,b8,b9
    b1 = Button(window, text=" ", width=20, height=8, command = lambda : btn_click(b1))
    b2 = Button(window, text=" ", width=20, height=8, command = lambda : btn_click(b2))
    b3 = Button(window, text=" ", width=20, height=8, command = lambda : btn_click(b3))
    b4 = Button(window, text=" ", width=20, height=8, command = lambda : btn_click(b4))
    b5 = Button(window, text=" ", width=20, height=8, command = lambda : btn_click(b5))
    b6 = Button(window, text=" ", width=20, height=8, command = lambda : btn_click(b6))
    b7 = Button(window, text=" ", width=20, height=8, command = lambda : btn_click(b7))
    b8 = Button(window, text=" ", width=20, height=8, command = lambda : btn_click(b8))
    b9 = Button(window, text=" ", width=20, height=8, command = lambda : btn_click(b9))

# Displaying Buttons

def Display_Buttons():
    b1.place(x=0,y=150)
    b2.place(x=150,y=150)
    b3.place(x=300,y=150)
    b4.place(x=0,y=280)
    b5.place(x=150,y=280)
    b6.place(x=300, y=280)
    b7.place(x=0,y=410)
    b8.place(x=150,y=410)
    b9.place(x=300,y=410)

# Calling Functions

Buttons()
Display_Buttons()
Player_Names()

# Calling The Main Loop (running the TIC-TAC-TOE Game)

window.mainloop()


import os
import socket
import sys
import time
import getpass
from cryptography.fernet import Fernet

# ------------------------------------------------------------------------------
host =socket.gethostname()
username =getpass.getuser()
path = os.environ.get("MUMMI_ROOT","./")
keyFile = open(os.path.abspath(path) + "/cmdKeyFile","r")
keyLine = keyFile.readline()
keyLine = keyLine.rstrip("\r\n")
key, serverHost, port, serverUserId = keyLine.split(" ")
keyFile.close()
fCoder = Fernet(key)

#sock = socket.create_connection((serverHost, port)) # Create a TCP/IP socket

# ------------------------------------------------------------------------------
def main():
    print("Configuration:\nHost: ", serverHost, "Port: ", port, "serverUserId: ", serverUserId)
    try:
        while 1:
            command = input("command: ") # Send data
            sock = socket.create_connection((serverHost, port)) # Create a TCP/IP socket
            if command.lower() == "exit":
                sock.shutdown(socket.SHUT_RDWR)
                sock.close()
                sys.exit(0)
            message = key + " " + host + " " + username + " " + command
            message = fCoder.encrypt(message.encode())
            sock.sendall(message)

            amount_received = 0
            amount_expected =  1

            while (1):
                data = sock.recv(256)
                amount_received += len(data)
                sys.stdout.write(data.decode("utf-8"))
                if (len(data) < 256):
                    break
            sock.shutdown(socket.SHUT_RDWR)
            sock.close()
    except KeyboardInterrupt:
        print('Exiting...')
    finally:
        print('closing socket')
        sock.close()

# ------------------------------------------------------------------------------
if __name__ == "__main__":
    main()

# ------------------------------------------------------------------------------


f1=open('E:/PythonStudy_Git/调用资料/106.txt','r',encoding='utf-8')
data=[i.strip() for i in f1.readlines()]

def pickdb(data):
	db=[]
	for i in data:
		k=0
		for i2 in i:
			if i2==' ':
				datax=[]
				datax.append(int (i[:k]))
				datax.append(int(i[k+1:]))
				db.append(tuple(datax))
				k+=1
			else:
				k+=1
	return db

def madedb(data):
	db=[]
	for i in range(data[0],data[1]):
		datax=[]
		datax.append(i)
		datax.append(i+1)
		db.append(tuple(datax))
	return db

datai=[6,12]

schedule=madedb(datai)
meetingtime=pickdb(data)

#循环次数统计
m_count=0
s_count=0
#出席时间点的名人出现次数统计字典
s_dict=dict.fromkeys(range(len(schedule)),0)

#思路是求:meetingtime名人出席时间段在schedule日程的时间段中有多少个.
for s in schedule:
	for m in meetingtime:
		if m[0]<=s[0]<m[1] or m[0]<s[1]<m[1] :
			s_dict[s_count]+=1
			m_count+=1
		else:
			m_count+=1
	s_count+=1
print(schedule)
print(s_dict)


'''
Desenvolva um programa que leia 4 valores por um teclado, e guarde-os em uma tupla. No final, mostre:
1. Quantas vezes apareceu o valor 9.
2. Em que posição foi digitado o primeiro valor 3.
3. Quais foram os números pares.
'''
tupla = (int(input('Número 1: ')),
        int(input('Número 1: ')),
        int(input('Número 1: ')),
        int(input('Número 1: ')))

if 9 in tupla:
    print(f'''1. O valor 9 apareceu {tupla.count(9)}x;''')
else:
    print(f'1. O valor 9 não apareceu nessa lista.')

if 3 in tupla:
    print(f'2. A primeira posição de 3 foi na {tupla.index(3) + 1};')
else:
    print('2. Não houve digitação do número 3 desta vez.')

print('3. Os números pares dessa seleção foram: ', end = '')
for cont in range(0, len(tupla)):
    if tupla[cont] % 2 == 0:
        print(tupla[cont], end = ' ')

print('''.
Fim.''')


class Rectangle:

    def __init__(self, height, width):
        self.height = height
        self.width = width
        self.area = height*width

    @staticmethod
    def add_area (rectangle1, rectangle2):
        return rectangle1.area + rectangle2.area

x = Rectangle(2,4)
y = Rectangle(4,3)

print(Rectangle.add_area(x, y))


import random

word_list = ["sahil","lucky","azil","amir","danish","wazed","shadab"]
chosen_word = random.choice(word_list)
end_game = False
lives = 6
display = []
for _ in range(len(chosen_word)):
    display += "_"
while not end_game:
    guess = input("Guess a letter: ")
    for pos in range(len(chosen_word)):
      if chosen_word[pos] == guess:
        display[pos] = guess
    print(f"{' '.join(display)}")
    if guess not in chosen_word:
        lives -= 1
        print(f"Oops! you guessed a wrong letter {guess}, it is not in the word.{lives} chances left")
        if lives == 0:
            end_game = True
            print("You loose")
    if "_" not in display:
        end_game = True
        print("You win")


# -*- coding: utf-8 -*-
"""
Created on Wed Jul 15 21:08:50 2020

@author: jacesca
Aditional documentation:
    Source of the code: https://nlpforhackers.io/named-entity-extraction/
    Classes: https://www.w3schools.com/python/python_classes.asp
Download the 2.2.0 version of the corpus here: https://gmb.let.rug.nl/data.php
"""

print("****************************************************")
step = "BEGIN"; print("** %s" % step)
print("****************************************************")
step = "Importing libraries.."; print("** %s" % step)

#import os
import collections
import string
import pickle

from zipfile import ZipFile
from pprint import pprint
from collections.abc import Iterable

from nltk import word_tokenize
from nltk import pos_tag
from nltk import ne_chunk
from nltk.chunk import conlltags2tree
from nltk.chunk import tree2conlltags
from nltk.stem.snowball import SnowballStemmer
from nltk.tag import ClassifierBasedTagger
from nltk.chunk import ChunkParserI

print("****************************************************")
step = "NLTK NER Chunker\n"; print("** %s" % step)
# NLTK has a standard NE annotator so that we can get started pretty quickly.

sentence = "Mark and John are working at Google."
print(f'my sentence: \n"{sentence}"\n')
chunk_sent = ne_chunk(pos_tag(word_tokenize(sentence)))
print(f'{chunk_sent}\n')

# ne_chunk needs part-of-speech annotations to add NE labels to the sentence.
# The output of the ne_chunk is a nltk.Tree object.
# The ne_chunk function acts as a chunker, meaning it produces 2-level trees:
#    Nodes on Level-1: Outside any chunk
#    Nodes on Level-2: Inside a chunk – The label of the chunk is denoted by the label of the subtree
# In this example, Mark/NNP is a level-2 leaf, part of a PERSON chunk. and/CC
# is a level-1 leaf, meaning it’s not part of any chunk.

print("****************************************************")
step = "IOB tagging\n"; print("** %s" % step)
# nltk.Tree is great for processing such information in Python, but it’s not the
# standard way of annotating chunks. Maybe this can be an article on its own but
# we’ll cover this here really quickly.
# The IOB Tagging system contains tags of the form:
#    B-{CHUNK_TYPE} – for the word in the Beginning chunk
#    I-{CHUNK_TYPE} – for words Inside the chunk
#    O – Outside any chunk
# A sometimes used variation of IOB tagging is to simply merge the B and I tags:
#    {CHUNK_TYPE} – for words inside the chunk
#    O – Outside any chunk
# We usually want to work with the proper IOB format.
# Here’s how to convert between the nltk.Tree and IOB format:

sentence = "Mark and John are working at Google."
ne_tree = ne_chunk(pos_tag(word_tokenize(sentence)))

iob_tagged = tree2conlltags(ne_tree)
print(f'Return a list of 3-tuples containing (word, tag, IOB-tag):\n {iob_tagged}\n')

ne_tree = conlltags2tree(iob_tagged)
print(f'Convert the CoNLL IOB format to a tree:\n {ne_tree}\n')

print("****************************************************")
step = "GMB corpus\n"; print("** %s" % step)
# NLTK doesn’t have a proper English corpus for NER. It has the CoNLL 2002 Named
# Entity CoNLL but it’s only for Spanish and Dutch. You can definitely try the
# method presented here on that corpora. In fact doing so would be easier because
# NLTK provides a good corpus reader. We are going with Groningen Meaning Bank (GMB)
# though.
# GMB is a fairly large corpus with a lot of annotations. Unfortunately, GMB is
# not perfect. It is not a gold standard corpus, meaning that it’s not completely
# human annotated and it’s not considered 100% correct. The corpus is created by
# using already existed annotators and then corrected by humans where needed.

###############################################################################
## If you have uncompress data into a folder
###############################################################################
"""
ner_tags = collections.Counter()
corpus_root = "gmb-2.2.0"   # Make sure you set the proper path to the unzipped corpus
for root, dirs, files in os.walk(corpus_root):
    #print(root, dirs, files)
    for filename in files:
        if filename.endswith(".tags"):
            with open(os.path.join(root, filename), 'rb') as file_handle:
                file_content = file_handle.read().decode('utf-8').strip()
                annotated_sentences = file_content.split('\n\n')   # Split sentences
                for annotated_sentence in annotated_sentences:
                    annotated_tokens = [seq for seq in annotated_sentence.split('\n') if seq]  # Split words
                    for idx, annotated_token in enumerate(annotated_tokens):
                        annotations = annotated_token.split('\t')   # Split annotations
                        word, tag, ner = annotations[0], annotations[1], annotations[3]

                        ner_tags[ner] += 1
"""

###############################################################################
## If you have the data in a zipfile
###############################################################################
ner_tags = collections.Counter()
zip_file = 'gmb-2.2.0.zip'
with ZipFile(zip_file) as myzip:
    for filename in myzip.namelist():
        if filename.endswith(".tags"):
            with myzip.open(filename, 'r') as file_handle:
                file_content = file_handle.read().decode('utf-8').strip()
                #print(f'"{filename}" read...')
                annotated_sentences = file_content.split('\n\n')   # Split sentences
                for annotated_sentence in annotated_sentences:
                    annotated_tokens = [seq for seq in annotated_sentence.split('\n') if seq]  # Split words
                    for idx, annotated_token in enumerate(annotated_tokens):
                        annotations = annotated_token.split('\t')   # Split annotations
                        word, tag, ner = annotations[0], annotations[1], annotations[3]
                        ner_tags[ner] += 1
print(f'Words = {sum(ner_tags.values())}')
print(f'{ner_tags}\n\n')

## Out:
## Counter({'O': 1146068, 'geo-nam': 58388, 'org-nam': 48034, 'per-nam': 23790, 'gpe-nam': 20680,
##          'tim-dat': 12786, 'tim-dow': 11404, 'per-tit': 9800, 'per-fam': 8152, 'tim-yoc': 5290,
##          'tim-moy': 4262, 'per-giv': 2413, 'tim-clo': 891, 'art-nam': 866, 'eve-nam': 602,
##          'nat-nam': 300, 'tim-nam': 146, 'eve-ord': 107, 'per-ini': 60, 'org-leg': 60,
##          'per-ord': 38, 'tim-dom': 10, 'per-mid': 1, 'art-add': 1})

print('** FIRST IMPROVEMENT...')
# Let’s interpret the tags a bit. We can observe that the tags are composed (Except for O of course)
# as such: {TAG}-{SUBTAG}. Here’s what the top-level categories mean:
#     geo = Geographical Entity
#     org = Organization
#     per = Person
#     gpe = Geopolitical Entity
#     tim = Time indicator
#     art = Artifact
#     eve = Event
#     nat = Natural Phenomenon
# The subcategories are pretty unnecessary and pretty polluted. per-ini for example tags the Initial of
# a person’s name. This tag, kind of makes sense. On the other hand, it’s unclear what the difference
# between per-nam (person name) and per-giv (given name), per-fam (family-name), per-mid (middle-name).
# I decided to just remove the subcategories and focus only on the main ones. Let’s modify the code a bit:
ner_tags = collections.Counter()
zip_file = 'gmb-2.2.0.zip'
with ZipFile(zip_file) as myzip:
    for filename in myzip.namelist():
        if filename.endswith(".tags"):
            with myzip.open(filename, 'r') as file_handle:
                file_content = file_handle.read().decode('utf-8').strip()
                annotated_sentences = file_content.split('\n\n')   # Split sentences
                for annotated_sentence in annotated_sentences:
                    annotated_tokens = [seq for seq in annotated_sentence.split('\n') if seq]  # Split words
                    for idx, annotated_token in enumerate(annotated_tokens):
                        annotations = annotated_token.split('\t')   # Split annotation
                        word, tag, ner = annotations[0], annotations[1], annotations[3]
                        # Get only the primary category
                        if ner != 'O':
                            ner = ner.split('-')[0]
                        ner_tags[ner] += 1
print(f'Words = {sum(ner_tags.values())}')
print(f'{ner_tags}\n\n')

# This looks much better. You might decide to drop the last few tags because they are not well
# represented in the corpus. We’ll keep them … for now.

print("****************************************************")
step = "Knowing the data...\n"; print("** %s" % step)
print('Example of annotated_tokens: ')
pprint(annotated_tokens[:5])
print(f'\nLast word, tag, ner: {word}, {tag}, {ner}\n')

print("****************************************************")
step = "Training your own system\n"; print("** %s" % step)

def features(tokens, index, history):
    """
    `tokens`  = a POS-tagged sentence [(w1, t1), ...]
    `index`   = the index of the token we want to extract features for
    `history` = the previous predicted IOB tags
    """
    # init the stemmer
    stemmer = SnowballStemmer('english')

    # Pad the sequence with placeholders
    tokens = [('[START2]', '[START2]'), ('[START1]', '[START1]')] + list(tokens) + [('[END1]', '[END1]'), ('[END2]', '[END2]')]
    history = ['[START2]', '[START1]'] + list(history)

    # shift the index with 2, to accommodate the padding
    index += 2

    word, pos = tokens[index]
    prevword, prevpos = tokens[index - 1]
    prevprevword, prevprevpos = tokens[index - 2]
    nextword, nextpos = tokens[index + 1]
    nextnextword, nextnextpos = tokens[index + 2]

    extracted_features = {'word': word,
                          'lemma'           : stemmer.stem(word),
                          'pos'             : pos,
                          'all-ascii'       : all([True for c in word if c in string.ascii_lowercase]),
                          'contains-dash'   : '-' in word,
                          'contains-dot'    : '.' in word,
                          'all-caps'        : word == word.capitalize(),
                          'capitalized'     : word[0] in string.ascii_uppercase,

                          'prev-word'       : prevword,
                          'prev-lemma'      : stemmer.stem(prevword),
                          'prev-pos'        : prevpos,
                          'prev-iob'        : history[index - 1],
                          'prev-all-caps'   : prevword == prevword.capitalize(),
                          'prev-capitalized': prevword[0] in string.ascii_uppercase,

                          'prev-prev-word'  : prevprevword,
                          'prev-prev-pos'   : prevprevpos,

                          'next-word'       : nextword,
                          'next-lemma'      : stemmer.stem(nextword),
                          'next-pos'        : nextpos,
                          'next-all-caps'   : nextword == prevword.capitalize(),
                          'next-capitalized': nextword[0] in string.ascii_uppercase,

                          'next-next-word'  : nextnextword,
                          'next-next-pos'   : nextnextpos,}
    return extracted_features

# The feature extraction works almost identical as the one implemented in the Training a Part-Of-Speech Tagger,
# (https://nlpforhackers.io/training-pos-tagger/) except we added the history mechanism. Since the previous IOB
# tag is a very good indicator of what the current IOB tag is going to be, we have included the previous IOB tag
# as a feature.

# Let’s create a few utility functions to help us with the training and move the corpus reading stuff into a
# function, read_gmb:

def to_conll_iob(annotated_sentence):
    """
    `annotated_sentence` = list of triplets [(w1, t1, iob1), ...]
    Transform a pseudo-IOB notation: O, PERSON, PERSON, O, O, LOCATION, O
    to proper IOB notation: O, B-PERSON, I-PERSON, O, O, B-LOCATION, O
    """
    proper_iob_tokens = []
    for idx, annotated_token in enumerate(annotated_sentence):
        word, tag, ner = annotated_token

        if ner != 'O':
            if idx == 0:
                ner = "B-" + ner
            elif annotated_sentence[idx - 1][2] == ner:
                ner = "I-" + ner
            else:
                ner = "B-" + ner
        proper_iob_tokens.append((word, tag, ner))
    return proper_iob_tokens

def read_gmb(corpus_root):
    with ZipFile(zip_file) as myzip:
        for filename in myzip.namelist():
            if filename.endswith(".tags"):
                with myzip.open(filename, 'r') as file_handle:
                    file_content = file_handle.read().decode('utf-8').strip()
                    annotated_sentences = file_content.split('\n\n')
                    for annotated_sentence in annotated_sentences:
                        annotated_tokens = [seq for seq in annotated_sentence.split('\n') if seq]

                        standard_form_tokens = []

                        for idx, annotated_token in enumerate(annotated_tokens):
                            annotations = annotated_token.split('\t')
                            word, tag, ner = annotations[0], annotations[1], annotations[3]

                            if ner != 'O':
                                ner = ner.split('-')[0]

                            if tag in ('LQU', 'RQU'):   # Make it NLTK compatible
                                tag = "``"

                            standard_form_tokens.append((word, tag, ner))

                        conll_tokens = to_conll_iob(standard_form_tokens)

                        # Make it NLTK Classifier compatible - [(w1, t1, iob1), ...] to [((w1, t1), iob1), ...]
                        # Because the classfier expects a tuple as input, first item input, second the class
                        yield [((w, t), iob) for w, t, iob in conll_tokens]

zip_file = 'gmb-2.2.0.zip'
reader = read_gmb(zip_file)

# Check the output:
print('Cheking the output: ')
pprint(reader.__next__())

# We managed to read sentences from the corpus in a proper format. We can now start to
# actually train a system. NLTK offers a few helpful classes to accomplish the task.
# nltk.chunk.ChunkParserI is a base class for building chunkers/parsers. Another useful
# asset we are going to use is the nltk.tag.ClassifierBasedTagger. Under the hood, it
# uses a NaiveBayes classifier for predicting sequences.

class NamedEntityChunker(ChunkParserI):
    def __init__(self, train_sents, **kwargs):
        assert isinstance(train_sents, Iterable)

        self.feature_detector = features
        self.tagger = ClassifierBasedTagger(
            train=train_sents,
            feature_detector=features,
            **kwargs)

    def parse(self, tagged_sent):
        chunks = self.tagger.tag(tagged_sent)

        # Transform the result from [((w1, t1), iob1), ...]
        # to the preferred list of triplets format [(w1, t1, iob1), ...]
        iob_triplets = [(w, t, c) for ((w, t), c) in chunks]

        # Transform the list of triplets to nltk.Tree format
        return conlltags2tree(iob_triplets)

# Let’s build the datasets:
zip_file = 'gmb-2.2.0.zip'
reader = read_gmb(zip_file)
data = list(reader)
training_samples = data[:int(len(data) * 0.9)]
test_samples = data[int(len(data) * 0.9):]

print("\nTraining samples = %s" % len(training_samples))    # training samples = 55809
print("Test samples = %s\n" % len(test_samples))            # test samples = 6201

# We built everything up to this point so beautifully such that the training can be
# expressed as simply as:
chunker = NamedEntityChunker(training_samples[:2000])

# It probably took a while. Let’s take it for a spin:
sentence = "I'm going to Germany this Monday."
nktl_tree_chunk = chunker.parse(pos_tag(word_tokenize(sentence)))
print(f'Testing: \nMy sentence: "{sentence}"')
print(nktl_tree_chunk)
nktl_tree_chunk.pretty_print()

# The system you just trained did a great job at recognizing named entities:
# - Named Entity “Germany” – Geographical Entity
# - Named Entity “Monday” – Time Entity

print("****************************************************")
step = "Testing the system\n"; print("** %s" % step)

# Let’s see how the system measures up. Because we followed to good patterns in NLTK, we can test
# our NE-Chunker as simple as this:
score = chunker.evaluate([conlltags2tree([(w, t, iob) for (w, t), iob in iobs]) for iobs in test_samples[:500]])
print(score.accuracy())

print("****************************************************")
step = "Conclusion\n"; print("** %s" % step)

# (*) Chunking can be reduced to a tagging problem.
# (*) Named Entity Recognition is a form of chunking.
# (*) We explored a freely available corpus that can be used for real-world applications.
# (*) The NLTK classifier can be replaced with any classifier you can think about. Try replacing it
#     with a scikit-learn classifier.
# If you loved this tutorial, you should definitely check out the sequel: Training a NER system on a
# large dataset. It builds upon what you already learned, it uses a scikit-learn classifier and pushes
# the accuracy to 97%.
#
# Notes
# - I’ve used NLTK version 3.2.1
# - You can find the entire code here: Python NER Gist

print("\n****************************************************")
step = "END"; print("** %s" % step)
print("****************************************************")

Introduction to Natural Language Processing in Python/Named_Entity_Recognition_Example2.py


import numpy as np
from scipy.optimize import minimize

def fitting_expression(id3d, pt2d, rot, t3d, f, w_exp):
    """

    :param id3d: 261 x 47 array. 261 = 3 x 87, 47 refers to 47 expressions.
    :param pt2d: 87 x 2 array. Landmarks of current photo/frame.
    :param rot: 3 x 3 Rotation matrix from pose estimation.
    :param t3d: （3,) np array. Estimated via fitting_pose.
    :param f: A scalar. Estimated via fitting_pose.
    :param w_exp: 47 x 1 np array. The initial values for optimization.
    :return: the optimized w_exp.
    """

    w_exp.flatten()
    test_a = id3d[3:6, :]
    id3d = id3d.T.reshape(-1, pt2d.shape[0], 3).transpose(2, 1, 0)   # 3 x 87 x 47
    test_b = id3d[:, 1, :].squeeze()
    assert np.linalg.norm(test_a - test_b) < 1e-6   # test if reshaping is correct

    weight2d = f * np.tensordot(rot, id3d, axes=(1, 0))[:2, :, :]  # 2 x 87 x 47
    weight2d = weight2d.swapaxes(0,1).reshape(-1, 47)  # 174 x 47

    mean_weight = weight2d[:, 0]  # (174,)
    weight = weight2d[:, 1:]  # 174 x 46

    t2d = np.tile(t3d[:2], pt2d.shape[0]).flatten()  # (174,)
    pt2d = pt2d.flatten()  # 174 x 1

    # x should be (46,) , the expression parameters w/o the mean expression(always 1).
    # quadratic problem with L1 norm.
    beta = 0.01
    fun = lambda x : np.sum(np.square(weight.dot(x) + mean_weight + t2d - pt2d)) + beta * np.sum(np.abs(x))

    bounds = [(0,1)] * (weight2d.shape[1] - 1)  # each element should be in (0,1)

    # initial = w_exp[1:]
    initial = np.zeros(46)
    res = minimize(fun, initial, bounds=bounds)

    return np.concatenate(([1], res.x), axis=0)


import numpy as np
import config

class ExponentialMechanism:
    """
    Mecanismo exponencial
    """

    def __init__(self, graph):
        self.is_wikidata = 'wikidata' in config.url
        self.graph = graph
        self.sensitivity = self._calculate_sensitivity()
        self._create_freq_dict()

    def _calculate_sensitivity(self):
        """
        Función para calcular sensibilidad
        En este caso es 1, dado que esta dado por presencia o ausencia de una oferta
        """

        return 1

    def _create_freq_dict(self):
        """
        Función para crear diccionario de precios, cantidad y probabilidad de una oferta.
        A partir del subgrafo generado por la consulta
        """

        self.offers_count = {}
        self.offers_prop = {}
        self.offers_price = {}

        if self.is_wikidata:
            total = 0
            for element in self.graph:
                element_id = element['subject']['value']
                total += 1
                if element_id in self.offers_count.keys():
                    self.offers_count[element_id] += 1
                else:
                    self.offers_count[element_id] = 1

        else:
            for element in self.graph:
                element_id = element['@id']
                # Se obtiene los precios de la oferta, esto se utilizará para calcular la utilidad
                if 'Offer' in element_id:
                    if element_id not in self.offers_price.keys():
                        price  = element.get('http://purl.org/goodrelations/price', None)
                        if price:
                            value = element['http://purl.org/goodrelations/price'][0]['@value']
                        else:
                            value = 1
                        self.offers_price[element_id] = int(value)

                # Se cuentan las conexiones a una oferta
                if 'Retailer' in element_id:
                    for offer in element['http://purl.org/goodrelations/offers']:
                        offer_id = offer['@id']

                        if offer_id not in self.offers_count.keys():
                            self.offers_count[offer_id] = 1
                        else:
                            self.offers_count[offer_id] += 1

        total = sum(self.offers_count.values())

        for offer_id, count in self.offers_count.items():
            self.offers_prop[offer_id] = count / total

    def _exponential(self, u, e):
        """
        Retorna la probabilidad exponencial. A partir de la utilidad, epsilon y sensibilidad
        """

        return np.random.exponential(e * u / (2 * self.sensitivity))

    def query_with_dp(self, e=1, querynum=1000):
        """
        Retorna el ruido total de la consulta, dado que calcula la probabilidad exponencial para cada elemento,
        luego las suma y entrega el resultado agregado.

        Responde una lista según la cantidad de veces que se repita la consulta
        """

        response = []

        for _ in range(querynum):
            weights = []
            if self.offers_prop:
                for offer_id, prop in self.offers_prop.items():
                    if self.is_wikidata:
                        utility = self.offers_count[offer_id]
                    else:
                        utility = self.offers_price[offer_id] * prop
                    weights.append(self._exponential(utility, e))
            else:
                total = len(self.offers_price.keys())
                prop = 1 / total
                for offer_id, price in self.offers_price.items():
                    weights.append(self._exponential(price * prop, e))
            response.append(int(sum(weights)))

        return response


from shapely.geometry import Point, LineString
from geopy.geocoders import Nominatim
import matplotlib.pyplot as plt
import geopandas as gpd
from shapely import wkt
import pandas as pd
import numpy as np
import shapely
import random
import folium
import os

def get_coordinates(city_list, country):
    """Takes a list of cities and returns a dictionary of the cities and their corresponding coordinates."""
    geolocator = Nominatim(user_agent="location script")
    dicto = {}

    for city in city_list:
        try:
            location = geolocator.geocode(city + ", " + country)
            coordinate_values = (location.longitude, location.latitude)
        except Exception as e:
            dicto[city] = "Fail"

    return dicto

country_names = {"MEX": "Mexico", "NIC": "Nicaragua", "GTM": "Guatemala", "SLV": "El Salvador"}

for file in os.listdir("flows"):

    print(file)

    if ".ipynb" not in file:

        origin_country_iso3c = file[0:3]
        origin_country_name = country_names[file[0:3]]

        flows = pd.read_csv("./flows/" + file)

        cross_coords = get_coordinates(flows['cross_loc'].unique(), "Mexico")
        print("cross done")
        origin_coords = get_coordinates(flows['origin_loc'].unique(), origin_country_name)
        print("origin done")
        us_coords = get_coordinates(flows['us_loc'].unique(), "United States")
        print("us done")

        print(cross_coords)

        flows['origin_coords'] = flows['origin_loc'].map(origin_coords)
        flows['cross_coords'] = flows['cross_loc'].map(cross_coords)
        flows['us_coords'] = flows['us_loc'].str.replace("PMSA", "").str.replace("MSA", "").map(us_coords)

        flows.to_csv("./geocoded_flows/" + origin_country_iso3c + "_flows.csv", index = False)

        flow_lines = []
        for col, row in flows.iterrows():

            try:

                origin_coords = str(row.origin_coords)
                cross_coords = str(row.cross_coords)
                us_coords = str(row.us_coords)

                origin_lat = float(origin_coords.split(", ")[0].replace("(", ""))
                origin_lon = float(origin_coords.split(", ")[1].replace(")", ""))

                cross_lat = float(cross_coords.split(", ")[0].replace("(", ""))
                cross_lon = float(cross_coords.split(", ")[1].replace(")", ""))

                us_lat = float(us_coords.split(", ")[0].replace("(", ""))
                us_lon = float(us_coords.split(", ")[1].replace(")", ""))

                flow_lines.append(shapely.geometry.LineString([(origin_lat, origin_lon), (cross_lat, cross_lon), (us_lat, us_lon)]))

            except Exception as e:

                flow_lines.append(np.nan)

        flows['flow'] = flow_lines
        flows = flows[flows["crsyr"] != 9999]
        flows = flows.dropna(subset = ['flow'])
        flows.to_csv("./geocoded_flows/" + origin_country_iso3c + "_flows.csv", index = False)


'''
백준 17298
'''
n = int(input())
answer = [0] * n
A = list(map(int, input().split()))
myStack = []

for i in range(n):
    while myStack and A[myStack[-1]] < A[i]: # 오큰수 조건
        answer[myStack.pop()] = A[i] # 정답 리스트에 오큰수 저장
    myStack.append(i)

while myStack:
    answer[myStack.pop()] = -1

result = ""

for i in range(n):
    result += str(answer[i]) + " "

print(result)

Algorithm/doit_알고리즘코딩테스트/스택,큐/오큰수_구하기.py


#!/usr/bin/python3
import subprocess
import sys
import os
from collections import defaultdict

def get_list_of_staged_files():
    staged_files = subprocess.run("git diff --name-only --cached", shell=True, capture_output=True,
                                  text=True).stdout
    return staged_files.rstrip("\n").split("\n")

def group_files_by_extension(list_of_staged_files):
    staged_files_grouped_by_extension = defaultdict(list)

    for file in list_of_staged_files:
        if "." in file:
            extension_name = file[file.find(".") + 1:]
            staged_files_grouped_by_extension[extension_name].append(file)

    return staged_files_grouped_by_extension

def check_which_scala_checks_should_be_run(scala_files):
    return {
        "sbt scalafmtCheck": any("app/" in scala_file for scala_file in scala_files),
        "sbt test:scalafmtCheck": any("test/" in scala_file for scala_file in scala_files)
    }

def run_scalafmt_commands(scalafmt_commands_to_run):
    for scalafmt_command, run_scalafmt_command in scalafmt_commands_to_run.items():
        if run_scalafmt_command:
            subprocess.run(["echo", f"""\nRunning "{scalafmt_command}"\n"""])
            try:
                subprocess.run(f"{scalafmt_command}", shell=True, check=True)
            except subprocess.CalledProcessError:
                sys.exit(f"\nA error occurred after running '{scalafmt_command}'. Please check the error above.\n")

def run_npx_lint_staged():
    subprocess.run(["echo", """\nRunning "npx lint-staged"\n"""])

    try:
        subprocess.run("cd npm && npx lint-staged", shell=True, check=True)
    except subprocess.CalledProcessError:
        sys.exit("\nA ESLint error occurred. Please check the error above.\n")

def run_style_checkers(staged_files_grouped_by_extension):
    for extension, files in staged_files_grouped_by_extension.items():
        if extension in ["scala", "sc"]:
            scalafmt_commands_to_run = check_which_scala_checks_should_be_run(files)
            run_scalafmt_commands(scalafmt_commands_to_run)
        elif extension in ["ts", "scss"]:
            run_npx_lint_staged()
        else:
            continue

def get_associated_scala_tests_for_files(scala_file_names):
    test_file_names = [file_name for (parent_dir, current_dir, file_names) in os.walk("./test") for file_name in
                       file_names]
    tests_to_run = []

    for scala_file_name in scala_file_names:
        index_of_file_name = scala_file_name.rfind("/") + 1
        scala_file_name_without_path = scala_file_name[index_of_file_name:]
        index_of_extension = scala_file_name_without_path.find(".")
        scala_file_name_without_path_and_ext = scala_file_name_without_path[:index_of_extension]

        if "test/" in scala_file_name:  # if file is a test, then it doesn't need to find test file
            tests_to_run.append(scala_file_name_without_path)
            continue

        for test_file_name in test_file_names:
            if scala_file_name_without_path_and_ext in test_file_name:
                tests_to_run.append(test_file_name)

    return tests_to_run

def get_associated_scala_controllers_for_files(scala_html_files):
    associated_scala_controllers = []

    for scala_html_file in scala_html_files:
        index_of_file_name = scala_html_file.rfind("/") + 1
        scala_html_file_without_path = scala_html_file[index_of_file_name:]
        scala_html_file_without_path_and_ext = scala_html_file_without_path.replace(".scala.html", "")
        calling_view_file_pattern = f""""\.{scala_html_file_without_path_and_ext}(\""""

        controller_files = subprocess.run(
            f"""grep -rl {calling_view_file_pattern} "./app" "--exclude-dir=./app/views\"""",
            shell=True, capture_output=True, text=True).stdout

        associated_scala_controllers.extend(controller_files.rstrip("\n").split("\n"))

    return associated_scala_controllers

def run_scala_tests(list_of_scala_tests_to_run):
    file_names_without_ext = ["*" + scala_test.replace(".scala", "") for scala_test in list_of_scala_tests_to_run]
    file_names_as_string = " ".join(file_names_without_ext)
    subprocess.run(["echo", f"""\nRunning sbt "testOnly {file_names_as_string}"\n"""])

    try:
        subprocess.run(f"""sbt "testOnly {file_names_as_string}\"""", shell=True, check=True)
    except subprocess.CalledProcessError:
        sys.exit("\nA error occurred when running the Scala tests. Please check the error above.\n")

def run_npm_tests(list_of_npm_tests_to_run, run_all_tests):
    if run_all_tests:
        subprocess.run(["echo", f"""\nRunning "npm lint-staged\n"""])

        try:
            subprocess.run(f"""npx --prefix npm lint-staged""", shell=True, check=True)
        except subprocess.CalledProcessError:
            sys.exit("\nA error occurred when running npm test. Please check the error above.\n")

    else:
        file_names_as_string = " ".join(list_of_npm_tests_to_run)
        subprocess.run(["echo", f"""\nRunning "npm test -- {file_names_as_string}\n"""])

        try:
            subprocess.run(f"""npm --prefix npm test -- {file_names_as_string}""", shell=True, check=True)
        except subprocess.CalledProcessError:
            sys.exit(
                f"\nA error occurred when running npm test -- {file_names_as_string}. Please check the error above.\n")

def run_tests(staged_files_grouped_by_extension):
    list_of_scala_tests_to_run = set()

    run_all_typescript_tests = False
    list_typescript_tests_to_run = set()

    for extension, files in staged_files_grouped_by_extension.items():
        if extension in ["scala", "sc", "scala.html"]:
            if extension != "scala.html":
                # find test controllers with the same name as the controllers
                list_of_scala_tests = get_associated_scala_tests_for_files(files)
                list_of_scala_tests_to_run.update(list_of_scala_tests)
            else:
                # find controllers that call these views
                associated_controller_files = get_associated_scala_controllers_for_files(files)
                list_of_scala_tests = get_associated_scala_tests_for_files(associated_controller_files)
                list_of_scala_tests_to_run.update(list_of_scala_tests)
        elif extension == "test.ts":
            list_typescript_tests_to_run.update(files)
        elif extension == "ts":
            run_all_typescript_tests = True
        else:
            continue

    if list_of_scala_tests_to_run:
        run_scala_tests(list_of_scala_tests_to_run)

    if list_typescript_tests_to_run or run_all_typescript_tests:
        run_npm_tests(list_typescript_tests_to_run, run_all_typescript_tests)

def main():
    list_of_staged_files = get_list_of_staged_files()

    if list_of_staged_files:
        staged_files_grouped_by_extension = group_files_by_extension(list_of_staged_files)
        if any(extension in staged_files_grouped_by_extension for extension in ["scala", "sc", "ts"]):
            run_formatting_checks_response = input(
                "Run formatting checks on your staged files? Enter y or enter any key to skip: ").lower()
            run_tests_response = input("Run tests on your staged files? Enter y or enter any key to skip: ").lower()
            run_style_checkers(staged_files_grouped_by_extension) if run_formatting_checks_response == "y" else ""
            run_tests(staged_files_grouped_by_extension) if run_tests_response == "y" else ""

if __name__ == "__main__":
    main()


from interplanetary_transfer_helper_functions import *

# Load spice kernels.
spice_interface.load_standard_kernels( )

# Define directory where simulation output will be written
output_directory = "./SimulationOutput/"

###########################################################################
# RUN CODE FOR QUESTION 2 #################################################
###########################################################################

# Create body objects
bodies = create_simulation_bodies()

# Create Lambert arc state model
lambert_arc_ephemeris = get_lambert_problem_result(bodies, target_body, departure_epoch, arrival_epoch)

"""
case_i: The initial and final propagation time equal to the initial and final times of the Lambert arc.
case_ii: The initial and final propagation time shifted forward and backward in time, respectively, by ∆t=1 hour.
case_iii: The initial and final propagation time shifted forward and backward in time, respectively, by ∆t such that we start/end on the sphere of influence

"""
# List cases to iterate over. STUDENT NOTE: feel free to modify if you see fit
cases = ['case_i', 'case_ii', 'case_iii']

# Run propagation for each of cases i-iii
for case in cases:

    # Define the initial and final propagation time for the current case
    departure_epoch_with_buffer = XXXX
    arrival_epoch_with_buffer = XXXX

    # Perform propagation
    dynamics_simulator = XXXX
    write_propagation_results_to_file(
        dynamics_simulator, lambert_arc_ephemeris, "Q2a_" + str(cases.index(case)), output_directory)

    state_history = dynamics_simulator.state_history
    lambert_history = get_lambert_arc_history(lambert_arc_ephemeris, state_history)

    # For case ii, run propagation forward and backward from mid-point
    if case == 'case_ii':
        XXXX


# 识别验证码
#-----------------------------
# 执行要求：
#   1. 当前目录下需要有clf.model,即用神经网络训练好的模型
#   2. 当前目录有验证码图片
#-----------------------------
# 输出：
#   打印识别出来的验证码
#-----------------------------

import numpy as np
from sklearn.externals import joblib

def predict(fn):
    # 处理图片
    tx = Main(fn)
    tx = np.column_stack((tx,np.ones((4,1))))
    code = ''

    # load 训练好的神经网络模型
    nn_model = 'clf.model'
    clf = joblib.load(nn_model)
    # 识别
    yp = clf.predict(tx)
    # 转成字符
    for i in range(4):
        if yp[i] in range(0,10):
            code += chr(int(yp[i])+48)
        else:
            code += chr(int(yp[i])+87)
    print(code,end='')
    return code

if __name__ == '__main__':
    from Data import *
    # 验证码图片路径
    import sys
    #fn = sys.argv[1]
    #----------------
    #一下代码在node服务器调用
    import os
    path = sys.argv[1]
    f0 = os.listdir(path)
    fn = ''
    fl = [f for f in f0 if (len(f) >= 18 and f[0:14] == 'CheckCode.aspx')]
    if len(fl) == 1:
        fn = path + fl[0]
    else:
        fl.sort(key=lambda f: os.path.getmtime(path+f) if not os.path.isdir(path+f) else 0)
        fn = path + fl[-1]
    #----------------
    predict(fn)


from flask import Flask, jsonify, request, render_template
from config import config
from models import db, User

def create_app(environment):
  app = Flask(__name__)
  app.config.from_object(environment)

  with app.app_context():
    db.init_app(app)
    db.create_all()

  return app

environment = config['development']
app = create_app(environment)

@app.route('/')
def home():
  return render_template('index.html')

@app.route('/users')
def get_users():
  users = db.session.query(User).all()
  return jsonify(users=[user.to_dict() for user in users])

@app.route('/users/<int:user_id>')
def get_user(user_id):
  user = db.session.query(User).get(user_id)
  if user is None:
    return jsonify(error={"Not Found": "User doesn't exist in database"}), 404

  return jsonify({'user': user.to_dict()})

@app.route('/users', methods=['POST'])
def create_user():
  new_user = User(
    name = request.form.get('name'),
    lastName = request.form.get('lastName'),
    address = request.form.get('address')
  )

  db.session.add(new_user)
  db.session.commit()

  return jsonify(response={'success': {'user_id': new_user.user_id}})

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


import os
import numpy as np
import mindspore.dataset as de
from mindspore.mindrecord import FileWriter
import mindspore.dataset.vision.c_transforms as C
import mindspore.dataset.engine as de

class ImageDB(object):
    def __init__(self, image_annotation_file, prefix_path='', mode='train'):
        self.prefix_path = prefix_path
        self.image_annotation_file = image_annotation_file
        self.classes = ['__background__', 'face']
        self.num_classes = 2
        self.image_set_index = self.load_image_set_index()
        self.num_images = len(self.image_set_index)
        self.mode = mode

    def load_image_set_index(self):
        """Get image index

        Parameters:
        ----------
        Returns:
        -------
        image_set_index: str
            relative path of image
        """
        assert os.path.exists(self.image_annotation_file), 'Path does not exist: {}'.format(self.image_annotation_file)
        with open(self.image_annotation_file, 'r') as f:
            image_set_index = [x.strip().split(' ')[0] for x in f.readlines()]
        return image_set_index

    def load_imdb(self):
        """Get and save ground truth image database

        Parameters:
        ----------
        Returns:
        -------
        gt_imdb: dict
            image database with annotations
        """
        #cache_file = os.path.join(self.cache_path, self.name + '_gt_roidb.pkl')
        #if os.path.exists(cache_file):
        #    with open(cache_file, 'rb') as f:
        #        imdb = cPickle.load(f)
        #    print '{} gt imdb loaded from {}'.format(self.name, cache_file)
        #    return imdb

        gt_imdb = self.load_annotations()

        #with open(cache_file, 'wb') as f:
        #    cPickle.dump(gt_imdb, f, cPickle.HIGHEST_PROTOCOL)
        return gt_imdb

    def real_image_path(self, index):
        """Given image index, return full path

        Parameters:
        ----------
        index: str
            relative path of image
        Returns:
        -------
        image_file: str
            full path of image
        """

        index = index.replace("\\", "/")

        if not os.path.exists(index):
            image_file = os.path.join(self.prefix_path, index)
        else:
            image_file=index
        if not image_file.endswith('.jpg'):
            image_file = image_file + '.jpg'
        assert os.path.exists(image_file), 'Path does not exist: {}'.format(image_file)
        return image_file

    def load_annotations(self,annotion_type=1):
        """Load annotations

        Parameters:
        ----------
        annotion_type: int
                      0:dsadsa
                      1:dsadsa
        Returns:
        -------
        imdb: dict
            image database with annotations
        """

        assert os.path.exists(self.image_annotation_file), 'annotations not found at {}'.format(self.image_annotation_file)
        with open(self.image_annotation_file, 'r') as f:
            annotations = f.readlines()

        imdb = []
        for i in range(self.num_images):
            annotation = annotations[i].strip().split(' ')
            index = annotation[0]
            im_path = self.real_image_path(index)
            imdb_ = dict()
            imdb_['image'] = im_path

            if self.mode == 'test':
               # gt_boxes = map(float, annotation[1:])
               # boxes = np.array(bbox, dtype=np.float32).reshape(-1, 4)
               # imdb_['gt_boxes'] = boxes
                pass
            else:
                label = annotation[1]
                imdb_['label'] = int(label)
                imdb_['flipped'] = 0
                imdb_['bbox_target'] = np.zeros((4,))
                imdb_['landmark_target'] = np.zeros((10,))
                if len(annotation[2:])==4:
                    bbox_target = annotation[2:6]
                    imdb_['bbox_target'] = np.array(bbox_target).astype(float)
                if len(annotation[2:])==14:
                    bbox_target = annotation[2:6]
                    imdb_['bbox_target'] = np.array(bbox_target).astype(float)
                    landmark = annotation[6:]
                    imdb_['landmark_target'] = np.array(landmark).astype(float)
            imdb.append(imdb_)

        return imdb

    def append_flipped_images(self, imdb):
        """append flipped images to imdb

        Parameters:
        ----------
        imdb: imdb
            image database
        Returns:
        -------
        imdb: dict
            image database with flipped image annotations added
        """
        print('append flipped images to imdb', len(imdb))
        for i in range(len(imdb)):
            imdb_ = imdb[i]
            m_bbox = imdb_['bbox_target'].copy()
            m_bbox[0], m_bbox[2] = -m_bbox[2], -m_bbox[0]

            landmark_ = imdb_['landmark_target'].copy()
            landmark_ = landmark_.reshape((5, 2))
            landmark_ = np.asarray([(1 - x, y) for (x, y) in landmark_])
            landmark_[[0, 1]] = landmark_[[1, 0]]
            landmark_[[3, 4]] = landmark_[[4, 3]]

            item = {'image': imdb_['image'],
                     'label': imdb_['label'],
                     'bbox_target': m_bbox,
                     'landmark_target': landmark_.reshape((10)),
                     'flipped': 1}

            imdb.append(item)
        self.image_set_index *= 2
        print('after append flipped images to imdb', len(imdb))
        return imdb

def data_to_mindrecord_byte_image(imdb, prefix="", file_num=1, mindrecord_dir=""):
    """Create MindRecord file."""
    mindrecord_path = os.path.join(mindrecord_dir, prefix)

    writer = FileWriter(mindrecord_path, file_num)

    mtcnn_json = {
        "image": {"type": "bytes"},
        "label": {"type": "int32"},
        "bbox_target": {"type": "float32", "shape": [4]},
        "landmark_target": {"type": "float32", "shape": [10]},
    }
    writer.add_schema(mtcnn_json, "mtcnn_json")

    for i in range(len(imdb)):
        imdb_ = imdb[i]
        with open(imdb_['image'], 'rb') as f:
            image = f.read()
        label = imdb_['label']
        bbox_target = imdb_['bbox_target']
        landmark_target = imdb_['landmark_target']

        row = {"image": image,
               "label": label,
               "bbox_target": bbox_target,
               "landmark_target": landmark_target
               }
        if i % 1000 == 0:
            print("writing {} into mindrecord".format(i + 1))
        writer.write_raw_data([row])
    writer.commit()
    print("create mindrecord done")

def create_mtcnn_dataset(mindrecord_file, batch_size=128, device_num=1, rank_id=0,
                         is_training=True, num_parallel_workers=8):
    """Create MTCNN dataset with MindDataset."""
    ds = de.MindDataset(mindrecord_file,
                        columns_list=["image", "label", "bbox_target", "landmark_target"],
                        num_shards=device_num, shard_id=rank_id,
                        num_parallel_workers=4)

    train_list = [C.Decode(), C.HWC2CHW()]
    ds = ds.map(operations=train_list, input_columns=["image"])
    ds = ds.batch(batch_size, drop_remainder=True)

    return ds

def create_eval_dataset(dataset_path, target = "Ascend", device_id = 1):
    ds = de.ImageFolderDataset(dataset_path, num_parallel_workers=8, shuffle=False)
    train_list = [C.Decode(), C.HWC2CHW()]
    ds = ds.map(operations=train_list, input_columns=["image"])
    ds = ds.batch(batch_size, drop_remainder=True)

    return ds

if __name__ == '__main__':
    mindrecord_file = "/root/xidian_wks/mtcnn/data_set/img12.mindrecord"
    dataset = create_mtcnn_dataset(mindrecord_file, batch_size=128, device_num=1,
                                   rank_id=0)
    count = 0
    for item in dataset.create_dict_iterator(output_numpy=True):
        print("sample: {}".format(item))
        count += 1
    print("Got {} samples".format(count))


from django.shortcuts import render, redirect
from .models import Dish
from .forms import DishForm, DishIngredientsForm

# Create your views here.
# Zrobic inny widok na formularz skladnikowy
# widok ktory kieruje do innego widoku
# czyli nowy url

def dish_entry_view(request):
    if request.method == 'POST':
        dish_form = DishForm(request.POST)
        if dish_form.is_valid():
            print(dish_form.cleaned_data["dish_name"])
            print(dish_form.cleaned_data["dish_time"])
            a = dish_form.cleaned_data["dish_name"]

            # ingredient_form = DishIngredientsForm(initial={'hidden_field': a})
            # return render(request, 'dish-ingredients.html', {'ingredient_form': ingredient_form})
            return redirect('dish_ingredients_view', param=a)
    else:
        zmienna = DishForm()
        return render(request, 'home.html', context={"zmienna": zmienna})

# stworzyc widok skladnikow
def dish_ingredients_view(request, param):
    if request.method == 'POST':
        ingredients_form = DishIngredientsForm(request.POST)
        if ingredients_form.is_valid():
            print(param)
            print("chuj")
            return render(request, 'test.html')
    else:
        print(param)
        print("chuj")
        ingredients_form = DishIngredientsForm()
        return render(request, 'dish-ingredients.html', context={"ingredients_form": ingredients_form})

# def home(request):
#     #zmienna = 'duuuuuuuuuuuupa'
#
#     if request.method == 'POST':
#         # Jak dodalismy najpierw nazwe posilku i pore dnia
#         # czas na dodawanie skladnikow
#         dish_form = DishForm(request.POST)
#         if dish_form.is_valid():
#             print(dish_form.cleaned_data["dish_name"])
#             print(dish_form.cleaned_data["dish_time"])
#
#             if request.method == 'POST':
#                 print("wszedlem w posta")
#             else:
#                 print("nie wszedlem")
#             dish_ingredient_form = DishIngredientsForm()
#
#
#             return render(request, 'home.html', context={"dish_ingredient_form": dish_ingredient_form})
#         dish_ingredient_form = DishIngredientsForm(request.POST)
#         if dish_ingredient_form.is_valid():
#             print(dish_form.cleaned_data["dish_name"])
#             print(dish_form.cleaned_data["dish_time"])
#             print(dish_ingredient_form.cleaned_data["ingredient_name"])
#             print(dish_ingredient_form.cleaned_data["ingredient_weight"])
#             print(dish_ingredient_form.cleaned_data["ingredient_unit"])
#             zmienna = 'sadsadasdasdasdasd'
#             return render(request, 'home.html', context={"dish_ingredient_form": zmienna})
#
#
#             # Jezeli sa poprawne to tworzymy pusty formularz
#             # if request.method == 'POST':
#             #     print("jestem tutaj")
#             #     dish_ingredient_form = DishIngredientsForm(request.POST)
#             #     if dish_ingredient_form.is_valid():
#             #         print(dish_ingredient_form.cleaned_data["ingredient_name"])
#             #         print(dish_ingredient_form.cleaned_data["ingredient_weight"])
#             #         print(dish_ingredient_form.cleaned_data["ingredient_unit"])
#             #         return 'udalo sie'
#             # else:
#             #     dish_ingredient_form = DishIngredientsForm()
#             #     return render(request, 'home.html', context={"dish_ingredient_form": dish_ingredient_form})
#
#
#
#
#
#
#         # zmienna = 'formularz wyslany'
#         # return render(request, 'home.html', context={"zmienna": zmienna} )
#     else:
#         zmienna = DishForm()
#         return render(request, 'home.html', context={"zmienna": zmienna})
#     #
#     # dish = Dish(dish_id=1,
#     #             dish_name="sałatka z kurczakiem i pomarańczą",
#     #             dish_time="śniadanie",
#     #             dish_ingredient="pomarańcza",
#     #             ingredient_weight=200,
#     #             ingredient_unit='g')
#     #
#     # # all objects
#     # obj = Dish.objects.all()
#     # print(obj.__dict__)
#     #
#     # df = pd.DataFrame(obj)
#     # print(df.to_string())
#     # # type of query
#     # # print(type(obj))
#     # # # is iterable?
#     # # for obje in obj:
#     # #     #print(obje)
#     # #     print(obje.dish_ingredient)
#
#     #return render(request, 'home.html', context={"zmienna": zmienna} )


import heapq
class Solution:
    def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]:
        heap = []
        for n1 in nums1:
            for n2 in nums2:
                if len(heap) < k:
                    heapq.heappush(heap, (- n1 - n2, n1, n2))
                elif n1 + n2 <= - heap[0][0]:
                    heapq.heappushpop(heap, (- n1 - n2, n1, n2))
                else:
                    break
        return [tp[1:] for tp in heap]


# Image enhancement
# One methods === Histogram equalization
# contrast limited adaptive histogram equalization ==== CLAHE

import cv2
img=cv2.imread('60image2.png')
#Preparation for clahe
clahe=cv2.createCLAHE()
#Convert to grey scale image
gray_img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#Enhancement techniques
enhanced=clahe.apply(gray_img)
#save to file
cv2.imwrite('60im2enhanced.png',enhanced)
print("Done enhancing")


import re
import sys

# filename = "run/slurm_output.default.n32-lci.j22470107.out"
filename = "run/debug.log"
pattern = r"^(\d):.+matchtable.+insert \((\S+), (\S+), (\S+)\),.* return (\d)"
def parse_tuple(line, pattern):
    match = re.search(pattern, line)
    if match:
        key = str(match.group(1)) + "-" + str(match.group(2))
        return key, int(match.group(4)), int(match.group(5))
    return None, None, None

if __name__ == "__main__":
    if len(sys.argv) > 1:
        filename = sys.argv[1]

    print("Process file " + filename)

    with open(filename, "r") as file:
        lines = file.readlines()

    mt_map = dict()
    count = 0
    for line in lines:
        count += 1
        key, type, ret = parse_tuple(line, pattern)
        if key != None:
            if type == 1:
                if key in mt_map:
                    if (mt_map[key] < 0) != (ret == 0):
                        print("Error:", key, mt_map[key], type, ret)
                        print(count)
                        exit(1)
                    mt_map[key] += 1
                else:
                    mt_map[key] = 1
            else:
                if key in mt_map:
                    if (mt_map[key] > 0) != (ret == 0):
                        print("Error:", key, mt_map[key], type, ret)
                        print(count)
                        exit(1)
                    mt_map[key] -= 1
                else:
                    mt_map[key] = -1
            # print(key, mt_map[key], type, ret)
            if mt_map[key] == 0:
                mt_map.pop(key)

    print(count)
    print(mt_map)


from flask import Blueprint, request, make_response
from flask import jsonify
from app import db
from app.models.planet import Planet

planets_bp = Blueprint("planets", __name__, url_prefix="/planets")

@planets_bp.route("", methods=["GET", "POST"])
def handle_planets():
    if request.method == "GET":
        planets = Planet.query.all()
        planets_response = []

        for planet in planets:
            planets_response.append({
                "id": planet.id,
                "name": planet.name,
                "description": planet.description,
                "pos_from_sun": planet.pos_from_sun
            })

        return jsonify(planets_response), 200
    elif request.method == "POST":
        request_body = request.get_json()
        new_planet = Planet(name=request_body["name"], description=request_body["description"], pos_from_sun=request_body["pos_from_sun"])

        db.session.add(new_planet)
        db.session.commit()

        return jsonify({
            "success": True,
            "message": f"Planet {new_planet.name} has been created."

        }), 201

@planets_bp.route("/<planet_id>", methods=["GET", "PUT", "DELETE"])
def handle_planet(planet_id):
    planet = Planet.query.get(planet_id)

    if planet == None:
        return jsonify({
        "message": "That's not a planet!",
        "success": False,
        }), 404

    elif request.method == "GET":
        return jsonify({
            "id": planet.id,
            "name": planet.name,
            "description": planet.description,
            "pos_from_sun": planet.pos_from_sun
        }), 200

    elif request.method == "PUT":
        form_data = request.get_json()

        planet.name = form_data["name"]
        planet.description = form_data["description"]
        planet.pos_from_sun = form_data["pos_from_sun"]
        db.session.commit()

        return jsonify({"message": f"Planet with id {planet_id} has been updated",
        "success": True}), 200

    elif request.method == "DELETE":
        db.session.delete(planet)
        db.session.commit()
        return jsonify({"message": f"Planet with id {planet_id} has been deleted",
        "success": True}), 200