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

""" runtests.py
    Glorified shortcut to `green -vv -q ARGS...`.
    Provides some sane defaults and extra output.
    -Christopher Welborn 03-19-2017
"""

import os
import re
import subprocess
import sys
import unittest
from importlib import import_module

from green import __version__ as green_version
from colr import (
    __version__ as colr_version,
    auto_disable as colr_auto_disable,
    docopt,
    Colr as C,
)

colr_auto_disable()

APPNAME = 'Colr'
APPVERSION = colr_version
NAME = '{} Test Runner'.format(APPNAME)
VERSION = '0.2.0'
VERSIONSTR = '{} v. {}'.format(NAME, VERSION)
SCRIPT = os.path.split(os.path.abspath(sys.argv[0]))[1]
SCRIPTDIR = os.path.abspath(sys.path[0])

USAGESTR = """{versionstr}
    Runs tests using `green` and provides a little more info.

    Usage:
        {script} -h | -v
        {script} (-C | -c)
        {script} [-d] [-s] [-r | -R]
        {script} [-d] [-s] [-r | -R] TESTS...
        {script} (-l | -L) [PATTERN...]

    Options:
        PATTERN              : Regex/text pattern to match against test names.
        TESTS                : Test names for `green`.
        -C,--view-browser    : Shortcut to open the html coverage report
        -c,--view-coverage   : View coverage report in the console.
        -d,--dryrun          : Just show test names.
        -h,--help            : Show this help message.
        -L,--listall         : List all test names with their full name.
        -l,--list            : List all test cases/names.
        -r,--run-coverage    : Run coverage.
        -R,--quiet-coverage  : Run coverage without stdout output.
        -s,--stdout          : Allow stdout (removes -q from green args).
                               in google-chrome.
        -v,--version         : Show version.
""".format(script=SCRIPT, versionstr=VERSIONSTR)

COVERAGE_DIR = os.path.join(SCRIPTDIR, 'coverage_html')

def main(argd):
    """ Main entry point, expects doctopt arg dict as argd. """
    # Use the test directory when no args are given.
    green_exe = get_green_exe()
    if argd['--list'] or argd['--listall']:
        userpats = [
            try_repat(s, default=None)
            for s in argd['PATTERN']
        ]

        return list_tests(full=argd['--listall'], patterns=userpats)
    elif argd['--view-browser']:
        return view_coverage_browser()
    elif argd['--view-coverage']:
        return view_coverage()

    green_args = parse_test_names(argd['TESTS']) or ['test']
    if argd['--dryrun']:
        return print_test_names(green_args)
    cmd = [green_exe, '-vv']
    if not argd['--stdout']:
        cmd.append('-q')
    if argd['--run-coverage'] or argd['--quiet-coverage']:
        cmd.append('-R')

    cmd.extend(green_args)
    print_header(cmd)

    exitcode = subprocess.run(cmd).returncode
    if exitcode:
        return exitcode
    # Success.
    if argd['--run-coverage'] or argd['--quiet-coverage']:
        # Create coverage report.
        return run_coverage(quiet=argd['--quiet-coverage'])

    return exitcode

def filter_test_info(patterns, test_info):
    """ Filter info returned from `load_test_info` using a list of compiled
        regex patterns. Only tests that match test method names, case names,
        or module names are returned in the same format as `load_test_info()`.

        If `patterns` is Falsey, the `test_info` is returned immediately.
    """
    if not patterns:
        return test_info
    filtered = {}
    for modulename, cases in test_info.items():
        keep_module = pats_search(patterns, modulename)
        if keep_module:
            filtered.setdefault(modulename, {})
        casenames = {type(c).__name__: c for c in cases}
        for casename in sorted(casenames):
            case = casenames[casename]
            keep_case = pats_search(patterns, casename)
            if keep_case:
                filtered.setdefault(modulename, {})
                filtered[modulename].setdefault(case, [])
            keepmethods = [
                methodname
                for methodname in sorted(cases[case])
                if pats_search(patterns, methodname)
            ]
            if keepmethods:
                # Add matching test methods, even if the module/case name
                # did not match.
                filtered.setdefault(modulename, {case: None})
                filtered[modulename][case] = keepmethods

    return filtered

def get_green_exe():
    """ Get the green executable for this Python version. """
    paths = set(
        s for s in os.environ.get('PATH', '').split(':')
        if s and os.path.isdir(s)
    )
    pyver = '{v.major}.{v.minor}'.format(v=sys.version_info)
    greenmajorexe = 'green{}'.format(sys.version_info.major)
    greenexe = 'green{}'.format(pyver)
    for trypath in paths:
        greenpath = os.path.join(trypath, greenexe)
        greenmajorpath = os.path.join(trypath, greenmajorexe)
        if os.path.exists(greenpath):
            return greenpath
        elif os.path.exists(greenmajorpath):
            return greenmajorpath
    raise MissingDependency('cannot find an executable for `green`.')

def get_test_cases(modulename, package='test'):
    """ Load all TestCase classes by module name. """
    modl = get_test_module(modulename, package=package)
    cases = []
    for attr in dir(modl):
        try:
            val = getattr(modl, attr, None)
        except AttributeError:
            # This can happen in weird cases.
            continue
        if type(val).__name__ != 'type':
            continue
        if issubclass(val, unittest.TestCase):
            cases.append(val())
    return cases

def get_test_files(package='test'):
    """ Load all test_XX.py module names from the test dir. """
    try:
        files = [s for s in os.listdir(package) if s.startswith('test_')]
    except EnvironmentError as ex:
        raise EnvironmentError(
            'Unable to list "test" dir: {}\n{}'.format(os.getcwd(), ex)
        )
    return [os.path.splitext(s)[0] for s in files]

def get_test_methods(testcase):
    """ Retrieve a list of test method names from a TestCase instance. """
    return [s for s in dir(testcase) if s.startswith('test_')]

def get_test_module(modulename, package='test'):
    """ Load a module object by name. """
    # thispath = sys.path.pop(0)
    cwd = os.getcwd()
    testpath = os.path.join(cwd, package)
    if not os.path.isdir(testpath):
        raise EnvironmentError('Test package not found: {}'.format(testpath))
    if testpath not in sys.path:
        sys.path.insert(0, testpath)
    if cwd not in sys.path:
        sys.path.insert(0, cwd)
    root = os.path.split(cwd)[-1]
    try:
        import_module(package, package=root)
    except ImportError as ex:
        raise ImportError('Cannot import test module: {}'.format(ex))
    try:
        modl = import_module('{}.{}'.format(package, modulename))
    except ImportError as ex:
        raise ImportError('Cannot import module: {}'.format(ex))

    return modl

def get_test_names(package='test'):
    """ Get a flat list of all test modules/cases/names, with their full path.
    """
    yield package
    for modulename, cases in load_test_info(package=package).items():
        yield '.'.join((package, modulename))
        casenames = {type(c).__name__: c for c in cases}
        for casename in sorted(casenames):
            yield '.'.join((package, modulename, casename))
            case = cases[casenames[casename]]
            for methodname in sorted(case):
                yield '.'.join((package, modulename, casename, methodname))

def list_tests(package='test', full=False, patterns=None):
    """ List all discoverable tests. """
    test_info = filter_test_info(
        patterns,
        load_test_info(package=package),
    )
    for modulename, cases in test_info.items():
        modulefmt = C(modulename, 'blue', style='bright')
        casenames = {type(c).__name__: c for c in cases}
        if not full:
            print(modulefmt(':'))
        for casename in sorted(casenames):
            methodnames = cases[casenames[casename]]
            casefmt = C(casename, 'cyan')
            if not full:
                print('  {}'.format(casefmt))
            for methodname in sorted(methodnames):
                methodfmt = C(methodname, 'green')
                if full:
                    print(C('.').join(modulefmt, casefmt, methodfmt))
                else:
                    print('    {}'.format(methodfmt))
            if full and (not methodnames):
                # Methods were filtered out.
                print(C('.').join(modulefmt, casefmt))
        if full and (not casenames):
            # Methods and cases were filtered out.
            print(modulefmt)

    return 0

def load_test_info(package='test'):
    """ Return a dict of {file: {testcase: [test_names...]}} """
    if not os.path.isdir(package):
        print_err('Cannot find test package (\'{}\') dir in: {}'.format(
            package,
            os.getcwd(),
        ))
        return {}
    testinfo = {}
    for modulename in get_test_files(package=package):
        testinfo[modulename] = {}
        for case in get_test_cases(modulename):
            testmethods = get_test_methods(case)
            if not testmethods:
                continue
            testinfo[modulename][case] = testmethods

    return testinfo

def parse_test_names(names):
    """ Prepend 'test.' to test names without it.
        Return a list of test names.
    """
    fixed = set()
    for testname in TESTNAMES:

        for i, name in enumerate(names):
            if not name:
                # Already done.
                continue
            if (name == testname) or (testname.endswith(name)):
                fixed.add(testname)
                names[i] = ''
    return sorted(fixed)

def pats_search(patterns, s):
    """ Returns a list of pattern matches against `s` for all regex patterns
        in the `patterns` list.
    """
    matches = []
    for p in patterns:
        match = p.search(s)
        if match is not None:
            matches.append(match)
    return matches

def print_err(*args, **kwargs):
    """ A wrapper for print() that uses stderr by default.
        Colorizes messages, unless a Colr itself is passed in.
    """
    if kwargs.get('file', None) is None:
        kwargs['file'] = sys.stderr

    # Use color if the file is a tty.
    if kwargs['file'].isatty():
        # Keep any Colr args passed, convert strs into Colrs.
        msg = kwargs.get('sep', ' ').join(
            str(a) if isinstance(a, C) else str(C(a, 'red'))
            for a in args
        )
    else:
        # The file is not a tty anyway, no escape codes.
        msg = kwargs.get('sep', ' ').join(
            str(a.stripped() if isinstance(a, C) else a)
            for a in args
        )

    print(msg, **kwargs)

def print_header(cmd):
    """ Print some info about the Colr and Green versions being used. """
    textcolors = {'fore': 'cyan'}
    libcolors = {'fore': 'blue', 'style': 'bright'}
    vercolors = {'fore': 'blue'}
    execolors = {'fore': 'green', 'style': 'bright'}
    argcolors = {'fore': 'green'}

    def fmt_app_info(name, ver):
        """ Colorize a library and version number. """
        return C(' v. ', **textcolors).join(
            C(name, **libcolors),
            C(ver, **vercolors)
        )

    def fmt_cmd_args(cmdargs):
        """ Colorize a command and argument list. """
        return C(' ').join(
            C(cmdargs[0], **execolors),
            C(' ').join(C(s, **argcolors) for s in cmdargs[1:]),
        ).join('(', ')', style='bright')

    print('{}\n'.format(
        C(' ').join(
            C('Testing', **textcolors),
            fmt_app_info(APPNAME, APPVERSION),
            C('using', **textcolors),
            fmt_app_info('Green', green_version),
            fmt_cmd_args(cmd),
        )
    ))
    print(
        C(': ').join(
            C('Running from', 'cyan'),
            C(os.getcwd(), 'blue', style='bright'),
        ),
    )

