import os
import re
import time
import csv
from difflib import HtmlDiff
from urllib.parse import urlparse
from selenium.webdriver.common.by import By
from selenium.common.exceptions import WebDriverException
from PIL import Image
from bs4 import BeautifulSoup
from pathlib import Path
import pdb
from lib.common import *
from concurrent import futures

EXCLUDE_KEYWORDS = [
    # "/search/result",
    "usedcar/detail/",
    "usedcar/shop/",
    "inquiry",
    "/usedcar/ajax",
    # "usedcar/search_area/",
    # "usedcar/search_shashu/",
    # "search/shop/pref/",
    # "search_kodawari/pref/01",
    # "usedcar/search_price",
    "page",
    # "usedcar/area_top/",
    # "usedcar/topic/lowprice/",
    # "usedcar/topic/ecocar/",
    # "usedcar/topic/4wd/",
    # "feature/search",
    # "feature/eco_car",
    # "feature/low_price"
    ]

class WebsiteComparer:
    def __init__(self, domain1, domain2, csv_name, is_sp):
        self.logger = init_logger(csv_name)
        self.csv_name = csv_name
        self.count = 0
        self.domain1 = domain1
        self.domain2 = domain2
        self.driver1 = init_driver(is_sp=is_sp)
        self.driver2 = init_driver(is_sp=is_sp)
        # HEADER: 'Path', 'Folder name', 'Pass?', 'Website 1 urls', 'Website 2 urls', 'Exception'
        self.old_paths_checked = self._read_csv(csv_name)
        # init writer using to append new data to file after read all the old data.
        self.writer = init_csv_writer(csv_name)

        self.driver1.get(self.domain1 + "/favorites")
        self.driver2.get(self.domain2 + "/favorites")
        # cookie_names = ["favorite_cars", "passersby_recent_view", "passersby_incomplete_inquiry"]
        add_cookies(self.driver1, ["user_id_mb7"])
        add_cookies(self.driver2, ["user_id_mb8"])

    def count_folders(self, path):
        if not os.path.exists(path):
            return 0

        folders = os.listdir(path)
        return len([x for x in folders if os.path.isdir(os.path.join(path, x))])

    def reach_max_folder(self, path):
        sub_path = Path(path)
        root_path = Path(*sub_path.parts[:5])
        if self.count_folders(root_path) >= 15:
            return True
        while sub_path != root_path:
            folder_count = self.count_folders(sub_path)
            if folder_count >= 8:
                return True
            sub_path = sub_path.parent
        return False

    def get_html_and_urls(self, driver, url, num):
        driver.get(url)
        el = driver.find_element(By.TAG_NAME, 'body')
        ignore_els = self.ignore_els(url)
        time.sleep(1)
        website_page_paths = get_page_paths(driver)
        el.screenshot(f"screenshot{num}.png")
        website_html = self.get_prettified_html(driver, ignore_els)

        return [website_html, website_page_paths]

    def compare_single_page(self, path):
        urls = self.find_website_urls(path)
        if len(urls) != 0:
            return urls

        url1 = urljoin(self.domain1, path)
        url2 = urljoin(self.domain2, path)
        folder_name = f"data/{self.csv_name}{re.sub(r'[?#=&]', '/', path)}"
        split_folder = folder_name.split("/")
        name_removed = folder_name.rsplit('/', 1)[0]
        if len(split_folder) > 5 and self.reach_max_folder(name_removed):
            print(f"Next => Max Folder: {path}")
            return []

        self.count += 1
        self.logger.info(f"{self.count}: Start compare: {path}")
        os.makedirs(folder_name, exist_ok=True)
        try:
            with futures.ThreadPoolExecutor(max_workers=2) as executor:
                # Đưa các nhiệm vụ vào thread pool
                future1 = executor.submit(self.get_html_and_urls, self.driver1, url1, 1)
                future2 = executor.submit(self.get_html_and_urls, self.driver2, url2, 2)

                # Chờ cả 2 nhiệm vụ được thực hiện xong
                futures.wait([future1, future2])

                # Lấy kết quả từ các nhiệm vụ
                website1_html, website1_page_urls = future1.result()
                website2_html, website2_page_urls = future2.result()

            image1 = Image.open("screenshot1.png")
            image2 = Image.open("screenshot2.png")

            # keys1 = self.get_global_key(website1_page_urls)
            # keys2 = self.get_global_key(website2_page_urls)

            # key_equal = self.compare_list(keys1, keys2)

            merge_imgs = self._append_images([image1, image2], aligment='top')
            merge_imgs.save(f"{folder_name}/sc.png")

            diff = HtmlDiff()
            content_diff = diff.make_file(website1_html.splitlines(), website2_html.splitlines(), context=True)
            if "No Differences Found" in content_diff:
                html_pass = True
                self.logger.info("    ----No differences found-----")
            else:
                html_pass = False
                with open(f"{folder_name}/diff.html", "w", encoding="utf-8") as file:
                    file.write(content_diff)
                self.logger.info("    ++++++++DIFF+++++++")

            self._write_to_csv([path, folder_name, html_pass, "\n".join(website1_page_urls), "\n".join(website2_page_urls), ''])

            return list(set(website1_page_urls + website2_page_urls))
        except WebDriverException as ex:
            self._write_to_csv([path, folder_name, '', '', '', ex, ''])
            self.logger.info(f"    Error: {ex}")
            return []

    def compare_multiple_pages(self, path):
        extend_paths = [
            '/usedcar/feature/top', #pc, sp
            '/usedcar/feature/search', #pc, sp
            '/usedcar/feature/low_price', #pc, sp
            '/usedcar/feature/eco_car', #pc, sp
            '/usedcar/feature/Seasonal/1', #pc, sp
            '/usedcar/feature/Seasonal/2', #pc, sp
            # '/usedcar/kcar', #sp
            # '/usedcar/shop/gcs211859002/stock', #sp
            '/usedcar/topic/carmodel', #pc
            '/usedcar/new_arrival_mail', #pc
            '/usedcar/new_arrival_line', #pc
            # '/usedcar/area_top/hokkaido',
            # '/usedcar/area_top/hokushinetsu',
            # '/usedcar/area_top/kansai',
            # '/usedcar/area_top/kanto',
            # '/usedcar/area_top/kyushu',
        ]
        remaining_paths = self.compare_single_page(path) +  extend_paths
        checked_paths = [path, '/usedcar/logout', '/usedcar/recent_search']
        while(remaining_paths):
            if self.count >= 5000:
                return
            current_path = remaining_paths.pop(0)
            if "/usedcar" not in current_path or any(keyword in current_path for keyword in EXCLUDE_KEYWORDS):
                continue

            if current_path in checked_paths:
                continue

            paths = self.compare_single_page(current_path)
            checked_paths.append(current_path)
            remaining_paths.extend(paths)
            print(f"Remaining urls left: {len(set(remaining_paths))}")
        self.logger.info(f"Remaining urls left: {len(set(remaining_paths))}")

    def find_website_urls(self, test_path):
        if test_path in self.old_paths_checked:
            return self.old_paths_checked[test_path]
        return []

    def get_prettified_html(self, driver, ignore_els):
        page_source = driver.page_source
        soup = BeautifulSoup(page_source, 'html.parser')

        elements = soup.select(", ".join(ignore_els))

        for element in elements:
            element.extract()

        self._remove_domain_from_links(soup, 'a', 'href')
        self._remove_domain_from_links(soup, 'form', 'action')

        return soup.body.prettify()

    def _remove_domain_from_links(self, soup, tag_name, attribute):
        for tag in soup.find_all(tag_name):
            attr_value = tag.get(attribute)
            if attr_value:
                new_value = urlparse(attr_value).path
                tag[attribute] = new_value

    def _read_csv(self, file_name):
        try:
            data = {}
            with open(f"data/{file_name}.csv", 'r') as file:
                reader = csv.reader(file)

                for row in reader:
                    data[row[0]] = row[3].split("\n") + row[4].split("\n")

            return data
        except FileNotFoundError:
            self.logger.info(f"File not found. New file created: data/{file_name}.csv")
            return {}

    def _write_to_csv(self, data):
        write_to_csv(self.writer, data)

    def _append_images(self, images, direction='horizontal', bg_color=(255,255,255), aligment='center'):
        widths, heights = zip(*(i.size for i in images))

        if direction=='horizontal':
            new_width = sum(widths)
            new_height = max(heights)
        else:
            new_width = max(widths)
            new_height = sum(heights)

        new_im = Image.new('RGB', (new_width, new_height), color=bg_color)

        offset = 0
        for im in images:
            if direction=='horizontal':
                y = 0
                if aligment == 'center':
                    y = int((new_height - im.size[1])/2)
                elif aligment == 'bottom':
                    y = new_height - im.size[1]
                new_im.paste(im, (offset, y))
                offset += im.size[0]
            else:
                x = 0
                if aligment == 'center':
                    x = int((new_width - im.size[0])/2)
                elif aligment == 'right':
                    x = new_width - im.size[0]
                new_im.paste(im, (x, offset))
                offset += im.size[1]

        return new_im

    def compare_list(self, l1, l2):
        l1.sort()
        l2.sort()
        if (l1 == l2):
            return True
        return False

    def get_global_key(self, urls):
        keys = []
        for url in urls:
            if "usedcar/detail/" not in url and "usedcar/shop/" not in url:
                continue
            if 'stock' in url:
                url = url.split("/stock")[0]
            key = url.rsplit("/", 1)[1]
            keys.append(key)
        return keys

    def ignore_els(self, url):
        ignore_els = [
            '.image',
            'input[name="authenticity_token"]',
            '[id^="batBeacon"]',
            '.ls-is-cached',
            '.lazyloading',
            '.lazyloaded',
            '.box_new',
            '.recommend-list-sp',
            '#page-top',
            '#ajax-recommends',
            '.ranking_list',
            '.ranking-shop',
            'img',
            '.status__label-list',
            '.information_board_pickup',
            '.ranking-list-item',
            '.recommendation_item',
            'source', # diff in url source image
            '.heading-level-02.header-fixed', # diff in style opacity
            '.favorite_count',
            '.recommendation_box_list',
            'script',
            '.usedResultList_item', # pc
            '.resultModePhoto', #pc
            '#user_search',
            '.internal-links-list.cf', # recommend result
            '.table_shop_pickup', # information board - pc
            # '.information_board_pickup', # information board - sp - tmp
            '.btn_line', # login/logout button
        ]

        if "feature" in url or 'search/result' in url or 'detail' in url:
            ignore_els += ['.balloon_message', '.result-wrap', '.review_detail_slider']
        elif "area_top" in url or "gd_city" in url:
            ignore_els.append('#resultWrapper')
        elif "topic" in url:
            ignore_els += ['iframe', 'noscript', 'meta']
        return ignore_els


'''
#gnuess My Number Exercise
@author = navin106
guessing game
'''
def main():
    '''
    function to find correct number
    '''
    low_n = 0
    mid_n = 50
    high_n = 100
    i = 'l'
    while i != 'c':
        print(mid_n)
        i = input("enter 'h' if gnuess is too high,\
            'l' if its too low.'c' to indicate I gnuessed correctly")
        if i == 'h':
            high_n = mid_n
            mid_n = (high_n+low_n)//2
        elif i == 'l':
            low_n = mid_n
            mid_n = (high_n+low_n)//2
    print('your gnuess number is :', mid_n)
if __name__ == "__main__":
    main()


from django.urls import path
from cart import views

urlpatterns = [
    path('', views.cart, name='cart'),
    path('add/<int:id>', views.add, name='add_item'),
    path('remove/<int:id>', views.remove, name="remove_item"),
    path('cart', views.remove_all, name="remove_all"),
    path('purchase/', views.purchase, name='purchase'),
    path('purchase/success/', views.purchase_success, name='purchase_success'),
    path('purchase/failed/', views.purchase_failed, name='purchase_failed'),
]


