#!/usr/bin/python3
""" Data Base Storage Engine """

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm.scoping import scoped_session
from os import getenv
from models.base_model import Base
from models.city import City
from models.user import User
from models.place import Place
from models.state import State
from models.amenity import Amenity
from models.review import Review

class DBStorage():
    """ Data Base Storage """
    __engine = None
    __session = None

    def __init__(self):
        """ init """
        self.__engine = create_engine('mysql+mysqldb://{}:{}@{}/{}'.format(
            getenv("HBNB_MYSQL_USER"),
            getenv("HBNB_MYSQL_PWD"),
            getenv("HBNB_MYSQL_HOST"),
            getenv("HBNB_MYSQL_DB")), pool_pre_ping=True)
        Base.metadata.create_all(self.__engine)
        if getenv("HBNB_ENV") == "test":
            Base.metadata.drop_all(self.__engine)

    def all(self, cls=None):
        """ all """
        dict_objects = {}
        if cls is not None:
            for query_obj in self.__session.query(cls).all():
                key = "{}.{}".format(
                    query_obj.__class__.__name__, query_obj.id)
                dict_objects[key] = query_obj
        else:
            all_classes = [State,
                           City, User]
            for clss in all_classes:
                for query_obj in self.__session.query(clss).all():
                    key = "{}.{}".format(
                        query_obj.__class__.__name__, query_obj.id)
                    dict_objects[key] = query_obj
        return dict_objects

    def new(self, obj):
        """ New Object """
        self.__session.add(obj)

    def save(self):
        """ Commit """
        self.__session.commit()

    def delete(self, obj=None):
        """ Delete """
        if obj is not None:
            self.__session.delete(obj)

    def reload(self):
        """ Create data base and new Session """
        Base.metadata.create_all(self.__engine)
        session_factory = sessionmaker(
            bind=self.__engine, expire_on_commit=False,)
        Session = scoped_session(session_factory)
        self.__session = Session()


for n in range(10):
    mod = 10 ** 9 + 7
    count = 0

    for k in range(1, n):
        product = 1
        for i in range(k):
            product *= 1 + n - k - i
            product %= mod
        count += product
        count %= mod
    if n == 1: print(1)
    else: print(count)


import scrapy
from datetime import datetime
from ..helpers import Helpers
from foodwatch.keywords import wordpool,xetidep
from foodwatch.pollocubaBot import PolloBot

class XetidSpider(scrapy.Spider):
    name = "xetid"
    timeflag = False

    def start_requests(self):
        urls = []

        # for keyword in wordpool:
        #     urls.append("https://5tay42.xetid.cu/module/categorysearch/catesearch?search_query="+keyword)
        for tienda in xetidep.keys():
            for dep in xetidep[tienda]:
                url="https://{}.enzona.net/{}".format(tienda,dep)
                # self.tienda=tienda
                self.url=url
                yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):
        count = 0
        tienda=response.request.url.split("/")[2].split(".")[0]
        bot=PolloBot()
        for product in response.css("ul#listado-prod div.product-container"):
            pname = product.css("div.right-block a.product-name").xpath('@title').get()
            pprice = product.css("div.right-block div.content_price span.price::text").get()

            if pname is None or pprice is None: continue

            phash = Helpers.mkhash(pname, pprice)

            if Helpers.ispresent(phash) is False:
                count += 1
                msg="{}\n_{}_\n[{}]({})   ".format(pname,pprice,tienda,response.request.url)
                bot.posttry(msg)
            yield {'place':tienda,'product': pname, 'price': pprice, 'chk': phash,'url':response.request.url }

        # if count > 0: Helpers.firetoast(2, count)


import os
from datetime import timedelta

#INSTALLED_APPS += (
#    'ebagis',
#    'ebagis.data',
#    'ebagis_ui',
#)

# Path where AOI files will be stored
AOI_DIRECTORY = os.path.join(BASE_DIR, 'AOIs')

# Path where AOI and download zips will be temporarily stored unzipped
EBAGIS_TEMP_DIRECTORY = None

# Path where download files will be stored
UPLOADS_DIRECTORY = os.path.join(
    MEDIA_ROOT, "uploads", "%Y", "%m", "%d"
)

# Path where download files will be stored
EBAGIS_DOWNLOADS_DIRECTORY = os.path.join(MEDIA_ROOT, 'downloads2')

# Download Expiration Time
EXPIRATION_DELTA = timedelta(days=1)

# Reference System Well-known ID for geo fields
GEO_WKID = 4326

# AWDB Web Service settings
AWDB_QUERY_URL = 'http://localhost/arcgis/rest/services/AWDB_ALL/stations_USGS_ALL/MapServer/0/query'
# note that the search buffer actually turns into a retangular
# search envelope, so take care in setting as not all found points
# may be within the specified distance from the search point
AWDB_SEARCH_BUFFER = "100 Meters"

# Conf Files
CONF_DIR =  r"C:\ebagis"
DESKTOP_SETTINGS = os.path.join(CONF_DIR, "desktop_settings.yaml")
LAYER_FILE = os.path.join(CONF_DIR, "BAGIS_Reference_Maps.lyr")


from caffe_user import *
import os

# ----- Configuration -----
name = "cnn"
num_epoch = 500
batch_size = 200
fc_type = "TBInnerProduct"
conv_type = "TBConvolution"
tb_param = Parameter('tb_param')
tb_param.add_param_if('full_train', True)
tb_param.add_param_if('use_bias', True)
tb_param.add_param_if('w_binary', True)
tb_param.add_param_if('in_binary', False)
activation_method = "ReLU"
filler_xavier = Filler('xavier')
filler_uniform = Filler('uniform', min_=-0.1, max_=0.1)
filler_constant = Filler('constant')

# --------- Network ----------
# 2x(128C3)-MP2-2x(256C3)-MP2-2x(512C3)-MP2-1024FC-SVM
Net("cifar10_" + name)
data, label = Data([], phase=TRAIN, source="../cifar10_train_lmdb", batch_size=batch_size, backend=Net.LMDB,
                   optional_params=[Transform(mean_file="../mean.binaryproto")])
Data([], phase=TEST, source="../cifar10_test_lmdb", batch_size=100, backend=Net.LMDB,
     optional_params=[Transform(mean_file="../mean.binaryproto")])
out = [data]
label = [label]
out = Conv(out, name='conv1', num_output=128, bias_term=True, kernel_size=3, stride=1, pad=1,
           weight_filler=filler_xavier, bias_filler=filler_constant)
out = Activation(out, name='act1', method=activation_method)
out = BN(out, name='bn1')
out = Conv(out, name='conv2', conv_type=conv_type, num_output=128, bias_term=True, kernel_size=3, stride=1, pad=1,
           weight_filler=filler_xavier, bias_filler=filler_constant, optional_params=[tb_param])
out = Activation(out, name='act2', method=activation_method)
out = Pool(out, name='pool1')

out = BN(out, name='bn3')
out = Conv(out, name='conv3', conv_type=conv_type, num_output=256, bias_term=True, kernel_size=3, stride=1, pad=1,
           weight_filler=filler_xavier, bias_filler=filler_constant, optional_params=[tb_param])
out = Activation(out, name='act3', method=activation_method)
out = BN(out, name='bn4')
out = Conv(out, name='conv4', conv_type=conv_type, num_output=256, bias_term=True, kernel_size=3, stride=1, pad=1,
           weight_filler=filler_xavier, bias_filler=filler_constant, optional_params=[tb_param])
out = Activation(out, name='act4', method=activation_method)
out = Pool(out, name='pool2')

out = BN(out, name='bn5')
out = Conv(out, name='conv5', conv_type=conv_type, num_output=512, bias_term=True, kernel_size=3, stride=1, pad=1,
           weight_filler=filler_xavier, bias_filler=filler_constant, optional_params=[tb_param])
out = Activation(out, name='act5', method=activation_method)
out = BN(out, name='bn6')
out = Conv(out, name='conv6', conv_type=conv_type, num_output=512, bias_term=True, kernel_size=3, stride=1, pad=1,
           weight_filler=filler_xavier, bias_filler=filler_constant, optional_params=[tb_param])
out = Activation(out, name='act6', method=activation_method)
out = Pool(out, name='pool3')

out = BN(out, name='bn7')
out = FC(out, name='fc7', fc_type=fc_type, num_output=1024, bias_term=True, weight_filler=filler_xavier,
         bias_filler=filler_constant, optional_params=[tb_param])
out = Activation(out, name='act7', method=activation_method)
out = FC(out, name='fc8', num_output=10, weight_filler=filler_xavier, bias_term=True, bias_filler=filler_constant)
accuracy = Accuracy(out + label)
loss = HingeLoss(out + label, norm=2)
# loss = SoftmaxWithLoss(out + label)

# ---------- solver ----
solver = Solver().net('./model.prototxt').CPU()
solver.test(test_iter=100, test_interval=1000, test_initialization=False)
num_iter = num_epoch * 50000 / batch_size
solver.train(base_lr=0.001, lr_policy='step', gamma=0.1, stepsize=num_iter / 5, max_iter=num_iter)
# solver.optimizer(type='SGD', momentum=0.9)
solver.optimizer(type='Adam')
solver.display(display=200, average_loss=100)
solver.snapshot(snapshot=10000, snapshot_prefix=name)

model_dir = os.path.join(os.getenv('HOME'), 'cifar10/' + name)
gen_model(model_dir, solver, [0, 2, 4, 6])


# Copyright 2020 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from json import dumps
from textwrap import dedent
from typing import Iterable

import pytest
from pkg_resources import Requirement

from pants.backend.python.macros.pipenv_requirements import PipenvRequirements
from pants.backend.python.target_types import PythonRequirementLibrary, PythonRequirementsFile
from pants.base.specs import AddressSpecs, DescendantAddresses, FilesystemSpecs, Specs
from pants.engine.addresses import Address
from pants.engine.target import Targets
from pants.testutil.rule_runner import QueryRule, RuleRunner

@pytest.fixture
def rule_runner() -> RuleRunner:
    return RuleRunner(
        rules=[QueryRule(Targets, (Specs,))],
        target_types=[PythonRequirementLibrary, PythonRequirementsFile],
        context_aware_object_factories={"pipenv_requirements": PipenvRequirements},
    )

def assert_pipenv_requirements(
    rule_runner: RuleRunner,
    build_file_entry: str,
    pipfile_lock: dict,
    *,
    expected_file_dep: PythonRequirementsFile,
    expected_targets: Iterable[PythonRequirementLibrary],
    pipfile_lock_relpath: str = "Pipfile.lock",
) -> None:
    rule_runner.add_to_build_file("", f"{build_file_entry}\n")
    rule_runner.create_file(pipfile_lock_relpath, dumps(pipfile_lock))
    targets = rule_runner.request(
        Targets,
        [Specs(AddressSpecs([DescendantAddresses("")]), FilesystemSpecs([]))],
    )

    assert {expected_file_dep, *expected_targets} == set(targets)

