import time
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import math

def calc(x):
  return str(math.log(abs(12*math.sin(int(x)))))

browser = webdriver.Chrome()

browser.get("http://suninjuly.github.io/alert_accept.html")
time.sleep(1)

submit_button = browser.find_element_by_css_selector('[type="submit"]')
submit_button.click()

alert = browser.switch_to.alert
alert.accept()

time.sleep(1)

x = browser.find_element_by_id("input_value").text

input_text = browser.find_element_by_id("answer")
submit_button = browser.find_element_by_css_selector('[type="submit"]')

input_text.send_keys(calc(x))

submit_button.click()
time.sleep(15)

# browser.execute_script("alert('Robots at work');")
# browser.execute_script('document.title="Script executing";')
# element = browser.execute_script('document.getElementsByName("name")')
# getElementById
# getElementsByTagName
# getElementsByClassName
# querySelector - для CSS
# querySelectorAll - для CSS (находит все совпадения)
# button = browser.find_element_by_tag_name("button")
# browser.execute_script("return arguments[0].scrollIntoView(true);", button)
# button.click()
# button = document.getElementsByTagName("button")[0];
# button.scrollIntoView();
#
# human_radio = browser.find_element_by_id("humanRule")
# human_checked = human_radio.get_attribute("checked")
# print("value of human radio: ", human_checked)
# assert human_checked is not None, "Human radio is not selected by default"

# browser.find_element_by_tag_name("select").click()
# browser.find_element_by_css_selector("option:nth-child(2)").click()
# Последняя строчка может выглядеть и так:
#
# browser.find_element_by_css_selector("[value='1']").click()
# Это не самый удобный способ, так как нам приходится делать лишний клик для открытия списка.
#
# Есть более удобный способ, для которого используется специальный класс Select из библиотеки WebDriver.
# Вначале мы должны инициализировать новый объект, передав в него WebElement с тегом select.
# Далее можно найти любой вариант из списка с помощью метода select_by_value(value):
#
# from selenium.webdriver.support.ui import Select
# select = Select(browser.find_element_by_tag_name("select"))
# select.select_by_value("1") # ищем элемент с текстом "Python"
#
# select.select_by_visible_text("text")
# select.select_by_index(index)
# select.select_by_visible_text("Python")
#
# import os
#
# current_dir = os.path.abspath(os.path.dirname(__file__))    # получаем путь к директории текущего исполняемого файла
# file_path = os.path.join(current_dir, 'file.txt')           # добавляем к этому пути имя файла
# element.send_keys(file_path)
# alert = browser.switch_to.alert
# alert.accept()
# Чтобы получить текст из alert, используйте свойство text объекта alert:
#
# alert = browser.switch_to.alert
# alert_text = alert.text
# метод для отказа: alert.dismiss()
#
# Третий вариант модального окна — prompt — имеет дополнительное поле для ввода текста. Чтобы ввести текст, используйте метод send_keys():
#
# prompt = browser.switch_to.alert
# prompt.send_keys("My answer")
# prompt.accept()

# После выполнения всех действий мы не должны забыть закрыть окно браузера
browser.quit()


import logging, hashlib, time, copy
from tname.core.execution import SplitSearch
from tname.core.cms import WordPressCLI
from tname.core.model import WebDriver, Verifier, Configuration, plain_html, check_frequency
from tname.core.model import dimensionality_reduction
from tname.core.visual_oracle.visual_oracle import VisualOracle
from tname.core.visual_oracle.screenshot import Screenshot
from tname.core.analyser import Analyser
from tname.core.timer import Timer

class PENA:
    def __init__(self, cms_path, url="http://localhost", heuristic=plain_html, debug=True):
        """
        Params:
            cms_path: path for wordpress folder
            url: wordpress index page url (default is http://localhost)
            heuristic: type of heuristic used to check page conflict
            debug: test pages will not been removed after execution (check ~/tmp/ folder)
        """
        self.cli = WordPressCLI(home=cms_path)
        self.web_driver = WebDriver(url=url)
        self.heuristic = heuristic
        self.debug = debug
        self.verifier = Verifier(self.cli, self.web_driver, self.heuristic)
        self.input_plugins = []
        self.mode = SplitSearch
        self.generate_configuration = False
        self.show_lines = True
        self.skip_dimensionality = False
        self.remove_spurious = True
        self.visual_oracle = True

    def set_input_plugins(self, input_plugins):
        """
            Description:
                Sets a list of plugins into a configuration
            Params:
                input_plugins: list of plugins name
            Return:
                None
        """
        self.input_plugins = input_plugins

    def set_mode(self, mode):
        """
            Description:
                Set a Search technnique to check conflicts
            Params:
                mode: SplitSearch | Pairwise (class)
            Return:
                None
        """
        self.mode = mode

    def set_generate_configuration(self, generate):
        """
            Description:
                Set true to generate a dictionary of conflicts
            Params:
                generate: boolean
            Return:
                None
        """
        self.generate_configuration = generate

    def set_show_lines(self, show_lines):
        """
            Description:
                Shows unexpected code added/removed from a plugin in log
            Params:
                show_lines: boolean
            Return:
                None
        """
        self.show_lines = show_lines

    def set_skip_dimensionality(self, skip):
        """
            Description:
                Set true to run input_plugins without dimensionality reduction
            Params:
                skip: boolean
            Return:
                None
        """
        self.skip_dimensionality = skip

    def run_only_dimensionality(self, input_plugins=[]):
        """
        Description:
            Runs only dimensionality reduction to generate a set of plugins with differents equivalence class
        Params:
            input_plugins: list of plugins name
        Return:
            list of plugins after dimensionality reduction
        """
        reduced_plugins = dimensionality_reduction(input_plugins, self.web_driver, self.cli)
        if len(reduced_plugins):
            logging.debug("Plugins after dimensionality reduction")
            [logging.debug(" {}".format(plugin)) for plugin in reduced_plugins]
        return reduced_plugins

    def run(self):
        """
        Description:
            PENA Runner
        Params:
            None
        Return:
            None
        """
        if len(self.input_plugins) < 2:
            raise Exception('Plugin list invalid. The plugin list must have lenght 2 or more.')

        # deactivate plugins
        self.cli.deactivate(self.input_plugins)

        logging.info('RUNNING {}'.format(str(self.mode)))
        logging.info('RUNNING N={}'.format(len(self.input_plugins)))

        logging.info("Run Started")

        technnique = self.mode(self.verifier)

        # STEP 1 - DIMENSIONALITY REDUCTION
        reduced_plugins = dimensionality_reduction(
            self.input_plugins,
            self.web_driver,
            self.cli,
            skip=self.skip_dimensionality
        )

        # STEP 2 and 3 - SEARCH FOR CONFLICTS AND CLUSTERING
        if len(reduced_plugins) > 1:
            configuration = Configuration(reduced_plugins)
            conflicts = technnique.run(configuration)
            logging.info("Delta timer (in minutes): {}".format(Timer.get_delta()))

            if self.generate_configuration:
                ''' generate nodes of conflicts '''
                self.generate_nodes(reduced_plugins, conflicts)
                return

            if conflicts:
                all_conflicts = copy.copy(conflicts)

                # REPORT CONFLICTS FOUND BEFORE CLEANSING
                logging.debug("Conflicts found after searching: {}".format(len(all_conflicts)))
                for conflict in all_conflicts:
                    logging.debug("Conflict - {}".format(conflict))
                    changes = self.verifier.conflicting_changes(conflict)
                    unexpected_added, unexpected_removed = changes[0], changes[1]
                    if len(unexpected_added):
                        added = [("    {}".format(line)) for line in unexpected_added]
                        logging.info("{}: {}".format("Unexpected added", added))
                    if len(unexpected_removed):
                        removed = [("    {}".format(line)) for line in unexpected_removed]
                        logging.info("{}: {}".format("Unexpected removed", removed))

                # STEP 4 - REPORT CLEANSING
                if self.remove_spurious:
                    logging.info("Cleansing started")

                    self.cleansing_data = {
                        "non-determinism": [],
                        "minify": [],
                        "union_tags": [],
                        "optional_closing": []
                    }

                    temp_conflicts = copy.copy(all_conflicts)
                    for conflict in all_conflicts:
                        changes = self.verifier.conflicting_changes(conflict)
                        unexpected_added, unexpected_removed, typeof_removal = Analyser(changes[0], changes[1]).run()
                        if (not unexpected_added) and (not unexpected_removed):
                            self.cleansing_data[typeof_removal].append(conflict)
                            temp_conflicts.remove(conflict)
                        else:
                            self.verifier.update_changes(conflict, unexpected_added, unexpected_removed)

                    remaining_conflicts = self.cleansing(temp_conflicts)

                    self.report_cleansing_data(len(temp_conflicts) - len(remaining_conflicts))
                    logging.info("Cleansing finished")
                    all_conflicts = remaining_conflicts

                # STEP 5 - VISUAL ORACLE
                if self.visual_oracle:
                    logging.info("Checking visual conflicts started")
                    visual_conflicts = self.get_visual_conflicts(all_conflicts)
                    for conflict in visual_conflicts:
                        logging.info('Visual conflict identified: {}'.format(conflict))
                    logging.info("Checking visual conflicts finished")

                # REPORT CONFLICTS
                logging.debug("===== FINAL REPORT =====")
                for conflict in all_conflicts:
                    logging.debug(conflict)
                    logging.debug("Conflicting config - {}".format(conflict))

                    if self.show_lines:
                        changes = self.verifier.conflicting_changes(conflict)
                        unexpected_added, unexpected_removed = changes[0], changes[1]
                        if len(unexpected_added):
                            added = [("    {}".format(line)) for line in unexpected_added]
                            logging.info("{}: {}".format("Unexpected added", added))
                        if len(unexpected_removed):
                            removed = [("    {}".format(line)) for line in unexpected_removed]
                            logging.info("{}: {}".format("Unexpected removed", removed))

                freq = check_frequency(all_conflicts)
                [logging.debug("{}: {}".format(k, v)) for k, v in freq.items()]

                self.cli.deactivate(self.input_plugins)

        logging.info("Run Finished!")

        if not self.debug:
            self.web_driver.cleanup()

    def generate_nodes(self, plugins, conflicts):
        """
        Description:
            Generate a data structure (Dictionary) with all conflicts found by PENA
        Params:
            plugins: list of plugins name
            conflicts: all conflicts found by Pena
        """
        _file = 'configuration-{}-{}.txt'.format(len(plugins), time.asctime())
        configuration = {}
        for i in plugins:
            # start all keys with empty list
            configuration[i] = []

        while len(conflicts) != 0:
            conflict_list = conflicts.pop()
            first, second = conflict_list[0], conflict_list[1]

            if second not in configuration[first]:
                configuration[first].append(second)
            if first not in configuration[second]:
                configuration[second].append(first)

        with open(_file, 'w') as f:
            f.write(str(configuration))

    def rerun(self, conflicts):
        """
        Description:
            Remove conflicts non-deterministics found by PENA
        Params:
            conflicts: list of conflicts
        Return:
            List of conflicts without non-deterministics conflicts
        """
        if not conflicts:
            return conflicts

        self.cli.deactivate(self.input_plugins)

        non_deterministic_conflicts = []
        for conflict in conflicts:
            try:
                self.cli.activate(conflict)
                plugins_on_html = self.web_driver.fetch()
                hash_value = hashlib.md5(plugins_on_html).hexdigest()
                self.cli.deactivate(conflict)

                self.cli.activate(conflict)
                plugins_on_html = self.web_driver.fetch()
                self.cli.deactivate(conflict)
            except Exception as e:
                self.cli.deactivate(conflict)

            if hash_value != hashlib.md5(plugins_on_html).hexdigest():
                self.cleansing_data["non-determinism"].append(conflict)
                non_deterministic_conflicts.append(conflict)

        [conflicts.remove(conflict) for conflict in non_deterministic_conflicts]
        return conflicts

    def cleansing(self, conflicts):
        """
        Description:
            Remove spurious from final report
        Params:
            conflicts: array of conflicts
        Optional Params:
            None
        Return:
            Conflicts list after remove spurious
        """
        cleansing_conflicts = [conflict for conflict in conflicts]

        # remove conflicts with minify keyword
        for conflict in conflicts:
            if 'minify' in str(conflict):
                self.cleansing_data["minify"].append(conflict)
                cleansing_conflicts.remove(conflict)

        # rerun to check non-determinism conflicts

        cleansing_conflicts = self.rerun(cleansing_conflicts)

        return cleansing_conflicts

    def get_visual_conflicts(self, conflicts):
        """
        Description:
            Check visual discrepance between plugins
        Params:
            conflicts: array of plugins
        Optional Params:
            None
        Return:
            array of conflicts that shows visual conflicts
        """
        screenshot = Screenshot()
        visual_oracle = VisualOracle(conflicts, self.cli, screenshot)
        visual_conflicts = visual_oracle.run()
        return visual_conflicts

    def report_cleansing_data(self, total_conflicts):
        logging.info("Conflicts removed by cleansing: {}".format(total_conflicts))
        logging.info("non-determinism conflicts: {}".format(len(self.cleansing_data["non-determinism"])))
        logging.info("{}".format(self.cleansing_data["non-determinism"]))
        logging.info("minify conflicts: {}".format(len(self.cleansing_data["minify"])))
        logging.info("{}".format(self.cleansing_data["minify"]))
        logging.info("union conflicts: {}".format(len(self.cleansing_data["union_tags"])))
        logging.info("{}".format(self.cleansing_data["union_tags"]))
        logging.info("optional closing conflicts: {}".format(len(self.cleansing_data["optional_closing"])))
        logging.info("{}".format(self.cleansing_data["optional_closing"]))