# Create a function that always returns True for every item in a given list. However, if an element is the word 'flick', switch to always returning the opposite boolean value.

lst = ['codewars', 'flick', 'code', 'wars']

def flick_switch(lst):
    new=[]
    switch = 0
    for item in lst:
        if item == "flick" and switch == 0:
            switch = 1
        elif item == "flick" and switch == 1:
            switch = 0

        if switch == 0:
            new.append(True)
        elif switch == 1:
            new.append(False)
    return new


import os
import pytest
from . import execute_sql_to_df
from . import read_sql

@pytest.fixture()
def select_script():
    path = os.getenv("SCRIPT_PATH")
    return read_sql(path)

@pytest.fixture()
def select_df(select_script, sqlalchemy_conn):
    return execute_sql_to_df(
        conn=sqlalchemy_conn,
        sql=select_script
    )

def test_view(select_df):
    assert set(select_df.columns) == {'order_id', 'buyer_name', 'seller_name',
                                      'product_name', 'amount', 'country', 'status', 'date'}
    assert select_df['order_id'].tolist() == [0, 0, 1, 2, 2, 3, 3, 3, 4, 5, 6]
    assert select_df['buyer_name'].tolist() == ["John Doe", "John Doe", "Jane Doe", "Bob Smith", "Bob Smith",
                                                "Alice Jones", "Alice Jones", "Alice Jones", "Mike Johnson", "John Smith", "Jane Smith"]
    assert select_df['seller_name'].tolist() == ["Seller 2", "Seller 3", "Seller 4", "Seller 5", "Seller 2", "Seller 5",
                                                 "Seller 7", "Seller 8", "Seller 7", "Seller 7", "Seller 7"]
    assert select_df['product_name'].tolist() == ["April Birthstone Heart Dangle Charm",
                                                 "Freshwater Pearl Necklace in Sterling Silver, 32",
                                                 "LA PANTHÈRE DE CARTIER WATCH",
                                                 "Earrings made of white gold with diamonds",
                                                 "April Birthstone Heart Dangle Charm",
                                                 "Earrings made of white gold with diamonds",
                                                 "Bangle in White Gold with Diamond Accents",
                                                 "Blue Pansy Flower Pendant",
                                                 "Bangle in White Gold with Diamond Accents",
                                                 "Bangle in White Gold with Diamond Accents",
                                                 "Bangle in White Gold with Diamond Accents"]
    assert select_df['amount'].tolist() == [1880, 1880, 91975, 2257, 2257, 12887, 12887, 12887, 10750, 10750, 10750]
    assert select_df['country'].tolist() == ["UK", "UK", "Russia", "USA", "USA", "France", "France",
                                             "France", "USA", "USA", "USA"]
    assert select_df['status'].tolist() == ["In progress", "In progress", "In progress", "Completed", "Completed", "Pending",
                                            "Pending", "Pending", "Completed", "Completed", "Pending"]
    assert select_df['date'].astype(str).tolist() == ['2022-07-01', '2022-07-01', '2022-08-11', '2022-01-01', '2022-01-01', '2022-09-07',
                                          '2022-09-07', '2022-09-07', '2022-02-12', '2021-11-12', '2022-09-12']


# basic info
PROGRAM_NAME = "The Chesser"

# character keywords
COMMENT_CHARACTER = "/"
IMPORT_CHARACTER = "@"
IMPORT_CHARACTER_AGAINST = "!"
OPENING_OBJECT_CHARACTER = "{"
CLOSING_OBJECT_CHARACTER = "}"

# opening files metadata
OPENINGS_PATH = "./openings"
OPENINGS_EXTENSION = "cl"
OPENINGS_COUNTER_PREFIX = "Vs"
PRIMARY_WHITE = "ForWhite"
PRIMARY_BLACK = "ForBlack"

# function usage
MOVE_SEPARATOR = " "
CASE_SENSITIVE = False

# CLI
MOVE_HISTORY_BORDER = "|"


#!/usr/bin/env python2
#
# Take a list of websites and scan them for any version headers
# Common headers are ignored, so only version or interesting ones are shown
# Sites with the same headers are merged in the output
# Usage: $ ./headerget.py <targetfile or domain>
# Target file can be nmap or servicescan XML output (use service detection with XML)
# Otherwise target file can be plain text, with one target per line
# Missing or misconfigured security headers are also listed
#

import os
import platform
import re
import requests
import sys
import traceback
from xml.dom import minidom
import OpenSSL

class col:
    if sys.stdout.isatty() and platform.system() != "Windows":
        green = '\033[32m'
        blue = '\033[94m'
        red = '\033[31m'
        brown = '\033[33m'
        end = '\033[0m'
    else:   # Colours mess up redirected output, disable them
        green = ""
        blue = ""
        red = ""
        brown = ""
        end = ""

# Parse Nmap XML file
def xmlparse_nmap(xmldoc):
    hostlist = xmldoc.getElementsByTagName("host")
    for hostNode in hostlist :
        addressNode = hostNode.getElementsByTagName("address")
        host = addressNode[0].attributes["addr"].value
        ports = hostNode.getElementsByTagName("ports")
        try:
            portlist = ports[0].getElementsByTagName("port")
        except:
            continue
        for portNode in portlist:
            protocol = portNode.attributes["protocol"].value
            # Only interested in TCP
            if protocol != "tcp":
                continue
            port = portNode.attributes["portid"].value
            stateNode = portNode.getElementsByTagName("state")
            state = stateNode[0].attributes["state"].value
            serviceNode = portNode.getElementsByTagName("service")
            try:
                service = serviceNode[0].attributes["name"].value
            except:
                continue
            try:
                tunnel = serviceNode[0].attributes["tunnel"].value
            except:
                tunnel = ""

            if state == "open" and service == "http" and port == "80" and tunnel == "":
                target = "http://" + host
            elif state == "open" and service == "http" and tunnel == "":
                target = "http://" + host + ":" + port
            elif state == "open" and service == "http" and port == "443" and tunnel == "ssl":
                target = "https://" + host
            elif state == "open" and service == "http" and tunnel == "ssl":
                target = "https://" + host + ":" + port
            # If we don't have service info, just catch http/https based on port
            elif state == "open" and port == "80":
                target = "http://" + host
            elif state == "open"  and port == 443:
                target = "https://" + host

            try:
                targets[target] = ""
            except UnboundLocalError:
                pass

# Parse servicescan XML file
def xmlparse_servicescan(xmldoc):
    hostlist = xmldoc.getElementsByTagName("host")
    for hostNode in hostlist :
        host = hostNode.attributes["address"].value
        portlist = hostNode.getElementsByTagName("port")
        for portNode in portlist:
            protocol = portNode.attributes["protocol"].value
            # Only interested in TCP
            if protocol != "TCP":
                continue
            port = portNode.attributes["number"].value
            state = portNode.attributes["state"].value
            desc = portNode.attributes["description"].value
            if state == "open" and desc == "HTTP" and port == "80":
                target = "http://" + host
            elif state == "open" and (desc == "HTTP" or desc == "HTTP-ALT"):
                target = "http://" + host + ":" + port
            elif state == "open" and desc == "HTTPS" and port == "443":
                target = "https://" + host
            elif state == "open" and desc == "HTTPS":
                target = "https://" + host + ":" + port

            try:
                targets[target] = ""
            except UnboundLocalError:
                pass

# Parse targets (xml)
def xmlparse ():
    xmldoc = minidom.parse(sys.argv[1])
    if xmldoc.getElementsByTagName("nmaprun"):
        xmlparse_nmap(xmldoc)
    elif xmldoc.getElementsByTagName("servicescan"):
        xmlparse_servicescan(xmldoc)
    else:
        print(col.red + "Invalid XML file" + col.end)
        sys.exit(1)

# Parse targets (txt)
def txtparse():
    for line in lines:
        if not line.startswith('http'):
            line = "http://" + line
        targets[line.rstrip()] = ""

# Reverse a dictionary
def reverse_dict(dictionary):
    sorted = {}
    for k, v in dictionary.items ():
        if v:
            if v not in sorted:
                sorted [v] = []
            sorted [v].append (k)
    return sorted

# Truncate a string to 80 chars
def trunc(string):
    return (string[:75] + '[...]') if len(string) > 80 else string

# Check for missing/bad security headers
def check_security_headers(target, headers):
    # X-Frame-Options
    try:
        m = re.search("SAMEORIGIN|DENY", headers["x-frame-options"], re.IGNORECASE)
        if not m:
            badheaders[target] += "x-frame-options: " + trunc(headers["x-frame-options"]) +"\n"
    except Exception as e:
        missingsecurity[target] += "x-frame-options\n"

    # X-Content-Type-Options: nosniff
    try:
        m = re.search("nosniff", headers["x-content-type-options"], re.IGNORECASE)
        if not m:
            badheaders[target] += "x-content-type-options\n"
    except:
        missingsecurity[target] += "x-content-type-options\n"

    # X-XSS-Protection
    try:
        m = re.search("0", headers["x-xss-protection"], re.IGNORECASE)
        if m:
            badheaders[target] += "x-xss-protection: " + trunc(headers["x-xss-protection"]) + "\n"
    except:
        pass

    # Strict-Transport-Security (HSTS)
    try:
        m = re.search("max-age=(\d+)", headers["strict-transport-security"], re.IGNORECASE)
        if int(m.group(1)) < (60*60*24 * 30):     # Flag if less than 30 days
            badheaders[target] += "strict-transport-security: " + trunc(headers["strict-transport-security"]) +"\n"
    except:
        missingsecurity[target] += "strict-transport-security\n"

    # Access-Control-Allow-Origin (CORS)
    try:
        m = re.search("\*", headers["access-control-allow-origin"], re.IGNORECASE)
        if m:
            badheaders[target] += "access-control-allow-origin: " + trunc(headers["access-control-allow-origin"]) +"\n"
    except:
        pass

    # Content-Security-Policy
    if not ("content-security-policy" in headers or "x-content-security-policy" in headers or "x-webkit-csp" in headers):
        missingsecurity[target] += "content-security-policy\n"

# Print the headers
def print_headers(headerarray):
    for headers,servers in headerarray.items():
       if not headers:
           continue
       for server in servers:
           print(col.blue + server + col.end)
       print(headers)

########
# Main #
########

# Supress warnings that we're not validating certificates
requests.packages.urllib3.disable_warnings()
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

# Get targets
targets = {}
try:
    arg = sys.argv[1]
except:
    print("\nUsage: $ " + sys.argv[0] + " <targetfile or domain>\n")
    sys.exit(1)

if arg == "-h" or arg == "--help":
    print("\nUsage: $ " + sys.argv[0] + " <targetfile or domain>\n")
    sys.exit(1)

if arg.startswith("http"):
    targets[arg] = ""
else:
    try:
        with open(sys.argv[1]) as f:
            lines = f.readlines()
    except:
        print("\nCould not open file " + arg)
        sys.exit(1)

# Parse the targets file based on extension
    if sys.argv[1].endswith("xml"):
        xmlparse()
    else:
        txtparse()

# Get list of boring headers
try:
    path = os.path.dirname(os.path.realpath(__file__)) + "/boringheaders.txt"
    boringheaders = []
    with open(path, "r") as f:
        lines = f.readlines()
        for line in lines:
            boringheaders.append(line.rstrip().lower())
except IOError:
    print("File boringheaders.txt not found")
    sys.exit(1)

