#!/usr/bin/env python2

from flask import (Flask, g, render_template, send_from_directory)
from werkzeug.routing import BaseConverter

# Initialize application objects
app = Flask(__name__)

class RegexConverter(BaseConverter):
    """RegEx class extending base converter
    adding regular expression support."""

    def __init__(self, url_map, *items):
        super(RegexConverter, self).__init__(url_map)
        self.regex = items[0]

# Add new RegEx converter class to URL Mapping for routes
app.url_map.converters['regex'] = RegexConverter

# Home page
@app.route('/')
def start():
    """Display home page"""
    return render_template('app.html')

# Resources
@app.route(r'/<regex("[^\.]+(\.(html|css|js|ico|json))?"):path>')
def send_stuff(path):
    """Generic route to fetch any resources
    required by HTML page, route is limited
    by regular expressions"""
    return send_from_directory('templates', path)

# Finally application start if file is run as main
if __name__ == '__main__':
    app.debug = True
    app.run(host='0.0.0.0', port=5000)


# Get executables

import os
import shutil

from flaky import flaky
import pymake

from build_exes import meson_build
from framework import running_on_CI

if running_on_CI():
    print("running on CI environment")
    os.environ["PYMAKE_DOUBLE"] = "1"

# path to rebuilt executables for previous versions of MODFLOW
rebuilt_bindir = os.path.join("..", "bin", "rebuilt")

if not os.path.exists(rebuilt_bindir):
    os.makedirs(rebuilt_bindir)

# paths to downloaded for previous versions of MODFLOW
downloaded_bindir = os.path.join("..", "bin", "downloaded")

if not os.path.exists(downloaded_bindir):
    os.makedirs(downloaded_bindir)

mfexe_pth = "temp/mfexes"

# use the line below to set fortran compiler using environmental variables
# os.environ["FC"] = "gfortran"

# some flags to check for errors in the code
# add -Werror for compilation to terminate if errors are found
strict_flags = (
    "-Wtabs -Wline-truncation -Wunused-label "
    "-Wunused-variable -pedantic -std=f2008 "
    "-Wcharacter-truncation"
)

def get_compiler_envvar(fc):
    env_var = os.environ.get("FC")
    if env_var is not None:
        if env_var != fc:
            fc = env_var
    return fc

def create_dir(pth):
    # create pth directory
    print(f"creating... {os.path.abspath(pth)}")
    os.makedirs(pth, exist_ok=True)

    msg = f"could not create... {os.path.abspath(pth)}"
    assert os.path.exists(pth), msg

def rebuild_mf6_release():
    target = "mf6"
    download_pth = os.path.join("temp")
    target_dict = pymake.usgs_program_data.get_target(target)

    pymake.download_and_unzip(
        target_dict["url"],
        pth=download_pth,
        verbose=True,
    )

    # update IDEVELOP MODE in the release
    srcpth = os.path.join(
        download_pth, target_dict["dirname"], target_dict["srcdir"]
    )
    fpth = os.path.join(srcpth, "Utilities", "version.f90")
    with open(fpth) as f:
        lines = f.read().splitlines()
    assert len(lines) > 0, f"could not update {srcpth}"

    f = open(fpth, "w")
    for line in lines:
        tag = "IDEVELOPMODE = 0"
        if tag in line:
            line = line.replace(tag, "IDEVELOPMODE = 1")
        f.write(f"{line}\n")
    f.close()

    # build release source files with Meson
    root_path = os.path.join(download_pth, target_dict["dirname"])
    meson_build(dir_path=root_path, libdir=os.path.abspath(rebuilt_bindir))

def test_create_dirs():
    pths = [os.path.join("..", "bin"), os.path.join("temp")]

    for pth in pths:
        create_dir(pth)

@flaky(max_runs=3)
def test_getmfexes(verify=True):
    pymake.getmfexes(mfexe_pth, verify=verify)
    for target in os.listdir(mfexe_pth):
        srcpth = os.path.join(mfexe_pth, target)
        if os.path.isfile(srcpth):
            dstpth = os.path.join(downloaded_bindir, target)
            print(f"copying {srcpth} -> {dstpth}")
            shutil.copy(srcpth, dstpth)

@flaky(max_runs=3)
def test_rebuild_mf6_release():
    rebuild_mf6_release()

if __name__ == "__main__":
    test_create_dirs()
    test_getmfexes(verify=False)
    test_rebuild_mf6_release()


# -*- coding: utf-8 -*-
"""
@author:XuMing(xuming624@qq.com)
@description:

前后词互信息PMI: 计算两个词语之间的关联程度，pmi越大越紧密
左右邻熵：左右熵值越大，说明该词的周边词越丰富，意味着词的自由程度越大，其成为一个独立的词的可能性也就越大。
"""

import os
import re
from collections import Counter

import numpy as np

from wordrank import config
from wordrank.utils.io_utils import save_json, load_json
from wordrank.utils.logger import logger

def ngram_freq(text, ngram=4):
    """
    Get Ngram freq dict
    :param text: input text, sentence
    :param ngram: N
    :return: word frequency dict
    """
    words = []
    for i in range(1, ngram + 1):
        words += [text[j:j + i] for j in range(len(text) - i + 1)]
    words_freq = dict(Counter(words))
    return words_freq

def generate_pmi_score(word_freq_dict, pmi_path):
    """
    Generate PMI score
    :param word_freq_dict: dict
    :return: dict(word, float), pmi score
    """
    result = dict()
    for word in word_freq_dict:
        if len(word) > 1:
            p_x_y = min([word_freq_dict.get(word[:i]) * word_freq_dict.get(word[i:]) for i in range(1, len(word))])
            score = p_x_y / word_freq_dict.get(word)
            result[word] = score
    save_json(result, pmi_path)
    return result

def entropy_score(char_list):
    """
    Get entropy score
    :param char_list: input char list
    :return: float, score
    """
    char_freq_dict = dict(Counter(char_list))
    char_size = len(char_list)
    entropy = -1 * sum([char_freq_dict.get(i) / char_size * np.log2(char_freq_dict.get(i) / char_size)
                        for i in char_freq_dict])
    return entropy

def generate_entropy_score(word_freq_dict, text, entropy_path):
    """
    Generate entropy score
    :param word_freq_dict: dict
    :param text: input text, document
    :param entropy_path: save entropy file path
    :return: dict
    """
    result = dict()
    for word in word_freq_dict:
        if len(word) == 1:
            continue
        # pass error pattern
        if '*' in word:
            continue
        try:
            left_right_char = re.findall('(.)%s(.)' % word, text)
            left_char = [i[0] for i in left_right_char]
            left_entropy = entropy_score(left_char)

            right_char = [i[1] for i in left_right_char]
            right_entropy = entropy_score(right_char)
            if left_entropy > 0 or right_entropy > 0:
                result[word] = [left_entropy, right_entropy]
        except Exception as e:
            logger.error('error word %s, %s' % (word, e))
    save_json(result, entropy_path)
    return result

class PMI(object):
    def __init__(self,
                 text='',
                 ngram=4,
                 pmi_path=config.pmi_path,
                 entropy_path=config.entropy_path
                 ):
        if os.path.exists(pmi_path) and os.path.exists(entropy_path):
            self.pmi_score_dict = load_json(pmi_path)
            self.entropy_score_dict = load_json(entropy_path)
        elif len(text.strip()) > 0:
            words_freq = ngram_freq(text, ngram)
            self.pmi_score_dict = generate_pmi_score(words_freq, pmi_path)
            self.entropy_score_dict = generate_entropy_score(words_freq, text, entropy_path)
        else:
            raise ValueError("text and saved file path must exist one")

    def pmi_score(self, word):
        """
        Get PMI score
        :param word:
        :return:
        """
        return self.pmi_score_dict.get(word, 0.0)

    def entropy_score(self, word):
        """
        Get entropy score
        :param word:
        :return:
        """
        return self.entropy_score_dict.get(word, [0.0, 0.0])


from django.shortcuts import render
from django.http import HttpResponse
from .models import user_info
from .Recommender import Recommender
from sklearn.externals import joblib
import pickle
import csv
import sys
import reverse_geocoder as rg
import pprint
import geocoder
from math import cos, asin, sqrt
from collections import defaultdict
from geopy.geocoders import Nominatim
import googlemaps
def getPlaceName(coordinate):
  result = rg.search(coordinate)
  # result is a list containing ordered dictionary.
  pprint.pprint(result)
  return result

def getDistance(dest_coor):
  print(dest_coor)
  lat1,lon1=23.7593572,90.3788136
  lat2,lon2=dest_coor
  p = 0.017453292519943295     #Pi/180
  a = 0.5 - cos((lat2 - lat1) * p)/2 + cos(lat1 * p) * cos(lat2 * p) * (1 - cos((lon2 - lon1) * p)) / 2
  return 12742 * asin(sqrt(a))

def getTravelTime(distance,travel_speed):

    return distance/travel_speed

def getName(coordinate):
  geolocator = Nominatim(user_agent="wheretogo")
  location = geolocator.reverse(coordinate)
  return location.address
def predict(id):
  with open('assets/mysite/images/recommender_model.pkl','rb') as file:
      sys.modules['Recommender']=Recommender
      model=joblib.load(file)
      place_id=model.predict(id)
      print(place_id)
      location=model.getLocationInfo()
      all_place_info=list()
      index=0
      for pid in place_id:
        print("Myid: ", pid)
        #name=getName(location[index])
        name=getPlaceName(location[pid])
        #x=name.split(',')
        single_place_info=defaultdict(list)
        distance=getDistance(location[pid])
        travel_time=getTravelTime(distance,40)
        lat,lng=location[pid]
        single_place_info['lat']=lat
        single_place_info['lng']=lng
        single_place_info['place_id']=pid
        single_place_info['place_name']=name[0]['name']
        single_place_info['country']=name[0]['cc']
        #single_place_info['place_name']=str(x[0])+","+str(x[1])+","+str(x[2])
        #single_place_info['country']=x[len(x)-1]
        single_place_info['distance']=distance
        single_place_info['travel_time']=travel_time
        all_place_info.append(single_place_info)
        index+=1
        #print("Distance:",getDistance(location[0]),"KM","Travel Time:",travel_time,"hours")
        #print(all_place_info)
      user={"all_place_info":all_place_info,
            "coors":location}
      return user

