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

"""The script for setting up sqlfluff."""

from __future__ import absolute_import
from __future__ import print_function

import io
import configparser
from os.path import dirname
from os.path import join

from setuptools import setup

# Get the global config info as currently stated
# (we use the config file to avoid actually loading any python here)
config = configparser.ConfigParser()
config.read(['src/sqlfluff/config.ini'])
version = config.get('sqlfluff', 'version')

def read(*names, **kwargs):
    """Read a file and return the contents as a string."""
    return io.open(
        join(dirname(__file__), *names),
        encoding=kwargs.get('encoding', 'utf8')
    ).read()

setup(
    name='sqlfluff',
    version=version,
    license='MIT License',
    description='Modular SQL Linting for Humans',
    long_description=read('README.md') + "\n\n---\n\n" + read('CHANGELOG.md'),
    # Make sure pypi is expecting markdown!
    long_description_content_type='text/markdown',
    author='Alan Cruickshank',
    author_email='alan@designingoverload.com',
    url='https://github.com/alanmcruickshank/sqlfluff',
    python_requires=">=3.5",
    keywords=['sqlfluff', 'sql', 'linter'],
    project_urls={
        # Homepage not ready yet.
        # 'Homepage': 'https://www.sqlfluff.com'
        'Documentation': 'https://docs.sqlfluff.com',
        'Source': 'https://github.com/alanmcruickshank/sqlfluff',
    },
    # Specify all the seperate packages, modules come automatically
    packages=['sqlfluff', 'sqlfluff.cli', 'sqlfluff.dialects', 'sqlfluff.parser', 'sqlfluff.rules'],
    package_dir={"": "src"},
    include_package_data=True,
    classifiers=[
        # complete classifier list: http://pypi.python.org/pypi?%3Aaction=list_classifiers
        'Development Status :: 3 - Alpha',
        # 'Development Status :: 5 - Production/Stable',
        'Environment :: Console',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Operating System :: Unix',
        'Operating System :: POSIX',
        'Operating System :: Microsoft :: Windows',
        'Programming Language :: Python',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
        'Programming Language :: Python :: 3.8',
        'Programming Language :: Python :: 3.9',
        'Programming Language :: Python :: Implementation :: CPython',
        'Programming Language :: Python :: Implementation :: PyPy',
        'Topic :: Utilities',
        'Topic :: Software Development :: Quality Assurance'
    ],
    install_requires=[
        # Core
        'click>=2.0',
        "colorama>=0.3",
        'configparser',
        'oyaml',
        'Jinja2',
        # Used for diffcover plugin
        "diff-cover>=2.5.0,<3.0",
        # Used for performance profiling
        'bench-it',
        # Used for .sqlfluffignore
        "pathspec"
    ],
    extras_require={
        # eg:
        #   'rst': ['docutils>=0.11'],
        #   ':python_version=="2.6"': ['argparse'],
    },
    entry_points={
        'console_scripts': [
            'sqlfluff = sqlfluff.cli.commands:cli',
        ],
        'diff_cover': [
            'sqlfluff = sqlfluff.diff_quality_plugin'
        ],
    },
)


import nltk
from nltk.corpus import treebank
from itertools import islice
from nltk.grammar import PCFG, induce_pcfg, toy_pcfg1, toy_pcfg2

# PCFG中的规则
gram2 = PCFG.fromstring("""
	A -> B B [.3] | C B C [.7]
	B -> B D [.5] | C [.5]
	C -> 'a' [.1] | 'b' [0.9]
	D -> 'b' [1.0]
	""")

prod1 = gram2.productions()[0]

print(prod1)  # A -> B B [0.3]

prod2 = gram2.productions()[1]

print(prod2)  # A -> C B C [0.7]

print(prod2.lhs())  # A

print(prod2.rhs())  # (C, B, C)

print((prod2.prob()))  # 0.7

print(gram2.start())  # A

print(gram2.productions())

# >>>[A -> B B [0.3], A -> C B C [0.7], B -> B D [0.5], B -> C [0.5], C -> 'a' [0.1], C -> 'b' [0.9], D -> 'b' [1.0]]

# NLTK中用于说明概率分布图的解析代码
tokens = "Jack told Bob to bring my cookie".split()

grammar = toy_pcfg2

print(grammar)

'''
    Grammar with 23 productions (start state = S)
    S -> NP VP [1.0]
    VP -> V NP [0.59]
    VP -> V [0.4]
    VP -> VP PP [0.01]
    NP -> Det N [0.41]
    NP -> Name [0.28]
    NP -> NP PP [0.31]
    PP -> P NP [1.0]
    V -> 'saw' [0.21]
    V -> 'ate' [0.51]
    V -> 'ran' [0.28]
    N -> 'boy' [0.11]
    N -> 'cookie' [0.12]
    N -> 'table' [0.13]
    N -> 'telescope' [0.14]
    N -> 'hill' [0.5]
    Name -> 'Jack' [0.52]
    Name -> 'Bob' [0.48]
    P -> 'with' [0.61]
    P -> 'under' [0.39]
    Det -> 'the' [0.41]
    Det -> 'a' [0.31]
    Det -> 'my' [0.28]
'''

5.语法解析-分析材料/5.3 CFG创建概率上下文语法.py

5.3 CFG创建概率上下文语法.py


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from typing import Union

from neodroidagent.agents import PolicyGradientAgent
from neodroidagent.common import ParallelSession
from neodroidagent.common.architectures.distributional.categorical import CategoricalMLP
from neodroidagent.configs.test_reference.base_dicrete_test_config import *

__author__ = "Christian Heider Nielsen"

from neodroidagent.entry_points.session_factory import session_factory
from warg import GDKC

CONFIG_NAME = __name__

CONFIG_FILE_PATH = pathlib.Path(__file__)

RENDER_ENVIRONMENT = True

OPTIMISER_SPEC = GDKC(torch.optim.Adam, lr=3e-4)
SCHEDULER_SPEC = None

POLICY_ARCH_SPEC = GDKC(constructor=CategoricalMLP, hidden_layers=128)

# RENDER_FREQUENCY = 1

pg_config = globals()

def pg_run(
    skip_confirmation: bool = True,
    environment_type: Union[bool, str] = True,
    *,
    config=None,**kwargs

) -> None:
    if config is None:
        config = pg_config

    session_factory(
        PolicyGradientAgent,
        config,
        session=ParallelSession,
        skip_confirmation=skip_confirmation,
        environment=environment_type,**kwargs
    )

def pg_test(config=None,**kwargs) -> None:
    if config is None:
        config = pg_config
    pg_run(environment_type="gym", config=config,**kwargs)

if __name__ == "__main__":
    pg_test()


#
# This file is part of pyasn1-alt-modules software.
#
# Created by Russ Housley with assistance from asn1ate v.0.6.0.
#
# Copyright (c) 2021, Vigil Security, LLC
# License: http://vigilsec.com/pyasn1-alt-modules-license.txt
#
# TimeStampedData
#
# ASN.1 source from:
# https://www.rfc-editor.org/rfc/rfc5544.txt
#

from pyasn1.type import char
from pyasn1.type import constraint
from pyasn1.type import namedtype
from pyasn1.type import namedval
from pyasn1.type import opentype
from pyasn1.type import tag
from pyasn1.type import univ

from pyasn1_alt_modules import rfc3161
from pyasn1_alt_modules import rfc4998
from pyasn1_alt_modules import rfc5280
from pyasn1_alt_modules import rfc5652

MAX = float('inf')

otherEvidenceMap = { }

# Imports from RFC 5652

Attribute = rfc5652.Attribute

# Imports from RFC 5280

CertificateList = rfc5280.CertificateList

# Imports from RFC 3161

TimeStampToken = rfc3161.TimeStampToken

# Imports from RFC 4998

EvidenceRecord = rfc4998.EvidenceRecord

# TimeStampedData

class Attributes(univ.SetOf):
    componentType = Attribute()
    subtypeSpec = constraint.ValueSizeConstraint(1, MAX)

class TimeStampAndCRL(univ.Sequence):
    componentType = namedtype.NamedTypes(
        namedtype.NamedType('timeStamp', TimeStampToken()),
        namedtype.OptionalNamedType('crl', CertificateList())
    )

class TimeStampTokenEvidence(univ.SequenceOf):
    componentType = TimeStampAndCRL()
    subtypeSpec = constraint.ValueSizeConstraint(1, MAX)

class OtherEvidence(univ.Sequence):
    componentType = namedtype.NamedTypes(
        namedtype.NamedType('oeType', univ.ObjectIdentifier()),
        namedtype.NamedType('oeValue', univ.Any(),
            openType=opentype.OpenType('oeType', otherEvidenceMap))
    )

class Evidence(univ.Choice):
    componentType = namedtype.NamedTypes(
        namedtype.NamedType('tstEvidence',
            TimeStampTokenEvidence().subtype(implicitTag=tag.Tag(
                tag.tagClassContext, tag.tagFormatSimple, 0))),
        namedtype.NamedType('ersEvidence',
            EvidenceRecord().subtype(implicitTag=tag.Tag(
                tag.tagClassContext, tag.tagFormatSimple, 1))),
        namedtype.NamedType('otherEvidence',
            OtherEvidence().subtype(implicitTag=tag.Tag(
                tag.tagClassContext, tag.tagFormatConstructed, 2)))
    )

class MetaData(univ.Sequence):
    componentType = namedtype.NamedTypes(
        namedtype.NamedType('hashProtected', univ.Boolean()),
        namedtype.OptionalNamedType('fileName', char.UTF8String()),
        namedtype.OptionalNamedType('mediaType', char.IA5String()),
        namedtype.OptionalNamedType('otherMetaData', Attributes())
    )

class TimeStampedData(univ.Sequence):
    componentType = namedtype.NamedTypes(
        namedtype.NamedType('version',
            univ.Integer(namedValues=namedval.NamedValues(('v1', 1)))),
        namedtype.OptionalNamedType('dataUri', char.IA5String()),
        namedtype.OptionalNamedType('metaData', MetaData()),
        namedtype.OptionalNamedType('content', univ.OctetString()),
        namedtype.NamedType('temporalEvidence', Evidence())
    )

id_ct_timestampedData = univ.ObjectIdentifier('1.2.840.113549.1.9.16.1.31')

# Update the CMS Content Type Map in rfc5652.py

_cmsContentTypesMapUpdate = {
    id_ct_timestampedData: TimeStampedData(),
}

rfc5652.cmsContentTypesMap.update(_cmsContentTypesMapUpdate)


import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