def print_test_names(names):
    """ Print formatted test names. """
    print(C(':').join(
        C('Parsed test names', 'cyan'),
        C(len(names), 'blue', style='bright'),
    ))
    for name in names:
        print(C(name, 'blue'))

    return 0 if names else 1

def run_coverage(quiet=False):
    """ Run coverage, and return an exit status code. """
    print(C(': ').join(
        C('Creating coverage report in', 'cyan'),
        C(COVERAGE_DIR, 'blue', style='bright'),
    ))
    covcmd = [
        'coverage',
        'html',
        '--directory',
        COVERAGE_DIR,
        '--title',
        'Coverage for Colr v. {}'.format(colr_version),
    ]
    exitcode = subprocess.run(covcmd).returncode
    if exitcode:
        return exitcode
    if quiet:
        return exitcode
    return view_coverage()

def try_repat(s, default=None):
    """ Try compiling a regex pattern.
        If `s` is Falsey, `default` is returned.
        On errors, InvalidArg is raised.
        On success, a compiled regex pattern is returned.
    """
    if not s:
        return default
    try:
        p = re.compile(s, flags=re.IGNORECASE)
    except re.error as ex:
        raise InvalidArg('Invalid pattern: {}\n{}'.format(s, ex))
    return p

def view_coverage():
    """ Print the coverage report to the console. """
    coverage_file = os.path.join(SCRIPTDIR, '.coverage')
    if not os.path.exists(coverage_file):
        raise InvalidArg('No coverage file found, run coverage: {}'.format(
            coverage_file,
        ))
    # Show console report.
    print(C('').join(C('\nCoverage Report', 'cyan'), ':'))

    covreportcmd = [
        'coverage',
        'report',
    ]
    try:
        output = subprocess.check_output(
            covreportcmd,
            stderr=subprocess.STDOUT,
        ).decode()
    except subprocess.CalledProcessError:
        return 1
    if not output.startswith('Name'):
        print_err(output)
        return 1

    divline = C('-' * 45, 'dimgrey')
    for line in output.splitlines():
        if line.startswith('--'):
            # Divider line.
            print(divline)
            continue
        name, statements, miss, cover = line.split()
        namefmt = C(name).ljust(25)
        statementsfmt = C(statements, (46, 137, 255)).rjust(5)
        try:
            miss = int(miss)
            if miss == 0:
                missfmt = C(miss, 'lightgreen', style='bright').rjust(6)
            else:
                missfmt = C(miss, 'red').rjust(6)
        except ValueError:
            # Actual 'Miss' header.
            missfmt = C(miss, 'red').rjust(6)
        try:
            cover = int(cover.rstrip('%'))
            if cover == 100:
                covercolr = {'fore': 'lightgreen', 'style': 'bright'}
            elif cover > 49:
                covercolr = {'fore': 'green'}
            else:
                covercolr = {'fore': 'red'}
            coverfmt = C('{}%'.format(cover), **covercolr).rjust(6)
        except ValueError:
            # Actual 'Cover' header.
            coverfmt = C(cover, 'green').rjust(6)
        print(C(' ').join(namefmt, statementsfmt, missfmt, coverfmt))
    return 0

def view_coverage_browser():
    """ Open the html coverage report in google-chrome. """
    indexfile = os.path.join(COVERAGE_DIR, 'index.html')
    if not os.path.exists(indexfile):
        print_err(C(': ').join(
            C('Missing coverage report file', 'red'),
            C(indexfile, 'blue'),
        ))
        print_err(C(' ').join(
            C('Run', 'red'),
            C('./runtests.py -R', 'blue').join('`', '`'),
            C('to generate it.', 'red'),
        ))
        return 1

    cmd = ['google-chrome', indexfile]
    print(C(': ').join(
        C('Running', 'cyan'),
        C(' ').join(
            C(cmd[0], 'blue', style='bright'),
            C(' '.join(cmd[1:]), 'blue'),
        ),
    ))
    subprocess.Popen(cmd, stderr=subprocess.DEVNULL)
    return 0

class InvalidArg(ValueError):
    """ Raised when the user has used an invalid argument. """
    def __init__(self, msg=None):
        self.msg = msg or ''

    def __str__(self):
        if self.msg:
            return 'Invalid argument, {}'.format(self.msg)
        return 'Invalid argument!'

class MissingDependency(EnvironmentError):
    def __init__(self, msg=None):
        self.msg = msg or ''

    def __str__(self):
        if self.msg:
            return 'Missing dependency, {}'.format(self.msg)
        return 'Missing a dependency!'

TESTNAMES = reversed(list(get_test_names()))

if __name__ == '__main__':
    try:
        mainret = main(docopt(USAGESTR, version=VERSIONSTR, script=SCRIPT))
    except (InvalidArg, MissingDependency) as ex:
        print_err(ex)
        mainret = 1
    except (EOFError, KeyboardInterrupt):
        print_err('\nUser cancelled.\n')
        mainret = 2
    except BrokenPipeError:
        print_err('\nBroken pipe, input/output was interrupted.\n')
        mainret = 3
    sys.exit(mainret)


# def first_last6(nums):
#
#        if len(nums)>=1 and nums[0] == nums[-1]:
#          print("True")
#        else:
#            print("False")
#
#
# #
# first_last6([1, 2, 6])

# def sum3(nums):
#
#     print(nums[::-1])
#
# sum3([1,2,3])

# Given an array of ints, return the sum of the first 2 elements in the array.
# If the array length is less than 2, just sum up the elements that exist, returning 0 if the array is length 0.

def sum2(nums):

  if len(nums)<=2:
      print(sum(nums))
  elif len(nums) == 0:
      print(0)

sum2([1,2,3])
sum2([1, 1])
sum2([1, 1, 1, 1])


#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
    脚本名: 【工商银行】智能客服bot