#!/usr/bin/env python
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# vim: set ts=4 sw=4 et tw=79:

"""Implementation of a counter with persistent storage."""

__author__ = 'Greg Darke <darke@google.com>'

import logging
import math
import time

from google.appengine.api import backends
from google.appengine.api import runtime
from google.appengine.api import taskqueue
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

_MEMORY_LIMIT = 128  # The memory limit for a B1 server
_CULL_AMOUNT = 0.15

def _get_taskqueue_target():
    return '%d.%s' % (backends.get_instance(), backends.get_backend())

class CounterModel(db.Model):
    value = db.IntegerProperty(default=0)
    _dirty = False
    _last_accessed = None

    @classmethod
    def get_or_new(cls, name):
        model = cls.get_by_key_name(name)
        if model is None:
            model = cls(key_name=name)
        return model

class CounterStore(object):
    def __init__(self):
        self._store = {}
        self._has_shutdown = False
        self._dirty = False
        self._batch_size = 100

    def get_value(self, name):
        self._ensure_within_memory_limit()

        if name not in self._store:
            model = CounterModel.get_or_new(name)
            self._store[name] = model
        else:
            model = self._store[name]

        model._last_accessed = time.time()
        return model

    def inc_value(self, name, delta):
        if not self._dirty:
            # Enqueue a task with a 'target' specifying traffic be sent to this
            # instance of the backend.
            taskqueue.add(url='/backend/counter/flush',
                          target=_get_taskqueue_target(),
                          countdown=2)
            self._dirty = True

        model = self.get_value(name)
        model.value += delta
        model._dirty = True
        if self._has_shutdown:
            # Since the shutdown hook may be called at any time, we need to
            # protect ourselves against this.
            model.put()
        return model

    def _ensure_within_memory_limit(self):
        memory_limit = _MEMORY_LIMIT * 0.8

        memory_usage = runtime.memory_usage().current()
        if memory_usage >= memory_limit:
            # Create a list of candidate counters to remove. We remove counters
            # that have not been modified before those that have been modified,
            # then order them by the last time they were accessed.
            counters = self._store.values()
            counters.sort(key=lambda counter: (counter._dirty,
                                               counter._last_accessed))
            counters_to_cull = int(math.ceil(len(counters) * _CULL_AMOUNT))
            counters = counters[:counters_to_cull]

            logging.info('Removing %d entries as we are over the memory limit '
                         'by %dMB.',
                         counters_to_cull, memory_limit - memory_usage)

            self._write_in_batches(counters)
            for counter in counters:
                del self._store[counter.key().name()]

    def _put_counters(self, counters):
        db.put(counters)
        for counter in counters:
            counter._dirty = False

    def _write_in_batches(self, counters):
        """Write out the dirty entries from 'counters' in batches.

        The batch size is determined by self._batch_size.

        Args:
          counters: An iterable containing instances of CounterModel.
        """
        to_put = []
        for counter in counters:
            if counter._dirty:
                to_put.append(counter)

                if len(to_put) >= self._batch_size:
                    self._put_counters(to_put)
                    to_put = []

        if to_put:
            self._put_counters(to_put)

    def flush_to_datastore(self):
        """Write the dirty entries from _store to datastore."""
        self._write_in_batches(self._store.itervalues())
        self._dirty = False

    def shutdown_hook(self):
        """Ensures all counters are written to datastore."""
        if self._dirty:
            self.flush_to_datastore()
        self._has_shutdown = True

class StartHandler(webapp.RequestHandler):
    """Handler for '/_ah/start'.

    This url is called once when the backend is started.
    """
    def get(self):
        runtime.set_shutdown_hook(_counter_store.shutdown_hook)

class FlushHandler(webapp.RequestHandler):
    """Handler for counter/flush.

    This handler is protected by login: admin in app.yaml.
    """
    def post(self):
        _counter_store.flush_to_datastore()

class CounterHandler(webapp.RequestHandler):
    """Handler for counter/{get,inc,dec}.

    This handler is protected by login: admin in app.yaml.
    """

    def _write_error(self, error_message):
        self.response.error(400)
        self.response.out.write(error_message)

    def post(self, method):
        """Handler a post to counter/{get,inc,dec}.

        The 'method' parameter is parsed from the url by a regex capture group.

        Args:
          method: The type of operation to perform against the counter store.
              It must be one of 'get', 'inc' or 'dec'.
        """
        key_name = self.request.get('name')
        delta = self.request.get('delta')

        if not key_name:
            self._write_error('Request did not have a "key_name" parameter.')
            return

        if method == 'get':
            model = _counter_store.get_value(key_name)
        else:
            if not delta:
                self._write_error('Request did not have a "delta" parameter.')
                return
            try:
                delta = int(delta)
            except ValueError:
                self._write_error('"delta" is not an integer.')
                return

            if method == 'inc':
                model = _counter_store.inc_value(key_name, delta)
            elif method == 'dec':
                model = _counter_store.inc_value(key_name, -delta)

        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('%d' % model.value)

_handlers = [(r'/_ah/start', StartHandler),
             (r'/backend/counter/flush$', FlushHandler),
             (r'/backend/counter/(get|inc|dec)$', CounterHandler)]

application = webapp.WSGIApplication(_handlers)
_counter_store = CounterStore()

def main():
    run_wsgi_app(application)

if __name__ == '__main__':
    main()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
from urllib2 import urlopen