a=int(input())
Ka=int(input())
Ki=int(input())
Ko=int(input())
for i in range(1,a,1):
    t=(float(i)/12)
    d=((np.log(50.0 / Ka) + (0.1 + (0.3 ** 2 / 2)) * (float(i) / 12)) / (0.3 * (float(i) / 12)**0.5))
    DeltaA= norm.cdf(d)
    di=((np.log(50.0 / Ki) + (0.1 + (0.3 ** 2 / 2)) * (float(i) / 12)) / (0.3 * (float(i) / 12)**0.5))
    DeltaI= norm.cdf(di)
    do=((np.log(50.0 / Ko) + (0.1 + (0.3 ** 2 / 2)) * (float(i) / 12)) / (0.3 * (float(i) / 12)**0.5))
    DeltaO= norm.cdf(do)
    x='3'
    plt.plot(0,0,x)
    plt.plot(0,0.5,x)
    plt.plot(0,1,x)
    plt.plot([t],[DeltaA],x)
    plt.plot([t],[DeltaI],x)
    plt.plot([t],[DeltaO],x)

plt.axis([0, 5, 0, 1])
plt.title('Graph of Delta Against Time to Maturity')
plt.ylabel('Delta')
plt.xlabel('Time to Maturity')
plt.ylabel('Delta')
plt.show()


# -*- coding: utf-8 -*-
#
# Copyright © 2009-2010 Pierre Raybaut
# Licensed under the terms of the MIT License
# (see spyderlib/__init__.py for details)

"""External Console plugin"""

# pylint: disable=C0103
# pylint: disable=R0903
# pylint: disable=R0911
# pylint: disable=R0201

from spyderlib.qt.QtGui import (QVBoxLayout, QFileDialog, QMessageBox,
                                QInputDialog, QLineEdit, QPushButton,
                                QGroupBox, QLabel, QTabWidget, QFontComboBox)
from spyderlib.qt.QtCore import SIGNAL, QString, Qt

import sys, os
import os.path as osp

# For debugging purpose:
STDOUT = sys.stdout

# Local imports
from spyderlib.config import get_icon, CONF, _
from spyderlib.utils import (programs, remove_trailing_single_backslash,
                             get_error_match)
from spyderlib.utils.qthelpers import create_action, mimedata2url
from spyderlib.widgets.tabs import Tabs
from spyderlib.widgets.externalshell.pythonshell import ExternalPythonShell
from spyderlib.widgets.externalshell.systemshell import ExternalSystemShell
from spyderlib.widgets.findreplace import FindReplace
from spyderlib.plugins import SpyderPluginWidget, PluginConfigPage

class ExternalConsoleConfigPage(PluginConfigPage):
    def setup_page(self):
        interface_group = QGroupBox(_("Interface"))
        font_group = self.create_fontgroup(option=None, text=None,
                                    fontfilters=QFontComboBox.MonospacedFonts)
        newcb = self.create_checkbox
        singletab_box = newcb(_("One tab per script"), 'single_tab')
        showtime_box = newcb(_("Show elapsed time"), 'show_elapsed_time')
        icontext_box = newcb(_("Show icons and text"), 'show_icontext')

        # Interface Group
        interface_layout = QVBoxLayout()
        interface_layout.addWidget(singletab_box)
        interface_layout.addWidget(showtime_box)
        interface_layout.addWidget(icontext_box)
        interface_group.setLayout(interface_layout)

        # Source Code Group
        display_group = QGroupBox(_("Source code"))
        buffer_spin = self.create_spinbox(
                            _("Buffer: "), _(" lines"),
                            'max_line_count', min_=0, max_=1000000, step=100,
                            tip=_("Set maximum line count"))
        wrap_mode_box = newcb(_("Wrap lines"), 'wrap')

        display_layout = QVBoxLayout()
        display_layout.addWidget(buffer_spin)
        display_layout.addWidget(wrap_mode_box)
        display_group.setLayout(display_layout)

        # Background Color Group
        bg_group = QGroupBox(_("Background color"))
        bg_label = QLabel(_("This option will be applied the next time "
                                  "a Python console or a terminal is opened."))
        bg_label.setWordWrap(True)
        lightbg_box = newcb(_("Light background (white color)"),
                            'light_background')
        ipybg_box = newcb(_("Set the appropriate IPython color option"),
                          'ipython_set_color')
        ipybg_box.setEnabled(programs.is_module_installed("IPython"))
        bg_layout = QVBoxLayout()
        bg_layout.addWidget(bg_label)
        bg_layout.addWidget(lightbg_box)
        bg_layout.addWidget(ipybg_box)
        bg_group.setLayout(bg_layout)

        # Advanced settings
        source_group = QGroupBox(_("Source code"))
        completion_box = newcb(_("Automatic code completion"),
                               'codecompletion/auto')
        case_comp_box = newcb(_("Case sensitive code completion"),
                              'codecompletion/case_sensitive')
        show_single_box = newcb(_("Show single completion"),
                               'codecompletion/show_single')
        comp_enter_box = newcb(_("Enter key selects completion"),
                               'codecompletion/enter_key')
        calltips_box = newcb(_("Balloon tips"), 'calltips')
        inspector_box = newcb(
              _("Automatic notification to object inspector"),
              'object_inspector', default=True,
              tip=_("If this option is enabled, object inspector\n"
                      "will automatically show informations on functions\n"
                      "entered in console (this is triggered when entering\n"
                      "a left parenthesis after a valid function name)"))

        source_layout = QVBoxLayout()
        source_layout.addWidget(completion_box)
        source_layout.addWidget(case_comp_box)
        source_layout.addWidget(show_single_box)
        source_layout.addWidget(comp_enter_box)
        source_layout.addWidget(calltips_box)
        source_layout.addWidget(inspector_box)
        source_group.setLayout(source_layout)

        # UMD Group
        umd_group = QGroupBox(_("User Module Deleter (UMD)"))
        umd_label = QLabel(_("UMD forces Python to reload modules "
                               "imported when executing a script in the "
                               "external console with the 'runfile' function"))
        umd_label.setWordWrap(True)
        umd_enabled_box = newcb(_("Enable UMD"), 'umd/enabled',
                                msg_if_enabled=True, msg_warning=_(
                        "This option will enable the User Module Deleter (UMD) "
                        "in Python/IPython interpreters. UMD forces Python to "
                        "reload deeply modules during import when running a "
                        "Python script using the Spyder's builtin function "
                        "runfile."
                        "

1. UMD may require to restart the "
                        "Python interpreter in which it will be called "
                        "(otherwise only newly imported modules will be "
                        "reloaded when executing scripts)."
                        "

2. If errors occur when re-running a "
                        "PyQt-based program, please check that the Qt objects "
                        "are properly destroyed (e.g. you may have to use the "
                        "attribute Qt.WA_DeleteOnClose on your main "
                        "window, using the setAttribute method)"),
                                )
        umd_verbose_box = newcb(_("Show reloaded modules list"),
                                'umd/verbose', msg_info=_(
                                        "Please note that these changes will "
                                        "be applied only to new Python/IPython "
                                        "interpreters"),
                                )
        umd_namelist_btn = QPushButton(
                            _("Set UMD excluded (not reloaded) modules"))
        self.connect(umd_namelist_btn, SIGNAL('clicked()'),
                     self.plugin.set_umd_namelist)

        umd_layout = QVBoxLayout()
        umd_layout.addWidget(umd_label)
        umd_layout.addWidget(umd_enabled_box)
        umd_layout.addWidget(umd_verbose_box)
        umd_layout.addWidget(umd_namelist_btn)
        umd_group.setLayout(umd_layout)

        # Startup Group
        startup_group = QGroupBox(_("Startup"))
        pystartup_box = newcb(_("Open a Python interpreter at startup"),
                              'open_python_at_startup')
        ipystartup_box = newcb(_("Open an IPython interpreter at startup"),
                               'open_ipython_at_startup')
        ipystartup_box.setEnabled(programs.is_module_installed('IPython'))

        startup_layout = QVBoxLayout()
        startup_layout.addWidget(pystartup_box)
        startup_layout.addWidget(ipystartup_box)
        startup_group.setLayout(startup_layout)

        # Monitor Group
        monitor_group = QGroupBox(_("Monitor"))
        monitor_label = QLabel(_("The monitor provides introspection "
                                       "features to console: code completion, "
                                       "calltips and variable explorer. "
                                       "Because it relies on several modules, "
                                       "disabling the monitor may be useful "
                                       "to accelerate console startup."))
        monitor_label.setWordWrap(True)
        monitor_box = newcb(_("Enable monitor"), 'monitor/enabled')
        for obj in (completion_box, case_comp_box, show_single_box,
                    comp_enter_box, calltips_box):
            self.connect(monitor_box, SIGNAL("toggled(bool)"), obj.setEnabled)
            obj.setEnabled(self.get_option('monitor/enabled'))

        monitor_layout = QVBoxLayout()
        monitor_layout.addWidget(monitor_label)
        monitor_layout.addWidget(monitor_box)
        monitor_group.setLayout(monitor_layout)

        # PyQt Group
        pyqt_group = QGroupBox(_("PyQt"))
        pyqt_label = QLabel(_("PyQt installs an input hook that "
                        "allows creating and interacting with widgets in an "
                        "interactive interpreter without blocking it. It is "
                        "strongly recommended to remove it on Windows "
                        "platforms (it has no effect in IPython)."))
        pyqt_label.setWordWrap(True)
        pyqt_hook_box = newcb(_("Remove PyQt input hook"),
                              'remove_pyqt_inputhook')
        pyqt_setapi_box = newcb(_("Ignore API change errors "
                                        "(sip.setapi)"),
                                'ignore_sip_setapi_errors')
        try:
            from sip import setapi
        except ImportError:
            pyqt_setapi_box.setDisabled(True)

        pyqt_layout = QVBoxLayout()
        pyqt_layout.addWidget(pyqt_label)
        pyqt_layout.addWidget(pyqt_hook_box)
        pyqt_layout.addWidget(pyqt_setapi_box)
        pyqt_group.setLayout(pyqt_layout)

        # IPython Group
        ipython_group = QGroupBox(_("IPython"))
        ipython_edit = self.create_lineedit(_(
                            "IPython interpreter command line options:\n"
                            "(Qt4 and matplotlib support: -q4thread -pylab)"),
                            'ipython_options', alignment=Qt.Vertical)

        ipython_layout = QVBoxLayout()
        ipython_layout.addWidget(ipython_edit)
        ipython_group.setLayout(ipython_layout)
        ipython_group.setEnabled(programs.is_module_installed("IPython"))

        # Matplotlib Group
        mpl_group = QGroupBox(_("Matplotlib"))
        mpl_label = QLabel(_("Patching Matplotlib library will add a "
                             "button to customize figure options "
                             "(curves/images plot parameters)."))
        mpl_label.setWordWrap(True)
        mpl_patch_box = newcb(_("Patch Matplotlib figures"),
                              'mpl_patch/enabled')
        mpl_backend_edit = self.create_lineedit(_("Matplotlib backend "
                                                        "(default: Qt4Agg):"),
                                                'mpl_patch/backend', "Qt4Agg",
                                                _("Set the GUI toolkit "
                                                        "used by Matplotlib to "
                                                        "show figures"),
                                                alignment=Qt.Vertical)
        self.connect(mpl_patch_box, SIGNAL("toggled(bool)"),
                     mpl_backend_edit.setEnabled)
        mpl_backend_edit.setEnabled(self.get_option('mpl_patch/enabled'))

        mpl_layout = QVBoxLayout()
        mpl_layout.addWidget(mpl_label)
        mpl_layout.addWidget(mpl_patch_box)
        mpl_layout.addWidget(mpl_backend_edit)
        mpl_group.setLayout(mpl_layout)
        mpl_group.setEnabled(programs.is_module_installed('matplotlib'))

        # ETS Group
        ets_group = QGroupBox(_("Enthought Tool Suite"))
        ets_label = QLabel(_("Enthought Tool Suite (ETS) supports "
                                   "PyQt4 (qt4) and wxPython (wx) graphical "
                                   "user interfaces."))
        ets_label.setWordWrap(True)
        ets_edit = self.create_lineedit(_("ETS_TOOLKIT "
                                                "(default value: qt4):"),
                                        'ets_backend', default='qt4',
                                        alignment=Qt.Vertical)

        ets_layout = QVBoxLayout()
        ets_layout.addWidget(ets_label)
        ets_layout.addWidget(ets_edit)
        ets_group.setLayout(ets_layout)
        ets_group.setEnabled(programs.is_module_installed(
                                                    "enthought.etsconfig.api"))

        tabs = QTabWidget()
        tabs.addTab(self.create_tab(font_group, interface_group, display_group,
                                    bg_group),
                    _("Display"))
        tabs.addTab(self.create_tab(monitor_group, source_group),
                    _("Introspection"))
        tabs.addTab(self.create_tab(startup_group, umd_group),
                    _("Advanced settings"))
        tabs.addTab(self.create_tab(pyqt_group, ipython_group, mpl_group,
                                    ets_group),
                    _("External modules"))

        vlayout = QVBoxLayout()
        vlayout.addWidget(tabs)
        self.setLayout(vlayout)