Created on 2019-03-18
@author:David Yisun
@group:data
"""

import requests
import codecs
from bs4 import BeautifulSoup
import re
import time

class Spider(object):
    '''
        工商银行爬虫爬取器
    '''
    url = 'http://www.icbc.com.cn/ICBCDynamicSite/Robot/RobotResult.aspx?title={title}'
    def __init__(self, name):
        self.name = name

    def _request(self, url, para={}, proxy={}):
        print('[request url] {0}'.format(url))
        res = requests.get(url=url, params=para, proxies=proxy)
        res.encoding = res.apparent_encoding
        print('[response url] {0}'.format(res.url))
        return res.text

    def _get_query(self, file_path):
        with codecs.open(file_path, 'r', 'utf-8') as f:
            data = f.read()
        query = [d.strip() for d in data.splitlines()]
        return query

    def _parse(self, text):
        soup = BeautifulSoup(text, 'lxml')
        query, answer, no_answer = None, None, None
        if soup.select('#lblMessage'):
            _text = soup.select('#lblMessage')[0].get_text()
            print('没有答案: {0}'.format(_text))
            no_answer = _text
            return query, answer, no_answer
        if soup.select('''#form1 a[onclick*="showLay('Layer0'"]'''):
            query = soup.select('''#form1 a[onclick*="showLay('Layer0'"]''')[0].get_text()
            query = re.sub(r' |\n|\r| ', '', query)
            print('标准问: {0}'.format(query))
        if soup.select('#Layer0 pre'):
            answer = soup.select('#Layer0 pre')[0].get_text()
            answer = re.sub(r' |\n|\r| |答：', '', answer)
            print('答案: {0}'.format(answer))
        return query, answer, no_answer

    def get_from_file(self, file_path, save_path, fail_path, sleep=10):
        query = self._get_query(file_path=file_path)
        res = []
        no_answer = []
        for _query in query:
            print('='*60)
            url = self.url.format(title=_query)
            print('原问题: {0}'.format(_query))
            text = self._request(url=url)
            question, answer, _no_answer = self._parse(text=text)
            if question and answer:
               _res = [_query, question, answer]
               res.append('--->'.join(_res))
            elif _no_answer:
                no_answer.append('--->'.join([_query, _no_answer]))

            # 写入答案
            if (len(res)+1) % 50 == 0:
                with codecs.open(save_path, 'a', 'utf-8') as f:
                    f.write('\n'.join(res))
                    res = []
            # 记录失败请求
            if (len(no_answer)+1) % 50 == 0:
                with codecs.open(fail_path, 'a', 'utf-8') as f:
                    f.write('\n'.join(no_answer))
                    no_answer = []
            time.sleep(sleep)

        if res:
            with codecs.open(save_path, 'a', 'utf-8') as f:
                f.write('\n'.join(res))
        if no_answer:
            with codecs.open(fail_path, 'a', 'utf-8') as f:
                f.write('\n'.join(no_answer))

def main(save_path, file_path, fail_path):
    spider = Spider(name='ICBC')
    spider.get_from_file(file_path=file_path, save_path=save_path, fail_path=fail_path)

if __name__ == '__main__':
    file_path = './query.txt'  # query 文件
    save_path = './ICBC.txt'  # [原问题 标准问 答案]
    fail_path = './fail.txt'  # 请求失败文件
    main(save_path=save_path, file_path=file_path, fail_path=fail_path)


#Below are all the modules for importing
import tkinter as tk #tkinter used for visualization
from tkinter import *
from tkinter import ttk
from PIL import Image, ImageDraw, ImageFont
#import Page2

width=800 #width of display in pixeld
height=600 #height of display in pixels
center=400 #center of page - half of width in pixels

#button dimensions
bw=10 #button width in pixels
bh=1 #button height pixels

class HomePage_GUI(): #class for multiple functions
    def __init__(self,master): #main function for GUI
        self.master=master
        self.master.configure(bg='#ffffff') #HEX code for background colour
        self.master.option_add('*Font','Georgin 18') #Button font style
        self.master.option_add('Background','#ffffff') #GUI Background Colour
        self.master.geometry('800x600') #resolution of GUI window

        #AISHA Logo
        self.AISHALogo = ttk.Frame(master)
        self.AISHALogo.pack()
        self.logo=PhotoImage(file='AISHALogo.png')
        ttk.Label(self.AISHALogo, image = self.logo).grid(row=1,column=1,rowspan=1, padx=0)

        #Home Button
        ButtonHome = tk.Button(text="Home",width=bw,height=bh,bg='#869EE2')
        ButtonHome.place(x=center, y=250)
        #ButtonHome.bind('<Button>',self.home_btn)

        #Location
        ButtonLocation = tk.Button(text="Location",width=bw,height=bh,bg='#869EE2')
        ButtonLocation.place(x=center,y=300)
        #ButtonLocation.bind('<Button>',self.Location_btn)

        #Help
        ButtonHelp = tk.Button(text="Help",width=bw,height=bh,bg='#869EE2')
        ButtonHelp.place(x=center,y=350)
        #ButtonHelp.bind('<Button>',self.Help_btn)

        #Exit
        quitButton = tk.Button(text="Exit",width=bw,height=bh,bg='#869EE2')
        quitButton.place(x=center,y=400)

        #ButtonExit.bind('<Button>',self.Exit_btn)

        def client_exit(self):
            exit()

if __name__ == "__main__":
     root=tk.Tk()
     tutor_app=HomePage_GUI(root) #runs every function under class
     root.mainloop()

Python Version1.0 7.12.18/Python Version 1.0 7-12-18/HomePage.py


import os
import numpy as np
import pandas as pd
import scipy.stats as stat
import matplotlib.pyplot as plt

import quad_solve
import progressbar
import importlib
import input_vars

def main(post_prod_waste, animal_feed, dist_losses, proc_losses, start, end):

    try:
        hist_supp = np.genfromtxt("input\\data\\food_supp_1970_2010")
    except OSError:
        print("Failed to open {}\\\"input\\data\\food_supp_1970_2010\"".format(os.getcwd()))
    years_2010 = np.arange(1970, 2011, 1)

    # Post production waste reductions
    init_waste = input_vars.waste_reduction["init_waste"]
    waste_reduction = input_vars.waste_reduction[str(post_prod_waste)]

    if animal_feed == "high_efficiency":
        waste_feed1 = input_vars.waste_feed()[2]
        waste_feed2 = input_vars.waste_feed()[3]

    if animal_feed == "low_efficiency":
        waste_feed1 = input_vars.waste_feed()[0]
        waste_feed2 = input_vars.waste_feed()[1]

    if dist_losses == "same_2010":
        dist_loss = input_vars.dist_loss()[1]
    if dist_losses == "high_efficiency":
        dist_loss = input_vars.dist_loss()[0]

    processing_losses_init = input_vars.processing_losses["2010"]
    processing_losses_end = input_vars.processing_losses[str(proc_losses)]

    food_struct = {"year":[0.0]*50,
                   "food_supply":[0.0]*50,
                   "actual_consumption":[0.0]*50,
                   "food_waste_perc":[init_waste]*50,
                   "waste_to_feed":[waste_feed1]*50,
                   "distribution_losses":[0.1]*50,
                   "processing_losses":[processing_losses_init]*50,
                   "food_used":[0.0]*50,
                   }

    food_supply = pd.DataFrame(data = food_struct)

    # project food supply to 2050
    line = stat.linregress(years_2010, hist_supp)
    # waste amounts coeff
    w_coeff = quad_solve.solve( start + 15, end,
                                init_waste,
                                init_waste*(1 - waste_reduction)
                                )

    # processing_losses coeffs
    p_coeff = quad_solve.solve(     start + 15, end,
                                    processing_losses_init,
                                    processing_losses_end
                                    )
    # Consumption is 70%?
    percent = 0.7
    years = []

    for i in range(start - start, 1 + end - start):
        year = start + i
        food_supply.loc[i, "year"] = year
        food_supply.loc[i, "food_supply"] = ((year) * line[0] + line[1])

        food_supply.loc[i, "actual_consumption"] =  food_supply.loc[i,
                                                    "food_supply"] * percent

        if year >= start + 14:
            food_supply.loc[i, "food_waste_perc"] = w_coeff[0] * year ** 2 +\
                                                    w_coeff[1] * year +\
                                                    w_coeff[2]

            food_supply.loc[i, "processing_losses"] = p_coeff[0] * year ** 2 +\
                                                      p_coeff[1] * year +\
                                                      p_coeff[2]

            food_supply.loc[i, "waste_to_feed"] = waste_feed1 + (((waste_feed2-\
                                                  waste_feed1)/36)*(year - (start + 14)))

            food_supply.loc[i, "distribution_losses"] = 0.1 + (((dist_loss-\
                                                  0.1)/36)*(year - (start + 14)))

        food_supply.loc[i, "food_used"] = food_supply.loc[i, "actual_consumption"]/\
                                          (1 - food_supply.loc[i, "food_waste_perc"])

        progressbar.bar(i*2, label = "Food and waste", length = 70 )
    #print(food_supply)
    cwd = os.getcwd()
    out_str = "{}\\output\\food_and_waste_out.csv".format(cwd)
    try:
        food_supply.to_csv(out_str, index = False)
    except OSError:
        print("Error: failed to write to {}\\{}: permission denied. (Try closing EXCEL?)".format(cwd, out_str))


from __future__ import print_function

from pyspark.sql import SparkSession, Row
from pyspark.sql.functions import *
from pyspark.sql.types import *

spark = SparkSession.builder \
    .appName("shortest-distance") \
    .config("spark.sql.shuffle.partitions", 8) \
    .getOrCreate()
spark.sparkContext.setLogLevel("WARN")

vertexFrom = 12
vertexTo = 34
nPartitions = int(spark.conf.get("spark.sql.shuffle.partitions"))

edges = spark.read \
    .text("twitter_sample_small.txt") \
    .repartition(nPartitions) \
    .withColumn("split", split(col("value"), "\s+")) \
    .withColumn("source", col("split").getItem(0).cast(LongType())) \
    .withColumn("target", col("split").getItem(1).cast(LongType())) \
    .select("source", "target") \
    .cache()

current_distance = 0
distances = spark.createDataFrame([Row(vertexFrom, current_distance)], ["vertex", "distance"]) \
    .repartition(nPartitions)
while True:
    candidate_distances = edges \
        .join(distances, on=expr("target = vertex"), how="inner") \
        .withColumn("distance", expr("distance + 1")) \
        .select(col("source").alias("vertex"), col("distance").alias("new_distance"))
    minDistance = when(col("distance").isNull(), col("new_distance")) \
        .otherwise(when(col("new_distance").isNull(), col("distance"))
                   .otherwise(least("distance", "new_distance")))
    distances = distances \
        .join(candidate_distances, on="vertex", how="full_outer") \
        .withColumn("distance", minDistance) \
        .select("vertex", "distance") \
        .cache()

    hasVertexTo = distances \
        .select("vertex") \
        .where(expr("vertex = " + str(vertexTo)))
    if hasVertexTo.count() > 0:
        break

    current_distance += 1

distanceTo = distances \
    .where(expr("vertex = " + str(vertexTo))) \
    .select("distance") \
    .head()[0]
print(distanceTo)


def main():
	string = input("Enter a genome string: ")

	string = string.upper()
	lengthOfGene = 0
	remString = string
	tripletStarting = string.find('ATG')
	tripletTAG = string.find('TAG')
	tripletTAA = string.find('TAA')
	tripletTGA = string.find('TGA')

	arrayOfEndingTriplets = {tripletTAG, tripletTAA, tripletTGA} - {-1}
	firstGeneEnding = min(arrayOfEndingTriplets)
	m = len(arrayOfEndingTriplets)
	if m == 0 or tripletStarting == -1:
		print("No gene found!")
		exit()

	while m != 0:

		if tripletStarting >= 0:
			gene = string[tripletStarting + 3 : firstGeneEnding]
			lengthOfGene = len(gene)
			if lengthOfGene % 3 == 0:
				if 'ATG' not in gene and 'TAG' not in gene and 'TAA' not in gene and 'TGA' not in gene:
					print(gene)
				remString = string[tripletStarting + 3 + lengthOfGene + 3: ]
				print(remString, "REMAINING")

		else:
			exit()

		tripletStarting = remString.find('ATG')
		tripletTAG = remString.find('TAG')
		tripletTAA = remString.find('TAA')
		tripletTGA = remString.find('TGA')

		#print(tripletStarting, "TSTARTING")

		#print(tripletTAG, tripletTAA, tripletTGA, "ENDING TRIPLETS")

		arrayOfEndingTriplets = {tripletTAG, tripletTAA, tripletTGA} - {-1}
		#print(arrayOfEndingTriplets, "AOF ENDING TRIPLETS")
		m = len(arrayOfEndingTriplets)
		#print (m , "M")
		firstGeneEnding = min(arrayOfEndingTriplets)
		#print(firstGeneEnding, "FRIEST GENE ENDING")

main()


import backoff

from grocery_mart_api.extensions import redis_conn
from rq import Connection, get_failed_queue, Queue
from rq.job import Job
from grocery_mart_api.models import User
import traceback

def get_failed_jobs_by_index(field_to_index_by="seller_id"):
    failed_job_data_by_index = {}
    with Connection(redis_conn):
        fq = get_failed_queue()
        for job in fq.jobs:
            print(job)
            try:
                for arg in job.args:
                    if type(arg) != User:
                        continue

                    data = {
                        "exc_info": job.exc_info,
                        "started_at": job.started_at.isoformat(),
                        "ended_at": job.ended_at.isoformat(),
                        "func_name": job.func_name
                    }

                    if field_to_index_by == "seller_id":
                        index = arg.seller_id
                    elif field_to_index_by == "username":
                        index = arg.username
                    else:
                        raise Exception("Invalid field_to_index_by value: {}".format(field_to_index_by))

                    if index not in failed_job_data_by_index:
                        failed_job_data_by_index[index] = [data]
                    else:
                        failed_job_data_by_index[index].append(data)
            except Exception as e:
                print(
                    "While getting failed jobs by index, we failed to iterate over rq job args: {}".format(
                        traceback.format_exc()
                    )
                )

    return failed_job_data_by_index

@backoff.on_exception(backoff.fibo, Exception, max_tries=3)
def fetch_job(queue, job_id):
    return queue.fetch_job(job_id)


#-------------------------------------------------------------------------------
# Name:        sum_elem_row
# Purpose:     Homework - Lesson 3_Algorythms
#
# Author:      Yuri Nesterovich
#
# Created:     31.03.2019
# Copyright:   (c) Fujitsu 2019
# Licence:     <your licence>
#-------------------------------------------------------------------------------

# Заполняем матрицу с клавиатуры и считаем сумму элементов строки.
# Выводим новую матрицу

row = 5
col = 4
a = []
for i in range(col):
    b = []
    s = 0
    print("{:d}-я строка:".format(i))
    for j in range(row-1):
        n = int(input())
        s += n
        b.append(n)
    print(b)
    b.append(s)
    a.append(b)
print()
print('Матрица с суммой элементов строки в последнем столбце')
for i in a:
    print(i)


# uncompyle6 version 3.7.4
# Python bytecode 3.7 (3394)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: build/bdist.macosx-10.9-x86_64/egg/tfnz/cli/tfacctbak.py
# Compiled at: 2018-05-18 20:34:30
# Size of source mod 2**32: 1381 bytes
import sys, os.path
from messidge import default_location, KeyPair
bash_template = "echo 'mkdir -p ~/.20ft\ncat > ~/.20ft/%s << EOF\n%s\nEOF\ncat > ~/.20ft/%s.pub << EOF\n%s\nEOF\ncat > ~/.20ft/%s.spub << EOF\n%s\nEOF\ncat > ~/.20ft/default_location << EOF\n%s\nEOF\n\nchmod 400 ~/.20ft/%s*' | /bin/sh\n"

def main():
    loc = default_location(prefix='~/.20ft') if len(sys.argv) == 1 else sys.argv[1]
    kp = KeyPair(loc, prefix='~/.20ft')
    with open(os.path.expanduser('~/.20ft/%s.spub' % loc)) as (f):
        spub = f.read()
    print(bash_template % (loc, kp.secret.decode(), loc, kp.public.decode(), loc, spub, loc, loc))

if __name__ == '__main__':
    main()


# implementation of card game - Memory

import simplegui
import random

cardList = range(0, 8) + range(0, 8)
exposed = []
turns = 0
state = 0
click1 = click2 = 0

# helper function to initialize globals
def new_game():
    global cardList, exposed, turns, state, click1, click2
    random.shuffle(cardList)
    exposed = [0] * 16
    turns = 0
    state = 0
    click1 = click2 = 0

# define event handlers
def mouseclick(pos):
    # add game state logic here
    global cardList, exposed, state, turns, click1, click2
    clickInd = pos[0] / 50
    if exposed[clickInd] == 0:
        exposed[clickInd] = 1
        if state == 0:
            click1 = clickInd
            state = 1
        elif state == 1:
            click2 = clickInd
            state = 2
        else:
            if cardList[click1] != cardList[click2]:
                exposed[click1] = exposed[click2] = 0
            turns += 1
            click1 = clickInd
            state = 1

# cards are logically 50x100 pixels in size
def draw(canvas):
    label.set_text("Turns = " + str(turns))
    for i in range(len(cardList)):
        if exposed[i] == 1:
            canvas.draw_text(str(cardList[i]),
                             [i * 50 + 10, 75], 60, "White")
        else:
            canvas.draw_polygon([[i * 50, 0], [i * 50, 100],
                                 [(i + 1) * 50, 100], [(i + 1) * 50, 0]],
                                4, "Black", "Green")

# create frame and add a button and labels
frame = simplegui.create_frame("Memory", 800, 100)
frame.add_button("Reset", new_game)
label = frame.add_label("Turns = 0")

# register event handlers
frame.set_mouseclick_handler(mouseclick)
frame.set_draw_handler(draw)

# get things rolling
new_game()
frame.start()

# Always remember to review the grading rubric


import sys
import os

# usage : python fileappend.py  "C:\\Temp\\test.log"
filehost = sys.argv[1]
print(filehost)
print(os.path.exists(filehost))
print(os.path.split(filehost))
fd, fr= os.path.split(filehost)
print("Directory contents: [ "+ fd + " ]\n" )
print(os.listdir(fd)) # list content of  directory

if os.path.exists(filehost):
	try:
		f= open(filehost, 'a+')
		f.write("# This is a test line appended.")

		#lines=f.readlines()
		#for ln in lines:
		#	print(ln)

		f.close()
	except:
		print("IOException occurred.")

"""
if os.path.isfile(filehost):
	try:
		with open(filehost) as file:
			print('File could be opened...')
		except IOError as e:
			print("Unable to open the file.")
