from bs4 import BeautifulSoup
import requests
from pymongo import MongoClient
import urllib3
urllib3.disable_warnings()
headers = {'Accept-Encoding': 'identity',
           "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
           }
db = MongoClient('167.172.165.25')
url = 'https://eda.ru/recepty/osnovnye-blyuda/russkaya-kuhnya?page=1'

form = {
    "region": "",
    "name": "",
    "photo": "",
    "type": "",
    "ingredients": {},
    "description": "",
    "_id": 0
}
# name : quantity
page_counter = 1

defined = [20, 10, 10, 10, 15, 10]
types = ["osnovnye-blyuda", "zavtraki", "zakuski", "vypechka-deserty", "salaty", "sup"]

regions = ['Русская кухня', 'Итальянская кухня', 'Японская кухня', 'Мексиканская кухня',
           'Китайская кухня', "Французская кухня", "Армянская кухня", 'Испанская кухня']

kitchen = ["russkaya-kuhnya", "italyanskaya-kuhnya", "yaponskaya-kuhnya", "meksikanskaya-kuhnya",
           "kitayskaya-kuhnya","francuzskaya-kuhnya", "armyanskaya-kuhnya", "ispanskaya-kuhnya"]

for ss, g in enumerate(regions):
    for sss, sl in enumerate(defined):
        f = 0
        for k in range((sl // 14) + 2):
            print(k)
            url = 'https://eda.ru/recepty/{0}/{1}?page={2}'.format(kitchen[ss], types[sss], str(k))
            r = requests.get(url, headers=headers, verify=False)
            soup = BeautifulSoup(r.content, 'lxml')
            for i in soup.findAll("h3", class_="horizontal-tile__item-title item-title"):
                url = "https://eda.ru/" + i.find('a')['href']
                form['name'] = " ".join(i.find("span").text.split()).replace('.', ' ')
                form['region'] = g
                form['type'] = types[sss]
                if f == sl:
                    break
                r = requests.get(url, headers=headers, verify=False)
                soup2 = BeautifulSoup(r.content, 'lxml')

                form['photo'] = soup2.find('div', class_='recipe__print-cover').find('img')['src']

                for m in soup2.find('div', class_='ingredients-list').findAll('p', class_="content-item"):
                    temp = " ".join(m.text.split()).replace('.', ' ').split()
                    if 'по' in temp and 'вкусу' in temp:
                        form["ingredients"][' '.join(temp[:-2])] = ' '.join(temp[-2:])
                    else:
                        for z in range(len(temp)):
                            try:
                                test = int(temp[z])
                                form["ingredients"][' '.join(temp[:-(len(temp)-z)])] = ' '.join(temp[-(len(temp)-z):])
                            except ValueError:
                                pass

                temp2 = []
                for pm in soup2.find_all('div', class_='instruction__wrap'):
                    temp2.append(' '.join(pm.find('span').text.split()))
                form["description"] = '\n'.join(temp2)
                form['_id'] += 1
                f += 1
                post_id = db.data.recipes.insert_one(form)
                print(post_id.inserted_id)
                form["ingredients"] = dict()
    print("cycle done")


import random
import time

start = time.time()

lst = []
for x in range(10000):
	lst.append(random.randint(1, 100))

def mergesort(array):
	if len(array) > 1:
		mid = len(array) // 2
		lefthalf = array[:mid]
		righthalf = array[mid:]

		mergesort(lefthalf)
		mergesort(righthalf)

		i = 0
		j = 0
		k = 0

		while i < len(lefthalf) and j < len(righthalf):
			if lefthalf[i] < righthalf[j]:
				array[k] = lefthalf[i]
				i += 1
			else:
				array[k] = righthalf[j]
				j += 1
			k += 1

		while i < len(lefthalf):
			array[k] = lefthalf[i]
			i += 1
			k += 1

		while j < len(righthalf):
			array[k] = righthalf[j]
			j += 1
			k += 1

	return array

lst = mergesort(lst)
print(lst)
end = time.time()
print(end-start)


"""
Get storm based warnings by lat lon point, optionally a time
"""
import sys
import json
import datetime

from paste.request import parse_formvars
from pyiem.util import get_dbconn, utc

ISO = "%Y-%m-%dT%H:%M:%SZ"

def get_events(ctx):
    """ Get Events """
    data = {"sbws": [], "lon": ctx["lon"], "lat": ctx["lat"], "valid": None}
    data["generation_time"] = utc().strftime(ISO)
    valid_limiter = ""
    if "valid" in ctx:
        valid_limiter = " and issue <= '%s+00' and expire > '%s' " % (
            ctx["valid"].strftime("%Y-%m-%d %H:%M"),
            ctx["valid"].strftime("%Y-%m-%d %H:%M"),
        )
        data["valid"] = ctx["valid"].strftime(ISO)

    pgconn = get_dbconn("postgis")
    cursor = pgconn.cursor()
    cursor.execute("""SET TIME ZONE 'UTC'""")
    cursor.execute(
        """
  select wfo, significance, phenomena, issue, expire, eventid,
  tml_direction, tml_sknt from sbw
  where status = 'NEW' and
  ST_Contains(geom, ST_SetSRID(ST_GeomFromEWKT('POINT(%s %s)'),4326)) and
  issue > '2005-10-01' """
        + valid_limiter
        + """
  ORDER by issue ASC
    """,
        (ctx["lon"], ctx["lat"]),
    )
    for row in cursor:
        data["sbws"].append(
            {
                "phenomena": row[2],
                "eventid": row[5],
                "significance": row[1],
                "wfo": row[0],
                "issue": row[3].strftime("%Y-%m-%dT%H:%MZ"),
                "expire": row[4].strftime("%Y-%m-%dT%H:%MZ"),
                "tml_direction": row[6],
                "tml_sknt": row[7],
            }
        )
    return data

def try_valid(ctx, fields):
    """See if a valid stamp is provided or not."""
    if fields.get("valid") is None:
        return
    # parse at least the YYYY-mm-ddTHH:MM
    ts = datetime.datetime.strptime(fields["valid"][:16], "%Y-%m-%dT%H:%M")
    ctx["valid"] = utc(ts.year, ts.month, ts.day, ts.hour, ts.minute)

def application(environ, start_response):
    """Answer request."""
    fields = parse_formvars(environ)
    ctx = {}
    ctx["lat"] = float(fields.get("lat", 41.99))
    ctx["lon"] = float(fields.get("lon", -92.0))
    try:
        try_valid(ctx, fields)
    except Exception as exp:
        sys.stderr.write(str(exp))
        headers = [("Content-type", "text/plain")]
        start_response("500 Internal Server Error", headers)
        return [b"Failed to parse valid, ensure YYYY-mm-ddTHH:MM:SSZ"]

    data = get_events(ctx)

    headers = [("Content-type", "application/json")]
    start_response("200 OK", headers)
    return [json.dumps(data).encode("ascii")]


#!/usr/bin/env python

import sys
import os
import json

sys.path.insert(0, 'src')

def main(targets):
    '''
    Runs the main project pipeline logic, given the targets.
    targets must contain: 'data', 'analysis', 'model'.

    `main` runs the targets in order of data=>analysis=>model.
    '''

    if 'model' in targets:
        with open('gpt2/webnlg_models/webnlgprefixtune_y_5_act_cat_b=5-e=5_d=0.0_u=no_lr=5e-05_w=0.0_s=10_r=n_m=512_o=1_o=1/config.json') as fh:
            model_cfg = json.load(fh)
            print(model_cfg)
    if 'test' in targets:
        os.chdir("gpt2/webnlg_models/webnlgprefixtune_y_5_act_cat_b=5-e=5_d=0.0_u=no_lr=5e-05_w=0.0_s=10_r=n_m=512_o=1_o=1")
        files = [f for f in os.listdir('.') if os.path.isfile(f)]
        if 'pytorch_model.bin'  in files:
            print('Since the model parameters have large size (>100 Mb), it could not be uploaded to github. We are figuring out a way to share the model with you. Some sample outputs generated from the model are in path `PrefixTuning/gpt2/res/webNLG_results2.`')
        else:
            COMMANDLINE = "python gen.py webnlg yes test webnlg_models/webnlgprefixtune_y_5_act_cat_b=5-e=5_d=0.0_u=no_lr=5e-05_w=0.0_s=10_r=n_m=512_o=1_o=1 no"
            os.chdir("../../../gpt2/")
            os.system(COMMANDLINE)

if __name__ == '__main__':
    # run via:
    # python main.py data features model
    targets = sys.argv[1:]
    main(targets)


import sys, time
from PyQt5.QtWidgets import (
    QLabel, QLineEdit, QLCDNumber, QPushButton, QTabWidget, QApplication,
    QVBoxLayout, QHBoxLayout, QWidget, QGroupBox, QSystemTrayIcon, QMenu,
    QSplashScreen
)
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtCore import QTimer, Qt

import resource
from style_sheet import style_sheet

# Global variable for each timer
POMODORO_TIME = 1800000  # 25 MINS IN MILLISECONDS
SHORT_BREAK_TIME = 300000  # 5 MINS IN MILLISECONDS
LONG_BREAK_TIME = 1200000  # 15 MINS IN MILLISECONDS

class PomodoroTimer(QWidget):
    def __init__(self):
        super().__init__()
        self.initializeUI()
        self.setupSystemTrayIcon()

    def initializeUI(self):
        """Initialize the window and display its contents to the screen."""
        self.setFixedSize(500, 400)
        self.setWindowTitle("Pomodoro Timer")
        self.setWindowIcon(QIcon(":/wall clock"))

        self.pomodoro_limit = POMODORO_TIME
        self.short_break_limit = SHORT_BREAK_TIME
        self.long_break_limit = LONG_BREAK_TIME

        self.setupTabsAndWidgets()

        # Variables related to the current tabs and widgets displayed
        # in the GUI's window
        self.current_tab_selected = 0
        self.current_start_button = self.pomodoro_start_button
        self.current_stop_button = self.pomodoro_stop_button
        self.current_reset_button = self.pomodoro_reset_button
        self.current_time_limit = self.pomodoro_limit
        self.current_lcd = self.pomodoro_lcd

        # variables related to user's current task
        self.task_is_set = False
        self.number_of_tasks = 0
        self.task_complete_counter = 0

        # create timer object
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.updateTimer)

        self.show()

    def setupTabsAndWidgets(self):
        """Set up the tab bar for the different pomodoro stages:
        pomodoro, short break, long break."""
        # create the tab bar, and the QWidgets (containers) for each tab
        self.tab_bar = QTabWidget(self)

        self.pomodoro_tab = QWidget()
        self.pomodoro_tab.setObjectName("Pomodoro")

        self.short_break_tab = QWidget()
        self.short_break_tab.setObjectName("ShortBreak")

        self.long_break_tab = QWidget()
        self.long_break_tab.setObjectName("LongBreak")

        self.tab_bar.addTab(self.pomodoro_tab, "POMODORO")
        self.tab_bar.addTab(self.short_break_tab, "SHORT BREAK")
        self.tab_bar.addTab(self.long_break_tab, "LONG BREAK")

        self.tab_bar.currentChanged.connect(self.tabsSwitched)

        # Call the functions that contain the widgets for each tab
        self.pomodoroTab()
        self.shortBreakTab()
        self.longBreakTab()

        # create the line edit and button widgets and layout for
        # Pomodoro Taskbar
        self.enter_task_lineedit = QLineEdit()
        self.enter_task_lineedit.setClearButtonEnabled(True)
        self.enter_task_lineedit.setPlaceholderText("Enter Your Current Task")

        confirm_task_button = QPushButton(QIcon(":/plus"), None)
        confirm_task_button.setObjectName("ConfirmButton")
        confirm_task_button.clicked.connect(self.addTaskToTaskbar)

        task_entry_h_box = QHBoxLayout()
        task_entry_h_box.addWidget(self.enter_task_lineedit)
        task_entry_h_box.addWidget(confirm_task_button)

        self.tasks_v_box = QVBoxLayout()

        task_v_box = QVBoxLayout()
        task_v_box.addLayout(task_entry_h_box)
        task_v_box.addLayout(self.tasks_v_box)

        # container for taskbar
        task_bar_gb = QGroupBox("Tasks")
        task_bar_gb.setLayout(task_v_box)

        # create and set layout f
        # or the main window
        main_v_box = QVBoxLayout()
        main_v_box.addWidget(self.tab_bar)
        main_v_box.addWidget(task_bar_gb)
        self.setLayout(main_v_box)

    def pomodoroTab(self):
        """Set up the Pomodoro tab, widgets and layout"""
        # convert starting time to display on timer
        start_time = self.calculateDisplayTime(self.pomodoro_limit)

        self.pomodoro_lcd = QLCDNumber()
        self.pomodoro_lcd.setObjectName("PomodoroLCD")
        self.pomodoro_lcd.setSegmentStyle(QLCDNumber.Filled)
        self.pomodoro_lcd.display(start_time)

        self.pomodoro_start_button = QPushButton("Start")
        self.pomodoro_start_button.clicked.connect(self.startCountDown)

        self.pomodoro_stop_button = QPushButton("Stop")
        self.pomodoro_stop_button.clicked.connect(self.stopCountDown)

        self.pomodoro_reset_button = QPushButton("Reset")
        self.pomodoro_reset_button.clicked.connect(self.resetCountDown)

        button_h_box = QHBoxLayout()  # A horizontal layout for buttons
        button_h_box.addWidget(self.pomodoro_start_button)
        button_h_box.addWidget(self.pomodoro_stop_button)
        button_h_box.addWidget(self.pomodoro_reset_button)

        # create and set layout for the pomodoro tab
        v_box = QVBoxLayout()
        v_box.addWidget(self.pomodoro_lcd)
        v_box.addLayout(button_h_box)
        self.pomodoro_tab.setLayout(v_box)

    def shortBreakTab(self):
        """Set up the short break tab, widgets  and layout"""
        # convert starting time to display on timer
        start_time = self.calculateDisplayTime(self.short_break_limit)

        self.short_break_lcd = QLCDNumber()
        self.short_break_lcd.setObjectName("ShortLCD")
        self.short_break_lcd.setSegmentStyle(QLCDNumber.Flat)
        self.short_break_lcd.display(start_time)

        self.short_start_button = QPushButton("Start")
        self.short_start_button.clicked.connect(self.startCountDown)

        self.short_stop_button = QPushButton("Stop")
        self.short_stop_button.clicked.connect(self.stopCountDown)

        self.short_reset_button = QPushButton("Reset")
        self.short_reset_button.clicked.connect(self.resetCountDown)

        button_h_box = QHBoxLayout()
        button_h_box.addWidget(self.short_start_button)
        button_h_box.addWidget(self.short_stop_button)
        button_h_box.addWidget(self.short_reset_button)

        # create and set layout for the short break tab
        v_box = QVBoxLayout()
        v_box.addWidget(self.short_break_lcd)
        v_box.addLayout(button_h_box)
        self.short_break_tab.setLayout(v_box)

    def longBreakTab(self):
        """Set up the long break tab, widget and layout."""
        # convert starting time to display on timer
        start_time = self.calculateDisplayTime(self.long_break_limit)

        self.long_break_lcd = QLCDNumber()
        self.long_break_lcd.setObjectName("LongLCD")
        self.long_break_lcd.setSegmentStyle(QLCDNumber.Filled)
        self.long_break_lcd.display(start_time)

        self.long_start_button = QPushButton("Start")
        self.long_start_button.clicked.connect(self.startCountDown)

        self.long_stop_button = QPushButton("Stop")
        self.long_stop_button.clicked.connect(self.stopCountDown)

        self.long_reset_button = QPushButton("Reset")
        self.long_reset_button.clicked.connect(self.resetCountDown)

        button_h_box = QHBoxLayout()
        button_h_box.addWidget(self.long_start_button)
        button_h_box.addWidget(self.long_stop_button)
        button_h_box.addWidget(self.long_reset_button)

        # create and set layout for the long break tab
        v_box = QVBoxLayout()
        v_box.addWidget(self.long_break_lcd)
        v_box.addLayout(button_h_box)
        self.long_break_tab.setLayout(v_box)

    def startCountDown(self):
        """Starts the timer. If the current tab's time is 00:00,
        reset the time if user pushes the start button."""
        self.current_start_button.setEnabled(False)

        # used to reset counter_label if user has already
        # completed four pomodoro cycles
        if self.task_is_set and self.task_complete_counter == 0:
            self.counter_label.setText(f"{self.task_complete_counter}/4")
        remaining_time = self.calculateDisplayTime(self.current_time_limit)
        if remaining_time == "00:00":
            self.resetCountDown()
            self.timer.start(1000)
        else:
            self.timer.start(1000)

    def stopCountDown(self):
        """If the timer is already running, then stop the timer."""
        if self.timer.isActive():
            self.timer.stop()
            self.current_start_button.setEnabled(True)

    def resetCountDown(self):
        """Resets the time for the current tab when the reset
        button is selected."""
        self.stopCountDown()  # stop countdown if timer is running

        # reset time for currently selected tab
        if self.current_tab_selected == 0:  # Pomodoro tab
            self.pomodoro_limit = POMODORO_TIME
            self.current_time_limit = self.pomodoro_limit
        elif self.current_tab_selected == 1:  # Short break tab
            self.short_break_limit = SHORT_BREAK_TIME
            self.current_time_limit = self.short_break_limit
        elif self.current_tab_selected == 2:  # long break tab
            self.long_break_limit = LONG_BREAK_TIME
            self.current_time_limit = self.long_break_limit
        reset_time = self.calculateDisplayTime(self.current_time_limit)
        self.current_lcd.display(reset_time)

    def updateTimer(self):
        """Updates the timer and the current QLCDNumber widget.
        Also, updates the task counter if a task is set."""
        remaining_time = self.calculateDisplayTime(self.current_time_limit)

        if remaining_time == "00:00":
            self.stopCountDown()
            self.current_lcd.display(remaining_time)
            if self.current_tab_selected == 0 and self.task_is_set:
                self.task_complete_counter += 1
                if self.task_complete_counter == 4:
                    self.counter_label.setText(f"Time for a long break. {self.task_complete_counter}/4")
                    self.tray_icon.showMessage("Time UP!", "Take a long break, You earned it!\n"
                                                           "Keep up the good work!")
                    self.task_complete_counter = 0
                    self.tab_bar.setCurrentIndex(2)
                    time.sleep(3)
                    self.startCountDown()
                elif self.task_complete_counter < 4:
                    # switch the display to the short break timer and start
                    # the countdown
                    if self.tab_bar.currentIndex() == 0:
                        self.tab_bar.setCurrentIndex(1)
                    time.sleep(3)
                    self.startCountDown()
                    self.counter_label.setText(f"{self.task_complete_counter}/4")
            elif self.current_tab_selected == 1 and self.task_is_set:
                if self.task_complete_counter < 4:
                    self.tab_bar.setCurrentIndex(0)
                    time.sleep(3)
                    self.startCountDown()
            elif self.current_tab_selected == 2 and self.task_is_set:
                if self.task_complete_counter < 4:
                    self.tab_bar.setCurrentIndex(0)
                    self.tray_icon.showMessage("Cycle complete!", "Set new task or continue"
                                                                  "present tasks.")
        else:
            # update the current timer by decreasing the current
            # running time by one second
            self.current_time_limit -= 1000
            self.current_lcd.display(remaining_time)

    def tabsSwitched(self, index):
        """Depending upon which tab the user is currently looking at, the
        information for that tab needs to be updated. This function updates
        the different variables that keep track of the timer, buttons,
        lcds and other widgets, and update them accordingly."""
        self.current_tab_selected = index
        self.stopCountDown()

        # reset variables, time and widgets depending upon
        # which tab is the current_tab_selected
        if self.current_tab_selected == 0:  # Pomodoro tab
            self.current_start_button = self.pomodoro_start_button
            self.current_stop_button = self.pomodoro_stop_button
            self.current_reset_button = self.pomodoro_reset_button
            self.pomodoro_limit = POMODORO_TIME
            self.current_time_limit = self.pomodoro_limit

            self.current_lcd = self.pomodoro_lcd
        elif self.current_tab_selected == 1:  # short break tab
            self.current_start_button = self.short_start_button
            self.current_stop_button = self.short_stop_button
            self.current_reset_button = self.short_reset_button
            self.short_break_limit = SHORT_BREAK_TIME
            self.current_time_limit = self.short_break_limit

            self.current_lcd = self.short_break_lcd
        elif self.current_tab_selected == 2:  # long break tab
            self.current_start_button = self.long_start_button
            self.current_stop_button = self.long_stop_button
            self.current_reset_button = self.long_reset_button
            self.long_break_limit = LONG_BREAK_TIME
            self.current_time_limit = self.long_break_limit

            self.current_lcd = self.long_break_lcd
        reset_time  = self.calculateDisplayTime(self.current_time_limit)
        self.current_lcd.display(reset_time)

    def addTaskToTaskbar(self):
        """When the user clicks the plus button, the widgets for the new taks
        will be added to the task bar. Only one task is allowed to be entered
        at a time"""
        text = self.enter_task_lineedit.text().strip()
        self.enter_task_lineedit.clear()

        # change number_of_task if you want to add more tasks to the task bar
        if text != "" and self.number_of_tasks != 1:
            self.enter_task_lineedit.setReadOnly(True)
            self.task_is_set = True
            self.new_task = QLabel(text)

            self.counter_label = QLabel(f"{self.task_complete_counter}/4")
            self.counter_label.setAlignment(Qt.AlignRight)

            self.cancel_task_button = QPushButton(QIcon("images/minus.png"), None)
            self.cancel_task_button.setMaximumWidth(24)
            self.cancel_task_button.clicked.connect(self.clearCurrentTask)

            self.new_task_h_box = QHBoxLayout()
            self.new_task_h_box.addWidget(self.new_task)
            self.new_task_h_box.addWidget(self.counter_label)
            self.new_task_h_box.addWidget(self.cancel_task_button)

            self.tasks_v_box.addLayout(self.new_task_h_box)
            self.number_of_tasks += 1

    def clearCurrentTask(self):
        """Delete the current task, and reset variables and widgets
        related to the tasks."""
        # remove items from parent widget by setting the argument
        # value in setParent() to None

        self.new_task.setParent(None)
        self.counter_label.setParent(None)
        self.cancel_task_button.setParent(None)

        self.number_of_tasks -= 1
        self.task_is_set = False
        self.task_complete_counter = 0

        self.enter_task_lineedit.setReadOnly(False)

    def convertTotalTime(self, time_in_milli) -> tuple:
        """Convert time to milliseconds."""
        minutes = (time_in_milli / (1000 * 60)) % 60
        seconds = (time_in_milli / 1000) % 60
        return int(minutes), int(seconds)

    def calculateDisplayTime(self, time) -> str:
        """Calculate the time that should be display in the QLCDNumber widget"""
        minutes, seconds = self.convertTotalTime(time)
        amount_of_time = "{:02d}:{:02d}".format(minutes, seconds)
        return amount_of_time

    def setupSystemTrayIcon(self):
        """set up system tray icon and context menu. User can re-open the window
        if it was closed or quit the application using the tray menu."""

        self.tray_icon = QSystemTrayIcon(QIcon(":/wall clock"))

        # create the actions' context menu for the tray icon
        tray_menu = QMenu()

        open_action = tray_menu.addAction("Open")
        open_action.triggered.connect(self.show)
        tray_menu.addSeparator()

        quit_act = tray_menu.addAction("Quit")
        quit_act.triggered.connect(QApplication.quit)

        self.tray_icon.setContextMenu(tray_menu)
        self.tray_icon.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setStyleSheet(style_sheet)
    pixmap = QPixmap(":/splash image")
    splash = QSplashScreen(pixmap)
    splash.show()
    time.sleep(5)
    window = PomodoroTimer()
    splash.finish(window)
    sys.exit(app.exec_())


# uncompyle6 version 3.7.4
# Python bytecode 3.7 (3394)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: /Users/dauptain/GITLAB/opentea/src/opentea/cli.py
# Compiled at: 2019-07-18 11:38:44
# Size of source mod 2**32: 598 bytes
"""
cli.py

Command line interface for tools in pyavbp
"""
import click
from opentea.noob.check_schema import nob_check_schema, read_serialized_data

@click.command()
@click.argument('schema_f')
def test_schema(schema_f):
    """Test a yaml file for schema."""
    schema = read_serialized_data(schema_f)
    nob_check_schema(schema)
    print('** Congratulations! **')
    print(schema_f + ' SCHEMA structure is valid\nfor opentea requirements.')


from django.urls import path

from .views import (
    ItemDetailView,
    checkout,
    HomeView,
    add_to_cart,
    remove_from_cart

)
from django.conf.urls.static import static,settings
from django.contrib.staticfiles.urls import static
from . import views
app_name='OnlineShop'
urlpatterns = [
    path('',HomeView.as_view(),name='item-list'),
    path('checkout/',checkout,name='checkout'),
    path('products/<slug>/',ItemDetailView.as_view(),name='products'),
    path('add-to-cart/<slug>/',add_to_cart ,name='add-to-cart'),
    path('remove-from-cart/<slug>/',remove_from_cart ,name='remove-from-cart')

]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)