# Scan the servers
headersfound = targets.copy()
missingsecurity = targets.copy()
badheaders = targets.copy()
for target in headersfound:
    if sys.stdout.isatty():
        sys.stdout.write(target + "                                        \r")
        sys.stdout.flush()
    try:
        # Timeout after 2 seconds, don't try and verify the SSL cert
        r = requests.head(target, timeout=2, verify=False)
    except requests.exceptions.RequestException:
        try:
            print("HEAD failed, trying GET")
            r = requests.get(target, timeout=2, verify=False)
        except Exception as e:
            continue
    except requests.exceptions.ReadTimeout:
        print(col.red + target + " timed out" + col.end)
        continue
    except requests.exceptions.ConnectTimeout:
        print(col.red + target + " timed out" + col.end)
        continue
    except requests.exceptions.SSLError:
        print(col.red + target + " SSL error" + col.end)
        continue
    except OpenSSL.SSL.ZeroReturnError:
        print(col.red + target + " empty response" + col.end)
        continue
    except KeyboardInterrupt:
        print("\n\nCaught KeyboardInterrupt, quitting...")
        print("Results so far:\n")
        break
    except:
        print(traceback.format_exc())
        continue

    for header in r.headers:
        if header.lower() not in boringheaders:
            h = (r.headers[header][:75] + '[...]') if len(r.headers[header]) > 80 else r.headers[header]
            headersfound[target] += header + ": " + h + "\n"
    check_security_headers(target, r.headers)

# Get rid of any trailing characters on the TTY
if sys.stdout.isatty():
    sys.stdout.write("                                                         \r")
    sys.stdout.flush()

# Interesting Headers
sorted = reverse_dict(headersfound)
if len(sorted) > 0:
    print(col.green + 'Interesting Headers' + col.end)
    print_headers(sorted)

# Missing security headers
sorted = reverse_dict(missingsecurity)
if len(sorted) > 0:
    print('\n' + col.brown + 'Missing Security Headers' + col.end)
    print_headers(sorted)

# Bad security headers
sorted = reverse_dict(badheaders)
if len(sorted) > 0:
    print('\n' + col.red + 'Bad Security Headers' + col.end)
    print_headers(sorted)


import csv

employees = [
        {'name': 'Маша', 'age': 25, 'job': 'Scientist'},
        {'name': 'Вася', 'age': 8, 'job': 'Programmer'},
        {'name': 'Эдуард', 'age': 48, 'job': 'Big boss'},
]

with open('employees.csv', 'w', encoding='utf-8', newline = '') as file:
    fields = ['name', 'age', 'job']
    writer = csv.DictWriter(file, fields, delimiter = ';')
    writer.writeheader()
    for record in employees:
        writer.writerow(record)


from sklearn.model_selection import GridSearchCV
from sklearn.metrics import roc_auc_score, roc_curve, auc
from sklearn.metrics import average_precision_score, precision_recall_curve
from sklearn.metrics import mean_absolute_error, mean_squared_error
from sklearn.linear_model import LogisticRegression, LinearRegression
from sklearn.externals import joblib
import math
import numpy as np
import pandas as pd
import sys
import matplotlib.pyplot as plt

def clfreport(modeltype,
              debug,
              devcheck,
              algo,
              X_train,
              y_train,
              X_test,
              y_test=None,
              param=None,
              cores=4,
              tune=False,
              use_saved_model=False,
              col_list=None):
    """

    Parameters
    ----------
    modeltype
    debug
    devcheck
    algo
    X_train
    y_train
    X_test
    y_test
    param
    cores
    tune
    use_saved_model
    col_list

    Returns
    -------

    """

    # Initialize conditional vars that depend on ifelse to avoid PC warnng
    y_pred_class = None
    y_pred = None

    if devcheck == 'yesdev':

        if tune:
            clf = GridSearchCV(algo,
                               param,
                               cv=5,
                               scoring='roc_auc',
                               n_jobs=cores)
        else:
            clf = algo

        if debug:
            print('\nclf object right before fitting main model:')
            print(clf)

        if modeltype == 'classification':
            y_pred = np.squeeze(clf.fit(X_train, y_train).predict_proba(
                X_test)[:, 1])
            #y_pred_class = clf.fit(X_train, y_train).predict(X_test)
        elif modeltype == 'regression':
            y_pred = clf.fit(X_train, y_train).predict(X_test)

        print('\n', algo)
        print("Best hyper-parameters found after tuning:")

        if hasattr(clf, 'best_params_') and tune:
            print(clf.best_params_)
        else:
            print("No hyper-parameter tuning was done.")

        if modeltype == 'classification':
            print('\nMetrics:')
            roc_auc = roc_auc_score(y_test, y_pred)
            print('AU_ROC Score:', roc_auc)

            precision, recall, thresholds = precision_recall_curve(y_test, y_pred)
            pr_auc = auc(recall, precision)
            print('\nAU_PR Score:', pr_auc)

        elif modeltype == 'regression':
            print('##########################################################')
            print('Model accuracy:')
            print('\nRMSE error:', math.sqrt(mean_squared_error(y_test,
                                                                y_pred)))
            print('\nMean absolute error:', mean_absolute_error(y_test,
                                                                y_pred), '\n')
            print('##########################################################')

        # TODO: refactor this logic to be simpler
        # Return without printing variable importance for linear case
        if (not hasattr(clf, 'feature_importances_')) and (not
            hasattr(clf, 'best_estimator_')):

            return y_pred, roc_auc

        # Print variable importance if rf and not tuning
        elif hasattr(clf, 'feature_importances_'):
            write_feature_importances(clf.feature_importances_,
                                          col_list)

            return y_pred, roc_auc, clf

        # Print variable importance if rf and tuning
        elif hasattr(clf.best_estimator_, 'feature_importances_'):
            write_feature_importances(
                clf.best_estimator_.feature_importances_,
                col_list)

            return y_pred, roc_auc, clf

    elif devcheck == 'notdev':

        clf = algo

        if use_saved_model is True:

            clf = joblib.load('probability.pkl')

        else:

            if debug:
                print('\nclf object right before fitting main model:')

            clf.fit(X_train, y_train)

            joblib.dump(clf, 'probability.pkl', compress=1)

        if modeltype == 'classification':
            y_pred = np.squeeze(clf.predict_proba(X_test)[:, 1])
        elif modeltype == 'regression':
            y_pred = clf.predict(X_test)

    return y_pred

def findtopthreefactors(debug,
                        X_train,
                        y_train,
                        X_test,
                        modeltype,
                        use_saved_model):

    # Initialize conditional vars that depend on ifelse to avoid PC warnng
    clf = None

    if modeltype == 'classification':
        clf = LogisticRegression()

    elif modeltype == 'regression':
        clf = LinearRegression()

    if use_saved_model is True:

        clf = joblib.load('factorlogit.pkl')

    elif use_saved_model is False:

        if modeltype == 'classification':

            if debug:
                print('\nclf object right before fitting factor ranking model')
                print(clf)

            clf.fit(X_train, y_train).predict_proba(X_test)

        elif modeltype == 'regression':

            if debug:
                print('\nclf object right before fitting factor ranking model')
                print(clf)

            clf.fit(X_train, y_train).predict(X_test)

        joblib.dump(clf, 'factorlogit.pkl', compress=1)

    if debug:
        print('\nCoeffs right before multiplic. to determine top 3 factors')
        print(clf.coef_)
        print('\nX_test right before this multiplication')
        print(X_test.loc[:3, :])

    # Populate X_test array of ordered col importance;
    # Start by multiplying X_test vals by coeffs
    res = X_test.values * clf.coef_

    if debug:
        print('\nResult of coef * Xtest row by row multiplication')
        for i in range(0, 3):
            print(res[i, :])

    col_list = X_test.columns.values

    first_fact = []
    second_fact = []
    third_fact = []

    if debug:
        print('\nSorting column importance rankings for each row in X_test...')

    # TODO: switch 2-d lists to numpy array
    # (although would always convert back to list for ceODBC
    for i in range(0, len(res[:, 1])):
        list_of_indexrankings = np.array((-res[i]).argsort().ravel())
        first_fact.append(col_list[list_of_indexrankings[0]])
        second_fact.append(col_list[list_of_indexrankings[1]])
        third_fact.append(col_list[list_of_indexrankings[2]])

    if debug:
        print('\nTop three factors for top five rows:')  # pretty-print w/ df
        print(pd.DataFrame({'first': first_fact[:3],
                            'second': second_fact[:3],
                            'third': third_fact[:3]}))

    return first_fact, second_fact, third_fact

def write_feature_importances(importance_attr, col_list):
    """
    This function prints an ordered list of rf-related feature importance.

    Parameters
    ----------
    importance_attr (attribute) : This is the feature importance attribute
    from a scikit-learn method that represents feature importances
    col_list (list) : Vector holding list of column names

    Returns
    -------
    Nothing. Simply prints feature importance list to console.
    """
    indices = np.argsort(importance_attr)[::-1]
    print('\nVariable importance:')
    for f in range(0, len(col_list)):
        print("%d. %s (%f)" % (f + 1, col_list[indices[f]],
                               importance_attr[indices[f]]))

def calculate_rfmtry(number_of_columns, type):
    if number_of_columns < 3:
        message = "You need more than two columns to tune hyperparameters."
        raise ValueError(message)

    if type == 'classification':
        # Default to grid of 1,2,3 for start of less than 2
        start_temp = math.floor(math.sqrt(number_of_columns))
        start = start_temp if start_temp >= 2 else 2
        grid_mtry = [start-1,start,start+1]

    if type == 'regression':
        # Default to grid of 1,2,3 for start of less than 2
        start_temp = math.floor(number_of_columns/3)
        start = start_temp if start_temp >= 2 else 2
        grid_mtry = [start-1,start,start+1]

    return grid_mtry