class ExternalConsole(SpyderPluginWidget):
    """
    Console widget
    """
    CONF_SECTION = 'console'
    CONFIGWIDGET_CLASS = ExternalConsoleConfigPage
    def __init__(self, parent, light_mode):
        self.light_mode = light_mode
        self.commands = []
        self.tabwidget = None
        self.menu_actions = None

        self.inspector = None # Object inspector plugin
        self.historylog = None # History log plugin
        self.variableexplorer = None # Variable explorer plugin

        self.ipython_count = 0
        self.python_count = 0
        self.terminal_count = 0

        try:
            from sip import setapi
        except ImportError:
            self.set_option('ignore_sip_setapi_errors', False)

        python_startup = self.get_option('open_python_at_startup', None)
        ipython_startup = self.get_option('open_ipython_at_startup', None)
        if ipython_startup is None:
            ipython_startup = programs.is_module_installed("IPython")
            self.set_option('open_ipython_at_startup', ipython_startup)
        if python_startup is None:
            python_startup = not ipython_startup
            self.set_option('open_python_at_startup', python_startup)

        if self.get_option('ipython_options', None) is None:
            self.set_option('ipython_options',
                            self.get_default_ipython_options())

        self.shellwidgets = []
        self.filenames = []
        self.icons = []
        self.runfile_args = ""

        SpyderPluginWidget.__init__(self, parent)

        layout = QVBoxLayout()
        self.tabwidget = Tabs(self, self.menu_actions)
        if hasattr(self.tabwidget, 'setDocumentMode'):
            self.tabwidget.setDocumentMode(True)
        self.connect(self.tabwidget, SIGNAL('currentChanged(int)'),
                     self.refresh_plugin)
        self.connect(self.tabwidget, SIGNAL('move_data(int,int)'),
                     self.move_tab)

        self.tabwidget.set_close_function(self.close_console)

        layout.addWidget(self.tabwidget)

        # Find/replace widget
        self.find_widget = FindReplace(self)
        self.find_widget.hide()
        self.register_widget_shortcuts("Editor", self.find_widget)

        layout.addWidget(self.find_widget)

        self.setLayout(layout)

        # Accepting drops
        self.setAcceptDrops(True)

    def move_tab(self, index_from, index_to):
        """
        Move tab (tabs themselves have already been moved by the tabwidget)
        """
        filename = self.filenames.pop(index_from)
        shell = self.shellwidgets.pop(index_from)
        icons = self.icons.pop(index_from)

        self.filenames.insert(index_to, filename)
        self.shellwidgets.insert(index_to, shell)
        self.icons.insert(index_to, icons)

    def close_console(self, index=None):
        if not self.tabwidget.count():
            return
        if index is None:
            index = self.tabwidget.currentIndex()
        self.tabwidget.widget(index).close()
        self.tabwidget.removeTab(index)
        self.filenames.pop(index)
        self.shellwidgets.pop(index)
        self.icons.pop(index)

    def set_variableexplorer(self, variableexplorer):
        """Set variable explorer plugin"""
        self.variableexplorer = variableexplorer

    def __find_python_shell(self, interpreter_only=False):
        current_index = self.tabwidget.currentIndex()
        if current_index == -1:
            return
        from spyderlib.widgets.externalshell import pythonshell
        for index in [current_index]+range(self.tabwidget.count()):
            shellwidget = self.tabwidget.widget(index)
            if isinstance(shellwidget, pythonshell.ExternalPythonShell):
                if not interpreter_only or shellwidget.is_interpreter():
                    self.tabwidget.setCurrentIndex(index)
                    return shellwidget

    def get_running_python_shell(self):
        """
        Called by object inspector to retrieve a running Python shell instance
        """
        current_index = self.tabwidget.currentIndex()
        if current_index == -1:
            return
        from spyderlib.widgets.externalshell import pythonshell
        shellwidgets = [self.tabwidget.widget(index)
                        for index in range(self.tabwidget.count())]
        shellwidgets = [_w for _w in shellwidgets
                        if isinstance(_w, pythonshell.ExternalPythonShell) \
                        and _w.is_running()]
        if shellwidgets:
            # First, iterate on interpreters only:
            for shellwidget in shellwidgets:
                if shellwidget.is_interpreter():
                    return shellwidget.shell
            else:
                return shellwidgets[0].shell

    def run_script_in_current_shell(self, filename, wdir, args, debug):
        """Run script in current shell, if any"""
        shellwidget = self.__find_python_shell(interpreter_only=True)
        if shellwidget is not None and shellwidget.is_running():
            line = "%s(r'%s'" % ('debugfile' if debug else 'runfile',
                                 unicode(filename))
            norm = lambda text: remove_trailing_single_backslash(unicode(text))
            if args:
                line += ", args=r'%s'" % norm(args)
            if wdir:
                line += ", wdir=r'%s'" % norm(wdir)
            line += ")"
            shellwidget.shell.execute_lines(line)
            shellwidget.shell.setFocus()

    def set_current_shell_working_directory(self, directory):
        """Set current shell working directory"""
        shellwidget = self.__find_python_shell()
        if shellwidget is not None and shellwidget.is_running():
            shellwidget.shell.set_cwd(unicode(directory))

    def execute_python_code(self, lines):
        """Execute Python code in an already opened Python interpreter"""
        shellwidget = self.__find_python_shell()
        if shellwidget is not None:
            shellwidget.shell.execute_lines(unicode(lines))
            shellwidget.shell.setFocus()

    def pdb_has_stopped(self, fname, lineno, shell):
        """Python debugger has just stopped at frame (fname, lineno)"""
        self.emit(SIGNAL("edit_goto(QString,int,QString)"),
                  fname, lineno, '')
        shell.setFocus()

    def start(self, fname, wdir=None, args='', interact=False, debug=False,
              python=True, ipython=False, python_args=''):
        """
        Start new console

        fname:
          string: filename of script to run
          None: open an interpreter
        wdir: working directory
        args: command line options of the Python script
        interact: inspect script interactively after its execution
        debug: run pdb
        python: True: Python interpreter, False: terminal
        ipython: True: IPython interpreter, False: Python interpreter
        python_args: additionnal Python interpreter command line options
                   (option "-u" is mandatory, see widgets.externalshell package)
        """
        # Note: fname is None <=> Python interpreter
        fname = unicode(fname) if isinstance(fname, QString) else fname
        wdir = unicode(wdir) if isinstance(wdir, QString) else wdir

        if fname is not None and fname in self.filenames:
            index = self.filenames.index(fname)
            if self.get_option('single_tab'):
                old_shell = self.shellwidgets[index]
                if old_shell.is_running():
                    answer = QMessageBox.question(self, self.get_plugin_title(),
                        _("%s is already running in a separate process.\n"
                          "Do you want to kill the process before starting "
                          "a new one?") % osp.basename(fname),
                        QMessageBox.Yes | QMessageBox.Cancel)
                    if answer == QMessageBox.Yes:
                        old_shell.process.kill()
                        old_shell.process.waitForFinished()
                    else:
                        return
                self.close_console(index)
        else:
            index = self.tabwidget.count()

        # Creating a new external shell
        pythonpath = self.main.get_spyder_pythonpath()
        light_background = self.get_option('light_background')
        show_elapsed_time = self.get_option('show_elapsed_time')
        if python:
            monitor_enabled = self.get_option('monitor/enabled')
            mpl_patch_enabled = self.get_option('mpl_patch/enabled')
            mpl_backend = self.get_option('mpl_patch/backend')
            ets_backend = self.get_option('ets_backend', 'qt4')
            remove_pyqt_inputhook = self.get_option('remove_pyqt_inputhook',
                                                    os.name == 'nt')
            ignore_sip_setapi_errors = self.get_option(
                                           'ignore_sip_setapi_errors', True)
            umd_enabled = self.get_option('umd/enabled')
            umd_namelist = self.get_option('umd/namelist')
            umd_verbose = self.get_option('umd/verbose')
            ar_timeout = CONF.get('variable_explorer', 'autorefresh/timeout')
            ar_state = CONF.get('variable_explorer', 'autorefresh')
            if self.light_mode:
                from spyderlib.plugins.variableexplorer import VariableExplorer
                sa_settings = VariableExplorer.get_settings()
            else:
                sa_settings = None
            shellwidget = ExternalPythonShell(self, fname, wdir, self.commands,
                           interact, debug, path=pythonpath,
                           python_args=python_args, ipython=ipython,
                           arguments=args, stand_alone=sa_settings,
                           umd_enabled=umd_enabled, umd_namelist=umd_namelist,
                           umd_verbose=umd_verbose, ets_backend=ets_backend,
                           monitor_enabled=monitor_enabled,
                           mpl_patch_enabled=mpl_patch_enabled,
                           mpl_backend=mpl_backend,
                           remove_pyqt_inputhook=remove_pyqt_inputhook,
                           ignore_sip_setapi_errors=ignore_sip_setapi_errors,
                           autorefresh_timeout=ar_timeout,
                           autorefresh_state=ar_state,
                           light_background=light_background,
                           menu_actions=self.menu_actions,
                           show_buttons_inside=False,
                           show_elapsed_time=show_elapsed_time)
            self.connect(shellwidget, SIGNAL('pdb(QString,int)'),
                         lambda fname, lineno, shell=shellwidget.shell:
                         self.pdb_has_stopped(fname, lineno, shell))
            self.register_widget_shortcuts("Console", shellwidget.shell)
        else:
            if os.name == 'posix':
                cmd = 'gnome-terminal'
                args = []
                if programs.is_program_installed(cmd):
                    if wdir:
                        args.extend(['--working-directory=%s' % wdir])
                    programs.run_program(cmd, args)
                    return
                cmd = 'konsole'
                if programs.is_program_installed(cmd):
                    if wdir:
                        args.extend(['--workdir', wdir])
                    programs.run_program(cmd, args)
                    return
            shellwidget = ExternalSystemShell(self, wdir, path=pythonpath,
                                          light_background=light_background,
                                          menu_actions=self.menu_actions,
                                          show_buttons_inside=False,
                                          show_elapsed_time=show_elapsed_time)

        # Code completion / calltips
        shellwidget.shell.setMaximumBlockCount(
                                            self.get_option('max_line_count') )
        shellwidget.shell.set_font( self.get_plugin_font() )
        shellwidget.shell.toggle_wrap_mode( self.get_option('wrap') )
        shellwidget.shell.set_calltips( self.get_option('calltips') )
        shellwidget.shell.set_codecompletion_auto(
                            self.get_option('codecompletion/auto') )
        shellwidget.shell.set_codecompletion_case(
                            self.get_option('codecompletion/case_sensitive') )
        shellwidget.shell.set_codecompletion_single(
                            self.get_option('codecompletion/show_single') )
        shellwidget.shell.set_codecompletion_enter(
                            self.get_option('codecompletion/enter_key') )
        if python and self.inspector is not None:
            shellwidget.shell.set_inspector(self.inspector)
            shellwidget.shell.set_inspector_enabled(
                                            self.get_option('object_inspector'))
        if self.historylog is not None:
            self.historylog.add_history(shellwidget.shell.history_filename)
            self.connect(shellwidget.shell,
                         SIGNAL('append_to_history(QString,QString)'),
                         self.historylog.append_to_history)
        self.connect(shellwidget.shell, SIGNAL("go_to_error(QString)"),
                     self.go_to_error)
        self.connect(shellwidget.shell, SIGNAL("focus_changed()"),
                     lambda: self.emit(SIGNAL("focus_changed()")))
        if python:
            if fname is None:
                if ipython:
                    self.ipython_count += 1
                    tab_name = "IPython %d" % self.ipython_count
                    tab_icon1 = get_icon('ipython.png')
                    tab_icon2 = get_icon('ipython_t.png')
                else:
                    self.python_count += 1
                    tab_name = "Python %d" % self.python_count
                    tab_icon1 = get_icon('python.png')
                    tab_icon2 = get_icon('python_t.png')
            else:
                tab_name = osp.basename(fname)
                tab_icon1 = get_icon('run.png')
                tab_icon2 = get_icon('terminated.png')
        else:
            fname = id(shellwidget)
            if os.name == 'nt':
                tab_name = _("Command Window")
            else:
                tab_name = _("Terminal")
            self.terminal_count += 1
            tab_name += (" %d" % self.terminal_count)
            tab_icon1 = get_icon('cmdprompt.png')
            tab_icon2 = get_icon('cmdprompt_t.png')
        self.shellwidgets.insert(index, shellwidget)
        self.filenames.insert(index, fname)
        self.icons.insert(index, (tab_icon1, tab_icon2))
        if index is None:
            index = self.tabwidget.addTab(shellwidget, tab_name)
        else:
            self.tabwidget.insertTab(index, shellwidget, tab_name)

        self.connect(shellwidget, SIGNAL("started()"),
                     lambda sid=id(shellwidget): self.process_started(sid))
        self.connect(shellwidget, SIGNAL("finished()"),
                     lambda sid=id(shellwidget): self.process_finished(sid))
        self.find_widget.set_editor(shellwidget.shell)
        self.tabwidget.setTabToolTip(index, fname if wdir is None else wdir)
        self.tabwidget.setCurrentIndex(index)
        if self.dockwidget and not self.ismaximized:
            self.dockwidget.setVisible(True)
            self.dockwidget.raise_()

        shellwidget.set_icontext_visible(self.get_option('show_icontext'))

        # Start process and give focus to console
        shellwidget.start_shell()
        shellwidget.shell.setFocus()

    #------ Private API --------------------------------------------------------
    def process_started(self, shell_id):
        for index, shell in enumerate(self.shellwidgets):
            if id(shell) == shell_id:
                icon, _icon = self.icons[index]
                self.tabwidget.setTabIcon(index, icon)
                if self.inspector is not None:
                    self.inspector.set_shell(shell.shell)
                if self.variableexplorer is not None:
                    self.variableexplorer.add_shellwidget(shell)

    def process_finished(self, shell_id):
        for index, shell in enumerate(self.shellwidgets):
            if id(shell) == shell_id:
                _icon, icon = self.icons[index]
                self.tabwidget.setTabIcon(index, icon)
                if self.inspector is not None:
                    self.inspector.shell_terminated(shell.shell)
        if self.variableexplorer is not None:
            self.variableexplorer.remove_shellwidget(shell_id)

    #------ SpyderPluginWidget API ---------------------------------------------
    def get_plugin_title(self):
        """Return widget title"""
        return _('Console')

    def get_plugin_icon(self):
        """Return widget icon"""
        return get_icon('console.png')

    def get_focus_widget(self):
        """
        Return the widget to give focus to when
        this plugin's dockwidget is raised on top-level
        """
        return self.tabwidget.currentWidget()

    def get_plugin_actions(self):
        """Return a list of actions related to plugin"""
        interpreter_action = create_action(self,
                            _("Open &interpreter"), None,
                            'python.png', _("Open a Python interpreter"),
                            triggered=self.open_interpreter)
        if os.name == 'nt':
            text = _("Open &command prompt")
            tip = _("Open a Windows command prompt")
        else:
            text = _("Open &terminal")
            tip = _("Open a terminal window inside Spyder")
        terminal_action = create_action(self, text, None, 'cmdprompt.png', tip,
                                        triggered=self.open_terminal)
        run_action = create_action(self,
                            _("&Run..."), None,
                            'run_small.png', _("Run a Python script"),
                            triggered=self.run_script)

        run_menu_actions = [interpreter_action]
        tools_menu_actions = [terminal_action]
        self.menu_actions = [interpreter_action, terminal_action, run_action]

        ipython_action = create_action(self,
                            _("Open IPython interpreter"), None,
                            'ipython.png',
                            _("Open an IPython interpreter"),
                            triggered=self.open_ipython)
        if programs.is_module_installed("IPython"):
            self.menu_actions.insert(1, ipython_action)
            run_menu_actions.append(ipython_action)

        self.main.run_menu_actions += [None]+run_menu_actions
        self.main.tools_menu_actions += tools_menu_actions

        return self.menu_actions+run_menu_actions+tools_menu_actions

    def register_plugin(self):
        """Register plugin in Spyder's main window"""
        if self.main.light:
            self.main.setCentralWidget(self)
            self.main.widgetlist.append(self)
        else:
            self.main.add_dockwidget(self)
            self.inspector = self.main.inspector
            if self.inspector is not None:
                self.inspector.set_external_console(self)
            self.historylog = self.main.historylog
            self.connect(self, SIGNAL("edit_goto(QString,int,QString)"),
                         self.main.editor.load)
            self.connect(self.main.editor,
                         SIGNAL('run_in_current_console(QString,QString,QString,bool)'),
                         self.run_script_in_current_shell)
            self.connect(self.main.editor, SIGNAL("open_dir(QString)"),
                         self.set_current_shell_working_directory)
            self.connect(self.main.workingdirectory,
                         SIGNAL("set_current_console_wd(QString)"),
                         self.set_current_shell_working_directory)
            self.connect(self, SIGNAL('focus_changed()'),
                         self.main.plugin_focus_changed)
            self.connect(self, SIGNAL('redirect_stdio(bool)'),
                         self.main.redirect_internalshell_stdio)
            expl = self.main.explorer
            if expl is not None:
                self.connect(expl, SIGNAL("open_terminal(QString)"),
                             self.open_terminal)
                self.connect(expl, SIGNAL("open_interpreter(QString)"),
                             self.open_interpreter)
                self.connect(expl, SIGNAL("open_ipython(QString)"),
                             self.open_ipython)
            pexpl = self.main.projectexplorer
            if pexpl is not None:
                self.connect(pexpl, SIGNAL("open_terminal(QString)"),
                             self.open_terminal)
                self.connect(pexpl, SIGNAL("open_interpreter(QString)"),
                             self.open_interpreter)
                self.connect(pexpl, SIGNAL("open_ipython(QString)"),
                             self.open_ipython)

    def closing_plugin(self, cancelable=False):
        """Perform actions before parent main window is closed"""
        for shellwidget in self.shellwidgets:
            shellwidget.close()
        return True

    def refresh_plugin(self):
        """Refresh tabwidget"""
        shellwidget = None
        if self.tabwidget.count():
            shellwidget = self.tabwidget.currentWidget()
            editor = shellwidget.shell
            editor.setFocus()
            widgets = [shellwidget.create_time_label(), 5
                       ]+shellwidget.get_toolbar_buttons()+[5]
        else:
            editor = None
            widgets = []
        self.find_widget.set_editor(editor)
        self.tabwidget.set_corner_widgets({Qt.TopRightCorner: widgets})
        if shellwidget:
            shellwidget.update_time_label_visibility()

    def apply_plugin_settings(self, options):
        """Apply configuration file's plugin settings"""
        whitebg_n = 'light_background'
        ipybg_n = 'ipython_set_color'
        if (whitebg_n in options or ipybg_n in options) \
           and self.get_option(ipybg_n):
            ipython_n = 'ipython_options'
            args = self.get_option(ipython_n, "")
            if args:
                lbgo = "-colors LightBG"
                if self.get_option(whitebg_n):
                    # White background
                    if lbgo not in args:
                        self.set_option(ipython_n, args+" "+lbgo)
                else:
                    # Black background
                    self.set_option(ipython_n, args.replace(" "+lbgo, ""
                                    ).replace(lbgo+" ", ""))
        font = self.get_plugin_font()
        showtime = self.get_option('show_elapsed_time')
        icontext = self.get_option('show_icontext')
        calltips = self.get_option('calltips')
        inspector = self.get_option('object_inspector')
        wrap = self.get_option('wrap')
        compauto = self.get_option('codecompletion/auto')
        case_comp = self.get_option('codecompletion/case_sensitive')
        show_single = self.get_option('codecompletion/show_single')
        compenter = self.get_option('codecompletion/enter_key')
        mlc = self.get_option('max_line_count')
        for shellwidget in self.shellwidgets:
            shellwidget.shell.set_font(font)
            shellwidget.set_elapsed_time_visible(showtime)
            shellwidget.set_icontext_visible(icontext)
            shellwidget.shell.set_calltips(calltips)
            if isinstance(shellwidget.shell, ExternalPythonShell):
                shellwidget.shell.set_inspector_enabled(inspector)
            shellwidget.shell.toggle_wrap_mode(wrap)
            shellwidget.shell.set_codecompletion_auto(compauto)
            shellwidget.shell.set_codecompletion_case(case_comp)
            shellwidget.shell.set_codecompletion_single(show_single)
            shellwidget.shell.set_codecompletion_enter(compenter)
            shellwidget.shell.setMaximumBlockCount(mlc)

    #------ Public API ---------------------------------------------------------
    def open_interpreter_at_startup(self):
        """Open an interpreter at startup, IPython if module is available"""
        if self.get_option('open_ipython_at_startup') \
           and programs.is_module_installed("IPython"):
            self.open_ipython()
        if self.get_option('open_python_at_startup'):
            self.open_interpreter()

    def open_interpreter(self, wdir=None):
        """Open interpreter"""
        if wdir is None:
            wdir = os.getcwdu()
        self.start(fname=None, wdir=unicode(wdir), args='',
                   interact=True, debug=False, python=True)

    def get_default_ipython_options(self):
        """Return default ipython command line arguments"""
        default_options = []
        if programs.is_module_installed('matplotlib'):
            default_options.append("-pylab")
        default_options.append("-q4thread")
        default_options.append("-colors LightBG")
        default_options.append("-xmode Plain")
        for editor_name in ("scite", "gedit"):
            real_name = programs.get_nt_program_name(editor_name)
            if programs.is_program_installed(real_name):
                default_options.append("-editor "+real_name)
                break
        return " ".join(default_options)

    def open_ipython(self, wdir=None):
        """Open IPython"""
        if wdir is None:
            wdir = os.getcwdu()
        args = self.get_option('ipython_options', "")
        self.start(fname=None, wdir=unicode(wdir), args=args,
                   interact=True, debug=False, python=True, ipython=True)

    def open_terminal(self, wdir=None):
        """Open terminal"""
        if wdir is None:
            wdir = os.getcwdu()
        self.start(fname=None, wdir=unicode(wdir), args='',
                   interact=True, debug=False, python=False)

    def run_script(self):
        """Run a Python script"""
        self.emit(SIGNAL('redirect_stdio(bool)'), False)
        filename = QFileDialog.getOpenFileName(self,
                      _("Run Python script"), os.getcwdu(),
                      _("Python scripts")+" (*.py ; *.pyw ; *.ipy)")
        self.emit(SIGNAL('redirect_stdio(bool)'), True)
        if filename:
            self.start(fname=unicode(filename), wdir=None, args='',
                       interact=False, debug=False)

    def set_umd_namelist(self):
        """Set UMD excluded modules name list"""
        arguments, valid = QInputDialog.getText(self, _('UMD'),
                                  _('UMD excluded modules:\n'
                                          '(example: guidata, guiqwt)'),
                                  QLineEdit.Normal,
                                  ", ".join(self.get_option('umd/namelist')))
        if valid:
            arguments = unicode(arguments)
            if arguments:
                namelist = arguments.replace(' ', '').split(',')
                fixed_namelist = [module_name for module_name in namelist
                                  if programs.is_module_installed(module_name)]
                invalid = ", ".join(set(namelist)-set(fixed_namelist))
                if invalid:
                    QMessageBox.warning(self, _('UMD'),
                                        _("The following modules are not "
                                          "installed on your machine:\n%s"
                                          ) % invalid, QMessageBox.Ok)
                QMessageBox.information(self, _('UMD'),
                                    _("Please note that these changes will "
                                      "be applied only to new Python/IPython "
                                      "interpreters"), QMessageBox.Ok)
            else:
                fixed_namelist = []
            self.set_option('umd/namelist', fixed_namelist)

    def go_to_error(self, text):
        """Go to error if relevant"""
        match = get_error_match(unicode(text))
        if match:
            fname, lnb = match.groups()
            self.emit(SIGNAL("edit_goto(QString,int,QString)"),
                      osp.abspath(fname), int(lnb), '')

    #----Drag and drop
    def __is_python_script(self, qstr):
        """Is it a valid Python script?"""
        fname = unicode(qstr)
        return osp.isfile(fname) and \
               ( fname.endswith('.py') or fname.endswith('.pyw') \
                 or fname.endswith('.ipy') )

    def dragEnterEvent(self, event):
        """Reimplement Qt method
        Inform Qt about the types of data that the widget accepts"""
        source = event.mimeData()
        if source.hasUrls():
            if mimedata2url(source):
                pathlist = mimedata2url(source)
                shellwidget = self.tabwidget.currentWidget()
                if all([self.__is_python_script(qstr) for qstr in pathlist]):
                    event.acceptProposedAction()
                elif shellwidget is None or not shellwidget.is_running():
                    event.ignore()
                else:
                    event.acceptProposedAction()
            else:
                event.ignore()
        elif source.hasText():
            event.acceptProposedAction()

    def dropEvent(self, event):
        """Reimplement Qt method
        Unpack dropped data and handle it"""
        source = event.mimeData()
        shellwidget = self.tabwidget.currentWidget()
        if source.hasText():
            qstr = source.text()
            if self.__is_python_script(qstr):
                self.start(qstr)
            elif shellwidget:
                shellwidget.shell.insert_text(qstr)
        elif source.hasUrls():
            pathlist = mimedata2url(source)
            if all([self.__is_python_script(qstr) for qstr in pathlist]):
                for fname in pathlist:
                    self.start(fname)
            elif shellwidget:
                shellwidget.shell.drop_pathlist(pathlist)
        event.acceptProposedAction()