def test_pipfile_lock(rule_runner: RuleRunner) -> None:
    """This tests that we correctly create a new python_requirement_library for each entry in a
    Pipfile.lock file.

    Edge cases:
    * Develop and Default requirements are used
    * If a module_mapping is given, and the project is in the map, we copy over a subset of the
        mapping to the created target.
    """
    assert_pipenv_requirements(
        rule_runner,
        "pipenv_requirements(module_mapping={'ansicolors': ['colors']})",
        {
            "default": {"ansicolors": {"version": ">=1.18.0"}},
            "develop": {"cachetools": {"markers": "python_version ~= '3.5'", "version": "==4.1.1"}},
        },
        expected_file_dep=PythonRequirementsFile(
            {"sources": ["Pipfile.lock"]}, Address("", target_name="Pipfile.lock")
        ),
        expected_targets=[
            PythonRequirementLibrary(
                {
                    "requirements": [Requirement.parse("ansicolors>=1.18.0")],
                    "dependencies": [":Pipfile.lock"],
                    "module_mapping": {"ansicolors": ["colors"]},
                },
                Address("", target_name="ansicolors"),
            ),
            PythonRequirementLibrary(
                {
                    "requirements": [
                        Requirement.parse("cachetools==4.1.1;python_version ~= '3.5'")
                    ],
                    "dependencies": [":Pipfile.lock"],
                },
                Address("", target_name="cachetools"),
            ),
        ],
    )

def test_properly_creates_extras_requirements(rule_runner: RuleRunner) -> None:
    """This tests the proper parsing of requirements installed with specified extras."""
    assert_pipenv_requirements(
        rule_runner,
        "pipenv_requirements()",
        {
            "default": {"ansicolors": {"version": ">=1.18.0", "extras": ["neon"]}},
            "develop": {
                "cachetools": {
                    "markers": "python_version ~= '3.5'",
                    "version": "==4.1.1",
                    "extras": ["ring", "mongo"],
                }
            },
        },
        expected_file_dep=PythonRequirementsFile(
            {"sources": ["Pipfile.lock"]}, Address("", target_name="Pipfile.lock")
        ),
        expected_targets=[
            PythonRequirementLibrary(
                {
                    "requirements": [Requirement.parse("ansicolors[neon]>=1.18.0")],
                    "dependencies": [":Pipfile.lock"],
                },
                Address("", target_name="ansicolors"),
            ),
            PythonRequirementLibrary(
                {
                    "requirements": [
                        Requirement.parse("cachetools[ring,mongo]==4.1.1;python_version ~= '3.5'")
                    ],
                    "dependencies": [":Pipfile.lock"],
                },
                Address("", target_name="cachetools"),
            ),
        ],
    )

def test_supply_python_requirements_file(rule_runner: RuleRunner) -> None:
    """This tests that we can supply our own `_python_requirements_file`."""
    assert_pipenv_requirements(
        rule_runner,
        dedent(
            """
            pipenv_requirements(
                requirements_relpath='custom/pipfile/Pipfile.lock',
                pipfile_target='//:custom_pipfile_target'
            )

            _python_requirements_file(
                name='custom_pipfile_target',
                sources=['custom/pipfile/Pipfile.lock']
            )
            """
        ),
        {"default": {"ansicolors": {"version": ">=1.18.0"}}},
        expected_file_dep=PythonRequirementsFile(
            {"sources": ["custom/pipfile/Pipfile.lock"]},
            Address("", target_name="custom_pipfile_target"),
        ),
        expected_targets=[
            PythonRequirementLibrary(
                {
                    "requirements": [Requirement.parse("ansicolors>=1.18.0")],
                    "dependencies": ["//:custom_pipfile_target"],
                },
                Address("", target_name="ansicolors"),
            ),
        ],
        pipfile_lock_relpath="custom/pipfile/Pipfile.lock",
    )


#!/usr/bin/env python

from os import path
import logging
import sys

# Use explicit version of python-requests
sys.path.insert(0, path.join(path.dirname(__file__),
                             "../../lib/python/vendor/requests-2.7.0"))
sys.path.insert(0, path.join(path.dirname(__file__), "../../lib/python"))

from balrog.submitter.cli import ReleasePusher

if __name__ == '__main__':

    from argparse import ArgumentParser
    parser = ArgumentParser()
    parser.add_argument("-a", "--api-root", dest="api_root",required=True)
    parser.add_argument("-c", "--credentials-file", dest="credentials_file", required=True)
    parser.add_argument("-u", "--username", dest="username", required=True)
    parser.add_argument("-V", "--version", dest="version", required=True)
    parser.add_argument("-p", "--product", dest="product_name", required=True)
    parser.add_argument("-b", "--build-number", dest="build_number", required=True)
    parser.add_argument("-R", "--rules", dest="rule_ids", action="append", required=True)
    parser.add_argument("-v", "--verbose", dest="verbose", action="store_true")
    args = parser.parse_args()

    logging_level = logging.INFO
    if args.verbose:
        logging_level = logging.DEBUG
    logging.basicConfig(stream=sys.stdout, level=logging_level,
                        format="%(message)s")

    credentials = {}
    execfile(args.credentials_file, credentials)
    auth = (args.username, credentials['balrog_credentials'][args.username])

    pusher = ReleasePusher(args.api_root, auth)
    pusher.run(args.product_name.capitalize(), args.version,
               args.build_number, args.rule_ids)


#!/usr/bin/python3
# -*- coding: utf-8 -*-
from PyQt5.QtCore import QEvent,QObject, pyqtSignal

class ObjectEvent(QObject):
    MouseHovered = pyqtSignal()
    MouseClicked = pyqtSignal()
    MouseReleased = pyqtSignal()
    DoubleClicked = pyqtSignal()
    MouseEnter = pyqtSignal()
    MouseLeave = pyqtSignal()
    mouseMoveEvent = pyqtSignal()

    def __init__(self, parent):
        QObject.__init__(self, parent)
        parent.setMouseTracking(True)
        parent.installEventFilter(self)

    def eventFilter(self, _, event):
        # if event.type() == QEvent.MouseMove:
        #     self.emit(SIGNAL("MouseMove"), event.pos())
        #
        #
        # elif event.type() == QEvent.MouseButtonDblClick:
        #     self.emit(SIGNAL("MouseDoubleClick"), event.pos())

        if event.type() == QEvent.Enter:
            self.MouseEnter.emit()
        elif event.type() == QEvent.Leave:
            self.MouseLeave.emit()
        elif event.type() == QEvent.HoverEnter:
            self.MouseHovered.emit()
        elif event.type() == QEvent.MouseButtonPress:
            self.MouseClicked.emit()
        elif event.type() == QEvent.MouseButtonRelease:
            self.MouseReleased.emit()
        elif event.type() == QEvent.MouseButtonDblClick:
            self.DoubleClicked.emit()
        elif event.type() == QEvent.MouseMove:
            self.mouseMoveEvent.emit()
        return False


import shutil
from datetime import datetime, timedelta

import dateutil
import pytest

from indy_common.config_helper import NodeConfigHelper
from indy_node.test.upgrade.helper import sdk_ensure_upgrade_sent
from sovtoken.constants import ADDRESS, AMOUNT, XFER_PUBLIC
from sovtokenfees.test.constants import NYM_FEES_ALIAS, XFER_PUBLIC_FEES_ALIAS
from sovtokenfees.test.helper import get_amount_from_token_txn, send_and_check_transfer, send_and_check_nym_with_fees

from plenum.common.constants import TXN_TYPE, DATA, VERSION, CURRENT_PROTOCOL_VERSION
from indy_common.constants import NODE_UPGRADE, ACTION, COMPLETE, AUTH_RULE, START
from indy_node.test.helper import sdk_send_and_check_auth_rule_request, TestNode
from plenum.common.request import Request
from plenum.common.types import f
from plenum.test.node_catchup.helper import waitNodeDataEquality

from plenum.test.helper import assertEquality
from plenum.test.test_node import checkNodesConnected, ensure_node_disconnected
from stp_core.loop.eventually import eventually

@pytest.fixture(scope='module')
def fees():
    return {NYM_FEES_ALIAS: 0, XFER_PUBLIC_FEES_ALIAS: 0}

@pytest.fixture()
def addresses(helpers):
    return helpers.wallet.create_new_addresses(4)

@pytest.fixture()
def mint_tokens(helpers, addresses):
    outputs = [{ADDRESS: addresses[0], AMOUNT: 1000}]
    return helpers.general.do_mint(outputs)

@pytest.fixture(scope="module")
def tconf(tconf):
    old_version_matching = tconf.INDY_VERSION_MATCHING
    tconf.INDY_VERSION_MATCHING = {"1.1.50": "1.0.0"}
    yield tconf
    tconf.INDY_VERSION_MATCHING = old_version_matching

@pytest.fixture(scope='module')
def valid_upgrade(nodeSetWithIntegratedTokenPlugin, tconf):
    schedule = {}
    unow = datetime.utcnow().replace(tzinfo=dateutil.tz.tzutc())
    startAt = unow + timedelta(seconds=30000000)
    acceptableDiff = tconf.MinSepBetweenNodeUpgrades + 1
    for n in nodeSetWithIntegratedTokenPlugin[0].poolManager.nodeIds:
        schedule[n] = datetime.isoformat(startAt)
        startAt = startAt + timedelta(seconds=acceptableDiff + 3)

    return dict(name='upgrade', version="10000.10.10",
                action=START, schedule=schedule, timeout=1,
                package=None,
                sha256='db34a72a90d026dae49c3b3f0436c8d3963476c77468ad955845a1ccf7b03f55')

def send_node_upgrades(nodes, version, looper, count=None):
    if count is None:
        count = len(nodes)
    last_ordered = nodes[0].master_last_ordered_3PC[1]
    for node in nodes[:count]:
        op = {
            TXN_TYPE: NODE_UPGRADE,
            DATA: {
                ACTION: COMPLETE,
                VERSION: version
            }
        }
        op[f.SIG.nm] = node.wallet.signMsg(op[DATA])

        request = node.wallet.signRequest(
            Request(operation=op, protocolVersion=CURRENT_PROTOCOL_VERSION))

        node.startedProcessingReq(request.key, node.nodestack.name)
        node.send(request)
        looper.run(eventually(lambda: assertEquality(node.master_last_ordered_3PC[1],
                                                     last_ordered + 1)))
        last_ordered += 1