def GenerateAUC(predictions, labels, aucType='SS', plotFlg=False, allCutoffsFlg=False):
    """
    This function creates an ROC or PR curve and calculates the area under it.

    Parameters
    ----------
    predictions (list) : predictions coming from an ML algorithm of length n.
    labels (list) : true label values corresponding to the predictions. Also length n.
    aucType (str) : either 'SS' for ROC curve or 'PR' for precision recall curve. Defaults to 'SS'
    plotFlg (bol) : True will return plots. Defaults to False.
    allCutoffsFlg (bol) : True will return plots. Defaults to False.

    Returns
    -------
    AUC (float) : either AU_ROC or AU_PR
    """
    # Error check for uneven length predictions and labels
    if len(predictions) != len(labels):
        raise Exception('Data vectors are not equal length!')

    # make AUC type upper case.
    aucType = aucType.upper()

    # check to see if AUC is SS or PR. If not, default to SS
    if aucType != 'SS' and aucType != 'PR':
        print('Drawing ROC curve with Sensitivity/Specificity')
        aucType = 'SS'

    # Compute ROC curve and ROC area
    if aucType == 'SS':
        fpr, tpr, thresh = roc_curve(labels, predictions)
        area = auc(fpr, tpr)
        print('Area under ROC curve (AUC): %0.2f' % area)
        # get ideal cutoffs for suggestions
        d = (fpr - 0)**2 + (tpr - 1)**2
        ind = np.where(d == np.min(d))
        bestTpr = tpr[ind]
        bestFpr = fpr[ind]
        cutoff = thresh[ind]
        print("Ideal cutoff is %0.2f, yielding TPR of %0.2f and FPR of %0.2f" % (cutoff, bestTpr, bestFpr))
        if allCutoffsFlg is True:
            print('%-7s %-6s %-5s' % ('Thresh', 'TPR', 'FPR'))
            for i in range(len(thresh)):
                print('%-7.2f %-6.2f %-6.2f' % (thresh[i], tpr[i], fpr[i]))

        # plot ROC curve
        if plotFlg is True:
            plt.figure()
            plt.plot(fpr, tpr, color='darkorange',
                     lw=2, label='ROC curve (area = %0.2f)' % area)
            plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
            plt.xlim([0.0, 1.0])
            plt.ylim([0.0, 1.05])
            plt.xlabel('False Positive Rate')
            plt.ylabel('True Positive Rate')
            plt.title('Receiver operating characteristic curve')
            plt.legend(loc="lower right")
            plt.show()
        return ({'AU_ROC': area,
                 'BestCutoff': cutoff[0],
                 'BestTpr': bestTpr[0],
                 'BestFpr': bestFpr[0]})
    # Compute PR curve and PR area
    else: # must be PR
        # Compute Precision-Recall and plot curve
        precision, recall, thresh = precision_recall_curve(labels, predictions)
        area = average_precision_score(labels, predictions)
        print('Area under PR curve (AU_PR): %0.2f' % area)
        # get ideal cutoffs for suggestions
        d = (precision - 1) ** 2 + (recall - 1) ** 2
        ind = np.where(d == np.min(d))
        bestPre = precision[ind]
        bestRec = recall[ind]
        cutoff = thresh[ind]
        print( "Ideal cutoff is %0.2f, yielding TPR of %0.2f and FPR of %0.2f"
               % (cutoff, bestPre, bestRec))
        if allCutoffsFlg is True:
            print('%-7s %-10s %-10s' % ('Thresh', 'Precision', 'Recall'))
            for i in range(len(thresh)):
                print('%5.2f %6.2f %10.2f' %(thresh[i],precision[i], recall[i]))

        # plot PR curve
        if plotFlg is True:
            # Plot Precision-Recall curve
            plt.figure()
            plt.plot(recall, precision, lw=2, color='darkred',
                     label='Precision-Recall curve' % area)
            plt.xlabel('Recall')
            plt.ylabel('Precision')
            plt.ylim([0.0, 1.05])
            plt.xlim([0.0, 1.0])
            plt.title('Precision-Recall AUC={0:0.2f}'.format(
                area))
            plt.legend(loc="lower right")
            plt.show()
        return({'AU_PR':area,
                'BestCutoff':cutoff[0],
                'BestPrecision':bestPre[0],
                'BestRecall':bestRec[0]})

if __name__ == "__main__":
    pass


# From the OpenCV library import imread, imwrite and resize function only.
from cv2 import imread, imwrite, resize

# Reading the image using imread() function
image = imread("../0_assets/road.jpg")

# Adjust the parameter's value to your liking.
# ! Keep in mind that this will be the resulting (w, h) to image.
w, h = (800, 700)

# First Parameter is the one who loads the image with imread.
# Second Parameter is tuple containing width and height.
resize_mod = resize(image, (w, h))

# Save the resized image buffer.
imwrite("result.jpg", resize_mod)


from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QTimer
from PyQt5.QtCore import pyqtSlot, pyqtSignal, QObject

import util
util.redirectPrintToShell(globals())

class QuickCmdEditDialog(QtWidgets.QDialog):
    finished = pyqtSignal([str, str, str])
    def __init__(self, *args):
        super().__init__(*args)
        self.mode = ""

        self.setModal(True)
        font = QtGui.QFont()
        font.setFamily("DejaVu Sans Mono")
        font.setPointSize(12)
        self.setFont(font)

        self.setFixedSize(500, 460)

        self.labelQuickCmdName = QtWidgets.QLabel(self)
        self.labelQuickCmdName.setGeometry(QtCore.QRect(20, 20, 60, 30))
        self.labelQuickCmdName.setText("Name:")

        self.lineEditQuickCmdName = QtWidgets.QLineEdit(self)
        self.lineEditQuickCmdName.setGeometry(QtCore.QRect(100, 20, 350, 30))

        self.labelQuickCmdSourceCode = QtWidgets.QLabel(self)
        self.labelQuickCmdSourceCode.setGeometry(QtCore.QRect(20, 60, 300, 30))
        self.labelQuickCmdSourceCode.setText("Source Code:")

        self.plainTextEditSourceCode = QtWidgets.QPlainTextEdit(self)
        self.plainTextEditSourceCode.setGeometry(QtCore.QRect(20, 100, 460, 280))

        self.buttonOk = QtWidgets.QPushButton(self)
        self.buttonOk.setGeometry(QtCore.QRect(190, 400, 80, 40))
        self.buttonOk.clicked.connect(self.onOkButtonClicked)
        self.buttonOk.setText("OK")
    def onOkButtonClicked(self):
        name = self.lineEditQuickCmdName.text()
        sourceCode = self.plainTextEditSourceCode.toPlainText()
        if not name or not sourceCode:
            self.messageBox = QtWidgets.QMessageBox(self)
            if not name:
                self.messageBox.setText("Name cannot be empty")
            elif not sourceCode:
                self.messageBox.setText("source code cannot be empty")
            self.messageBox.open()
        else:
            self.finished.emit(self.mode, name, sourceCode)
            self.accept()
    def setMode(self, mode):
        self.mode = mode
    def setCmdName(self, name):
        self.lineEditQuickCmdName.setText(name)
    def setSourceCode(self, code):
        self.plainTextEditSourceCode.setPlainText(code)


"""
Proyecto python MySQL

El pryecto consiste en crear una aplicacion
que sirva para guardar notas de diferentes usuarios:
* Abrir asistente
* Login o registro
* Si elegimos registro, crear un usuario en la DB
* Si elegimos Login, identificar al usuario y preguntar acciones
* Crear, mostraar y eliminar notas

"""

from usuarios import acciones

hacer = acciones.Acciones()

print(""" Acciones disponibles:
    * Registro (r)
    * Login (l)
""")

accion = input("¿Que desea hacer?:")

if accion == "r":
    hacer.registro()

elif accion == "l":
    hacer.login()


import re
import os
import logging
import pyrogram
from config import Config
from plugins import helper
from pyrogram import Client, filters

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
logging.getLogger("pyrogram").setLevel(logging.WARNING)

Current_Text = {}

#@Client.on_message(filters.text & ~filters.command)
def my_handler(client, message):
    print(message)

async def GetCurrentTextToStyle(Chat_Id):
  try:
    TextToChange = Current_Text[int(Chat_Id)]
  except:
    TextToChange = "Nonee"
  return TextToChange

@Client.on_message(filters.private & filters.text & filters.regex(r"^(?!/).*"))
async def TextHandlewithFont(bot,message):
  #Current_Text.clear()
  #Current_Text.append(message.text)
  print(message.chat.id)
  Current_Text[message.chat.id] = message.text
  await message.reply_text("Choose Your Methods",reply_markup=helper.STARTFontingBTN)
  print(Current_Text)


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

#
# # MACD Implementation
#
#

# ## Celem projektu jest zaipmlementowanie wskaźnika giełdowego MACD
# ## i opracowanie algorytmu , który na podstawie MACD będzie
# ## podejmował za nas decyzje o sprzedaży i zakupie

# ### Importing libraries
#

# In[4]:

import numpy as np
import math
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
get_ipython().run_line_magic('matplotlib', 'inline')
import mpld3
mpld3.enable_notebook()

# ### Parsowanie danych wejściowych z pliku (txt/csv)
#

# In[5]:

data = pd.read_csv('TSLA.csv')
#data = pd.read_csv('MSFT.csv')
start_date = '2016-01-01'
end_date = '2021-01-01'
prices_with_date =  data.loc[(data['Date'] > start_date) & (data['Date'] < end_date)]
prices_with_date

# ### Wizualizacja danych wejściowych
#
#
#

# In[6]:

price_array = prices_with_date['Close']
date_array = pd.to_datetime(prices_with_date['Date'])
plt.figure(figsize=(10, 5))
plt.xlabel("dni")
plt.ylabel("money")
plt.plot( date_array, price_array,label = "Closed price")
plt.gcf().autofmt_xdate()
plt.tight_layout()
plt.legend()
plt.show()

# ### EMA formula
# #### Function that compute Exponential Moving Average
# #### where n is period
# #### prices is an array of price with length n;

# In[8]:

def EMA(prices,n):
    a = 2/(n+1)
    wagi = (1-a)**np.arange(n,-1,-1)
    return (wagi*prices).sum()/wagi.sum()

# ### Calculating EMA1 with period 12 and EMA2 with period 26
# #### And EMA1 - EMA2
# ####  Calculating Exponensial moving average starting with 26 day because it's impossible to calculate EMA_2(26) before this day

# In[9]:

price_array
period_1 = 12
period_2 = 26
EMA1 = []
EMA2 = []
MACD = []
for i in range(period_2,price_array.size):
    ema_elem1 = EMA(price_array[i-period_1:i], period_1-1)
    EMA1.append(ema_elem1)
    ema_elem2 = EMA(price_array[i-period_2:i],period_2-1)
    EMA2.append(ema_elem2)
    MACD.append(ema_elem1 - ema_elem2)

# #### start drawing plot starting 26 day.
#

# In[10]:

plt.figure(figsize=(10, 10))
plt.xlabel("dni")
plt.ylabel("money")
plt.plot(date_array,price_array, label = "closed price")
plt.plot(date_array[period_2:],EMA1, label= "EMA12")
plt.plot(date_array[period_2:],EMA2, label= "EMA26")
plt.plot(date_array[period_2:],MACD, label= "MACD")
plt.legend()

# ### Implementacja wskaźnika MACD
#
# #### Linii Sygnal i MACD
# #### invest dates starting with period 2 + period 3 = 26 + 9 = 35 day
# #### invest points shows where exactly crossing of two lines happend
# #### invest dicision True - sell , False - buy
#

# In[11]:

period_3 = 9
Sygnal = []
for i in range(period_3,len(MACD)):
    Sygnal.append(EMA(MACD[i-period_3:i],period_3-1))

MACD_is_higher = MACD[0] > Sygnal[0]
invest_dates = []
invest_price = []
invest_decision = []
change_cnt = 0

# ### Ploting
#

# In[12]:

plt.figure(figsize=(10, 5))
plt.xlabel("dni")
plt.ylabel("money")
plt.plot(date_array[period_2+period_3:], Sygnal,label="Sygnal")
plt.plot(date_array[period_2+period_3:], MACD[period_3:],label="MACD")
plt.plot(date_array[period_2+period_3:],price_array[period_2+period_3:],label = "closed price")
difference = np.array(MACD[period_3:]) - np.array(Sygnal)
plt.legend()

# In[13]:

# True - buy
MACD_bigger = difference[0] > 0
invest = []
for i in range(0,len(difference)):
    is_change = False
    if difference[i] > 0 and MACD_bigger == False:
        MACD_bigger = True
        invest.append((True, np.array(price_array)[period_2+period_3+i]))
    elif difference[i] < 0 and MACD_bigger == True:
        MACD_bigger = False
        invest.append((False, np.array(price_array)[period_2+period_3+i]))

# ## Analiza i omówienie wyników, wniosków
#
#
#

# ## Analiza automatycznego zarabiania przez algorytm

# In[14]:

gotowka = 0
stock = 1000
price_begin = np.array(price_array)[period_2+period_3]
print("price_begin:" + str(price_begin))
print("Stock begin:" + str(stock))
print("Gotowka:" + str(gotowka))
money_begin = stock * price_begin + gotowka

for decision, price in invest:
    if decision: # BUY
        amount =  math.floor(gotowka/price)
        stock += amount
        gotowka -= price * amount
    else:# SELL
        amount = stock
        gotowka += amount * price
        stock -= amount