def rankingsGetter(url="http://ai-contest.com/rankings.php"):
	html=urlopen(url).read()

	html=html[html.find("
")+7:]
	html=html[:html.find("
")]

	html=html.split("
")
	html=map(lambda x: x.split("	"),html)

	def cleanuptr(tr):
		def cleanuptd(td):
			td=td.split("\n")[-1]
			td=td.strip()
			td=td[4:]
			if td.startswith("<"):
				td=td[1:]
			return td
		tr=map(cleanuptd, tr)
		tr=filter(lambda x: x!="",tr)
		#return tr
		if len(tr)<5:
			return []
		return [
			tr[0],
			tr[1][tr[1].find(">")+1:tr[1].find("<")],
			tr[3][tr[3].find(">")+1:tr[3].find("<")],
			tr[4][tr[4].find(">")+1:tr[4].find("<")],
			tr[5]
		]

	html=map(cleanuptr,html)
	return filter(lambda x: x!=[],html)

#print ', '.join([rank[4] for rank in rankingsGetter()[:10]])


#!/usr/bin/python

import cv2
import numpy as np

def filter_region(image, vertices):
    mask = np.zeros_like(image)
    if len(mask.shape) == 2:
        cv2.fillPoly(mask, vertices, 255)
    else:
        cv2.fillPoly(mask, vertices, (255,)*mask.shape[2])
    return cv2.bitwise_and(image, mask)

kernel = np.ones((5,5),np.uint8)
cap = cv2.VideoCapture(0)
ret,frame = cap.read()
rows, cols = frame.shape[:2]

bottom_left = [cols*.1, rows]
top_left = [cols*0.4, rows*0.6]
bottom_right = [cols*0.9, rows]
top_right = [cols*0.6, rows*0.6]
vertices = np.array([[bottom_left, top_left, top_right, bottom_right]], dtype = np.int32)

while True:
    ret,frame = cap.read()
    hsv = cv2.cvtColor(filter_region(frame, vertices) , cv2.COLOR_BGR2HSV)
    ymn = np.array([20,100,100])
    ymx = np.array([50,255,255])

    mask = cv2.inRange(hsv, ymn, ymx)
    res = cv2.bitwise_and(frame, frame, mask = mask)
    cv2.imshow('res', res)
    cv2.imshow('mask', mask)

    threshold = []
    for i in range(479):
        if mask[i][240] != 0:
            threshold.append(255)
        else:
            threshold.append(0)
    print(threshold)

    edge = cv2.Canny(mask, 50, 150, apertureSize= 3)
    cv2.imshow('edges', edge)

    minLineLength = 100
    maxLineGap = 10
    lines = cv2.HoughLinesP(edge,1,np.pi/180,100,minLineLength,maxLineGap)
    if lines is not None:
        for x1,y1,x2,y2 in lines[0]:
            cv2.line(res,(x1,y1),(x2,y2),(0,255,0),2)

        cv2.imshow('houghlines2',res)

    a = cv2.waitKey(5)
    if a == 20:
        break
cv2.destroyAllWindows()
cap.release()


""" Module for testing CoinBasse API """

import json
from sys import path
from unittest import TestCase

from hokonui.exchanges.base import Exchange as base
from hokonui.exchanges.coinbase import CoinBase as cba
from hokonui.utils.helpers import docstring_parameter as docparams

LIBPATH = "../hokonui"
if LIBPATH not in path:
    path.append(LIBPATH)

class TestCoinbase(TestCase):
    """Class for testing Coinbase API"""

    @classmethod
    @docparams(cba.__name__, "setup")
    def setup(cls):
        """{0}.{1}"""
        print(__name__, ": TestClass.setup_class() ----------")

    @classmethod
    @docparams(cba.__name__, "teardown")
    def teardown(cls):
        """{0}.{1}"""
        print(__name__, ": TestClass.teardown_class() -------")

    @classmethod
    @docparams(cba.__name__, "name")
    def test_name(cls):
        """{0}.{1}"""
        assert cba.NAME == cls.__name__.replace("Test", "")

    @classmethod
    @docparams(cba.__name__, "price")
    def test_price(cls):
        """{0}.{1}"""
        assert float(cba.get_current_price(base.CCY_DEFAULT, None, None, cba.HEADER)) > 0.00

    @classmethod
    @docparams(cba.__name__, "bid")
    def test_bid(cls):
        """{0}.{1}"""
        assert float(cba.get_current_bid(base.CCY_DEFAULT, None, None, cba.HEADER)) > 0.00

    @classmethod
    @docparams(cba.__name__, "ask")
    def test_ask(cls):
        """{0}.{1}"""
        assert float(cba.get_current_ask(base.CCY_DEFAULT, None)) > 0.00

    @classmethod
    @docparams(cba.__name__, "ticker")
    def test_ticker(cls):
        """{0}.{1}"""
        data = json.loads(cba.get_current_ticker(base.CCY_DEFAULT, None))
        assert data["pair"] == base.CCY_DEFAULT, "pair should be base.CCY_DEFAULT"
        assert float(data["ask"]) > 0.00, "ask should not be empty"
        assert float(data["bid"]) > 0.00, "bid should not be empty"
        assert float(data["bid"]) <= float(data["ask"]), "bid should be < ask"
        assert float(data["timestamp"]) > 0, "Timestamp should be > zero"

    @classmethod
    @docparams(cba.__name__, "orders")
    def test_orders(cls):
        """{0}.{1}"""
        orders = cba.get_current_orders(base.CCY_DEFAULT)
        assert len(orders["asks"]) > 0, "Asks array should not be empty"
        assert len(orders["bids"]) > 0, "Bids array should not be empty"
        assert orders["source"] == "Coinbase", "Source should be 'Coinbase'"
        assert float(orders["timestamp"]) > 0, "Timestamp should be > zero"

if __name__ == "__main__":
    pass


import gdx
'''
This examples is the same as gdx_getting_started_1.py, except we are now
passing arguments in the select_sensors() and start() functions to avoid the prompts.

This example assumes the Go Direct sensor is connected via USB. Go to the
gdx_getting_started_4.py example to see how to open a bluetooth connection.

Note that the select_sensors argument is a list and the sensor numbers must be inside
list brackets, e.g. gdx.select_sensors([1,2,3]).

How do you know what the sensor numbers are on your Go Direct device? Go to
gdx_getting_started.3.py example to list all of the sensor channels.

'''

from gdx import gdx #the gdx function calls are from a gdx.py file inside the gdx folder.
gdx = gdx.gdx()

gdx.open_usb()
gdx.select_sensors([1,2])
gdx.start(period=1000)

for i in range(0,5):
    measurements = gdx.read() #returns a list of measurements from the sensors selected.
    if measurements == None:
        break
    print(measurements)

gdx.stop()
gdx.close()


import pygame
import random
WIDTH = 800
HEIGHT = 600

class Projectile:
    def __init__(self,x,y,radius,color):
        self.x = x
        self.y = y
        self.radius = radius
        self.color = color
        self.vel = 7

    def draw(self,win):
        pygame.draw.circle(win, self.color, (self.x,self.y), self.radius)


from django.db import models
from django.contrib.auth.models import User
from jdirs.models import Category, Reltype, Region
from django.utils.translation import ugettext_lazy as _
from django_bleach.models import BleachField

from django.shortcuts import render_to_response
from django.core.context_processors import csrf

class Client(models.Model):
    owner 		= models.ForeignKey(User, null=False, blank=False, related_name='client_owner')
    parent      = models.ForeignKey('self', null=True, blank=True, related_name='client_parent', verbose_name=_('Parent'))
    isfolder 	= models.BooleanField(default=False)
    code 		= models.CharField(max_length=9, null=False, unique=False, blank=False, verbose_name = _('Code'))
    name 		= BleachField(max_length=255, null=True, unique=False, blank=True, verbose_name = _('Name'))
    iscompany 	= models.BooleanField(default=True)
    INN 		= models.CharField(max_length=12, null=True, unique=False, blank=True)
    KPP 		= models.CharField(max_length=12, null=True, unique=False, blank=True)
    email 		= models.EmailField()
    uradress	= BleachField(max_length=150, null=True, unique=False, blank=True,  verbose_name = _('Uradress'))
    factadress	= models.CharField(max_length=150, null=True, unique=False, blank=True, verbose_name = _('Factadress'))
    phone		= BleachField(max_length=30, null=True, unique=False, blank=True, verbose_name = _('Phone'))
    fax			= models.CharField(max_length=30, null=True, unique=False, blank=True)
    site		= models.CharField(max_length=50, null=True, unique=False, blank=True)
    isdeleted 	= models.BooleanField(default=False)
    region 		= models.ForeignKey(Region, null=True, blank=True, related_name='client_region')
    categoty	= models.ForeignKey(Category, null=True, blank=True, related_name='client_category')
    reltype		= models.ForeignKey(Reltype, null=True, blank=True, related_name='client_reltype')

    def __unicode__(self):
        return '(' + str(self.id) + ')'+ self.code + ' ' + self.name

class Note(models.Model):
    code 		= models.CharField(max_length=9, null=False, unique=False, blank=False)
    client 		= models.ForeignKey(Client, null=False, blank=False, related_name='Note_client')
    content 	= models.TextField(null=True, unique=False, blank=True, default='')
    isdeleted 	= models.BooleanField(default=False)
    user 		= models.ForeignKey(User, null=False, blank=False, related_name='Note_user')

    def __unicode__(self):
        if len(self.content) > 25:
            cont = '...'
        else:
            cont = ''
        return self.client.code + ' ' + self.client.name + '   ' + self.content[:25] + cont


#!/usr/bin/env python3

""" Sum square difference

The sum of the squares of the first ten natural numbers is,
1^2 + 2^2 + ... + 10^2 = 385

The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)^2 = 55^2 = 3025

Hence the difference between the sum of the squares of the first ten
natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred
natural numbers and the square of the sum.
"""

def sum_of_squares(n):
    s = 0
    for i in range(1, n+1):
        s += i * i
    return s

def square_of_sum(n):
    s = 0
    for i in range(1, n+1):
        s += i
    s *= s
    return s

numbers = 100
print('Result:', square_of_sum(numbers) - sum_of_squares(numbers))


# coding: utf-8

# flask
from flask import Flask
# request param
from flask import request
# GPIO
import RPi.GPIO as GPIO
import time
###########################################################

# GPIO 핀번호 할당
GPIO.setmode(GPIO.BOARD)

# pin 번호 chanel
LED = 19
#LED_2= 16
# 핀 설정
GPIO.setup(LED, GPIO.OUT, initial=GPIO.LOW)
#GPIO.setup(LED_2, GPIO.OUT, initial=GPIO.LOW)

# PWM 객체 생성 : 11번 핀, 주파수 - 100Hz
p_1 = GPIO.PWM(LED, 100)
#p_2= GPIO.PWM(LED_2, 100)

# PWM 신호 출력
p_1.start(0)
#p_2.start(0)
def change_duty_1(c):
    p_1.ChangeDutyCycle(c)

#def change_duty_2(c):
#    p_2.ChangeDutyCycle(c)

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

app = Flask(__name__)

@app.route('/')

def index():

   while 1:
       dc=[10,20,30,40,50]
       for value in dc:

            value =  request.args.get('value', '0')
            #val_2 = request.args.get('val_2', '0')

            change_duty_1(int(value))
            time.sleep(1)
    #change_duty_2(int(val_2))

            return value

if __name__ == '__main__':
    # host
    # 로컬 테스트 시에는 개발하는 PC의 IP로 설정
    # 라즈베리파이 에서 실행을 할 경우 라즈베리파이 IP를 설정
    app.run(host='192.168.0.68', port='5000')


import psycopg2

class Database():
    '''Create a psql database and execute the schema file'''

    def __init__(self, db_name, schema_file):
        self.db_name = db_name
        self.schema_file = schema_file
        conn = psycopg2.connect(dbname='postgres')
        conn.autocommit = True
        cursor = conn.cursor()
        cursor.execute("DROP DATABASE IF EXISTS " + db_name)
        cursor.execute("CREATE DATABASE " + db_name)
        cursor.close()
        conn.commit()
        conn.close()
        conn = psycopg2.connect("dbname="+db_name)
        cursor = conn.cursor()
        cursor.execute(open(self.schema_file, 'r').read())
        cursor.close()
        conn.commit()
        conn.close()

class Cursor(Database):
    '''Create a psycopg2 connection and cursor object

    Args: none

    Returns:
        An open cursor to the database "tournament".

    This class offers a context manager through __enter__ and __exit__ methods.
    Usage:
        with Cursor() as my_cursor:
            my_cursur.execute(some_sql)'''

    def __init__(self, database):
        self.db_name = database.db_name

    def __enter__(self):
        self.conn = psycopg2.connect("dbname=" + self.db_name)
        self.cursor = self.conn.cursor()
        return self.cursor

    def __exit__(self, exec_type, exec_val, exec_tb):
        self.conn.commit()
        self.cursor.close()
        self.conn.close()


#! python

#EchoClient

def usage( script_name ):
    print( 'Usage: py ' + script_name + ' <port number>' )

if __name__ == "__main__":
	# get the command line arguments
	import sys
	import socket
	argc= len( sys.argv )
	if argc != 2:
		usage( sys.argv[0] )
		sys.exit()
	message= sys.stdin.readline() # read a message from standard input
	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # TCP/IP socket
	sock.connect(('localhost', int(sys.argv[1])))
	sock.send( message.encode() )
	return_msg= sock.recv( 1024 )
	if return_msg:
		print( 'message returned: ' + return_msg.decode() )
	else:
		print( 'other side shut down' )
	sock.close()


import numpy as np
import dnstwisterAPI as dnstwisterAPI

import datetime

class Analyser():

    def split_name(self, url):
        """
        Splits an url between the name and the extension and removes the www if it exists
        Example: 'www.google.com' -> ('google', '.com')

        url: the url to split
        returns: tuple (name, extension)
        """
        url_splitted = url.split('.')
        extension = '.' + url_splitted.pop(-1)
        if (url_splitted[0] == 'www'):
            del url_splitted[0]
        name = '.'.join(url_splitted)
        return (name, extension)

    def __init__(self, url, authority):
        self.url = url
        if len(url) != 0:
            (self.name, self.extension) = self.split_name(self.url)
            if authority is not None:
                self.authority = authority.lower()
            else:
                self.authority = ""
            self.api = dnstwisterAPI.dnstwisterAPI(self.url)
        self.number_features = 16 # Number of feature we can compute

        # Taken from research paper / web-site ( https://security-soup.net/good-domains-for-bad-guys-the-riskiest-tlds-for-malware-and-phishing/)
        self.suspiciousTLDs = ['.bank', '.online', '.business', '.party', '.cc', '.pw', '.center', '.racing', '.cf',
                               '.ren', '.click', '.review', '.club', '.science', '.country', '.stream', '.download',
                               '.study', '.ga', '.support', '.gb', '.tech', '.gdn', '.tk', '.gq', '.top', '.info',
                               '.vip', '.kim', '.loan', '.work', '.men', '.win', '.ml', '.xyz', '.mom', '.xin',
                               '.jetzt', '.bid', '.ren', '.trade', '.date', '.wang', 'accountants', '.cricket', '.link',
                               '.rest', '.casa', '.tw', '.buzz', '.faith', '.ref', '.biz', '.tokyo', '.ooo',
                               '.yokohama', '.ryukyu', '.world', '.asia']

        # Original domain + country with +50M ppl
        self.nonSuspiciousTLDs = ['.com', '.org', '.net', '.eu', '.int', '.edu', '.gov', '.mil', '.cn', '.in', '.us',
                                  '.id', '.pk', '.br', '.ng', '.bd', '.ru', '.mx', '.jp', '.ph', '.cd', '.eg', '.et',
                                  '.vn', '.ir', '.tr', '.de', '.fr', '.uk', '.th', '.it', '.za', '.il', '.tz', '.mm',
                                  '.kr', '.co', '.tel', '.kw', '.jobs']

        self.suspicious_keywords_list = ['activity', 'office', 'appleid', 'outlook', 'poloniex', 'facebook',
                                         'moneygram', 'overstock', 'skype', 'alert', 'online', 'icloud', 'office365',
                                         'coinhive', 'tumblr', 'westernunion', 'alibaba', 'github', 'purchase',
                                         'recover', 'iforgot', 'microsoft', 'bithumb', 'reddit', 'bankofamerica',
                                         'aliexpress', 'authentication', 'safe', 'itunes', 'windows', 'kraken',
                                         'youtube', 'wellsfargo', 'leboncoin', 'authorize', 'secure', 'apple',
                                         'protonmail localbitcoin', 'twitter', 'paypal', 'amazon', 'netflix', 'bill',
                                         'security', 'tutanota', 'bitstamp', 'linkedin', 'citigroup', 'client',
                                         'service', 'hotmail', 'bittrex', 'instagram', 'santander support',
                                         'transaction', 'gmail', 'blockchain', 'flickr', 'morganstanley', 'unlock',
                                         'update', 'google', 'bitflyer', 'whatsapp', 'barclays', 'wallet', 'account',
                                         'outlook', 'coinbase', 'hsbc', 'form', 'login', 'yahoo', 'hitbtc', 'scottrade',
                                         'log-in', 'password', 'google', 'lakebtc', 'ameritrade', 'live', 'signin',
                                         'yandex', 'bitfinex', 'merilledge', 'manage', 'sign-in', 'bitconnect', 'bank',
                                         'verification', ' verify', 'coinsbank', 'webscr', 'invoice', 'authenticate',
                                         ' confirm', 'credential', 'customer', 'invoice', 'post', 'document', 'postal',
                                         'calculations', 'copy', 'fedex', 'statement', 'financial', 'dhl', 'usps',
                                         'notification', 'n', 'irs', 'ups', 'no', 'delivery', 'ticket', 'https', 'http',
                                         'auth', 'access', 'account', 'admin', 'agree', 'blue', 'business', 'cdn',
                                         'choose', 'claim', 'cl', 'click', 'confirm', 'confirmation', 'connect',
                                         'download', 'enroll', 'find', 'group', 'http', 'https', 'https-www', 'install',
                                         'login', 'mobile', 'mail', 'my', 'online', 'pay', 'payment', 'payments',
                                         'portal', 'recovery', 'register', 'ssl', 'safe', 'secure', 'security',
                                         'service', 'services', 'signin', 'signup', 'support', 'summary', 'update',
                                         'user', 'verify', 'verification', 'view', 'ww', 'www', 'web']

        self.suspicious_characters_list = ['@', '-']

        self.free_certificates_authorities = ["hubspot", "let's encrypt", "comodo", "cloudflare", "ssl for free",
                                              "godaddy", "geoTrust", "gogetssl", "instantssl", "basicssl", "zerossl",
                                              "certbot", "wosign", "free ssl space", "cacert", "startssl", "free ssl",
                                              "free ssl certificate", "gandi", "sectigo", "digicert",
                                              "wosign’s kuaissl"]
        self.suspicious_hosting = ["namecheap", "godaddy", "namesilo"]

    def levenshtein_distance(self, word):
        l1, l2 = len(self.name), len(word)
        d = np.zeros((l1 + 1, l2 + 1))
        subCost = 0

        for i in range(l1 + 1):
            d[i, 0] = i
        for j in range(l2 + 1):
            d[0, j] = j

        for i in range(1, l1 + 1):
            for j in range(1, l2 + 1):
                if self.name[i - 1] == word[j - 1]:
                    subCost = 0
                else:
                    subCost = 1
                d[i, j] = min(d[i - 1, j] + 1, d[i, j - 1] + 1, d[i - 1, j - 1] + subCost)

        return int(d[l1, l2])

    def levenshtein(self):
        ld = []
        for word in self.suspicious_keywords_list:
            ld.append(self.levenshtein_distance(word))
        return min(ld)
        """
        Indicates if the certificate authority is a free certificate authority

        return: bool : true if certificate authority is free, false if not free
        """

    def issued_from_free_CA(self):
        res = False
        if self.authority == "":
            return res
            for ca in self.free_certificates_authorities:
                if ca in self.authority:
                    return res
        return res

    def deeply_nested_subdomains(self):
        """
        Give the number of '.' in the name which can be an indicator of suspiciousness if it is too high (F2 property)

        returns : nb of '.' in the name
        """
        return self.name.count('.')

    def suspicious_tld(self):
        """
        Say if the domain name is suspicious, not suspicious or unknown (F4 property)

        returns: 0 if unknown, 1 if non-suspicious, 2 if suspicious
        """

        if self.extension in self.suspiciousTLDs:
            return 2
        elif self.extension in self.nonSuspiciousTLDs:
            return 1
        else:
            return 0

    def inner_tld_in_subdomain(self):
        """
        Say if there is a domain name into the name, maybe to trick a user (F5 property)

        returns: True if there is at least one, False if not
        """

        for extension in self.nonSuspiciousTLDs:
            if extension in self.name:
                return 1
        return 0

    def suspicious_keywords(self):
        """
        Checks if there is suspicious keywordsin the name (F6 property)

        returns: ture if there is at least one
        """

        for keyword in self.suspicious_keywords_list:
            if keyword in self.name:
                if keyword == self.name:
                    # google.com isn't suspicious because there is google in the name
                    return 0
                else:
                    return 1
        return 0

    def hyphens_in_subdomain(self):
        """
        Give the number of "'" in the name which can be an indicator of suspiciousness if it is too high (F8 property)

        returns : nb of "'" in the name
        """
        return self.name.count("'")

    def suspicious_domain_length(self):
        """
        Analyse the length of the name which can be an indicator of suspiciousness if it is too high (F9 property)

        returns true if length is >= 54 ( Statistical analysis )
        """
        if len(self.name) >= 54:
            return 1
        else:
            return 0

    def suspicious_characters(self):
        """
        Checks if there is suspicious characters in the name (F10 property)

        returns: true if there is at least one
        """

        for ch in self.suspicious_characters_list:
            if ch in self.name:
                return 1
        return 0

    def suspicious_age_domain(self):
        """
        Checks if the domain age is suspicious (F11 property)
        dnstwister API
        Age Of Domain <= 6 months -> Phishing
        """
        today = datetime.date.today()
        d1 = today.strftime("%Y-%m-%d")
        #print("d1 =", d1)
        date = self.api.requestDateCreation()
        if date == {}:
            return True
        if self.api.numMonth(d1,date) > 6 :
            return False
        else :
            return True

    def suspicious_date_creation(self):
        """
        date_creation in last month -> phishing
        dnstwister API date = api.requestDateCreation(self.name+self.extension)
        """
        today = datetime.date.today()
        d1 = today.strftime("%Y-%m-%d")
        #print("d1 =", d1)
        date = self.api.requestDateCreation()
        if date == {}:
            return True
        if self.api.numMonth(d1,date) == 0 :
            return True
        else :
            return False

    def suspicious_date_expiry(self):
        """
        Registry Expiry Date <= 1 year -> Phishing
        dnstwister API
        """
        today = datetime.date.today()
        d1 = today.strftime("%Y-%m-%d")
        #print("d1 =", d1)
        date = self.api.requestExpiryDate()
        if date == {}:
            return True
        if self.api.numMonth(d1,date) > 12 :
            return False
        else :
            return True

    def suspicious_valid_period_domain(self):
        """
        Registry Expiry Date - Creation Date <= 1 year -> Phishing
        dnstwister API
        """

        dateEx = self.api.requestExpiryDate()
        dateCr = self.api.requestDateCreation()
        if dateEx == {} or dateCr == {}:
            return True
        if self.api.numMonth(dateEx,dateCr) > 12 :
            return False
        else :
            return True

    def suspicious_registrant_name(self):
        """
        if Api.requestRegistrantName(self.name+self.extension) return {} is suspicious
        dnstwister API
        """

        name = self.api.requestRegistrantName()
        if name == {}:
            return True
        return False

    def suspicious_registrant_organization(self):
        """
        if Api.requestRegistrantOrganization(self.name+self.extension) return {} is suspicious
        dnstwister API
        """

        name = self.api.requestRegistrantOrganization()
        if name == {}:
            return True
        return False

    def suspicious_registrarURL(self):
        """
        if Api.requestRegistrantURL_Host(self.name+self.extension) return {} is suspicious
        dnstwister API
        """

        name = self.api.requestRegistrarURL_Host()
        if name == {}:
            return True
        for host in self.suspicious_hosting:
            if host in self.name:
                return True
        return False

    def suspicious_parkerURL(self):
        """
       return requestParkedCheckUrl(self.name + self.extension) return score  ( > 0.50 good parameter -> possibly no phishing )
       dnstwister API
       """

        if (self.suspicious_age_domain() and   self.suspicious_date_creation() and self.suspicious_date_expiry() and self.suspicious_valid_period_domain()):
            value = self.api.requestParkedCheckUrl()
            if value > 0.50 :
                return False
            else:
                return True
        else:
          return False

"""
1 Gaston
3 Morgane
2-4-5-6-8 Done here
7 to discuss with the teatcher
"""


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

    walle-web

    :copyright: © 2015-2017 walle-web.io
    :created time: 2017-03-25 11:15:01
    :author: wushuiyong@walle-web.io
"""

from flask import request
from walle.api.api import SecurityResource
from walle.form.server import ServerForm
from walle.model.server import ServerModel
from walle.service.extensions import permission
from walle.service.rbac.role import *

class ServerAPI(SecurityResource):
    def get(self, id=None):
        """
        fetch environment list or one item
        /environment/<int:env_id>

        :return:
        """
        super(ServerAPI, self).get()

        return self.item(id) if id else self.list()

    def list(self):
        """
        fetch environment list

        :return:
        """
        page = int(request.args.get('page', 0))
        page = page - 1 if page else 0
        size = int(request.args.get('size', 10))
        kw = request.values.get('kw', '')

        server_model = ServerModel()
        server_list, count = server_model.list(page=page, size=size, kw=kw)
        return self.list_json(list=server_list, count=count, enable_create=permission.enable_role(MASTER))

    def item(self, id):
        """
        获取某个用户组

        :param id:
        :return:
        """

        server_model = ServerModel(id=id)
        server_info = server_model.item()
        if not server_info:
            return self.render_json(code=-1)
        return self.render_json(data=server_info)

    def post(self):
        """
        create a environment
        /environment/

        :return:
        """
        super(ServerAPI, self).post()

        form = ServerForm(request.form, csrf_enabled=False)
        if form.validate_on_submit():
            server_new = ServerModel()
            id = server_new.add(name=form.name.data, host=form.host.data)
            if not id:
                return self.render_json(code=-1)

            return self.render_json(data=server_new.item(id))
        else:
            return self.render_json(code=-1, message=form.errors)

    def put(self, id):
        """
        update environment
        /environment/<int:id>

        :return:
        """
        super(ServerAPI, self).put()

        form = ServerForm(request.form, csrf_enabled=False)
        form.set_id(id)
        if form.validate_on_submit():
            server = ServerModel(id=id)
            ret = server.update(name=form.name.data, host=form.host.data)
            return self.render_json(data=server.item())
        else:
            return self.render_json(code=-1, message=form.errors)

    def delete(self, id):
        """
        remove an environment
        /environment/<int:id>

        :return:
        """
        super(ServerAPI, self).delete()

        server_model = ServerModel(id=id)
        server_model.remove(id)

        return self.render_json(message='')


'''Scheduled tasks tests module'''
from .base_test import BaseTestCase
from ..api.utils.notifications.email_notices import mail_success_ops

class SuccessOpsWeeklyTaskTestCase(BaseTestCase):
    '''Test app scheduler'''

    def test_that_mail_success_ops_works(self):
        '''
        Test that the mail success ops scheduled task works
        '''
        self.assertFalse(mail_success_ops(self.app))

        self.successops_role.save()
        self.client.get(
            f'/api/v1/logged-activities?paginate=false',
            headers=self.success_ops
        )
        self.log_alibaba_challenge.status = 'pending'
        self.log_alibaba_challenge.save()

        self.assertTrue(mail_success_ops(self.app)[0])


import operator
import sqlalchemy as sa
from sqlalchemy_utils import primary_keys, identity
from .utils import tx_column_name, end_tx_column_name

def eq(tuple_):
    return tuple_[0] == tuple_[1]

def parent_identity(obj_or_class):
    return tuple(
        getattr(obj_or_class, column.name)
        for column in primary_keys(obj_or_class)
        if column.name != tx_column_name(obj_or_class)
    )

class VersionObjectFetcher(object):
    def __init__(self, manager):
        self.manager = manager

    def previous(self, obj):
        """
        Returns the previous version relative to this version in the version
        history. If current version is the first version this method returns
        None.
        """
        return self.previous_query(obj).first()

    def index(self, obj):
        """
        Return the index of this version in the version history.
        """
        session = sa.orm.object_session(obj)
        return session.execute(self._index_query(obj)).fetchone()[0]

    def next(self, obj):
        """
        Returns the next version relative to this version in the version
        history. If current version is the last version this method returns
        None.
        """
        return self.next_query(obj).first()

    def parent_identity_correlation(self, obj):
        return map(
            eq,
            zip(
                parent_identity(obj.__class__),
                parent_identity(obj)
            )
        )

    def _transaction_id_subquery(self, obj, next_or_prev='next'):
        if next_or_prev == 'next':
            op = operator.gt
            func = sa.func.min
        else:
            op = operator.lt
            func = sa.func.max

        alias = sa.orm.aliased(obj)
        query = (
            sa.select(
                [func(
                    getattr(alias, tx_column_name(obj))
                )],
                from_obj=[alias.__table__]
            )
            .where(
                sa.and_(
                    op(
                        getattr(alias, tx_column_name(obj)),
                        getattr(obj, tx_column_name(obj))
                    ),
                    *map(eq, zip(parent_identity(alias), parent_identity(obj)))
                )
            )
            .correlate(alias.__table__)
        )
        return query

    def _next_prev_query(self, obj, next_or_prev='next'):
        session = sa.orm.object_session(obj)

        return (
            session.query(obj.__class__)
            .filter(
                sa.and_(
                    getattr(
                        obj.__class__,
                        tx_column_name(obj)
                    )
                    ==
                    self._transaction_id_subquery(
                        obj, next_or_prev=next_or_prev
                    ),
                    *self.parent_identity_correlation(obj)
                )
            )
        )

    def _index_query(self, obj):
        """
        Returns the query needed for fetching the index of this record relative
        to version history.
        """
        alias = sa.orm.aliased(obj)

        subquery = (
            sa.select([sa.func.count('1')], from_obj=[alias.__table__])
            .where(
                getattr(alias, tx_column_name(obj))
                <
                getattr(obj, tx_column_name(obj))
            )
            .correlate(alias.__table__)
            .label('position')
        )
        query = (
            sa.select([subquery], from_obj=[obj.__table__])
            .where(
                sa.and_(
                    *map(eq, zip(identity(obj.__class__), identity(obj)))
                )
            )
            .order_by(
                getattr(obj.__class__, tx_column_name(obj))
            )
        )
        return query

class SubqueryFetcher(VersionObjectFetcher):
    def previous_query(self, obj):
        """
        Returns the query that fetches the previous version relative to this
        version in the version history.
        """
        return self._next_prev_query(obj, 'previous')

    def next_query(self, obj):
        """
        Returns the query that fetches the next version relative to this
        version in the version history.
        """
        return self._next_prev_query(obj, 'next')

class ValidityFetcher(VersionObjectFetcher):
    def next_query(self, obj):
        """
        Returns the query that fetches the next version relative to this
        version in the version history.
        """
        session = sa.orm.object_session(obj)

        return (
            session.query(obj.__class__)
            .filter(
                sa.and_(
                    getattr(obj.__class__, tx_column_name(obj))
                    ==
                    getattr(obj, end_tx_column_name(obj)),
                    *self.parent_identity_correlation(obj)
                )
            )
        )

    def previous_query(self, obj):
        """
        Returns the query that fetches the previous version relative to this
        version in the version history.
        """
        session = sa.orm.object_session(obj)

        return (
            session.query(obj.__class__)
            .filter(
                sa.and_(
                    getattr(obj.__class__, end_tx_column_name(obj))
                    ==
                    getattr(obj, tx_column_name(obj)),
                    *self.parent_identity_correlation(obj)
                )
            )
        )


#      888 ,e,                                     d8   888
#  e88 888  "   dP"Y 888 888 8e   ,"Y88b 888 8e   d88   888  ,e e,
# d888 888 888 C88b  888 888 88b "8" 888 888 88b d88888 888 d88 88b
# Y888 888 888  Y88D 888 888 888 ,ee 888 888 888  888   888 888   ,
#  "88 888 888 d,dP  888 888 888 "88 888 888 888  888   888  "YeeP"

# Copyright 2021 Gary Stidston-Broadbent
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =============================================================================
import shutil
from pathlib import Path
import pytest

@pytest.fixture(scope='function')
def datadir(tmpdir, request):
    """Fixture responsible for searching a folder with the same name of test
    module and, if available, moving all contents to a temporary directory so
    tests can use them freely.
    """
    filename = Path(request.module.__file__)
    test_dir = Path(filename.parent, 'data', filename.stem)

    if test_dir.is_dir():
        shutil.rmtree(str(tmpdir), ignore_errors=True)
        shutil.copytree(test_dir, str(tmpdir))

    return tmpdir

@pytest.fixture(scope="session")
def httpserver_listen_address():
    """Use port 9090 to ensure not to conflict with Area28s main port."""
    return ("127.0.0.1", 9090)


"""Minimization of Homogenous Loss Functions
Author: Gabriel Fernandez
Date: 09/21/2018
git: gabriel80808
"""

import argparse
import numpy as np
import tensorflow as tf
import schema.loss_functions as lf

# Uncomment bottom out after debugging
tf.enable_eager_execution

def parse():
    """Pass in arguments form user for experiments"""
    parser = argparse.ArgumentParser()
    parser.add_argument('--initial_guess', help='Initial values or guess where minimum', default=None)
    parser.add_argument('--loss_func', type=str, help='Name of loss function to be minimized', default=None)
    parser.add_argument('--exp_name', '-n', type=str)
    args = parser.parse_args()

    return args

# def min_itr():
#     algo = JOpt(
#         x0,
#         loss_function,
#         gamma0=2,
#         flb=2)
#     algo.minimize()

if __name__ == '__main__':
    args = parse()
    loss_func = lf.HomogeneousQuartic(
        x0=np.array([5, 5, 5, 5]), flb=0)
    f = loss_func.eval_func(np.array([5.0, 7.1, 5.3, 6.5])[np.newaxis, :])
    g = loss_func.eval_grad(np.array([5, 7, 5, 6]))
    # min_itr()


import os
import json

folders = os.listdir(".")
folders = [f for f in folders if not f.startswith('.')]
folders = [f for f in folders if not f.endswith('.py')]

data = {}

for f in folders:
    files = os.listdir(f)
    files = [f + os.sep + fi for fi in files]
    files = [fi for fi in files if os.path.splitext(fi)[1].lower() in [".png", ".gif", ".jpeg", ".jpg"]]
    data[f] = files

print(data)

with open('images.json', 'w') as fp:
    json.dump(data, fp)


import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
import os

PATH_TEST = os.path.dirname(os.path.realpath(__file__))

url = "http://127.0.0.1:8000/alignments/"
f = open(os.path.join(PATH_TEST, 'example/umbrella_line.txt'), 'rb')

alignment_data = MultipartEncoder(
    fields={
            # a file upload field
            'lyrics': ('test_long_lyrics.txt', f, 'text/plain'),
            # plain text fields
            'title': 'new composition',
            'accompaniment': '2',
            'level':'1'

           }
    )

response = requests.post(url, data=alignment_data, headers={'Content-Type': alignment_data.content_type})
response.raise_for_status()
print(response.text)


# Prime Finder v1.0 (dev)
# By Ethan Crooks

import math
import time

count = 1
recp = 1

def isprime(num):
    #print(str(num))
    num = float(num)
    prime = True
    if math.fmod(num,2) == 0.0:
        prime = False
    if math.fmod(num,3) == 0.0:
        prime = False
    if math.fmod(num,5) == 0.0:
        prime = False
    if math.fmod(num,7) == 0.0:
        prime = False
    if num == 2.0 or num == 3.0 or num == 5.0 or num == 7.0:
        prime = True
    if num == 1.0:
        prime = False
    #print(str(prime))
    return prime

def gettestnum():
    print('Type in number to test.')
    a = input('>>> ')
    try:
        int(a)
    except ValueError:
        print('[ValueError]')
        print('Needs to be a whole number!')
        print()
        gettestnum()
    return a

def testsingle():
    a = gettestnum()
    result = isprime(float(a))
    if result == True:
        print(a + ' is a prime!')
    else:
        print(a + " isn't a prime!")
    print()
    main()

def testall():
    global count
    global recp
    while True:
        totalcount = 1
        result = isprime(float(count))
        if result == True:
            print(str(count))
        count += 1
    if recp >= 950:
        pass
    else:
        recp += 1
        testall()

testall()


import tweepy
import credentials
import twitterSettings
from twilio.rest import Client

def returnTweets(username):
    auth = tweepy.OAuthHandler(credentials.twitter["consumer_key"], credentials.twitter["consumer_secret"])
    auth.set_access_token(credentials.twitter["access_token"], credentials.twitter["access_token_secret"])
    api = tweepy.API(auth)

    # Will be sent as a text message
    toText = []

    for tweet in tweepy.Cursor(api.user_timeline, id=username).items():
        # Twitter's api doesn't provide a way to find tweets within a certain date range, so we'll use likes
        if not tweet.favorited:
            api.create_favorite(tweet.id)
            addToText = {}
            addToText["author"] = tweet.user.name
            addToText["message"] = tweet._json["text"]
            toText.append(addToText)
        else:
            # Once we hit the first tweet that has been liked, we can stop looping over the rest
            return toText

def sendTexts(tweetQueue):
    # Since I'm paying per text, I'm sending everything in one text.
    # It's less readable, but I don't want to spend much on a side project
    client = Client(credentials.twilio["accountSID"], credentials.twilio["authToken"])
    for number in credentials.twilio["cellPhone"]:
        client.messages.create(body=str(tweetQueue), from_=credentials.twilio["twilioNumber"], to=number)

if __name__ == "__main__":
    tweetQueue = []
    for user in twitterSettings.followNames:
        tweetQueue += returnTweets(user)
    if tweetQueue:
        sendTexts(tweetQueue)


# -*- coding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2016 Pharmadus. All Rights Reserved
#    $Óscar Salvador <oscar.salvador@pharmadus.com>$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp import models, api, exceptions, _

class AccountInvoiceConfirm(models.TransientModel):
    _inherit = 'account.invoice.confirm'

    @api.multi
    def invoice_confirm(self):
        invoice_ids = self.env.context.get('active_ids', [])
        invoices = self.env['account.invoice'].browse(invoice_ids)
        orphan_invoices = []
        for invoice in invoices:
            if invoice.payment_mode_id and \
               invoice.payment_mode_id.banking_mandate_needed and \
               not invoice.mandate_id:
                orphan_invoices.append(invoice)

        if len(orphan_invoices) > 0:
            invoice_list = '\n'.join([x.origin if x.origin \
                                               else x.partner_id.name \
                                      for x in orphan_invoices])
            raise exceptions.Warning(_('The following invoices do not have the'
                                       ' needed mandates assigned:\n%s' %
                                       invoice_list))

        return super(AccountInvoiceConfirm, self).invoice_confirm()


from facturacion_clientes import models

def crear_factura_de_liqueed_a_cliente(nombre_cliente, ingreso, gastos, fecha, context):
    cliente = models.Cliente.objects.get(nombre=nombre_cliente)
    factura = models.FacturaDeLiqueedACliente(
        cliente=cliente,
        monto=ingreso,
        gastos=gastos,
        fecha=fecha
    )
    factura.save()
    context.ultima_factura = factura

def crear_factura_de_consultor_a_cliente(nombre_consultor, nombre_cliente, ingreso, gastos, fecha, context):
    cliente = models.Cliente.objects.get(nombre=nombre_cliente)
    consultor = models.Consultor.objects.get(nombre=nombre_consultor)
    factura = models.FacturaDeConsultorACliente(
        consultor=consultor,
        cliente=cliente,
        monto=ingreso,
        gastos=gastos,
        fecha=fecha
    )
    factura.save()
    context.ultima_factura = factura


import json
from channels.generic.websocket import (
    AsyncWebsocketConsumer, WebsocketConsumer)
from asgiref.sync import async_to_sync
from random import randint

class SpinConsumer(AsyncWebsocketConsumer):
    async def connect(self):
        # self.spin_name = self.scope['url_route']['kwargs']['spin_name']
        self.spin_group_name = 'daru_spin'

        # Join spin group
        await self.channel_layer.group_add(
            self.spin_group_name,
            self.channel_name
        )

        await self.accept()

    async def disconnect(self, close_code):

        await self.channel_layer.group_discard(
            self.spin_group_name,
            self.channel_name
        )

    # Receive pointer from WebSocket
    async def receive(self, text_data): # not needed if FrontEnd is not communicating/Our case
        text_data_json = json.loads(text_data)
        pointer = text_data_json['pointer']

        print(f'MESWEB{pointer}')

        # Send pointer to spin group
        await self.channel_layer.group_send(
            self.spin_group_name,
            {
                'type': 'spin_pointer',
                'pointer': pointer,
            }
        )

    # Receive pointer from spin group
    async def spin_pointer(self, event):
        pointer = event['pointer']
        # secondvalu = event['secondvalu']

        print(f'SPINNER{ pointer}')

        # Send pointer to WebSocket
        await self.send(text_data=json.dumps({
            'pointer': pointer,
            # 'second_valu':secondvalu
        }))

    # Receive pointer from spin group
    async def second_value(self, event):
        secondvalu = event['secondvalu']
        # mssg = ''
        # if secondvalu >50:
        #     mssg ='Betting is Active'
        # else:
        #    mssg ='Wait for next Market'
        # print(f'TIMER:{secondvalu}')

        # Send secondvalu to WebSocket
        await self.send(text_data=json.dumps({
            'secondvalu': secondvalu,
            # 'mssg':mssg,
        }))
    # Receive pointer from spin group

    async def market_info(self, event):
        market = event['market']

        print(f'MAKO {market}')

        # Send pointer to WebSocket
        await self.send(text_data=json.dumps({
            'market': market
        }))

class IspinConsumer(AsyncWebsocketConsumer):
    async def connect(self):
        # self.spin_name = self.scope['url_route']['kwargs']['spin_name']
        self.spin_group_name = 'i_spin'

        # Join spin group
        await self.channel_layer.group_add(
            self.spin_group_name,
            self.channel_name
        )

        await self.accept()

    async def disconnect(self, close_code):

        await self.channel_layer.group_discard(
            self.spin_group_name,
            self.channel_name
        )

    # Receive pointer from WebSocket
    async def receive(self, text_data): # not needed if FrontEnd is not communicating/Our case
        text_data_json = json.loads(text_data)
        ipointer = text_data_json['ipointer']

        print(f'I_MESWEB{ipointer}')

        # Send pointer to spin group
        await self.channel_layer.group_send(
            self.spin_group_name,
            {
                'type': 'ispin_pointer',
                'ipointer': ipointer,
            }
        )

    # Receive pointer from spin group
    async def ispin_pointer(self, event):
        ipointer = event['ipointer']
        # secondvalu = event['secondvalu']

        print(f'I_SPINNER{ ipointer}')

        # Send pointer to WebSocket
        await self.send(text_data=json.dumps({
            'ipointer': ipointer,
            # 'second_valu':secondvalu
        }))

from .models import IoutCome
class QspinConsumer(WebsocketConsumer):
    def connect(self):
        self.accept()

    def disconnect(self, close_code):
        pass

    # Receive pointer from spin group
    def return_pointer(self):#user_id):
        # slist =IoutCome.objects.filter(user_id=user_id)
        # act_l=[slval in slist if slval.active=True]
        # if len(act_l)==0:
        #     return ""
        # else:
        #     act_l[0].active = False  # update field/
        #     return act_l[0].ipointer
        #
        #
        #
        return [6,'']

        return randint(1,28),2 #IoutCome.objects.get(id=5).pointer

    def receive(self, text_data):
        text_data_json = json.loads(text_data)
        ipointer = text_data_json['ipointer']
        ipointer = self.return_pointer()[0]
        spinet = self.return_pointer()[1]

        self.send(text_data=json.dumps({
            'ipointer': ipointer,
            'spinet': spinet
        }))

        #normal ttp request return no of available spins//


import numpy as np
from matplotlib import pyplot as plt

def cosmo():

    '''
    Construct plots for my introduction

    '''
    scaleFactor()

def scaleFactor():

    '''
    Determine the scale factor as function of time
    for three different curvatures

    H^2 = H0^2( omega_a + ...)
    H = H0( omega ...)^1/2.
    da/a *(omega)^-1/2 = H0dt

    H0t = int[ ( omega_m a^-1 + omega_r a^-2 + omega_l a^2 + (1-Omega_k) )^-1/2 da]

    Matter
    -------
    H0t = int[ a^3/2 / a da ]
    H0t = 2/3a^3/2
    a = 3H0/2 t^2/3

    H0t = int[ (a^-1 + (1-k))^{-1/2} ]

    u = x + b
    du = dx
    h0t = int[ du u^-1/2 ]
    h0t = 2 u^1/2
    h0t = 2( a + (1-k))^1/2

    '''
    h0 = 70
    #Matter dominated and flat
    a = np.arange(10000)/100.+0.0001
    t_m_flat = hubble( a, 0.3, 0., 0.7 )
    t_m_open = hubble( a, 0., 0., 0.  )
    t_m_closed = hubble( a, 1., 0., 0. )
    t_m_closed = hubble( a, 1.1, 0., 0. )

    plt.plot( t_m_flat, a,   'r.', \
              t_m_open, a,  'b.', \
              t_m_closed, a,  'g.', \
              t_m_closed, a,  'y.')

    plt.yscale('log')
    plt.xscale('log')

    plt.show()

def hubble(a, omega_m, omega_r, omega_l):

    omega_k =  omega_m + omega_r + omega_l

    t = np.zeros( len(a), 'double')
    increments = 1000
    da = a / float(increments)

    for i in xrange(increments):

        ia = da * float(i+1)

        iT = (omega_m*ia**(-1.) + \
              omega_r*ia**(-2.)  + \
              omega_l*ia**2.     + \
        (1.0 - omega_k))

        ABSiT = (np.abs(iT))**(-0.5)*da

        t += ABSiT

    return t

if __name__ == '__main__':
    cosmo()


'''
Created on 26.01.2016

@author: fabian
'''
import logging
from lxml import etree
from patches.mavenfilepatches import MavenFilePatch
import multiprocessing
from parser.baseparser import BaseParser
import os
import evotest.util
import subprocess
from evotest.error import FileError
import sys

class MavenParser(BaseParser):
    '''
    classdocs
    '''

    def __init__(self):
        '''
        Constructor
        '''
        self.logger = logging.getLogger("parser")
        self.root = None

    @property
    def name(self):
        return "mavenParser"

    def getBuildFile(self, processingCommit):
        """
        Try to find the correct pom in the project.

        If there are more valid poms, that have a surefire-plugin in it, abort.

        :param processingCommit: current commit that is processed
        :return: path to the buildfile
        """

        standardMavenBuildFile = processingCommit.workingDirectory+"/pom.xml"
        if(os.path.exists(standardMavenBuildFile) and
           self.checkIfXMLIsMavenBuildFile(standardMavenBuildFile)):
            self.logger.debug("Found the following build files for mavenParser: "+standardMavenBuildFile)
            return [standardMavenBuildFile]

        potentialBuildFiles = []
        for root, subFolders, files in os.walk(processingCommit.workingDirectory):

            for file in files:
                if file.endswith(".xml"):
                    if(self.checkIfXMLIsMavenBuildFile(os.path.join(root, file))):
                        potentialBuildFiles.append(os.path.join(root, file))

        if potentialBuildFiles:
            self.logger.debug("Found the following build files for mavenParser: "+','.join(potentialBuildFiles))
        return potentialBuildFiles

    def checkIfXMLIsMavenBuildFile(self, path):
        try:
            tree = etree.parse(path)
            root = tree.getroot()
            projectNode = root.xpath('/x:project', namespaces={'x': "http://maven.apache.org/POM/4.0.0"})
            buildNode = root.xpath('/x:project/x:build',
                               namespaces={'x': "http://maven.apache.org/POM/4.0.0"})
            if(projectNode and buildNode):
                return True
            else:
                return False
        except Exception as e:
            return False

    def getNewBuildFile(self, processingCommit):
        return self.buildFileRootPath+"/newBuild.xml"

    def parseErrors(self, processingCommit):
        errors = set([])
        try:
            with open(processingCommit.outputDirectory+"/build_errors.txt", "r") as buildErrorF:
                data = buildErrorF.readlines()

            for i in range(0,len(data)):
                line = data[i]
                if(line.startswith("[ERROR] Exit code: 1 - ")):
                    line = line[23:]

                if(line.startswith("[ERROR] ")):
                    line = line[8:]

                parts = line.split(":")
                parts = [part.strip() for part in parts]
                if("error" in line):
                    # Check if exact place is a new error or not
                    exactPlace = data[i+1]
                    checkExactPlace = exactPlace.split(":")
                    if(len(checkExactPlace) == 1):
                        exactPlace = exactPlace[9:].lstrip().rstrip('\r\n')
                    else:
                        exactPlace = ""

                    if(len(parts) == 3):
                        errorMessage = [part.rstrip('\r\n') for part in parts[2:]]
                    else:
                        errorMessage = [part.rstrip('\r\n') for part in parts[3:]]

                    sourceFileLineNumber = parts[1].replace("error", "")
                    sourceFileLineNumber = parts[1].replace("[", "")
                    sourceFileLineNumber = parts[1].replace("]", "")

                    errorMessage = ':'.join(errorMessage)
                    fileError = FileError(sourceFile=parts[0], sourceFileLineNumber=parts[1], errorMessage=errorMessage, exactPlace = exactPlace)
                    errors.add(fileError)

        except Exception:
            pass

        return errors

    def sanitizeParsedOutput(self, output):
        output = output.replace("[javac]", "")
        output = output.strip()

        return output

    def executeClean(self, processingCommit):
        try:
            self.logger.info("Calling mvn clean task")
            output = subprocess.check_output(["mvn", "-f", self.getNewBuildFile(processingCommit), "clean"], stderr=subprocess.STDOUT)
        except Exception as e:
            logging.error("Error calling clean: "+str(e))

    def executeCoverageAndMutationTask(self, processingCommit):
        try:
            self.logger.info("Calling mvn prepare-package task, output is written to: "+processingCommit.outputDirectory+'/build_out.txt')
            output = subprocess.check_output(["mvn", "-f", self.getNewBuildFile(processingCommit), "prepare-package"], stderr=subprocess.STDOUT)
            evotest.util.writeCommandOutputToFile(processingCommit.outputDirectory+'/build_out.txt', output)
            return True

        except subprocess.CalledProcessError as e:
            self.logger.error("Error for calling prepare-package! Output is written to: "+processingCommit.outputDirectory+'/build_errors.txt')
            evotest.util.writeCommandOutputToFile(processingCommit.outputDirectory+'/build_errors.txt', e.output)
            return False

    def isApplicable(self, processingCommit):
        buildFiles = self.getBuildFile(processingCommit)
        if(len(buildFiles) != 1):
            self.logger.debug("Number of potential build files for mavenParser is not equal to 1!")
            return False

        # Parse XML
        try:
            self.tree = etree.parse(buildFiles[0])
        except etree.XMLSyntaxError as e:
            self.logger.error("Could not parse xml file!")
            return False

        self.buildFileRootPath = os.path.dirname(os.path.abspath(buildFiles[0]))

        # Get root node
        self.root = self.tree.getroot()

        # Generate new build file
        self.generateOutputFile(processingCommit)

        return True

    def createNewBuildFile(self, processingCommit):
        self.addJacocoPlugin()
        self.addPitPlugin(processingCommit)
        self.generateOutputFile(processingCommit)

        self.executeMavenPatches(processingCommit)

    def executeMavenPatches(self, processingCommit):
        for sc in MavenFilePatch.__subclasses__():
            patch = sc(processingCommit)
            if(patch.isApplicable()):
                self.logger.info("Apply maven patch: "+patch.name)
                patch.execute()
                processingCommit.addBuildFilePatch(patch.name)

    def addJacocoPlugin(self):
        # If there is already an jacocoplugin configured, remove it
        jacocoPlugin = self.root.xpath('/x:project/x:build/x:plugins/x:plugin[x:artifactId[text()="jacoco-maven-plugin"]]', namespaces={'x' : "http://maven.apache.org/POM/4.0.0"})
        if(jacocoPlugin):
            jacocoPlugin[0].getparent().remove(jacocoPlugin[0])

        pluginsNode = self.root.xpath('/x:project/x:build/x:plugins', namespaces={'x' : "http://maven.apache.org/POM/4.0.0"})
        pluginNode = etree.SubElement(pluginsNode[0], "plugin")
        groupId = etree.SubElement(pluginNode, "groupId")
        groupId.text = "org.jacoco"
        artifactId = etree.SubElement(pluginNode, "artifactId")
        artifactId.text= "jacoco-maven-plugin"
        version = etree.SubElement(pluginNode, "version")
        version.text="0.7.5.201505241946"
        executions = etree.SubElement(pluginNode, "executions")
        execution1 = etree.SubElement(executions, "execution")
        execution1_id = etree.SubElement(execution1, "id")
        execution1_id.text = "default-prepare-agent"
        execution1_phase = etree.SubElement(execution1, "phase")
        execution1_phase.text = "initialize"
        execution1_goals = etree.SubElement(execution1, "goals")
        execution1_goal = etree.SubElement(execution1_goals, "goal")
        execution1_goal.text = "prepare-agent"

        execution2 = etree.SubElement(executions, "execution")
        execution2_id = etree.SubElement(execution2, "id")
        execution2_id.text = "default-report"
        execution2_phase = etree.SubElement(execution2, "phase")
        execution2_phase.text = "prepare-package"
        execution2_goals = etree.SubElement(execution2, "goals")
        execution2_goal = etree.SubElement(execution2_goals, "goal")
        execution2_goal.text = "report"
        execution2_configuration = etree.SubElement(execution2, "configuration")
        execution2_outputDirectory = etree.SubElement(execution2_configuration, "outputDirectory")
        execution2_outputDirectory.text = "report"

        surefirePlugin = self.root.xpath('/x:project/x:build/x:plugins/x:plugin[x:artifactId[text()="maven-surefire-plugin"]]', namespaces={'x' : "http://maven.apache.org/POM/4.0.0"})

        if(surefirePlugin):
            surefirePlugin_jacoco = surefirePlugin[0].xpath('x:configuration/x:systemPropertyVariables/x:jacoco-agent.destfile',  namespaces={'x' : "http://maven.apache.org/POM/4.0.0"})

            if(surefirePlugin_jacoco):
                surefirePlugin_jacoco[0].text = "${project.build.directory}/jacoco.exec"
            else:
                # add elements if they are not existent (e.g. configuration, systempropertyvariables and so on)
                surefirePlugin_configuration = surefirePlugin[0].xpath('x:configuration', namespaces={'x' : "http://maven.apache.org/POM/4.0.0"} )
                if(not surefirePlugin_configuration):
                    surefirePlugin_configuration = [etree.SubElement(surefirePlugin, "configuration")]

                surefirePlugin_systemPropertyVariables = surefirePlugin_configuration[0].xpath('x:systemPropertyVariables', namespaces={'x' : "http://maven.apache.org/POM/4.0.0"} )
                if(not surefirePlugin_systemPropertyVariables):
                    surefirePlugin_systemPropertyVariables = [etree.SubElement(surefirePlugin_configuration[0], "systemPropertyVariables")]

                surefirePlugin_jacoco = etree.SubElement(surefirePlugin_systemPropertyVariables[0], "jacoco-agent.destfile")
                surefirePlugin_jacoco.text = "${project.build.directory}/jacoco.exec"

    def addPitPlugin(self, processingCommit):
        # If there is already an pitplugin configured, remove it
        pitPlugin = self.root.xpath('/x:project/x:build/x:plugins/x:plugin[x:artifactId[text()="pitest-maven"]]', namespaces={'x' : "http://maven.apache.org/POM/4.0.0"})
        if(pitPlugin):
            pitPlugin[0].getparent().remove(pitPlugin[0])

        pluginsNode = self.root.xpath('/x:project/x:build/x:plugins', namespaces={'x' : "http://maven.apache.org/POM/4.0.0"})
        pluginNode = etree.SubElement(pluginsNode[0], "plugin")
        groupId = etree.SubElement(pluginNode, "groupId")
        groupId.text = "org.pitest"
        artifactId = etree.SubElement(pluginNode, "artifactId")
        artifactId.text= "pitest-maven"
        version = etree.SubElement(pluginNode, "version")
        version.text="1.1.9"
        executions = etree.SubElement(pluginNode, "executions")
        execution1 = etree.SubElement(executions, "execution")
        execution1_id = etree.SubElement(execution1, "id")
        execution1_id.text = "mutationCoverage goal"
        execution1_phase = etree.SubElement(execution1, "phase")
        execution1_phase.text = "prepare-package"
        execution1_goals = etree.SubElement(execution1, "goals")
        execution1_goal = etree.SubElement(execution1_goals, "goal")
        execution1_goal.text = "mutationCoverage"

        execution1_configuration = etree.SubElement(execution1, "configuration")
        execution1_targetClasses = etree.SubElement(execution1_configuration, "targetClasses")
        execution1_targetTests = etree.SubElement(execution1_configuration, "targetTests")

        targetTests, targetClasses = self.determineTargetTestsAndClasses(processingCommit.workingDirectory)

        for targetClass in targetClasses:
            param = etree.SubElement(execution1_targetClasses, "param")
            param.text=targetClass

        for targetTest in targetTests:
            param = etree.SubElement(execution1_targetTests, "param")
            param.text=targetTest

        execution1_outputFormats = etree.SubElement(execution1_configuration, "outputFormats")
        execution1_outputFormatsParam = etree.SubElement(execution1_outputFormats, "param")
        execution1_outputFormatsParam.text = "html"
        execution1_outputFormatsParam2 = etree.SubElement(execution1_outputFormats, "param")
        execution1_outputFormatsParam2.text = "csv"
        execution1_outputFormatsParam3 = etree.SubElement(execution1_outputFormats, "param")
        execution1_outputFormatsParam3.text = "xml"

        execution1_maxMutationsPerClass = etree.SubElement(execution1_configuration, "maxMutationsPerClass")
        execution1_maxMutationsPerClass.text = "30"

        execution1_threads = etree.SubElement(execution1_configuration, "threads")
        execution1_threads.text = str(int(multiprocessing.cpu_count()/2))

        execution1_reportDir = etree.SubElement(execution1_configuration, "reportsDirectory")
        execution1_reportDir.text = "pitReports"

        execution1_jvmArgs = etree.SubElement(execution1_configuration, "jvmArgs")
        for jvmArg in self.getJVMArgs():
            value = etree.SubElement(execution1_jvmArgs, "value")
            value.text = jvmArg

    def getJVMArgs(self):
        # Check for argline
        argLine = self.root.findall(".//x:argLine", namespaces={'x' : "http://maven.apache.org/POM/4.0.0"})
        jvmArgs = set([])

        for line in argLine:
            if(self.checkIfBelongsToSurefirePlugin(line)):
                for arg in line.text.split(" "):
                    if(arg.startswith("-D")):
                        jvmArgs.add(arg)

        systemProperties = self.root.findall(".//x:systemProperties",
                                             namespaces={'x': "http://maven.apache.org/POM/4.0.0"})
        for sysproperty in systemProperties:
            if(self.checkIfBelongsToSurefirePlugin(sysproperty)):
                for buildProperty in sysproperty:
                    jvmArg = ""
                    for value in buildProperty:
                        if("name" in value.tag):
                            jvmArg="-D"+value.text+"="
                        if("value" in value.tag):
                            jvmArg=jvmArg+value.text
                    jvmArgs.add(jvmArg)

        self.logger.debug("Found jvmArgs: "+','.join(jvmArgs))
        return jvmArgs

    def checkIfBelongsToSurefirePlugin(self, node):
        while(not node.xpath('x:artifactId', namespaces={'x' : "http://maven.apache.org/POM/4.0.0"})):
            node = node.getparent()

        artifactId = node.xpath('x:artifactId', namespaces={'x' : "http://maven.apache.org/POM/4.0.0"})
        if(artifactId and "maven-surefire-plugin" in artifactId[0].text):
            return True

        return False


import os
import cv2 as cv
import argparse
import numpy as np
import math
import linecache
from pre_process import show,clear

#rootdir = "D:/Download/PID_YOLO/train"
#savepath = "D:/Download/PID_YOLO/train/return"
rootdir = "/mnt/data/datasets/PID_YOLO/divide"  #images+labels acquire from
savepath = "/mnt/data/datasets/PID_YOLO/divide/return"  # images+labels save in
filedir = rootdir+"/coordinate"

#rootdir refers to the cropped output ,savepath refers to the path for saving postprocess
#filedir refers to the filedir saving the infomation of coordinate for each adaptive cropped images

#for post-process: re-calculate the coordinatate and then recover original image
def main():
    if os.path.isdir(savepath):
        clear(savepath)
    os.makedirs(savepath)
    #First, read the absolute coordinate of cropped image from filedir
    filelist = os.listdir(rootdir)
    filelist.sort()
    n = 0
    name = []
    #the normalization refers to the original size of image
    #n refer to the order of original image, name refers to the name of each
    for filename in filelist:
        if filename[-1] == "g" :
            print(filename)
            i = int(filename[-6])
            j = int(filename[-5])
            image = cv.imread(os.path.join(rootdir, filename))

            #if i=0 and j=0. a new original image is loaded
            #save the former completied read image
            #then record the new name, initilize the size of new image and build a new imgae as background
            if i ==0 and j==0 :
                name.append(filename[:-7])
                print(name)
                if n != 0 :
                    save = savepath + '/' + name[n-1] +'.jpg'
                    cv.imwrite(save, final_matrix)
                maximum = np.zeros(4)
                #norm function is to calculate the size of each orginal image according to the cropped image
                norm(name[n],maximum)
                #new image
                final_matrix = np.zeros((int(maximum[3]), int(maximum[1]), 3), np.uint8)
                n+=1

            #every original image calculate size once at the begining of loading
            imw = maximum[1] - maximum[0]
            imh = maximum[3] - maximum[2]

            #paste every cropped image on the background image according to the coordinate file
            #in every coordinate file, it only contains one line for the image four coordinate imformation
            #the minimum and maximum x-coordinate and the minimum and maximum y-coordinate of cropped image
            filename1 = filename[:-3] + 'txt'
            f = open(os.path.join(filedir,filename1),'r')
            lines = f.readlines()
            img = lines[0].strip().split(" ")
            img = list(map(float,img))
            #imx_min, imx_max refers to the minimum and maximum x-coordinate of each image on the orignal image respectively
            #imy_min, imy_max refers to the minimum and maximum y-coordinate of each image on the orignal image respectively
            imx_min = int(img[0])
            imx_max = int(img[1])
            imy_min = int(img[2])
            imy_max = int(img[3])
            w = imx_max - imx_min
            h = imy_max - imy_min
            #paste every cropped image, no need to worry about interception because it will cover automatically
            final_matrix[imy_min:imy_max, imx_min:imx_max] = image
            f.close()

            #then, re-calculate the related coordinate for the orginal images and save into according label file
            file = open(savepath +'/' + name[n-1]+'.txt','a+')
            f = open(os.path.join(rootdir,filename1),'r')
            lines = f.readlines()
            #calculte every bounding box related coordination to the absolute coordinate on original image
            #x_min, x_max refers to the minimum and maximum x-coordinate of each bounding box on the orignal image respectively
            #y_min, y_max refers to the minimum and maximum y-coordinate of each bounding box on the orignal image respectively
            for line in lines :
                box = line.strip().split(" ")
                box = list(map(float,box))
                xmin = (box[1]-box[3]/2)*w + imx_min
                ymin = (box[2]-box[4]/2)*h + imy_min
                xmax = (box[1]+box[3]/2)*w + imx_min
                ymax = (box[2]+box[4]/2)*h + imy_min
                #char is the related coordinate of each bounding box for original image as the YOLO format
                char = np.zeros(5)
                char[0] = int(box[0])
                char[1] = (xmin + xmax)/(2*imw)
                char[2] = (ymin + ymax)/(2*imh)
                char[3] = (xmax - xmin)/imw
                char[4] = (ymax - ymin)/imh
                char = list(map(str,char))
                file.write(' '.join(char))
                file.write('\n')
            file.close()
            f.close()
    save = savepath + '/' + name[n-1] +'.jpg'
    cv.imwrite(save, final_matrix)
    show(savepath)

def norm(name,maximum):
    filelist = os.listdir(rootdir)
    for filename in filelist:
        if filename[:-7] == name and filename[-1] == "g":
            filename1 = filename[:-3] + 'txt'
            f = open(os.path.join(filedir,filename1),'r')
            lines = f.readlines()
            img = lines[0].strip().split(" ")
            img = list(map(float,img))
            for m in range(4):
                if m %2 == 0 :
                    maximum[m] = min(maximum[m],img[m])
                else :
                    maximum[m] = max (maximum[m],img[m])

if __name__ == '__main__':
    main()