def test_state_recovery_with_xfer(looper, tconf, tdir,
                                  sdk_pool_handle,
                                  sdk_wallet_trustee,
                                  allPluginsPath,
                                  do_post_node_creation,
                                  nodeSetWithIntegratedTokenPlugin,
                                  helpers,
                                  valid_upgrade,
                                  mint_tokens,
                                  addresses,
                                  fees_set, fees,
                                  monkeypatch):
    version1 = "1.1.50"
    version2 = "1.1.88"
    current_amount = get_amount_from_token_txn(mint_tokens)
    seq_no = 1
    node_set = nodeSetWithIntegratedTokenPlugin

    current_amount, seq_no, _ = send_and_check_nym_with_fees(helpers, fees_set, seq_no, looper, addresses,
                                                             current_amount)
    # send POOL_UPGRADE to write in a ledger
    last_ordered = node_set[0].master_last_ordered_3PC[1]
    sdk_ensure_upgrade_sent(looper, sdk_pool_handle, sdk_wallet_trustee,
                            valid_upgrade)
    looper.run(eventually(lambda: assertEquality(node_set[0].master_last_ordered_3PC[1],
                                                 last_ordered + 1)))

    send_node_upgrades(node_set, version1, looper)
    for n in node_set:
        handler = n.write_manager.request_handlers.get(XFER_PUBLIC)[0]
        handler_for_1_0_0 = n.write_manager._request_handlers_with_version.get((XFER_PUBLIC, "1.0.0"))[0]
        monkeypatch.setattr(handler, 'update_state',
                            handler_for_1_0_0.update_state)

    current_amount, seq_no, _ = send_and_check_transfer(helpers, [addresses[0], addresses[1]], fees_set, looper,
                                                        current_amount, seq_no,
                                                        transfer_summ=current_amount)
    send_node_upgrades(node_set, version2, looper)
    monkeypatch.undo()
    current_amount, seq_no, _ = send_and_check_transfer(helpers, [addresses[1], addresses[0]], fees_set, looper,
                                                        current_amount, seq_no,
                                                        transfer_summ=current_amount)

    node_to_stop = node_set[-1]
    state_db_pathes = [state._kv.db_path
                       for state in node_to_stop.states.values()]
    node_to_stop.cleanupOnStopping = False
    node_to_stop.stop()
    looper.removeProdable(node_to_stop)
    ensure_node_disconnected(looper, node_to_stop, node_set[:-1])

    for path in state_db_pathes:
        shutil.rmtree(path)
    config_helper = NodeConfigHelper(node_to_stop.name, tconf, chroot=tdir)
    restarted_node = TestNode(
        node_to_stop.name,
        config_helper=config_helper,
        config=tconf,
        pluginPaths=allPluginsPath,
        ha=node_to_stop.nodestack.ha,
        cliha=node_to_stop.clientstack.ha)
    do_post_node_creation(restarted_node)

    looper.add(restarted_node)
    node_set[-1] = restarted_node

    looper.run(checkNodesConnected(node_set))
    waitNodeDataEquality(looper, restarted_node, *node_set[:-1], exclude_from_check=['check_last_ordered_3pc_backup'])
    current_amount, seq_no, _ = send_and_check_transfer(helpers, [addresses[0], addresses[1]], {}, looper,
                                                        current_amount, seq_no,
                                                        transfer_summ=1)
    waitNodeDataEquality(looper, restarted_node, *node_set[:-1], exclude_from_check=['check_last_ordered_3pc_backup'])


from __future__ import division, print_function, absolute_import

'''
Author      : Lyubimov, A.Y.
Created     : 10/10/2014
Last Changed: 12/10/2018
Description : IOTA I/O module. Reads PHIL input, also creates reasonable IOTA
              and PHIL defaults if selected.
'''

import os
import iotbx.phil as ip

from iota.components.iota_utils import convert_phil_to_text
from iota.components.iota_processing import cctbx_str

master_phil_str = """
description = None
  .type = str
  .help = Run description (optional).
  .multiple = False
  .optional = True
input = None
  .type = path
  .multiple = True
  .help = Path to folder with raw data in pickle format, list of files or single file
  .help = Can be a tree with folders
  .optional = True
output = $PWD
  .type = path
  .multiple = False
  .help = Base output directory
image_import
  .help = Parameters for image importing (& triage)
{
  image_triage = True
    .type = bool
    .help = Perform a round of spotfinding to see if image has diffraction
  minimum_Bragg_peaks = 10
    .type = int
    .help = Minimum number of Bragg peaks to establish diffraction
  strong_sigma = 5.0
    .type = float
    .help = Sigma level to define a strong reflection
  mask = None
    .type = path
    .help = Mask for ignored pixels
  estimate_gain = False
    .type = bool
    .help = Estimates detector gain (helps improve spotfinding)
  beamstop = 0
    .type = float
    .help = Beamstop shadow threshold, zero to skip
  distance = 0
    .type = float
    .help = Alternate crystal-to-detector distance (set to zero to leave the same)
  beam_center
    .help = Alternate beam center coordinates (in PIXELS)
    .help = Set to zero to leave the same
  {
    x = 0
      .type = int
    y = 0
      .type = int
  }
}
analysis
  .help = "Analysis / visualization options."
{
  run_clustering = False
    .type = bool
    .help = Set to True to turn on hierarchical clustering of unit cells
  cluster_threshold = 5000
    .type = int
    .help = threshold value for unit cell clustering
  cluster_n_images = None
    .type = int
    .help = How many images to cluster? (Useful for huge datasets.)
  cluster_limit = 5
    .type = int
    .help = "Major clusters" are defined as clusters with over n members
  cluster_write_files = True
    .type = bool
    .help = Set to True to write lists of images belonging to major clusters
  viz = None *no_visualization integration cv_vectors
    .type = choice
    .help = Set to "integration" to visualize spotfinding and integration results.
    .help = Set to "cv_vectors" to visualize accuracy of CV vectors
  charts = False
    .type = bool
    .help = If True, outputs PDF files w/ charts of mosaicity, rmsd, etc.
  summary_graphs = False
    .type = bool
    .help = If True: spot-finding heatmap, res. histogram and beamXY graph
}
advanced
  .help = "Advanced, debugging and experimental options."
{
  processing_backend = *cctbx.xfel ha14
    .type = choice
    .help = Choose image processing backend software
  debug = False
    .type = bool
    .help = Used for various debugging purposes.
  experimental = False
    .type = bool
    .help = Set to true to run the experimental section of codes
  prime_prefix = prime
    .type = str
    .help = Prefix for the PRIME script filename
  temporary_output_folder = None
    .type = path
    .help = If None, temp output goes to <output>/integration/###/tmp/
  image_range
    .help = Use a range of images, e.g. 5 - 1000
  {
    flag_on = False
      .type = bool
    range = None
      .type = str
      .help = Can input multiple ranges, e.g. "5, 60-100, 200-1500"
  }
  random_sample
    .help = Use a randomized subset of images (or -r <number> option)
  {
    flag_on = False
      .type = bool
    number = 0
      .type = int
      .help = Number of images in random sample
  }
}
mp
  .help = Multiprocessing options
{
  n_processors = 0
    .type = int
    .help = No. of processing units
  method = *multiprocessing mpi lsf torq custom
    .type = choice
    .help = Multiprocessing method
  queue = None
    .type = str
    .help = Multiprocessing queue
  submit_command = None
    .type = str
    .help = Command to submit IOTA job to a queue
  kill_command = None
    .type = str
    .help = Command to kill the current IOTA job submitted to a queue
}
"""

master_phil = ip.parse(master_phil_str + cctbx_str, process_includes=True)

def get_input_phil(input_source=None, phil_args=None, mode='auto',
                   ha14=False, gui=False):
  ''' Generate PHIL from file, master, and/or command arguments
  :param args: command line arguments
  :param phil_args: PHIL settings as command line arguments
  :param input_source: file with input settings in PHIL format
  :param mode:
  :return:
  '''
  from libtbx.phil.command_line import argument_interpreter
  from libtbx.utils import Sorry

  # Depending on mode, either read input from file, or generate defaults
  if mode == 'file':
    with open(input_source, 'r') as inpf:
      user_phil = ip.parse(inpf.read())
    phil_fixer = PHILFixer()
    working_phil = phil_fixer.run(old_phil=user_phil, write_file=True)
  else:
    if ha14:
      from iota.components.iota_cctbx_ha14 import ha14_str
      working_phil = ip.parse(master_phil_str + ha14_str, process_includes=True)
    else:
      working_phil = master_phil

  if gui:
    from libtbx.phil import find_scope
    if not find_scope(working_phil, 'gui'):
      from iota.components.iota_ui_base import gui_phil
      working_phil.adopt_scope(gui_phil)

  # Parse in-line params into phil
  if phil_args:
    argument_interpreter = argument_interpreter(master_phil=working_phil)
    for arg in phil_args:
      try:
        command_line_params = argument_interpreter.process(arg=arg)
        working_phil = working_phil.fetch(sources=[command_line_params,])
        phil_args.remove(arg)
      except Sorry:
        pass
    if len(phil_args) > 0:
      raise Sorry("Not all arguments processed, remaining: {}".format(phil_args))

  # Self-fetch to resolve variables
  return working_phil.fetch(source=working_phil)

def process_ui_input(args, phil_args, input_source, mode='auto'):
  ''' Read and parse parameter file and/or command-line args for IOTA GUI

  :param args: command-line arguments upon launch
  :param phil_args: command-line arguments pertaining to IOTA parameters
  :param input_source: text file with IOTA parameters
  :return:
  '''
  working_phil = get_input_phil(phil_args=phil_args, input_source=input_source,
                                mode=mode, gui=True)
  params = working_phil.extract()

  # Check for -r option and set random subset parameter
  if args.random > 0:
    params.advanced.random_sample.flag_on = True
    params.advanced.random_sample.number = args.random[0]

  if args.watch > 0:
    params.gui.monitor_mode = True
    params.gui.monitor_mode_timeout = True
    params.gui.monitor_mode_timeout_length = args.watch[0]

  if args.tmp is not None:
    params.advanced.temporary_output_folder = args.tmp[0]

  # Check for -n option and set number of processors override
  # (for parallel map only, for now)
  from multiprocessing import cpu_count
  max_proc = cpu_count() - 2
  if args.nproc > 0:
    if args.nproc >= max_proc:
      params.mp.n_processors = max_proc
    else:
      params.mp.n_processors = args.nproc[0]
  elif params.mp.method == 'multiprocessing':
    if (params.mp.n_processors >= max_proc or
            params.mp.n_processors == 0):
      params.mp.n_processors = int(max_proc / 2)

  return working_phil.format(python_object=params)

def process_input(args, phil_args, input_source, mode='auto', now=None):
  """ Read and parse parameter file and/or command-line args; if none found,
  create a default parameter object

  :param args: command-line arguments upon launch
  :param phil_args: command-line arguments pertaining to IOTA parameters
  :param input_source: text file with IOTA parameters (if 'file' mode) or
                       source of images (if 'auto' mode)
  :param mode: Mode of XtermIOTA run. See the InitAll base class
  :param now: date / time stamp
  :return: PHIL-formatted parameters
  """

  working_phil = get_input_phil(phil_args=phil_args, ha14=args.ha14,
                                mode=mode, input_source=input_source)

  # Perform command line check and modify params accordingly
  params = working_phil.extract()
  if args.ha14:
    params.advanced.processing_backend = 'ha14'

  if mode == 'auto':
    output_dir = os.path.abspath(os.curdir)
    target_filename = '{}.phil'.format(
      params.advanced.processing_backend.replace('.', '_'))
    params.cctbx_xfel.target = os.path.join(output_dir, target_filename)
    params.description = 'IOTA parameters auto-generated on {}'.format(now)
    params.input = [input_source]
    params.output = output_dir

  # Check for -r option and set random subset parameter
  if args.random > 0:
    params.advanced.random_sample.flag_on = True
    params.advanced.random_sample.number = args.random[0]

  # Check for -n option and set number of processors override
  # (for parallel map only, for now)
  from multiprocessing import cpu_count
  max_proc = cpu_count() - 2
  if args.nproc > 0:
    if args.nproc >= max_proc:
      params.mp.n_processors = max_proc
    else:
      params.mp.n_processors = args.nproc[0]
  elif params.mp.method == 'multiprocessing':
    if (params.mp.n_processors >= max_proc or
            params.mp.n_processors == 0):
      params.mp.n_processors = int(max_proc / 2)

  final_phil = working_phil.format(python_object=params)

  if mode == 'auto':
    write_defaults(os.path.abspath(os.curdir), final_phil.as_str(),
                   params.advanced.processing_backend)

  return final_phil