"""


#!/usr/bin/env python3
import os
import praw
from praw.handlers import MultiprocessHandler
import pprint

import config
from gracefulcharity.bot import RedditBot
from gracefulcharity.requesthandler import GracefulCharityBot

pprint.pprint({k: v for k, v in config.__dict__.items() if not k.startswith('__')})

r = praw.Reddit(user_agent=config.USERAGENT,
                handler=MultiprocessHandler(*config.PRAW_SERVER) if getattr(config, 'PRAW_SERVER', None) else None)
r.login(config.USERNAME, config.PASSWORD)
print("[+] Done Reddit login!")

bot = RedditBot.from_log_file(
    r.get_subreddit(config.SUBREDDITS),
    config.BOT_LOG)

bot.observers.append(GracefulCharityBot().on_notify)
try:
    bot.run_forever()
except BaseException as e:
    bot.shutdown()
    print("[+] Shut down cleanly: '{}'".format(config.BOT_LOG))
    try:
        raise e
    except KeyboardInterrupt:
        exit(0)


class Contact:
    all_contacts = []

    def __init__(self, name, email):
        self.name = name
        self.email = email
        Contact.all_contacts.append(self)

class Supplier(Contact):
    def order(self, order):
        print(
            "If this were a real system we would send "
            f"'{order}' order to '{self.name}'"
        )

### Inherit from built-in classes

class ContactList(list):
    def search(self, name):
        '''
        Return all contacts that contain the search value in their name.
        '''
        matching_contacts = []
        for contact in self:
            if name in contact.name:
                matching_contacts.append(contact)

        return matching_contacts

class LongNameDict(dict):
    def longest_key(self):
        longest = None
        for key in self:
            if not longest or len(key) > len(longest):
                longest = key
        return longest

### Use super
class Friend(Contact):
    def __init__(self, name, email, phone):
        super().__init__(name, email)
        self.phone = phone


# Python mathematics library learning
import math

def main():
    print("please input the parameters like a b c in this expression: ax² + bx + c = 0.\n")
    aPar = int(input("a = "))
    print()
    bPar = int(input("b = "))
    print()
    cPar = int(input("c = "))
    print()

    dRoot = math.sqrt(bPar * bPar - 4 * aPar * cPar)
    x1 = round((bPar * (-1) + dRoot) / (aPar * 2), 3)
    x2 = round((bPar * (-1) - dRoot) / (aPar * 2), 3)

    print("The result is : ", x1, x2)

main()


import sqlite3
from StringIO import StringIO
from main_db_script import hr_db_filename
import pandas as pd

def add_qc_data(con):
    qc_table = StringIO(
        """0,None,These data are raw values and have not been quality controlled at all
        1,by researcher,These data have been quality controlled by us the researchers using our own judgment or algorithms
        2,by institution,These data have been verified by the institution that provided the data"""
    )
    qc_df = pd.read_csv(qc_table, sep=',', header=-1)
    qc_df.columns = ['QCID', 'name', 'description']
    qc_df.to_sql(con=con, name='qualitycontrollevels', if_exists='append', index=False)

#
# def drop_all_tables(cur, table_list):
#     for table in table_list:
#         try:
#             cur.execute("DROP TABLE {}".format(table))
#         except sqlite3.OperationalError:
#             pass
#
con = sqlite3.connect(hr_db_filename)
c = con.cursor()
#
# tables = ['variables', 'datavalues', 'sites', 'qualitycontrollevels']
# drop_all_tables(c, tables)

# c.execute("""CREATE TABLE variables\
#             (VariableID INTEGER PRIMARY KEY AUTOINCREMENT, \
#             VariableCode text, \
#             VariableName text, \
#             VariableDescription text, \
#             Units text, \
#             TimeSupport text);""")
# c.execute("""CREATE TABLE sites \
#             (SiteID INTEGER PRIMARY KEY AUTOINCREMENT, \
#             SiteCode text, \
#             SiteName text, \
#             SourceOrg text, \
#             Lat real, \
#             Lon real);""")
c.execute("""CREATE TABLE qualitycontrollevels\
            (QCID INTEGER PRIMARY KEY, \
            name text, \
            description text);""")
# c.execute("""CREATE TABLE datavalues \
#             (ValueID INTEGER PRIMARY KEY AUTOINCREMENT, \
#             Value real, \
#             Datetime text, \
#             VariableID, \
#             SiteID,\
#             QCID,\
#             FOREIGN KEY(VariableID) REFERENCES variables(VariableID),\
#             FOREIGN KEY(SiteID) REFERENCES sites(SiteID),\
#             FOREIGN KEY(QCID) REFERENCES qualitycontrollevels(QCID)\
#             );""")
con.commit()
add_qc_data(con)
con.close()


from bs4 import BeautifulSoup
from collections import deque
import json
import nmap
import os
import re
import requests
import requests.exceptions
import time
from time import gmtime, strftime
from urllib.error import URLError
from urllib.parse import urlsplit
import urllib3
import urllib.request
from urllib.request import urlopen

def banner():
    print('''
 _______   __
/       \ /  |
$$$$$$$  |$$ | __    __   ______
$$ |__$$ |$$ |/  |  /  | /      \
$$    $$  $$ |$$ |  $$ |/$$$$$$  |
$$$$$$$  |$$ |$$ |  $$ |$$    $$ |
$$ |__$$ |$$ |$$ \__$$ |$$$$$$$$/
$$    $$/ $$ |$$    $$/ $$       |
$$$$$$$/  $$/  $$$$$$/   $$$$$$$/

        Blue - Information Gathering Tool
        Author: Lexxrt | https://github.com/Lexxrt
 ''')

def menu():
    print("[+] 1.   Whois Lookup")
    print("[+] 2.   DNS Lookup")
    print("[+] 3.   Nmap Port Scan")
    print("[+] 4.   HTTP Header Grabber")
    print("[+] 5.   Clickjacking Test - X-Frame-Options Header")
    print("[+] 6.   Robots.txt Scanner")
    print("[+] 7.   Link Grabber")
    print("[+] 8.   IP GeoLocation Finder")
    print("[+] 9.   Traceroute")
    print("[+] 10.  Have I been pwned")
    print("[+] 11.  Exit\n")