x1 = input("请输入第一个元素：")
x2 = input("请输入第二个元素: ")
x3 = input("请输入第三个元素: ")
x4 = input("请输入第四个元素: ")
x5 = input("请输入第五个元素: ")

xArr =[x1,x2,x3,x4,x5]

print ("结果为： ",xArr)
temp=0

for x in range(len(xArr)):
    for y in range(x+1,len(xArr)):
        if xArr[x]>xArr[y]:
           temp=xArr[x]
           xArr[x] = xArr[y]
           xArr[y]=temp

#输出排序结果

print ("按升序排序结果为： ",xArr)


#!/usr/bin/env python
import json
import pygal

def load_stats(filename):

    try:
        with open(filename, 'r') as f:
            data = json.load(f)
            f.close
            return data
    except IOError as e:
        print('Unable to open file')

def main():

    stats_file = 'stats.json'
    stats = load_stats(stats_file)

    prev_time = int(stats[1]['sysUptime'])
    prev_InOctets = int(stats[1]['ifInOctets_fa4'])
    prev_OutOctets = int(stats[1]['ifOutOctets_fa4'])
    prev_InUcastPkts = int(stats[1]['ifInUcastPkts_fa4'])
    prev_OutUcastPkts = int(stats[1]['ifOutUcastPkts_fa4'])

    time_list = []
    InOctets_list = []
    OutOctets_list = []
    InUcastPkts_list = []
    OutUcastPkts_list = []

    for entry in stats[2:-1]:
        time = int(entry['sysUptime']) - prev_time
        InOctets = int(entry['ifInOctets_fa4']) - prev_InOctets
        OutOctets = int(entry['ifOutOctets_fa4']) - prev_OutOctets
        InUcastPkts = int(entry['ifInUcastPkts_fa4']) - prev_InUcastPkts
        OutUcastPkts = int(entry['ifOutUcastPkts_fa4']) - prev_OutUcastPkts

        time_list.append(time)
        InOctets_list.append(InOctets)
        OutOctets_list.append(OutOctets)
        InUcastPkts_list.append(InUcastPkts)
        OutUcastPkts_list.append(OutUcastPkts)

        prev_time = int(entry['sysUptime'])
        prev_InOctets = int(entry['ifInOctets_fa4'])
        prev_OutOctets = int(entry['ifOutOctets_fa4'])
        prev_InUcastPkts = int(entry['ifInUcastPkts_fa4'])
        prev_OutUcastPkts = int(entry['ifOutUcastPkts_fa4'])

        OctetChart = pygal.Line()
        OctetChart.title = 'Input/Output Bytes'
        OctetChart.x_labels = time_list
        OctetChart.add('InBytes', InOctets_list)
        OctetChart.add('OutBytes', OutOctets_list)
        OctetChart.render_to_file('bytes.svg')

        UcastPktChart = pygal.Line()
        UcastPktChart.title = 'Input/Output Packets'
        UcastPktChart.x_labels = time_list
        UcastPktChart.add('InPackets', InUcastPkts_list)
        UcastPktChart.add('OutPackets', OutUcastPkts_list)
        UcastPktChart.render_to_file('packets.svg')