def write_defaults(current_path=None, txt_out=None, method='cctbx_xfel',
                   write_target_file=True, write_param_file=True,
                   filepath=None):
  """ Generates list of default parameters for a reasonable target file:
        - old cctbx.xfel (HA14) now deprecated, but can still be used
        - also writes out the IOTA parameter file.

  :param current_path: path to current output folder
  :param txt_out: text of IOTA parameters
  :param method: which backend is used (default = cctbx.xfel AB18)
  :param write_target_file: write backend parameters to file
  :param write_param_file: write IOTA parameters to file
  :return: default backend settings as list, IOTA parameters as string
  """

  if filepath:
    def_target_file = filepath
  else:
    def_target_file = '{}/{}.phil' \
                      ''.format(current_path, method.replace('.', '_'))

  if method.lower() in ('cctbx', 'ha14'):
    # This is a deprecated backend from 2014; no longer recommended, may suck
    default_target_str = '''# -*- mode: conf -*-
difflimit_sigma_cutoff = 0.01
distl {
  peak_intensity_maximum_factor = 1000
  spot_area_maximum_factor = 20
  compactness_filter = False
  method2_cutoff_percentage = 2.5
}
integration {
  background_factor = 2
  enable_one_to_one_safeguard = True
  model = user_supplied
  spotfinder_subset = spots_non-ice
  mask_pixel_value = -2
  greedy_integration_limit = True
  combine_sym_constraints_and_3D_target = True
  spot_prediction = dials
  guard_width_sq = 4.
  mosaic {
    refinement_target = LSQ *ML
    domain_size_lower_limit = 4.
    enable_rotational_target_highsym = True
  }
}
mosaicity_limit = 2.0
distl_minimum_number_spots_for_indexing = 16
distl_permit_binning = False
beam_search_scope = 0.5'''

    # default_target = ['# -*- mode: conf -*-',
    #                       'difflimit_sigma_cutoff = 0.01',
    #                       'distl{',
    #                       '  peak_intensity_maximum_factor=1000',
    #                       '  spot_area_maximum_factor=20',
    #                       '  compactness_filter=False',
    #                       '  method2_cutoff_percentage=2.5',
    #                       '}',
    #                       'integration {',
    #                       '  background_factor=2',
    #                       '  enable_one_to_one_safeguard=True',
    #                       '  model=user_supplied',
    #                       '  spotfinder_subset=spots_non-ice',
    #                       '  mask_pixel_value=-2',
    #                       '  greedy_integration_limit=True',
    #                       '  combine_sym_constraints_and_3D_target=True',
    #                       '  spot_prediction=dials',
    #                       '  guard_width_sq=4.',
    #                       '  mosaic {',
    #                       '    refinement_target=LSQ *ML',
    #                       '    domain_size_lower_limit=4.',
    #                       '    enable_rotational_target_highsym=True',
    #                       '  }',
    #                       '}',
    #                       'mosaicity_limit=2.0',
    #                       'distl_minimum_number_spots_for_indexing=16',
    #                       'distl_permit_binning=False',
    #                       'beam_search_scope=0.5'
    #                       ]
    # default_target_str = '\n'.join(default_target)
  elif method.lower() in ('dials', 'cctbx.xfel'):
    default_target_str = '''verbosity=10
                            spotfinder {
                              threshold {
                                dispersion {
                                  gain = 1
                                  sigma_strong = 3
                                  global_threshold = 0
                                  }
                               }
                            }
                            geometry {
                              detector {
                                distance = None
                                slow_fast_beam_centre = None
                              }
                            }
                            indexing {
                              refinement_protocol {
                                n_macro_cycles = 1
                                d_min_start = 2.0
                              }
                              basis_vector_combinations.max_combinations = 10
                              stills {
                                indexer = stills
                                method_list = fft1d real_space_grid_search
                              }
                            }
                            refinement {
                              parameterisation {
                                beam.fix = *all in_spindle_plane out_spindle_plane wavelength
                                detector  {
                                  fix = all position orientation
                                  hierarchy_level=0
                                }
                                auto_reduction {
                                  action=fix
                                  min_nref_per_parameter=1
                                }
                              }
                              reflections {
                                outlier.algorithm=null
                                weighting_strategy  {
                                  override=stills
                                  delpsi_constant=1000000
                                }
                              }
                            }
                            integration {
                              integrator=stills
                              profile.fitting=False
                              background {
                                simple {
                                  outlier {
                                    algorithm = null
                                  }
                                }
                              }
                            }
                            profile {
                              gaussian_rs {
                                min_spots.overall = 0
                              }
                            }
                            '''

  target_phil = ip.parse(default_target_str)
  if write_target_file:
    with open(def_target_file, 'w') as targ:
      targ.write('{}'.format(target_phil.as_str()))

  if write_param_file:
    with open('{}/iota.param'.format(current_path), 'w') as default_param_file:
      default_param_file.write(txt_out)

  return target_phil.as_str(), txt_out

def print_params():
  """ Print out master parameters for IOTA """

  help_out = convert_phil_to_text(master_phil, att_level=1)
  txt_out = convert_phil_to_text(master_phil)

  return help_out, txt_out

# -------------------------- Backwards Compatibility ------------------------- #

class PHILFixer():
  ''' Class for backwards compatibility; will read old IOTA parameters and
  convert to current IOTA parameters (PHIL format ONLY). Can optionally
  output a new param file. Will run automatically most of the time, but can
  also be called from command-line '''

  def __init__(self):

    self.diffs = [
      ('image_triage.min_Bragg_peaks', 'image_import.miminum_Bragg_peaks'),
      ('cctbx_xfel.minimum_Bragg_peaks', 'image_import.miminum_Bragg_peaks'),
      ('cctbx_xfel.strong_sigma', 'image_import.strong_sigma'),
      ('cctbx_xfel.selection.min_sigma', 'image_import.strong_sigma'),
      ('image_conversion', 'image_import'),
      ('integrate_with', 'processing_backend'),
      ('n_processors', 'mp.n_processors'),
      ('mp_method', 'mp.method'),
      ('mp_queue', 'mp.queue'),
      ('advanced.image_viewer', 'gui.image_viewer'),
      ('advanced.monitor_mode', 'gui.monitor_mode')
    ]

    self.word_diffs = [
      ('cctbx_ha14.image_conversion.square_mode', "None", 'no_modification'),
      ('cctbx_ha14.image_triage.type', "None", 'no_triage'),
      ('cctbx_ha14.grid_search.type', "None", 'no_grid_search'),
      ('analysis.viz', "None", "no_visualization"),
      ('processing_backend', 'dials', 'cctbx.xfel'),
      ('processing_backend', 'cctbx', 'ha14')
    ]

  def read_in_phil(self, old_phil, current_phil):
    """ Extract parts of incoming PHIL not recognizable to current PHIL """
    new_phil, wrong_defs = current_phil.fetch(old_phil,
                                              track_unused_definitions=True)
    return new_phil, wrong_defs

  def make_changes(self, wrong_defs, backend='cctbx_xfel'):
    """ Find paths that have changed and change them to new paths

    :return: 'user phil' object with corrected paths
    """
    from libtbx.phil import strings_from_words
    fix_txt = ''
    fixed_defs = []

    # Change old paths to new paths
    for l in wrong_defs:
      path = l.path
      for fix in self.diffs:
        if fix[0] in path:
          new_path = path.replace(fix[0], fix[1])
          value = strings_from_words(l.object.words)
          value = self.check_values(new_path, value)
          if type(value) == list:
            value = ' '.join(value)
          entry = '{} = {}\n'.format(new_path, value)
          fix_txt += entry
          fixed_defs.append(l)

    # Change backend to the appropriate version
    remaining_defs = list(set(wrong_defs) - set(fixed_defs))
    if backend == 'ha14':
      backend_defs = ['cctbx', 'cctbx_ha14']
    else:
      backend_defs = ['dials']
    for r in remaining_defs:
      path = r.path
      for bd in backend_defs:
        if bd in path:
          new_path = path.replace(bd, 'cctbx_xfel')
          value = strings_from_words(r.object.words)
          value = self.check_values(new_path, value)
          if type(value) == list:
            value = ' '.join(value)
          entry = '{} = {}\n'.format(new_path, value)
          fix_txt += entry

    return ip.parse(fix_txt)

  def check_values(self, path, value):
    for wd in self.word_diffs:
      if wd[0] in path:
        if type(value) == list:
          for v in value:
            if wd[1] == v.replace('*', ''):
              value[value.index(v)]= v.replace(wd[1], wd[2])
              break
        else:
          value = wd[2]
    return value

  # def fix_ha14_params(self, params):
  #   # Renaming of imported images
  #   if not hasattr(params.cctbx_ha14.image_conversion, 'rename_pickle'):
  #     prefix = params.cctbx_ha14.image_conversion.rename_pickle_prefix
  #     if 'none' in str(prefix).lower():
  #       params.cctbx_ha14.image_conversion.__inject__('rename_pickle',
  #                                          'keep_file_structure')
  #       params.cctbx_ha14.image_conversion.rename_pickle_prefix = None
  #     elif 'auto' in str(prefix).lower():
  #       params.cctbx_ha14.image_conversion.__inject__('rename_pickle', 'auto_filename')
  #       params.cctbx_ha14.image_conversion.rename_pickle_prefix = None
  #     else:
  #       params.cctbx_ha14.image_conversion.__inject__('rename_pickle', 'custom_filename')
  #       if hasattr(prefix, '__iter__'):
  #         if len(prefix) > 1:
  #           params.cctbx_ha14.image_conversion.rename_pickle_prefix = \
  #             [i for i in prefix if "*" in i][0].replace('*', '')
  #         else:
  #           params.cctbx_ha14.image_conversion.rename_pickle_prefix = prefix[0]
  #
  #   if str(params.cctbx_ha14.image_conversion.square_mode).lower() == 'none':
  #     params.cctbx_ha14.image_conversion.square_mode = 'no_modification'
  #
  #   if str(params.cctbx_ha14.image_triage.type).lower() == 'none':
  #     params.cctbx_ha14.image_triage.type = 'no_triage'
  #
  #   if str(params.cctbx_ha14.grid_search.type).lower() == 'none':
  #     params.cctbx_ha14.grid_search.type = 'no_grid_search'
  #
  # def fix_old_phil(self, phil):
  #   """ Backwards compatibility: convert settings from old format to new """
  #
  #   temp_phil = master_phil.fetch(source=phil)
  #   params = temp_phil.extract()
  #
  #   if str(params.analysis.viz).lower() == 'none':
  #     params.analysis.viz = 'no_visualization'
  #
  #   fixed_phil = temp_phil.format(python_object=params)
  #
  #   return fixed_phil

  def determine_backend(self, phil=None, prm=None):
    # Try to get parameter for backend type (for backwards compatibility)
    if phil and not prm:
      prm = phil.extract()

    backend = None
    try:
      backend = prm.advanced.processing_backend
    except AttributeError:
      backend = prm.advanced.integrate_with
    finally:
      if backend and type(backend) == list:
        for b in backend:
          if b.startswith('*'):
            backend = b[1:]
            break
      return backend

  def create_current_phil(self, phil):
    # Create master PHIL based on backend and settings
    if phil.__class__.__name__ == 'scope_extract':
      backend = self.determine_backend(prm=phil)
    elif phil.__class__.__name__ == 'scope':
      backend = self.determine_backend(phil=phil)
    else:
      return None

    if not backend:
      return None

    if backend.lower() in ('ha14', 'cctbx'):
      from iota.components.iota_cctbx_ha14 import ha14_str
      new_phil = ip.parse(master_phil_str + ha14_str)
    else:
      new_phil = master_phil

    # Append GUI PHIL if it should be there:
    from libtbx.phil import find_scope
    gui_phil = find_scope(phil, 'gui')
    if gui_phil:
      from iota.components.iota_ui_base import gui_phil
      new_phil.adopt_scope(gui_phil)

    return new_phil

  def run(self, old_phil, current_phil=None, write_file=False):

    if not current_phil:
      current_phil = self.create_current_phil(phil=old_phil)

    # Get mismatched definitions
    new_phil, wrong_defs = self.read_in_phil(old_phil=old_phil,
                                             current_phil=current_phil)

    # If no mismatches found, return PHIL, else, fix it
    if not wrong_defs:
      fixed_phil = new_phil
    else:
      backend = self.determine_backend(phil=current_phil)
      phil_fixes = self.make_changes(wrong_defs, backend=backend)
      fixed_phil = new_phil.fetch(source=phil_fixes)

      if write_file:
        fixed_filepath = '{}/iota_fixed.param'.format(
          os.path.abspath(os.curdir))
        with open(fixed_filepath, 'w') as default_param_file:
          default_param_file.write(fixed_phil.as_str())

    return fixed_phil