def home_view(request,*arg,**kwargs):

    user=predict(80)
    return render(request,'home.html',user)

def recommendation(request,*arg,**kwargs):
    id=int(request.POST['id'])
    user=predict(id)
    return render(request,'home.html',user)

def sign_in(request,*arg,**kwargs):

  return render(request,'sign_in.html')
def sign_up(request,*arg,**kwargs):

  return render(request,'sign_up.html')


""" Performs web page automation either in a visible browser, or a headless one (invisible) """

import logging
from time import time, sleep
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.common import exceptions

class WebAutomation:  # pylint: disable=too-many-public-methods
    """ Returns browser object with which to interface """
    def __init__(self, browser_name="firefox", headless=False, executable=None):
        """ Setup requirements
            Args:
                browser_name (str): "firefox" or "chrome"
                headless (bool): True = Do not show browser; False = Show browser during automation
                executable (str): Optional, if set, should be a direct path to the browser executable
        """

        self.selenium_driver = None
        self.browser_name = browser_name.lower()
        self.headless = headless
        self.webdriver_wait = 20  # How long to wait for elements to appear and events to occur in seconds
        self.executable = executable
        self.launch_browser()

    def launch_browser(self):
        """ Launch browser and create instance. Sets self.selenium_driver """

        try:
            # Chrome
            if self.browser_name == "chrome":
                self.start_chrome()

            # Firefox
            elif self.browser_name == "firefox":
                self.start_firefox()
        except:
            logging.error("Error starting web browser")
            raise

    def start_chrome(self):
        """ Configure and start Chrome """

        # Setup browser options
        options = webdriver.chrome.options.Options()  # Create options object
        options.add_argument(
            "--disable-extensions"
        )  # Don't want user's extensions interfering
        options.add_argument("--no-sandbox")  # Set sandbox argument

        # Enable headless operation if desired
        if self.headless:
            options.add_argument(
                "--headless"
            )

        # If user specifies an exact location, use it instead of the $PATH variable
        if self.executable:
            options.binary_location = (
                self.executable
            )

        # Set Capabilities
        cap = webdriver.common.desired_capabilities.DesiredCapabilities.CHROME
        cap["loggingPrefs"] = {"browser": "ALL"}

        # Create instance
        self.selenium_driver = webdriver.Chrome(options=options, desired_capabilities=cap)  # Create instance
        self.selenium_driver.implicitly_wait(self.webdriver_wait)  # Sets wait time
        self.selenium_driver.maximize_window()  # Maximize window

    def start_firefox(self):
        """ Configure and start Firefox """

        # Setup browser options
        options = webdriver.firefox.options.Options()  # Create options object
        options.add_argument(
            "--safe-mode"
        )  # Don't want user's extensions interfering

        # Enable headless operation if desired
        if self.headless:
            options.add_argument("--headless")

        # Create instance
        try:
            # If user specifies an exact location, use it instead of the $PATH variable
            self.selenium_driver = webdriver.Firefox(options=options, firefox_binary=self.executable)
        except exceptions.WebDriverException:
            logging.error(
                "Error creating Selenium driver. Your web browser or browser driver may be out of date. \
                Updating both should fix this.")
            raise
        self.selenium_driver.implicitly_wait(self.webdriver_wait)
        self.selenium_driver.maximize_window()

    def _find_element(self, element_id, element_type):
        """ When provided an identifier, returns the element object which can be used by Selenium functions

            Exits with an exception if not found within the self.webdriver_wait timeout.

            Args:
                element_id (str): Name/ID/XPATH/etc of element
                element_type (str): Valid element type (see path_types below)

            Returns:
                element object
        """

        # Get object for the provided identifier
        path_types = {
            "id": self.selenium_driver.find_element_by_id,
            "xpath": self.selenium_driver.find_element_by_xpath,
            "link text": self.selenium_driver.find_element_by_link_text,
            "partial link text": self.selenium_driver.find_element_by_partial_link_text,
            "name": self.selenium_driver.find_element_by_name,
            "class": self.selenium_driver.find_element_by_class_name,
            "css selector": self.selenium_driver.find_element_by_css_selector,
            "tag": self.selenium_driver.find_element_by_tag_name
        }

        assert element_type in path_types, "Invalid element type provided"  # Notify about user error
        element = path_types[element_type](element_id)  # Get element object
        assert element is not None, "Element was not found. Likely does not exist on Web Page."
        return element

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

    def wait_for_element(self, element_id, element_type, timeout=None):
        """ Wait until element appears, or timeout reached

            Args:
                element_id (str): Name/ID/XPATH/etc of element
                element_type (str): If None, we will try to automatically determine the type of element
                timeout (int): If set, driver wait is changed temporarily to "timeout". Time is in seconds

            Returns:
                True if element exists, False if not after the timeout has been reached
        """

        # Change driver wait, if user requests it
        if timeout:
            self.selenium_driver.implicitly_wait(timeout)  # Sets wait time

        # Try to access the element, and wait until it's found or timeout occurs
        try:
            result = True
            self._find_element(element_id, element_type)  # Use implicitly_wait()
        except exceptions.NoSuchElementException:
            result = False
        finally:
            # Reset the wait time to default
            self.selenium_driver.implicitly_wait(self.webdriver_wait)  # Sets wait time

        return result  # Return True/False

    def wait_for_element_removal(self, element_id, element_type, timeout=20):
        """ Wait until element appears, or timeout reached

            Args:
                element_id (str): Name/ID/XPATH/etc of element
                element_type (str): If None, we will try to automatically determine the type of element
                timeout (int): If set, driver wait is changed temporarily to "timeout". Time is in seconds

            Returns:
                True if element does NOT exist
        """

        # Sets wait time to minimum, so we don't waste time after the element's removal
        self.selenium_driver.implicitly_wait(1)

        # Try to access the element, and wait until it's found or timeout occurs
        result = True
        etime = time() + timeout
        while result:
            result = self.wait_for_element(element_id, element_type, timeout=1)  # Check for elements existence
            if result:
                sleep(1)
                if time() > etime:  # Timeout reached
                    break

        # Reverse result, to make it more obvious that the test was successful
        result = [True, False][result]

        # Reset to default wait time
        self.selenium_driver.implicitly_wait(self.webdriver_wait)

        return result  # Return True/False

    def wait_for_expected_conditions(self, url=None, is_in_url=None):
        """ Wait for a condition to become available

            Available Conditions:
            https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/\
                selenium.webdriver.support.expected_conditions.html

            Args (must pick only one):
                url (str): Waits for URL to change
                is_in (str): Waits until URL contains the provided string
        """

        # Set time to wait, using default wait time
        wait = WebDriverWait(self.selenium_driver, self.webdriver_wait)

        # Wait depending on which type user selected
        if url:
            wait.until(EC.url_changes(url))
        elif is_in_url:
            wait.until(EC.url_contains(is_in_url))

    def open_url(self, url):
        """ Go to URL in browser, and wait for page to load per the self.webdriver_wait time """
        self.selenium_driver.get(url)

    def text_entry(self, text, element_id, element_type):
        """ Enter text into a text box

            Args:
                text (string): text to enter into the text box
                element_id (string): Name/ID/XPATH/etc of element
                element_type (string): When None, searches every element type, but is not recommended.
                                       Set it to the type you expect. Eg: "id"
        """

        element = self._find_element(element_id, element_type)  # Get element object
        element.click()  # Set focus
        try:
            element.clear()  # Remove any existing text
        except exceptions.InvalidElementStateException:  # Ignore this when we need to enter text in non-text fields
            pass
        element.send_keys(text)  # Enter text

    def click(self, element_id, element_type):
        """ Click on anything which has an identifiable name

            Args:
                element_id (string): Name/ID/XPATH/etc of element
                element_type (string): When None, searches every element type, but is not recommended.
                                       Set it to the type you expect. Eg: "id"
        """

        element = self._find_element(element_id, element_type)  # Get element object
        element.click()  # Click on object

    def get_text(self, element_id, element_type):
        """ Retrieves text from a tag or textbox and returns it """
        element = self._find_element(element_id, element_type)  # Get element object
        value = element.get_attribute("value")  # Try to read as a textbox first, produces None if not a textbox
        if value is None:
            return element.text  # Should be a tag, so return it's text
        return value

    def get_url(self):
        """ Return the current URL from the address bar

            Returns:
                URL (string)
        """
        return self.selenium_driver.current_url

    def accept_alert(self):
        """ Accept an alert """
        alert = self.selenium_driver.switch_to.alert
        alert.accept()

    def get_alert_text(self):
        """ Return text from an alert """
        alert = self.selenium_driver.switch_to.alert
        return alert.text

    def check_for_alert(self):
        """ Checks for the existence of an alert popup """
        result = True
        try:
            self.accept_alert()
        except exceptions.NoAlertPresentException:
            result = False
        return result

    def click_hold(self, seconds, element_id, element_type):
        """ Left click and hold specified number of seconds """
        element = self._find_element(element_id, element_type)  # Get element object
        action = ActionChains(self.selenium_driver).click_and_hold(element)
        action.perform()
        sleep(seconds)
        action = ActionChains(self.selenium_driver).release(element)
        action.perform()

    def right_click(self, element_id, element_type):
        """ Right (context) click on element """
        element = self._find_element(element_id, element_type)  # Get element object
        action = ActionChains(self.selenium_driver).context_click(element)
        action.perform()

    def double_click(self, element_id, element_type):
        """ Double left click on element """
        element = self._find_element(element_id, element_type)  # Get element object
        action = ActionChains(self.selenium_driver).double_click(element)
        action.perform()

    def mouse_hover(self, element_id, element_type):
        """ Hover mouse over element """
        element = self._find_element(element_id, element_type)  # Get element object
        action = ActionChains(self.selenium_driver).move_to_element(element)
        action.perform()

    def drag_drop(self, src_element_id, src_element_type, dst_element_id, dst_element_type):
        """ Hover mouse over element  """
        src_element = self._find_element(src_element_id, src_element_type)  # Get element object
        dst_element = self._find_element(dst_element_id, dst_element_type)  # Get element object
        action = ActionChains(self.selenium_driver).drag_and_drop(src_element, dst_element)
        action.perform()

    def keyboard_shortcut(self, character, control=False, alt=False, shift=False):
        """ Send any key press - UNTESTED """
        key_combo = ""
        if control:
            key_combo += Keys.CONTROL
        if alt:
            key_combo += Keys.ALT
        if shift:
            key_combo += Keys.SHIFT

        keymap = {
            "ALT": Keys.ALT,
            "BACKSPACE": Keys.BACKSPACE,
            "COMMAND": Keys.COMMAND,
            "DELETE": Keys.DELETE,
            "DOWN": Keys.DOWN,
            "END": Keys.END,
            "ENTER": Keys.ENTER,
            "ESCAPE": Keys.ESCAPE,
            "F1": Keys.F1,
            "F10": Keys.F10,
            "F11": Keys.F11,
            "F12": Keys.F12,
            "F2": Keys.F2,
            "F3": Keys.F3,
            "F4": Keys.F4,
            "F5": Keys.F5,
            "F6": Keys.F6,
            "F7": Keys.F7,
            "F8": Keys.F8,
            "F9": Keys.F9,
            "HOME": Keys.HOME,
            "INSERT": Keys.INSERT,
            "LEFT": Keys.LEFT,
            "META": Keys.META,
            "PAGE_DOWN": Keys.PAGE_DOWN,
            "PAGE_UP": Keys.PAGE_UP,
            "RETURN": Keys.RETURN,
            "RIGHT": Keys.RIGHT,
            "SPACE": Keys.SPACE,
            "TAB": Keys.TAB,
            "UP": Keys.UP
        }

        if character.upper() in keymap:
            key_combo += keymap[character.upper()]  # Any button as mapped above
        else:
            key_combo += character  # Any alphanumeric
        logging.warning(key_combo)

        self.selenium_driver.find_element_by_tag_name('body').send_keys(key_combo)

    def scroll_page(self, direction="down"):
        """ Scroll web page up, down, left, right
            Currently, only scrolls the main page
        """
        window_name = 'window'
        scroll_window = ""
        dimensions = self.selenium_driver.get_window_size()
        vertical = 0
        horizontal = 0

        if direction == "down":
            vertical = dimensions["height"]
        elif direction == "up":
            vertical = dimensions["height"] * -1
        elif direction == "right":
            horizontal = dimensions["width"]
        elif direction == "left":
            horizontal = dimensions["width"] * -1
        else:
            logging.error("Invalid direction (up/down/right/left)")
            return

        self.selenium_driver.execute_script(f"{window_name}.scrollBy({horizontal}, {vertical})", scroll_window)

    def scroll_to_element(self, element_id, element_type):
        """ Move element until in view """
        element = self._find_element(element_id, element_type)  # Get element object
        self.selenium_driver.execute_script("arguments[0].scrollIntoView();", element)

    def page_navigation(self, command):
        """ Various actions outside the web page """
        if command == 'back':
            self.selenium_driver.back()
        elif command == 'forward':
            self.selenium_driver.forward()
        elif command == 'refresh':
            self.selenium_driver.refresh()

    def close(self):
        """ Shut down the web browser driver. Failure to call this will result in zombie processes """
        self.selenium_driver.close()