price_end = np.array(price_array)[-1]
money_end = stock * price_end + gotowka
print("price_end:" + str(price_end))
print("Stock end:" + str(stock))
print("Gotowka:" + str(gotowka))
print("Money end/ Money Begin " + str(money_end/ money_begin))
print("Algorytm Save and Keep " + str(price_end/price_begin))

# #### Algorytm sprzedawania wszystkich akcji (dla Microsoft i Tesla)daje wynik gorszy niż zachowanie tych akcji.
# #### Z tego wynika ze wykorzystanie MACD nie jest skuteczne(przynajmniej dla tych kompanij i algorytmu w którym sprzedajemy wszytko i kupujemy ile się da).

# In[30]:

gotowka = 0
stock = 1000
price_begin = np.array(price_array)[period_2+period_3]
print("price_begin:" + str(price_begin))
print("Stock begin:" + str(stock))
print("Gotowka:" + str(gotowka))
money_begin = stock * price_begin + gotowka
amount = 100
for decision, price in invest:
    if decision: # BUY
        stock += amount
        gotowka -= price * amount
    else:# SELL
        gotowka += amount * price
        stock -= amount
price_end = np.array(price_array)[-1]
money_end = stock * price_end + gotowka
print("price_end:" + str(price_end))
print("Stock end:" + str(stock))
print("Gotowka:" + str(gotowka))
print("Money end/ Money Begin " + str(money_end/ money_begin))
print("Algorytm Save and Keep " + str(price_end/price_begin))

# ### Wyniki lepsze gdy sprzedajemy część akcji
# #### Im bliżej liczba sprzedanych/ kupowanych do 0 tym bardziej strategia podobna do przechowywania akcji.

# # Podsumowanie:
# ### Myśle że nie odkryłem pełnego potencjalu MACD. Mój algorytm pozwala zarobić, ale mniej niż to możliwe.


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
.. $Id: views.py 106507 2017-02-14 15:39:53Z carlos.sanchez $
"""

from __future__ import print_function, unicode_literals, absolute_import, division
__docformat__ = "restructuredtext en"

logger = __import__('logging').getLogger(__name__)

from itertools import chain

from zope import component
from zope import lifecycleevent

from zope.security.interfaces import IPrincipal

from pyramid import httpexceptions as hexc

from pyramid.view import view_config

from nti.app.base.abstract_views import AbstractAuthenticatedView

from nti.app.contentfile import validate_sources
from nti.app.contentfile import get_content_files
from nti.app.contentfile import read_multipart_sources

from nti.app.externalization.error import raise_json_error

from nti.app.externalization.view_mixins import ModeledContentUploadRequestUtilsMixin

from nti.app.messaging import MessageFactory as _

from nti.app.messaging.conversations import Conversation

from nti.app.messaging.interfaces import IConversationProvider

from nti.app.messaging.utils import get_user

from nti.appserver.pyramid_authorization import has_permission

from nti.appserver.ugd_edit_views import UGDPostView

from nti.dataserver import authorization as nauth

from nti.externalization.interfaces import LocatedExternalDict
from nti.externalization.interfaces import StandardExternalFields

from nti.messaging.interfaces import IMailbox
from nti.messaging.interfaces import IMessage
from nti.messaging.interfaces import IReceivedMessage

ITEMS = StandardExternalFields.ITEMS
TOTAL = StandardExternalFields.TOTAL
ITEM_COUNT = StandardExternalFields.ITEM_COUNT

@view_config(route_name='objects.generic.traversal',
             renderer='rest',
             permission=nauth.ACT_READ,
             request_method='GET',
             context=IMailbox)
class MailboxGetView(AbstractAuthenticatedView):

    def __call__(self):
        return self.context

@view_config(route_name='objects.generic.traversal',
             renderer='rest',
             permission=nauth.ACT_READ,
             request_method='GET',
             name="conversations",
             context=IMailbox)
class MailboxConversationsGetView(AbstractAuthenticatedView):

    def __call__(self):
        provider = IConversationProvider(self.context)
        conversations = provider.conversations(self.remoteUser)
        result = LocatedExternalDict()
        result.__name__ = self.request.view_name
        result.__parent__ = self.request.context
        result.lastModified = self.context.lastModified
        result[ITEMS] = conversations
        result[TOTAL] = result[ITEM_COUNT] = len(conversations)
        return result

@view_config(route_name='objects.generic.traversal',
             renderer='rest',
             permission=nauth.ACT_CREATE,
             request_method='POST',
             context=IMailbox)
class MailboxPOSTView(UGDPostView):

    content_predicate = IMessage.providedBy

    def validate_attachments(self, user, context, sources=()):
        sources = sources or ()
        validate_sources(user, context, sources)
        for source in sources:
            source.__parent__ = context

    def _validate(self, context):
        sources = get_content_files(context)
        if sources and self.request.POST:
            read_multipart_sources(self.request, sources.values())
        if sources:
            self.validate_attachments(self.remoteUser,
                                      context,
                                      sources.values())
        return context

    def __call__(self):
        message = self.readCreateUpdateContentObject(self.remoteUser,
                                                     search_owner=False)
        message = self._validate(message)
        if message.inReplyTo or message.references:
            raise_json_error(self.request,
                             hexc.HTTPUnprocessableEntity,
                             {
                                 u'message': _("Cannot create replies here."),
                                 u'code': 'CannotCreateReply',
                             },
                             None)

        message.updateLastMod()
        message.creator = self.remoteUser.username
        message.From = IPrincipal(message.creator)

        # validate receivers
        receivers = set()
        for name in message.To:
            user = get_user(name)
            if user is None:
                raise_json_error(self.request,
                                 hexc.HTTPUnprocessableEntity,
                                 {
                                     u'message': _("Invalid user"),
                                     u'code': 'InvalidUser',
                                 },
                                 None)
            receivers.add(IPrincipal(name))
        message.To = tuple(receivers)

        lifecycleevent.created(message)
        mailbox = component.getMultiAdapter((self.remoteUser, message,),
                                            IMailbox)
        # distribute to mailbox.
        mailbox.send(message)
        self.request.response.status_int = 201
        return Conversation(mailbox, message, (message,))

@view_config(route_name='objects.generic.traversal',
             renderer='rest',
             permission=nauth.ACT_UPDATE,
             request_method='POST',
             name="opened",
             context=IReceivedMessage)
class MarkOpenedView(AbstractAuthenticatedView):

    def __call__(self):
        if self.context.ViewDate is not None:
            raise_json_error(self.request,
                             hexc.HTTPUnprocessableEntity,
                             {
                                 u'message': _("Message already opened"),
                                 u'code': 'MessageAlreadyOpened',
                             },
                             None)
        self.context.mark_viewed()
        return self.context.Message

@view_config(route_name='objects.generic.traversal',
             renderer='rest',
             permission=nauth.ACT_READ,
             request_method='GET',
             name="thread",
             context=IMessage)
class ThreadView(AbstractAuthenticatedView):
    """
    Returns all the messages in the current thread.  The
    context should be the top level item (thread root)
    """

    def __call__(self):
        messages = []
        message = self.context
        for x in chain((message,), message.referents or ()):
            if has_permission(nauth.ACT_READ, x, self.request):
                messages.append(x)
        result = LocatedExternalDict()
        result.__name__ = self.request.view_name
        result.__parent__ = self.request.context
        result[ITEMS] = messages
        result[TOTAL] = result[ITEM_COUNT] = len(messages)
        return result

@view_config(route_name='objects.generic.traversal',
             renderer='rest',
             permission=nauth.ACT_READ,  # if you can read you can reply
             request_method='POST',
             name="reply",
             context=IMessage)
class ReplyView(AbstractAuthenticatedView,
                ModeledContentUploadRequestUtilsMixin):
    """
    Creates and dispatches a reply in this conversation. This api
    is specialized to the way messaging currently working in the housing
    app.  The threads are one long thread A<-B<-C.  Our context here
    should be the top level.
    """

    def __call__(self):
        message = self.context
        if message.inReplyTo is not None:
            raise_json_error(self.request,
                             hexc.HTTPBadRequest,
                             {
                                 u'message': _("Expected top level message."),
                                 u'code': 'TopLevelMessageExpected',
                             },
                             None)

        # if nothing screwed up there should only be one message
        # without a reply (one leaf). In the event we find more than one leaf
        # we try to get things back on track by choosing the one that was created
        # most recently.
        all_messages = chain(message.referents or (), (message,))
        leafs = [msg for msg in all_messages if not msg.replies]

        if len(leafs) < 1:
            __traceback_info__ = message, leafs
            raise_json_error(self.request,
                             hexc.HTTPBadRequest,
                             {
                                 u'message': _("Expected at least one leaf."),
                                 u'code': 'ExpectedAtLeastOneLeaf',
                             },
                             None)

        if len(leafs) > 1:
            logger.warn('More than one leaf message found.  Bad client?',
                        leafs)

        reply_to = leafs[0]
        reply = self.readCreateUpdateContentObject(self.remoteUser)

        # make sure sender is set to creator (i.e. app can't change that)?
        # anything else?
        reply.From = IPrincipal(self.remoteUser)

        to_principals = set()
        to_principals.update(reply_to.To)
        to_principals.add(reply_to.From)
        to_principals.discard(reply.From)

        reply.To = tuple(to_principals)
        if not reply.Subject:
            reply.Subject = reply_to.Subject

        reply.inReplyTo = reply_to
        reply.addReference(reply_to)
        for ref in reply_to.references or ():
            reply.addReference(ref)

        # distribute to mailbox.
        mailbox = component.getMultiAdapter((self.remoteUser, reply,),
                                            IMailbox)
        mailbox.send(reply)

        # if we can get to a recieved message for us and what we are replying to,
        # update its ReplyDate
        received_msg = component.queryMultiAdapter((self.remoteUser, reply_to),
                                                   IReceivedMessage)
        if received_msg:
            received_msg.mark_replied_to()

        self.request.response.status_int = 201
        return reply


from data_structures.binaryTree import Node

def pathSum(root, target):

    # base case: leaf node & node.data = target -> return [[node.data]]
    if root.left == None and root.right == None and root.data == target:
        return [[root.data]]

    # array to store paths
    paths = []

    # check left and right child with target - root.data
    if root.left != None:
        result = pathSum(root.left, target - root.data)
        # add root.data to front
        for path in result:
            path.insert(0, root.data)
        paths += result

    if root.right != None:
        result = pathSum(root.right, target - root.data)
        for path in result:
            path.insert(0, root.data)
        paths += result

    return paths

tree = Node(1)

print(pathSum(tree, 1)) # [[1]]

tree = Node(1)
tree.right = Node(2)

print(pathSum(tree, 0)) # []

tree = Node(1)
tree.left = Node(2)
tree.right = Node(3)

print(pathSum(tree, 4)) # [[1,3]]

tree = Node(5)
tree.left = Node(4)
tree.left.left = Node(11)
tree.left.left.left = Node(7)
tree.left.left.right = Node(2)
tree.right = Node(8)
tree.right.left = Node(13)
tree.right.right = Node(4)
tree.right.right.left = Node(5)
tree.right.right.right= Node(1)

print(pathSum(tree, 22)) # [[5,4,11,2],[5,8,4,5]]


from flask import Flask, request, jsonify
from TaskModel import Task, task_schema, tasks_schema
from verify import verify
from config import db, app

# Create new task
@app.route('/api/task/add', methods=['POST'])
@verify
def addTask(user):
    try:
        userId = user.userId
        phaseId = request.json['phaseId']
        taskText = request.json['taskText']
        stage = "todo"

        task = Task(userId, phaseId, taskText, stage)
        db.session.add(task)
        db.session.commit()

        return task_schema.jsonify(task)
    except:
        return {"msg": "Something went wrong"}, 500

# Get specific task
@app.route('/api/task/<id>', methods=['GET'])
def getTask(id):
    try:
        task = Task.query.get(id)
        return task_schema.jsonify(task)
    except:
        return {"msg": "Something went wrong"}, 500

# Get all tasks from certain phase
@app.route('/api/tasks/<phaseId>', methods=['GET'])
@verify
def getTasks(user, phaseId):
    try:
        task = Task.query.filter(Task.phaseId == phaseId).all()
        return tasks_schema.jsonify(task)
    except:
        return {"msg": "Something went wrong"}, 500

# Update task
@app.route('/api/task/update/<id>', methods=['PUT'])
@verify
def updateTask(user, id):
    try:
        task = Task.query.get(id)
        userId = user.userId
        phaseId = request.json['phaseId']
        taskText = request.json['taskText']
        stage = request.json['stage']

        task.userId = userId
        task.phaseId = phaseId
        task.taskText = taskText
        task.stage = stage

        db.session.commit()
        return task_schema.jsonify(task)
    except:
        return {"msg": "Something went wrong"}, 500

@app.route('/api/task/<id>/<stage>', methods=['POST'])
def updateStage(id, stage):
    try:
        task = Task.query.get(id)
        task.stage = stage

        db.session.commit()
        return task_schema.jsonify(task)
    except:
        return {"msg": "Something went wrong"}, 500

# Delete tasks
@app.route('/api/task/delete/<id>', methods=['DELETE'])
@verify
def deleteTask(user, id):
    try:
        task = Task.query.get(id)
        db.session.delete(task)
        db.session.commit()

        return task_schema.jsonify(task)
    except:
        return {"msg": "Something went wrong"}, 500


r"""Get volume image data for download.