DEBUG = 1
if __name__ == '__main__':
    N = int(input())

    lListResult = []
    iList = []
    for i in range (N) :
      str = input()
      iList = str.split(' ')
      if iList[0] == 'insert' :
        lListResult.insert(int(iList[1]),int(iList[2]))
      if iList[0] == 'print' :
        print(lListResult)
      if iList[0] == 'remove' :
        lListResult.remove(int(iList[1]))
      if iList[0] == 'append' :
        lListResult.append(int(iList[1]))
      if iList[0] == 'sort' :
        lListResult.sort()
      if iList[0] == 'pop' :
        lListResult.pop()
      if iList[0] == 'reverse' :
        lListResult.reverse()


from Kalman import KalmanAngle
from Phidget22.Phidget import *
from Phidget22.Devices.Accelerometer import *
from Phidget22.Devices.Gyroscope import *
import time
import math
import sys

class PhidgetKalman(object):
	#Read the gyro and acceleromater values from MPU6050
	def __init__(self):
		self.kalmanX = KalmanAngle()
		self.kalmanY = KalmanAngle()

		self.RestrictPitch = True	#Comment out to restrict roll to ±90deg instead - please read: http://www.freescale.com/files/sensors/doc/app_note/AN3461.pdf
		self.radToDeg = 57.2957786
		self.kalAngleX = 0
		self.kalAngleY = 0
		try:
			self.accel = Accelerometer() #ch.getAcceleration()
			self.gyro  = Gyroscope()	  #ch.getAngularRate()
		except Exception as e:
			print("Cannot initalize, try checking you have the Phidget22 library installed.")
			print("Error:", e)
			sys.exit()
		# return self.accel, self.gyro

	def start(self):
		try:
			self.accel.openWaitForAttachment(1000)
			self.gyro.openWaitForAttachment(1000)
		except Exception as e:
			print("Issue with attaching to IMU. The IMU maybe connected to something else right now..")
			print("Error:", e)
			sys.exit()
	def stop(self):
		self.accel.close()
		self.gyro.close()
		print("Stopping")
	def get_angles(self, measure_time=5):
		#Keep going
		time.sleep(1)
		accX, accY, accZ = self.accel.getAcceleration()
		if (self.RestrictPitch):
			roll = math.atan2(accY,accZ) * self.radToDeg
			pitch = math.atan(-accX/math.sqrt((accY**2)+(accZ**2))) * self.radToDeg
		else:
			roll = math.atan(accY/math.sqrt((accX**2)+(accZ**2))) * self.radToDeg
			pitch = math.atan2(-accX,accZ) * self.radToDeg
		# print(roll)
		self.kalmanX.setAngle(roll)
		self.kalmanX.setAngle(pitch)
		gyroXAngle = roll;
		gyroYAngle = pitch;
		compAngleX = roll;
		compAngleY = pitch;

		timer = time.time()
		compare_timer = time.time()
		flag = 0
		try:
			while int(time.time()) < int(measure_time) + int(compare_timer): #Should only run for about 5 seconds <- Could make lower

				#Read Accelerometer raw value
				accX, accY, accZ = self.accel.getAcceleration()

				#Read Gyroscope raw value
				gyroX, gyroY, gyroZ = self.gyro.getAngularRate()

				dt = time.time() - timer
				timer = time.time()

				if (self.RestrictPitch):
					roll = math.atan2(accY,accZ) * self.radToDeg
					pitch = math.atan(-accX/math.sqrt((accY**2)+(accZ**2))) * self.radToDeg
				else:
					roll = math.atan(accY/math.sqrt((accX**2)+(accZ**2))) * self.radToDeg
					pitch = math.atan2(-accX,accZ) * self.radToDeg

				gyroXRate = gyroX/131
				gyroYRate = gyroY/131

				if (self.RestrictPitch):

					if((roll < -90 and self.kalAngleX >90) or (roll > 90 and self.kalAngleX < -90)):
						self.kalmanX.setAngle(roll)
						complAngleX = roll
						self.kalAngleX   = roll
						gyroXAngle  = roll
					else:
						self.kalAngleX = self.kalmanX.getAngle(roll,gyroXRate,dt)

					if(abs(self.kalAngleX)>90):
						gyroYRate  = -gyroYRate
						self.kalAngleY  = self.kalmanX.getAngle(pitch,gyroYRate,dt)
				else:

					if((pitch < -90 and self.kalAngleY >90) or (pitch > 90 and self.kalAngleY < -90)):
						self.kalmanX.setAngle(pitch)
						complAngleY = pitch
						self.kalAngleY   = pitch
						gyroYAngle  = pitch
					else:
						self.kalAngleY = self.kalmanX.getAngle(pitch,gyroYRate,dt)

					if(abs(self.kalAngleY)>90):
						gyroXRate  = -gyroXRate
						self.kalAngleX = self.kalmanX.getAngle(roll,gyroXRate,dt)

				#angle = (rate of change of angle) * change in time
				gyroXAngle = gyroXRate * dt
				gyroYAngle = gyroYAngle * dt

				#compAngle = constant * (old_compAngle + angle_obtained_from_gyro) + constant * angle_obtained from accelerometer
				compAngleX = 0.93 * (compAngleX + gyroXRate * dt) + 0.07 * roll
				compAngleY = 0.93 * (compAngleY + gyroYRate * dt) + 0.07 * pitch

				if ((gyroXAngle < -180) or (gyroXAngle > 180)):
					gyroXAngle = self.kalAngleX
				if ((gyroYAngle < -180) or (gyroYAngle > 180)):
					gyroYAngle = self.kalAngleY

				self.X_angle = self.kalAngleX
				self.Y_angle = self.kalAngleY
				#print("Angle X: " + str(self.kalAngleX)+"   " +"Angle Y: " + str(self.kalAngleY))
				#print(str(roll)+"  "+str(gyroXAngle)+"  "+str(compAngleX)+"  "+str(self.kalAngleX)+"  "+str(pitch)+"  "+str(gyroYAngle)+"  "+str(compAngleY)+"  "+str(self.kalAngleY))
				time.sleep(0.005)

		except KeyboardInterrupt:
			print("test")
		return self.X_angle, self.Y_angle

if __name__ == "__main__":
	obj = PhidgetKalman()
	obj.start()
	print(obj.get_angles(measure_time=5)) #5 seconds seems to be a good amount of time. 4 is okay. Anything lower doesn't work well.
	obj.stop()
	print('End')


import requests
from urllib.parse import urlparse
from requests.auth import HTTPBasicAuth

def register_store(project):
    if not project.api_url:
        return False, 'You did not provide any API url'

    root = 'https://{}.api.mailchimp.com/3.0/'.format(urlparse(project.api_url).netloc.split('.')[0])
    endpoint = 'ecommerce/stores/'

    payload = {
        'id': project.token,
        'list_id': project.list.unique_id,
        'name': project.name,
        'currency_code': project.list.currency_code
    }

    if not project.pk:
        response = requests.post(root + endpoint, json=payload, auth=HTTPBasicAuth('', project.api_key))
    else:
        response = requests.patch(root + endpoint + str(project.token), json=payload, auth=HTTPBasicAuth('', project.api_key))

    if response.ok:
        return True, ''
    return False, response.json()['detail']

def delete_store(project):
    root = 'https://{}.api.mailchimp.com/3.0/'.format(urlparse(project.api_url).netloc.split('.')[0])
    endpoint = 'ecommerce/stores/'

    response = requests.delete(root + endpoint + str(project.token), auth=HTTPBasicAuth('', project.api_key))

    if response.ok:
        return True, ''
    return False, response.json()['detail']


# syntax analysis
# author: yjch
# date: 2018.6.6
# ==========================================================================================

import LexicalAnalysis.lexical_analysis as ls

sym = [((0,0),0)]
variableTable = []  #存放Var
procedureTable = [] #存放Pro
proCurrent = ['global'] #函数调用栈

class Var:
    vname = ''
    vproc = ''
    vkind = 0  #0指变量，1指形参
    vtype = 'integer'
    vlev = 0 #变量层次
    vadr = 0 #变量在变量表中的位置
    defi = False #若形参已在括号中声明，但还未定义，则为false

class Pro:
    pname = ''
    ptype = 'integer'
    plev = 0
    fadr = 0
    ladr = 0

Level = 0  #层数

def checkVar(var): #检查符号有无重复定义
    for i in range(len(variableTable)):
        if variableTable[i].vname == var.vname:
            if variableTable[i].vkind == 1 and variableTable[i].defi == False:
                variableTable[i].defi == True
                return 1 #未重复定义,但是变量名表里面已经有了
            return 0 #重复定义
    for j in range (len(procedureTable)):
        if procedureTable[j].pname == var.vname:
            return 0
    return 2 #未重复定义

def checkVarUdef(varName):#检查变量是否未定义
    for i in range(len(variableTable)):
        if variableTable[i].vname == varName:
            return True #已定义
    for j in range(len(procedureTable)):
        if procedureTable[j].pname == varName:
            return True
    return False #未定义

def checkUdef(varName):
    if not checkVarUdef(varName):
        error('Line %d: 变量%s未定义' % (sym[0][1], ls.formatOutputVar(sym[0])))

def checkExe(infoList): #检查是否时执行语句
    if infoList[0][0][0] == ls.RESERVED_WORD_DICT['read'] or infoList[0][0][0] == ls.RESERVED_WORD_DICT['write'] or infoList[0][0][0] == ls.RESERVED_WORD_DICT['if'] or infoList[1][0][0] == ls.OPERATOR_DICT[':=']:
        return True
    return False