from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'download/$',                   views.download_csv, name='user_download'),
    url(r'^edit/(?P<user_id>[0-9]+)/$',   views.update,  name='user_edit'),
    url(r'^delete/(?P<user_id>[0-9]+)/$', views.delete,  name='user_delete'),
    url(r'^add/$',                        views.create,  name='user_add'),
    url(r'^(?P<user_id>[0-9]+)/$',        views.read,    name='user_show'),
    url(r'^$',                            views.list_all,name='user_list'),
]


# -*- coding: utf-8 -*-
"""
Created on Mon Jun 14 17:55:39 2021

@author: AAKASH
"""
from flask import Flask, render_template, request
import jsonify
import requests
import pickle
import numpy as np
import sklearn
from sklearn.preprocessing import StandardScaler
app = Flask(__name__)
model = pickle.load(open('linear_regression_model.pkl', 'rb'))
@app.route('/',methods=['GET'])
def Home():
    return render_template('index.html')

standard_to = StandardScaler()
@app.route("/predict", methods=['POST'])
def predict():
    #Fuel_Type_Diesel=0
    if request.method == 'POST':

        km_driven=int(request.form['km_driven'])

        ex_showroom_price=float(request.form['ex_showroom_price'])

        Year = int(request.form['year'])

        name=request.form['name']
        if(name=='Hero'):
            name_Hero=1
            name_Honda=0
            name_Royal = 0
            name_Suzuki = 0
            name_TVS = 0
            name_Yamaha = 0
        elif(name=='Honda'):
            name_Hero=0
            name_Honda=1
            name_Royal = 0
            name_Suzuki = 0
            name_TVS = 0
            name_Yamaha = 0
        elif(name=='Yamaha'):
            name_Hero=0
            name_Honda=0
            name_Royal = 0
            name_Suzuki = 0
            name_TVS = 0
            name_Yamaha = 1
        elif(name=='Royal'):
            name_Hero=0
            name_Honda=0
            name_Royal = 1
            name_Suzuki = 0
            name_TVS = 0
            name_Yamaha = 0
        elif(name=='TVS'):
            name_Hero=0
            name_Honda=0
            name_Royal = 0
            name_Suzuki = 0
            name_TVS = 1
            name_Yamaha = 0
        elif(name=='Suzuki'):
            name_Hero=0
            name_Honda=0
            name_Royal = 0
            name_Suzuki = 1
            name_TVS = 0
            name_Yamaha = 0
        else:
            name_Hero=0
            name_Honda=0
            name_Royal = 0
            name_Suzuki = 0
            name_TVS = 0
            name_Yamaha = 0

        Seller_Type=request.form['seller_type_Individual']
        if(Seller_Type=='Individual'):
            Seller_Type_Individual=1
        else:
             Seller_Type_Individual=0

                 # Present_Price=float(request.form['Present_Price'])

       # Kms_Driven2=np.log(Kms_Driven)
        Owner=request.form['owner']
        if(Owner=='First Owner'):
            owner_second=0
            owner_third=0
        elif(Owner=='Second Owner'):
            owner_second=1
            owner_third=0
        elif(Owner=='Third Owner'):
            owner_second=0
            owner_third=1

        Year=2021-Year

        prediction=model.predict([[km_driven,ex_showroom_price,Year,name_Hero,name_Honda,name_Royal,name_Suzuki,name_TVS,name_Yamaha,Seller_Type_Individual,owner_second,owner_third]])
        output=round(prediction[0],2)
        if output<0:
            return render_template('index.html',prediction_text="Sorry you cannot sell this bike")
        else:
            return render_template('index.html',prediction_text="You Can Sell The bike at {}".format(output))
    else:
        return render_template('index.html')

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


import sys
from test import PASH_TOP
sys.path.append(PASH_TOP)
from scripts.test_eval.logparser import LogParser, DEFAULT_LOG_FOLDER
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sys import argv
import os

def plot_barchart(df, y_axis="exec_time", test_names=None, save_file="output.png"):
    if test_names is not None:
        df = df[df["test_name"].isin(test_names)]
        labels = sorted(test_names)
    else:
        labels = sorted(df.test_name.unique())

    x = np.arange(len(labels))  # the label locations
    width = 0.2  # the width of the bars

    r_split = df[(df.split_type == "r-split") & (df.no_eager == False)]
    r_split_no_eager = df[(df.split_type == "r-split") & (df.no_eager == True)]
    auto_split = df[(df.split_type == "auto-split") & (df.no_eager == False)]
    auto_split_no_eager = df[(df.split_type == "auto-split") & (df.no_eager == True)]

    r = []
    rn = []
    a = []
    an = []
    error_r = []
    error_rn = []
    error_a = []
    error_an = []

    for label in labels:
        r.append(r_split.loc[r_split["test_name"] == label][y_axis].min())
        rn.append(r_split_no_eager.loc[r_split_no_eager["test_name"] == label][y_axis].min())
        a.append(auto_split.loc[auto_split["test_name"] == label][y_axis].min())
        an.append(auto_split_no_eager.loc[auto_split_no_eager["test_name"] == label][y_axis].min())

        error_r.append(r_split.loc[r_split["test_name"] == label][y_axis].max() - r[-1])
        error_rn.append(r_split_no_eager.loc[r_split_no_eager["test_name"] == label][y_axis].max() - rn[-1])
        error_a.append(auto_split.loc[auto_split["test_name"] == label][y_axis].max() - a[-1])
        error_an.append(auto_split_no_eager.loc[auto_split_no_eager["test_name"] == label][y_axis].max() - an[-1])

    fig, ax = plt.subplots(figsize=(20,10))
    rects1 = ax.bar(x - 3/2*width, r, width, yerr=error_r, label='r-split')
    rects2 = ax.bar(x - 1/2*width, rn, width, yerr=error_rn, label='r-split-no-eager')
    rects3 = ax.bar(x + 1/2*width, a, width, yerr=error_a, label='auto-split')
    rects4 = ax.bar(x + 3/2*width, an, width, yerr=error_an, label='auto-split-no-eager')

    # Add some text for labels, title and custom x-axis tick labels, etc.
    ax.set_ylabel('ms')
    # ax.set_title('')
    ax.set_xticks(x)
    ax.set_xticklabels(labels)
    plt.xticks(rotation=90)
    ax.legend()

    fig.savefig(save_file)