def main():
    choice = ("1")
    banner()

    while choice != ("11"):
        menu()
        choice = input("[+] Enter your choice: ")

        if choice == ("1"):
            try:
                target = input("[+] Enter Domain or IP Address: ").lower()
                os.system("reset")
                print(f"[+] Searching for... Whois Lookup: ".format(target) + target)
                time.sleep(1.5)
                command = ("whois " + target)
                proces = os.popen(command)
                results = str(proces.read())
                print("" + results + command + "")

            except Exception:
                pass

        elif choice == ("2"):
            try:
                target = input("[+] Enter Domain or IP Address: ").lower()
                os.system("reset")
                print(f"[+] Searching for... DNS Lookup: ".format(target) + target)
                time.sleep(1.5)
                command = ("dig " + target + " +trace ANY")
                proces = os.popen(command)
                results = str(proces.read())
                print("" + results + command + "")

            except Exception:
                pass

        elif choice == ("3"):
            try:
                target = input("Enter Domain or IP Address: ").lower()
                os.system("reset")
                print("[+] Scanning.... Nmap Port Scan: " + target)
                print("[+] This will take a moment.\n")
                time.sleep(1.5)

                scanner = nmap.PortScanner()
                command = ("nmap -Pn " + target)
                process = os.popen(command)
                results = str(process.read())
                logPath = "logs/nmap-" + strftime("%Y-%m-%d_%H:%M:%S", gmtime())

                print("" + results + command + logPath + "")
                print("[+] Nmap Version: ", scanner.nmap_version())

            except KeyboardInterrupt:
                    print("\n")
                    print("[-] User Interruption Detected..!")
                    time.sleep(1)

        elif choice == ("4"):
            try:
                target = input("[+] Enter Domain or IP Address: ").lower()
                os.system("reset")
                print("[+] Scanning.... HTTP Header Grabber: \n" + target)
                time.sleep(1.5)
                command = ("http -v " + target)
                proces = os.popen(command)
                results = str(proces.read())
                print("" + results + command + "")

            except Exception:
                pass

        elif choice == ("5"):
            target = input("[+] Enter the Domain to test: ").lower()
            os.system("reset")

            if not (target.startswith("http://") or target.startswith("https://")):
                target = "http://" + target
            print("[+] Testing...  Clickjacking Test: " + target)
            time.sleep(2)
            try:
                resp = requests.get(target)
                headers = resp.headers
                print("\nHeader set are: \n")
                for item, xfr in headers.items():
                    print("" + item + ":" + xfr + "")

                if "X-Frame-Options" in headers.keys():
                    print("\n[+] Click Jacking Header is present")
                    print("[+] You can't clickjack this site !\n")
                else:
                    print("\n[*] X-Frame-Options-Header is missing ! ")
                    print("[!] Clickjacking is possible, this site is vulnerable to Clickjacking\n")

            except Exception as ex:
                print("[+] Exception caught: " + str(ex))

        elif choice == ("6"):
            try:
                target = input("Enter Domain: ").lower()
                os.system("reset")
                print("[+] Scanning.... Robots.txt Scanner: \n" + target)
                time.sleep(1.5)

                if not (target.startswith("http://") or target.startswith("https://")):
                    target = "http://" + target
                robot = target + "/robots.txt"

                try:
                    bots = urlopen(robot).read().decode("utf-8")
                    print("" + (bots) + "")
                except URLError:
                    print("[-] Can\'t access to {page}!".format(page=robot))

            except Exception as ex:
                print("[+] Exception caught: " + str(ex))

        elif choice == ("7"):
            try:
                target = input("Enter Domain: ").lower()
                os.system("reset")
                print("[+] Scanning.... Link Grabber: \n" + target)
                time.sleep(2)
                if not (target.startswith("http://") or target.startswith("https://")):
                    target = "http://" + target
                deq = deque([target])
                pro = set()

                try:
                    while len(deq):
                        url = deq.popleft()
                        pro.add(url)
                        parts = urlsplit(url)
                        base = "{0.scheme}://{0.netloc}".format(parts)

                        print("[+] Crawling URL " + "" + url + "")
                        try:
                            response = requests.get(url)
                        except (requests.exceptions.MissingSchema, requests.exceptions.ConnectionError):
                            continue

                        soup = BeautifulSoup(response.text, "lxml")
                        for anchor in soup.find_all("a"):
                            link = anchor.attrs["href"] if "href" in anchor.attrs else ''
                            if link.startswith("/"):
                                link = base + link
                            if not link in deq and not link in pro:
                                deq.append(link)
                            continue

                except KeyboardInterrupt:
                        print("\n")
                        print("[-] User Interruption Detected..!")
                        time.sleep(1)
                        print("\n[+] Exiting...\n")

            except Exception:
                pass

        elif choice == ("8"):
            try:
                target = input("[+] Enter Domain or IP Address: ").lower()
                url = ("http://ip-api.com/json/")
                response = urllib.request.urlopen(url + target)
                data = response.read()
                jso = json.loads(data)
                os.system("reset")
                print(f"[+] Searching.... IP Location Finder: ".format(url) + target)
                time.sleep(1.5)

                print("\n [+] Url: " + target + "")
                print(" [+] " + "" + "IP: " + jso["query"] + "")
                print(" [+] " + "" + "Status: " + jso["status"] + "")
                print(" [+] " + "" + "Region: " + jso["regionName"] + "")
                print(" [+] " + "" + "Country: " + jso["country"] + "")
                print(" [+] " + "" + "City: " + jso["city"] + "")
                print(" [+] " + "" + "ISP: " + jso["isp"] + "")
                print(" [+] " + "" + "Lat & Lon: " + str(jso['lat']) + " & " + str(jso['lon']) + "")
                print(" [+] " + "" + "Zipcode: " + jso["zip"] + "")
                print(" [+] " + "" + "TimeZone: " + jso["timezone"] + "")
                print(" [+] " + "" + "AS: " + jso["as"] + "" + "\n")

            except URLError:
                print("[-] Please provide a valid IP address!")

        elif choice == ("9"):
            try:
                target = input("[+] Enter Domain or IP Address: ").lower()
                os.system("reset")
                print(f"[+] Searching for: Traceroute: ".format(target) + target)
                print("[+] This will take a moment... Get some coffee :)\n")
                time.sleep(5)
                command = ("mtr " + "-4 -rwc 1 " + target)
                proces = os.popen(command)
                results = str(proces.read())
                print("" + results + command + "")

            except Exception:
                pass

        elif choice == ("10"):
            try:
                target = input("[+] Enter email: ")
                os.system("reset")
                print("[+] Scanning....Have I been pwned: \n" + target)
                time.sleep(1.5)
                url = ("https://haveibeenpwned.com/api/v2/breachedaccount/%s" % target)
                response = requests.get(url)

                if response.status_code == 200:
                    response = response.json()
                    le = len(response)

                    for item in range(le):
                        clas = str(response[item]["DataClasses"])
                        clas = re.sub("\[(?:[^\]|]*\|)?([^\]|]*)\]", r"\1", clas)
                        clas = clas.replace("'", "")

                        print("\n")
                        print("[+] Name:     " + "" + str(response[item]["Title"]) + "")
                        print("[+] Domain:   " + "" + str(response[item]["Domain"]) + "")
                        print("[+] Breached: " + "" + str(response[item]["BreachDate"]) + "")
                        print("[+] Details:  " + "" + str(clas) + "")
                        print("[+] Verified: " + "" + str(response[item]["IsVerified"]) + "")
                else:
                    print("[+] Email NOT Found in Database")

            except Exception:
                print("[+] Unable to reach HaveIBeenPwned")

        elif choice == ("11"):
            time.sleep(1)
            print("\n[+] Exiting...")

        else:
            print("[-] Invalid option!")

# =====# Main #===== #

if __name__ == "__main__":
    main()


__author__ = 'Emre'
import numpy as np
from scipy import stats

def sim(d,p, x_bf, y_bf):

    n = d.size

    # p = .4
    x = d[:, 0:1]

    y = d[:, 1:]
    # x = np.array([.5, .3,.2])
    # y = np.array([.5, .4, .1])
    own_bid = x_bf(x)
    opp_bid = y_bf(y)
    # print(own_bid)
    # print(opp_bid)
    bids = np.hstack((opp_bid, own_bid))
    # print(bids)
    # input()
    # print(bids)

    sorted_bid_owners = np.argsort(bids,axis=1)
    # print(sorted_bid_owners)
    # input()
    # print(sorted_bid_owners)
    lowest = sorted_bid_owners[:,n-1] == 0

    p_clearing = own_bid <= p

    # print(p_clearing)
    # print(lowest)
    mask = lowest*p_clearing
    # print(mask)

    payoff = ((p-x)*mask)
    average = np.mean(payoff)

    return average

min_bid = 0
max_bid = 1
delta = max_bid-min_bid
trials = 10000
size = 1
competitors = [2]
for c in competitors:
    print("c=",c)
    # for k in range(7):
        # print("k=",k)
    z_hist = np.zeros((trials))

    z_hist = np.zeros((trials))
    y_hist = np.zeros((trials))
    bidrv = stats.uniform(min_bid, delta)
    for t in range(trials):
        d = bidrv.rvs((1, c))
        # print(d)
        p = np.random.random()

        a = lambda v: np.where(v< (c-1)/(2*c), 0, (2*c*v-(c-1))/(c+1))
        b = lambda v: np.where(v < (c)/(2*(c+1)), 0, (2*(c+1)*v-(c))/(c+2))
        aa = lambda v: np.where(v<1/3, 0, (6*v-2)/4)
        bb = lambda v: np.where(v<1/3, 1, (6*v-2)/4)
        cc = lambda v: v
        dd = lambda v: np.where(v < 1/4, 0, 2*v-.5)
        # print(p)
        z = sim(d, p, a, a)
        y = sim(d, p, dd, a)
        z_hist[t] = z
        y_hist[t] = y

print(np.mean(z_hist))
print(np.mean(y_hist))


# import the necessary packages
from keras.models import Sequential, Model
from keras.layers.normalization import BatchNormalization
from keras.layers.core import Activation
from keras.layers.core import Flatten
from keras.layers.core import Dropout
from keras.layers.core import Dense
from keras.applications import VGG16
from keras import layers
# import our config
from config import config

class MyVGGNet:
    @staticmethod
    def build(classes, finalAct="softmax"):
        # initialize VGG16 network without the fully connected layer
        conv_base = VGG16(weights="imagenet", include_top=False, input_shape=config.IMAGE_DIMS)
        for layer in conv_base.layers:
            layer.trainable = False

        # setting up our model
        model = Sequential()
        model.add(conv_base)

        # fully connected layer
        model.add(Flatten())
        model.add(Dense(1024))
        model.add(Activation("relu"))
        model.add(BatchNormalization())
        model.add(Dropout(0.5))

        # classifier
        model.add(Dense(classes))
        model.add(Activation(finalAct))

        return model

    @staticmethod
    def ensemble_models(models, model_input):
        # collect outputs of models in a list
        y_models = [model(model_input) for model in models]
        # averaging outputs
        y_avg = layers.average(y_models)
        # build model from same input and avg output
        model_ens = Model(inputs=model_input, outputs=y_avg, name='ensemble')

        return model_ens


# -*- coding: utf-8 -*-
from flask import (
    render_template, Blueprint, current_app, request, redirect, flash, url_for)

from ..mail import send_mail
from .forms import ContactForm
from ..utils import after_app_teardown

index_blueprint = Blueprint('index', __name__,)

def send_contact_mail(data):
    test = data.get('test', '')
    send_mail(
        subject='[CONTACT_FORM]{} message from {}'.format(
            test, data.get('email', '')),
        body=data.get('message'),
        recipients=[current_app.config.get('CONTACT_EMAIL')],
        reply_to=data.get('email', 'unknownemail@example.com'))

@index_blueprint.route('/contact.html')
def contact():
    form = ContactForm()
    return render_template('contact.html', form=form)

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

@index_blueprint.route('/contact_form', methods=['GET', 'POST'])
def contact_form():
    data = request.form

    @after_app_teardown
    def send_mail():
        send_contact_mail(data)

    flash('Thank you for your feedback.', 'success')
    return redirect(url_for('.index'))

# vim:set ft=python sw=4 et spell spelllang=en:


from PyQt5.QtWidgets import (QLabel, QDialog, QVBoxLayout, QTableView, QPushButton)

from tinos_steam_helper.data_queries import open_browser
from .CardsTableModel import CardsTableModel, MySortFilterProxyModel

