''' non-interactive pages '''
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator
from django.db.models import Q
from django.http import HttpResponseNotFound
from django.template.response import TemplateResponse
from django.utils import timezone
from django.utils.decorators import method_decorator
from django.views import View

from bookwyrm import forms, models
from bookwyrm.activitypub import ActivitypubResponse
from bookwyrm.settings import PAGE_LENGTH
from .helpers import get_activity_feed, get_user_from_username
from .helpers import is_api_request, is_bookwyrm_request, object_visible_to_user

# pylint: disable= no-self-use
@method_decorator(login_required, name='dispatch')
class Feed(View):
    ''' activity stream '''
    def get(self, request, tab):
        ''' user's homepage with activity feed '''
        try:
            page = int(request.GET.get('page', 1))
        except ValueError:
            page = 1

        if tab == 'home':
            activities = get_activity_feed(
                request.user, following_only=True)
        elif tab == 'local':
            activities = get_activity_feed(
                request.user, privacy=['public', 'followers'], local_only=True)
        else:
            activities = get_activity_feed(
                request.user, privacy=['public', 'followers'])
        paginated = Paginator(activities, PAGE_LENGTH)

        data = {**feed_page_data(request.user), **{
            'title': 'Updates Feed',
            'user': request.user,
            'activities': paginated.page(page),
            'tab': tab,
            'goal_form': forms.GoalForm(),
            'path': '/%s' % tab,
        }}
        return TemplateResponse(request, 'feed/feed.html', data)

@method_decorator(login_required, name='dispatch')
class DirectMessage(View):
    ''' dm view '''
    def get(self, request, username=None):
        ''' like a feed but for dms only '''
        try:
            page = int(request.GET.get('page', 1))
        except ValueError:
            page = 1

        queryset = models.Status.objects

        user = None
        if username:
            try:
                user = get_user_from_username(request.user, username)
            except models.User.DoesNotExist:
                pass
        if user:
            queryset = queryset.filter(Q(user=user) | Q(mention_users=user))

        activities = get_activity_feed(
            request.user, privacy=['direct'], queryset=queryset)

        paginated = Paginator(activities, PAGE_LENGTH)
        activity_page = paginated.page(page)
        data = {**feed_page_data(request.user), **{
            'title': 'Direct Messages',
            'user': request.user,
            'partner': user,
            'activities': activity_page,
            'path': '/direct-messages',
        }}
        return TemplateResponse(request, 'feed/direct_messages.html', data)

class Status(View):
    ''' get posting '''
    def get(self, request, username, status_id):
        ''' display a particular status (and replies, etc) '''
        try:
            user = get_user_from_username(request.user, username)
            status = models.Status.objects.select_subclasses().get(
                id=status_id, deleted=False)
        except ValueError:
            return HttpResponseNotFound()

        # the url should have the poster's username in it
        if user != status.user:
            return HttpResponseNotFound()

        # make sure the user is authorized to see the status
        if not object_visible_to_user(request.user, status):
            return HttpResponseNotFound()

        if is_api_request(request):
            return ActivitypubResponse(
                status.to_activity(pure=not is_bookwyrm_request(request)))

        data = {**feed_page_data(request.user), **{
            'title': 'Status by %s' % user.username,
            'status': status,
        }}
        return TemplateResponse(request, 'feed/status.html', data)

class Replies(View):
    ''' replies page (a json view of status) '''
    def get(self, request, username, status_id):
        ''' ordered collection of replies to a status '''
        # the html view is the same as Status
        if not is_api_request(request):
            status_view = Status.as_view()
            return status_view(request, username, status_id)

        # the json view is different than Status
        status = models.Status.objects.get(id=status_id)
        if status.user.localname != username:
            return HttpResponseNotFound()

        return ActivitypubResponse(status.to_replies(**request.GET))

def feed_page_data(user):
    ''' info we need for every feed page '''
    if not user.is_authenticated:
        return {}

    goal = models.AnnualGoal.objects.filter(
        user=user, year=timezone.now().year
    ).first()
    return {
        'suggested_books': get_suggested_books(user),
        'goal': goal,
        'goal_form': forms.GoalForm(),
    }

def get_suggested_books(user, max_books=5):
    ''' helper to get a user's recent books '''
    book_count = 0
    preset_shelves = [
        ('reading', max_books), ('read', 2), ('to-read', max_books)
    ]
    suggested_books = []
    for (preset, shelf_max) in preset_shelves:
        limit = shelf_max if shelf_max < (max_books - book_count) \
                else max_books - book_count
        shelf = user.shelf_set.get(identifier=preset)

        shelf_books = shelf.shelfbook_set.order_by(
            '-updated_date'
        ).all()[:limit]
        if not shelf_books:
            continue
        shelf_preview = {
            'name': shelf.name,
            'books': [s.book for s in shelf_books]
        }
        suggested_books.append(shelf_preview)
        book_count += len(shelf_preview['books'])
    return suggested_books


import numpy as np
from dynamixel.mt_dxl import DxlAPI
import matplotlib.pyplot as plt
import time

v_t = np.array([0 for i in range(12)])
p_t = np.array([0 for i in range(12)])
motor_group = DxlAPI(range(12), '/dev/ttyUSB0')
# position initialize
motor_group.set_operating_mode('p')
motor_group.torque_enable()
motor_group.set_position(np.array([0 for i in range(12)]))
time.sleep(6)
motor_group.torque_disable()
motor_group.set_operating_mode('t')
motor_group.torque_enable()
breaking_flag = 0
p_rec = []
v_rec = []
t_rec = []
calc_torque_rec = []
beta = np.array([[0.02], [0.01], [0.01], [0.02], [0.01], [0.01], [0.02], [0.01], [0.01], [0.02], [0.01], [0.01]])
# a = np.array([[2.0, 1.5, 7.0, 2.0, 1.5, 7.0, 2.0, 1.5, 7.0, 2.0, 1.5, 7.0]]) # cannot detach
a = np.array([[1.6, 1.5, 4.0, 1.6, 1.5, 4.0, 1.6, 1.5, 4.0, 1.6, 1.5, 4.0]])
b = np.array([[0.7, 0.8, 2.0, 0.7, 0.8, 2.0, 0.7, 0.8, 2.0, 0.7, 0.8, 2.0]])
TA = time.time()
for j in range(3):
    for i in range(10000):
        t0 = time.time()
        t_p = motor_group.get_torque()
        v_p = motor_group.get_velocity()  # 1xn
        p_p = motor_group.get_position()  # 1xn
        v_e = np.array([v_p - v_t]).T  # velocity error nx1
        p_e = np.array([p_p - p_t]).T  # position error nx1
        tra_diff = p_e + beta * v_e  # track difference error(t) nx1
        co_diff = a / (1 + b * np.linalg.norm(tra_diff) ** 2)  # gamma(t) 1xn
        ff = tra_diff / co_diff.T  # force F(t) nx1
        # p_gain = np.dot(ff, p_e.T)  # nxn
        # d_gain = np.dot(ff, v_e.T)  # nxn
        # p_gain = ff * p_e  # nx1
        # d_gain = ff * v_e  # nx1
        p_gain = np.array([[0.010, 0.02, 0.01, 0.010, 0.02, 0.01, 0.010, 0.02, 0.01, 0.010, 0.02, 0.01]]).T
        d_gain = np.array([[0.010, 0.02, 0.01, 0.010, 0.02, 0.01, 0.010, 0.02, 0.01, 0.010, 0.02, 0.01]]).T
        # calc_torque = (-ff - np.dot(p_gain, p_e) - np.dot(d_gain, v_e)).T[0]  # (nx1).T[0]
        calc_torque = (-ff - p_gain * p_e - d_gain * v_e).T[0]
        for t in calc_torque.tolist():
            if t > 5 or t < -5:
                motor_group.torque_disable()
                motor_group.portHandler.closePort()
                breaking_flag = 1
        if breaking_flag == 1:
            break
        motor_group.set_torque(calc_torque.tolist())
        v_rec.append(v_p)
        p_rec.append(p_p)
        t_rec.append(t_p)
        calc_torque_rec.append(calc_torque)
        t1 = time.time()
        if (0.01-(t1-t0)) > 0:
            time.sleep(0.01-(t1-t0))
        print("Total time: %d, time for one period: %f" % (j, t1-t0))
if breaking_flag == 0:
    motor_group.torque_disable()
    motor_group.portHandler.closePort()
else:
    print('torque too big!')
TB = time.time()
p_rec = np.array(p_rec)
v_rec = np.array(v_rec)
calc_torque_rec = np.array(calc_torque_rec)
t_rec = np.array(t_rec)
print(TB-TA)
plt.figure()
plt.title('position')
plt.plot(p_rec[:, 0], label='j0_present')
plt.plot(p_rec[:, 1], label='j1_present')
plt.plot(p_rec[:, 2], label='j2_present')
plt.legend()
plt.show()

plt.figure()
plt.title('calculate torque')
plt.plot(calc_torque_rec[:, 0], label='j0_present')
plt.plot(calc_torque_rec[:, 1], label='j1_present')
plt.plot(calc_torque_rec[:, 2], label='j2_present')
plt.legend()
plt.show()

plt.figure()
plt.title('present torque')
plt.plot(t_rec[:, 0], label='j0_present')
plt.plot(t_rec[:, 1], label='j1_present')
plt.plot(t_rec[:, 2], label='j2_present')
plt.legend()
plt.show()


# -*- coding: utf-8 -*-
import numpy as np

def somaLinhas(a):
    b=[]
    for i in range(0,a.shape[0],1):
        soma=0
        for j in range(0,a.shape[1],1):
            soma=soma+a[i,j]
        b.append(soma)
    return(b)

def semRepeticao(b):
    c=[]
    for i in range(0,len(b),1):
        if b[i] not in c:
            c.append(b[i])
    return(c)

def vezes(x,b):
    cont=0
    for i in range(0,len(b),1):
        if b[i]==x:
            cont=cont+1
    return(cont)

def somaErrada(c,b):
    somaE=c[0]
    for i in range(1,len(c),1):
        if vezes(c[i],b)<vezes(c[i-1],b):
            somaE=c[i]
    return(somaE)

def somaCerta(c,b):
    somaC=c[0]
    for i in range(1,len(c),1):
        if vezes(c[i],b)>vezes(c[i-1],b):
            somaC=c[i]
    return(somaC)

def linhaErro(d,a):
    for i in range(0,a.shape[0],1):
        soma=0
        for j in range(0,a.shape[1],1):
            soma=soma+a[i,j]
        if soma==d:
            return(i)

def colunaErro(d,a):
    for j in range(0,a.shape[1],1):
        soma=0
        for i in range(0,a.shape[0],1):
            soma=soma+a[i,j]
        if soma==d:
            return(j)

def numeroTrocado(diferenca,errado,d,e):
    trocado=0
    if d>e:
        trocado=errado-diferenca
    else:
        trocado=errado+diferenca
    return(trocado)

n=int(input('Digite a ordem da matriz: '))
a=np.zeros((n,n))
for i in range(0,a.shape[0],1):
    for j in range(0,a.shape[1],1):
        a[i,j]=int(input('Digite o valor do termo da matriz: '))