#This function only compares between folders
#dfs is tuple: (df_name, pd.df)
def plot_barchart_against(dfs: list, y_axis="exec_time", test_names=None, save_file="output.png"):
    print(test_names)
    df1 = dfs[0][1]
    if test_names is not None:
        df1 = df1[df1["test_name"].isin(test_names)]
        labels = sorted(test_names)
    else:
        labels = sorted(df1.test_name.unique())

    x = np.arange(len(labels))  # the label locations
    width = 1/(len(dfs))*4/5  # the width of the bars

    rects = []
    for df_name, df in dfs:
        df = df[(df.split_type == "r-split") & (df.no_eager == False) & (df.dgsh_tee == True)]
        rects.append(df)

    bar_height = [[] for i in range(len(rects))]
    error_height = [[] for i in range(len(rects))]

    for label in labels:
        for i, rect in enumerate(rects):
            try:
                bar_height[i].append(rect.loc[rect["test_name"] == label][y_axis].mean())
                error_height[i].append(rect.loc[rect["test_name"] == label][y_axis].std())
            except:
                print("plot_shart_against: failed: ", label)

    fig, ax = plt.subplots(figsize=(20,10))
    for i in range(len(dfs)):
        label = ""
        df_name = dfs[i][0]
        offset = (i -  int(len(dfs)/2))
        if len(dfs)%2 == 0:
            offset += 1/2
        print(offset)
        ax.bar(x + offset*width, bar_height[i], width, yerr=error_height[i], label=label+df_name)

    # Add some text for labels, title and custom x-axis tick labels, etc.
    ax.set_ylabel('ms')
    # ax.set_title('')
    ax.set_xticks(x)
    ax.set_xticklabels(labels)
    plt.xticks(rotation=90)
    ax.legend()

    fig.savefig(save_file)

if __name__ == '__main__':
    #sample execution

    log_dfs = []
    if len(argv) > 1:
        for folder in argv[1:]:
            log_parser = LogParser()
            df = log_parser.parse_folder(folder)
            log_dfs.append((folder, df))
    # parse more directories if needed
    # df = log_parser.parse_folder("tmp2_1G_width4bbackup")

    plot_barchart_against(log_dfs, save_file="compare" + ".png")


import numpy as np
from flask import Flask, make_response,Response,request,jsonify,render_template,session
import cv2
import base64
import sqlite3
from keras.models import load_model

#先预测一次防止keras报错
global img
model = load_model('static/mnist.h5')
img = cv2.imread('static/img/2.png', 0)
img = cv2.resize(img, (28, 28))
img = (1 - np.array(img) / 255.0).reshape([-1, 28, 28, 1])
model.predict(img)

app = Flask(__name__)
app.config['SECRET_KEY'] = '\xf1\x92Y\xdf\x8ejY\x04\x96\xb4V\x88\xfb\xfc\xb5\x18F\xa3\xee\xb9\xb9t\x01\xf0\x96'
conn = sqlite3.connect('py.db', check_same_thread=False)
cur = conn.cursor()

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

@app.route("/register", methods=['POST'])
def register():
    data=request.get_json('data')
    name = data["name"]
    id1 = data["id"]
    password1 = data["password1"]
    password2 = data["password2"]
    major = data["major1"]
    line2="{\"result\":\""
    if name=="" or id1=="" or password1=="" or password2==""or major=="":
        line2=line2+"注册信息不完整，请重新输入"
        return line2+"\"}"
    if password1!= password2:
        line2=line2+"两次密码不一致，请重新输入"
        return line2+"\"}"
    if len(password1)<6:
        line2=line2+"密码长度至少6位"
        return line2+"\"}"
    sql = "SELECT id FROM student where id= "+id1
    cur.execute(sql)
    if cur.fetchone() is not None:
        return line2+"学号已经注册，请登陆\"}"
    sql = "insert into student (id,name,password,major) values ('%s','%s','%s','%s')"%(id1,name,password1,major)
    # sql = "UPDATE EMPLOYEE SET AGE = AGE + 1 WHERE SEX = '%c'" % ('M')
    # sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (20)
    cur.execute(sql)
    conn.commit()
    return line2+"注册成功！\"}"

@app.route('/login', methods=['POST'])
def login():
    data=request.get_json('data')
    sql = "SELECT * FROM student where id= "+data['user']

    cur.execute(sql)
    result = cur.fetchone() # cursor.fetchall()
    if result[2] == data['pass']:
        session['username']=result[1]
        session['id']=result[0]
        session['major']=result[3]
       # session['time']='2019.01.01'
        return jsonify({'name': 'xmr', 'age': 's'})
    else:
        return 'error'

@app.route("/logout")
def logout():
    session.pop('username',None)
    session.pop('password',None)
    return render_template('index.html')

@app.route('/home')
def home():
    if session.get('username') is not None:
        context={
            'name':session.get('username'),
            'id': session.get('id'),
            'major': session.get('major'),
            'time': session.get('time'),
            'renwu': "s",
        }
        return render_template('home.html', **context)
    else:
        return render_template('index.html')

@app.route("/listpeople")
def listpeople():
    if session.get('username') is not None:
        return render_template('listpeople.html',name=session.get('username'))
    else:
        return render_template('index.html')

@app.route("/study_basics")
def study_basics():
    if session.get('username') is not None:
        return render_template('study_basics.html',name=session.get('username'))
    else:
        return render_template('index.html')
@app.route("/study_imgprc")
def study_imgprc():
    if session.get('username') is not None:
        return render_template('study_imgprc.html',name=session.get('username'))
    else:
        return render_template('index.html')
@app.route("/study_objdec")
def study_objdec():
    if session.get('username') is not None:
        return render_template('study_objdec.html',name=session.get('username'))
    else:
        return render_template('index.html')
@app.route("/study_3d")
def study_3d():
    if session.get('username') is not None:
        return render_template('study_3d.html',name=session.get('username'))
    else:
        return render_template('index.html')
@app.route("/imgProc")
def imgProc():
    if session.get('username') is not None:
        return render_template('imgProc.html',name=session.get('username'))
    else:
        return render_template('index.html')
@app.route("/objdec")
def objdec():
    if session.get('username') is not None:
        return render_template('objdec.html',name=session.get('username'))
    else:
        return render_template('index.html')

@app.route("/calibration")
def calibration():
    if session.get('username') is not None:
        return render_template('calibration1.html',name=session.get('username'))
    else:
        return render_template('index.html')
@app.route("/measure")
def measure():
    if session.get('username') is not None:
        return render_template('measure.html',name=session.get('username'))
    else:
        return render_template('index.html')
@app.route("/down")
def down():
    if session.get('username') is not None:
        return render_template('down.html',name=session.get('username'))
    else:
        return render_template('index.html')
@app.route("/mnist1")
def mnist1():
    if session.get('username') is not None:
        return render_template('mnist.html',name=session.get('username'))
    else:
        return render_template('index.html')

@app.route('/paper')
def paper():
    context=(
        ('name1','201622362013271','name','07','name','name','5','1'),
        ('name2','201622362013271','name','07','name','name','5','2'),
    )
    return render_template('paper.html', u=context)

@app.route('/api/mnist', methods=['POST'])
def mnist():
    X = (1- np.array(request.json, dtype=np.uint8)/ 255.0).reshape([-1, 28, 28, 1])
    output=model.predict(X)[0].tolist()
    return jsonify(results=[output])

@app.route("/upload",methods=['POST'])
def uploa2d():
    global img
    arr = np.asarray(bytearray(request.files['file'].read()), dtype=np.uint8)
    img = cv2.imdecode(arr, -1)
    img=cv2.resize(img,(100,100))
    ret, jpeg = cv2.imencode('.jpg', img)
    return base64.b64encode(jpeg.tobytes())

@app.route("/gray")
def gray():
    global img
    img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    ret, jpeg = cv2.imencode('.jpg', img)
    return base64.b64encode(jpeg.tobytes())

@app.route("/canny")
def canny():
    global img
    img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    img=cv2.Canny(img, 100, 255, 3)
    ret, jpeg = cv2.imencode('.jpg', img)
    return base64.b64encode(jpeg.tobytes())

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


from threading import Thread, Lock
from PyQt5.QtCore import QObject, pyqtSignal
from time import sleep
from events import RefreshEvent, ExplodeEvent, MoveEvent
from parameters import Parameter
from collections import namedtuple
from random import random, choice