def changeForm(infoList):
    return list(filter(lambda x: x[0][0] != ls.OTHER_DICT['EOLN'],infoList))

def error(string):
    fw = open('error_information.err','a')
    fw.write(string)
    fw.write('\n')
    fw.close
    print(string);
    return

def advance(infoList):
    global sym
    if  not infoList:
        raise ValueError('程序残缺')
    sym[0] = infoList.pop(0)
    #print(sym)
    return sym[0]

def Program(infoList):
    try:
        Subprogram(infoList)
        if infoList[0][0][0] != ls.OTHER_DICT['EOF']:
            error('Line %d: 程序格式错，不被期待的字符' % infoList[0][1])
    except ValueError as s:
        fw = open('error_information.err','a')
        fw.write(str(s))
        fw.write('\n')
        fw.close
        print(s)
    return

def Subprogram(infoList):
    global sym,Level
    whetherExe = checkExe(infoList)
    if advance(infoList)[0][0] != ls.RESERVED_WORD_DICT['begin']:
        error('Line %d: 子程序缺少begin' % sym[0][1])
        if sym[0][0][0] == ls.RESERVED_WORD_DICT['integer'] or whetherExe:
            infoList.insert(0, sym[0])
    Level +=1
    if not checkExe(infoList):
        StatementTable(infoList)
        if advance(infoList)[0][0] != ls.OPERATOR_DICT[';']:
            error('Line %d: 子程序说明语句表后缺少分号' % sym[0][1])
            infoList.insert(0, sym[0])
    ExecutionTable(infoList)
    advance(infoList)
    if sym[0][0][0] != ls.RESERVED_WORD_DICT['end']:
            error('Line %d: 子程序缺少end' % sym[0][1])
    Level -=1
    return

def StatementTable(infoList):
    Statement(infoList)
    while infoList[0][0][0] == ls.OPERATOR_DICT[';']: #因为说明语句和函数说明里面都是用;分隔
        if infoList[1][0][0] == ls.RESERVED_WORD_DICT['integer']:
            advance(infoList)
            Statement(infoList)
            continue
        break
    return

def Statement(infoList):
    global sym
    advance(infoList)
    if sym[0][0][0] != ls.RESERVED_WORD_DICT['integer']:
        error('Line %d: 说明语句格式错,缺少integer' % sym[0][1])
        #infoList.insert(0, sym[0])
    if infoList[0][0][0] == ls.RESERVED_WORD_DICT['function']:
        advance(infoList)
        Identifier(infoList)

        pro = Pro()
        pro.pname = ls.symbolTable[sym[0][0][1]]
        pro.plev = Level
        proCurrent.append(pro.pname)

        if advance(infoList)[0][0] != ls.OPERATOR_DICT['(']:
            error('Line %d: 函数说明缺少左括号' % sym[0][1])
            infoList.insert(0, sym[0])
        if infoList[0][0][0] ==  ls.OPERATOR_DICT[')']:
            advance(infoList)
            pro.fadr = variableTable[-1].vadr + 1
            if advance(infoList)[0][0] != ls.OPERATOR_DICT[';']:
                error('Line %d: 函数说明缺少分号' % sym[0][1])
                infoList.insert(0, sym[0])
            procedureTable.append(pro)
            FunctionBody(infoList)
            procedureTable[-1].ladr = variableTable[-1].vadr
            proCurrent.pop()
            return
        Parameter(infoList)

        var = Var()
        var.vname = ls.symbolTable[sym[0][0][1]]
        var.vproc = proCurrent[-1]
        var.vkind = 1
        var.vlev = Level + 1
        var.vadr = len(variableTable)
        if checkVar(var) == 0:
            error('Line %d: 变量%s重复定义' % (sym[0][1], ls.formatOutputVar(sym[0])))
        variableTable.append(var)
        pro.fadr = var.vadr

        if advance(infoList)[0][0] != ls.OPERATOR_DICT[')']:
            error('Line %d: 函数说明缺少右括号' % sym[0][1])
            infoList.insert(0, sym[0])
        if advance(infoList)[0][0] != ls.OPERATOR_DICT[';']:
            error('Line %d: 函数说明缺少分号' % sym[0][1])
            infoList.insert(0, sym[0])
        procedureTable.append(pro)
        FunctionBody(infoList)
        procedureTable[-1].ladr = variableTable[-1].vadr
        proCurrent.pop()
        return
    Variable(infoList)

    var = Var()
    var.vname = ls.symbolTable[sym[0][0][1]]
    var.vproc = proCurrent[-1]
    var.vlev = Level
    var.vadr = len(variableTable)
    if checkVar(var) == 0:
        error('Line %d: 变量%s重复定义' % (sym[0][1], ls.formatOutputVar(sym[0])))
    if checkVar(var) == 1:
        return
    variableTable.append(var)

    return

def Variable(infoList):
    Identifier(infoList)
    return

def Parameter(infoList):
    Variable(infoList)
    return

def FunctionBody(infoList):
    global Level
    whetherExe = checkExe(infoList)
    if advance(infoList)[0][0] != ls.RESERVED_WORD_DICT['begin']:
        error('Line %d: 函数体缺少begin' % sym[0][1])
        if sym[0][0][0] == ls.RESERVED_WORD_DICT['integer'] or whetherExe():
            infoList.insert(0, sym[0])
    Level +=1
    if not checkExe(infoList):
        StatementTable(infoList)
        if advance(infoList)[0][0] != ls.OPERATOR_DICT[';']:
            error('Line %d: 函数体说明语句表缺少分号' % sym[0][1])
            infoList.insert(0, sym[0])
    ExecutionTable(infoList)
    if advance(infoList)[0][0] != ls.RESERVED_WORD_DICT['end']:
        error("Line %d: 函数体缺少end" % sym[0][1])
        infoList.insert(0, sym[0])
    Level -= 1
    return

def ExecutionTable(infoList):
    Execution(infoList)
    while infoList[0][0][0] == ls.OPERATOR_DICT[';']:
        advance(infoList)
        Execution(infoList)
    return

def Execution(infoList):
    global sym
    if infoList[0][0][0] == ls.RESERVED_WORD_DICT['read']:
        advance(infoList)
        if advance(infoList)[0][0] != ls.OPERATOR_DICT['(']:
            error('Line %d: 读语句缺少左括号' % sym[0][1])
            infoList.insert(0, sym[0])
        if infoList[0][0][0] == ls.OPERATOR_DICT[')']:
            advance(infoList)
            error('Line %d: 读语句缺少参数' % sym[0][1])
            return
        Variable(infoList)
        checkUdef(ls.symbolTable[sym[0][0][1]])
        if advance(infoList)[0][0] != ls.OPERATOR_DICT[')']:
            error('Line %d: 读语句缺少右括号' % sym[0][1])
            infoList.insert(0, sym[0])
        return
    if infoList[0][0][0] == ls.RESERVED_WORD_DICT['write']:
        advance(infoList)
        if advance(infoList)[0][0] != ls.OPERATOR_DICT['(']:
            error('Line %d: 写语句缺少左括号' % sym[0][1])
            infoList.insert(0, sym[0])
        if infoList[0][0][0] == ls.OPERATOR_DICT[')']:
            advance(infoList)
            error('Line %d: 写语句缺少参数' % sym[0][1])
            return
        Variable(infoList)
        checkUdef(ls.symbolTable[sym[0][0][1]])
        if advance(infoList)[0][0] != ls.OPERATOR_DICT[')']:
            error('Line %d: 写语句缺少右括号' % sym[0][1])
            infoList.insert(0, sym[0])
        return
    if infoList[0][0][0] == ls.RESERVED_WORD_DICT['if']:
        advance(infoList)
        ConditionalExpression(infoList)
        if advance(infoList)[0][0] != ls.RESERVED_WORD_DICT['then']:
            error('Line %d: 条件语句缺少then' % sym[0][1])
            if not checkExe(infoList):
                infoList.insert(0, sym[0])
        Execution(infoList)
        if advance(infoList)[0][0] != ls.RESERVED_WORD_DICT['else']:
            error('Line %d: 条件语句缺少else' % sym[0][1])
            if not checkExe(infoList):
                infoList.insert(0, sym[0])
        Execution(infoList)
        return

    #赋值语句
    Variable(infoList)
    checkUdef(ls.symbolTable[sym[0][0][1]])
    if advance(infoList)[0][0] != ls.OPERATOR_DICT[':=']:
        error('Line %d: 赋值语句缺少:=' % sym[0][1])
        infoList.insert(0, sym[0])
    ArithmeticExpression(infoList)
    return

def ArithmeticExpression(infoList):
    Item(infoList)
    while infoList[0][0][0] == ls.OPERATOR_DICT['-']:
        advance(infoList)
        Item(infoList)
    return

def Item(infoList):
    Factor(infoList)
    while infoList[0][0][0] == ls.OPERATOR_DICT['*']:
        advance(infoList)
        Factor(infoList)
    return

def Factor(infoList):
    if infoList[0][0][0] == ls.OPERATOR_DICT[')']:
        error('Line %d: 算术表达式错，多余的右括号' % infoList[0][1])
        advance(infoList)
        return
    if (infoList[0][0][0] == ls._SYMBOL or infoList[0][0][0] == ls._CONST) and infoList[1][0][0] != ls.OPERATOR_DICT['(']:
        advance(infoList)
        if sym[0][0][0] == ls._SYMBOL:
            checkUdef(ls.symbolTable[sym[0][0][1]])
        return
    FunctionCall(infoList)
    return

def Identifier(infoList):
    global sym
    advance(infoList)
    if sym[0][0][0] == ls._SYMBOL:
        return
    if sym[0][0][0] >= 1 and sym[0][0][0] <= 9:
        error('Line %d: 变量与保留字冲突' % sym[0][1])
        return
    error('Line %d: 变量/标识符不合规范' % sym[0][1])
    return

def FunctionCall(infoList):
    Identifier(infoList)
    if advance(infoList)[0][0] != ls.OPERATOR_DICT['(']:
        error('Line %d: 函数调用缺少左括号' % sym[0][1])
        infoList.insert(0, sym[0])
    if infoList[0][0][0] == ls.OPERATOR_DICT[')']:
        advance(infoList)
        return
    ArithmeticExpression(infoList)
    if advance(infoList)[0][0] != ls.OPERATOR_DICT[')']:
        error('Line %d: 函数调用缺少右括号' % sym[0][1])
        infoList.insert(0, sym[0])
    return

def ConditionalExpression(infoList):
    ArithmeticExpression(infoList)
    RelationOperator(infoList)
    ArithmeticExpression(infoList)
    return

def RelationOperator(infoList):
    global sym
    advance(infoList)
    if sym[0][0][0] >= 12 and sym[0][0][0] <= 17:
        return
    error('Line %d: 关系运算符错' % sym[0][1])
    return


def get_integral_from_data(data, times):
    previous_time = times[0]

    accumulated_data = 0.0

    results = []

    for i in range(1, len(times)):
        delta_time = times[i] - previous_time

        delta_data = delta_time * data[i]

        previous_time = times[i]

        accumulated_data += delta_data

        results.append(accumulated_data)

    return results

Intro to Self-Driving Cars/Vehicle motion and control/Lesson_2/Integrating_rate_gyro_data.py