b=somaLinhas(a)
c=semRepeticao(b)
d=somaErrada(c,b)
e=somaCerta(c,b)
diferenca=abs(d-e)
x=linhaErro(d,a)
y=colunaErro(d,a)
errado=a[x,y]
trocado=numeroTrocado(diferenca,errado,d,e)
print('%.d'%trocado)
print('%.d'%errado)


def searchlist(arr):
    item = input("Input item in array:")
    if item.isdigit():
        item = int(item)
    for i in range(len(arr)):
        if arr[i] == item:
            return i
    return 0

array1 = [1, 2, 5, 44, 55, 66]
array2 = 'ghjkl'

print(searchlist(array1))
print(searchlist(array2))

array3 = input("Input sorted array 3: ")
# print(array3)
# print(searchlist(array3))


#!/usr/bin/env python3

from admissibles import admissibles, odd_ending_admissibles, LexBasisMonomial
from itertools import chain
from lambda_alg import AdmissibleTerm
from random import seed
from sys import argv, stderr

class CurtisTable(object):
    def __init__(self, max_t):
        self._table = [None] * (max_t - 1)
        for t in range(2, max_t + 1):
            self._table[t-2] = [None]*t
            for s in range(t):
                self._table[t-2][s] = dict()
        self._inverse_table = dict()
        self._max_t = max_t

    def __getitem__(self, key):
        try:
            if not isinstance(key, tuple):
                s = key.homological_degree()
                t = key.total_degree()
                return self._table[t-2][s-1][key]
            elif len(key) == 2:
                return self._table[key[1] - 2][key[0] - 1]
            elif len(key) == 3:
                return self._table[key[1] - 2][key[0] - 1][key[2]]
        except (TypeError, AttributeError) as e:
            raise TypeError('unsupported indexing in CurtisTable') from e
        except IndexError as e:
            raise IndexError('Index out of bounds in CurtisTable') from e
        except KeyError as e:
            raise KeyError("%s not found in CurtisTable" % (str(key[2]))) from e

    def __setitem__(self, key, value):
        try:
            if not isinstance(key, tuple):
                s = key.homological_degree()
                t = key.total_degree()
                self._table[t-2][s-1][key] = value
                if value is not None:
                    self._inverse_table[value] = key
            elif len(key) == 3:
                self._table[key[1] - 2][key[0] - 1][key[2]] = value
                if value is not None:
                    self._inverse_table[value] = key[2]
        except (AttributeError, TypeError) as e:
            raise TypeError('CurtisTable only supports integer indexing' % str(e)) from e
        except IndexError as e:
            raise IndexError('Index out of bounds in CurtisTable') from e

    def __delitem__(self, key):
        try:
            if not isinstance(key, tuple):
                s = key.homological_degree()
                t = key.total_degree()
                value = self._table[t-2][s-1][key]
                del self._table[t-2][s-1][key]
                if value is not None:
                    del self._inverse_table[value]
            elif len(key) == 3:
                value = self._table[key[1] - 2][key[0] - 1][key[2]]
                del self._table[key[1] - 2][key[0] - 1][key[2]]
                if value is not None:
                    del self._inverse_table[value]
        except (AttributeError, TypeError) as e:
            raise TypeError('CurtisTable only supports integer indexing' % str(e)) from e
        except IndexError as e:
            raise IndexError('Index out of bounds in CurtisTable') from e

    def __contains__(self, monomial):
        s = monomial.homological_degree()
        t = monomial.total_degree()
        return monomial in self._table[t-2][s-1]

    def bounds(self):
        return tuple(max_t, max_t)

    def lookup_by_tag(self, tag):
        return self._inverse_table[tag]

    def invisible_tag(self, monomial):
        s = monomial.homological_degree()
        suffix_degree = monomial[s-1] + 1
        for i in range(s-2, 0, -1):
            suffix = monomial[i:]
            suffix_degree += 1 + monomial[i]
            suffix_s = s - i
            try:
                suffix_tag = self[suffix_s, suffix_degree, suffix]
                if suffix_tag is None:
                    continue
                if 2*monomial[i-1] >= suffix_tag[0]:
                    return monomial[:i] * suffix_tag
            except KeyError:
                pass
        return None

    def tags_invisibly(self, monomial):
        for i in range(len(monomial)-1, 0, -1):
            suffix = monomial[i:]
            try:
                suffix_taggee = self.lookup_by_tag(suffix)
                if 2*monomial[i-1] >= suffix_taggee[0]:
                    return monomial[:i] * suffix_taggee
            except KeyError:
                pass
        return None

    def is_cycle_product(self, monomial):
        if len(monomial) < 2:
            return False
        for i in range(len(monomial) - 1):
            if monomial[0:i] in self and monomial[i:] in self:
                return True
        else:
            return False

    def curtis_shortcut(self, monomial):
        if len(monomial) < 2:
            return None
        twice_i, j = monomial[:2]
        if twice_i % 2 == 1:
            return None
        possible_taggee = LexBasisMonomial(twice_i-1, j-1, 1) * monomial[2:]
        if possible_taggee in self and self[possible_taggee] is None:
            return possible_taggee

def log_output(s):
    stderr.write(s)
    stderr.flush()