class Bomb(Thread, QObject):

    refresh_bomb = pyqtSignal(RefreshEvent)
    set_explosion = pyqtSignal(ExplodeEvent)

    def __init__(self, parent, bomber, _id, _map, x_block, y_block, enemies, *args, **kwargs):
        Thread.__init__(self, *args, **kwargs)
        QObject.__init__(self)

        self.refresh_bomb.connect(parent.refresh_bomb)
        self.set_explosion.connect(parent.set_explosion)

        self.id = _id
        self.map = _map
        self.x_block = x_block
        self.y_block = y_block
        self.parent = parent
        self.bomber = bomber
        self.enemies = enemies
        self._pause = False
        self.explosions = []
        self.alive = True

    def run(self):
        self.map.edit.acquire()
        self.map.blocks[self.y_block][self.x_block] = 'B'
        self.map.edit.release()

        count = 0
        while count < 10:
            while self._pause:
                sleep(0.25)
            else:
                sleep(0.25)
                self.refresh_bomb.emit(RefreshEvent(self.id, 'refresh'))
                count += 1
        self.refresh_bomb.emit(RefreshEvent(self.id, 'kill'))
        self.map.remove(self.x_block, self.y_block)
        self.explode()

    def explode(self):
        self.bomber.current_bombs -= 1

        self.set_explosion.emit(ExplodeEvent(self.x_block, self.y_block, 'center', 'center', Explosion.id))
        _id = Explosion.id

        if self.bomber.x // 30 == self.x_block and self.bomber.y // 30 == self.y_block:
            self.bomber.life -= 1 # Agregar parte de abajo del bomber

        Explosion.id_lock.acquire()
        Explosion.id += 1
        Explosion.id_lock.release()

        if self.map.blocks[self.y_block - 1][self.x_block] != 'X':
            explosion = Explosion('north', self.parent, self.bomber, self.map, self.x_block, self.y_block - 1, self.enemies)
            self.explosions.append(explosion)
            explosion.start()
        if self.map.blocks[self.y_block + 1][self.x_block] != 'X':
            explosion = Explosion('south', self.parent, self.bomber, self.map, self.x_block, self.y_block + 1, self.enemies)
            self.explosions.append(explosion)
            explosion.start()
        if self.map.blocks[self.y_block][self.x_block + 1] != 'X':
            explosion = Explosion('west', self.parent, self.bomber, self.map, self.x_block + 1, self.y_block, self.enemies)
            self.explosions.append(explosion)
            explosion.start()
        if self.map.blocks[self.y_block][self.x_block - 1] != 'X':
            explosion = Explosion('east', self.parent, self.bomber, self.map, self.x_block - 1, self.y_block, self.enemies)
            self.explosions.append(explosion)
            explosion.start()

         # Se eliminan las explosiones que teminaron
        while self.explosions:
            self.explosions = [explosion for explosion in self.explosions if explosion.alive]
            sleep(0.1)
        self.set_explosion.emit(ExplodeEvent(_type='kill', _id=_id))
        self.alive = False

    def pause(self, state):
        self._pause = state
        for explosion in self.explosions:
            explosion.paused = state

class Explosion(Thread, QObject):

    set_explosion = pyqtSignal(ExplodeEvent)
    kill_enemy = pyqtSignal(MoveEvent)
    id = 0
    id_lock = Lock()
    set_lock = Lock()
    block = namedtuple('ExplosionBlock', 'id x y')

    def __init__(self, direction, parent, bomber, _map, x, y, enemies, *args, **kwargs):
        Thread.__init__(self, *args, **kwargs)
        QObject.__init__(self)
        self.direction = direction
        self.x = x
        self.y = y
        self.map = _map
        self.bomber = bomber
        self.explosions = []
        self.enemies = enemies
        self.did_damage = False
        self.parent = parent
        self.alive = True
        self.paused = False

        self.set_explosion.connect(parent.set_explosion)
        self.kill_enemy.connect(enemies.kill)

    def run(self):
        counter = 0
        while counter < Parameter.explosion_radius:
            while self.paused:
                sleep(0.1)
            else:
                if counter > 0:
                    for explosion in self.explosions:
                        self.set_lock.acquire()
                        self.set_explosion.emit(ExplodeEvent(_type='root', direction=self.direction, _id=explosion.id))
                        self.set_lock.release()
                # Explosion hacia arriba.
                if self.direction == 'north':
                    block = self.map.blocks[self.y - counter][self.x]
                    x = self.x
                    y = self.y - counter
                if self.direction == 'south':
                    block = self.map.blocks[self.y + counter][self.x]
                    x = self.x
                    y = self.y + counter
                if self.direction == 'east':
                    block = self.map.blocks[self.y][self.x - counter]
                    x = self.x - counter
                    y = self.y
                if self.direction == 'west':
                    block = self.map.blocks[self.y][self.x + counter]
                    x = self.x + counter
                    y = self.y
                if block == '0':
                    self.set_lock.acquire()
                    self.set_explosion.emit(ExplodeEvent(x, y, 'arm', self.direction, self.id))
                    self.set_lock.release()
                    self.explosions.append(self.block(self.id, x, y))
                    self.id_lock.acquire()
                    Explosion.id += 1
                    self.id_lock.release()
                if block == 'J':
                    self.bomber.life -= 1
                    break
                if block == 'G':
                    self.bomber.score += Parameter.ENEMY_SCORE
                    self.kill_enemy.emit(MoveEvent(x=x, y=y))
                    break
                if block == 'H':
                    self.bomber.score += Parameter.ENEMY_SCORE
                    self.kill_enemy.emit(MoveEvent(x=x, y=y))
                    break
                if block == 'P':
                    self.map.remove(x, y)
                    self.bomber.score += Parameter.WALL_SCORE
                    self.set_explosion.emit(ExplodeEvent(x, y, 'delete', self.direction, self.id))
                    wall = DestroyedWall(self.parent, self.map, x, y)
                    wall.start()
                    break
                sleep(0.1)
                counter += 1
        for explosion in self.explosions:
            self.set_lock.acquire()
            self.set_explosion.emit(ExplodeEvent(_type='kill', _id=explosion.id))
            self.set_lock.release()
        self.alive = False

class DestroyedWall(Thread, QObject):

    set_power = pyqtSignal(ExplodeEvent)
    set_wall = pyqtSignal(ExplodeEvent)

    def __init__(self, parent, _map, x, y, *args, **kwargs):
        """
        :param parent: QWidget
        :param _map: list[list]
        :param x: int (block)
        :param y: int (block)
        :param args:
        :param kwargs:
        """
        Thread.__init__(self, *args, **kwargs)
        QObject.__init__(self)
        self.x = x
        self.y = y
        self.map = _map
        self.id = '{},{}'.format(str(self.x),str(self.y))
        self.set_power.connect(parent.set_power)
        self.set_wall.connect(parent.set_wall)

    def run(self):
        self.set_wall.emit(ExplodeEvent(x=(self.x * 30), y=(self.y * 30), _type='create', direction='1', _id=self.id))
        sleep(0.05)
        for i in range(2, 6):
            self.set_wall.emit(ExplodeEvent(_type='refresh', direction=str(i), _id=self.id))
            sleep(0.05)
        self.set_wall.emit(ExplodeEvent(_type='kill', _id=self.id))
        if random() <= 0.3:
            power = choice(list(Parameter.POWERS.keys()))
            self.map.add('W{}'.format(Parameter.POWERS[power]), self.x, self.y)
            self.set_power.emit(ExplodeEvent(x=(self.x * 30), y=(self.y * 30), _type='create', direction=power,
                                             _id=self.id))


'''
Created on Mar 16, 2017

@author: dan
'''

from keras.layers.merge import average
from keras.layers.core import Lambda
from keras.models import Model

import tensorflow as tf