[smh_js]

This is a sample of "smh js":

    SMH.imgData({
        "bid": 29747,
        "bname": "飞行文学",
        "bpic": "29747.jpg",
        "cid": 398038,
        "cname": "第11回",
        "files": ["001.jpg.webp",
                  "002.jpg.webp",
                  "003.jpg.webp",
                  "097.jpg.webp",
                  "098.jpg.webp"],
        "finished": false,
        "len": 5,
        "path": "/ps2/f/feixingwen_xue/第11回/",
        "status": 1,
        "block_cc": "",
        "nextId": 0,
        "prevId": 397656,
        "sl": {
            "md5": "GTdWjMobp0W0r6Y9tVW5tw"
        }
    }).preInit();

"smh_js" is decoded from "target_js"

[chapter_info]

The "chapter_info" is the object stored in "smh_js".

The important part include:

    path:
        image file path (not include filename) from root the root of hosts.

    files:
        multiple image filenames, order by reading order.

        If a filename ends with `.webp`, which can safely strip `.webp` and
        retriving the original format.

    cid:
        should exist in query string when fetch image files. maybe mean the "chapter id"?

    sl.md5:
        should exist in query string when fetch image files.

[image url format]

A sample image url look like this one:

    https://i.hamreus.com/ps2/f/feixingwen_xue/第11回/001.jpg?cid=398038&md5=GTdWjMobp0W0r6Y9tVW5tw

The structure is:

    https://{image_host_code}.hamreus.com{path}{files[*]}?cid={cid}&md5={sl.md5}

[image_host_code]

Run `servs` in browser's console can find the "image_host_code". Like
following:

    [{"name":"自動",
      "hosts":[{"h":"i","w":100},{"h":"us","w":1}],
      "w":101},
     {"name":"電信",
      "hosts":[{"h":"eu","w":100}, {"h":"i","w":1}, {"h":"us","w":1}],
      "w":102},
     {"name":"聯通",
      "hosts":[{"h":"us","w":100},{"h":"i","w":1},{"h":"eu","w":1}],
      "w":102}]

[target_js]

This is a "target_js", it look like:

    window["\x65\x76\x61\x6c"](function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1;};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p;}('c.h({"i":6,"e":"g","l":"6.2","m":j,"k":"4","d":["8.2.3","7.2.3","b.2.3","9.2.3","a.2.3"],"n":y,"z":5,"w":"/x/f/A/4/","B":1,"r":"","o":0,"p":u,"v":{"s":"t"}}).q();',38,38,'D7BWAcHNgdwUwEbmIGm8CMbB7aiATATgHYAWA4ABjJ3LLXMLoA5qBmYAZQFkAJYAMwEsANnADOwBADsAhgFs4IQHsZgGQjA4aaAzbWD8ZkACJSALlPH8AJsGZ4GZZkwDG0uePD9bwW6b78J/EQAs4ZhJwAB76AJJm4ABOcABuEcDRcGHe+uKCAPa2ANYA+rauMiYArMAA4gAqJgDqoBwZSGTVZFEAbACaePoAatXF+jDmhK3FrcAigokGvokiVLxSgiLywhJ8cPzBXpDwErnBAK7yIob6ByJAA=='['\x73\x70\x6c\x69\x63']('\x7c'),0,{}))    # NOQA

Strip `window[...]` and decrypt this one can find the "smh_js".

"""

import re
import json
from random import choice
from urllib.parse import urlencode

from cmdlr.autil import run_in_nodejs

from .sharedjs import get_shared_js

async def _get_chapter_info(soup, loop):
    """Get single chapter info in volume entry soup."""
    target_js = soup.find('script', string=re.compile(r'window\["')).string
    encrypted_js = re.sub(r'^window\[.+?\]', '', target_js)

    full_js = get_shared_js() + encrypted_js

    smh_js = await loop.run_in_executor(None, lambda: run_in_nodejs(full_js))

    json_string = re.search(r'{.*}', smh_js).group(0)

    return json.loads(json_string)

def _get_normalized_filenames(filenames):
    webp_regex = re.compile(r'\.webp$')

    return [webp_regex.sub('', filename) for filename in filenames]

def _get_image_url(filename, chapter_info, image_host_codes):
    cid = chapter_info['cid']
    md5 = chapter_info['sl']['md5']
    chapter_path = chapter_info['path']

    image_host_code = choice(image_host_codes)

    return (
        'https://{image_host_code}.hamreus.com{chapter_path}{filename}?{qs}'
        .format(image_host_code=image_host_code,
                chapter_path=chapter_path,
                filename=filename,
                qs=urlencode({'cid': cid, 'md5': md5}))
    )

async def get_image_urls(soup, image_host_codes, loop):
    """Get image's urls from soup and configuration."""
    chapter_info = await _get_chapter_info(soup, loop)

    filenames = chapter_info['files']

    norm_filenames = _get_normalized_filenames(filenames)
    image_urls = [
        _get_image_url(filename, chapter_info, image_host_codes)
        for filename in norm_filenames
    ]

    return image_urls


import bisect
import pickle

def interval_intersect(i1, i2):
    if(i1 < i2):
        return i1[1] > i2[0]
    else:
        return i2[1] > i1[0]

class curve_eval:
    def __init__(self, curve):
        self.curve = curve

    def calc(self, time):
        loc = bisect.bisect(self.curve, (time, 0))
        if loc - 1 < 0 or loc >= len(self.curve):
            return 0
        else:
            return (self.curve[loc - 1][1] + (self.curve[loc][1] - self.curve[loc - 1][1])
                    * (time - self.curve[loc - 1][0]) / (self.curve[loc][0] - self.curve[loc - 1][0]))

class req_break:
    def __init__(self, start_end, length, penalty):
        self.start_end = start_end
        self.length = length
        self.penalty = penalty

def evaluate_combination(sections, early_curve, late_curve, interval_curve, required_breaks):
    time_tables = [[], [], [], [], []]
    early_eval = curve_eval(early_curve)
    late_eval = curve_eval(late_curve)
    interval_eval = curve_eval(interval_curve)
    early_cost = 0
    late_cost = 0
    interval_cost = 0
    break_cost = 0
    for section in sections:
        if section.apply_penalty:
            for day in section.days_list:
                bisect.insort(time_tables[day], section.start_end)
    for time_table in time_tables:
        class_start = time_table[0][0] if time_table else 1440
        class_end = time_table[len(time_table) - 1][1] if time_table else 0
        early_cost += early_eval.calc(class_start)
        late_cost += late_eval.calc(class_end)
        breaks_satisfied = [req_break.start_end[0] <= class_start
                            or req_break.start_end[1] >= class_end
                            for req_break in required_breaks]
        for i in range(len(time_table) - 1):
            interval = (time_table[i][1], time_table[i + 1][0])
            interval_len = interval[1] - interval[0]
            is_req_break = False
            for i in range(len(required_breaks)):
                if (interval_intersect(interval, required_breaks[i].start_end)
                        and interval_len >= required_breaks[i].length):
                    breaks_satisfied[i] = True
                    is_req_break = True
                    break
            if not is_req_break:
                interval_cost += interval_eval.calc(interval_len)
        for i in range(len(required_breaks)):
            if not breaks_satisfied[i]:
                break_cost += required_breaks[i].penalty
    return (early_cost, late_cost, interval_cost, break_cost)

# if __name__ == '__main__':
#     with open("one_combination.pickle", "rb") as ifile:
#         sections = pickle.load(ifile)

#     early_c = [(0, 19), (570, 0)]
#     late_c = [(840, 0), (1440, 10)]
#     interval_c = [(10, 0), (1440, 12)]
#     breaks = [req_break((720, 780), 45, 4), req_break((1020, 1110), 45, 8)] # (720, 780, 45, 4), (1020, 1110, 45, 8)

#     print(evaluate_combination(sections, early_c, late_c, interval_c, breaks))

def parse_ELI_penalty(input_sstr):
    try:
        penalty_list = eval('[' + input_sstr + ']')
    except (SyntaxError, NameError):
        return None
    if not isinstance(penalty_list, list):
        return None
    for pair in penalty_list:
        if not isinstance(pair, tuple):
            return None
        if len(pair) != 2:
            return None
        for item in pair:
            if not isinstance(item, int):
                return None
    return penalty_list

def parse_req_breaks(input_sstr):
    try:
        penalty_list = eval('[' + input_sstr + ']')
    except (SyntaxError, NameError):
        return None
    if not isinstance(penalty_list, list):
        return None
    for pair in penalty_list:
        if not isinstance(pair, tuple):
            return None
        if len(pair) != 4:
            return None
        for item in pair:
            if not isinstance(item, int):
                return None
    return [req_break((t[0], t[1]), t[2], t[3]) for t in penalty_list]

if __name__ == '__main__':
    print(parse_req_breaks('(720, 780, 45, 4), (1020, 1110, 45, 8)'))


#!/usr/bin/env python3

import numpy as np
import os
import torch
from derp.controller import Controller
import derp.util

class Clone(Controller):

    def __init__(self, config, car_config, state):
        super(Clone, self).__init__(config, car_config, state)
        self.camera_config = derp.util.find_component_config(car_config, config['thumb']['component'])

        # Show the user what we're working with
        derp.util.print_image_config(self.camera_config)
        derp.util.print_image_config(self.config['thumb'])

        # Prepare camera inputs
        self.bbox = derp.util.get_patch_bbox(self.config['thumb'], self.camera_config)
        self.size = (config['thumb']['width'], config['thumb']['height'])

        # Prepare model
        self.model_dir = derp.util.get_controller_models_path(self.config['name'])
        self.model_path = derp.util.find_matching_file(self.model_dir, 'clone.pt$')
        if self.model_path is not None and os.path.exists(self.model_path):
            self.model = torch.load(self.model_path)
            self.model.eval()
        else:
            print("Clone: Unable to find model path [%s]" % self.model_path)

        # Useful variables for params
        self.prev_steer = 0
        self.prev_speed = 0

        # Data saving
        self.frame_counter = 0

    def prepare_thumb(self):
        frame = self.state[self.config['thumb']['component']]
        patch = derp.util.crop(frame, self.bbox)
        thumb = derp.util.resize(patch, self.size)
        return thumb

    def predict(self):
        status = derp.util.extractList(self.config['status'], self.state)
        self.state['thumb'] = self.prepare_thumb()
        status_batch = derp.util.prepareVectorBatch(status)
        thumb_batch = derp.util.prepareImageBatch(self.state['thumb'])
        status_batch = derp.util.prepareVectorBatch(status)
        if self.model:
            prediction_batch = self.model(thumb_batch, status_batch)
            prediction = derp.util.unbatch(prediction_batch)
            derp.util.unscale(self.config['predict'], prediction)
        else:
            prediction = np.zeros(len(self.config['predict']), dtype=np.float32)
        self.state['prediction'] = prediction

    def plan(self):
        self.predict()
        if self.state['auto']:
            self.state['speed'] = float(self.state['prediction'][0])
            self.state['steer'] = float(self.state['prediction'][1])