class InventoryPopup(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setGeometry(300, 500, 1000, 300)
        self.setWindowRole('pop-up')

        self.popup_window = QVBoxLayout()

    @classmethod
    def make_popup(cls, inventory, cards_filter, title, parent=None):
        popup = cls(parent)
        data, column_names = inventory.tabulate_data()
        data = [datum for datum in data if datum[0] in cards_filter]

        if data:
            cards_model = CardsTableModel(data, popup, column_names=column_names)

            sort_proxy = MySortFilterProxyModel()
            sort_proxy.setSourceModel(cards_model)
            popup_cards_view = QTableView(popup)
            popup_cards_view.setModel(sort_proxy)
            popup_cards_view.doubleClicked.connect(popup.double_clicked_inventory_cell)

            popup.popup_window.addWidget(popup_cards_view)
        else:
            popup.setGeometry(700, 550, 200, 100)
            popup.popup_window.addWidget(QLabel("no " + title))

        popup.setLayout(popup.popup_window)
        popup.setWindowTitle(title)
        popup.show()

    @classmethod
    def find_overfull_cards(cls, inventory, parent=None):
        overfull_cards = [game for game in inventory.find_overfull_cards()]
        cls.make_popup(inventory, overfull_cards, "overfull cards", parent=parent)

    @classmethod
    def find_overfull_sets(cls, inventory, parent=None):
        overfull_sets = [game for game in inventory.find_overfull_sets()]
        cls.make_popup(inventory, overfull_sets, "overfull sets", parent=parent)

    @classmethod
    def find_removed_sets(cls, inventory, parent=None):
        removed_sets = [game for game in inventory.find_removed_sets()]
        cls.make_popup(inventory, removed_sets, "removed sets", parent=parent)

    def double_clicked_inventory_cell(self, index):
        open_browser.double_clicked_inventory_cell(self.parent(), index)


import argparse
import os
import time

class ExiFillOptions:
    def __init__(self):
        self.parser = TestArgs()

    def parse(self,dataset="places2",loadmodeldir='./gmcnn/checkpoints/paris-streetview_256x256_rect'):

        self.opt = self.parser

        if os.path.exists(self.opt.test_dir) is False:
            os.mkdir(self.opt.test_dir)

        assert self.opt.random_mask in [0, 1]
        self.opt.random_mask = True if self.opt.random_mask == 1 else False

        assert self.opt.mask_type in ['rect', 'stroke']

        str_img_shapes = self.opt.img_shapes.split(',')
        self.opt.img_shapes = [int(x) for x in str_img_shapes]

        str_mask_shapes = self.opt.mask_shapes.split(',')
        self.opt.mask_shapes = [int(x) for x in str_mask_shapes]

        self.opt.dataset = dataset
        self.opt.load_model_dir = loadmodeldir

        # saving dir name form = model name and date
        self.opt.model_folder = 'temp_' + self.opt.dataset + '_' + self.opt.model
        # self.opt.model_folder += '_s' + str(self.opt.img_shapes[0]) + 'x' + str(self.opt.img_shapes[1])
        # self.opt.model_folder += '_gc' + str(self.opt.g_cnum)
        # self.opt.model_folder += '_randmask-' + self.opt.mask_type if self.opt.random_mask else ''
        if self.opt.random_mask:
            self.opt.model_folder += '_seed-' + str(self.opt.seed)
        self.opt.saving_path = os.path.join(self.opt.test_dir, self.opt.model_folder)

        if os.path.exists(self.opt.saving_path) is False and self.opt.mode == 'save':
            os.mkdir(self.opt.saving_path)

        args = vars(self.opt)

        print('------------ Options -------------')
        for k, v in sorted(args.items()):
            print('%s: %s' % (str(k), str(v)))
        print('-------------- End ----------------')

        return self.opt

class TestArgs:
    def __init__(self):
        self.dataset='paris'
        self.data_file='./App/static/images/pstreetview_256x256'
        self.test_dir='./App/static/center_results'
        self.load_model_dir='./App/gmcnn/checkpoints/paris-streetview_256x256_rect'
        self.model_prefix='snap'
        self.seed=1
        self.model='gmcnn'
        self.img_shapes='256,256,3'

        self.mask_shapes='128,128'
        self.mask_type='rect'
        self.random_mask=0

        self.test_num=-1
        self.mode='save'
        # for generator
        self.g_cnum=32
        self.d_cnum=64


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

import numpy as np
import pickle as pkl
import theano
import theano.tensor as T
import matplotlib as plt
from theano.tensor.shared_randomstreams import RandomStreams
from theano import function
from sklearn.model_selection import train_test_split
import timeit
import os
import sys

class LSTMlayer:
    def __init__(self,input,n_input,n_hidden):
        self.input = input
        self.n_input = n_input
        self.n_hidden = n_hidden
        #input
        #srng = RandomStreams(seed=234)
        init_wi0 = np.array(np.random.uniform(low=np.sqrt(1./n_input),high=(np.sqrt(1./n_input)) ,size=(n_input,n_hidden)),dtype=theano.config.floatX)
        init_wi1 = np.array(np.random.uniform(low=np.sqrt(1. / n_input), high=(np.sqrt(1. / n_input)), size=(n_hidden,n_hidden)),dtype=theano.config.floatX)
        init_wi2 = np.array(np.random.uniform(low=np.sqrt(1. / n_input), high=(np.sqrt(1. / n_input)), size=(n_hidden,n_hidden)),dtype=theano.config.floatX)
        init_bi = np.zeros(shape=(n_hidden,),dtype=theano.config.floatX)
        self.wi0 = theano.shared(value=init_wi0,name='wi0')
        self.wi1 = theano.shared(value=init_wi1, name='wi1')
        self.wi2 = theano.shared(value=init_wi2, name='wi2')
        self.bi = theano.shared(value=init_bi,name='bi')

        #forget
        init_wf0 = np.array(np.random.uniform(low=np.sqrt(1./n_input),high=(np.sqrt(1./n_input)) ,size=(n_input,n_hidden)),dtype=theano.config.floatX) #dot遵循正常矩阵的乘法
        init_wf1 = np.array(np.random.uniform(low=np.sqrt(1. / n_input), high=(np.sqrt(1. / n_input)), size=(n_hidden,n_hidden)),dtype=theano.config.floatX)
        init_wf2 = np.array(np.random.uniform(low=np.sqrt(1. / n_input), high=(np.sqrt(1. / n_input)), size=(n_hidden,n_hidden)),dtype=theano.config.floatX)
        init_bf = np.zeros(shape=(n_hidden,),dtype=theano.config.floatX)
        self.wf0 = theano.shared(value=init_wf0,name='wf0')
        self.wf1 = theano.shared(value=init_wf1, name='wf1')
        self.wf2 = theano.shared(value=init_wf2, name='wf2')
        self.bf = theano.shared(value=init_bf,name='bf')
        #cell
        init_wc0 = np.array(np.random.uniform(low=np.sqrt(1./n_input),high=(np.sqrt(1./n_input)) ,size=(n_input,n_hidden)),dtype=theano.config.floatX)
        init_wc1 = np.array(np.random.uniform(low=np.sqrt(1. / n_input), high=(np.sqrt(1. / n_input)), size=(n_hidden,n_hidden)),dtype=theano.config.floatX)
        init_wc2 = np.array(np.random.uniform(low=np.sqrt(1. / n_input), high=(np.sqrt(1. / n_input)), size=(n_hidden,n_hidden)),dtype=theano.config.floatX)
        init_bc = np.zeros(shape=(n_hidden,),dtype=theano.config.floatX)
        self.wc0 = theano.shared(value=init_wc0,name='wc0')
        self.wc1 = theano.shared(value=init_wc1, name='wc1')
        self.wc2 = theano.shared(value=init_wc2, name='wc2')
        self.bc = theano.shared(value=init_bc,name='bc')

        #output
        init_wo0 = np.array(np.random.uniform(low=np.sqrt(1./n_input),high=(np.sqrt(1./n_input)) ,size=(n_input,n_hidden)),dtype=theano.config.floatX)
        init_wo1 = np.array(np.random.uniform(low=np.sqrt(1. / n_input), high=(np.sqrt(1. / n_input)), size=(n_hidden,n_hidden)),dtype=theano.config.floatX)
        init_wo2 = np.array(np.random.uniform(low=np.sqrt(1. / n_input), high=(np.sqrt(1. / n_input)), size=(n_hidden,n_hidden)),dtype=theano.config.floatX)
        init_bo = np.zeros(shape=(n_hidden,),dtype=theano.config.floatX)
        self.wo0 = theano.shared(value=init_wo0,name='wo0')
        self.wo1 = theano.shared(value=init_wo1, name='wo1')
        self.wo2 = theano.shared(value=init_wo2, name='wo2')
        self.bo = theano.shared(value=init_bo,name='bo')

        self.params1 = [self.wi0,self.wi1,self.wi2,self.bi,self.wf0,self.wf1,self.wf2,self.bf,self.wc0,self.wc1,self.wc2,self.bc]

        def _cellcalculate(x,bh,sc):                                 #bh和sc需要自己定义初值
            ai = T.dot(self.wi0,x)+T.dot(self.wi1,bh)+T.dot(self.wi2,sc)
            _bi = T.nnet.hard_sigmoid(ai+self.bi)

            af = T.dot(self.wf0,x)+T.dot(self.wf1,bh)+T.dot(self.wf2,sc)
            _bf = T.nnet.hard_sigmoid(af+self.bf)

            ac = T.dot(self.wc0,x)+T.dot(self.wc1,bh)+T.dot(self.wc2,sc)
            _bc = T.tanh(ac+self.bc)
            sc = _bf*sc + _bi*_bc

            ao = T.dot(self.wo0,x)+T.dot(self.wo1,bh)+T.dot(self.wo2,sc)
            _bo = T.nnet.hard_sigmoid(ao+self.bo)

            bh = _bo*(T.tanh(sc))

            return [bh,sc]

            # outputs_info = T.as_tensor_variable(np.asarray(0, x.dtype))  (如果outputs_info报错，可以试试这个）

        x = T.matrix('x')
        [result_b, result_s], updates = theano.scan(_cellcalculate,
                                                truncate_gradient=-1,
                                                sequences=x,
                                                outputs_info=[np.zeros(self.n_hidden),np.zeros(self.n_hidden)])  # 全都初始化为0,不知道对不对.

        self.predict = result_b[-1]

    # mean pooling  之后再添加
    # i=0
    # _y = np.zeros(len(results[1]))
    # while i<len(results[1]):
    #     _y[i] = T.mean(results[1,i])
    #     _y[i] = 1/(1+T.exp(_y[i]))
    #     if _y[i]<0.5:
    #         _y[i] = 0
    #     else:
    #         _y[i] = 1
    #
    # #sigmoid
    #
    #
    # #costfunction
    # costfunction =T.mean((y-_y)*(y-_y))
    #
    # return costfunction

class logisticlayer:
    def __init__(self,input,n_input,n_hidden,n_output):

        self.input=input
        self.n_hidden=n_hidden
        self.n_output=n_output
        self.w=theano.shared(
            value=np.zeros(
                (n_hidden,n_output),
                dtype=theano.config.floatX
            ),
            name='w',
            borrow=True
        )
        self.b=theano.shared(
            value=np.zeros(
                (n_output,),   #一直不明白n_output后面的逗号是什么意思
                dtype=theano.config.floatX
            ),
            name='b',
            borrow=True
        )
        self.params2 = [self.w, self.b]

        self.lstmlayer = LSTMlayer(input=self.input,n_input=n_input,n_hidden=n_hidden)
        self.lastlayeroutput=self.lstmlayer.predict

        print (self.lastlayeroutput)

        self.params = self.lstmlayer.params1 + self.params2

        self.p_y_given_x=T.nnet.softmax(T.dot(self.lastlayeroutput,self.w)+self.b) #分成两类
        self.y_predict=T.argmax(self.p_y_given_x,axis=1)

        y = T.ivector('y')

        self.negative_log_likelihood = -T.mean(T.log(self.p_y_given_x)[T.arange(y.shape[0]),y])

        self.errors = T.mean(T.neq(self.y_predict,y))

def load_data(filename):
    f= open(filename,'rb')
    x_test=pkl.load(f)
    x_train=pkl.load(f)
    y_test=pkl.load(f)
    y_train=pkl.load(f)
    f.close()
    x_train, x_valid, y_train, y_valid = train_test_split(
        x_train,y_train, test_size = 0.1, random_state = 0)
    def shared_dataset(datax,borrow=True):
        shared_x=theano.shared(np.array(datax
                             ,dtype=theano.config.floatX),
                               borrow=borrow)
        return shared_x

    x_train=([[1,2,3,4,5],
                       [1, 2, 3, 4, 5]]
                     #  [[1, 2, 3, 4, 5],
                     #  [1, 2, 3, 4, 5]],
                     # [[1, 2, 3, 4, 5],
                     # [1, 2, 3, 4, 5]]
                      )
    y_train =[0,1,0]
    x_valid=([[1,2,3,4,5],
                       [1, 2, 3, 4, 5]]
                     #  [[1, 2, 3, 4, 5],
                     #  [1, 2, 3, 4, 5]],
                     # [[1, 2, 3, 4, 5],
                     # [1, 2, 3, 4, 5]]
                     )
    y_valid =[0,1,0]
    x_test=([[1,2,3,4,5],
                       [1, 2, 3, 4, 5]]
                     #  [[1, 2, 3, 4, 5],
                     #  [1, 2, 3, 4, 5]],
                     # [[1, 2, 3, 4, 5],
                     # [1, 2, 3, 4, 5]]
                      )
    y_test =[0,1,0]

    # x_test=shared_dataset(x_test)
    # y_test=shared_dataset(y_test)
    # x_train=shared_dataset(x_train)
    # y_train=shared_dataset(y_train)
    # x_valid=shared_dataset(x_valid)
    # y_valid=shared_dataset(y_valid)

    rval=[(x_train,y_train),(x_valid,y_valid),(x_test,y_test)]
    return rval

def sgd_optimization(lr=0.01,n_epochs=100,filemane='data.pkl',batch_size=1,n_input=5,n_hidden=10,n_output=2):
    # datasets=load_data(filemane)
    # train_x,train_y=datasets[0]
    # valid_x,valid_y=datasets[1]
    # test_x,test_y=datasets[2]
    #

    train_x=([[1,2,3,4,5],
             [1, 2, 3, 4, 5]]
                     #  [[1, 2, 3, 4, 5],
                     #  [1, 2, 3, 4, 5]],
                     # [[1, 2, 3, 4, 5],
                     # [1, 2, 3, 4, 5]]
                      )
    train_y =[0,1,0]
    valid_x=([[1,2,3,4,5],
                       [1, 2, 3, 4, 5]]
                      # [[1, 2, 3, 4, 5],
                     #  [1, 2, 3, 4, 5]],
                     # [[1, 2, 3, 4, 5],
                     # [1, 2, 3, 4, 5]]
                      )
    valid_y =[0,1,0]
    test_x=([[1,2,3,4,5],
                       [1, 2, 3, 4, 5]]
                     #  [[1, 2, 3, 4, 5],
                     #  [1, 2, 3, 4, 5]],
                     # [[1, 2, 3, 4, 5],
                     # [1, 2, 3, 4, 5]]
                      )
    test_y =[0,1,0]

    # n_train_batches=train_x.get_value(borrow=True).shape[0]//batch_size
    # n_valid_batches=valid_x.get_value(borrow=True).shape[0]//batch_size
    # n_test_batches=test_x.get_value(borrow=True).shape[0]//batch_size
    # n_train_batches=train_x.shape[0]//batch_size
    # n_valid_batches=valid_x.shape[0]//batch_size
    # n_test_batches=test_x.shape[0]//batch_size
    n_train_batches= 3
    n_valid_batches=3
    n_test_batches=3

    print('...building the model')

    index=T.iscalar()
    x=T.matrix('x')
    y=T.matrix('y')

    rng=np.random.RandomState(1234)

    classifier = logisticlayer(
        input=x,
        n_input=n_input,
        n_hidden=n_hidden,
        n_output=n_output
    )

    cost = classifier.negative_log_likelihood

    print('test_model')
    test_model = theano.function(
        inputs=[index],
        outputs=classifier.errors,
        givens={
            x: test_x[index*batch_size:(index+1)*batch_size],
            y: test_y[index*batch_size:(index+1)*batch_size]
        }
    )
    print ('valid_model')
    valid_model = theano.function(
        inputs=[index],
        outputs=classifier.errors,
        givens={
            x: valid_x[index*batch_size:(index+1)*batch_size],
            y: valid_y[index*batch_size:(index+1)*batch_size]
        }
    )

    gparams = [T.grad(cost,param)for param in classifier.params]
    updates = [
        (param,param - lr*gparams)
        for param,gparams in zip(classifier.params,gparams)
    ]

    train_model = theano.function(
        inputs = [index],
        outputs=cost,
        updates=updates,
        givens={
            x: train_x[index * batch_size: (index + 1) * batch_size],
            y: train_y[index * batch_size: (index + 1) * batch_size]
        }
    )
    print('... training')

    # early-stopping parameters
    patience = 10000  # look as this many examples regardless
    patience_increase = 2  # wait this much longer when a new best is
    # found
    improvement_threshold = 0.995  # a relative improvement of this much is
    # considered significant
    validation_frequency = min(n_train_batches, patience // 2)
    # go through this many
    # minibatche before checking the network
    # on the validation set; in this case we
    # check every epoch

    best_validation_loss = np.inf
    best_iter = 0
    test_score = 0.
    start_time = timeit.default_timer()

    epoch = 0
    done_looping = False

    while (epoch < n_epochs) and (not done_looping):
        epoch = epoch + 1
        for minibatch_index in range(n_train_batches):

            minibatch_avg_cost = train_model(minibatch_index)
            # iteration number
            iter = (epoch - 1) * n_train_batches + minibatch_index

            if (iter + 1) % validation_frequency == 0:
                # compute zero-one loss on validation set
                validation_losses = [valid_model(i) for i
                                     in range(n_valid_batches)]
                this_validation_loss = np.mean(validation_losses)

                print(
                    'epoch %i, minibatch %i/%i, validation error %f %%' %
                    (
                        epoch,
                        minibatch_index + 1,
                        n_train_batches,
                        this_validation_loss * 100.
                    )
                )

                # if we got the best validation score until now
                if this_validation_loss < best_validation_loss:
                    # improve patience if loss improvement is good enough
                    if (
                                this_validation_loss < best_validation_loss *
                                improvement_threshold
                    ):
                        patience = max(patience, iter * patience_increase)

                    best_validation_loss = this_validation_loss
                    best_iter = iter

                    # test it on the test set
                    test_losses = [test_model(i) for i
                                   in range(n_test_batches)]
                    test_score = np.mean(test_losses)

                    print(('     epoch %i, minibatch %i/%i, test error of '
                           'best model %f %%') %
                          (epoch, minibatch_index + 1, n_train_batches,
                           test_score * 100.))

            if patience <= iter:
                done_looping = True
                break

    end_time = timeit.default_timer()
    print(('Optimization complete. Best validation score of %f %% '
           'obtained at iteration %i, with test performance %f %%') %
          (best_validation_loss * 100., best_iter + 1, test_score * 100.))
    print(('The code for file ' +
           os.path.split(__file__)[1] +
           ' ran for %.2fm' % ((end_time - start_time) / 60.)) )###file=sys.stderr

if __name__ == '__main__':
    sgd_optimization()


# Given a digit string, return all possible letter combinations
# that the number could represent.
#
# Input: "23"
# Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
# your answer could be in any order you want.

class Solution(object):
    # Runtime of this solution beats 98.51% of python submissions
    # see details: https://leetcode.com/submissions/detail/108170741/
    def letterCombinations(self, digits):
        """
        :type digits: str
        :rtype: List[str]
        """
        if digits == '':
            return []

        phone = {'2': ('a', 'b', 'c'), '3': ('d', 'e', 'f'),
                 '4': ('g', 'h', 'i'), '5': ('j', 'k', 'l'),
                 '6': ('m', 'n', 'o'), '7': ('p', 'q', 'r', 's'),
                 '8': ('t', 'u', 'v'), '9': ('w', 'x', 'y', 'z')}

        result = phone[digits[0]]
        for i in digits[1:]:
            result = self.cartProduct(result, phone[i])
        return result

    def cartProduct(self, l1, l2):
        return [i + j for i in l1 for j in l2]

s = Solution()
print(s.letterCombinations('23'))


# config file for training or other process
# check the config file before your process
# and import the file to get pararmeters
# the project is for enhancing the target speech with directional features

# data config
total_channels = 7
target_channel = 7
refer_channels = [1]  # set of channels would be referred
                     # range of [1, total_channels] excluding target_channel
spatial_style = "concat"  # ['concat', 'average']
# microphone array params, for computing directional feats
# shape of ring, target channel 'ch7' locates at center
c = 340  # speed of sound
radius = 0.1
relative_angles = [(ch - 1) * 60 - 180 for ch in refer_channels]
relative_distances = [radius for ch in refer_channels]

target_dir = "target"
beamed_dir = "DSB"
target_lst_file = "target.lst"
num_spkrs = 2
num_refers = 1
samp_rate = 8000
frame_duration = 0.032
frame_size = int(samp_rate * frame_duration)
feat_dim = frame_size // 2 + 1
shift_duration = 0.008
shift = int(samp_rate * shift_duration)
overlap_rate = 0
batch_size = 32
dev_batch_size = 32
min_queue_size = 20
load_file_num = 20
min_sent_len = 10
shorter_sent_len = 100
longer_sent_len = 400
embedding_dim = 20
# train & dev dataset
#train_dir = "/gpfs/share/home/1801213802/data//data/train"
#dev_dir = "/gpfs/share/home/1801213802/data//data/dev"
global_cmvn_file = 'list/ipd_magn_net_cmvn_ipd'
train_dir = "/gpfs/share/home/1801213802/data/mixreverb/2speakers_reverb/wav8k/min/tr"
dev_dir = "/gpfs/share/home/1801213802/data/mixreverb/2speakers_reverb/wav8k/min/cv"
train_anechoic_dir = "/gpfs/share/home/1801213802/data/mixreverb/2speakers_anechoic/wav8k/min/tr"
dev_anechoic_dir = "/gpfs/share/home/1801213802/data/mixreverb/2speakers_anechoic/wav8k/min/cv"
# job config
job_type = "train"
lamda = 100.0
#job_dir = "job/no_pretrain_2layer_nodrop_ipd_anechoic"
#job_dir = "job/no_preptrain_multidcl1_cmvn_npsm_lambda50"
#job_dir = "job/original_nreluqkv_1tr_test_50epoch"
job_dir = "job/gw_tet_delete_relu_layer_2_change_inner_layer_400_yeta"
#job_dir = "job/original_nreluqkv"
#job_dir = "job/no_preptrain_multidc_cmvn"
#job_dir = "job/no_pretrain_2layer_nodrop_multidc"
#job_dir = "job/no_pretrain_multidcl1_lambda1000"

ipd_dir = "trained/IPD/model.ckpt"
magn_dir = "trained/magn/model.ckpt"
#job_dir = "job/DFonAngle_ReferAll_concatDF_4x600lstm_dropout0.5_psm_without_steeringvertor"
gpu_list = [0]

# model config
log_feat = True
rnn_type = "lstm"
bidirectional = True
num_layers = 2
hidden_size = 600  # unidirectional
dropout_keep_fw = 0.7
dropout_keep_rc = 1.0
phase_sensitive = True
threshold = -40
fcl_layers = 0
fcl_hidden_size = 600
# training param
seed = 123
resume = True
init_mean = 0.0
init_stddev = 0.02
max_grad_norm = 200
learning_rate = 0.00064
max_epoch = 300
pretrain_shorter_epoch = 0
log_period = 10
save_period = 500
dev_period = 3000
early_stop_count = 3
decay_lr_count = 1
decay_lr = 0.8
min_learning_rate = 1e-6

# test config
#test_dir = "/gpfs/share/home/1801213802/data//data/test"
test_dir = '/gpfs/share/home/1801213802/data/mixreverb/2speakers_reverb/wav8k/min/tt'
#test_anechoic_dir = '/gpfs/share/home/1801213802/data/mixreverb/2speakers_anechoic/wav8k/min/tt'
#test_name = "test_h1000"
#test_dir = "/gpfs/share/home/1801213802/data/mixreverb/2speakers_reverb/wav8k/min/cv"
#test_name = 'test_h1000_load1'
test_name = 'GW_load_option_1_old_average'
# load option
load_option = 1
load_path = "job/gw_tet_delete_relu_layer_2_change_inner_layer_400_yeta/model.ckpt-average"
stable = False
silence = False
ipdonly = False
DC = True
DC_new = True
job_anechoic = False

#transformer config
transformer_total_units = 387
transformer_heads = 3
transformer_num_layers = 2


import json
import os
import requests
import sqlite3
import zipfile
import global_settings as g

conn = None

def connect():
    global conn
    if g.DB_FILE is None:
        return None
    if conn:
        conn.close()
    return sqlite3.connect('file:{}?mode=ro'.format(g.DB_FILE), uri=True)

def list_tables():
    with conn:
        cur = conn.execute("SELECT name FROM sqlite_master WHERE type='table';")
    results = cur.fetchall()
    return [table[0] for table in results]

def update_manifest():
    global conn, tables
    response = requests.get(g.BASE_URL + "/Platform/Destiny2/Manifest/",
                            headers=g.HEADERS)
    if os.path.exists("manifest.txt"):
        with open("manifest.txt", "r") as infile:
            current_manifest = infile.read()
    else:
        current_manifest = "NONE"
    new_manifest = response.json()['Response']['mobileWorldContentPaths']['en']
    if new_manifest.split('/')[-1] == current_manifest:
        return False
    download_manifest(new_manifest)
    with open("manifest.txt", "w") as outfile:
        outfile.write(new_manifest.split('/')[-1])
    g.DB_FILE = new_manifest.split('/')[-1]
    conn = connect()
    tables = list_tables()
    return True

def download_manifest(manifesturl):
    downloadurl = g.BASE_URL + manifesturl
    r = requests.get(downloadurl, headers=g.HEADERS, stream=True)
    with open("manifest.zip", "wb+") as outfile:
        for chunk in r.iter_content(chunk_size=1024):
            if chunk:
                outfile.write(chunk)
    with zipfile.ZipFile("manifest.zip", "r") as archive:
        archive.extractall(".")
    os.remove("manifest.zip")

def lookup(table, hashvalue):
    if table not in tables:
        return None
    column = 'key' if table == 'DestinyHistoricalStatsDefinition' else 'id'
    key = int(hashvalue)
    if (key & (1 << 31)) != 0:
        key = key - (1 << 32)
    with conn:
        cur = conn.execute("SELECT json FROM {} WHERE {} = ?;".format(table,
                                                                  column), (key,))
    return json.loads(cur.fetchone()[0])

conn = connect()
tables = list_tables() if conn else None


# Import various modules for string cleaning
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import re
from nltk.corpus import stopwords
import nltk.data
from unidecode import unidecode
import wiki_parse

def text_to_wordlist(text, remove_stopwords=False):
    # Function to convert a document to a sequence of words,
    # optionally removing stop words.  Returns a list of words.

    # 1. Remove HTML
    text_ = BeautifulSoup(text, "html.parser").get_text()

    # 2. Remove non-letters
    text_ = re.sub("[^a-zA-Z]", " ", text_)

    # 3. Convert words to lower case and split them
    words = text_.lower().split()

    # 4. Optionally remove stop words (false by default)
    if remove_stopwords:
        stops = set(stopwords.words("spanish"))
        words = [w for w in words if w not in stops]

    # 5. Return a list of words
    return(words)

# Define a function to split a review into parsed sentences

def text_to_sentences(text, tokenizer, remove_stopwords=False):
    # Function to split a review into parsed sentences. Returns a
    # list of sentences, where each sentence is a list of words

    # 0. Replace accents with equivalent ASCII
    text = unidecode(text)
    text.encode("ascii")

    # 1. Use the NLTK tokenizer to split the paragraph into sentences
    raw_sentences = tokenizer.tokenize(text.strip())

    # 2. Loop over each sentence
    sentences = []
    for raw_sentence in raw_sentences:
        # If a sentence is empty, skip it
        if len(raw_sentence) > 0:
            # Otherwise, call review_to_wordlist to get a list of words
            sentences.append(text_to_wordlist(raw_sentence, remove_stopwords))

    # Return the list of sentences (each sentence is a list of words,
    # so this returns a list of lists
    return sentences

tokenizer = nltk.data.load('tokenizers/punkt/spanish.pickle')

sentences = []  # Initialize an empty list of sentences

# print("\n\n")
# with open('mexico_dataset.txt', 'r', encoding='utf-8') as file:
# 	for sentence in file:
# 		if len(sentence) > 1:
# 			wordlist = text_to_sentences(sentence, tokenizer)
# 			sentences += wordlist
# 			#print("\n**********\n")
# 			#print(wordlist)
# 			#input()

print("\nParsing wiki")
# Extract 1000 sentences
sentences = wiki_parse.extract_wiki(5000)
print("\nWiki parsed")
input()

# Import the built-in logging module and configure it so that Word2Vec
# creates nice output messages
import logging
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s',
                    level=logging.INFO)

# Set values for various parameters
num_features = 300    # Word vector dimensionality
min_word_count = 40   # Minimum word count
num_workers = 4       # Number of threads to run in parallel
context = 10          # Context window size
downsampling = 1e-3   # Downsample setting for frequent words

# Initialize and train the model (this will take some time)
from gensim.models import word2vec
print("Training model...")
model = word2vec.Word2Vec(sentences, workers=num_workers,
                          size=num_features, min_count=min_word_count,
                          window=context, sample=downsampling)

# If you don't plan to train the model any further, calling
# init_sims will make the model much more memory-efficient.
model.init_sims(replace=True)

# It can be helpful to create a meaningful model name and
# save the model for later use. You can load it later using Word2Vec.load()
model_name = "SBW-vectors-300-min5.txt"
model.save(model_name)

from gensim import models
model = models.Word2Vec.load(model_name)
# model = models.KeyedVectors.load_word2vec_format("SBW-vectors-300-min5.txt")

for word in model.wv.vocab:
    print(word)

model.wv.most_similar(positive=['woman', 'king'], negative=['man'])
model.wv.most_similar_cosmul(positive=['woman', 'king'], negative=['man'])


from decimal import Decimal
from django.conf import settings
from obp.models import Product
from django.shortcuts import get_object_or_404

class Cart(object):
    #Инициализация
    def __init__(self,request):
        #Инициализация корзины пользователя

        self.session = request.session
        self.session.set_expiry(0)
        #request.session.set_expiry(0)
        cart = self.session.get(settings.CART_SESSION_ID)
        if not cart:
            #Сохранение корзины в сессию
            cart = self.session[settings.CART_SESSION_ID] = {}
        self.cart = cart

    # Добавление товара в корзину пользователя
    def add(self, product):
        product_id = str(product.id)
        if product_id not in self.cart:
            self.cart[product_id] = {'quantity': 1,
                                     'price': str( product.get_price() ),
                                     }
        self.save()

    #Повышение количества товара в корзине на 1
    def plus_quantity(self, product):
        product_id = str(product.id)
        self.cart[product_id]['quantity'] += 1
        self.save()

    #Понижение количества товара в корзине на 1
    def minus_quantity(self, product):
        product_id = str(product.id)
        if (self.cart[product_id]['quantity'] > 1):
            self.cart[product_id]['quantity'] -= 1
        else:
            self.remove(product)
        self.save()

    def remove(self, product):
        product_id = str(product.id)
        if product_id in self.cart:
            del self.cart[product_id]
            self.save()

    # Сохранение данных в сессию
    def save(self):
        self.session[settings.CART_SESSION_ID] = self.cart
        # Указываем, что сессия изменена
        self.session.modified = True

    def clear(self):
        del self.session[settings.CART_SESSION_ID]
        self.session.modified = True
    # Итерация по товарам
    def __iter__(self):
        product_ids = self.cart.keys()
        products = Product.objects.filter(id__in=product_ids)
        for product in products:
            self.cart[str(product.id)]['product'] = product

        for item in self.cart.values():
            item['price'] = Decimal(item['price'])
            item['total_price'] = item['price'] * item['quantity']
            yield item

    # Количество товаров
    def __len__(self):
        return sum(item['quantity'] for item in self.cart.values())

    def get_total_price(self):
        return sum(Decimal(item['price']) * item['quantity'] for item in self.cart.values())

    def getCartList(self):
        cartList = []
        flag = False
        for product in self.cart:
            obj = get_object_or_404(Product, id = product)
            cartList.append({
            "id": product,
            "img_url": obj.image.url,
            "title": obj.title,
            "quantity": self.cart[product]['quantity'],
            "price": self.cart[product]['price'],
            "total_price": self.cart[product]['quantity'] * float(self.cart[product]['price'])
            })
            flag = True
        if flag:
            return cartList
        return False

    def getCartListId(self):
        cartList = []
        for product in self.cart:
            obj = get_object_or_404(Product, id = product)
            cartList.append(obj.id)
        return cartList