def get_slice(data, idx, parts):
       # import tensorflow as tf
        shape = tf.shape(data)
        size = tf.concat(0, [ shape[:1] // parts, shape[1:] ])
        stride = tf.concat(0, [ shape[:1] // parts, shape[1:]*0 ])
        start = stride * idx
        return tf.slice(data, start, size)
def _merge_function(inputs):
        output = inputs[0]
        for i in range(1, len(inputs)):
            output += inputs[i]
        return output / len(inputs)
def make_parallel(model, gpu_count):
#    def get_slice(data, idx, parts):
#        shape = tf.shape(data)
#        size = tf.concat(0, [ shape[:1] // parts, shape[1:] ])
#        stride = tf.concat(0, [ shape[:1] // parts, shape[1:]*0 ])
#        start = stride * idx
#        return tf.slice(data, start, size)
    print(model.outputs,model.inputs)
    outputs_all = []

    #Place a copy of the model on each GPU, each getting a slice of the batch
    for i in range(gpu_count):
        with tf.device('/gpu:%d' % i):
            with tf.name_scope('tower_%d' % i) as scope:

                inputs = []
                #Slice each input into a piece for processing on this GPU
                for x in model.inputs:
                    input_shape = tuple(x.get_shape().as_list())[1:]
                    slice_n = Lambda(get_slice, output_shape=input_shape, arguments={'idx':i,'parts':gpu_count})(x)
                    inputs.append(slice_n)
                outputs = model(inputs)
#                 if not isinstance(outputs, list):
#                     outputs = [outputs]

                #Save all the outputs for merging back together later
                print(i,outputs)

                outputs_all.append(outputs)

    # merge outputs on CPU
    with tf.device('/cpu:0'):
#         merged = []
#         for outputs in outputs_all:
#             merged.append(merge(outputs, mode='concat', concat_axis=0))
        print(len(outputs_all))
#         for i,out in enumerate(outputs_all):
#             print(i,":",average(out))
        merged=average(outputs_all)
        print(merged)

        return Model(input=model.inputs, output=merged)


#!/usr/bin/python

import spidev
import os
import time

# Define Axis Channels (channel 3 to 7 can be assigned for more buttons / joysticks)
swt_channel = 0
vrx_channel = 1
vry_channel = 2

#Time delay, which tells how many seconds the value is read out
delay = 0.5

# Spi oeffnen
spi = spidev.SpiDev()
spi.open(0,0)

# Function for reading the MCP3008 channel between 0 and 7
def readChannel(channel):
  val = spi.xfer2([1,(8+channel)<<4,0])
  data = ((val[1]&3) << 8) + val[2]
  return data

# endless loop
while True:

  # Determine position
  vrx_pos = readChannel(vrx_channel)
  vry_pos = readChannel(vry_channel)

  # SW determine
  swt_val = readChannel(swt_channel)

  # output
  print("VRx : {}  VRy : {}  SW : {}".format(vrx_pos,vry_pos,swt_val))

  # wait
  time.sleep(delay)


import requests
import simplejson
import unidecode
import operator
import json
import sys
from IPython.core.display import display, HTML
import pandas as pd
from collections import OrderedDict
from datetime import date
import random
from bs4 import *
import http.client, sys
#from Queue import Queue
from multiprocessing import Queue
# Example 3: asynchronous requests with larger thread pool
import asyncio
import concurrent.futures

async def fix_sold(range_start,range_end):

    with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:

        loop = asyncio.get_event_loop()
        futures = [
            loop.run_in_executor
            (
                executor,
                requests.get,
                df['permalink'].iloc[i],
                print ("sent ",i," ",df['permalink'].iloc[i])
            )
            for i in range(range_start,range_end)
        ]
        for response in await asyncio.gather(*futures):
            sold = retrieve_sold_from_text(response.text)
            print ("returned sold ",sold," from ",response.url)
            for index, row in df[range_start:range_end].iterrows():
                if str(df.loc[index,'permalink']) in response.url:
                    print (df.loc[index,'title'])
                    df.loc[index,'sold_new'] = sold

def retrieve_sold_from_link(link):
    r = requests.get(link)
    soup = BeautifulSoup(r.text)
    text_soup = soup.prettify()
    split_text = text_soup.split('\n')
    for num in range(0,len(split_text)):
        if ( '\tvendidos' in split_text[num]):
            return int(split_text[num - 1])
def retrieve_sold_from_text(text):
    soup = BeautifulSoup(text)
    text_soup = soup.prettify()
    split_text = text_soup.split('\n')
    for num in range(0,len(split_text)):
        if ( '\tvendidos' in split_text[num]):
            return int(split_text[num - 1])

def path_to_image_html(path):
    '''
     This function essentially convert the image url to
     '' format. And one can put any
     formatting adjustments to control the height, aspect ratio, size etc.
     within as in the below example.
    '''

    return ''

def path_to_link_html(path):
    '''
     This function essentially convert the image url to
     '' format. And one can put any
     formatting adjustments to control the height, aspect ratio, size etc.
     within as in the below example.
    '''

    return 'link'

def check_ALL_list_in_title(list,string ):
# ALL NEEDS TO MATCH
    result = 1
    for item in list:
        if item.lower() in string.lower():
            result = 1
        else:
            result = 0
    return result

def check_ANY_list_in_title(list,string ):
# ANY NEEDS TO MATCH
    result = 0
    for item in list:
        #print ("item:",item,"str:",string)
        if item.lower() in string.lower() :
            result = 1
    return result

def search_mla_return_list(product,min_price,max_price,category):
    #print ("Returning complete MLA list for product",product)
    link = 'https://api.mercadolibre.com/sites/MLA/search?q=\'' + product + '\'' + '&category=' + category
    price_filter = "&price=" + str(min_price) + "-" + str(max_price)
    link2 = link + '&offset=' + str(0) + '&limit=' + str(50) + price_filter
    r = requests.get(link2)
    #print (link2)
    c = r.json()
    #print (c['results'][0])
    c_orig = c
    c_partial = c['results']
    #print (c_partial)
    start =  c['paging']['offset']
    end =  c['paging']['limit']
    total =  c['paging']['total']
    total_saved = total
    total_c = c['results']
    maximum_sell = 0
    #print ("Total found:",total," (limited to 1k no token) start:",start," end ",end," , range: ",min_price,"-",max_price)
    if total > 1000:
        total = 1000
    # TEMOPORARY
    num_new = 0
    num=0
    num_new = 0
    while ( total > 51 and end > 10 ): # and 0):  ## <--- full list instead of first 50
        start = start + end
        #print (start,"..",end='')
        if ( (c['paging']['total'] - start) < end  ):
            end = c['paging']['total'] - 1 - start
        link2 = link + '&offset=' + str(start) + '&limit=' + str(end) + price_filter
        #print ("requesting LINK:",link2)
        r = requests.get(link2)
        c = r.json()
        c_partial = 0
        #print ("Total:",c['paging']['total']," start:",start," limit ",end)
        total_c = total_c + c['results']
        total = total - end
        num = num + 1
    return total_c

def return_max_sold_from_list(list,product,min_price,max_price):
    #print ("----- Searching max sold...");
    product_list = product.split()
    maximum_sell = 0
    num_new = 0
    for x in list[:]:
        #print(c['site_id'], c['results']['title'],c['results']['sold_quantity'], "$",c['results']['price'],c['site_id'], c['results']['address']['city_name'])
        sold_qty = list[num_new]['sold_quantity']
        price = list[num_new]['price']
        #print ("sold qty",sold_qty,"num:",num)
        title = unidecode.unidecode(list[num_new]['title']).lower()
        if ( sold_qty > maximum_sell and check_ALL_list_in_title(product_list,title ) and not check_ALL_list_in_title(product_to_avoid.split(),title ) and ( int(price) >= int(min_price) and int(price) <= int(max_price) )):
            maximum_sell = sold_qty
            #print ("new max sell found",maximum_sell)
        num_new = num_new + 1
    #print ("....max units sold at this range: ",maximum_sell)
    return maximum_sell

def return_tuples_from_list(list,product,min_price,max_price):
    print ("----- Searching maximum units sold...")
    product_list = product.split()
    maximum_sell = 0
    num_new = 0
    for x in list[:]:
        #print(c['site_id'], c['results']['title'],c['results']['sold_quantity'], "$",c['results']['price'],c['site_id'], c['results']['address']['city_name'])
        sold_qty = list[num_new]['sold_quantity']
        price = list[num_new]['price']
        title = list[num_new]['title']
        title_list = title.split()
        title_short = title_list[0:3]
        #print ("Title short:",title_short)
        #print ("sold qty",sold_qty,"num:",num)
        title = unidecode.unidecode(list[num_new]['title']).lower()
        if ( sold_qty > maximum_sell and check_ALL_list_in_title(product_list,title ) and not check_ALL_list_in_title(product_to_avoid.split(),title ) and ( int(price) >= int(min_price) and int(price) <= int(max_price) )):
            maximum_sell = sold_qty
            #print ("Title short:",title_short)
            #print ("new max sell found",maximum_sell)
        num_new = num_new + 1
    return maximum_sell

def return_products_from_list_pandas(total_c,product,condition,min_price,max_price):
    from operator import itemgetter
    import pandas as pd
    num = 0
    list_new = list()
    product_list = product.split()
    df = pd.DataFrame.from_dict(total_c)
    if (len(df)>0):
        df_temp = pd.DataFrame()
        df_temp = df.loc[:, ['title', 'price','sold_quantity','address','thumbnail','permalink','shipping','seller']].sort_values('sold_quantity',ascending=False)
        df_temp['city'] = df_temp['address'].apply(lambda x: x.get('city_name'))
        df_temp['seller_id'] = df_temp['seller'].apply(lambda x: x.get('id'))
        df_temp['free_shipping'] = df_temp['shipping'].apply(lambda x: x.get('free_shipping'))
        df_temp['state'] = df_temp['address'].apply(lambda x: x.get('state_name'))

        df_temp['total'] = df['price'] * df['sold_quantity']
        df_temp['title'] = df['title'].str.lower()
        return df_temp
    return ''

def display_product_old(product,min_price_list,max_price_list,sell_condition,percentage_max_sell,category,total):
    num = 0
    df_temp = ''
    df_temp = pd.DataFrame.from_dict([])
    for price in min_price_list:
        min_price = min_price_list[num]
        max_price = max_price_list[num]
        mla_list = search_mla_return_list(product,min_price,max_price,category)
        ranger = '$' + str(min_price) + '-$' + str(max_price)
        df = return_products_from_list_pandas(mla_list,product,sell_condition,min_price,max_price)

        for num2 in range(0,len(df)):
            df_value = df.iloc[num2]
            if ( float(df_value['total']) > total ):
                df_temp = df_temp.append(df_value, ignore_index=True)
        if ( len(df_temp)>0):
             a=1
        num = num + 1
    return df_temp

def display_product(product, min_price_list, max_price_list, sell_condition, percentage_max_sell, category, total):
    df_temp = pd.DataFrame()

    for min_price, max_price in zip(min_price_list, max_price_list):
        mla_list = search_mla_return_list(product, min_price, max_price, category)
        ranger = '$' + str(min_price) + '-$' + str(max_price)
        df = return_products_from_list_pandas(mla_list, product, sell_condition, min_price, max_price)

        try:
            df['total'] = pd.to_numeric(df['total'], errors='coerce')

            mask = df['total'] > total
            df_filtered = df[mask]

            if not df_filtered.empty:
                if df.empty:
                    df = df_filtered.copy()
                else:
                    df = pd.concat([df_temp, df_filtered])
        except:
            pass

    return df

def return_products_from_list_in_locations_pandas(total_c,product,sell_condition,locations,min_sell,min_price,max_price):
    #print ("----- Printing products from list in LOCATION..min $:",min_price," max $:",max_price)
    #sorted(total_c, key=lambda sold_quantity: sold_quantity[8])
    #sorted(total_c, key=operator.itemgetter(16) )
    df = pd.DataFrame.from_dict(total_c)
    df_resu = pd.DataFrame()
    df_temp = df.loc[:, ['total','title', 'price','sold_quantity','address','thumbnail','permalink']].sort_values('sold_quantity',ascending=False)
    df_temp['city'] = df_temp['address'].apply(lambda x: x.get('city_name'))
    df_temp['state'] = df_temp['address'].apply(lambda x: x.get('state_name'))
    df_temp2['title'] = df_temp2['title'].str.lower()

    for palabra in product.split(' '):
        df_temp2 = df_temp2[ df_temp2['title'].str.contains(palabra)]
        df_resu = df_resu.concat([df_resu,df_temp2])
    df_temp2 = df_resu[ df_resu['city'].str.contains(locations)]
    return df_temp2


#!/usr/bin/env python

import os
import sys
import json

sys.path.append(os.environ['REPO_DIR'] + '/utilities/')
from metadata import *

stack = sys.argv[1]
input_dir = sys.argv[2]
output_dir = sys.argv[3]
filename_pairs = json.loads(sys.argv[4])
suffix = 'thumbnail'

parameter_dir = os.path.join(os.environ['REPO_DIR'], "preprocess/parameters")

rg_param = os.path.join(parameter_dir, "Parameters_Rigid.txt")
rg_param_mutualinfo = os.path.join(parameter_dir, "Parameters_Rigid_MutualInfo.txt")
rg_param_noNumberOfSamples = os.path.join(parameter_dir, "Parameters_Rigid_noNumberOfSpatialSamples.txt")
rg_param_requiredRatioOfValidSamples = os.path.join(parameter_dir, "Parameters_Rigid_RequiredRatioOfValidSamples.txt")

for fn_pair in filename_pairs:
	prev_fn = fn_pair['prev_fn']
	curr_fn = fn_pair['curr_fn']

	output_subdir = os.path.join(output_dir, curr_fn + '_to_' + prev_fn)

	if os.path.exists(output_subdir):
		sys.stderr.write('Result for aligning %s to %s already exists.\n' % (curr_fn, prev_fn))
		continue

	execute_command('rm -rf ' + output_subdir)
	os.makedirs(output_subdir)

	execute_command('%(elastix_bin)s -f %(fixed_fn)s -m %(moving_fn)s -out %(output_subdir)s -p %(rg_param)s' % \
			{'elastix_bin': os.environ['ELASTIX_BIN'],
			'rg_param': rg_param,
			'output_subdir': output_subdir,
			'fixed_fn': os.path.join(input_dir, prev_fn + '.tif'),
			'moving_fn': os.path.join(input_dir, curr_fn + '.tif')
			})


# -*- coding: utf-8 -*-
"""
Created on Sat Jul 25 19:00:10 2020

@author: Josh Burkey & Ivan Ahumada
based on code by arun prakash
https://blog.francium.tech/build-your-own-image-classifier-with-tensorflow-and-keras-dc147a15e38e

"""

import cv2
import numpy as np
import os
from random import shuffle
from tqdm import tqdm
import tensorflow as tf
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.models import Model
from keras.layers import *
from keras.optimizers import *
from keras.callbacks import *
from keras.applications.vgg16 import VGG16

#declare folders with the data and where the model is saved.#
model_weights_path = 'model'
train_data = 'train'
test_data = 'test'

#initialize classes and label split labels for identification#
def one_hot_label(img):
    label = img.split('.')[0]
    if label == 'antelope':
        ohl = np.array([1,0,0,0,0,0])
    elif label == 'bobcat':
        ohl = np.array([0,1,0,0,0,0])
    elif label == 'buffalo':
        ohl = np.array([0,0,1,0,0,0])
    elif label == 'chimpanzee':
        ohl = np.array([0,0,0,1,0,0])
    elif label == 'rhinoceros':
        ohl = np.array([0,0,0,0,1,0])
    elif label == 'wolf':
        ohl = np.array([0,0,0,0,0,1])
    return ohl

#pull in the training images and labels, resize and shuffle them#
def train_data_with_label():
    train_images = []
    for i in tqdm(os.listdir(train_data)):
        path = os.path.join(train_data, i)
        img = cv2.imread(path, cv2.IMREAD_COLOR)
        img = cv2.resize(img, (200, 200))
        train_images.append([np.array(img), one_hot_label(i)])
        shuffle(train_images)
    return train_images

#pull in testing images and labels, resize and shuffle them#
def test_data_with_label():
    test_images = []
    for i in tqdm(os.listdir(test_data)):
        path = os.path.join(test_data, i)
        img = cv2.imread(path, cv2.IMREAD_COLOR)
        img = cv2.resize(img, (200, 200))
        test_images.append([np.array(img), one_hot_label(i)])
        shuffle(test_images)
    return test_images

training_images = train_data_with_label()
testing_images = test_data_with_label()

#establish arrays for processing by model and training during network#
#compilation, establish sizes and color mode (3 for color)#
tr_img_data = np.array([i[0] for i in training_images]).reshape(-1,200,200,3)
tr_lbl_data = np.array([i[1] for i in training_images])

tst_img_data = np.array([i[0] for i in testing_images]).reshape(-1,200,200,3)
tst_lbl_data = np.array([i[1] for i in testing_images])

#pull in VGG16 pretrained model#
IMG_SHAPE = (200,200,3)
base_model = VGG16(input_shape = IMG_SHAPE,include_top = False,weights = 'imagenet')
base_model.trainable = False

#callback saves the model after each epoch#
checkpoint_callback = ModelCheckpoint(filepath=model_weights_path+"/"+'best_model.h5', verbose=1,
                                      save_best_only=False, include_optimizer=False, mode='max')

#establish model parameters and import pretrained model#
model = Sequential()
model.add(base_model)

#add input layer and first convolution and pooling layer#
model.add(InputLayer(input_shape=[200,200,3]))
model.add(Conv2D(filters=32,kernel_size=5,strides=1,padding='same', activation='relu'))
model.add(MaxPool2D(pool_size=5,padding='same'))

#add second convolution layer and pooling layer#
model.add(Conv2D(filters=50,kernel_size=5,strides=1,padding='same', activation='relu'))
model.add(MaxPool2D(pool_size=5,padding='same'))

#add third convolution layer and pooling layer#
model.add(Conv2D(filters=80,kernel_size=5,strides=1,padding='same', activation='relu'))
model.add(MaxPool2D(pool_size=5,padding='same'))

#establish neural network layers#
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512,activation='relu'))
model.add(Dropout(rate=0.5))
model.add(Dense(6,activation='softmax'))

optimizer = Adam(lr=1e-3)

#compile model, epochs and batch_size were established through testing#
model.compile(optimizer = optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x=tr_img_data,y=tr_lbl_data,epochs=30,batch_size=100, callbacks = [checkpoint_callback])
model = Model(model.input,model.output)
model.summary()

#establish plot for test data#
fig = plt.figure(figsize=(14, 14))
#run testing phase going through first 30 images in the testing image set#
for cnt, data, in enumerate(testing_images[10:40]):

    y = fig.add_subplot(6, 5, cnt+1)
    img = data[0]
    data = img.reshape(1,200, 200,3)
    model_out = model.predict([data])

    if np.argmax(model_out) == 0:
        str_label='antelope'
    elif np.argmax(model_out) == 1:
        str_label='bobcat'
    elif np.argmax(model_out) == 2:
        str_label='buffalo'
    elif np.argmax(model_out) == 3:
        str_label='chimpanzee'
    elif np.argmax(model_out) == 4:
        str_label='rhinoceros'
    elif np.argmax(model_out) == 5:
        str_label='wolf'

#plot test output from neural network model post training for visual check of accuracy#
    y.imshow(img, cmap='gray')
    plt.title(str_label)
    print(str_label)
    y.axes.get_xaxis().set_visible(False)
    y.axes.get_yaxis().set_visible(False)
plt.show()
#convert model to tflite format for use in android studio#
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
open(model_weights_path + "/" + 'img_rec_model.tflite', 'wb').write(tflite_model)


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

import logging
import pika
import json
import sys

LOG_FORMAT = ('%(levelname) -10s %(asctime)s %(name) -30s %(funcName) '
              '-35s %(lineno) -5d: %(message)s')
LOGGER = logging.getLogger(__name__)

class Publisher(object):
    EXCHANGE = 'mmy_default'
    EXCHANGE_TYPE = 'topic'

    def __init__(self):
        amqp_url = 'amqp://guest:guest@localhost:5672/%2F?connection_attempts=3&heartbeat_interval=3600'
        self._connection = None
        self._channel = None
        self._url = amqp_url
        self._message_body = None
        self._routing_key = None

    def connect(self):
        LOGGER.info('Connecting to %s', self._url)
        return pika.SelectConnection(pika.URLParameters(self._url),
                                     self.on_connection_open)

    def on_connection_open(self, unused_connection):
        LOGGER.info('Connection opened')
        self.add_on_connection_close_callback()
        self.open_channel()

    def add_on_connection_close_callback(self):
        LOGGER.info('Adding connection close callback')
        self._connection.add_on_close_callback(self.on_connection_closed)

    def on_connection_closed(self, connection, reply_code, reply_text):
        self._channel = None
        LOGGER.warning('Connection closed: (%s) %s',
                           reply_code, reply_text)

    def open_channel(self):
        LOGGER.info('Creating a new channel')
        self._connection.channel(on_open_callback=self.on_channel_open)

    def on_channel_open(self, channel):
        LOGGER.info('Channel opened')
        self._channel = channel
        self.add_on_channel_close_callback()
        self.setup_exchange(self.EXCHANGE)

    def add_on_channel_close_callback(self):
        LOGGER.info('Adding channel close callback')
        self._channel.add_on_close_callback(self.on_channel_closed)

    def on_channel_closed(self, channel, reply_code, reply_text):
        LOGGER.warning('Channel was closed: (%s) %s', reply_code, reply_text)
        self._connection.close()

    def setup_exchange(self, exchange_name):
        LOGGER.info('Declaring exchange %s', exchange_name)
        self._channel.exchange_declare(self.on_exchange_declareok,
                                       exchange_name,
                                       self.EXCHANGE_TYPE)

    def on_exchange_declareok(self, unused_frame):
        LOGGER.info('Exchange declared')
        self.publish_message()

    def publish_message(self):

        properties = pika.BasicProperties(delivery_mode=1,
                                          content_type='application/json')

        self._channel.basic_publish(self.EXCHANGE, self._routing_key,
                                    json.dumps(self._message_body, ensure_ascii=False),
                                    properties)
        LOGGER.info('Published message:%s', self._message_body)
        self._connection.close()

    def send_message(self, device, message):
        self._message_body = message
        self._routing_key = device
        # Create a new connection
        self._connection = self.connect()

        self._connection.ioloop.start()

    def close_channel(self):
        LOGGER.info('Closing the channel')
        if self._channel:
            self._channel.close()

    def close_connection(self):
        LOGGER.info('Closing connection')
        self._closing = True
        self._connection.close()

    def close(self):
        self.close_channel()
        self.close_connection()

def main():
    logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT)

    # Connect to localhost:5672 as guest with the password guest and virtual host "/" (%2F)
    example = Publisher()
    try:
        test_device = 'device1'
        test_message = {u'type':u'any',
                        u'cmd':u'ls',
                        u'data':u'-l'}
        example.send_message(test_device, test_message)
    except KeyboardInterrupt:
        sys.stderr.write("Error occured\n")
        example.close()
        sys.exit(-1)

if __name__ == '__main__':
    main()


'''
DO NOT RUN THIS FILE AGAIN
TASK HAS ALREADY BEEN COMPLETED!

(In the event that you do, however, simply change line 40 to divide instead of multiply and run again.)
'''

import numpy as np
from os import listdir
from .nameparser import parse_small_file

def kelvin_to_fahrenheit(kelvin):
    return (9.0/5)*(kelvin - 273) + 32

if __name__ == "__main__":

    SCALE = 86400*0.03937 # to convert mm/sec -> in/day

    FOLDERS = (
            "GFDL-files/",
            "IPSL-files/",
            # 'HadGEM2-files/',
            # 'MIROC5-files/',
            )

    for folder in FOLDERS:
        path = "small-isimip5-files/" + folder

        for count, file in enumerate(listdir(path)):

            model, ssp, variable, start_year, end_year = parse_small_file(filename=file)
            print(model, ssp, variable, start_year, end_year)

            #? INSERT ANY FILERTERING HERE.
            #? Ex: if variable == 'pr': continue (skip)
            if variable != 'pr': continue

            #************? Do meaningful work here now for each file! **************
            try:
                loaded_arr = np.loadtxt(path+file)
                loaded_arr *= SCALE
            except:
                print("error loading:", file)
                continue

            try:
                np.savetxt(path+file, loaded_arr)
            except:
                print("Error writing to file:", file)


# Licensed to Modin Development Team under one or more contributor license agreements.
# See the NOTICE file distributed with this work for additional information regarding
# copyright ownership.  The Modin Development Team licenses this file to you 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.

"""The module holds the factory which performs I/O using pandas on unidist."""

import io

import pandas

from modin.core.storage_formats.pandas.query_compiler import PandasQueryCompiler
from modin.core.execution.unidist.generic.io import UnidistIO
from modin.core.io import (
    CSVDispatcher,
    FWFDispatcher,
    JSONDispatcher,
    ParquetDispatcher,
    FeatherDispatcher,
    SQLDispatcher,
    ExcelDispatcher,
)
from modin.core.storage_formats.pandas.parsers import (
    PandasCSVParser,
    PandasFWFParser,
    PandasJSONParser,
    PandasParquetParser,
    PandasFeatherParser,
    PandasSQLParser,
    PandasExcelParser,
)
from modin.core.execution.unidist.common import UnidistWrapper, SignalActor
from ..dataframe import PandasOnUnidistDataframe
from ..partitioning import PandasOnUnidistDataframePartition

class PandasOnUnidistIO(UnidistIO):
    """Factory providing methods for performing I/O operations using pandas as storage format on unidist as engine."""

    frame_cls = PandasOnUnidistDataframe
    query_compiler_cls = PandasQueryCompiler
    build_args = dict(
        frame_partition_cls=PandasOnUnidistDataframePartition,
        query_compiler_cls=PandasQueryCompiler,
        frame_cls=PandasOnUnidistDataframe,
        base_io=UnidistIO,
    )

    def __make_read(*classes, build_args=build_args):
        # used to reduce code duplication
        return type("", (UnidistWrapper, *classes), build_args).read

    def __make_write(*classes, build_args=build_args):
        # used to reduce code duplication
        return type("", (UnidistWrapper, *classes), build_args).write

    read_csv = __make_read(PandasCSVParser, CSVDispatcher)
    read_fwf = __make_read(PandasFWFParser, FWFDispatcher)
    read_json = __make_read(PandasJSONParser, JSONDispatcher)
    read_parquet = __make_read(PandasParquetParser, ParquetDispatcher)
    to_parquet = __make_write(ParquetDispatcher)
    # Blocked on pandas-dev/pandas#12236. It is faster to default to pandas.
    # read_hdf = __make_read(PandasHDFParser, HDFReader)
    read_feather = __make_read(PandasFeatherParser, FeatherDispatcher)
    read_sql = __make_read(PandasSQLParser, SQLDispatcher)
    to_sql = __make_write(SQLDispatcher)
    read_excel = __make_read(PandasExcelParser, ExcelDispatcher)

    del __make_read  # to not pollute class namespace
    del __make_write  # to not pollute class namespace

    @staticmethod
    def _to_csv_check_support(kwargs):
        """
        Check if parallel version of ``to_csv`` could be used.

        Parameters
        ----------
        kwargs : dict
            Keyword arguments passed to ``.to_csv()``.

        Returns
        -------
        bool
            Whether parallel version of ``to_csv`` is applicable.
        """
        path_or_buf = kwargs["path_or_buf"]
        compression = kwargs["compression"]
        if not isinstance(path_or_buf, str):
            return False
        # case when the pointer is placed at the beginning of the file.
        if "r" in kwargs["mode"] and "+" in kwargs["mode"]:
            return False
        # encodings with BOM don't support;
        # instead of one mark in result bytes we will have them by the number of partitions
        # so we should fallback in pandas for `utf-16`, `utf-32` with all aliases, in instance
        # (`utf_32_be`, `utf_16_le` and so on)
        if kwargs["encoding"] is not None:
            encoding = kwargs["encoding"].lower()
            if "u" in encoding or "utf" in encoding:
                if "16" in encoding or "32" in encoding:
                    return False
        if compression is None or not compression == "infer":
            return False
        if any((path_or_buf.endswith(ext) for ext in [".gz", ".bz2", ".zip", ".xz"])):
            return False
        return True

    @classmethod
    def to_csv(cls, qc, **kwargs):
        """
        Write records stored in the `qc` to a CSV file.

        Parameters
        ----------
        qc : BaseQueryCompiler
            The query compiler of the Modin dataframe that we want to run ``to_csv`` on.
        **kwargs : dict
            Parameters for ``pandas.to_csv(**kwargs)``.
        """
        if not cls._to_csv_check_support(kwargs):
            return UnidistIO.to_csv(qc, **kwargs)

        signals = SignalActor.remote(len(qc._modin_frame._partitions) + 1)

        def func(df, **kw):  # pragma: no cover
            """
            Dump a chunk of rows as csv, then save them to target maintaining order.

            Parameters
            ----------
            df : pandas.DataFrame
                A chunk of rows to write to a CSV file.
            **kw : dict
                Arguments to pass to ``pandas.to_csv(**kw)`` plus an extra argument
                `partition_idx` serving as chunk index to maintain rows order.
            """
            partition_idx = kw["partition_idx"]
            # the copy is made to not implicitly change the input parameters;
            # to write to an intermediate buffer, we need to change `path_or_buf` in kwargs
            csv_kwargs = kwargs.copy()
            if partition_idx != 0:
                # we need to create a new file only for first recording
                # all the rest should be recorded in appending mode
                if "w" in csv_kwargs["mode"]:
                    csv_kwargs["mode"] = csv_kwargs["mode"].replace("w", "a")
                # It is enough to write the header for the first partition
                csv_kwargs["header"] = False

            # for parallelization purposes, each partition is written to an intermediate buffer
            path_or_buf = csv_kwargs["path_or_buf"]
            is_binary = "b" in csv_kwargs["mode"]
            csv_kwargs["path_or_buf"] = io.BytesIO() if is_binary else io.StringIO()
            storage_options = csv_kwargs.pop("storage_options", None)
            df.to_csv(**csv_kwargs)
            csv_kwargs.update({"storage_options": storage_options})
            content = csv_kwargs["path_or_buf"].getvalue()
            csv_kwargs["path_or_buf"].close()

            # each process waits for its turn to write to a file
            UnidistWrapper.materialize(signals.wait.remote(partition_idx))

            # preparing to write data from the buffer to a file
            with pandas.io.common.get_handle(
                path_or_buf,
                # in case when using URL in implicit text mode
                # pandas try to open `path_or_buf` in binary mode
                csv_kwargs["mode"] if is_binary else csv_kwargs["mode"] + "t",
                encoding=kwargs["encoding"],
                errors=kwargs["errors"],
                compression=kwargs["compression"],
                storage_options=kwargs.get("storage_options", None),
                is_text=not is_binary,
            ) as handles:
                handles.handle.write(content)

            # signal that the next process can start writing to the file
            UnidistWrapper.materialize(signals.send.remote(partition_idx + 1))
            # used for synchronization purposes
            return pandas.DataFrame()

        # signaling that the partition with id==0 can be written to the file
        UnidistWrapper.materialize(signals.send.remote(0))
        # Ensure that the metadata is syncrhonized
        qc._modin_frame._propagate_index_objs(axis=None)
        result = qc._modin_frame._partition_mgr_cls.map_axis_partitions(
            axis=1,
            partitions=qc._modin_frame._partitions,
            map_func=func,
            keep_partitioning=True,
            lengths=None,
            enumerate_partitions=True,
            max_retries=0,
        )
        # pending completion
        UnidistWrapper.materialize(
            [part.list_of_blocks[0] for row in result for part in row]
        )