import tkinter
from fileReader import *
import sys

#######################################################
# Author: Nicolas Besson                              #
# TL-OR: Project                                      #
#######################################################

def drawPoint(canvas, x, y, color="green"):
    canvas.create_oval(x - 5, y - 5, x + 5, y + 5, fill=color)

def drawDepot(canvas, x, y):
    canvas.create_rectangle(x-5, y-5, x + 5, y + 5, fill="red")

def drawLine(canvas, p1, p2):
    [x1, y1] = p1
    [x2, y2] = p2
    canvas.create_line(x1*w//1280, y1*h//720, x2*w//1280, y2*h//720, fill="purple")

def drawPath(canvas, points, path):
    for i, p in enumerate(path[:-1]):
        drawLine(canvas, points[p], points[path[i + 1]])

def drawCostAllocation(canvas, points, allocation):
    for i, y in enumerate(allocation):
        if points[i][0] <= 1210:
            canvas.create_text(points[i][0]*w//1280 + 35, points[i][1]*h//720 - 10, text=str(round(y, ndigits=2)), fill="blue")
        else:
            canvas.create_text(points[i][0]*w//1280 - 35, points[i][1]*h//720 - 10, text=str(round(y, ndigits=2)), fill="blue")

if __name__ == "__main__":
    top = tkinter.Tk()
    h = 900
    w = 1600
    canvas = tkinter.Canvas(top, height=h, width=w, bg="black")

    nbClients, nbFarmers, capacity, locationCost, costPerKm = readInfoInstanceFile("../data/info_instance.txt")

    coords = readCoordFile("../data/coordinates.txt")
    route = readSolutionFile(sys.argv[2])
    allocation = readAllocationFile(sys.argv[3])
    depots = readDepotsFile(sys.argv[1])

    for d in depots:
        drawDepot(canvas, coords[d][0]*w//1280, coords[d][1]*h//720)
    drawCostAllocation(canvas, coords[1:], allocation)
    color = "green"
    for i, [x, y] in enumerate(coords[1:]):
        if i == nbFarmers:
            color = "yellow"
        drawPoint(canvas, x*w//1280, y*h//720, color)
    drawPath(canvas, coords, route)
    canvas.grid(row=0, column=0)
    top.mainloop()


from django.contrib import admin
from django import forms

from . import models

class CountryAdminForm(forms.ModelForm):

    class Meta:
        model = models.Country
        fields = "__all__"

class CountryAdmin(admin.ModelAdmin):
    form = CountryAdminForm
    list_display = [
        "last_updated",
        "id",
        "name",
        "created",
    ]
    readonly_fields = [
        "last_updated",
        "id",
        "name",
        "created",
    ]

class StateAdminForm(forms.ModelForm):

    class Meta:
        model = models.State
        fields = "__all__"

class StateAdmin(admin.ModelAdmin):
    form = StateAdminForm
    list_display = [
        "created",
        "id",
        "last_updated",
        "name",
    ]
    readonly_fields = [
        "created",
        "id",
        "last_updated",
        "name",
    ]

class CityAdminForm(forms.ModelForm):

    class Meta:
        model = models.City
        fields = "__all__"

class CityAdmin(admin.ModelAdmin):
    form = CityAdminForm
    list_display = [
        "name",
        "id",
        "created",
        "last_updated",
    ]
    readonly_fields = [
        "name",
        "id",
        "created",
        "last_updated",
    ]

class LocalityAdminForm(forms.ModelForm):

    class Meta:
        model = models.Locality
        fields = "__all__"

class LocalityAdmin(admin.ModelAdmin):
    form = LocalityAdminForm
    list_display = [
        "id",
        "created",
        "last_updated",
        "name",
    ]
    readonly_fields = [
        "id",
        "created",
        "last_updated",
        "name",
    ]

admin.site.register(models.Country, CountryAdmin)
admin.site.register(models.State, StateAdmin)
admin.site.register(models.City, CityAdmin)
admin.site.register(models.Locality, LocalityAdmin)


def add_suffix(file: str, suffix: str):
    assert len(file.rsplit(".", 1)) == 2, "file should have an extension!"
    base, extension = file.rsplit(".", 1)
    res_file = base+suffix + "." + extension
    return res_file

if __name__ == "__main__":
    file = r"./xxx.jpg"
    print("original file: {}".format(file))
    file = add_suffix(file, r"_result")
    print("new file: {}".format(file))


import numpy as np

# boosting is a method that use several simple classifiers
# at each step, we give a weight to each point
# this weight is higher if the point has been often classified right
# then, we train the weighted points with a new base classifier, and so on
# we also give a weight to each classifier depending of their accuracy

# finally, to classify a new data, we classify it with each classifier
# the result is the class of highest sum of weighted votes

class BoostClassifier(object):
    def __init__(self, base_classifier, T=10, params = []):
        self.base_classifier = base_classifier
        self.T = T
        self.params = params

    def train(self, X, labels):
        Npts,Ndims = np.shape(X)
        self.classes = np.unique(labels)

        self.classifiers = [] # list of base classifiers
        self.alphas = np.array([]) # vote weights of the classifiers

        # initial weights
        w = np.ones(Npts)/float(Npts)

        for _ in range(0, self.T):
            self.classifiers.append(self.base_classifier(*self.params))
            self.classifiers[-1].train(X, labels, w)
            vote = self.classifiers[-1].classify(X) != labels
            eps = max(w.dot(vote),1e-100)
            self.alphas = np.append(self.alphas, 1/2 * (np.log(1-eps) - np.log(eps)))
            w = np.exp(self.alphas[-1] * ((vote) * 2 - 1))
            w /= np.sum(w)

    def classify(self, X):
        #classify X with each classifiers
        votes = np.array([c.classify(X) for c in self.classifiers])
        #compute the weighted means of votes
        weighted_vote = np.array([self.alphas.dot(votes == i) for i in self.classes])
        #return the most likely classes
        return np.argmax(weighted_vote, axis=0)

if __name__ == '__main__':
    # In this example, we use boosting with decision trees
    from decisionTree import DecisionTree
    from numpy.random import shuffle, normal, random

    # generate dataset
    N=400
    K=10
    dataA = normal(1,1,(N,K))
    dataB = normal(-1,1,(N,K))
    targetA = np.zeros(N)
    targetB = np.ones(N)
    for i in range(N) :
        if random()<0.1 :
            targetA[i], targetB[i] = targetB[i], targetA[i]
    data = np.concatenate((dataA, dataB))
    target = np.concatenate((targetA, targetB))

    # shuffle data set
    indexes = np.arange(2*N)
    shuffle(indexes)
    data = data[indexes]
    target = target[indexes]

    # split data set in training/test subsets
    training_data = data[:N]
    training_target = target[:N]
    test_data = data[N:]
    test_target = target[N:]

    # train and use boosted classifier
    model = BoostClassifier(DecisionTree, T=10)
    model.train(training_data, training_target)
    print("model trained")

    predicted = model.classify(test_data)
    err = 1-np.mean(predicted == test_target)
    print("error: ", err)


# This program is to shutdown the computer at a certain time.

import os  # inport Operating System Libary
import time  # inport Time Libary
import datetime  # inport DateTime Libary
from datetime import datetime
from datetime import timedelta
from tkinter import *  # inport tkinter Libary for windows GUI
import tkinter as tk

# make a class
class ShutdownApp(tk.Tk):

    def __init__(self):  # defines all varibles

        # Creating the window GUI
        tk.Tk.__init__(self)
        self.title("---WARNING!---")  # title of the window
        self.label = tk.Label(self, text="", width=100)
        self.iconbitmap(r'rsz_1rsz_city_seal_Xbl_icon.ico')  # icon for window (must be an ico file)
        self.geometry("300x200")  # size of window
        self.myButton = tk.Button(self, text="Shutdown Now", command=self.ButtonAction, width=15)  # Button on window
        self.myButton.pack()
        self.label.pack()
        self.myButton.place(x=90, y=165)  # button placement
        self.protocol("WM_DELETE_WINDOW", False)  # disable "x" button
        self.resizable(width=False, height=False)  # disable resize of window

        # adding the photo
        self.photo = PhotoImage(file="rsz_1rsz_city-seal.png")  # added a picture
        self.photolabel = Label(self, image=self.photo)
        self.photolabel.place(x=100, y=60)  # Where the picture is placed on the window

        # Funtions
        self.getTime()  # gets time initialize

        self.countdown(self.getTime())  # countdown funtion initialize and takes seconds as argument.
        self.remaining = 0
        self.ButtonAction()  # button action initialize

    # This function will take the current time and subtract another time and returns seconds.
    def getTime(self):
        # 86400 is 24 hours
        # cannot work if over that many seconds so keep it to the same day.
        time_delta = datetime.combine(datetime.now().date() + timedelta(), datetime.strptime("19:00:00","%H:%M:%S").time()) - datetime.now()  # subtracts the current time from the time you want to shutdown in 24-hour format.
        return time_delta.seconds  # returns the seconds left till time

    # countdown function takes seconds as an input and returns the time left in 24-hour format
    def countdown(self, remaining=None):
        if remaining is not None:
            self.remaining = remaining

        if self.remaining <= 0:  # if remaining seconds == 0 it shutdowns
            self.label.configure(text="Shutdown...", font=32)
            os.system("shutdown -t 5 -r -f")  # shutdown operation with additional 5 secs
        else:
            self.label.configure(
                text="DISCLAMER: YOU CAN NOT CLOSE THIS WINDOW.\n" + "\nTime left till Shutdown: %s" % time.strftime(
                    "%H:%M:%S", time.gmtime(self.remaining)))  # formats time in a string and takes seconds as inputs.
            self.remaining = self.remaining - 1  # subracts remaing seconds
            self.after(1000, self.countdown)  # tells how fast to update in milliseconds

    def ButtonAction(self):
        os.system("shutdown -t 0 -r -f")  # shutdown command for button.

if __name__ == "__main__":  # main function
    app = ShutdownApp()  # runs program
    app.mainloop()  # runs tkinter window


#!/usr/bin/python3
"""Python script that takes in a letter and sends a POST request to
http://0.0.0.0:5000/search_user with the letter as a parameter.
"""

from requests import post
from sys import argv

if __name__ == "__main__":
    if len(argv) > 1:
        q = argv[1]
    else:
        q = ""
    try:
        request = post("http://0.0.0.0:5000/search_user",
                       data={'q': q}).json()
        if "id" in request and "name" in request:
            print("[{}] {}".format(request["id"], request["name"]))
        else:
            print("No result")
    except ValueError:
        print("Not a valid JSON")


#!/usr/bin/env python3
import collections
import copy
import operator
import pathlib
import re

with open(pathlib.Path(__file__).parent / "input" / "day_05.txt") as f:
    text_stacks, text_moves = f.read().strip("\n").split("\n\n")

stacks_n = 9
stack_col_index = lambda i: 1 + 4 * (i - 1)
col = lambda i: tuple(row[i] for row in text_stacks.split("\n"))

stacks = tuple(
    map(
        lambda i: collections.deque(filter(str.isalpha, col(i))),
        map(stack_col_index, range(1, stacks_n + 1)),
    )
)

pattern = r"move (\d+) from (\d+) to (\d+)"
moves = tuple(map(lambda x: tuple(map(int, x)), re.findall(pattern, text_moves)))

backup = copy.deepcopy(stacks)
for (count, src, dest) in moves:
    stacks[dest - 1].extendleft(stacks[src - 1].popleft() for _ in range(count))

# First puzzle answer
print("".join(map(operator.itemgetter(0), stacks)))

stacks = backup
for (count, src, dest) in moves:
    stacks[dest - 1].extendleft(
        reversed(tuple(stacks[src - 1].popleft() for _ in range(count)))
    )

# Second puzzle answer
print("".join(map(operator.itemgetter(0), stacks)))


import cv2 as cv
cvNet = cv.dnn.readNetFromTensorflow('/Users/Tinku/Desktop/RoboticsShit/Skystone-Vision/Final/image_tensor/PB_file/model.pb', '/Users/Tinku/Desktop/RoboticsShit/Skystone-Vision/Final/image_tensor/Pbtxt/model.pbtxt')
img = cv.imread('/Users/Tinku/Desktop/RoboticsShit/DataForFuture/StoneandSkystone/IMG_3380.jpeg')

rows = img.shape[0]
cols = img.shape[1]
cvNet.setInput(cv.dnn.blobFromImage(img, size=(300, 300), swapRB=True, crop=False))
cvOut = cvNet.forward()
objects_dict = {}
StoneOrder = []
for detection in cvOut[0,0,:,:]:
    score = float(detection[2])
    if score > 0.8:
        objectClass = detection[1]
        left = detection[3] * cols
        top = detection[4] * rows
        right = detection[5] * cols
        bottom = detection[6] * rows
        objects_dict[score]=[left,top,right,bottom, objectClass]

l=list(objects_dict.keys())
l.sort(reverse=True)
for i in l[:6] :
    #print(i,objects_dict[i])
    if objects_dict[i][4] == 3.0:
        detectedObject = "Skystone"
    elif objects_dict[i][4] == 4.0:
        detectedObject = "Stone"
    elif objects_dict[i][4] == 2.0:
        detectedObject = "Red Foundation"
    elif objects_dict[i][4] == 1.0:
        detectedObject = "Blue Foundation"
    cv.rectangle(img, (int(objects_dict[i][0]), int(objects_dict[i][1])), (int(objects_dict[i][2]), int(objects_dict[i][3])), (23, 230, 210), thickness=5)
    cv.putText(img, detectedObject, (int(objects_dict[i][0]), int(objects_dict[i][1])), cv.FONT_HERSHEY_SIMPLEX, 5, (0,0,0), 10, cv.LINE_AA)
    StoneOrder.append(detectedObject)
    print(objects_dict[i][1])

print(StoneOrder)

SkystonePosition = [i for i, value in enumerate(StoneOrder) if value == "Skystone"]
print('The Skystone is located in positions: ' + str(int(SkystonePosition[0]) + 1) + ' and ' + str(int(SkystonePosition[1]) + 1))
cv.imshow('img', img)
cv.waitKey()


#!/usr/bin/python
#===============================================================================
# This file is part of PyManageMC.
#
#    PyManageMC is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 2 of the License, or
#    (at your option) any later version.
#
#    PyManageMC is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with PyManageMC.  If not, see http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#===============================================================================
'''
Created on Apr 11, 2014

@author: Paulson McIntyre (GpMidi) <paul@gpmidi.net>
'''
# Logging
import logging
log = logging.getLogger('mcdocker.views')

# Built-in
import os, os.path, sys  # @UnusedImport
import urllib

# External
from django.core.exceptions import ObjectDoesNotExist  # @UnusedImport
from django.shortcuts import render_to_response, get_object_or_404, render, redirect  # @UnusedImport
from django.db.models import Q  # @UnusedImport
from django.template import RequestContext  # @UnusedImport
from django.contrib.auth.models import AnonymousUser  # @UnusedImport
from django.views.decorators.cache import cache_page  # @UnusedImport
from django.core.paginator import Paginator, EmptyPage, InvalidPage  # @UnusedImport
from django.contrib.auth.decorators import login_required, permission_required
from django.http import Http404
from couchdbkit.exceptions import ResourceNotFound  # @UnusedImport

# Ours
from minecraft.models import *  # @UnusedWildImport
from mcdocker.models import *  # @UnusedWildImport
from mcdocker.tasks import *  # @UnusedWildImport
from mcdocker.forms import *  # @UnusedWildImport

@login_required
@permission_required('mcdocker.view_dockerimage')
@permission_required('mcdocker.change_dockerimage')
def dockerImageBuild(req, dockerImageId):
    """ Docker Image Change """
    try:
        di = DockerImage.get(dockerImageId)
        job = doBuildImage(di=di)
        return redirect('DockerImageEdit', di._id)
    except ResourceNotFound:
        raise Http404("Couldn't find an image with id %r" % dockerImageId)

@login_required
@permission_required('mcdocker.view_dockerimage')
def dockerImageIndex(req):
    """ Docker Image index """
    return render_to_response(
                              'mcdocker/dockerMgmt/index.djhtml',
                              dict(
                                   allImages=DockerImage.view('mcdocker/allOSImages'),
                                   baseImages=DockerImage.view('mcdocker/baseOSImages'),
                                   userImages=DockerImage.view('mcdocker/userOSImages'),
                                   ),
                              context_instance=RequestContext(req),
                              )

@login_required
@permission_required('mcdocker.view_dockerimage')
@permission_required('mcdocker.change_dockerimage')
def dockerImageEdit(req, dockerImageName):
    """ Docker Image Change """
    try:
        return render_to_response(
                                  'mcdocker/dockerMgmt/edit.djhtml',
                                  dict(
                                       image=DockerImage.get(dockerImageName),
                                       ),
                                  context_instance=RequestContext(req),
                                  )
    except ResourceNotFound:
        raise Http404("Couldn't find an image named %r" % dockerImageName)

@login_required
@permission_required('mcdocker.view_dockerimage')
@permission_required('mcdocker.add_dockerimage')
def dockerImageCreate(req):
    """ Docker Image Add """
    if req.method == 'POST':
        form = NewDockerInstanceForm(req.POST)
        if form.is_valid():
            di = DockerImage(
                             _id=form.cleaned_data['itag'],
                             humanName=form.cleaned_data['humanName'],
                             humanDescription=form.cleaned_data['description'],
                             imageType='UserImage',
                             imageID=None,
                             dockerParent=form.cleaned_data['baseImage'],
                             dockerMemoryLimitMB=form.cleaned_data['dockerMemoryLimitMB'],
                             dockerCPUShare=form.cleaned_data['dockerCPUShare'],
                             dockerName=form.cleaned_data['dockerName'],
                             dockerIndexer=form.cleaned_data['dockerIndexer'],
                             repo=form.cleaned_data['repo'],
                             tag=form.cleaned_data['tag'],
                             user=form.cleaned_data['user'],
                             uid=form.cleaned_data['uid'],
                             gid=form.cleaned_data['gid'],
                             supervisordUser=form.cleaned_data['supervisordUser'],
                             supervisordAutoRestart=form.cleaned_data['supervisordAutoRestart'],
                             supervisordAutoStart=form.cleaned_data['supervisordAutoStart'],
                             supervisordStartTimeSeconds=form.cleaned_data['supervisordStartTimeSeconds'],
                             extraPackages=map(lambda x: x.rstrip(), form.cleaned_data['extraPackages'].splitlines()),
                             sshKeysRoot=map(lambda x: x.rstrip(), form.cleaned_data['sshKeysRoot'].splitlines()),
                             sshKeysMinecraft=map(lambda x: x.rstrip(), form.cleaned_data['sshKeysMinecraft'].splitlines()),
                             firstName=form.cleaned_data['firstName'],
                             lastName=form.cleaned_data['lastName'],
                             email=form.cleaned_data['email'],
                             javaMaxMemMB=form.cleaned_data['javaMaxMemMB'],
                             javaInitMemMB=form.cleaned_data['javaInitMemMB'],
                             javaGCThreads=form.cleaned_data['javaGCThreads'],
                             )
            di.save()
            return redirect('DockerImageEdit', di._id)
    else:
        form = NewDockerInstanceForm()

    return render_to_response(
                              'mcdocker/dockerMgmt/add.djhtml',
                              dict(
                                   form=form,
                                   ),
                              context_instance=RequestContext(req),
                              )

@login_required
@permission_required('mcdocker.view_dockerimage')
@permission_required('mcdocker.add_dockerimage')
@permission_required('mcdocker.add_basedockerimage')
def dockerBaseImageCreate(req):
    """ Docker Image Add """
    if req.method == 'POST':
        form = BaseDockerInstanceForm(req.POST)
        if form.is_valid():
            # TODO: Catch model validation errors and pass to user as something friendly
            di = DockerImage(
                             _id=form.cleaned_data['itag'],
                             humanName=form.cleaned_data['humanName'],
                             humanDescription=form.cleaned_data['description'],
                             imageType='BaseImage',
                             imageID=None,
                             # TODO: Stop hard coding this
                             dockerParent='ubuntu:14.04',
                             dockerName=form.cleaned_data['dockerName'],
                             dockerIndexer=form.cleaned_data['dockerIndexer'],
                             repo=form.cleaned_data['repo'],
                             tag=form.cleaned_data['tag'],
                             proxy=form.cleaned_data['proxy'],
                             extraPackages=map(lambda x: x.rstrip(), form.cleaned_data['extraPackages'].splitlines()),
                             firstName=form.cleaned_data['firstName'],
                             lastName=form.cleaned_data['lastName'],
                             email=form.cleaned_data['email'],
                             )
            di.save()
            return redirect('DockerImageEdit', di._id)
    else:
        form = BaseDockerInstanceForm()

    return render_to_response(
                              'mcdocker/dockerMgmt/add.djhtml',
                              dict(
                                   form=form,
                                   ),
                              context_instance=RequestContext(req),
                              )


# Train mô hình
import tensorflow as tf
from transformer.transformer import Transformer
from transformer.preprocessing.text_processor import TextProcessor

# Load preprocessed data
en_data_path = "data/preprocessed/en_data.txt"
vi_data_path = "data/preprocessed/vi_data.txt"
text_processor = TextProcessor(en_data_path, vi_data_path)

# Create the transformer model
transformer = Transformer(
    num_layers=4,
    d_model=128,
    num_heads=8,
    dff=512,
    input_vocab_size=text_processor.en_vocab_size + 2,
    target_vocab_size=text_processor.vi_vocab_size + 2,
    pe_input=text_processor.en_vocab_size + 2,
    pe_target=text_processor.vi_vocab_size + 2,
)

# Restore the latest checkpoint
checkpoint_path = tf.train.latest_checkpoint("checkpoints")
transformer.load_weights(checkpoint_path)

# Translate input sentence
input_sentence = "Đây là một ví dụ về việc dịch máy sử dụng Transformer."
input_sequence = text_processor.preprocess_vi_text(input_sentence)
output_sequence = transformer.translate(input_sequence)
output_sentence = text_processor.postprocess_en_text(output_sequence)

# Print the translation
print("Input sentence: ", input_sentence)
print("Output sentence: ", output_sentence)