#!/usr/bin/env python
# (C) 2017 OpenEye Scientific Software Inc. All rights reserved.
#
# TERMS FOR USE OF SAMPLE CODE The software below ("Sample Code") is
# provided to current licensees or subscribers of OpenEye products or
# SaaS offerings (each a "Customer").
# Customer is hereby permitted to use, copy, and modify the Sample Code,
# subject to these terms. OpenEye claims no rights to Customer's
# modifications. Modification of Sample Code is at Customer's sole and
# exclusive risk. Sample Code may require Customer to have a then
# current license or subscription to the applicable OpenEye offering.
# THE SAMPLE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED.  OPENEYE DISCLAIMS ALL WARRANTIES, INCLUDING, BUT
# NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. In no event shall OpenEye be
# liable for any damages or liability in connection with the Sample Code
# or its use.

import sys
from openeye import oechem
from openeye import oedepict
from openeye import oegrapheme

def ImportMolecule(filename):

    ifs = oechem.oemolistream()
    if not ifs.open(filename):
        oechem.OEThrow.Fatal("Unable to open %s for reading" % filename)

    mol = oechem.OEGraphMol()
    if not oechem.OEReadMolecule(ifs, mol):
        oechem.OEThrow.Fatal("Unable to read molecule in %s" % filename)
    return mol

if len(sys.argv) != 3:
    oechem.OEThrow.Usage("%s <ref> <fit>" % sys.argv[0])

refmol3D = ImportMolecule(sys.argv[1])
fitmol3D = ImportMolecule(sys.argv[2])

# @ <SNIPPET-PREPARE-ALIGNED-DEPICTION-FROM-3D>
refmol2D = oechem.OEGraphMol(refmol3D)
oegrapheme.OEPrepareDepictionFrom3D(refmol2D)

fitmol2D = oechem.OEGraphMol(fitmol3D)
oegrapheme.OEPrepareAlignedDepictionFrom3D(fitmol2D, fitmol3D, refmol2D, refmol3D)

width, height = 300, 300
opts = oedepict.OE2DMolDisplayOptions(width, height, oedepict.OEScale_AutoScale)
refscale = oedepict.OEGetMoleculeScale(refmol2D, opts)
fitscale = oedepict.OEGetMoleculeScale(fitmol2D, opts)
opts.SetScale(min(refscale, fitscale))

refdisp = oedepict.OE2DMolDisplay(refmol2D, opts)
oedepict.OERenderMolecule("OEPrepareAlignedDepictionFrom3D-ref-2D.png", refdisp)

fitdisp = oedepict.OE2DMolDisplay(fitmol2D, opts)
oedepict.OERenderMolecule("OEPrepareAlignedDepictionFrom3D-fit-2D.png", fitdisp)

# @ </SNIPPET-PREPARE-ALIGNED-DEPICTION-FROM-3D>
oedepict.OERenderMolecule("OEPrepareAlignedDepictionFrom3D-ref-2D.pdf", refdisp)
oedepict.OERenderMolecule("OEPrepareAlignedDepictionFrom3D-fit-2D.pdf", fitdisp)


from webpay import WebPay
from httmock import HTTMock, urlmatch
import pytest

import tests.helper as helper
import webpay.errors as errors

class TestWebpay:

    def test_request_raises_not_found_with_ja_language(self):
        webpay = WebPay('test_key')
        webpay.accept_language('ja')

        @urlmatch(scheme='https', netloc='api.webpay.jp', path='/v1/customers/cus_eS6dGfa8BeUlbS')
        def mock(url, request):
            assert request.headers['Accept-Language'] == 'ja'
            return helper.response_from_file('errors/not_found_ja.txt', request)

        with pytest.raises(errors.InvalidRequestError) as excinfo:
            with HTTMock(mock):
                webpay.customers.retrieve('cus_eS6dGfa8BeUlbS')
        exc = excinfo.value
        assert exc.args[0].encode('utf-8') == \
            b'\xe8\xa9\xb2\xe5\xbd\x93\xe3\x81\x99\xe3\x82\x8b\xe9\xa1\xa7\xe5\xae\xa2\xe3\x81' + \
            b'\x8c\xe3\x81\x82\xe3\x82\x8a\xe3\x81\xbe\xe3\x81\x9b\xe3\x82\x93: cus_eS6dGfa8BeUlbS'

    def test_request_raises_not_found_with_default_language(self):
        webpay = WebPay('test_key')

        @urlmatch(scheme='https', netloc='api.webpay.jp', path='/v1/charges/foo')
        def mock(url, request):
            assert request.headers['Accept-Language'] == 'en'
            return helper.response_from_file('errors/not_found.txt', request)

        with pytest.raises(errors.InvalidRequestError) as excinfo:
            with HTTMock(mock):
                webpay.charges.retrieve('foo')
        exc = excinfo.value
        assert exc.args[0] == 'No such charge: foo'


import random
wordlist=[]
with open ('D:\python projects and pr\.vscode\wordlist.txt','r') as wordsource:
    wordlist=wordsource.read().splitlines()

word=random.choice(wordlist)
letters=list(word)

print(word)
p_letters=[]
for x in range(len(letters)):
    p_letters.append("*")

while p_letters!=letters:
    lettere=str(input("enter value  "))
    for i in range(len(letters)):
        if lettere==letters[i]:
            p_letters[i]=lettere
    #for p_letter in p_letters:
    print(*p_letters)
#Score Card System not included right now
print("I love Open Source because it empowers everyone on the platform either a beginner or a pro")


# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from botbuilder.schema import Activity, ActivityTypes, EndOfConversationCodes
from botbuilder.core import TurnContext

from coco_microsoft_bot_framework import CoCoActivityHandler

class CoCoBot(CoCoActivityHandler):
    async def on_message_activity(self, turn_context: TurnContext):
        print(turn_context.activity.conversation.id)
        if self.is_component_active():
            await self.call_active_component(turn_context)
            return
        else:
            self.conversation_state.active_component = turn_context.activity.text

        await self.activate_component(
            turn_context, self.conversation_state.active_component
        )

        if not self.conversation_state.active_component:
            end_of_conversation = Activity(type=ActivityTypes.end_of_conversation)
            end_of_conversation.code = EndOfConversationCodes.completed_successfully
            await turn_context.send_activity(end_of_conversation)

    async def on_end_of_conversation_activity(self, turn_context: TurnContext):
        # This will be called if the root bot is ending the conversation.  Sending additional messages should be
        # avoided as the conversation may have been deleted.
        # Perform cleanup of resources if needed.
        pass