# courtesy of Curtis-Goerss-Mahowald-Milgram
def dynamic_ehp_algorithm(max_r):
    curtis_table = CurtisTable(2*max_r)

    for k in range((max_r+1) // 2):
        curtis_table[1, 2*k+2, LexBasisMonomial(2*k+1)] = None

    def div2ceil(n):
        return (n + n%2)//2

    dele = list()
    untagged = set()
    min_untagged = None
    def tag_and_remove(s, t, lt, tag):
        nonlocal min_untagged, untagged, dele
        curtis_table[s+1, t, lt] = tag
        dele.append(tag)
        if lt in untagged:
            untagged.remove(lt)
            if lt == min_untagged:
                min_untagged = min(untagged) if untagged else None

    for r in range(2, max_r+1):
        for s in range(1, r):
            t = r-2 + s
            for monomial, tag in curtis_table[s, t].items():
                lower_bound = div2ceil(monomial[0])
                if tag is None:
                    upper_bound = max_r - monomial.internal_degree()
                else:
                    upper_bound = min(max_r - monomial.internal_degree(), div2ceil(tag[0])-1)
                for m in range(lower_bound, upper_bound + 1):
                    new_monomial = LexBasisMonomial(m) * monomial
                    curtis_table[s+1, t+m+1, new_monomial] = None

        for s in range(1, r+1):
            t = s+r
            log_output("Processing box (%d, %d) " % (s, t))
            count = steps = 0
            dele = list()
            untagged = set(curtis_table[s+1, t])
            min_untagged = min(untagged) if untagged else None
            for monomial in sorted(curtis_table[s, t]):
                count += 1
                if min_untagged is None or monomial < min_untagged:
                    log_output('d')
                    continue
                if curtis_table.is_cycle_product(monomial):
                    log_output('h')
                    continue
                shortcut = curtis_table.curtis_shortcut(monomial)
                if shortcut is not None:
                    tag_and_remove(s, t, shortcut, monomial)
                    log_output('s')
                    continue
                diff = AdmissibleTerm([monomial]).differential(min_untagged)
                while diff and diff.leading_term() >= min_untagged:
                    steps += 1
                    lt = diff.leading_term()
                    if lt in curtis_table[s+1, t]:
                        prev_tag = curtis_table[s+1, t, lt]
                    else:
                        prev_tag = curtis_table.invisible_tag(lt)

                    if prev_tag is None:
                        tag_and_remove(s, t, lt, monomial)
                        log_output('.')
                        break

                    diff += AdmissibleTerm([prev_tag]).differential(min_untagged)
                else:
                    log_output('c')
            for d in dele:
                del curtis_table[s, t, d]
            if count > 0:
                log_output(" (%d monomials, %d steps)\n" % (count, steps))
            else:
                log_output('\n')
    return curtis_table

if __name__ == '__main__':
    try:
        seed()
        max_r = int(argv[1])
        if max_r < 2:
            raise ValueError
        curtis_table = dynamic_ehp_algorithm(max_r)
        print("In the following table, P represents the sequence 2, 4, 1, 1:")
        for r in range(1, max_r):
            for s in range(1, r+1):
                t = r + s
                first_line = (str(r) + ', ' + str(s)).ljust(8)
                if curtis_table[s, t]:
                    for cycle, tag in sorted(curtis_table[s, t].items()):
                        print(first_line + str(cycle) + ' / ' + str(tag))
                        first_line = ' ' * 8
    except (IndexError, ValueError) as e:
        print(str(e))
        print("Usage: ./curtis_lto <homological degree>")


# -*- coding: utf-8 -*-
# @Author: Teiei
# @Date:   2018-05-20 09:45:06
# @Last Modified by:   Teiei
# @Last Modified time: 2018-05-20 12:10:01

import numpy as np
my_arr = np.arange(1000000)  ## 注意这里是arrage 而不是range
my_list = list(range(1000000))
## np.arange的速度要快很多 ！
get_ipython().run_line_magic('time', 'for _ in range(10): my_arr2 = my_arr * 2')	# rbguo added 20180520 这里的_相当于i
get_ipython().run_line_magic('time', 'for _ in range(10): my_list2 = [x * 2 for x in my_list]')


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

# Write a Python program that matches a string that has an a followed by 3 b's

import re

txt = "I am ab string with abbsolutely no abbbreviation at all abbbb"

expression = r"(.*)ab{2,3}"

tableau = txt.split(' ')

for word in tableau:
	if re.match(expression, word):
		print(word)


from socket import *

def main():
    # 1.创建套接字,绑定
    tcp_server_socket = socket(AF_INET, SOCK_STREAM)
    tcp_server_socket.bind(("", 60000))
    # 2.设置套接字为接收模式
    tcp_server_socket.listen(128)
    while True:
        # 3.监听端口，存链接的套接字
        print("等待接受数据")
        client_socket, client_addr = tcp_server_socket.accept()
        # 4.接受客户端请求
        print("一个客户链接成功：%s" % str(client_addr))
        while True:
            recv_data = client_socket.recv(1024)
            if not recv_data:
                break
            print("客户:" + recv_data.decode("utf-8"))
            # 5.回复
            client_socket.send("收到".encode("gbk"))
        # 6.关闭两个套接字
        print("服务完毕")
        client_socket.close()

    print("客户端关闭")
    tcp_server_socket.close()

if __name__ == "__main__":
    main()


class GiovaniFelipeBenatto(object):
    """Giovani Felipe Benatto has interest to work and become a python developer.
       However In this moment he is 'enjoying' washing dishes at a restaurant
       in Brighton.
    """

    def __init__(self):
        self.type = "human"
        self.interests = ["learning", "coding", "python", "linux"]

    def contact(self):
        self.email = "torlax@gmail.com"
        self.linkedin = "https://www.linkedin.com/in/torlax/"
        self.github = "https://github.com/torlax"

    def experience(self):
        """Most of my experience is in Customer Success area, however
        now I really want to code in python"""

        jobs = {}
        jobs["idmax"] = {
            "when": "March 2018 - May 2018",
            "role": "Customer Success",
            "tasks": """Helping customers improve data and market analysis
                        by monitoring metrics and profile analysis""",
        }

        jobs["Nexxera"] = {
            "when": "Dec 2013 - Oct 2017",
            "role": "Support Analyst",
            "tasks": """Customer and bank support on banking systems and archives
                        of Nexxera web portals, analysis and maintenance of data
                        transfer systems""",
            "tools": ["linux", "ssh", "jira"],
        }

        jobs["teledata"] = {
            "when": "Nov 2010 - Apr 2011",
            "role": "Support Analyst",
            "tasks": """Analyst of Microinformatics, providing support to the
                        Windows system, bank payment and collection systems""",
            "weapons": ["windows", "linux"],
        }

        jobs["unisys"] = {
            "when": "May 2009 - Sep 2010",
            "role": "Support Analyst",
            "tasks": """Work performed as a Support Analyst, with installation,
                        configuration and training of Ipiranga (Ultra) systems for
                        Point of Sale and Accounting""",
        }

        return jobs

    def certification(self):
        certs = []
        lpi = {
            "title": "LPIC-1",
            "description": "Linux Professional Institute",
            "link": "https://cs.lpi.org/caf/Xamman/certification/verify/LPI000406113/nxm2dasx4p",
        }
        certs.append(lpi)

        return certs

if __name__ == "__main__":
    gfb = GiovaniFelipeBenatto()
    print(gfb.experience())
    print(gfb.certification())


from __future__ import print_function
import os
import sys
# import uuid
import time
import math
import types
import signal
import shutil
import timeit
import numbers
import subprocess
import tempfile
import contextlib
import platform
import argparse
from multiprocessing import cpu_count, Lock, current_process
from collections import OrderedDict, deque, Iterable, Iterator
from itertools import islice, tee, chain

from six import string_types
from six.moves.urllib.request import urlopen
from six.moves.urllib.error import URLError, HTTPError
import tarfile

try:
    from numba import jit, autojit, vectorize, guvectorize
except:
    pass
import numpy
import six

from .mpi import SelfIterator, segment_list, SharedCounter
from .profile import *
from . import mpi
from . import shape_calculation

# ===========================================================================
# Basics
# ===========================================================================
def is_path(path):
    if isinstance(path, str):
        path = os.path.abspath(path)
        if os.path.exists(path):
            #the file is there
            return True
        elif os.access(os.path.dirname(path), os.W_OK):
            #the file does not exists but write privileges are given
            return True
    return False

def is_string(s):
    return isinstance(s, string_types)

def is_number(i):
    return isinstance(i, numbers.Number)

def is_bool(b):
    return isinstance(b, type(True))

def iter_chunk(it, n):
    """ Chunking an iterator into small chunk of size `n`
    Note: this can be used to slice data into mini batches
    """
    if not isinstance(it, Iterator):
        it = iter(it)
    obj = list(islice(it, n))
    while obj:
        yield obj
        obj = list(islice(it, n))

def batching(n, batch_size):
    return [(i, min(i + batch_size, n))
            for i in range(0, n + batch_size, batch_size) if i < n]

# ===========================================================================
# Others
# ===========================================================================
def raise_return(e):
    raise e

class _LogWrapper():

    def __init__(self, stream):
        self.stream = stream

    def write(self, message):
        # no backtrack for writing to file
        self.stream.write(message.replace("\b", ''))
        sys.__stdout__.write(message)

    def flush(self):
        self.stream.flush()
        sys.__stdout__.flush()

    def close(self):
        try:
            self.stream.close()
        except:
            pass

def stdio(path=None, suppress=False, stderr=True):
    """
    Parameters
    ----------
    path: None, str
        if str, specified path for saving all stdout (and stderr)
        if None, reset stdout and stderr back to normal
    suppress: boolean
        totally turn-off all stdout (and stdeer)
    stderr:
        apply output file with stderr also

    """
    # turn off stdio
    if suppress:
        f = open(os.devnull, "w")
    # reset
    elif path is None:
        f = None
    # redirect to a file
    else:
        f = _LogWrapper(open(path, "w"))
    # ====== assign stdio ====== #
    if f is None: # reset
        if isinstance(sys.stdout, _LogWrapper):
            sys.stdout.close()
        if isinstance(sys.stderr, _LogWrapper):
            sys.stderr.close()
        sys.stdout = sys.__stdout__
        sys.stderr = sys.__stderr__
    else: # redirect to file
        sys.stdout = f
        if stderr:
            sys.stderr = f

_uuid_chars = list(chain(map(chr, range(65, 91)),  # ABCD
                         map(chr, range(97, 123)),  # abcd
                         map(chr, range(48, 57)))) # 0123
_uuid_random_state = numpy.random.RandomState(int(str(int(time.time() * 100))[3:]))

def uuid():
    """ Generate random UUID 8 characters with very very low collision """
    # m = time.time()
    # uniqid = '%8x%4x' % (int(m), (m - int(m)) * 1000000)
    # uniqid = str(uuid.uuid4())[:8]
    uniquid = ''.join(_uuid_random_state.choice(_uuid_chars,
                                          size=8, replace=True))
    return uniquid

@contextlib.contextmanager
def change_recursion_limit(limit):
    """Temporarily changes the recursion limit."""
    old_limit = sys.getrecursionlimit()
    if old_limit < limit:
        sys.setrecursionlimit(limit)
    yield
    sys.setrecursionlimit(old_limit)

@contextlib.contextmanager
def signal_handling(sigint=None, sigtstp=None, sigquit=None):
    # We cannot handle SIGTERM, because it prevent subproces from
    # .terminate()
    orig_int = signal.getsignal(signal.SIGINT)
    orig_tstp = signal.getsignal(signal.SIGTSTP)
    orig_quit = signal.getsignal(signal.SIGQUIT)

    if sigint is not None: signal.signal(signal.SIGINT, sigint)
    if sigtstp is not None: signal.signal(signal.SIGTSTP, sigtstp)
    if sigquit is not None: signal.signal(signal.SIGQUIT, sigquit)

    yield
    # reset
    signal.signal(signal.SIGINT, orig_int)
    signal.signal(signal.SIGTSTP, orig_tstp)
    signal.signal(signal.SIGQUIT, orig_quit)

# ===========================================================================
# UniqueHasher
# ===========================================================================
class UniqueHasher(object):
    """ This hash create strictly unique hash value by using
    its memory to remember which key has been assigned

    Note
    ----
    This function use deterministic hash, which give the same
    id for all labels, whenever you call it
    """

    def __init__(self, nb_labels=None):
        super(UniqueHasher, self).__init__()
        self.nb_labels = nb_labels
        self._memory = {} # map: key -> hash_key
        self._current_hash = {} # map: hash_key -> key

    def hash(self, value):
        key = abs(hash(value))
        if self.nb_labels is not None:
            key = key % self.nb_labels
        # already processed hash
        if value in self._current_hash:
            return self._current_hash[value]
        # not yet processed
        if key in self._memory:
            if self.nb_labels is not None and \
                len(self._memory) >= self.nb_labels:
                raise Exception('All %d labels have been assigned, outbound value:"%s"' %
                                (self.nb_labels, value))
            else:
                while key in self._memory:
                    key += 1
                    if self.nb_labels is not None and key >= self.nb_labels:
                        key = 0
        # key not in memory
        self._current_hash[value] = key
        self._memory[key] = value
        return key

    def __call__(self, value):
        return self.hash(value)

    def map(self, order, array):
        """ Re-order an ndarray to new column order """
        order = as_tuple(order)
        # get current order
        curr_order = self._current_hash.items()
        curr_order.sort(key=lambda x: x[1])
        curr_order = [i[0] for i in curr_order]
        # new column order
        order = [curr_order.index(i) for i in order]
        return array[:, order]

# ===========================================================================
# ArgCOntrol
# ===========================================================================
class ArgController(object):
    """ Simple interface to argparse """

    def __init__(self, version='1.00', print_parsed=True):
        super(ArgController, self).__init__()
        self.parser = None
        self._require_input = False
        self.arg_dict = {}
        self.name = []
        self.version = str(version)
        self.print_parsed = print_parsed

    def _is_positional(self, name):
        if name[0] != '-' and name[:2] != '--':
            return True
        return False

    def _parse_input(self, key, val):
        # ====== search if manual preprocessing available ====== #
        for i, preprocess in self.arg_dict.iteritems():
            if key in i and preprocess is not None:
                return preprocess(val)
        # ====== auto preprocess ====== #
        try:
            val = float(val)
            if int(val) == val:
                val = int(val)
        except:
            val = str(val)
        return val

    def add(self, name, help, default=None, preprocess=None):
        """ NOTE: if the default value is not given, the argument is
        required

        Parameters
        ----------
        name: str
            [-name] for optional parameters
            [name] for positional parameters
        help: str
            description of the argument
        default: str
            default value for the argument
        preprocess: callable
            take in the parsed argument and preprocess it into necessary
            information
        """
        if self.parser is None:
            self.parser = argparse.ArgumentParser(
                description='Automatic argument parser (yes,true > True; no,false > False)',
                version=self.version, add_help=True)
        # ====== NO default value ====== #
        if default is None:
            if self._is_positional(name):
                self.parser.add_argument(name, help=help, type=str, action="store",
                    metavar='')
            else:
                self.parser.add_argument(name, help=help, type=str, action="store",
                    required=True, metavar='')
            self._require_input = True
        # ====== boolean default value ====== #
        elif isinstance(default, bool):
            help += ' (default: %s)' % str(default)
            self.parser.add_argument(name, help=help,
                                     action="store_%s" % str(not default).lower())
            preprocess = lambda x: bool(x)
        # ====== add defaults value ====== #
        else:
            help += ' (default: %s)' % str(default)
            self.parser.add_argument(name, help=help, type=str, action="store",
                default=str(default), metavar='')

        # store preprocess dictionary
        if not callable(preprocess):
            preprocess = None
        self.arg_dict[name] = preprocess
        return self

    def parse(self):
        if self.parser is None:
            raise Exception('Call add to assign at least 1 argument for '
                            'for the function.')
        # TODO fix bug here
        exit_now = False
        try:
            if len(sys.argv) == 1 and self._require_input:
                self.parser.print_help()
                exit_now = True
            else:
                args = self.parser.parse_args()
        except Exception as e:
            # if specfy version or help, don't need to print anything else
            if all(i not in ['-h', '--help', '-v', '--version']
                   for i in sys.argv):
                self.parser.print_help()
            exit_now = True
        if exit_now: exit()
        # parse the arguments
        try:
            args = {i: self._parse_input(i, j)
                    for i, j in args._get_kwargs()}
        except Exception as e:
            print('Error parsing given arguments: "%s"' % str(e))
            self.parser.print_help()
            exit()
        # reset everything
        self.parser = None
        self.arg_dict = {}
        self._require_input = False
        # print the parsed arguments if necessary
        if self.print_parsed:
            max_len = max(len(i) for i in args.keys())
            max_len = '%-' + str(max_len) + 's'
            print('\n******** Parsed arguments ********')
            for i, j in args.iteritems():
                print(max_len % i, ': ', j)
            print('**********************************\n')
        return args

# ===========================================================================
# Simple math and processing
# ===========================================================================
def one_hot(y, n_classes=None):
    '''Convert class vector (integers from 0 to nb_classes)
    to binary class matrix, for use with categorical_crossentropy
    '''
    y = numpy.asarray(y, dtype='int32')
    if not n_classes:
        n_classes = numpy.max(y) + 1
    Y = numpy.zeros((len(y), n_classes))
    for i in range(len(y)):
        Y[i, y[i]] = 1.
    return Y

def pad_sequences(sequences, maxlen=None, dtype='int32',
                  padding='pre', truncating='pre', value=0.,
                  transformer=None):
    """Pads each sequence to the same length:
    the length of the longest sequence.

    If maxlen is provided, any sequence longer
    than maxlen is truncated to maxlen.
    Truncation happens off either the beginning (default) or
    the end of the sequence.

    Supports post-padding and pre-padding (default).

    Parameters
    ----------
    sequences: list
        a list that contains a list of object
    maxlen: int
        maximum length of each individual sequence
    dtype: np.dtype
        desire data type of output array
    padding: 'pre' or 'post'
        pad either before or after each sequence.
    truncating: 'pre' or 'post'
        remove values from sequences larger than maxlen either
        in the beginning or in the end of the sequence
    value: object
        padding object
    transformer: callable
        a function transform each element in sequence into desire value
        (e.g. a dictionary)

    Returns
    -------
    numpy array with dimensions (number_of_sequences, maxlen)
    """
    # ====== check valid input ====== #
    if truncating not in ('pre', 'post'):
        raise ValueError('truncating must be "pre" or "post", given value is %s'
                         % truncating)
    if padding not in ('pre', 'post'):
        raise ValueError('padding must be "pre" or "post", given value is %s'
                         % padding)
    if transformer is None:
        transformer = lambda x: x
    if not callable(transformer):
        raise ValueError('transformer must be callable, but given value is %s' %
                         type(transformer))
    # ====== processing ====== #
    if maxlen is None:
        maxlen = int(max(len(s) for s in sequences))
    nb_samples = len(sequences)
    value = numpy.cast[dtype](value)
    X = numpy.full(shape=(nb_samples, maxlen), fill_value=value, dtype=dtype)
    for idx, s in enumerate(sequences):
        s = [transformer(_) for _ in s]
        if len(s) == 0: continue # empty list
        # check truncating
        if len(s) >= maxlen:
            slice_ = slice(None, None)
            s = s[-maxlen:] if truncating == 'pre' else s[:maxlen]
        # check padding
        elif len(s) < maxlen:
            slice_ = slice(-len(s), None) if padding == 'pre' else slice(None, len(s))
        # assign value
        X[idx, slice_] = numpy.asarray(s, dtype=dtype)
    return X

def pad_center(data, size, axis=-1, **kwargs):
    '''Wrapper for numpy.pad to automatically center an array prior to padding.
    This is analogous to `str.center()`

    Examples
    --------
    >>> # Generate a vector
    >>> data = numpy.ones(5)
    >>> librosa.util.pad_center(data, 10, mode='constant')
    array([ 0.,  0.,  1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.])

    >>> # Pad a matrix along its first dimension
    >>> data = numpy.ones((3, 5))
    >>> librosa.util.pad_center(data, 7, axis=0)
    array([[ 0.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  0.],
           [ 1.,  1.,  1.,  1.,  1.],
           [ 1.,  1.,  1.,  1.,  1.],
           [ 1.,  1.,  1.,  1.,  1.],
           [ 0.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  0.]])
    >>> # Or its second dimension
    >>> librosa.util.pad_center(data, 7, axis=1)
    array([[ 0.,  1.,  1.,  1.,  1.,  1.,  0.],
           [ 0.,  1.,  1.,  1.,  1.,  1.,  0.],
           [ 0.,  1.,  1.,  1.,  1.,  1.,  0.]])

    Parameters
    ----------
    data : numpy.ndarray
        Vector to be padded and centered

    size : int >= len(data) [scalar]
        Length to pad `data`

    axis : int
        Axis along which to pad and center the data

    kwargs : additional keyword arguments
      arguments passed to `numpy.pad()`

    Returns
    -------
    data_padded : numpy.ndarray
        `data` centered and padded to length `size` along the
        specified axis

    Raises
    ------
    ParameterError
        If `size < data.shape[axis]`

    See Also
    --------
    numpy.pad
    '''
    kwargs.setdefault('mode', 'constant')

    n = data.shape[axis]

    lpad = int((size - n) // 2)

    lengths = [(0, 0)] * data.ndim
    lengths[axis] = (lpad, int(size - n - lpad))

    if lpad < 0:
        raise ValueError(('Target size ({:d}) must be '
                          'at least input size ({:d})').format(size,
                                                               n))

    return numpy.pad(data, lengths, **kwargs)

def framing(y, frame_length=2048, hop_length=512):
    '''Slice a time series into overlapping frames.

    This implementation uses low-level stride manipulation to avoid
    redundant copies of the time series data.

    Parameters
    ----------
    y : np.ndarray [shape=(n,)]
        Time series to frame. Must be one-dimensional and contiguous
        in memory.

    frame_length : int > 0 [scalar]
        Length of the frame in samples

    hop_length : int > 0 [scalar]
        Number of samples to hop between frames

    Returns
    -------
    y_frames : np.ndarray [shape=(frame_length, N_FRAMES)]
        An array of frames sampled from `y`:
        `y_frames[i, j] == y[j * hop_length + i]`

    Raises
    ------
    ParameterError
        If `y` is not contiguous in memory, framing is invalid.
        See `np.ascontiguous()` for details.

        If `hop_length < 1`, frames cannot advance.

    Examples
    --------
    Extract 2048-sample frames from `y` with a hop of 64 samples per frame

    >>> y, sr = librosa.load(librosa.util.example_audio_file())
    >>> librosa.util.frame(y, frame_length=2048, hop_length=64)
    array([[ -9.216e-06,   7.710e-06, ...,  -2.117e-06,  -4.362e-07],
           [  2.518e-06,  -6.294e-06, ...,  -1.775e-05,  -6.365e-06],
           ...,
           [ -7.429e-04,   5.173e-03, ...,   1.105e-05,  -5.074e-06],
           [  2.169e-03,   4.867e-03, ...,   3.666e-06,  -5.571e-06]], dtype=float32)

    '''

    if len(y) < frame_length:
        raise ValueError('Buffer is too short (n={:d})'
                         ' for frame_length={:d}'.format(len(y), frame_length))

    if hop_length < 1:
        raise ValueError('Invalid hop_length: {:d}'.format(hop_length))

    if not y.flags['C_CONTIGUOUS']:
        raise ParameterError('Input buffer must be contiguous.')

    # Compute the number of frames that will fit. The end may get truncated.
    n_frames = 1 + int((len(y) - frame_length) / hop_length)

    # Vertical stride is one sample
    # Horizontal stride is `hop_length` samples
    y_frames = numpy.lib.stride_tricks.as_strided(y,
        shape=(frame_length, n_frames),
        strides=(y.itemsize, hop_length * y.itemsize))
    return y_frames

def segment_axis(a, frame_length=2048, hop_length=512, axis=0,
                 end='cut', endvalue=0, endmode='post'):
    """Generate a new array that chops the given array along the given axis
    into overlapping frames.

    This method has been implemented by Anne Archibald,
    as part of the talk box toolkit
    example::

        segment_axis(arange(10), 4, 2)
        array([[0, 1, 2, 3],
           ( [2, 3, 4, 5],
             [4, 5, 6, 7],
             [6, 7, 8, 9]])

    Parameters
    ----------
    a: numpy.ndarray
        the array to segment
    frame_length: int
        the length of each frame
    hop_length: int
        the number of array elements by which the frames should overlap
    axis: int, None
        the axis to operate on; if None, act on the flattened array
    end: 'cut', 'wrap', 'pad'
        what to do with the last frame, if the array is not evenly
            divisible into pieces. Options are:
            - 'cut'   Simply discard the extra values
            - 'wrap'  Copy values from the beginning of the array
            - 'pad'   Pad with a constant value
    endvalue: int
        the value to use for end='pad'
    endmode: 'pre', 'post'
        if "pre", padding or wrapping at the beginning of the array.
        if "post", padding or wrapping at the ending of the array.

    Return
    ------
    a ndarray

    The array is not copied unless necessary (either because it is unevenly
    strided and being flattened or because end is set to 'pad' or 'wrap').

    Note
    ----
    Modified work and error fixing Copyright (c) TrungNT

    """
    if axis is None:
        a = numpy.ravel(a) # may copy
        axis = 0

    length = a.shape[axis]
    overlap = frame_length - hop_length

    if overlap >= frame_length:
        raise ValueError("frames cannot overlap by more than 100%")
    if overlap < 0 or frame_length <= 0:
        raise ValueError("overlap must be nonnegative and length must" +
                         "be positive")

    if length < frame_length or (length - frame_length) % (frame_length - overlap):
        if length > frame_length:
            roundup = frame_length + (1 + (length - frame_length) // (frame_length - overlap)) * (frame_length - overlap)
            rounddown = frame_length + ((length - frame_length) // (frame_length - overlap)) * (frame_length - overlap)
        else:
            roundup = frame_length
            rounddown = 0
        assert rounddown < length < roundup
        assert roundup == rounddown + (frame_length - overlap) \
        or (roundup == frame_length and rounddown == 0)
        a = a.swapaxes(-1, axis)

        if end == 'cut':
            a = a[..., :rounddown]
        elif end in ['pad', 'wrap']: # copying will be necessary
            s = list(a.shape)
            s[-1] = roundup
            b = numpy.empty(s, dtype=a.dtype)
            # pre-padding
            if endmode == 'pre':
                b[..., :length] = a
                if end == 'pad':
                    b[..., length:] = endvalue
                elif end == 'wrap':
                    b[..., length:] = a[..., :roundup - length]
            # post-padding
            elif endmode == 'post':
                b[..., -length:] = a
                if end == 'pad':
                    b[..., :(roundup - length)] = endvalue
                elif end == 'wrap':
                    b[..., :(roundup - length)] = a[..., :roundup - length]
            a = b
        a = a.swapaxes(-1, axis)
        length = a.shape[0] # update length

    if length == 0:
        raise ValueError("Not enough data points to segment array " +
                "in 'cut' mode; try 'pad' or 'wrap'")
    assert length >= frame_length
    assert (length - frame_length) % (frame_length - overlap) == 0
    n = 1 + (length - frame_length) // (frame_length - overlap)
    s = a.strides[axis]
    newshape = a.shape[:axis] + (n, frame_length) + a.shape[axis + 1:]
    newstrides = a.strides[:axis] + ((frame_length - overlap) * s, s) + a.strides[axis + 1:]

    try:
        return numpy.ndarray.__new__(numpy.ndarray, strides=newstrides,
                                  shape=newshape, buffer=a, dtype=a.dtype)
    except TypeError:
        a = a.copy()
        # Shape doesn't change but strides does
        newstrides = a.strides[:axis] + ((frame_length - overlap) * s, s) \
        + a.strides[axis + 1:]
        return numpy.ndarray.__new__(numpy.ndarray, strides=newstrides,
                                     shape=newshape, buffer=a, dtype=a.dtype)

def as_shape_tuple(shape):
    if is_number(shape):
        shape = (int(shape),)
    if not isinstance(shape, (tuple, list)):
        raise ValueError('We only accept shape in tuple or list form.')
    shape = tuple([int(i) if i is not None and i >= 0 else None for i in shape])
    if len([i for i in shape if i is None]) >= 2:
        raise Exception('Shape tuple can only have 1 unknown dimension.')
    return shape

def as_tuple(x, N=None, t=None):
    """
    Coerce a value to a tuple of given length (and possibly given type).

    Parameters
    ----------
    x : value or iterable
    N : integer
        length of the desired tuple
    t : type, optional
        required type for all elements

    Returns
    -------
    tuple
        ``tuple(x)`` if `x` is iterable, ``(x,) * N`` otherwise.

    Raises
    ------
    TypeError
        if `type` is given and `x` or any of its elements do not match it
    ValueError
        if `x` is iterable, but does not have exactly `N` elements

    Note
    ----
    This function is adpated from Lasagne
    Original work Copyright (c) 2014-2015 lasagne contributors
    All rights reserved.

    LICENSE: https://github.com/Lasagne/Lasagne/blob/master/LICENSE
    """
    if not isinstance(x, tuple):
        if isinstance(x, (types.GeneratorType, types.ListType)):
            x = tuple(x)
        else:
            x = (x,)
    # ====== check length ====== #
    if is_number(N):
        N = int(N)
        if len(x) == 1:
            x = x * N
        elif len(x) != N:
            raise ValueError('x has length=%d, but required length N=%d' %
                             (len(x), N))
    # ====== check type ====== #
    if (t is not None) and not all(isinstance(v, t) for v in x):
        raise TypeError("expected a single value or an iterable "
                        "of {0}, got {1} instead".format(t.__name__, x))
    return x

def as_list(x, N=None, t=None):
    return list(as_tuple(x, N, t))

# ===========================================================================
# Python
# ===========================================================================
class struct(object):

    '''Flexible object can be assigned any attribtues'''

    def __getitem__(self, x):
        return getattr(self, str(x))

class bidict(dict):
    """ Bi-directional dictionary (i.e. a <-> b)
    Note
    ----
    When you iterate over this dictionary, it will be a doubled size
    dictionary
    """

    def __init__(self, *args, **kwargs):
        super(bidict, self).__init__(*args, **kwargs)
        # this is duplication
        self._inv = dict()
        for i, j in self.items():
            self._inv[j] = i

    @property
    def inv(self):
        return self._inv

    def __setitem__(self, key, value):
        super(bidict, self).__setitem__(key, value)
        self._inv[value] = key
        return None

    def __getitem__(self, key):
        if key not in self:
            return self._inv[key]
        return super(bidict, self).__getitem__(key)

    def update(self, *args, **kwargs):
        for k, v in dict(*args, **kwargs).iteritems():
            self[k] = v
            self._inv[v] = k

    def __delitem__(self, key):
        del self._inv[super(bidict, self).__getitem__(key)]
        return dict.__delitem__(self, key)

class queue(object):

    """ FIFO, fast, NO thread-safe queue
    put : append to end of list
    append : append to end of list
    pop : remove data from end of list
    get : remove data from end of list
    empty : check if queue is empty
    clear : remove all data in queue
    """

    def __init__(self):
        super(queue, self).__init__()
        self._data = []
        self._idx = 0

    # ====== queue ====== #
    def put(self, value):
        self._data.append(value)

    def append(self, value):
        self._data.append(value)

    # ====== dequeue ====== #
    def pop(self):
        if self._idx == len(self._data):
            raise ValueError('Queue is empty')
        self._idx += 1
        return self._data[self._idx - 1]

    def get(self):
        if self._idx == len(self._data):
            raise ValueError('Queue is empty')
        self._idx += 1
        return self._data[self._idx - 1]

    # ====== dqueue with default ====== #
    def pop_default(self, default=None):
        if self._idx == len(self._data):
            return default
        self._idx += 1
        return self._data[self._idx - 1]

    def get_default(self, default=None):
        if self._idx == len(self._data):
            return default
        self._idx += 1
        return self._data[self._idx - 1]

    def empty(self):
        if self._idx == len(self._data):
            return True
        return False

    def clear(self):
        del self._data
        self._data = []
        self._idx = 0

    def __len__(self):
        return len(self._data) - self._idx

class Progbar(object):

    '''
    This function is adpated from: https://github.com/fchollet/keras
    Original work Copyright (c) 2014-2015 keras contributors
    Modified work Copyright 2016-2017 TrungNT
    '''

    def __init__(self, target, title=''):
        '''
            @param target: total number of steps expected
        '''
        self.width = 39
        self.target = target
        self.sum_values = {}
        self.unique_values = []
        self.start = time.time()
        self.total_width = 0
        self.seen_so_far = 0
        self.title = title

    def update(self, current, values=[]):
        '''
            @param current: index of current step
            @param values: list of tuples (name, value_for_last_step).
            The progress bar will display averages for these values.
        '''
        for k, v in values:
            if k not in self.sum_values:
                self.sum_values[k] = [v * (current - self.seen_so_far), current - self.seen_so_far]
                self.unique_values.append(k)
            else:
                self.sum_values[k][0] += v * (current - self.seen_so_far)
                self.sum_values[k][1] += (current - self.seen_so_far)
        self.seen_so_far = current

        now = time.time()

        prev_total_width = self.total_width
        sys.stdout.write("\b" * prev_total_width)
        sys.stdout.write("\r")

        numdigits = int(numpy.floor(numpy.log10(self.target))) + 1
        barstr = '%s %%%dd/%%%dd [' % (self.title, numdigits, numdigits)
        bar = barstr % (current, self.target)
        prog = float(current) / self.target
        prog_width = int(self.width * prog)
        if prog_width > 0:
            bar += ('=' * (prog_width - 1))
            if current < self.target:
                bar += '>'
            else:
                bar += '='
        bar += ('.' * (self.width - prog_width))
        bar += ']'
        sys.stdout.write(bar)
        self.total_width = len(bar)

        if current:
            time_per_unit = (now - self.start) / current
        else:
            time_per_unit = 0
        eta = time_per_unit * (self.target - current)
        info = ''
        if current < self.target:
            info += ' - ETA: %ds' % eta
        else:
            info += ' - %ds' % (now - self.start)
        for k in self.unique_values:
            info += ' - %s:' % k
            if type(self.sum_values[k]) is list:
                avg = self.sum_values[k][0] / max(1, self.sum_values[k][1])
                if abs(avg) > 1e-3:
                    info += ' %.4f' % avg
                else:
                    info += ' %.4e' % avg
            else:
                info += ' %s' % self.sum_values[k]

        self.total_width += len(info)
        if prev_total_width > self.total_width:
            info += ((prev_total_width - self.total_width) * " ")

        sys.stdout.write(info)
        if current >= self.target:
            if "Linux" in platform.platform():
                sys.stdout.write("\n\n")
            else:
                sys.stdout.write("\n")
        sys.stdout.flush()

    def add(self, n, values=[]):
        self.update(self.seen_so_far + n, values)

def progbar(list_iter_func):
    """ Wrap any list, tuple, ndarray or func object to
    print ProgressBar when iterating over it
    """
    def iter_prog(l):
        n = len(l) if hasattr(l, '__len__') else 120
        friction = 1.2
        prog = Progbar(target=n)
        for i, j in enumerate(l):
            if i >= prog.target - 1:
                prog.target += int(i * max(friction, 0.1))
                friction /= 1.2
            prog.add(1)
            yield j
        prog.target = i + 1
        prog.update(i + 1)
    # ====== create progress monitoring ====== #
    if callable(list_iter_func):
        from functools import wraps

        @wraps(list_iter_func)
        def wrapper(*args, **kwargs):
            return iter_prog(list_iter_func(*args, **kwargs))
        return wrapper
    return iter_prog(list_iter_func)

# Under Python 2, 'urlretrieve' relies on FancyURLopener from legacy
# urllib module, known to have issues with proxy management
if sys.version_info[0] == 2:
    def urlretrieve(url, filename, reporthook=None, data=None):
        '''
        This function is adpated from: https://github.com/fchollet/keras
        Original work Copyright (c) 2014-2015 keras contributors
        '''
        def chunk_read(response, chunk_size=8192, reporthook=None):
            total_size = response.info().get('Content-Length').strip()
            total_size = int(total_size)
            count = 0
            while 1:
                chunk = response.read(chunk_size)
                if not chunk:
                    break
                count += 1
                if reporthook:
                    reporthook(count, chunk_size, total_size)
                yield chunk

        response = urlopen(url, data)
        with open(filename, 'wb') as fd:
            for chunk in chunk_read(response, reporthook=reporthook):
                fd.write(chunk)
else:
    from six.moves.urllib.request import urlretrieve

def get_file(fname, origin, untar=False):
    '''
    This function is adpated from: https://github.com/fchollet/keras
    Original work Copyright (c) 2014-2015 keras contributors
    Modified work Copyright 2016-2017 TrungNT

    Return
    ------
    file path of the downloaded file
    '''
    datadir = get_datasetpath()
    if untar:
        untar_fpath = os.path.join(datadir, fname)
        fpath = untar_fpath + '.tar.gz'
    else:
        fpath = os.path.join(datadir, fname)

    if not os.path.exists(fpath):
        print('Downloading data from', origin)
        global _progbar
        _progbar = None

        def dl_progress(count, block_size, total_size):
            global _progbar
            if _progbar is None:
                _progbar = Progbar(total_size)
            else:
                _progbar.update(count * block_size)

        error_msg = 'URL fetch failure on {}: {} -- {}'
        try:
            try:
                urlretrieve(origin, fpath, dl_progress)
            except URLError as e:
                raise Exception(error_msg.format(origin, e.errno, e.reason))
            except HTTPError as e:
                raise Exception(error_msg.format(origin, e.code, e.msg))
        except (Exception, KeyboardInterrupt) as e:
            if os.path.exists(fpath):
                os.remove(fpath)
            raise
        _progbar = None

    if untar:
        if not os.path.exists(untar_fpath):
            print('Untaring file...')
            tfile = tarfile.open(fpath, 'r:gz')
            try:
                tfile.extractall(path=datadir)
            except (Exception, KeyboardInterrupt) as e:
                if os.path.exists(untar_fpath):
                    if os.path.isfile(untar_fpath):
                        os.remove(untar_fpath)
                    else:
                        shutil.rmtree(untar_fpath)
                raise
            tfile.close()
        return untar_fpath

    return fpath

# ===========================================================================
# Python utilities
# ===========================================================================
def get_all_files(path, filter_func=None):
    ''' Recurrsively get all files in the given path '''
    file_list = []
    if os.access(path, os.R_OK):
        for p in os.listdir(path):
            p = os.path.join(path, p)
            if os.path.isdir(p):
                file_list += get_all_files(p, filter_func)
            else:
                if filter_func is not None and not filter_func(p):
                    continue
                # remove dump files of Mac
                if '.DS_Store' in p or '.DS_STORE' in p or \
                    '._' == os.path.basename(p)[:2]:
                    continue
                file_list.append(p)
    return file_list

def package_installed(name, version=None):
    import pip
    for i in pip.get_installed_distributions():
        if name.lower() == i.key.lower() and \
        (version is None or version == i.version):
            return True
    return False

def package_list(include_version=False):
    """
    Return
    ------
    ['odin', 'lasagne', 'keras', ...] if include_version is False
    else ['odin==8.12', 'lasagne==25.18', ...]
    """

    all_packages = []
    import pip
    for i in pip.get_installed_distributions():
        all_packages.append(i.key +
            (('==' + i.version) if include_version is True else ''))
    return all_packages

def get_module_from_path(identifier, path='.', prefix='', suffix='', exclude='',
                         prefer_compiled=False):
    ''' Algorithms:
     - Search all files in the `path` matched `prefix` and `suffix`
     - Exclude all files contain any str in `exclude`
     - Sorted all files based on alphabet
     - Load all modules based on `prefer_compiled`
     - return list of identifier found in all modules

    Parameters
    ----------
    identifier : str
        identifier of object, function or anything in script files
    prefix : str
        prefix of file to search in the `path`
    suffix : str
        suffix of file to search in the `path`
    path : str
        searching path of script files
    exclude : str, list(str)
        any files contain str in this list will be excluded
    prefer_compiled : bool
        True mean prefer .pyc file, otherwise prefer .py

    Returns
    -------
    list(object, function, ..) :
        any thing match given identifier in all found script file

    Notes
    -----
    File with multiple . character my procedure wrong results
    If the script run this this function match the searching process, a
    infinite loop may happen!
    * This function try to import each modules and find desire function,
    it may mess up something.

    '''
    import re
    import imp
    from inspect import getmembers
    # ====== validate input ====== #
    if exclude == '': exclude = []
    if type(exclude) not in (list, tuple, numpy.ndarray):
        exclude = [exclude]
    prefer_flag = -1
    if prefer_compiled: prefer_flag = 1
    # ====== create pattern and load files ====== #
    pattern = re.compile('^%s.*%s\.pyc?' % (prefix, suffix)) # py or pyc
    files = os.listdir(path)
    files = [f for f in files
             if pattern.match(f) and
             sum([i in f for i in exclude]) == 0]
    # ====== remove duplicated pyc files ====== #
    files = sorted(files, key=lambda x: prefer_flag * len(x)) # pyc is longer
    # .pyc go first get overrided by .py
    files = sorted({f.split('.')[0]: f for f in files}.values())
    # ====== load all modules ====== #
    modules = []
    for f in files:
        try:
            if '.pyc' in f:
                modules.append(
                    imp.load_compiled(f.split('.')[0],
                                      os.path.join(path, f))
                )
            else:
                modules.append(
                    imp.load_source(f.split('.')[0],
                                    os.path.join(path, f))
                )
        except:
            pass
    # ====== Find all identifier in modules ====== #
    ids = []
    for m in modules:
        for i in getmembers(m):
            if identifier in i:
                ids.append(i[1])
    # remove duplicate py
    return ids

def ordered_set(seq):
    seen = {}
    result = []
    for marker in seq:
        if marker in seen: continue
        seen[marker] = 1
        result.append(marker)
    return result

def dict_union(*dicts, **kwargs):
    r"""Return union of a sequence of disjoint dictionaries.

    Parameters
    ----------
    dicts : dicts
        A set of dictionaries with no keys in common. If the first
        dictionary in the sequence is an instance of `OrderedDict`, the
        result will be OrderedDict.
    \*\*kwargs
        Keywords and values to add to the resulting dictionary.

    Raises
    ------
    ValueError
        If a key appears twice in the dictionaries or keyword arguments.

    """
    dicts = list(dicts)
    if dicts and isinstance(dicts[0], OrderedDict):
        result = OrderedDict()
    else:
        result = {}
    for d in list(dicts) + [kwargs]:
        duplicate_keys = set(result.keys()) & set(d.keys())
        if duplicate_keys:
            raise ValueError("The following keys have duplicate entries: {}"
                             .format(", ".join(str(key) for key in
                                               duplicate_keys)))
        result.update(d)
    return result

# ===========================================================================
# PATH, path manager
# ===========================================================================
@contextlib.contextmanager
def TemporaryDirectory(add_to_path=False):
    """
    add_to_path: bool
        temporary add the directory to system $PATH
    """
    temp_dir = tempfile.mkdtemp()
    if add_to_path:
        os.environ['PATH'] = temp_dir + ':' + os.environ['PATH']
    current_dir = os.getcwd()
    os.chdir(temp_dir)
    yield temp_dir
    os.chdir(current_dir)
    if add_to_path:
        os.environ['PATH'] = os.environ['PATH'].replace(temp_dir + ':', '')
    shutil.rmtree(temp_dir)

def get_tempdir():
    return tempfile.mkdtemp()

def _get_managed_path(folder, name, override, is_folder=False, root='~'):
    if root == '~':
        root = os.path.expanduser('~')
    datadir_base = os.path.join(root, '.odin')
    if not os.path.exists(datadir_base):
        os.mkdir(datadir_base)
    elif not os.access(datadir_base, os.W_OK):
        raise Exception('Cannot acesss path: ' + datadir_base)
    datadir = os.path.join(datadir_base, folder)
    if not os.path.exists(datadir):
        os.makedirs(datadir)
    # ====== check given path with name ====== #
    if is_string(name):
        datadir = os.path.join(datadir, str(name))
        if os.path.exists(datadir) and override:
            if os.path.isfile(datadir): # remove file
                os.remove(datadir)
            else: # remove and create new folder
                shutil.rmtree(datadir)
        if is_folder and not os.path.exists(datadir):
            os.mkdir(datadir)
    return datadir

def get_datasetpath(name=None, override=False, root='~'):
    return _get_managed_path('datasets', name, override, is_folder=True, root=root)

def get_modelpath(name=None, override=False, root='~'):
    """ Default model path for saving ODIN networks """
    return _get_managed_path('models', name, override, is_folder=False, root=root)

def get_logpath(name=None, override=False, root='~'):
    return _get_managed_path('logs', name, override, is_folder=False, root=root)

# ===========================================================================
# Misc
# ===========================================================================
def exec_commands(cmds):
    ''' Execute a command or list of commands in parallel with multiple process
    (as much as we have CPU)

    Parameters
    ----------
    cmds: str or list of str
        string represent command you want to run

    Return
    ------
    failed: list of failed command

    '''
    if not cmds: return [] # empty list
    if not isinstance(cmds, (list, tuple)):
        cmds = [cmds]

    def done(p):
        return p.poll() is not None

    def success(p):
        return p.returncode == 0

    max_task = cpu_count()
    processes = []
    processes_map = {}
    failed = []
    while True:
        while cmds and len(processes) < max_task:
            task = cmds.pop()
            p = subprocess.Popen(task, shell=True)
            processes.append(p)
            processes_map[p] = task

        for p in processes:
            if done(p):
                if success(p):
                    processes.remove(p)
                else:
                    failed.append(processes_map[p])

        if not processes and not cmds:
            break
        else:
            time.sleep(0.005)
    return failed

def save_wav(path, s, fs):
    from scipy.io import wavfile
    wavfile.write(path, fs, s)

def play_audio(data, fs, volumn=1, speed=1):
    ''' Play audio from numpy array.

    Parameters
    ----------
    data : numpy.ndarray
            signal data
    fs : int
            sample rate
    volumn: float
            between 0 and 1
    speed: float
            > 1 mean faster, < 1 mean slower

    Note
    ----
    Only support play audio on MacOS
    '''
    import soundfile as sf
    import os

    data = numpy.asarray(data, dtype=numpy.int16)
    if data.ndim == 1:
        channels = 1
    else:
        channels = data.shape[1]
    with TemporaryDirectory() as temppath:
        path = os.path.join(temppath, 'tmp_play.wav')
        with sf.SoundFile(path, 'w', fs, channels, subtype=None,
            endian=None, format=None, closefd=None) as f:
            f.write(data)
        os.system('afplay -v %f -q 1 -r %f %s &' % (volumn, speed, path))
        raw_input('<enter> to stop audio.')
        os.system("kill -9 `ps aux | grep -v 'grep' | grep afplay | awk '{print $2}'`")

# ===========================================================================
# System query
# ===========================================================================
__process_pid_map = {}

def get_process_status(pid=None, memory_usage=False, memory_shared=False,
                       memory_virtual=False, memory_maps=False,
                       cpu_percent=False, threads=False,
                       status=False, name=False, io_counters=False):
    import psutil
    if pid is None:
        pid = os.getpid()
    if pid in __process_pid_map:
        process = __process_pid_map[pid]
    else:
        process = psutil.Process(pid)
        __process_pid_map[pid] = process

    if status:
        return process.status()
    if name:
        return process.name()
    if io_counters:
        return process.io_counters()
    if memory_usage:
        return process.memory_info().rss / float(2 ** 20)
    if memory_shared:
        return process.memory_info().shared / float(2 ** 20)
    if memory_virtual:
        return process.memory_info().vms / float(2 ** 20)
    if memory_maps:
        return {i[0]: i[1] / float(2 ** 20)
                for i in process.memory_maps()}
    if cpu_percent:
        # first time call always return 0
        process.cpu_percent(None)
        return process.cpu_percent(None)
    if threads:
        return {i.id: (i.user_time, i.system_time) for i in process.threads()}

def get_system_status(memory_total=False, memory_total_actual=False,
                      memory_total_usage=False, memory_total_free=False,
                      all_pids=False, swap_memory=False, pid=False):
    """
    Parameters
    ----------
    threads: bool
        return dict {id: (user_time, system_time)}
    memory_maps: bool
        return dict {path: rss}

    Note
    ----
    All memory is returned in `MiB`
    To calculate memory_percent:
        get_system_status(memory_usage=True) / get_system_status(memory_total=True) * 100
    """
    import psutil
    # ====== general system query ====== #
    if memory_total:
        return psutil.virtual_memory().total / float(2 ** 20)
    if memory_total_actual:
        return psutil.virtual_memory().available / float(2 ** 20)
    if memory_total_usage:
        return psutil.virtual_memory().used / float(2 ** 20)
    if memory_total_free:
        return psutil.virtual_memory().free / float(2 ** 20)
    if swap_memory:
        tmp = psutil.swap_memory()
        tmp.total /= float(2 ** 20)
        tmp.used /= float(2 ** 20)
        tmp.free /= float(2 ** 20)
        tmp.sin /= float(2**20)
        tmp.sout /= float(2**20)
        return tmp
    if all_pids:
        return psutil.pids()
    if pid:
        return os.getpid()


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

"""Unittests for methods in toolchain_util.py"""

import stat
import unittest

from core import util_stub
from environment import toolchain_util
from project import platform_stub
from test import stubs

class ToolchainUtilTest(unittest.TestCase):

    OS_VERSION = '43.21'
    TARGET_ARCH = 'x86'
    HOST_ARCH = 'host_arch'

    def setUp(self):
        self.stub_os = stubs.StubOs()
        self.stub_open = stubs.StubOpen(self.stub_os)
        self.stub_glob = stubs.StubGlob(self.stub_os)
        self.stub_util = util_stub.StubUtil()

        toolchain_util.os = self.stub_os
        toolchain_util.open = self.stub_open.open
        toolchain_util.glob = self.stub_glob
        toolchain_util.util = self.stub_util

        self.platform = platform_stub.StubPlatform(
            os_version=self.OS_VERSION,
            device_arch=self.TARGET_ARCH)

    def test_generate_toolchain(self):
        stub_tools = ['no_flags', 'g++']
        tool_paths = {}
        tool_dests = {}
        for tool in stub_tools:
            tool_paths[tool] = self.platform.os.path(
                toolchain_util.EXISTING_TOOLS_FORMAT.format(
                    host_arch=self.HOST_ARCH),
                toolchain_util.ARCH_TOOL_PREFIX[self.TARGET_ARCH] + tool)
            self.stub_os.path.should_exist.append(tool_paths[tool])

            tool_dests[tool] = self.stub_os.path.join('dest', tool)
            self.stub_os.should_chmod.append((tool_dests[tool], stat.S_IEXEC))

        self.stub_os.should_makedirs = ['dest']
        toolchain_util.GenerateToolchain(self.platform, self.HOST_ARCH, 'dest')
        # Should have created dest dir.
        self.assertTrue(self.stub_os.path.isdir('dest'))
        for tool in stub_tools:
            # Should have generated the tool file.
            self.assertTrue(self.stub_os.path.isfile(tool_dests[tool]))
            # Should have correctly generated the tool file.
            tool_file = self.stub_open.files[tool_dests[tool]]
            self.assertEqual(len(tool_file.contents), 3)
            # Should invoke sh.
            self.assertEqual(tool_file.contents[0], '#!/bin/sh')
            # Should be a nice file with a trailing newline.
            self.assertEqual(tool_file.contents[2], '')
            # Should point to the original tool.
            self.assertTrue(tool_paths[tool] in tool_file.contents[1])

            if tool == 'g++':
                # g++ is supposed to get some flags. Make sure those appeared.
                # Note: this is not comprehensive testing of all the flags.
                # Non arch-specific:
                self.assertTrue('-Wformat' in tool_file.contents[1])
                # Arch-specific:
                self.assertTrue('-m32' in tool_file.contents[1])

        # All chmods should have happened.
        self.assertEqual(self.stub_os.should_chmod, [])


import string

a = string.ascii_letters

def rotate(x, y=25):

	b = a

	print('\nRot 0 ~> ' + x)
	for i in range(0, y):

		b = b[1:] + b[0]
		t = str.maketrans(a, b)

		if y == 25:

			print('Rot ' + str(i) + ' ~> ' + (x.translate(t)).lower() + '\n')

	if y != 24:
		return (x.translate(t)).lower()

def decrypt():

	text = input('\nText to decrypt ~> ')
	rots = input('\nRotations ("all" for bruteforce) ~> ')

	if rots == 'all':

		rotate(text)

	else:

		print('\nRot ' + rots + ' ~> ' + rotate(text, int(rots)) + '\n')

decrypt()


# You are given two arrays (without duplicates) nums1 and nums2 where nums1’s
# elements are subset of nums2. Find all the next greater numbers for nums1's
# elements in the corresponding places of nums2.

# The Next Greater Number of a number x in nums1 is the first greater number
# to its right in nums2. If it does not exist, output -1 for this number.

# Example 1:
# Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
# Output: [-1,3,-1]
# Explanation:
#     For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
#     For number 1 in the first array, the next greater number for it in the second array is 3.
#     For number 2 in the first array, there is no next greater number for it in the second array, so output -1.

# Example 2:
# Input: nums1 = [2,4], nums2 = [1,2,3,4].
# Output: [3,-1]
# Explanation:
#     For number 2 in the first array, the next greater number for it in the second array is 3.
#     For number 4 in the first array, there is no next greater number for it in the second array, so output -1.

# Note:
#     All elements in nums1 and nums2 are unique.
#     The length of both nums1 and nums2 would not exceed 1000.

# ----------------------------------------------
# Ideas:
# - in decreasing sequence, first element that breaks monotone-ness
#   is next greater element for all sequence elements: e.g. [3, 2, 1, 4]
# - find next greater element for all elements in nums2

# Considerations:

# Complexity: O(m + n) time, O(n) space
# ----------------------------------------------

class Solution:
    def nextGreaterElement(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        m_dict = {}  # num -> next greater element
        stack = []  # hold decreasing sequence
        for num in nums2:
            while stack and stack[-1] < num:
                m_dict[stack.pop()] = num
            stack.append(num)
        return [m_dict.get(num, -1) for num in nums1]


### Taken from Robin's Blog, [http://blog.rtwilson.com/reading-aeronet-data-in-pandas-a-simple-helper-function/]
#modified rows 15 & 20 to accomodate V3 data
import pandas as pd

def read_aeronet(filename):
    """Read a given AERONET AOT data file, and return it as a dataframe.

    This returns a DataFrame containing the AERONET data, with the index
    set to the timestamp of the AERONET observations. Rows or columns
    consisting entirely of missing data are removed. All other columns
    are left as-is.
    """
    dateparse = lambda x: pd.datetime.strptime(x, "%d:%m:%Y %H:%M:%S")
    aeronet = pd.read_csv(filename, skiprows=6, na_values=[-999.0], #na_values=['N/A'] /change skip rows to 6 from 4
                          parse_dates={'times':[0,1]},
                          date_parser=dateparse)

    aeronet = aeronet.set_index('times')
    #del aeronet['Julian_Day'] #comented out

    # Drop any rows that are all NaN and any cols that are all NaN
    # & then sort by the index
    an = (aeronet.dropna(axis=1, how='all')
                .dropna(axis=0, how='all')
                .rename(columns={'Last_Processing_Date(dd/mm/yyyy)': 'Last_Processing_Date'})
                .sort_index())

    return an


import cv2 as cv
import numpy as np

class _StereoCaptureFileImpl:
    def __init__(self, left_video, right_video):
        self._left_cap = cv.VideoCapture(left_video)
        self._right_cap = cv.VideoCapture(right_video)

        if not self._left_cap.isOpened():
            raise ValueError('Can\'t open a video for a left channles')
        if not self._right_cap.isOpened():
            raise ValueError('Can\'t open a video for a left channles')

    def get(self, prop):
        return self._left_cap.get(prop)

    def set(self, prop, val):
        self._left_cap.set(prop, val)
        self._right_cap.set(prop, val)

    def read(self):
        ret1, left_frame = self._left_cap.read()
        ret2, right_frame = self._right_cap.read()
        if not (ret1 & ret2):
            return False, left_frame
        else:
            if left_frame.ndim > 2:
                if left_frame.shape[-1] == 1:
                    left_frame = left_frame[..., 0]
                else:
                    left_frame = cv.cvtColor(left_frame, cv.COLOR_BGR2GRAY)

            if right_frame.ndim > 2:
                if right_frame.shape[-1] == 1:
                    right_frame = right_frame[..., 0]
                else:
                    right_frame = cv.cvtColor(right_frame, cv.COLOR_BGR2GRAY)

            frame = cv.merge([np.zeros_like(left_frame), left_frame, right_frame])
            return True, frame

    def release(self):
        self._left_cap.release()
        self._right_cap.release()

class StereoCapture:
    def __init__(self, source, stereo_params=None):
        self._mono = False
        if isinstance(source, int):
            self._cap = cv.VideoCapture(source)
            #self._cap.set(cv.CAP_PROP_FOURCC, cv.VideoWriter_fourcc(*'Y16 '))
        elif isinstance(source, tuple) and len(source) == 2:
            if isinstance(source[0], str) and isinstance(source[1], str):
                self._cap = _StereoCaptureFileImpl(*source)
            else:
                self._cap = cv.VideoCapture(source[0])
                self._mono = bool(source[1])

        if stereo_params is None:
            self._stereo_params = None
        else:
            self._stereo_params = stereo_params

    def get(self, prop):
        return self._cap.get(prop)

    def set(self, prop, val):
        self._cap.set(prop, val)

    def read(self):
        ret, frame = self._cap.read()
        if not ret:
            return ret, (frame, frame)
        else:
            if self._mono:
                second = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
                first = second
            else:
                _, second, first = cv.split(frame)

            if self._stereo_params is None:
                return ret, (first, second)
            else:
                left = self._stereo_params.remap_left(first)
                right = self._stereo_params.remap_right(second)

                return ret, (left, right)

    def release(self):
        self._cap.release()


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

"""Convert Gmail (Outlook csv format) contact to Outlook csv format contact

This script converts the csv file exported from gmail into the csv format
can be recognized by Outlook (web version) Importer, and append them to
Outlook Contact.

To use it, you need first export your contact from Gmail.
"""

import csv

# Gmail contact
gmc = 'contacts-gmc.csv'
# Outlook contact header
olc = 'contacts-olc.csv'

olc_header = csv.reader(open(olc, 'r')).next()
olc_header[0] = 'First Name'

# create a map from each header entry to its index
n = len(olc_header)
mapper_olc = dict(zip(olc_header, range(n)))

# create a csv writer
fp = open(olc, 'a')
writer = csv.writer(fp, delimiter=',', lineterminator='\n')

def get_X_index(header, X):
	"""Get the index of X
	"""
	index = []
	for i, elm in enumerate(header):
		if elm.lower().find(X) != -1:
			index.append(i)
	return index

def has_X(row, X_index):
	"""Check whether a row has X
	"""
	for index in X_index:
		if not row[index]:
			pass
		else:
			return True
	return False

# process gmail csv file
with open(gmc, 'r') as csvf:
	reader = csv.reader(csvf)
	header = reader.next()

	names_index = get_X_index(header, 'name')
	email_index = get_X_index(header, 'e-mail')
	phone_index = get_X_index(header, 'phone')

	for row in reader:
		# only consider rows with at least one of First Name,
		# Last Name, and Middle Name, and at least with an e-mail
		# or a phone number
		if has_X(row, names_index) and \
		   (has_X(row, email_index) or has_X(row, phone_index)):
			olc_row = [''] * n
			for i, elm in enumerate(row):
				if not elm:
					pass
				else:
					if mapper_olc.has_key(header[i]):
						col_id = mapper_olc[header[i]]
						olc_row[col_id] = elm
			writer.writerow(olc_row)
# close the file
fp.close()


import unittest
from main.src.elevator import Elevator
from main.src.elevator_controller import ElevatorController

class TestElevatorController(unittest.TestCase):

    def test_add_elevator(self):
        elevator = Elevator(3)
        controller = ElevatorController()
        test = controller.add_elevator(elevator)
        self.assertEqual(test, 0)

    def test_remove_elevator(self):
        elevator = Elevator(3)
        controller = ElevatorController()
        controller.add_elevator(elevator)
        controller.remove_elevator(elevator)
        self.assertEqual(len(controller.all()), 0)

    def test_time(self):
        elevator = Elevator(4)
        controller = ElevatorController()
        time = controller.time(elevator, 0)
        self.assertEqual(time, 0)

    def test_choice(self):
        elevator1 = Elevator(4)
        elevator1.actual_floor = 2
        elevator2 = Elevator(4)
        elevator2.actual_floor = 4
        elevator2.add_floor(6)

        controller = ElevatorController()
        controller.add_elevator(elevator1)
        controller.add_elevator(elevator2)

        choice = controller.choice(9)
        choice = controller.choice(8)
        self.assertEqual(choice.all(), [6, 8, 9])


import chainer
import chainermn
import numpy as np
from chainercv.links import ResNet101
from chainercv.links import ResNet152
from chainercv.links import ResNet50

def setup_comm(name):
    kwargs = {}
    if name == 'pure_nccl_fp16':
        name = 'pure_nccl'
        kwargs['allreduce_grad_dtype'] = np.float16
    comm = chainermn.create_communicator(name, **kwargs)
    return comm

def setup_model(model_name, label_num):
    model_cfgs = {
        'resnet50': {'class': ResNet50, 'score_layer_name': 'fc6',
                     'kwargs': {'arch': 'fb'}},
        'resnet101': {'class': ResNet101, 'score_layer_name': 'fc6',
                      'kwargs': {'arch': 'fb'}},
        'resnet152': {'class': ResNet152, 'score_layer_name': 'fc6',
                      'kwargs': {'arch': 'fb'}}
    }
    assert model_name in model_cfgs.keys()
    model_cfg = model_cfgs[model_name]
    extractor = model_cfg['class'](
        n_class=label_num, **model_cfg['kwargs'])
    extractor.pick = model_cfg['score_layer_name']
    model = chainer.links.Classifier(extractor)
    model.cleargrads()

    return model

def update_once(model):
    opt =  chainer.optimizers.MomentumSGD()
    opt.setup(model)
    import cupy as cp
    imgs = cp.ndarray((1, 3, 224, 224), dtype=np.float32)
    labels = cp.ndarray((1,), dtype=np.int32)
    labels += 1
    model(imgs, labels)
    opt.update()


# -*- coding: utf-8 -*-
"""
Created on Wed Aug 21 12:21:45 2019

@author: K
"""

# -*- coding: utf-8 -*-
from algo import pyltp_my

"""
Created on Wed Aug 14 15:24:45 2019

@author: K

"""
# 问题：1.model训练 （同义词准确性和全面性）出现的次数>=2. 2。后置语句 :通过引号确定位置 3.整篇文章：解决
# import pandas as pd
# df = pd.read_csv('sqlResult_1558435.csv')
# print(df)

# file=open('sqlResult_1558435.csv','r',encoding='gb18030')
# news=file.readlines()
from sklearn.feature_extraction.text import TfidfVectorizer
from scipy.spatial.distance import cosine
from gensim.models import Word2Vec
from collections import defaultdict
import jieba
import joblib
import os

def cut(string):
    return ' '.join(jieba.cut(string))

# model = Word2Vec.load("word2vec.model")
# model = Word2Vec.load("word2vec_final.model")
# similar=get_related_words(['说','指出','报道','提出','表示','称'], model)   # 找说的近义词
path1 = os.path.dirname(os.path.abspath(__file__))
similar = joblib.load(path1 + '/keyword_similar.pkl')
vectorized = joblib.load(path1 + '/tfidf_model.pkl')

sentence = """
台湾工业总会是岛内最具影响力的工商团体之一，2008年以来，该团体连续12年发表对台当局政策的建言白皮书，集中反映岛内产业界的呼声。\
台湾工业总会指出，2015年的白皮书就特别提到台湾面临“五缺”（缺水、缺电、缺工、缺地、缺人才）困境，使台湾整体投资环境走向崩坏。然而四年过去，“五缺”未见改善，反而劳动法规日益僵化、两岸关系陷入紧张、对外关系更加孤立。该团体质疑，台当局面对每年的建言，“到底听进去多少，又真正改善了几多”？\
围绕当局两岸政策，工总认为，由数据来看，当前大陆不仅是台湾第一大出口市场，亦是第一大进口来源及首位对外投资地，建议台湾当局摒弃两岸对抗思维，在“求同存异”的现实基础上，以“合作”取代“对立”，为台湾多数民众谋福创利。\
工总现任理事长、同时也是台塑企业总裁的王文渊指出，过去几年，两岸关系紧张，不仅影响岛内观光、零售、饭店业及农渔蔬果产品的出口，也使得岛内外企业对投资台湾却步，2020年新任台湾领导人出炉后，应审慎思考两岸问题以及中国大陆市场。
"""
import re
from pyltp import SentenceSplitter  # 分句

def sentence_splitter(sentence):
    return list(SentenceSplitter.split(sentence))  # 分句

def token3(string):
    # we will learn the regular expression next course.
    string2 = string.replace('\\n', '')
    return re.findall('\w+', string2)

def get_head_parral_words(head, parse, words):
    re = []
    for i in range(len(parse)):
        if parse[i][0] == head + 1 and parse[i][1] == 'COO':
            re.append((i, words[i]))
    return re

def get_genju(parse, words):  # 根据。。报道
    if parse[0][1] == 'ADV':
        for i in range(len(parse)):
            if parse[i][0] == 1 and parse[i][1] == 'POB':  # and ('Ni'in NER[i] or 'Nh'in NER[i]):
                return i, words[i]
    return -1, 0

def VOB(head, parse, words):
    for i in range(len(parse)):
        if parse[i][0] == head + 1 and parse[i][1] == 'VOB':
            return i, words[i]
    return -1, 0

def get_sub_words(head, parse, words):
    for i in range(len(parse)):
        if parse[i][0] == head + 1 and parse[i][1] == 'SBV':  # and ('Ni'in NER[i] or 'Nh'in NER[i]):
            return i, words[i]

    return -1, 0

# 搜索代码
def get_related_words(initial_words, model):
    """
    @initial_words are initial words we already know
    @model is the word2vec model
    """
    unseen = initial_words
    seen = defaultdict(int)
    max_size = 1000  # could be greater
    while unseen and len(seen) < max_size:
        if len(seen) % 50 == 0:
            print('seen length : {}'.format(len(seen)))
        node = unseen.pop(0)
        new_expanding = [w for w, s in model.most_similar(node, topn=20)]
        unseen += new_expanding
        seen[node] += 1
    return seen

def distance(v1, v2):
    return cosine(v1, v2)

def final_model(sentence):
    sents = sentence_splitter(sentence)
    sents = [s for s in sents if len(s) > 3]
    results = []
    length = len(sents)
    pre_new = [token3(n) for n in sents]
    n2 = [''.join(n) for n in pre_new]
    n3 = [cut(n) for n in n2]
    X = vectorized.transform(n3)

    ltp = pyltp_my.HIT()
    for index, s in enumerate(sents):
        result = []
        words = ltp.get_segmentor(s)
        ltp.get_posttagger()
        ltp.get_ner()
        parse = ltp.get_parse()[1]  # 依存分析结果
        flag = True

        # 找head
        for i in range(len(parse)):
            if parse[i][1] == 'HED':
                head = i

        head_word = words[head]
        # print("the origin head is ",head_word)

        if head != 0:  # eg：head=0"展望未来 (以动词开头的)
            if similar[head_word] >= 2:
                sub_index, sub_word = get_sub_words(head, parse, words)
                if sub_word != 0:
                    result.append(sub_word)
                    result.append(head_word)
                    if words[head + 1] == '，':

                        result.append(''.join(words[head + 2:]))
                    else:
                        result.append(''.join(words[head + 1:]))
                    flag = False

        if flag:
            new_head, new_head_word = VOB(head, parse, words)  # 工总现任理事长、同时也是。。。(eg,真正的核心和head是VOB)
            if new_head_word != 0:
                if similar[new_head_word] >= 2:
                    sub_index, sub_word = get_sub_words(new_head, parse, words)
                    if sub_word != 0:
                        result.append(sub_word)
                        result.append(new_head_word)
                        if words[new_head + 1] == '，':

                            result.append(''.join(words[new_head + 2:]))
                        else:
                            result.append(''.join(words[new_head + 1:]))
                        flag = False
        if flag:
            re = get_head_parral_words(head, parse, words)  # 8月15日，中国联通举办年中业绩发布会 。。。 这种情况(eg,真正的核心和head是COO)
            if len(re) > 0:
                for (parra_index, parral_word) in re:
                    if parral_word != 0:
                        if similar[parral_word] >= 2:
                            sub_index, sub_word = get_sub_words(parra_index, parse, words)
                            if sub_word != 0:
                                result.append(sub_word)
                                result.append(parral_word)
                                if len(words) - parra_index <= 3:
                                    begin = words.index("“")
                                    end = words.index("”")
                                    result.append(''.join(words[begin + 1:end]))
                                elif words[parra_index + 1] == '，' or words[parra_index + 1] == '。':
                                    result.append(''.join(words[parra_index + 2:]))
                                else:
                                    result.append(''.join(words[parra_index + 1:]))
                                flag = False
        if flag:
            new_head, new_head_word = get_genju(parse, words)  # 根据近期媒体报道，中国电信的5G...（e.g ：根据。。什么报道，类型）
            if new_head_word != 0:
                if similar[new_head_word] >= 2:
                    sub_index, sub_word = get_sub_words(new_head, parse, words)
                    if sub_word != 0:
                        result.append(sub_word)
                        result.append(new_head_word)
                        if words[new_head + 1] == '，':

                            result.append(''.join(words[new_head + 2:]))
                        else:
                            result.append(''.join(words[new_head + 1:]))
                        flag = False

        results.append(result)

        if index > 0:
            if len(results[index - 1]) >= 1:
                if len(result) == 0:
                    v1 = X[(-1) * (length - index)].toarray()[0]
                    v2 = X[(-1) * (length - index + 1)].toarray()[0]
                    if distance(v1, v2) >= 0.5:
                        results[index - 1][-1] = results[index - 1][-1] + sents[index]
    results = [result for result in results if result != []]

    return results

if __name__ == '__main__':
    print(final_model(sentence))


import pytest
from array_binary_search import binary_search

# happy path test1
def test_arr_key_positive_one():
    lst = [1,2,3,4,6]
    key = 6

    expected = 4
    actual = binary_search (lst, key)

    assert actual == expected

# happy path test2
def test_arr_key_positive_two():
    lst = [1,2,6,4,]
    key = 6

    expected = 2
    actual = binary_search (lst, key)

    assert actual == expected

# happy path test3
def test_arr_key_positive_three():
    lst = [1,2,4,]
    key = 6

    expected = -1
    actual = binary_search (lst, key)

    assert actual == expected

# edge case test1
def test_arr_key_edge_one():
    lst = []
    key = 6

    expected = -1
    actual = binary_search (lst, key)

    assert actual == expected

# fauilure case test
@pytest.mark.skip('pending')
def test_arr_key_fauilure():
    lst = [2,6,4,5,6]
    key = 6

    expected = [1,4]
    actual = binary_search (lst, key)

    assert actual == expected

# above test expected failure test, consciously i put 2 same value in array, there is no rule that same value can not be use second time, however it says sorted.