import pygame
from pygame.locals import *
from snake_agent import SnakeAgent
from board import BoardEnv
import helper
import time

#   This is the SnakeGame class you'll be working with.
#   It first initializes based on the default conditions.
#   Then it trains according to the number of training steps
#       printing the stats along the way
#   Then it tests using the training set and again prints
#        prints the points along the way
#   Then it calls the show_games function which also based on
#       the parameters, shows a number of games being played
#       based on the training done.

class SnakeGame:

    #   This constructor initializes the board according to the conditions mentioned
    #       in the helper file.
    #   It sets the board and initializes the snake agent.
    def __init__(self, args):
        self.args = args
        self.env = BoardEnv(args.snake_head_x, args.snake_head_y, args.food_x, args.food_y)
        self.agent = SnakeAgent(self.env.get_actions(), args.Ne, args.LPC, args.gamma)

    #   This function does the necessary function calls, to do_training() (if necessary)
    #       then the do_testint() then show_games()
    def play(self):
        if self.args.NUM_TRAIN_ITER != 0:
            self.do_training()
        self.do_testing()
        self.show_games()

    #   This is the function that does calls the functions to do reinforcement training
    #       as many times as specified. It also prints the statistics based on the
    #       parameter specfiied
    def do_training(self):
        print("IN TRAINING PHASE: ")
        self.agent.set_train()
        NUM_TO_STAT = self.args.NUM_TO_STAT
        self.points_results = []
        start = time.time()
        self.env.display()
        pygame.event.pump()

        #   This loop will train for required number of times
        #   WRITE YOUR CODE IN THIS LOOP TO CALL THE TRAINING FUNCTION.
        #   AS TRAINING IS HAPPENING THE CODE IN THE LOOP WILL PRINT STATISTICS.
        #   Use self.env.reset() to reset your game after each iteration.
        for game in range(1, self.args.NUM_TRAIN_ITER + 1):
            print("TRAINING NUMBER : " + str(game))
            state = self.env.get_state()
            dead = False
            action = self.agent.agent_action(state, 0, dead)
            count = 0
            while not dead:
                count +=1
                pygame.event.pump()
                state, points, dead = self.env.step(action)
                # Qlearning agent
                action = self.agent.agent_action(state, points, dead)
            #after game, reset
            print("GAME OVER\n\n\n")
            self.env.reset()
            self.points_results.append(points)
            #UNCOMMENT THE CODE BELOW TO PRINT STATISTICS
            if game % self.args.NUM_TO_STAT == 0:
                print(
                    "Played games:", len(self.points_results) - NUM_TO_STAT, "-", len(self.points_results),
                    "Calculated points (Average:", sum(self.points_results[-NUM_TO_STAT:])/NUM_TO_STAT,
                    "Max points so far:", max(self.points_results[-NUM_TO_STAT:]),
                    "Min points so far:", min(self.points_results[-NUM_TO_STAT:]),")",
                )

        print("Training takes", time.time() - start, "seconds")
        #   THIS LINE WILL SAVE THE MODEL TO THE FILE "model.npy"
        self.agent.save_model()

    #   This function will test based on the model you created. It first reads the
    #       "model.npy" file created above and makes moves based on the trained model
    def do_testing(self):
        print("Test Phase:")
        self.agent.set_eval()
        #   This line loads the model
        self.agent.load_model()
        points_results = []
        start = time.time()

        #   This loop runs the test the specified number of times.
        #   This is where you will write your code.
        #   Use self.env.reset() to reset your state everytime a new game begins.
        for game in range(1, self.args.NUM_TEST_ITER + 1):
            print("TESTING NUMBER: " + str(game))
            # YOUR CODE HERE
            # YOUR CODE HERE
            # YOUR CODE HERE
            # YOUR CODE HERE
            # YOUR CODE HERE

        #UNCOMMENT THE CODE BELOW TO PRINT STATISTICS
        #print("Testing takes", time.time() - start, "seconds")
        #print("Number of Games:", len(points_results))
        #print("Average Points:", sum(points_results)/len(points_results))
        #print("Max Points:", max(points_results))
        #print("Min Points:", min(points_results))

    #   This function is the one where the game will be displayed.
    #   This function is already written for you. No changes are necessary
    #       as long as YOU don't change function names or parameters.
    def show_games(self):
        print("Display Games")
        self.env.display()
        pygame.event.pump()
        self.agent.set_eval()
        points_results = []
        end = False
        for game in range(1, self.args.NUM_DISP_ITER + 1):
            state = self.env.get_state()
            dead = False
            action = self.agent.agent_action(state, 0, dead)
            count = 0
            while not dead:
                count +=1
                pygame.event.pump()
                keys = pygame.key.get_pressed()
                if keys[K_ESCAPE] or self.check_quit():
                    end = True
                    break
                state, points, dead = self.env.step(action)
                # Qlearning agent
                action = self.agent.agent_action(state, points, dead)
            if end:
                break
            self.env.reset()
            points_results.append(points)
            print("Game:", str(game)+"/"+str(self.args.NUM_DISP_ITER), "Points:", points)
        if len(points_results) == 0:
            return
        print("Average Points:", sum(points_results)/len(points_results))

    def check_quit(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return True
        return False

#   This is the main for the program, it generates the default arguemnts and calls the play function
if __name__ == "__main__":

    main_args = helper.make_args()
    print(main_args)
    game1 = SnakeGame(main_args)
    game1.play()


# 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: D:\Development\django2-propeller\django2_propeller\card.py
# Compiled at: 2019-04-26 08:32:14
# Size of source mod 2**32: 10322 bytes
"""This module contains classes for constructing propeller cards"""
from django.utils.safestring import mark_safe
from .utils import render_tag, add_css_class
from .components import Button, FAB, Image
from .text import text_concat
from .exceptions import PropellerException

class CardTitle(object):
    __doc__ = '\n    Renders a Card Title.\n\n    **Parameters**:\n\n        text\n            The display text for the title.\n\n        size\n            The size for the title as integer. Works with the h-tag, so size=1 is bigger than size=3.\n            Optional. (default=3)\n    '
    text = ''
    size = 3

    def as_html(self):
        """Returns card title as html"""
        tag = 'h%d' % self.size
        attrs = {'class': 'pmd-card-title-text'}
        content = self.text
        return render_tag(tag, attrs=attrs, content=(mark_safe(content)))

class CardSubtitle(object):
    __doc__ = '\n    Renders a Card Subtitle.\n\n    **Parameters**:\n\n        text\n            The display text for the subtitle.\n    '
    text = ''

    def as_html(self):
        """Returns card subtitle as html"""
        tag = 'span'
        attrs = {'class': 'pmd-card-subtitle-text'}
        content = self.text
        return render_tag(tag, attrs=attrs, content=(mark_safe(content)))

class CardBody(object):
    __doc__ = '\n    Renders a Card Body.\n\n    **Parameters**:\n\n        text\n            The text to display in the body.\n    '
    text = ''

    def as_html(self):
        """Returns card body as html"""
        tag = 'div'
        attrs = {'class': 'pmd-card-body'}
        content = self.text
        return render_tag(tag, attrs=attrs, content=(mark_safe(content)))

class CardHeader(object):
    __doc__ = '\n    Renders a Card Header.\n\n    **Parameters**:\n\n        content_left\n            A list of items to display on the left of header. \n            May contain Button, FAB, Image, CardTitle, or CardSubtitle.\n\n        content_middle\n            A list of items to display in the middle of header. \n            May contain Button, FAB, Image, CardTitle, or CardSubtitle.\n    '
    content_left = []
    content_middle = []

    def get_left_content(self):
        """Returns left content of card header as html"""
        tag = 'div'
        attrs = {'class': 'media-left'}
        content = ''
        for itm in self.content_left:
            content = text_concat(content, mark_safe(itm.as_html()))

        return render_tag(tag, attrs=attrs, content=(mark_safe(content)))

    def get_middle_content(self):
        """Returns middle content of card header as html"""
        tag = 'div'
        attrs = {'class': 'media-body media-middle'}
        content = ''
        for itm in self.content_middle:
            content = text_concat(content, mark_safe(itm.as_html()))

        return render_tag(tag, attrs=attrs, content=(mark_safe(content)))

    def as_html(self):
        """Returns card header as html"""
        tag = 'div'
        attrs = {'class': 'pmd-card-title'}
        content = text_concat(self.get_left_content(), self.get_middle_content())
        return render_tag(tag, attrs=attrs, content=(mark_safe(content)))

class CardMediaActions(object):
    __doc__ = '\n    Renders Card Media Actions.\n\n    **Parameters**:\n\n        items\n            A list of items to display in the Card Media Action section. \n            May contain Button or FAB.\n    '
    items = []

    def as_html(self):
        """Returns card media actions as html"""
        tag = 'div'
        attrs = {'class': 'pmd-card-actions'}
        content = ''
        for btn in self.items:
            if isinstance(btn, FAB):
                content = text_concat(content, mark_safe(btn.as_html()))

        return render_tag(tag, attrs=attrs, content=(mark_safe(content)))

class CardActions(object):
    __doc__ = '\n    Renders Card Actions.\n\n    **Parameters**:\n\n        items\n            A list of items to display in the Card Action section. \n            May contain Button or FAB.\n    '
    items = []

    def as_html(self):
        """Returns card actions as html"""
        tag = 'div'
        attrs = {'class': 'pmd-card-actions'}
        content = ''
        for btn in self.items:
            if isinstance(btn, Button):
                content = text_concat(content, mark_safe(btn.as_html()))

        return render_tag(tag, attrs=attrs, content=(mark_safe(content)))

class CardMediaImage(object):
    __doc__ = '\n    Renders a Card Media Image.\n\n    **Parameters**:\n\n        image\n            Must be an instance of an Image.\n    '
    image = None

    def as_html(self):
        """Returns card media image as html"""
        if isinstance(self.image, Image):
            return self.image.as_html()

class CardMedia(object):
    __doc__ = '\n    Renders Card Media\n\n    **Parameters**:\n\n        content\n            if style_inline=True:\n                A list of items to display in the card media section. \n                May contain CardMediaImage, CardTitle, or CardSubtitle.\n            or if style_inline=False: (default)\n                A instance of CardMediaImage\n\n        style_inline\n            Display card with inline style if true. (Default: False)\n    '
    content = None
    style_inline = False

    def get_media_body_inline(self):
        """Returns media body inline as html"""
        tag = 'div'
        attrs = {'class': 'media-body'}
        content = ''
        if self.style_inline:
            if not isinstance(self.content, list):
                raise PropellerException('Propeller Card: content must be a list')
            for itm in self.content:
                if isinstance(itm, (CardTitle, CardSubtitle)):
                    content = text_concat(content, mark_safe(itm.as_html()))

            content = text_concat(content, mark_safe('
'))
            content = text_concat(content, mark_safe('
'))
            for itm in self.content:
                if isinstance(itm, CardMediaImage):
                    content = text_concat(content, mark_safe(itm.as_html()))

        return render_tag(tag, attrs=attrs, content=(mark_safe(content)))

    def get_media_body(self):
        """Returns media body as html"""
        if self.style_inline:
            return self.get_media_body_inline()
        tag = 'div'
        attrs = {'class': 'media-body'}
        content = ''
        if not isinstance(self.content, list):
            raise PropellerException('Propeller Card: content must be a list')
        for itm in self.content:
            if isinstance(itm, CardMediaImage):
                content = text_concat(content, mark_safe(itm.as_html()))

        return render_tag(tag, attrs=attrs, content=(mark_safe(content)))

    def as_html(self):
        """Returns card media as html"""
        tag = 'div'
        attrs = {'class': 'pmd-card-media'}
        content = self.get_media_body()
        return render_tag(tag, attrs=attrs, content=(mark_safe(content)))

class Card(object):
    __doc__ = '\n    Card is a class that generates a Propeller Card.\n\n    **Parameters**:\n\n        primary_title\n            An instance of CardTitle. Optional.\n\n        secondary_title\n            An instance of CardSubtitle. Optional.\n\n        header\n            An instance of CardHeader. Optional.\n\n        media\n            An instance of CardMedia. Optional.\n\n        body\n            An instance of CardBody. Optional.\n\n        actions\n            An instance of CardActions. Optional.\n\n        media_actions\n            An instance of CardMediaActions. Optional.\n\n        style_inverse\n            Display dark style if true. (Default: False)\n\n        style_inline\n            Display card with inline style if true. (Default: False)\n\n        width\n            Width of the card in Bootstrap grid (col-md) as integer. (Default: 4)\n    '
    primary_title = None
    secondary_title = None
    header = None
    media = None
    body = None
    actions = None
    media_actions = None
    style_inverse = False
    style_inline = False
    width = 4

    def get_actions(self):
        """Returns actions of card as html"""
        actions = ''
        if self.media_actions:
            actions = text_concat(actions, self.media_actions.as_html())
        if self.actions:
            actions = text_concat(actions, self.actions.as_html())
        return actions

    def get_content(self):
        """Returns content of card as html"""
        content = ''
        if self.header:
            if not self.style_inline:
                content = text_concat(content, self.header.as_html())
        if self.media:
            content = text_concat(content, self.media.as_html())
        if not self.style_inline:
            if self.primary_title or self.secondary_title:
                content = text_concat(content, '
')
                if self.primary_title:
                    content = text_concat(content, self.primary_title.as_html())
                if self.secondary_title:
                    content = text_concat(content, self.secondary_title.as_html())
                content = text_concat(content, '
')
            if self.body:
                content = text_concat(content, self.body.as_html())
        content = text_concat(content, self.get_actions())
        return content

    def as_html(self):
        """Returns card as html"""
        tag = 'div'
        classes = 'pmd-card'
        if self.style_inline:
            classes = add_css_class(classes, 'pmd-card-media-inline')
        elif self.style_inverse:
            classes = add_css_class(classes, 'pmd-card-inverse')
        else:
            classes = add_css_class(classes, 'pmd-card-default')
        classes = add_css_class(classes, 'pmd-z-depth')
        classes = add_css_class(classes, 'col-md-%d' % self.width)
        attrs = {'class': classes}
        content = self.get_content()
        return render_tag(tag, attrs=attrs, content=(mark_safe(content)))


# -*- coding: utf-8 -*-
import datetime
import json
import os

import requests
import requests.exceptions
import secretballot
import simplejson.scanner
from concurrency.fields import IntegerVersionField
from django.conf import settings
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.http import QueryDict
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy as _
from readability.readability import Document, Unparseable
from taggit.managers import TaggableManager
from taggit.models import TagBase, GenericTaggedItemBase
from taggit_autosuggest.managers import TaggableManager

from frontend.models import Tip
# import the logging library
import logging

# Get an instance of a logger
logger = logging.getLogger(__name__)
ISSUE_STATUS_CHOICES = (('active', u'Активный'), ('draft', u'Черновик'),)

LIBRARY_SECTIONS = None

def load_library_sections():
    global LIBRARY_SECTIONS
    titles = [
        'Интересные проекты, инструменты, библиотеки',
        'Релизы'
    ]
    try:
        LIBRARY_SECTIONS = [Section.objects.get(title=title) for title in titles]
    except (ObjectDoesNotExist, Section.DoesNotExist):
        LIBRARY_SECTIONS = []

def get_start_end_of_week(dt):
    start = dt - datetime.timedelta(days=dt.weekday())
    end = start + datetime.timedelta(days=6)
    return start, end

class Keyword(TagBase):
    class Meta:
        verbose_name = _("Keyword")
        verbose_name_plural = _("Keywords")

class KeywordGFK(GenericTaggedItemBase):
    tag = models.ForeignKey(Keyword, related_name="%(app_label)s_%(class)s_items")

class Issue(models.Model):
    """Выпуск дайджеста."""
    title = models.CharField(max_length=255, verbose_name=u'Заголовок', )
    description = models.TextField(verbose_name=u'Описание',
                                   null=True,
                                   blank=True, )
    announcement = models.TextField(verbose_name=u'Анонс',
                                    null=True,
                                    blank=True, )
    image = models.ImageField(verbose_name=u'Постер',
                              upload_to='issues',
                              null=True,
                              blank=True, )
    date_from = models.DateField(verbose_name=u'Начало освещаемого периода',
                                 null=True,
                                 blank=True, )
    date_to = models.DateField(verbose_name=u'Завершение освещаемого периода',
                               null=True,
                               blank=True, )
    published_at = models.DateField(verbose_name=u'Дата публикации',
                                    null=True,
                                    blank=True, )
    status = models.CharField(verbose_name=u'Статус',
                              max_length=10,
                              choices=ISSUE_STATUS_CHOICES,
                              default='draft', )
    version = IntegerVersionField(verbose_name=u'Версия')

    tip = models.ForeignKey(Tip, null=True, blank=True, verbose_name=u'Совет')

    trend = models.CharField(verbose_name='Тенденция недели',
                             blank=True,
                             null=True,
                             max_length=255, )

    last_item = models.IntegerField(
        verbose_name='Последняя модерированая новость',
        blank=True,
        null=True, )

    def __str__(self):
        return self.title

    @property
    def link(self):
        return reverse('digest:issue_view', kwargs={'pk': self.pk})

    class Meta:
        ordering = ['-pk']
        verbose_name = u'Выпуск дайджеста'
        verbose_name_plural = u'Выпуски дайджеста'

SECTION_STATUS_CHOICES = (('pending', u'Ожидает проверки'),
                          ('active', u'Активный'),)

class Section(models.Model):
    """Раздел."""
    title = models.CharField(max_length=255, verbose_name=u'Заголовок', )
    priority = models.PositiveIntegerField(
        verbose_name=u'Приоритет при показе',
        default=0, )
    status = models.CharField(verbose_name=u'Статус',
                              max_length=10,
                              choices=SECTION_STATUS_CHOICES,
                              default='active', )
    version = IntegerVersionField(verbose_name=u'Версия')
    habr_icon = models.CharField(max_length=255,
                                 verbose_name=u'Иконка для хабры',
                                 null=True,
                                 blank=True)

    def __str__(self):
        return self.title

    class Meta:
        ordering = ['-pk']
        verbose_name = u'Раздел'
        verbose_name_plural = u'Разделы'

class Resource(models.Model):
    """Источник получения информации."""
    title = models.CharField(max_length=255, verbose_name=u'Заголовок', )
    description = models.TextField(verbose_name=u'Описание',
                                   null=True,
                                   blank=True, )
    link = models.URLField(max_length=255, verbose_name=u'Ссылка', )
    version = IntegerVersionField(verbose_name=u'Версия')

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = u'Источник'
        verbose_name_plural = u'Источники'

ITEM_STATUS_CHOICES = (
    ('pending', u'На рассмотрении'),
    ('active', u'Активная'),
    ('draft', u'Черновик'),
    ('moderated', u'Рассмотрена'),
    ('autoimport', u'Автоимпорт'),
    ('queue', u'В очереди'),
)

ITEM_LANGUAGE_CHOICES = (('ru', u'Русский'), ('en', u'Английский'),)

def build_url(*args, **kwargs):
    params = kwargs.pop('params', {})
    url = reverse(*args, **kwargs)
    if not params: return url

    qdict = QueryDict('', mutable=True)
    for k, v in params.items():
        if type(v) is list:
            qdict.setlist(k, v)
        else:
            qdict[k] = v

    return url + '?' + qdict.urlencode()

class Item(models.Model):
    """Новость."""
    section = models.ForeignKey(Section,
                                verbose_name=u'Раздел',
                                null=True,
                                blank=True, )
    title = models.CharField(max_length=255, verbose_name=u'Заголовок', )
    is_editors_choice = models.BooleanField(verbose_name=u'Выбор редакции',
                                            default=False, )
    description = models.TextField(verbose_name=u'Описание',
                                   null=True,
                                   blank=True, )
    issue = models.ForeignKey(Issue,
                              verbose_name=u'Выпуск дайджеста',
                              null=True,
                              blank=True, )
    resource = models.ForeignKey(Resource,
                                 verbose_name=u'Источник',
                                 null=True,
                                 blank=True, )
    link = models.URLField(max_length=255, verbose_name=u'Ссылка', )
    additionally = models.CharField(max_length=255,
                                    verbose_name=u'Дополнительно',
                                    null=True,
                                    blank=True)
    related_to_date = models.DateField(
        verbose_name=u'Дата',
        help_text=u'Например, дата публикации новости на источнике',
        default=datetime.datetime.today, )
    status = models.CharField(verbose_name=u'Статус',
                              max_length=10,
                              choices=ITEM_STATUS_CHOICES,
                              default='pending', )
    language = models.CharField(verbose_name=u'Язык новости',
                                max_length=2,
                                choices=ITEM_LANGUAGE_CHOICES,
                                default='en', )
    created_at = models.DateField(verbose_name=u'Дата публикации',
                                  auto_now_add=True, )
    modified_at = models.DateTimeField(verbose_name=u'Дата изменения',
                                       null=True,
                                       blank=True, )

    activated_at = models.DateTimeField(
        verbose_name=u'Дата активации',
        default=datetime.datetime.now,
    )
    priority = models.PositiveIntegerField(
        verbose_name=u'Приоритет при показе',
        default=0, )
    user = models.ForeignKey(User,
                             verbose_name=u'Кто добавил новость',
                             editable=False,
                             null=True,
                             blank=True, )
    version = IntegerVersionField(verbose_name=u'Версия')
    tags = TaggableManager(blank=True)
    keywords = TaggableManager(through=KeywordGFK, blank=True, verbose_name=_("Keywords"))
    to_update = models.BooleanField(verbose_name=u'Обновить новость', default=False, )
    article_path = models.FilePathField(verbose_name='Путь до статьи', blank=True, null=True)

    def save(self, *args, **kwargs):
        try:
            if self.issue is None and self.created_at is not None:
                date_from, date_to = get_start_end_of_week(self.created_at)
                issue = Issue.objects.filter(date_from=date_from,
                                             date_to=date_to)
                if issue.count() == 0:
                    # если нет выпуска, то создадим
                    old_issue = Issue.objects.latest('date_to')
                    cnt_issue = int(old_issue.title.replace('Выпуск ', '')) + 1
                    new_issue = Issue(title='Выпуск %s' % cnt_issue,
                                      date_from=date_from,
                                      date_to=date_to, )
                    new_issue.save()
                    self.issue = new_issue
                elif issue.count() == 1:
                    self.issue = issue[0]
                else:
                    raise Exception('Несколько выпусков на неделе')

        except Exception as e:
            logger.error("Many issues are on one week: {0}".format(e))
        super(Item, self).save(*args, **kwargs)

    @property
    def cls_check(self):
        try:
            item = ItemClsCheck.objects.get(item=self)
            item.check_cls()
        except (ObjectDoesNotExist, ItemClsCheck.DoesNotExist):
            item = ItemClsCheck(item=self)
            item.save()
            item.check_cls(force=True)
        return item.status

    @property
    def link_type(self):
        global LIBRARY_SECTIONS
        if LIBRARY_SECTIONS is None:
            load_library_sections()
        if any([x == self.section_id for x in LIBRARY_SECTIONS]):
            return 'library'
        else:
            return 'article'

    @property
    def text(self):
        if self.article_path is not None and self.article_path and os.path.exists(self.article_path):
            with open(self.article_path, 'r') as fio:
                result = fio.read()
        else:
            try:
                resp = requests.get(self.link)
                text = resp.text
                try:
                    result = Document(text,
                                      min_text_length=50,
                                      positive_keywords=','.join(settings.DATASET_POSITIVE_KEYWORDS),
                                      negative_keywords=','.join(settings.DATASET_NEGATIVE_KEYWORDS)
                                      ).summary()
                except Unparseable:
                    result = text
            except (KeyError,
                    requests.exceptions.RequestException,
                    requests.exceptions.Timeout,
                    requests.exceptions.TooManyRedirects) as e:
                result = ''
            self.article_path = os.path.join(settings.DATASET_ROOT, '{0}.html'.format(self.id))
            with open(self.article_path, 'w') as fio:
                fio.write(result)
            self.save()
        return result

    def get_data4cls(self, status=False):
        result = {
            'link': self.link,
            'data': {
                'language': self.language,
                'title': self.title,
                'description': self.description,
                'article': self.text,
                'type': self.link_type,
            }
        }
        if status:
            result['data']['label'] = self.status == 'active'
        return result

    data4cls = property(get_data4cls)

    @property
    def internal_link(self):
        return reverse('digest:item', kwargs={'pk': self.pk})

    @property
    def tags_as_links(self):
        return [(x.name, build_url('digest:feed', params={'tag': x.name})) for x in self.tags.all()]

    @property
    def tags_as_str(self):
        if self.tags and self.tags.all():
            result = ','.join([x.name for x in self.tags.all()])
        else:
            result = 'Without tag'
        return result

    @property
    def keywords_as_str(self):
        return ', '.join(list(set([x.name for x in self.keywords.all()]))[:13])

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = 'Новость'
        verbose_name_plural = 'Новости'

class ItemClsCheck(models.Model):
    item = models.OneToOneField(Item, verbose_name='Новость')
    last_check = models.DateTimeField(verbose_name='Время последней проверки', auto_now=True)
    status = models.BooleanField(default=False, verbose_name='Оценка')

    def check_cls(self, force=False):
        # print("Run check: {}".format(self.pk))
        if force or self.last_check <= datetime.datetime.now() - datetime.timedelta(days=10):

            try:
                url = "{0}/{1}".format(settings.CLS_URL_BASE, 'api/v1.0/classify/')
                resp = requests.post(url,
                                     data=json.dumps({'links': [
                                         self.item.data4cls
                                     ]}))
                self.status = resp.json()['links'][0].get(self.item.link, False)
            except (requests.exceptions.RequestException,
                    requests.exceptions.Timeout,
                    requests.exceptions.TooManyRedirects,
                    simplejson.scanner.JSONDecodeError) as e:
                self.status = False
            # print("Real run check: {}".format(self.pk))
            self.save()

    def __str__(self):
        return "{0} - {1} ({2})".format(str(self.item), self.status, self.last_check)

    class Meta:
        verbose_name = 'Проверка классификатором'
        verbose_name_plural = 'Проверка классификатором'

class AutoImportResource(models.Model):
    """Источники импорта новостей."""
    TYPE_RESOURCE = (('twitter', u'Сообщения аккаунтов в твиттере'),
                     ('rss', u'RSS фид'),)

    name = models.CharField(max_length=255, unique=True,
                            verbose_name=u'Название источника', )
    link = models.URLField(max_length=255, unique=True, verbose_name=u'Ссылка', )
    type_res = models.CharField(max_length=255,
                                verbose_name=u'Тип источника',
                                choices=TYPE_RESOURCE,
                                default='twitter', )
    resource = models.ForeignKey(Resource,
                                 verbose_name=u'Источник',
                                 null=True,
                                 blank=True, )
    incl = models.CharField(max_length=255,
                            verbose_name=u'Обязательное содержание',
                            help_text='Условие отбора новостей
 \
                   Включение вида [text]
 \
                   Включение при выводе будет удалено',
                            null=True,
                            blank=True, )
    excl = models.TextField(
        verbose_name=u'Список исключений',
        help_text='Список источников подлежащих исключению через ", "',
        null=True,
        blank=True, )
    in_edit = models.BooleanField(verbose_name=u'На тестировании',
                                  default=False, )

    language = models.CharField(verbose_name=u'Язык источника',
                                max_length=2,
                                choices=ITEM_LANGUAGE_CHOICES,
                                default='en', )

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = u'Источник импорта новостей'
        verbose_name_plural = u'Источники импорта новостей'

class Package(models.Model):
    name = models.CharField(max_length=255, verbose_name=u'Название', )

    description = models.TextField(verbose_name=u'Описание',
                                   null=True,
                                   blank=True, )

    url = models.CharField(max_length=255, verbose_name=u'Ссылка', )

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = u'Библиотека'
        verbose_name_plural = u'Библиотеки'

class ParsingRules(models.Model):
    IF_ELEMENTS = (('title', u'Заголовок новости'), ('link', u'Url новости'),
                   ('content', u'Текст новости'),
                   ('description', u'Описание новости'),
                   ('http_code', u'HTTP Code'),)

    IF_ACTIONS = (('equal', u'Равен'), ('contains', u'Содержит'),
                  ('not_equal', u'Не равен'), ('regex', u'Regex match'),)

    THEN_ELEMENT = (('title', u'Заголовок новости'),
                    ('description', u'Описание новости'),
                    ('section', u'Раздел'), ('status', u'Статус'),
                    ('tags', u'Тэг новости'),)

    THEN_ACTION = (('set', u'Установить'), ('add', u'Добавить'),
                   ('remove', u'Удалить часть строки'),)

    name = models.CharField(max_length=255, verbose_name=u'Название правила', )

    is_activated = models.BooleanField(verbose_name=u'Включено',
                                       default=True, )

    if_element = models.CharField(max_length=255,
                                  verbose_name=u'Элемент условия',
                                  choices=IF_ELEMENTS,
                                  default='item_title', )

    if_action = models.CharField(max_length=255,
                                 verbose_name=u'Условие',
                                 choices=IF_ACTIONS,
                                 default='consist', )

    if_value = models.CharField(max_length=255, verbose_name=u'Значение', )

    then_element = models.CharField(max_length=255,
                                    verbose_name=u'Элемент действия',
                                    choices=THEN_ELEMENT,
                                    default='item_title', )

    then_action = models.CharField(max_length=255,
                                   verbose_name=u'Действие',
                                   choices=THEN_ACTION,
                                   default='item_title', )

    then_value = models.CharField(max_length=255, verbose_name=u'Значение', )

    weight = models.PositiveIntegerField(default=100, verbose_name=_('Weight'))

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = u'Правило обработки'
        verbose_name_plural = u'Правила обработки'
        ordering = ['-weight']

@receiver(post_save, sender=Item)
def update_cls_score(instance, **kwargs):
    try:
        item = ItemClsCheck.objects.get(item=instance)
        item.check_cls()
    except (ObjectDoesNotExist, ItemClsCheck.DoesNotExist):
        item = ItemClsCheck(item=instance)
        item.save()
        item.check_cls(force=True)

secretballot.enable_voting_on(Item)


# Main code
from sklearn.ensemble import RandomForestClassifier

from data.consts import *
from base import extract

if __name__ == "__main__":
    dt = extract(RandomForestClassifier,
                 CONFLICTS_PATH,
                 CONFLICTS_DATA_TYPES,
                 hasHeaders=CONFLICTS_HAS_HEADER,
                 headers=CONFLICTS_HEADERS)

    print(str(dt))


"""initial database migration

Revision ID: d9781a6cf2e1
Revises:
Create Date: 2019-01-25 11:17:43.281258

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "d9781a6cf2e1"
down_revision = None
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "blacklist_tokens",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("token", sa.String(length=500), nullable=False),
        sa.Column("blacklisted_on", sa.DateTime(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("token"),
    )
    op.create_table(
        "category",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("name", sa.String(length=32), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_category_name"), "category", ["name"], unique=False)
    op.create_table(
        "option",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("title", sa.String(), nullable=True),
        sa.Column("question_id", sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(["question_id"], ["question.id"], use_alter=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_option_title"), "option", ["title"], unique=False)
    op.create_table(
        "user",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("email", sa.String(length=255), nullable=False),
        sa.Column("registered_on", sa.DateTime(), nullable=False),
        sa.Column("admin", sa.Boolean(), nullable=False),
        sa.Column("public_id", sa.String(length=100), nullable=True),
        sa.Column("username", sa.String(length=50), nullable=True),
        sa.Column("password_hash", sa.String(length=100), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("email"),
        sa.UniqueConstraint("public_id"),
        sa.UniqueConstraint("username"),
    )
    op.create_table(
        "vendor",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("name", sa.String(length=32), nullable=True),
        sa.Column("description", sa.String(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_vendor_name"), "vendor", ["name"], unique=False)
    op.create_table(
        "exam",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("name", sa.String(length=32), nullable=True),
        sa.Column("category_id", sa.Integer(), nullable=True),
        sa.Column("vendor_id", sa.Integer(), nullable=True),
        sa.Column("timestamp", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["category_id"], ["category.id"]),
        sa.ForeignKeyConstraint(["vendor_id"], ["vendor.id"]),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_exam_name"), "exam", ["name"], unique=False)
    op.create_table(
        "question",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("title", sa.String(), nullable=True),
        sa.Column("answer_id", sa.Integer(), nullable=True),
        sa.Column("exam_id", sa.Integer(), nullable=True),
        sa.Column("qtype", sa.Integer(), nullable=True),
        sa.Column("timestamp", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["answer_id"], ["option.id"]),
        sa.ForeignKeyConstraint(["exam_id"], ["exam.id"]),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_question_title"), "question", ["title"], unique=False)
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_question_title"), table_name="question")
    op.drop_table("question")
    op.drop_index(op.f("ix_exam_name"), table_name="exam")
    op.drop_table("exam")
    op.drop_index(op.f("ix_vendor_name"), table_name="vendor")
    op.drop_table("vendor")
    op.drop_table("user")
    op.drop_index(op.f("ix_option_title"), table_name="option")
    op.drop_table("option")
    op.drop_index(op.f("ix_category_name"), table_name="category")
    op.drop_table("category")
    op.drop_table("blacklist_tokens")
    # ### end Alembic commands ###


from django.conf import settings
from django.contrib.auth.forms import AuthenticationForm
_nav = ["instructors","classes","projects","tools","about us","join us"]

def nav(request):
    return dict(
        current = request.path.split('/')[1] or 'home',
        nav = _nav,
        STATIC_URL = settings.STATIC_URL,
        auth_form = AuthenticationForm,
        app_path = "/admin/login/",
        next = request.path,
        settings = settings,
        )


__author__ = 'Nick'
import os
from flask import Flask, redirect, request, render_template, url_for, jsonify, Response
from forms import CampaignForm, AssetForm, PageForm, ContainerForm
from model import Page, Container, Campaign, Assets, ContainerAssets
from werkzeug.utils import secure_filename

app = Flask(__name__)
app.secret_key = 'some_sekcret_hablah_bababooey1-023'

IMAGE_UPLOADS = 'static/upload/images/'
VIDEO_UPLOADS = 'static/upload/videos/'

app.config['VIDEOS'] = VIDEO_UPLOADS
app.config['IMAGES'] = IMAGE_UPLOADS

ALLOWED_IMAGES = set(['png', 'jpg', 'jpeg', 'gif'])
ALLOWED_VIDEOS = set(['mp4', 'webm'])

def allowed_image(filename):
    return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_IMAGES

def allowed_video(filename):
    return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_VIDEOS

@app.route('/', methods=['GET', 'POST'])
def index():
    return render_template('base.html')

@app.route('/page/<id>', methods=['GET'])
def page(id):
    form = ContainerForm()
    pg = Page.get(Page.id == id)
    return render_template('page.html', form=form, page=pg)

@app.route('/pages', methods=['GET'])
def pages():
    form = PageForm()
    page = Page.select()
    return render_template('pages.html', pages=page, form=form)

@app.route('/campaigns', methods=['GET'])
def campaigns():
    asset_form = AssetForm()
    form = CampaignForm()
    camps = Campaign.select()
    return render_template('campaigns.html', form=form, asset_form=asset_form, camps=camps)

@app.route('/src/<id>', methods=['GET'])
def src(id):
    camp = Campaign.get(Campaign.id == id)
    js = render_template('test.js', file=("/"+app.config['IMAGES'] + camp.file))
    return Response(js, mimetype='text/javascript')

@app.route('/test/<id>')
def test(id):
    return render_template('test.html', id=id)

@app.route('/_container', methods=['POST'])
def _container():
    form = ContainerForm()
    pageid = request.form["page"]

    if form.validate_on_submit():
        container = Container.create(name=form.name.data, page=pageid)

        return redirect(url_for('page', id=pageid))

    pg = Page.get(Page.id == pageid)
    return render_template('pages.html', page=pg, form=form)

@app.route('/_page', methods=['POST'])
def _page():
    form = PageForm()

    if form.validate_on_submit():
        page = Page.create(name=form.name.data,
                           url=form.url.data)

        return redirect(url_for('pages'))

    page = Page.select()
    return render_template('pages.html', pages=page, form=form)

@app.route('/_asset', methods=['POST'])
def _asset():
    form = AssetForm()
    if form.validate_on_submit():
        file = request.files['file']
        if file and allowed_image(file.filename):
            filename = secure_filename(file.filename)
            directory = app.config['IMAGES']
            if not os.path.exists(directory):
                os.makedirs(directory)
            file.save(os.path.join(directory, filename))
            campaign = request.form["campaign"]

            asset = Assets.create(name=form.name.data,
                                  src=filename,
                                  campaign=campaign,
                                  type=1)

            return redirect(url_for('campaigns'))

    camps = Campaign.select()
    return render_template('campaigns.html', form=CampaignForm(), asset_form=form, camps=camps)

@app.route('/_campaign', methods=['POST', 'GET'])
def _campaign():
    form = CampaignForm()

    if form.validate_on_submit():
        camp = Campaign.create(name=form.name.data,
                               start=form.start.data,
                               end=form.end.data)

        return redirect(url_for('campaigns'))

    camps = Campaign.select()
    return render_template('campaigns.html', form=form, asset_form=AssetForm(), camps=camps)

# START
if __name__ == '__main__':
    app.run(debug=True, host="127.0.0.1")