"""add author field to exercise

Revision ID: 2f849a7e6f1c
Revises: 551aa4724636
Create Date: 2020-08-30 17:12:04.118955

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '2f849a7e6f1c'
down_revision = '551aa4724636'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('workout', sa.Column('author', sa.String(), nullable=False))
    op.create_index(op.f('ix_workout_author'), 'workout', ['author'], unique=False)
    op.create_foreign_key('workout_author_id', 'workout', 'stronk_user', ['author'], ['id'])
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint('workout_author_id', 'workout', type_='foreignkey')
    op.drop_index(op.f('ix_workout_author'), table_name='workout')
    op.drop_column('workout', 'author')
    # ### end Alembic commands ###


import os

from src import ConfigManager

__thumbnails_dir = "db/thumbnails/"
__users_dir = "db/userfiles/"

__my_rabbit_id = ConfigManager.get_config("DL_RABBIT_ID")

def create_thumbnail(file_id):
    os.system("/usr/bin/convert " + __users_dir + file_id + " -resize 64x64 " + __thumbnails_dir + file_id)
    return None


from django.urls import path
from . import views
app_name = 'accounts'

urlpatterns =[
    path('', views.account_list, name='account_list' ),
    path('<int:user_pk>/', views.detail, name='detail'),
    path('signup/', views.signup, name='signup'),
    path('login/', views.login, name='login'),
    path('logout/', views.logout, name='logout'),
    path('follow/<int:person_pk>/', views.follow, name='follow'),
]


def millas_kilometros():
    KILOMETROS_EN_MILLAS = 1.609344
    millas = float(input("Millas: "))
    kilometros = millas * KILOMETROS_EN_MILLAS
    print(str(millas) + " son " + str(kilometros) + " kilometros")

def kilometros_millas():
    MILLAS_EN_KILOMETROS = 0.621371
    kilometros = float(input("Kilometros: "))
    millas = kilometros * MILLAS_EN_KILOMETROS
    print(str(kilometros) + " son " + str(millas) + " millas")
    pass

def run():
    menu = """
    Escribe el número correspondiente a tu elección
        [1] - Millas a kilometros
        [2] - Kilometros a millas

    Cualquier otra tecla finalizará el convertidor de unidades.
    Seleccionar: """

    seleccion = input(menu)

    if seleccion == "1":
        millas_kilometros()
    elif seleccion == "2":
        kilometros_millas()
    else:
        print("Convertidor finalizado.")

if __name__ == "__main__":
    run()


# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from django.test import TestCase

from ralph.discovery.models import Device, DeviceType, IPAddress
from ralph.scan.plugins.puppet import (
    _get_ip_addresses_hostnames_sets,
    _is_host_virtual,
    _merge_disks_results,
)

class PuppetPluginTest(TestCase):
    def setUp(self):
        device = Device.create(
            sn='sn_qweasd_123_1',
            model_name='SomeDevice',
            model_type=DeviceType.unknown,
        )
        IPAddress.objects.create(
            address='127.0.0.1',
            hostname='localhost',
            device=device,
        )
        IPAddress.objects.create(
            address='127.0.0.2',
            device=device,
        )

    def test_is_host_virtual(self):
        self.assertFalse(_is_host_virtual({
            'virtual': 'physical',
            'manufacturer': 'Frania',
        }))
        self.assertFalse(_is_host_virtual({
            'manufacturer': 'Frania',
        }))
        self.assertTrue(_is_host_virtual({
            'manufacturer': 'Bochs',
        }))
        self.assertTrue(_is_host_virtual({
            'virtual': 'xen',
        }))

    def test_merge_disks_results(self):
        self.assertEqual(
            _merge_disks_results(
                [
                    {
                        'size': 100000,
                        'label': 'Hitachi',
                        'mount_point': '/mnt/d1',
                        'family': 'Hitachi',
                        'serial_number': 'qwe123_1',
                    },
                    {
                        'size': 100000,
                        'label': 'Hitachi',
                        'mount_point': '/mnt/d2',
                        'family': 'Hitachi',
                        'serial_number': 'qwe123_2',
                    },
                    {
                        'size': 100000,
                        'label': 'Hitachi',
                        'mount_point': '/mnt/n1',
                        'family': 'Hitachi',
                    },
                ],
                [
                    {
                        'size': 100001,
                        'label': 'Hitachi Super',
                        'serial_number': 'qwe123_1',
                    },
                    {
                        'size': 100001,
                        'label': 'Hitachi Super',
                        'serial_number': 'qwe123_3',
                    },
                ]
            ),
            [
                {
                    'family': 'Hitachi',
                    'label': 'Hitachi Super',
                    'mount_point': '/mnt/d1',
                    'serial_number': 'qwe123_1',
                    'size': 100001,
                },
                {
                        'size': 100000,
                        'label': 'Hitachi',
                        'mount_point': '/mnt/d2',
                        'family': 'Hitachi',
                        'serial_number': 'qwe123_2',
                },
                {
                    'family': 'Hitachi',
                    'label': 'Hitachi',
                    'mount_point': '/mnt/n1',
                    'size': 100000,
                },
                {
                    'label': 'Hitachi Super',
                    'serial_number': 'qwe123_3',
                    'size': 100001,
                },
            ],
        )

    def test_get_ip_addresses_hostnames_sets(self):
        self.assertEqual(
            _get_ip_addresses_hostnames_sets('10.123.22.11'),
            ({'10.123.22.11'}, set()),
        )
        self.assertEqual(
            _get_ip_addresses_hostnames_sets('127.0.0.1'),
            ({'127.0.0.1', '127.0.0.2'}, {'localhost'}),
        )


from pyspark.sql.types import StructType, StructField, StringType, ArrayType
import pyspark.sql.functions as f
from pyspark.sql.functions import col
import load_to_spark

base_path = "/home/ubuntu/dumps/wiki_small_"
log_path = "/home/ubuntu/BA_Project/log/articles_per_author_log.txt"
bots = ['bot', 'bots']
schema = StructType([StructField("id",StringType(),True),StructField("revision", \
    ArrayType(StructType([StructField("comment",StringType(),True),StructField("contributor", \
    StructType([StructField("id",StringType(),True),StructField("ip",StringType(),True), \
    StructField("username",StringType(),True)]),True),StructField("id",StringType(),True), \
    StructField("parentid",StringType(),True),StructField("timestamp",StringType(),True)]),True), \
    True),StructField("title",StringType(),True)])

for i in range(1, 27):
    logfile = open(log_path, "+a")
    path = base_path + str(i) + '.json'
    df = load_to_spark.main_init_df(path)
    df = df.drop(df.id)
    df = df.drop(df.authorID)
    df = df.where(df.author.isNotNull())
    df_bots = df.where(df.author.rlike('|'.join(bots)))
    df_users = df.subtract(df_bots)

    #title|author|timestamp --> revision count
    df_rev_per_user = df_users.groupBy(df_users.author).count()
    avg_rev = int(df_rev_per_user.select(f.avg(col("count")) \
        .alias("avg")).first()["avg"])
    stddev_rev = int(df_rev_per_user.select(f.stddev(col("count")) \
        .alias("stddev")).first()["stddev"])

    df_users = df_users.drop(df_users.editTime).distinct()

    #title|author --> article count
    df_art_per_user = df_users.groupBy(df_users.author).count()
    avg_art = int(df_art_per_user.select(f.avg(col("count")) \
        .alias("avg")).first()["avg"])
    stddev_art = int(df_art_per_user.select(f.stddev(col("count")) \
        .alias("stddev")).first()["stddev"])

    output = "{} {} {} {}\n".format(avg_rev, stddev_rev, avg_art, stddev_art)
    logfile.write(output)
    logfile.close()
    print("Done", i)

print("Done")


import csv
import glob
import hashlib
import json
import logging
import os
import re
from configparser import ConfigParser
from datetime import datetime
from multiprocessing.pool import Pool

import pattern.web
import requests
from boto.s3.connection import S3Connection
from boto.s3.key import Key
from goose import Goose
from goose.text import StopWordsArabic
from multiprocessing import Queue

from parsedatetime import Calendar

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

cal = Calendar()
geese = {'arabic': Goose({'stopwords_class': StopWordsArabic, 'enable_image_fetching': False}),
         'english': Goose({'use_meta_language': False, 'target_language': 'en', 'enable_image_fetching': False})}
g_def = Goose()

not_finished = True

history = {}

class RSS:
    def __init__(self, name, url, region, lang):
        self.name = name
        self.url = url
        self.region = region
        self.lang = lang

class Article:
    def __init__(self, title, text, url, authors, date, website="", description="", filename=""):
        super().__init__()
        self.filename = filename
        self.description = description
        self.title = title
        self.website = website
        self.text = self.clean_text(text)
        self.url = url
        self.authors = authors
        self.publish_date, self.rss_date = date

    def clean_text(self, text):
        """
        Private function to clean some of the cruft from the content pulled from
        various sources.

        Parameters
        ----------

        text: String.
                Dirty text.

        Returns
        -------

        text: String.
                Less dirty text.
        """
        site_list = ['menafn_algeria', 'menafn_bahrain', 'menafn_egypt',
                     'menafn_iraq', 'menafn_jordan', 'menafn_kuwait',
                     'menafn_lebanon', 'menafn_morocco', 'menafn_oman',
                     'menafn_palestine', 'menafn_qatar', 'menafn_saudi',
                     'menafn_syria', 'menafn_tunisia', 'menafn_turkey',
                     'menafn_uae', 'menafn_yemen']

        if self.website == 'bbc':
            text = text.replace(
                "This page is best viewed in an up-to-date web browser with style sheets (CSS) "
                "enabled. While you will be able to view the content of this page in your current "
                "browser, you will not be able to get the full visual experience. Please consider "
                "upgrading your browser software or enabling style sheets (CSS) if you are able to do "
                "so.",
                '')
        if self.website == 'almonitor':
            text = re.sub("^.*?\(photo by REUTERS.*?\)", "", text)
        if self.website in site_list:
            text = re.sub("^\(.*?MENAFN.*?\)", "", text)
        elif self.website == 'upi':
            text = text.replace(
                "Since 1907, United Press International (UPI) has been a leading provider of critical "
                "information to media outlets, businesses, governments and researchers worldwide. UPI "
                "is a global operation with offices in Beirut, Hong Kong, London, Santiago, Seoul and "
                "Tokyo. Our headquarters is located in downtown Washington, DC, surrounded by major "
                "international policy-making governmental and non-governmental organizations. UPI "
                "licenses content directly to print outlets, online media and institutions of all "
                "types. In addition, UPI's distribution partners provide our content to thousands of "
                "businesses, policy groups and academic institutions worldwide. Our audience consists "
                "of millions of decision-makers who depend on UPI's insightful and analytical stories "
                "to make better business or policy decisions. In the year of our 107th anniversary, "
                "our company strives to continue being a leading and trusted source for news, "
                "analysis and insight for readers around the world.",
                '')

        self.text = text.replace('\n', ' ')

    def generate_date(self, publish_date, rss_date):
        try:
            # todo : this solution appears to ignore time zone specifications
            # e.g -  07 Dec 2016 00:00:00 EST  maps to 2016 - 12 - 07T00:00:00
            # so the time zone info is lost.

            # The publish time returned via rss feeds are not parsed correctly
            # by the calendar.  They have a leading 3 character day of the week string
            # which confounds the parser. The day of the month is often wrong.
            #  we strip that out first

            target = rss_date
            if rss_date[:3] in ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']:
                target = rss_date[5:]
            time_struct, parse_status = cal.parse(target)
            date_parsed = datetime(*time_struct[:6]).isoformat()
        except (Exception, TypeError, ValueError, KeyError, IndexError, OverflowError, AttributeError):
            date_parsed = None
        self.publish_date = publish_date
        self.rss_date = rss_date
        return date_parsed

def convert_url(url, website):
    """
    Private function to clean a given page URL.

    Parameters
    ----------

    url: String.
            URL for the news stories to be scraped.

    website: String.
                Nickname for the RSS feed being scraped.

    Returns
    -------

    page_url: String.
                Cleaned and unicode converted page URL.
    """

    if website == 'xinhua':
        page_url = url.replace('"', '')
        page_url = page_url.encode('ascii')
    elif website == 'upi':
        page_url = url.encode('ascii')
    elif website == 'zaman':
        # Find the weird thing. They tend to be ap or reuters, but generalized
        # just in case
        com = url.find('.com')
        slash = url[com + 4:].find('/')
        replaced_url = url.replace(url[com + 4:com + slash + 4], '')
        split = replaced_url.split('/')
        # This is nasty and hackish but it gets the jobs done.
        page_url = '/'.join(['/'.join(split[0:3]), 'world_' + split[-1]])
    else:
        page_url = url.encode('utf-8')

    return page_url.decode('utf-8')

def writer(q: Queue):

    try:
        os.mkdir("articles")
    except FileExistsError:
        pass

    while not_finished or not q.empty():
        a = q.get()
        with open("articles/" + a.filename, 'w') as f:
            f.write(json.dumps(a, default=lambda o: o.__dict__))

def s3write(q: Queue, aws_key=None, aws_secret=None, bucket_name=None):
    conn = S3Connection(aws_key, aws_secret)
    bucket = conn.get_bucket(bucket_name, validate=False)
    while not_finished or not q.empty():
        key = str(datetime.now())
        k = Key(bucket, key)
        batch = [json.dumps(q.get()) for _ in range(10)]  # grab 10 articles to write to file
        k.set_contents_from_string('\n'.join(batch))

def main():

    config_file = glob.glob('config.ini')
    if not config_file:
        config_file = 'default_config.ini'
    parser = ConfigParser()
    parser.read(config_file)

    sources = parser.get('URLS', 'sources').split(',')

    # Convert from CSV of URLs to a dictionary and filter based on type
    try:
        with open(parser.get('URLS', 'file')) as csv_file:
            rss = [RSS(*row) for row in csv.reader(csv_file)]
            rss_list = [listing for listing in rss if listing.region in sources]
    except IOError:
        logger.exception('Could not open URL whitelist file.')
        raise

    with Pool(int(parser.get('Processes', 'pool_size'))) as pool:
        articles = Queue()
        pool.apply_async(writer, articles)

        for rss_item in rss_list:
            try:
                rss_results = pattern.web.Newsfeed().search(rss_item.url, count=100, cached=False, timeout=10)
                logger.debug('There are {} rss_results from {}'.format(len(rss_results), rss_item.url))

                for result in rss_results:
                    pool.apply_async(process_url, (result, articles))
            except (pattern.web.URLTimeout, pattern.web.HTTP404NotFound, pattern.web.URLError):
                logger.error("TIMEOUT404URLERROR {}".format(rss_item.url))
            except Exception:
                logger.exception('Problem fetching RSS feed for {}.'.format(rss_item.url))

            logger.info('Scrape of {} finished'.format(rss_item.url))
        pool.terminate()
        global not_finished
        not_finished = False
        logger.info('All done.')

def process_url(result, queue: Queue):
    page_url = convert_url(result.url, "")
    file_name = hashlib.sha512(page_url.encode("utf-8")).hexdigest()

    if file_name not in history:
        try:
            article = geese.get(result.lang, g_def).extract(page_url)

            a = Article(text=article.cleaned_text,
                        url=page_url,
                        authors=article.authors,
                        date=(article.publish_date, result.date),
                        title=article.title,
                        website=article.domain,
                        description=article.meta_description,
                        filename=file_name)

            queue.put(a)
            return a
        except pattern.web.HTTP404NotFound:
            logger.error("404 {}".format(page_url))
        except TypeError as e:
            logger.exception('Problem obtaining text from URL: {}'.format(page_url, e))
        except requests.exceptions.ReadTimeout:
            logger.exception("Timeout at {}".format(page_url))
        except Exception:
            logger.exception("Unknown Exception")
    else:
        logger.debug('Result from {} already in database'.format(page_url))

if __name__ == '__main__':
    main()