if __name__ == "__main__":
    main()


#functions
#callable classes
#inheriting Thread class-Java

import threading;
import time;

def my_thread(a):
    print("child id:",threading.currentThread().name);
    for i in range(5):
        print("In my_thread function:",i);
        time.sleep(1);

class MyThread():
    def __call__(self,a,b):
        print("call received:",a,b);
        for i in range(5):
            print("tO thread fun:",i);
            time.sleep(1);

#print("main started");
#print("main id:",threading.currentThread().name);
my_thread=MyThread();
th1=threading.Thread(target=my_thread,args=(10,20),name="CHILDTHREAD");
th1.start();
for i in range(5):
    print("tO main fun:",i);
    time.sleep(1);


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import mybase
import re

url = 'http://www.ebd.frank.eyebuy.direct/eyeglasses'
html = mybase.get_url_content(url)
ulInfo = re.compile('
.*?
(.*?)

', re.S)
strlist = re.findall(ulInfo, html)

# liInfo = re.compile('', re.S)
# liIn = re.findall(liInfo, strlist)
print(strlist[0])

# write_info('ebd.txt', strlist)


## Imports

import numpy as np
from pyquaternion import Quaternion
from handybeam.misc import copy_docstring
from handybeam.samplers.abstract_sampler import AbstractSampler
import handybeam.visualise

## Global variables

tau = 2*np.pi

## Class

class HexagonalSampler(AbstractSampler):

    '''This is the hexagonal sampling grid class. It takes the specification for a
    hexagonal sampling array and then samples the acoustic field at these points.

    '''

    def __init__(self,
                parent = None,
                normal_vector = np.array((0,0,1)),
                parallel_vector = np.array((0,1,0)),
                origin = np.array((0,0,200e-3)),
                grid_spacing_per_wavelength = 0.2,
                grid_spacing_per_m = None,
                grid_extent_around_origin = 0.2,
                local_work_size = (1,1,1)):

        '''This method intialises an instance of the HexagonalSampler class.

        Parameters
        ----------

        parent : handybeam.world.World
                This is an instance of the handybeam world class.
        normal_vector : numpy array
                This is the vector normal to the desired sampling grid.
        parallel_vector : numpy array
                This is a vector parallel to the desired sampling grid.
        origin : numpy array
                This is a vector specifying the origin of the sampling grid.
        grid_spacing_per_wavelength : float
                This specifies the grid spacing as a fraction of the medium wavelength.
        grid_spacing_per_m : float
                This specifies the grid spacing in meters.
        grid_extent_around_origin : float
                This specifies the distance between the origin of the sampling grid and the edge.
        local_work_size : tuple
                This sets the local work size for the GPU, not recommended to change unless the user
                has experience with OpenCL and pyopencl.
        '''

        super(HexagonalSampler,self).__init__()

        self.parent = parent
        self.normal_vector = normal_vector
        self.parallel_vector = parallel_vector
        self.vector_2 = None
        self.origin = origin
        self.local_work_size = local_work_size

        if grid_spacing_per_m is None:

            self.delta = grid_spacing_per_wavelength * self.parent.medium_wavelength
            self.side_length = np.int(np.ceil((2*grid_extent_around_origin)/self.delta))

        else:

            self.delta = grid_spacing_per_m
            self.side_length = np.int(np.ceil((2*grid_extent_around_origin)/ self.delta))

        self.grid_length = np.int32(2*self.side_length - 1)

        self.x_lim = None
        self.y_lim = None
        self.vx1 = None
        self.vy1 = None
        self.vz1 = None
        self.x0 = None
        self.y0 = None
        self.z0 = None
        self.vx2 = None
        self.vy2 = None
        self.vz2 = None

        self.pressure_field = np.zeros((self.grid_length,self.grid_length,1),dtype = np.complex)
        self.coordinates = np.zeros((self.grid_length,self.grid_length,3),dtype = np.float32)

        self.generate_propagation_parameters()
        self.find_hex_grid_area()

    def find_hex_grid_area(self):

        '''This method finds the area of the requested sampling grid.

        '''

        points_in_hex = np.sum([6*k for k in range(1,self.side_length)]) + 1
        points_in_par = np.power(self.grid_length,2)

        area_of_par = np.power((self.grid_length - 1) * self.delta,2) * np.sqrt(3)/2
        self.area = (points_in_hex/points_in_par) * area_of_par

    def generate_propagation_parameters(self):

        '''This method finds the parameters to pass to the opencl kernel to correctly
        define the sampling grid.

        '''

        # Find the unit normal vector to the plane.

        unit_normal = (1/np.linalg.norm(self.normal_vector))*self.normal_vector

        # Define the quaternion required to rotate the parallel vector by an angle of pi/2.

        quaternion = Quaternion(axis=[unit_normal[0], unit_normal[1],unit_normal[2]], angle = tau/6)

        # Rotate the parallel vector to get a new vector which is perpendicular to the normal and the parallel vector.

        self.vector_2 = quaternion.rotate(self.parallel_vector)

        # Find the norm of these vectors.

        parallel_vector_norm = np.linalg.norm(self.parallel_vector)
        vector_2_norm = np.linalg.norm(self.vector_2)

        # Find the unit vectors.

        unit_parallel_vector = (1/parallel_vector_norm)*self.parallel_vector

        unit_vector_2 = (1/vector_2_norm)*self.vector_2

        # Compute the angle between these vectors to check that is equal to pi/2.

        angle_1 = int(np.round(np.rad2deg(np.arccos(np.dot(unit_parallel_vector,unit_vector_2))),1))

        # Compute the angle between each of these vectors and the normal to ensure that they parameterise the plane perpendicular to the nromal.

        angle_2 = int(np.round(np.rad2deg(np.arccos( np.dot(self.parallel_vector,self.normal_vector) / (parallel_vector_norm*vector_2_norm) )),1))
        angle_3 = int(np.round(np.rad2deg(np.arccos( np.dot(self.vector_2,self.normal_vector) / (parallel_vector_norm*vector_2_norm) )),1))

        # Throw error if they are not.

        if np.bitwise_or(angle_1 != 60,  np.bitwise_or(int(angle_2) != 90, int(angle_3) != 90)):

            raise Exception('Could not parameterise the requested sampling grid correctly!')

        self.vx1 = np.float32(self.parallel_vector[0])
        self.vy1 = np.float32(self.parallel_vector[1])
        self.vz1 = np.float32(self.parallel_vector[2])
        self.x0 = np.float32(self.origin[0])
        self.y0 = np.float32(self.origin[1])
        self.z0 = np.float32(self.origin[2])
        self.vx2 = np.float32(np.round(self.vector_2[0],4))
        self.vy2 = np.float32(np.round(self.vector_2[1],4))
        self.vz2 = np.float32(np.round(self.vector_2[2],4))

    def propagate(self,print_performance_feedback = False):

        '''This method calls the hex_propagator to propagate the acoustic field to
        the desired sampling points.

        Parameters
        ----------

        print_performance_feedback : boolean
                Boolean value determining whether or not to output the GPU performance.

        '''

        kernel_output = self.parent.propagator.hex_propagator(
                                                            self.parent.tx_array,
                                                            self.side_length,
                                                            self.delta,
                                                            self.x0,self.y0,self.z0,
                                                            self.vx1,self.vy1,self.vz1,
                                                            self.vx2,self.vy2,self.vz2,
                                                            local_work_size = self.local_work_size,
                                                            print_performance_feedback = print_performance_feedback
                                                        )

        self.pressure_field= kernel_output[:,:,3] + np.complex(0,1) * kernel_output[:,:,4]
        self.coordinates = kernel_output[:,:,0:4]


from influxdb_client.client.write_api import SYNCHRONOUS
import influxdb_client

from datetime import datetime, timedelta
import serial.tools.list_ports
import serial, time
import binascii
import re

bucket = "Pluvio L 400"
org = "WG"
token = "mytoken"
# Store the URL of your InfluxDB instance
url="http://localhost:8086"

client = influxdb_client.InfluxDBClient(url=url, token=token, org=org)

a = serial.tools.list_ports.comports()
i = 0
for w in a:
    vidn = w.vid if (type(w.vid) is int) else 0
    print('%d)\t%s\t(USB VID=%04X)' % (i, w.device, vidn))
    i = i+1

ser = serial.Serial()
ser.port = "COM4"

#9600,N,8,1
ser.baudrate = 9600
ser.bytesize = serial.EIGHTBITS #number of bits per bytes
ser.parity = serial.PARITY_NONE #set parity check
ser.stopbits = serial.STOPBITS_ONE #number of stop bits

ser.timeout = 0.5          #non-block read 0.5s
ser.writeTimeout = 0.5     #timeout for write 0.5s
ser.xonxoff = False    #disable software flow control
ser.rtscts = False     #disable hardware (RTS/CTS) flow control
ser.dsrdtr = False     #disable hardware (DSR/DTR) flow control

try:
    ser.open()
except Exception as ex:
    print ("open serial port error " + str(ex))
    exit()

if ser.isOpen():
    try:
        ser.flushInput() #flush input buffer
        ser.flushOutput() #flush output buffer

        print('Open: ' + ser.portstr)

        time_now = datetime.utcnow()  # Or .now() for local time
        time_rounded = time_now.replace(second=0, microsecond=0)

        while True:
            # Wait until next 5 minute time
            time_rounded += timedelta(minutes=1)
            time_to_wait = (time_rounded - datetime.utcnow()).total_seconds()
            time.sleep(time_to_wait)

            send_data = 'M ' + '\r'
            ser.write(send_data.encode('ascii'))

            response = [float(i) for i in ser.readline().decode('ascii').split()]
            print('RT Intensity:', response[0])

            write_api = client.write_api(write_options = SYNCHRONOUS)
            p = [influxdb_client.Point("RT Intensity").field("Pluvio L", response[0]),
                 influxdb_client.Point("Accu RT-NRT").field("Pluvio L", response[1]),
                 influxdb_client.Point("Accu NRT").field("Pluvio L", response[2]),
                 influxdb_client.Point("Accu total NRT").field("Pluvio L", response[3]),
                 influxdb_client.Point("Accu Bucket RT").field("Pluvio L", response[4]),
                 influxdb_client.Point("Accu Bucket NRT").field("Pluvio L", response[5]),]

            write_api.write(bucket=bucket, org=org, record=p)

        ser.close()
    except Exception as e1:
        print ("communicating error " + str(e1))

else:
    print ("open serial port error")


# -*- coding: utf-8 -*-
"""ThreatConnect TI Email """
from tcex.tcex_ti.mappings.group.tcex_ti_group import Group

class Email(Group):
    """Unique API calls for Email API Endpoints"""

    def __init__(self, tcex, name, to, from_addr, subject, body, header, owner=None, **kwargs):
        """Initialize Class Properties.

        Args:
            name (str): The name for this Group.
            subject (str): The subject for this Email.
            header (str): The header for this Email.
            body (str): The body for this Email.
            date_added (str, kwargs): The date timestamp the Indicator was created.
            from_addr (str, kwargs): The **from** address for this Email.
            to (str, kwargs): The **to** address for this Email.
        """
        super(Email, self).__init__(
            tcex, 'Email', 'email', 'emails', owner=owner, name=name, **kwargs
        )
        self._data['to'] = to or kwargs.get('to') or kwargs.get('to_addr')
        self._data['from'] = from_addr or kwargs.get('from_addr') or kwargs.get('from')
        self._data['subject'] = subject or kwargs.get('subject')
        self._data['body'] = body or kwargs.get('body')
        self._data['header'] = header or kwargs.get('header')
        self._data['score'] = kwargs.get('score', 0)


'''
Documentation, License etc.

@package RoguePy.UI
'''
from RoguePy.libtcod import libtcod
from RoguePy.UI.Elements import Element

class Bar(Element):

  def __init__(self, x, y, w, h=1):
    super(Bar, self).__init__(x, y, w, h)
    self.setMin(0)
    self.setMax(100)
    self.setVal(100)

    self.chars = [
      libtcod.CHAR_BLOCK1,
      libtcod.CHAR_BLOCK2,
      libtcod.CHAR_BLOCK3
    ]

    self._fgMin = libtcod.white
    self._fgMax = libtcod.white
    self._bgMin = libtcod.black
    self._bgMax = libtcod.black

  def setMin(self, min):
    self._min = min
    return self
  def setMax(self,max):
    self._max = max
    return self
  def getMin(self):
    return self._min
  def getMax(self):
    return self._max

  def setVal(self,val):
    if val > self._max:
      val = self._max
    elif val < self._min:
      val = self._min
    self._val = val
    return self
  def getVal(self):
    return self._val

  def setChars(self,chars):
    self.chars = chars
    return self
  def getChars(self):
    return self.chars

  def calculateColors(self):
    self._colors = []
    length = self.width * len(self.chars)
    for i in range(length):
      coef = i / float(length)
      fg = libtcod.color_lerp(self._fgMin, self._fgMax, coef)
      bg = libtcod.color_lerp(self._bgMin, self._bgMax, coef)
      self._colors.append((fg, bg))

  def setMinColor(self, fg, bg=libtcod.black):
    self._fgMin = fg
    self._bgMin = bg
    self.calculateColors()
    return self
  def setMaxColor(self, fg, bg=libtcod.black):
    self._fgMax = fg
    self._bgMax = bg
    self.calculateColors()
    return self

  def draw(self):

    _max = float(self._max - self._min)
    val = self._val - self._min

    chars = len(self.chars)
    steps = self.width * chars

    if steps != len(self._colors):
      self.calculateColors()

    fullSteps = int(val / _max * steps)
    fullChars = int(fullSteps / chars)

    lastChar = self.chars[fullSteps % chars]

    colorIndex = max(0, min(len(self._colors) - 1, val-1))

    fg = self._colors[colorIndex][0]
    bg = self._colors[colorIndex][1]

    for y in range(self.height):
      for i in range(fullChars):
        libtcod.console_put_char_ex(self.console, i, y, self.chars[-1], fg, bg)
      libtcod.console_put_char_ex(self.console, fullChars, y, lastChar, fg, bg)


"""
 * Copyright 2020, Departamento de sistemas y Computación
 * Universidad de Los Andes
 *
 *
 * Desarrolado para el curso ISIS1225 - Estructuras de Datos y Algoritmos
 *
 *
 * This program 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 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 """

#______________________________________________________
# Imports
#______________________________________________________

import config as cf
from DISClib.ADT import list as lt
from DISClib.ADT import map as mp
from DISClib.DataStructures import listiterator as it
from DISClib.DataStructures import liststructure as lt
from DISClib.DataStructures import mapentry as me
import csv

"""
El controlador se encarga de mediar entre la vista y el modelo.
Existen algunas operaciones en las que se necesita invocar
el modelo varias veces o integrar varias de las respuestas
del modelo en una sola respuesta. Esta responsabilidad
recae sobre el controlador.
"""

#______________________________________________________
# Funcion 0
#______________________________________________________

def newlist():
    """
    Crea una lista vacia.
    """
    lst=lt.newList
    return lst

def loadCSVFile (file, lst):
    lst=lt.newList(datastructure="ARRAY_LIST")
    dialect = csv.excel()
    dialect.delimiter=";"
    try:
        with open( file, encoding="utf-8") as csvfile:
            row = csv.DictReader(csvfile, dialect=dialect)
            for elemento in row:
                lt.addLast(lst,elemento)
    except:
        print("Hubo un error con la carga del archivo")
    return lst

#______________________________________________________
# API del TAD Catalogo de peliculas
#______________________________________________________

def newCatalog():
    """
    Crea un nuevo catalogo.
    """
    Data={'casting': None, 'details': None}
    catalog={ 'Data': Data, 'production_companies': None, 'IDs':None,
    'director_name': None, 'actor_name': None,'genres':None, 'production_countries': None}

    catalog['Data']['casting']=lt.newList(datastructure='ARRAY_LIST')
    catalog['Data']['details']=lt.newList(datastructure='ARRAY_LIST')

    #catalog['IDs']=mp.newMap(numelements=(lt.size(catalog['Data']['details'])),
                                        #maptype='CHAINIG',
                                        #loadfactor=1,
                                        #comparefunction=ct.cmpfunctionID)
    catalog['production_companies']=mp.newMap(numelements=350000,
                                        maptype='PROBING',
                                        loadfactor=0.4,
                                        comparefunction=cmpfunctionCompanies)
    catalog['director_name']=mp.newMap(numelements=350000,
                                        maptype='PROBING',
                                        loadfactor=0.4,
                                        comparefunction= cmpfunctionDirectors)
    catalog['actor_name']=mp.newMap(numelements=350000,
                                        maptype='PROBING',
                                        loadfactor=0.4,
                                        comparefunction= cmpfunctionActor)
    catalog['genres']=mp.newMap(numelements=350000,
                                        maptype='PROBING',
                                        loadfactor=0.4,
                                        comparefunction= cmpfunctionGenres)
    catalog['production_countries']=mp.newMap(numelements=350000,
                                        maptype='PROBING',
                                        loadfactor=0.4,
                                        comparefunction= cmpfunctionCountry)
    return catalog

def newComopanies(company_name):
    """
    Crea un nuevo elemento de las compañias.
    """
    company={'name': None, 'movies':None, 'vote_avg': None}
    company['name']=company_name
    company['movies']=lt.newList(datastructure='SINGLE_LINKED', cmpfunction= cmpfunctionCompanies)
    return company

def newDirector(director_name):
    """
    Crea un nuevo elemento de los directores.
    """
    director={'name': None, 'movies': None, 'details': None, 'vote_avg': None}
    director['name']=director_name
    director['movies']=lt.newList(datastructure='SINGLE_LINKED')
    director['details']=lt.newList(datastructure='SINGLE_LINKED')
    return director

def newActor(actor_name):
    """
    Crea un nuevo elemento de los actores.
    """
    actor={'name': None, 'movies': None, 'details': None,'vote_avg': None}
    actor['name']=actor_name
    actor['movies']=lt.newList(datastructure='SINGLE_LINKED')
    actor['details'] = lt.newList(datastructure='SINGLE_LINKED')
    return actor

def newGenre(genre_name):
    """
    Crea un nuevo elemento de generos.
    """
    genre={'name': None, 'movies': None, 'vote_count': None}
    genre['name']=genre_name
    genre['movies']=lt.newList(datastructure='SINGLE_LINKED')
    return genre

def newCountry(country_name):
    """
    Crea un nuevo elemento de paises.
    """
    country={'name':None, 'movies':None, 'casting':None,'vote_avg': None}
    country['name']=country_name
    country['movies']=lt.newList(datastructure='SINGLE_LINKED')
    country['casting']=lt.newList(datastructure='SINGLE_LINKED')
    return country

def newID(ID):
    """
    Crea un nuevo elemento de ID.
    """
    elemnt={'ID':None, 'details':None, 'casting':None, 'vote_avg':None}
    elemnt['ID']=ID
    return elemnt

#______________________________________________________
# Funciones para agregar informacion al catalogo
#______________________________________________________

def addMovie(catalog, movie, info):
    """
    Agregar una pelicula.
    """
    if info == 1:
        movies=catalog['Data']['casting']
        lt.addLast(movies, movie)
    elif info ==2:
        movies=catalog['Data']['details']
        lt.addLast(movies, movie)

def addCompany(movie,catalog):
    """
    Agrega una nueva compañia al mapa de productoras
    """
    companies=catalog['production_companies']
    company_name=movie['production_companies']
    exist=mp.contains(companies, company_name)
    if exist:
        entry=mp.get(companies, company_name)
        company=me.getValue(entry)
    else:
        company=newComopanies(company_name)
        mp.put(companies, company_name, company)
    lt.addLast(company['movies'], movie)
    avg_cp=company['vote_avg']
    avg_mv=movie['vote_average']
    if avg_cp == None:
        company['vote_avg']=float(avg_mv)
    else:
        company['vote_avg']= round(((avg_cp+ float(avg_mv))/2),2)

def addDirector(movie2, movie1, catalog):
    """
    Agrega informacion de un director en el mapa de directores.
    """
    directors=catalog['director_name']
    director_name=movie2['director_name']
    exist=mp.contains(directors, director_name)
    if exist:
        entry=mp.get(directors, director_name)
        director=me.getValue(entry)
    else:
        director=newDirector(director_name)
        mp.put(directors, director_name, director)
    lt.addLast(director['movies'], movie2)
    lt.addLast(director['details'], movie1)
    avg_dt=director['vote_avg']
    avg_mv=movie1['vote_average']
    if avg_dt == None:
        director['vote_avg']=float(avg_mv)
    else:
        director['vote_avg']= round(((avg_dt+ float(avg_mv))/2),2)

def addActor(movie2, movie1, catalog):
    """
    Agrega informacion de un actor en el mapa de actores.
    """
    actors=catalog['actor_name']
    actor1=movie2['actor1_name']
    if actor1 != None:
        exist=mp.contains(actors, actor1)
        if exist:
            entry=mp.get(actors, actor1)
            actor=me.getValue(entry)
        else:
            actor=newActor(actor1)
            mp.put(actors, actor1, actor)
        lt.addLast(actor['movies'], movie2)
        lt.addLast(actor['details'], movie1)
        avg_at=actor['vote_avg']
        avg_mv=movie1['vote_average']
        if avg_at == None:
            actor['vote_avg']=float(avg_mv)
        else:
            actor['vote_avg']= round(((avg_at+ float(avg_mv))/2),2)

    actor2=movie2['actor2_name']
    if actor2 != None:
        exist=mp.contains(actors, actor2)
        if exist:
            entry=mp.get(actors, actor2)
            actor=me.getValue(entry)
        else:
            actor=newActor(actor2)
            mp.put(actors, actor2, actor)
        lt.addLast(actor['movies'], movie2)
        lt.addLast(actor['details'], movie1)
        avg_at=actor['vote_avg']
        avg_mv=movie1['vote_average']
        if avg_at == None:
            actor['vote_avg']=float(avg_mv)
        else:
            actor['vote_avg']= round(((avg_at+ float(avg_mv))/2),2)
    actor3=movie2['actor3_name']
    if actor3 != None:
        exist=mp.contains(actors, actor3)
        if exist:
            entry=mp.get(actors, actor3)
            actor=me.getValue(entry)
        else:
            actor=newActor(actor3)
            mp.put(actors, actor3, actor)
        lt.addLast(actor['movies'], movie2)
        lt.addLast(actor['details'], movie1)
        avg_at=actor['vote_avg']
        avg_mv=movie1['vote_average']
        if avg_at == None:
            actor['vote_avg']=float(avg_mv)
        else:
            actor['vote_avg']= round(((avg_at+ float(avg_mv))/2),2)
    actor4=movie2['actor4_name']
    if actor4 != None:
        exist=mp.contains(actors, actor4)
        if exist:
            entry=mp.get(actors, actor4)
            actor=me.getValue(entry)
        else:
            actor=newActor(actor4)
            mp.put(actors, actor4, actor)
        lt.addLast(actor['movies'], movie2)
        lt.addLast(actor['details'], movie1)
        avg_at=actor['vote_avg']
        avg_mv=movie1['vote_average']
        if avg_at == None:
            actor['vote_avg']=float(avg_mv)
        else:
            actor['vote_avg']= round(((avg_at+ float(avg_mv))/2),2)
    actor5=movie2['actor5_name']
    if actor5 != None:
        exist=mp.contains(actors, actor5)
        if exist:
            entry=mp.get(actors, actor5)
            actor=me.getValue(entry)
        else:
            actor=newActor(actor5)
            mp.put(actors, actor5, actor)
        lt.addLast(actor['movies'], movie2)
        lt.addLast(actor['details'], movie1)
        avg_at=actor['vote_avg']
        avg_mv=movie1['vote_average']
        if avg_at == None:
            actor['vote_avg']=float(avg_mv)
        else:
            actor['vote_avg']= round(((avg_at+ float(avg_mv))/2),2)

def addGenre(movie, catalog):
    """
    Agrega informacion de un genero en el mapa de generos.
    """
    genres=catalog['genres']
    genre_list=movie['genres'].split('|')
    for genre_name in genre_list:
        exist=mp.contains(genres, genre_name)
        if exist:
            entry=mp.get(genres, genre_name)
            genre=me.getValue(entry)
        else:
            genre=newGenre(genre_name)
            mp.put(genres, genre_name, genre)
        lt.addLast(genre['movies'], movie)
        count_gn=genre['vote_count']
        count_mv=movie['vote_count']
        if count_gn == None:
            genre['vote_count']=float(count_mv)
        else:
            genre['vote_count']=round(((count_gn+ float(count_mv))/2),2)

def addCountry(movie2, movie1, catalog):
    """
    Agrega informacion de un país en el mapa de un país.
    """
    countries=catalog['production_countries']
    country_name=movie1['production_countries']
    exist=mp.contains(countries, country_name)
    if exist:
        entry=mp.get(countries, country_name)
        country=me.getValue(entry)
    else:
        country=newCountry(country_name)
        mp.put(countries, country_name, country)
    lt.addLast(country['movies'], movie1)
    lt.addLast(country['casting'], movie2)
    avg_ct=country['vote_avg']
    avg_mv=movie1['vote_average']
    if avg_ct == None:
        country['vote_avg']=float(avg_mv)
    else:
        country['vote_avg']=round(((avg_ct+ float(avg_mv))/2),2)

def addID(movie, catalog, info):
    """
    Agraga informacion al mapa ID deacuerdo a la info.
    """
    IDs=catalog['ID']
    ID=movie['id']
    exist=mp.contains(IDs, ID)
    if info==1:
        if exist:
            entry=mp.get(IDs, ID)
            ID_info=me.getValue(entry)
        else:
            ID_info=newID(ID)
            mp.put(IDs, ID, ID_info)
        lt.addLast(ID_info['casting'], movie)
    elif info ==2:
        if exist:
            entry=mp.get(IDs, ID)
            ID_info=me.getValue(entry)
        else:
            ID_info=newID(ID)
            mp.put(IDs, ID, ID_info)
        lt.addLast(ID_info['casting'], movie)
        avg_ID=ID_info['vote_avg']
        avg_mv=movie['vote_average']
        if avg_ID == None:
            ID_info['vote_avg']=float(avg_mv)
        else:
            ID_info['vote_avg']= round(((avg_ID+ float(avg_mv))/2),2)

#______________________________________________________
# Funciones de consulta
#______________________________________________________

def getFirstLastMovies(catalog):
    """
    Retorna el primer y ultimo libro de la lista.
    """
    first_movie=lt.firstElement(catalog)
    last_movie=lt.lastElement(catalog)
    return (first_movie, last_movie)

def countBy(criteria, lst):
    """
    cuenta la cantidad de elementos por un crterio.
    """
    number=0
    names=[]
    iterator=it.newIterator(lst)
    while it.hasNext(iterator):
        movie=it.next(iterator)
        value=movie[criteria]
        if value not in names:
            number+=1
            names.append(value)
    return number

def countGenres(lst):
    """
    cuenta la cantidad de elementos por un crterio.
    """
    number=0
    names=[]
    iterator=it.newIterator(lst)
    while it.hasNext(iterator):
        movie=it.next(iterator)
        genres_list=movie['genres'].split('|')
        for genre in genres_list:
            if genre not in names:
                number+=1
                names.append(genre)
    return number

def countActors(lst):
    """
    Cuenta los actores de la lista.
    """
    names=[]
    number=0
    iterator=it.newIterator(lst)
    while it.hasNext(iterator):
        movie=it.next(iterator)
        actor1=movie['actor1_name']
        actor2=movie['actor2_name']
        actor3=movie['actor3_name']
        actor4=movie['actor4_name']
        actor5=movie['actor5_name']
        if actor1 not in names:
            number+=1
            names.append(actor1)
        if actor2 not in names:
            number+=1
            names.append(actor2)
        if actor3 not in names:
            number+=1
            names.append(actor3)
        if actor4 not in names:
            number+=1
            names.append(actor4)
        if actor5 not in names:
            number+=1
            names.append(actor5)
    return number

def getElementCriteria(catalog, criteria, key):
    """
    Retorna el elemento a buscar de acuerdo al criterio
    """
    value= None
    if mp.contains(catalog[criteria], key):
        entry=mp.get(catalog[criteria], key)
        value=me.getValue(entry)
    return value

def getCompany(catalog, company):
    """
    Busca la productora en el mapa de productoras del catalogo.
    """
    value= getElementCriteria(catalog, 'production_companies', company)
    try:
        movies=value['movies']
        lst=lt.newList(datastructure='SINGLE_LINKED')
        iterator=it.newIterator(movies)
        while it.hasNext(iterator):
            movie=it.next(iterator)
            movie_name=movie['title']
            lt.addLast(lst, movie_name)
        avg=value['vote_avg']
        size=lt.size(movies)
        return (lst, avg, size)
    except:
        return None

def getDirector(catalog, director):
    """
    Retorna a un director con su informacion.
    """
    value= getElementCriteria(catalog, 'director_name', director)
    try:
        movies=value['movies']
        info=value['details']
        lst=lt.newList(datastructure='SINGLE_LINKED')
        iterator=it.newIterator(info)
        while it.hasNext(iterator):
            movie=it.next(iterator)
            title=movie['title']
            lt.addLast(lst, title)
        avg=value['vote_avg']
        size=lt.size(movies)
        return (lst, size, avg)
    except:
        return None

def getActor(catalog, actor):
    """
    Retorna a un actor con su informacion.
    """
    value= getElementCriteria(catalog, 'actor_name', actor)
    try:
        movies=value['movies']
        info=value['details']
        lst=lt.newList(datastructure='SINGLE_LINKED')
        directors={}
        iterator1=it.newIterator(movies)
        iterator2=it.newIterator(info)
        while it.hasNext(iterator1):
            movie=it.next(iterator1)
            director=movie['director_name']
            if director in directors:
                directors[director]+=1
            else:
                directors[director]=1
        while it.hasNext(iterator2):
            movie=it.next(iterator2)
            title=movie['title']
            lt.addLast(lst, title)
        avg=value['vote_avg']
        size=lt.size(movies)
        max_director=max(directors)
        return (lst, size, avg, max_director )
    except:
        return None

def getGenre(catalog, genre):
    """
    Retorna el genero con la informacion que tiene como valor,
    """
    value=getElementCriteria(catalog, 'genres', genre)
    try:
        info=value['movies']
        lst=lt.newList(datastructure='SINGLE_LINKED')
        iterator=it.newIterator(info)
        while it.hasNext(iterator):
            movie=it.next(iterator)
            title=movie['title']
            lt.addLast(lst, title)
        avg=value['vote_count']
        size=lt.size(info)
        return (lst, avg, size)
    except:
        return None

def getCountry(catalog, country):
    """
    Retorna el lista de paises con su respectiva información.
    """
    value=getElementCriteria(catalog, 'production_countries', country)
    try:
        info= value['movies']
        casting=value['casting']
        lst=lt.newList(datastructure='SINGLE_LINKED')
        iterator1= it.newIterator(info)
        iterator2= it.newIterator(casting)
        while it.hasNext(iterator1):
            movie1=it.next(iterator1)
            movie2=it.next(iterator2)
            title=movie1["title"]
            date= movie1["release_date"]
            director= movie2["director_name"]
            data=(title, date, director)
            lt.addLast(lst, data)
        return lst
    except:
        return None

#______________________________________________________
# Funciones de Comparacion
#______________________________________________________

def cmpfunctionCompanies(element1, entry):
    """
    Compara Compañias.
    """
    company = me.getKey(entry)
    if (element1 == company):
        return 0
    elif (element1 > company):
        return 1
    else:
        return -1

def cmpfunctionDirectors(element1, entry):
    """
    Compara Directores.
    """
    Director = me.getKey(entry)
    if (element1 == Director):
        return 0
    elif (element1 > Director):
        return 1
    else:
        return -1

def cmpfunctionActor(element1, entry):
    """
    Compara Actores.
    """
    Actor = me.getKey(entry)
    if (element1 == Actor):
        return 0
    elif (element1 > Actor):
        return 1
    else:
        return -1

def cmpfunctionGenres(element1, entry):
    """
    Compara Generos.
    """
    genre = me.getKey(entry)
    if (element1 == genre):
        return 0
    elif (element1 > genre):
        return 1
    else:
        return -1

def cmpfunctionCountry(element1, entry):
    """
    Compara Países.
    """
    country = me.getKey(entry)
    if (element1 == country):
        return 0
    elif (element1 > country):
        return 1
    else:
        return -1

def cmpfunctionID(element1, entry):
    """
    Compara IDs.
    """
    id = float(me.getKey(entry))
    element1= float(element1)
    if (element1 == id):
        return 0
    elif (element1 > id):
        return 1
    else:
        return -1


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

# In[34]:

#引入sklearn的工具包
from sklearn import svm
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib as mpl
get_ipython().run_line_magic('matplotlib', 'inline')
from sklearn.model_selection import train_test_split
from sklearn import preprocessing

# In[35]:

data=pd.read_csv('svmdata.csv')

# In[17]:

data.head()

# In[18]:

data.describe()

# In[19]:

#因为数据集列比较多，需要把df的列都显示出来
#显示所有列----pd.set_option('display.max_columns', None)
#显示所有行----pd.set_option('display.max_rows', None)

pd.set_option('display.max_columns',None)
print(data.columns)
print(data.head(5))
print(data.describe())

# In[20]:

#将特征字段分成3段
features_mean=list(data.columns[2:12])
features_se=list(data.columns[12:22])
features_worst=list(data.columns[22:32])

# In[21]:

#数据清洗
#id列没有用，删除
data.drop('id',axis=1,inplace=True)
#将B良性替换成0，恶性替换成1
data['diagnosis']=data['diagnosis'].map({'M':1,'B':0})

# In[22]:

data.head()

# In[23]:

#接下来进行特征字段的筛选，首先需要观察下features_mean各个变量之间的关系。可以用corr（）的函数

#将肿瘤诊断结果可视化
sns.countplot(data['diagnosis'],label="Count")
plt.show()
#用热力图呈现 features_mean字段之间的相关性
corr=data[features_mean].corr()
plt.figure(figsize=(14,14))
#annot=True 显示每个方格的数据
sns.heatmap(corr,annot=True)
plt.show()

# 图中颜色越浅表示相关性越大。所以我们能发现：radius_mean、perimeter_mean，area_mean这三个相关性大，而compactness_mean、concavity_mean，concave_points_mean这三个特征相关性大。

# 下面来进行特征选择
# 特征选择的目的是降维，用少量的特征代表数据的特性，这样也可以增强分类器的泛化能力，避免数据过拟合。
# mean/se/worst这三组特征是对同一组内容的不同度量。我们可以只保留mean这组特征。
# 另外，在mean这组特征中，radius_mean,perimeter_mean,area_mean这三个属性相关性大，
# 而compactness_mean、concavity_mean，concave_points_mean这三个特征相关性大。
# 我们可以分别从这2类中选择一个属性作为代表，比如radius_mean和compactness_mean
#
# 那么我们就可以把原来的10个属性缩减为6个属性
#

# In[24]:

#特征选择
features_remain=['radius_mean','texture_mean','smoothness_mean','compactness_mean','symmetry_mean','fractal_dimension_mean']

# #准备训练集和测试集
# #30%为测试集，70%为训练集
#

# In[29]:

#记得引入---from sklearn.model_selection import train_test_split
train,test=train_test_split(data,test_size=0.3)
train_X=train[features_remain]
train_y=train['diagnosis']
test_X=test[features_remain]
test_y=test['diagnosis']

# 训练之前，需要对数据进行规范化，让数据在同一个量级上，避免因为维度问题造成数据误差。

# In[38]:

#采用z-score 规范化数据，保证每个特征维度的数据均值为0，方差为1
#记得引入---from sklearn import preprocessing
ss=preprocessing.StandardScaler()
train_X=ss.fit_transform(train_X)
test_X=ss.transform(test_X)

# In[43]:

from sklearn.metrics import accuracy_score
#创建svm分类器
model=svm.SVC()
#用训练集做训练
model.fit(train_X,train_y)
#用测试集做预测
prediction=model.predict(test_X)
print('准确率： ',accuracy_score(prediction,test_y))

# In[ ]:


def big_matrix_process_results(year, results, big_matrix, week):

    import pandas as pd
    import numpy as np
    import seaborn as sns
    import matplotlib.pyplot as plt
    from DK_Data_Import import dk_data_import

    'Read in Result and Lineup Files'
    result_data = dk_data_import(year, week, results, 2)
    big_matrix = pd.read_csv(big_matrix)

    'Initialize Point Vectors'
    dk_competitor_points = result_data['Points']
    python_lineup_points = []

    big_matrix['FPTS'] = 0
    big_matrix['%Drafted'] = 0
    big_matrix['Real_Value'] = 0

    for i in range(len(big_matrix)):
        player = big_matrix.loc[i, 'Player']
        index = result_data.index[result_data['Player'] == player].values

        points = result_data.loc[index, 'FPTS'].values
        if points.size ==0:
            points = 0
        big_matrix.loc[i, 'FPTS'] = points

        ownership = result_data.loc[index, '%Drafted'].values
        if ownership.size ==0:
            ownership = 0
        else:
            ownership = float(np.asscalar(ownership).rstrip("%"))
        big_matrix.loc[i, '%Drafted'] = ownership

        if points>1:
            big_matrix.loc[i, 'Real_Value'] = big_matrix.loc[i, 'Salary']/points
        else:
            big_matrix.loc[i, 'Real_Value'] = 9999

    big_matrix.to_csv(year + '/' + week + '/BigMatrix_' + week + '_Processed.csv')


# coding: utf8
from __future__ import unicode_literals
from abc import ABCMeta, abstractmethod
from six import with_metaclass
import warnings
import unittest

from geopy.point import Point
from geopy.geocoders import Pelias
from test.geocoders.util import GeocoderTestBase, env

class BasePeliasTestCase(with_metaclass(ABCMeta, object)):

    delta = 0.04

    @classmethod
    @abstractmethod
    def make_geocoder(cls, **kwargs):
        pass

    @classmethod
    def setUpClass(cls):
        cls.geocoder = cls.make_geocoder()

        cls.known_state_de = "Verwaltungsregion Ionische Inseln"
        cls.known_state_en = "Ionian Islands Periphery"

    def test_geocode(self):
        self.geocode_run(
            {"query": "435 north michigan ave, chicago il 60611 usa"},
            {"latitude": 41.890, "longitude": -87.624},
        )

    def test_unicode_name(self):
        self.geocode_run(
            {"query": "san josé california"},
            {"latitude": 37.33939, "longitude": -121.89496},
        )

    def test_reverse(self):
        self.reverse_run(
            {"query": Point(40.75376406311989, -73.98489005863667)},
            {"latitude": 40.75376406311989, "longitude": -73.98489005863667}
        )

    def test_boundary_rect(self):
        self.geocoder = self.make_geocoder(
            boundary_rect=[[50.1, -130.1], [44.1, -100.9]])
        self.geocode_run(
            {"query": "moscow"},  # Idaho USA
            {"latitude": 46.7323875, "longitude": -117.0001651},
        )

    def test_boundary_rect_deprecated(self):
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            self.geocoder = self.make_geocoder(
                boundary_rect=[-130.1, 44.1, -100.9, 50.1])
            self.geocode_run(
                {"query": "moscow"},  # Idaho USA
                {"latitude": 46.7323875, "longitude": -117.0001651},
            )
            self.assertEqual(1, len(w))

@unittest.skipUnless(
    bool(env.get('PELIAS_DOMAIN')),
    "No PELIAS_DOMAIN env variable set"
)
class PeliasTestCase(BasePeliasTestCase, GeocoderTestBase):

    def make_geocoder(cls, **kwargs):
        return Pelias(env.get('PELIAS_DOMAIN'), api_key=env.get('PELIAS_KEY'),
                      **kwargs)


import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
import re

m = 0
address = input('Enter location: ')

print('Retrieving', address)
uh = urllib.request.urlopen(address)
data = uh.read()
print('Retrieved', len(data), 'characters')

y = BeautifulSoup(data, "xml")
tags = y('count')

y = (re.findall('[0-9]+',str(tags)))
y = list(map(int, y))
print ("Count: ", len(y))

for i in y:
    m = i + m
print("Sum: ", m)

Using Python to Access Web Data/Week 5_Extracting Data from XML.py


import pandas as pd

import numpy as np

from fbprophet import Prophet

from ..oracles import FalseProphet
from .general_utils import load_obj
from .viz_utils import plot_performance

def pred_stream(start_value, df_ts, model, remappers, rescaler,
                n_boot=30):
    """
    """
    df_ts = df_ts.sort_values('ds')
    month = pd.DatetimeIndex(df_ts['ds']).month.values
    day_week = pd.DatetimeIndex(df_ts['ds']).dayofweek.values
    day_month = pd.DatetimeIndex(df_ts['ds']).day.values

    start_value = rescaler.transform(
        np.array([start_value]).reshape(1, 1)
    ).reshape(1, 1, 1)

    month = np.array(
        [remappers['month'][value] for value in month]
    ).reshape(-1, 1)
    day_week = np.array(
        [remappers['day_week'][value] for value in day_week]
    ).reshape(-1, 1)
    day_month = np.array(
        [remappers['day_month'][value] for value in day_month]
    ).reshape(-1, 1)

    X = [
        start_value,
        month,
        day_week,
        day_month
    ]

    predictions = model.predict_with_uncertainty(
        X,
        n_boot=n_boot,
        batch_size=1
    )

    mean_prediction = predictions.mean(axis=1)
    mean_prediction = rescaler.inverse_transform(
        mean_prediction.reshape(-1, 1)
    ).flatten()
    upper_prediction = np.percentile(predictions, 92.5, axis=1)
    upper_prediction = rescaler.inverse_transform(
        upper_prediction.reshape(-1, 1)
    ).flatten()
    lower_prediction = np.percentile(predictions, 2.5, axis=1)
    lower_prediction = rescaler.inverse_transform(
        lower_prediction.reshape(-1, 1)
    ).flatten()

    return mean_prediction, upper_prediction, lower_prediction

def test_models(datasets, periods=None, figsize=(15, 5), n_boot=30):
    """
    """
    for dataset_name, cut_point in datasets.items():

        print(f'Testing {dataset_name} with Prophet')

        if periods is None:
            look_ahead = cut_point
        else:
            look_ahead = periods

        df = pd.read_csv(
            f'data//csv//{dataset_name}.csv'
        )

        df_tr = df[:-cut_point]

        y = df['y'].values[-cut_point:]
        remappers = load_obj(
            path=f'results//saved_obj//{dataset_name}_rmp'
        )
        rescaler = load_obj(
            path=f'results//saved_obj//{dataset_name}_rsc'
        )

        # ######################### PROPHET ###################################

        prophet = Prophet()
        prophet.fit(df_tr)

        df_ts = prophet.make_future_dataframe(periods=look_ahead)

        predictions = prophet.predict(df_ts)

        mean_prediction = predictions['yhat'].values[-look_ahead:]
        upper_prediction = predictions['yhat_upper'].values[-look_ahead:]
        lower_prediction = predictions['yhat_lower'].values[-look_ahead:]

        plot_performance(
            y=y,
            mean_prediction=mean_prediction,
            upper_prediction=upper_prediction,
            lower_prediction=lower_prediction,
            model='Prophet',
            dataset_name=dataset_name,
            figsize=figsize
        )

        # ################### FALSE PROPHET ###################################

        false_prophet = FalseProphet(
            X_shape=(None, 1, 1),
            batch_size=1,
            model_tag=dataset_name
        )

        false_prophet.build()
        false_prophet.load_weights()

        mean_prediction, upper_prediction, lower_prediction = pred_stream(
            start_value=df_tr['y'].values[-1],
            df_ts=df_ts[-look_ahead:],
            model=false_prophet,
            remappers=remappers,
            rescaler=rescaler,
            n_boot=n_boot
        )

        plot_performance(
            y=y,
            mean_prediction=mean_prediction,
            upper_prediction=upper_prediction,
            lower_prediction=lower_prediction,
            model='False Prophet',
            dataset_name=dataset_name,
            figsize=figsize
        )


import os
os.environ['CUDA_VISIBLE_DEVICES']='-1'
import cv2
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

from inputs.ssdinputs import SSDInputs
from ssd.configure import Configure
from builder.model_builder import ModelBuilder
import ssd.dataset.voc_eval as voc

config = Configure(path='../ssd/ssd_mobile.config').get_config()

log_dir = config['eval']['log_dir']
ckpt_dir = config['eval']['ckpt_dir']
# filenames = config['image']['path']

builder = ModelBuilder(config, mode='eval', input_class=SSDInputs)

net = builder.model

net_inputs = net.inputs
ground_truth = net.ground_truth
box, prob = builder.pipeline.decode_for_ap(net.outputs, config['model']['num_classes'])

color = [(255, 0, 0)]
saver = tf.train.Saver(name="saver")
num_classes = config['model']['num_classes'] + 1

# txt = ['aeroplane', 'bicycle', 'bird', 'boat', 'bottle',
#     'bus', 'car', 'cat', 'chair', 'cow', 'diningtable',
#     'dog', 'horse', 'motorbike', 'person', 'pottedplant',
#     'sheep', 'sofa', 'train', 'tvmonitor']
txt = ['fish']
fs = []
for i in range(len(txt)):
    fs.append(open('{}_det.txt'.format(txt[i]), 'w'))
s = 1
with tf.Session() as sess:
    sess.run(tf.local_variables_initializer())
    dir = tf.train.latest_checkpoint(ckpt_dir)
    saver.restore(sess, dir)

    while True:
        try:
            im, name, b, p = sess.run([net_inputs['images'], net_inputs['names'], box, prob], feed_dict={net.is_training: False})
            im = (im * 128 + 128).astype(np.uint8)[0, :]
            im = cv2.resize(im, (500, 375))
            name = name[0].decode()
            for bb, pp, f in zip(b, p, fs):
                for bbb, ppp in zip(bb, pp):
                    xmin = max(int(bbb[1] * 500) + 1, 0)
                    ymin = max(int(bbb[0] * 375) + 1, 0)
                    xmax = min(int(bbb[3] * 500) + 1, 499)
                    ymax = min(int(bbb[2] * 375) + 1, 499)
                    xc = (xmin + xmax) / 2
                    yc = (ymin + ymax) / 2
                    w = (xmax - xmin) / 2 * s
                    h = (ymax - ymin) / 2 * s
                    xmin = int(xc - w)
                    xmax = int(xc + w)
                    ymin = int(yc - h)
                    ymax = int(yc + h)
                    if ppp > 0.11:
                        cv2.rectangle(im, (xmin, ymin), (xmax, ymax), color[0], 2)
                    f.write('{:s} {:f} {:d} {:d} {:d} {:d}\n'.format(name, ppp, xmin, ymin, xmax, ymax))
                    a = 1
            cv2.imshow("", im)
            cv2.waitKey(1)
        except tf.errors.OutOfRangeError as e:
            for f in fs:
                f.close()
            break
mAP = 0
for n in txt:
    rec, prec, ap = voc.voc_eval("/home/yqi/workspace/DL-tools/test/{}_det.txt".format(n),
                                 "/home/yqi/Desktop/WineDownloads/大作业3/to student/FishData_subset/Annotations/{:s}.xml",
                                 "/home/yqi/Desktop/WineDownloads/大作业3/to student/FishData_subset/ImageSets/Main/test.txt",
                                 n,
                                 ".",
                                 ovthresh=0.5,
                                 use_07_metric=False)
    plt.figure()
    plt.plot(rec, prec)
    plt.xlim(0, 1)
    plt.xlabel('Recall', fontsize=14)
    plt.ylabel('Precision', fontsize=14)
    plt.ylim(0, 1)
    plt.grid()
    plt.text(0.1, 1.02, n, fontsize=14)
    plt.text(0.82, 1.02, 'AP=%.02f%%' % (ap * 100), fontsize=14)
    plt.savefig(n+'.png')

    mAP += ap / len(txt)
    print('AP for {}:'.format(n), ap)
print("mAP:", mAP)


import os
import sys
import socket
import threading
import time
import logging

from mininet.net import Containernet
from mininet.node import RemoteController
from mininet.cli import CLI
from mininet.link import TCLink

logger = logging.getLogger("mininet")
logger.setLevel(logging.INFO)
stream_handler = logging.StreamHandler()
logger.addHandler(stream_handler)

def receive_nsf_request():
    while True:
        server_socket = socket.socket()
        server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        server_socket.bind(('127.0.0.1', 55564)) #EDIT TO YOUR OPENSTACK IP ADDRESS
        server_socket.listen(5)
        client_socket, addr = server_socket.accept()
        data = client_socket.recv(1024).decode('utf_8')
        temp_data = data.split(",")

        # for i in range(0, len(temp_data)):
            # print("tacker vnf-create --vnfd-name " + temp_data[i] + "_vnfd "
            # + temp_data[i] + "_vnf")
            # os.system("tacker vnf-create --vnfd-name " +
            #           temp_data[i] + "_vnfd " + temp_data[i] + "_vnf")
            # logger.info("vnf-create --vnfd-name " + temp_data[i] + "_vnfd " + temp_data[i] + "_vnf")
            # time.sleep(5)

        # print("tacker vnffg-create --vnffgd-template vnffg_test.yaml
        # block_sns")
        # time.sleep(90)
        # os.system(
            # "tacker vnffg-create --vnffgd-template vnffg_test.yaml block_sns")
        #logger.info("vnffg-create --vnffgd-template vnffg_test.yaml block_sns")

        logger.info('iptables -A FORWARD -p tcp -d ' + d3.IP() + ' --dport 80 -j REJECT')
        r1.cmd('iptables -A FORWARD -p tcp -d ' + d3.IP() + ' --dport 80 -j REJECT')

threading._start_new_thread(receive_nsf_request, ())

logger.info("\nStarting Mininet Server...\n")

net = Containernet(controller=RemoteController)

logger.info('*** Adding controller\n')
c0 = net.addController('c0',
			controller=RemoteController,
			ip='127.0.0.1',
			port=6633)

logger.info('*** Adding docker containers\n')
r1 = net.addDocker('r1', ip='10.1.1.1/24', dimage="httpd:test")
d1 = net.addDocker('d1', ip='10.1.1.251/24', dimage="dos:1.0", defaultRoute='via 10.1.1.1', dcmd="flask run --host='0.0.0.0'", ports=[5000], port_bindings={5000:5000})
d2 = net.addDocker('d2', ip='10.1.1.252/24', dimage="httpd:test", defaultRoute='via 10.1.1.1', dcmd="sh run.sh")
d3 = net.addDocker('d3', ip='11.2.2.252/24', dimage="httpd:test", defaultRoute='via 11.2.2.1', dcmd="sh run.sh")
d4 = net.addDocker('d4', ip='11.2.2.253/24', dimage="httpd:test", defaultRoute='via 11.2.2.1', dcmd="sh run.sh")

logger.info('*** Adding switches\n')
s1 = net.addSwitch('s1')
s2 = net.addSwitch('s2')

logger.info('*** Creating links\n')
net.addLink(s1, r1)
net.addLink(s2, r1, params2={"ip":"11.2.2.1/24"})
net.addLink(s1, d1)
net.addLink(s1, d2)
net.addLink(s2, d3)
net.addLink(s2, d4)

logger.info('*** Starting network\n')
net.start()

logger.info('*** Running CLI\n')
CLI(net)

logger.info('*** Stopping network')
net.stop()

# r1 iptables -D FORWARD 1
# r1 iptables -L -v
# r1 iptables -A FORWARD -p tcp -d d2 --dport 80 -j DROP
# r1 iptables -A FORWARD -d d2 -j DROP


#! /usr/bin/python3

"""
    Initial setup a structure for Energy evaluation
"""
import argparse
import os

from Bio.PDB.PDBParser import PDBParser
from Bio.PDB.NACCESS import NACCESS_atomic
from Bio.PDB.PDBIO import PDBIO, Select

from residue_library import ResiduesDataLib
from forcefield import VdwParamset
import energies as en

NACCESS_BINARY = '/home/gelpi/DEVEL/Biophysics/soft/NACCESS/naccess'

parse_cmd = argparse.ArgumentParser(
    prog='structure_setup',
    description='basic structure setup'
)

parse_cmd.add_argument(
    '--rlib',
    action='store',
    dest='reslib_file',
    default='data/aaLib.lib',
    help='Residue Library'
)
parse_cmd.add_argument(
    '--vdw',
    action='store',
    dest='vdwprm_file',
    default='data/vdwprm',
    help='Vdw parameters'
)

parse_cmd.add_argument(
    '--dist',
    action='store',
    dest='cutoff_dist',
    default=8.0,
    type=float,
    help='Cutoff distance for determining the interface (0: use all residues):'
)
parse_cmd.add_argument('pdb_file', help='Input PDB', type=open)

args = parse_cmd.parse_args()

print("PDB.filename:", args.pdb_file.name)
print("Residue Lib.:", args.reslib_file)
print("PDB.filename:", args.vdwprm_file)
print("Distance:", args.cutoff_dist)

# Loading Libraries
# loading residue library from data/aaLib.lib
residue_library = ResiduesDataLib(args.reslib_file)

# loading VdW parameters
ff_params = VdwParamset(args.vdwprm_file)

parser = PDBParser(PERMISSIVE=1)
print('Parsing', args.pdb_file)
# load structure from PDB file of PDB ifle handler
st = parser.get_structure('STR', args.pdb_file.name)

# assign data types, and charges from libraries
# We will use the xtra attribute in Bio.PDB.Atom to hold the new data
# Possible errors on N-term and C-Term atoms
# Possible errors on HIS alternative forms

en.add_atom_parameters(st, residue_library, ff_params)

# Calculating surfaces
# The specific PATH to naccess script (in soft) is needed
# ASA goes to .xtra field directly

srf = NACCESS_atomic(st[0], naccess_binary=NACCESS_BINARY)

# Prepare surfaces for the separate chains
# Alternatively the twp PDB files can be prepared outside and parsed here

io = PDBIO()
st_chains = {}
# Using BioIO trick (see tutorial) to select chains
class SelectChain(Select):
    def __init__(self, chid):
        self.id = chid

    def accept_chain(self, chain):
        if chain.id == self.id:
            return 1
        else:
            return 0

for ch in st[0]:
    io.set_structure(st)
    io.save('tmp.pdb', SelectChain(ch.id))
    st_chains[ch.id] = parser.get_structure('stA', 'tmp.pdb')
    en.add_atom_parameters(st_chains[ch.id], residue_library, ff_params)
    srfA = NACCESS_atomic(st_chains[ch.id][0], naccess_binary=NACCESS_BINARY)
os.remove('tmp.pdb')

## Interface residues
if args.cutoff_dist > 0.:
    interface = en.get_interface(st, args.cutoff_dist)

## Initiatlize Energy aggregates
elec = {}
elec_ala = {}

vdw = {}
vdw_ala = {}

solvAB = {}
solvAB_ala = {}

solvA = {}
solvA_ala = {}

totalIntElec = 0.
totalIntVdw = 0.
totalSolv = 0.
totalSolvMon = {}
## We get the chsin ids,not always they are A and B
chids = []
for ch in st[0]:
    chids.append(ch.id)
    totalSolvMon[ch.id] = 0

total = 0.

for ch in st[0]:
    for res in ch.get_residues():
        if args.cutoff_dist > 0 and res not in interface[ch.id]:
            continue
        elec[res], elec_ala[res], vdw[res], vdw_ala[res] = en.calc_int_energies(st[0], res)
        solvAB[res], solvAB_ala[res] = en.calc_solvation(st[0], res)
        solvA[res], solvA_ala[res] = en.calc_solvation(
            st_chains[ch.id],
            st_chains[ch.id][0][ch.id][res.id[1]]
        )
        totalIntElec += elec[res]
        totalIntVdw += vdw[res]
        totalSolv += solvAB[res]
        totalSolvMon[ch.id] += solvA[res]
        total += elec[res] + vdw[res] + solvAB[res] - solvA[res]
        print(
            'D#{:11} {:11.4f}{:11.4f}{:11.4f}{:11.4f} - {:11.4f}{:11.4f}{:11.4f}{:11.4f}'.format(
                en.residue_id(res),
                elec[res], vdw[res], solvAB[res], solvA[res],
                elec_ala[res], vdw_ala[res], solvAB_ala[res], solvA_ala[res]
            )
        )
print("Interaction energy based in interface residues only")
print('{:20}: {:11.4f}'.format('Total Elec Int.', totalIntElec))
print('{:20}: {:11.4f}'.format('Total Vdw Int.', totalIntVdw))
print('{:20}: {:11.4f}'.format('Total Solv AB', totalSolv))
print('{:19}{}: {:11.4f}'.format('Total Solv ', chids[0], totalSolvMon[chids[0]]))
print('{:19}{}: {:11.4f}'.format('Total Solv ', chids[1], totalSolvMon[chids[1]]))
print('{:20}: {:11.4f}'.format('DGintAB-A-B', total))

print("Ala Scanning: DDGs for X->Ala mutations on interface residues")
for ch in st[0]:
    for res in ch.get_residues():
        if args.cutoff_dist > 0 and res not in interface[ch.id]:
            continue
        print(
            '{:11} {:11.4f}{:11.4f}{:11.4f}{:11.4f}{:11.4f}'.format(
                en.residue_id(res),
                - elec[res] + elec_ala[res],
                - vdw[res] + vdw_ala[res],
                - solvAB[res] + solvAB_ala[res],
                - solvA[res] + solvA_ala[res],
                - elec[res] + elec_ala[res] - vdw[res] + vdw_ala[res] -solvAB[res] +\
                    solvAB_ala[res] -solvA[res] + solvA_ala[res]
            )
        )