from fed_learn.protos.federated_pb2 import ModelData
from fed_learn.numproto import proto_to_ndarray, ndarray_to_proto
from client_methods import encrypt, cpp_encrypt_bytes, decrypt
from fed_learn.protos.federated_pb2 import NDArray

import numpy as np

# Below is the protobuf msg definition of model
#
# // A model consists of multiple tensors
# message ModelData {
#     map<string, NDArray> params = 1;
# }
#
# // NDArray data for protobuf
# message NDArray {
#   bytes ndarray = 1;
# }

def protobuf_to_dict(model_data):
    """Transform the model data protobuf structure to a dict of keys - numpy arrays."""
    result = {}
    for v in model_data.params:
        if v == 'enc_values':
            enc_params = model_data.params[v].ndarray
            enc_params = enc_params.split(b'boundary')
            result[v] = enc_params
        else:
            result[v] = proto_to_ndarray(model_data.params[v])
    return result

    # --- ORIGINAL ---
    # result = {}
    # for v in model_data.params:
    #     result[v] = proto_to_ndarray(model_data.params[v])
    # return result

def dict_to_protobuf(params):
    """Transform a dict of keys - numpy arrays to the model data protobuf structure."""
    result = ModelData()
    for v in params:
        if v == 'enc_values':
            enc_values = params['enc_values']
            enc_bytes = enc_values[0] + b'boundary' + enc_values[1] + b'boundary' + enc_values[2]
            enc_array = NDArray()
            enc_array.ndarray = enc_bytes
            result.params['enc_values'].CopyFrom(enc_array)
        else:
            result.params[v].CopyFrom(ndarray_to_proto(params[v]))
    return result

    # --- ORIGINAL ---
    # result = ModelData()
    # for v in params:
    #     result.params[v].CopyFrom(ndarray_to_proto(params[v]))
    # return result

def encryption(params):
    # Input: Dictionary of plaintext parameters
    # Output: Dictionary of encrypted parameters

    print(f'Running Berkeley Encryption algorithm. # of model variables: {len(params.keys())}')

    if 'enc_values' in params.keys():
        print('ENC VALUES ALREADY IN DICT!')
        params.pop('enc_values')

    # # Add shape parameter for each feature and flatten values
    for feature in list(params.keys()):
        if not feature.startswith('shape'):
            params['shape_' + feature] = list(params[feature].shape)
        params[feature] = params[feature].flatten().tolist()

    # Convert keys to byte strings for each feature
    params = {key.encode(): value for key, value in params.items()}

    # Serialize and Encrypt params
    print('START ENCRYPTION')
    enc_out, iv, tag = encrypt(params)
    print('END ENCRYPTION')
    enc_dict = {'enc_values': [enc_out, iv, tag]}

    return enc_dict

    # --- ORIGINAL ---
    # return params

def decryption(params):
    # print(f'Running Berkeley Decryption algorithm. # of model variables: {len(params.keys())}')

    # # Input: Dictionary of encrypted parameters
    # # Output: Dictionary of plaintext parameters

    # # Decrypt and deserialize encrypted params
    enc_values = params['enc_values']
    params = decrypt(enc_values[0], enc_values[1], enc_values[2], len(enc_values[0]))

    # Convert byte strings to keys for each feature
    params = {key.decode(): value for key, value in params.items()}

    # Unflatten values
    for key, value in params.items():
        params[key] = np.array(value)
        if not key.startswith('shape_'):
            params[key] = np.array(value).reshape(np.array(params['shape_' + key]).astype(int))
            params[key] = params[key].astype(np.float32)

    ## --- ORIGINAL ---
    return params


#! /usr/bin/env python3

import sys
import re

def main():
    import argparse
    from find_orf import find_first_orf
    from translate import translate_sequence
    from find_orf import parse_sequence_from_path

# Create a command-line parser object
    parser = argparse.ArgumentParser(
            formatter_class = argparse.ArgumentDefaultsHelpFormatter)

# Tell the parser what command-line arguments this script can receive
    parser.add_argument('sequence',
            metavar = 'SEQUENCE',
            type = str,
            help = ('The sequence to search for an open-reading frame. '
                    'If the path flag (\'-p\'/\'--path\') is specified, '
                    'then this should be a path to a file containing the '
                    'sequence to be searched.'))
    parser.add_argument('-p', '--path',
            action = 'store_true',
            help = ('The sequence argument should be treated as a path to a '
                    'containing the sequence to be searched.'))
    parser.add_argument('-s', '--start-codons',
            type = str,
            nargs = '+', # one or more arguments
            default = ['AUG'],
            help = ('One or more possible start codons.'))
    parser.add_argument('-x', '--stop-codons',
            type = str,
            nargs = '+', # one or more arguments
            default = ['UAA', 'UAG', 'UGA'],
            help = ('One or more possible stop codons.'))

# Parse the command-line arguments into a 'dict'-like container
    args = parser.parse_args()

# Check to see if the path option was set to True by the caller. If so, parse
# the sequence from the path
    if args.path:
        sequence = parse_sequence_from_path(args.sequence)
    else:
        sequence = args.sequence
    orf = find_first_orf(sequence = sequence,
            start_codons = args.start_codons,
            stop_codons = args.stop_codons)

    translation = translate_sequence(orf)

    sys.stdout.write('{}\n'.format(translation))

if __name__ == '__main__':
    main()


from ..base import BaseAPITest
import abc

class PostDuplicateWorkflowFailure(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractproperty
    def post_data(self):
        pass

    def setUp(self):
        super(PostDuplicateWorkflowFailure, self).setUp()

    def test_should_set_expected_error_message(self):
        initial_response = self.post(self.post_url, self.post_data)
        self.assertEqual(201, initial_response.status_code)
        duplicate_response = self.post(self.post_url, self.post_data)
        self.assertEqual(400, duplicate_response.status_code)
        self.assertEqual({'error': self.expected_error_message},
                duplicate_response.DATA)

class NameFailure(PostDuplicateWorkflowFailure, BaseAPITest):
    expected_error_message = "Workflow with name 'foxes' already exists"
    post_data = {
        'tasks': { },
        'links': [
            {
                'source': 'input connector',
                'destination': 'output connector',
                'sourceProperty': 'in_a',
                'destinationProperty': 'out_a',
            },
        ],
        'inputs': {
            'in_a': 'kittens',
        },
        'name': 'foxes',
    }


from django.conf.urls import url
from video.view.votes import *
from django.contrib.auth.decorators import login_required

urlpatterns = [
	url(r'^user_like/(?P<uuid>[0-9a-f-]+)/(?P<pk>\d+)/$',login_required(VideoUserLikeCreate.as_view())),
    url(r'^user_dislike/(?P<uuid>[0-9a-f-]+)/(?P<pk>\d+)/$',login_required(VideoUserDislikeCreate.as_view())),
    url(r'^user_comment/(?P<comment_pk>\d+)/(?P<pk>\d+)/like/$',login_required(VideoCommentUserLikeCreate.as_view())),
    url(r'^user_comment/(?P<comment_pk>\d+)/(?P<pk>\d+)/dislike/$',login_required(VideoCommentUserDislikeCreate.as_view())),

	url(r'^community_like/(?P<uuid>[0-9a-f-]+)/(?P<pk>\d+)/$',login_required(VideoCommunityLikeCreate.as_view())),
    url(r'^community_dislike/(?P<uuid>[0-9a-f-]+)/(?P<pk>\d+)/$',login_required(VideoCommunityDislikeCreate.as_view())),
    url(r'^community_comment/(?P<comment_pk>\d+)/(?P<pk>\d+)/like/$',login_required(VideoCommentCommunityLikeCreate.as_view())),
    url(r'^community_comment/(?P<comment_pk>\d+)/(?P<pk>\d+)/dislike/$',login_required(VideoCommentCommunityDislikeCreate.as_view())),
]


from bs4 import BeautifulSoup
import audiojack
from threading import Thread

urllib = audiojack.urllib
youtube_dl = audiojack.youtube_dl

ydl_opts = {
    'format': 'bestaudio',
    'outtmpl': 'F:\\%(id)s.%(ext)s',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '256'}]
    }

ydl = youtube_dl.YoutubeDL(params=ydl_opts)

class MusicDownloaderAPI:

    def __init__ (self, settings):
        self.settings = settings
        #PARSE SETTINGS

    def _get_thumbnail_url (self, video_id):
        url = f"http://i.ytimg.com/vi/{video_id}/mqdefault.jpg"
        return url

    def _get_image (self, url):
        img_req = urllib.request.urlopen(url)
        img = img_req.read()
        img_req.close()
        return img

    def _add_images (self, entries):
        res = []
        for video in entries:
            img_url = self._get_thumbnail_url(video['id'])
            img = self._get_image(img_url)
            video['image'] = img
            res.append(video)
        return res

    def set_param (self, name, value):
        ydl.params[name] = value

    def get_search_results (self, query, download=False, download_first=True, limit=3):
        query = query.replace(' ', '+')
        url = 'http://youtube.com/results?search_query=' + query
        info = ydl.extract_info(url, download=download, limit=limit)
        self.download(info['entries'][0]['webpage_url'])
        res = self._add_images(info['entries'])
        return res

    def get_lyrics (self, song_title, song_artist):
        artist = song_artist.lower()
        title = song_title.lower()
        artist = artist.replace(' ', '')
        title = title.replace(' ', '')

        url = "http://azlyrics.com/lyrics/"+artist+"/"+title+".html"
        print(url)

        content = urllib.request.urlopen(url).read()
        soup = BeautifulSoup(content, 'html.parser')
        lyrics = str(soup)
        # lyrics lies between up_partition and down_partition
        up_partition = ''
        down_partition = ''
        lyrics = lyrics.split(up_partition)[1]
        lyrics = lyrics.split(down_partition)[0]
        lyrics = lyrics.replace('
','').replace('
','').replace('
','').strip()
        return lyrics

    def download (self, video_urls):
        if not isinstance(video_urls, list):
            video_urls = [video_urls]
        thr = Thread(target=ydl.download, args=(video_urls,))
        thr.start()


# 4.Write a program which accept one number form user and return addition of its factors.
# Input : 12 Output : 16 (1+2+3+4+6)

print("Enter num");
num=int(input());
factorial=1;
for i in range(1,num + 1):
       factorial = factorial+i;

print("factorial is= ",factorial);

# output-:

# zine@DESKTOP-BU1PSC0 MINGW64 ~/Desktop/python/Assignment/Assignment2
# $ python Assignment2_4.py
# Enter num
# 5
# factorial is=  16


import bme280
import smbus2
from time import sleep
import timestamp
port = 1
address = 0x77 # Adafruit BME280 address. Other BME280s may be different
bus = smbus2.SMBus(port)

bme280.load_calibration_params(bus,address)

while True:
    bme280_data = bme280.sample(bus,address)
    humidity  = bme280_data.humidity
    pressure  = bme280_data.pressure/33.8639
    temperature = (bme280_data.temperature * 9.0/5.0) +32
#    data = f"{timestamp.stamp()} Humidity {humidity:03.2f} % Pressure {pressure:03.2f} InHg Temperature {temperature:03.1f} F
"
    current = f"
{timestamp.stamp()}

 Humidity {humidity:03.2f} %

 Pressure {pressure:03.2f} InHg

 Temperature {temperature:03.1f} F

"
    print(current)
#    print(f"
 {timestamp.stamp()}
 Humidity {humidity:03.2f} % Pressure {pressure:03.2f} InHg Temperature {temperature:03.1f} F")

    try:

        with open("/var/www/html/current.html", "w") as outfile1:
            outfile1.write("<meta http-equiv='refresh' content='15'/>
")
            outfile1.write(str(current))

    except KeyboardInterrupt:
        print("Later Dude")

    finally:

        with open("/var/www/html/current.html", "a") as outfile1:
            outfile1.close()

    sleep(15)


# output : qiita-dictionalyFromWardnet004_s.json
# {"name":{"s":"(1) それをする、またはそれであるさま"},"test_id":{"s":"-風"}}
# {"name":{"s":"(1) 全体に対する比率（通常、百分率）"},"test_id":{"s":"%"}}
# {"name":{"s":"(1) それをする、またはそれであるさま"},"test_id":{"s":"-様"}}

import pandas as pd
csvFile = "qiita-dictionalyFromWardnet001_s.csv"
hoge = pd.read_csv(csvFile,
                   parse_dates=[1],  # 対象のカラムインデックス
                   names=['test_id', 'name'],
                   dtype={2: str}  # カラムインデックスと型の dict
                   )

path_w = 'qiita-dictionalyFromWardnet004_s.json'
with open(path_w, mode='w', encoding="utf_8") as f:
    debugMax = 599999999999
    debugNo  = 0
    item00 = ""
    item01 = ""
    tmpStr = ""
    for index,item in hoge.iterrows():
        if debugNo == debugMax:
            break
        else:
            debugNo += 1
        if debugNo == 1:
            item00 = item['test_id']
            item01 = item['name']
        else:
            tmpStr+= "{"
            tmpStr+= "\"" + item01 + "\":{\"s\":\"" + str(item['name']) + "\"}"
            tmpStr+= ","
            tmpStr+= "\"" + item00 + "\":{\"s\":\"" + str(item['test_id']) + "\"}"
            tmpStr+= "}\n"
            f.write( tmpStr )
            tmpStr = ""


import requests
import csv
import time
import datetime
import dateutil.relativedelta

cookie = 'v_id=YOUR_ID; api_access_token=YOUR_API_TOKEN; csrftoken2=YOUR_CSRF_TOKEN'

start_date = datetime.datetime.strptime("2019-11-01", "%Y-%m-%d")

header_included = False

with open('venmo_data.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile, delimiter=',',
                            quotechar='"', quoting=csv.QUOTE_MINIMAL)
    for x in range(3):
        month_end_date = start_date + \
            dateutil.relativedelta.relativedelta(months=1, days=-1)

        url = 'https://api.venmo.com/v1/transaction-history?start_date=' + \
            start_date.strftime("%Y-%m-%d") + '&end_date=' + \
            month_end_date.strftime("%Y-%m-%d") + '&csv=true'

        r = requests.get(url, headers={"cookie": cookie})

        decoded_content = r.content.decode('utf-8')
        cr = csv.reader(decoded_content.splitlines(), delimiter=',')
        my_list = list(cr)
        length = len(my_list)
        printed = 0
        for row in my_list:
            printed += 1
            if (printed == 1):
                if (header_included):
                    # If this is the first row and we've already included the header we want to skip
                    continue
                else:
                    header_included = True
            if (printed == 2):
                # the second line is the begining balance for the period we are downloading
                continue
            if (printed > length - 4):
                # the last 4 lines of the csv are ending balance stuff, notes on
                # fees and some legal notes we want to ignore
                break

            # don't include Transfers (which is just transfering money back to the bank)
            # or merchant transactions (like paying for ubers or whatever)
            if(row[3] != 'Standard Transfer' and row[3] != 'Merchant Transaction'):
                print(row)
                writer.writerow(row)

        start_date = start_date - dateutil.relativedelta.relativedelta(months=1)
        # wait one second between requests just to avoid rate limiting issues or whatever
        time.sleep(1)


def fib(n):
    if n ==1:
        return 1
    elif n == 2:
        return 2
    else:
        return fib(n-2)+fib(n-1)

fib_seq = []
i=1
while fib(i) < 4000000:
    fib_seq.append(fib(i))
    i += 1

sum([x for x in fib_seq if (x % 2)==0])


# -*- encoding: utf-8 -*-

'''
每一个 tkinter 应用的主体框架都可以包含下面这部分.
1. 定义 window 窗口 和 window的一些属性,
2. 设置窗口内容
3. 执行window.mainloop激活窗口.
'''

import tkinter as tk

window = tk.Tk()
window.title('my window')
window.geometry('600x400')

# 窗口内容

l = tk.Label(window, text='', bg='yellow')
l.pack()
counter = 0
def do_job():
    global counter
    l.config(text='do ' + str(counter))
    counter+=1

# 创建菜单栏Menu
menubar = tk.Menu(window)

#定义一个空菜单单位
filemenu = tk.Menu(menubar, tearoff=0)
# 在File中添加小菜单
filemenu.add_command(label='New', command=do_job)
filemenu.add_command(label='Open', command=do_job)
filemenu.add_command(label='Save', command=do_job)
# 添加一条分割线
filemenu.add_separator()
# 在File中添加Exit续爱菜单,对应的命令为 window.quit
filemenu.add_command(label='Exit', command=window.quit)
# 将定义的空菜单命名为File,放在菜单栏中
menubar.add_cascade(label='File', menu=filemenu)

editmenu = tk.Menu(menubar, tearoff=0)
# 在File中添加小菜单
editmenu.add_command(label='Cut', command=do_job)
editmenu.add_command(label='Copy', command=do_job)
editmenu.add_command(label='Paste', command=do_job)
# 将定义的空菜单命名为Edit,放在菜单栏中
menubar.add_cascade(label='Edit', menu=editmenu)

# 在File上创建一个空菜单
submenu = tk.Menu(filemenu)
submenu.add_command(label='Submenu1', command=do_job)
filemenu.add_cascade(label='Import', menu=submenu, underline=0)

window.config(menu=menubar)

# 显示窗口
window.mainloop()


#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.

from typing import Dict, Tuple

import ffmpeg  # @manual
from augly.utils import pathmgr
from augly.video.augmenters.ffmpeg import BaseFFMPEGAugmenter
from augly.video.helpers import get_audio_info, get_video_info
from ffmpeg.nodes import FilterableStream

class VideoAugmenterByAudioSwap(BaseFFMPEGAugmenter):
    def __init__(self, audio_path: str, offset: float):
        assert offset >= 0, "Offset cannot be a negative number"

        self.audio_path = pathmgr.get_local_path(audio_path)
        self.offset = offset

    def add_augmenter(
        self, in_stream: FilterableStream, **kwargs
    ) -> Tuple[FilterableStream, Dict]:
        """
        Swaps the audio of a video

        @param in_stream: the FFMPEG object of the video

        @returns: a tuple containing the FFMPEG object with the augmentation
            applied and a dictionary with any output arguments as necessary
        """
        audio_info = get_audio_info(self.audio_path)
        video_info = get_video_info(kwargs["video_path"])

        audio_duration = float(audio_info["duration"])
        audio_sample_rate = float(audio_info["sample_rate"])

        start = self.offset
        end = start + float(video_info["duration"])

        audio = ffmpeg.input(self.audio_path).audio

        if end > audio_duration:
            pad_len = (end - audio_duration) * audio_sample_rate
            audio = audio.filter_("apad", pad_len=pad_len)

        audio = audio.filter_("atrim", start=start, end=end).filter_(
            "asetpts", "PTS-STARTPTS"
        )

        return ffmpeg.concat(in_stream.video, audio, v=1, a=1, n=1), {}


import scrapy
from ..items import RumahItem

class RumahSpider(scrapy.Spider):
    name = 'rumah'
    start_urls = ['https://www.rumah123.com/jual/bandung/gudang',]
    page = 1

    def get_number(self,text):
        result=''
        try:
            iter(text)
        except TypeError:
            return None
        for i in text:
            try:
                result+=str(int(i))
            except ValueError:
                continue
        if result:
            return int(result)
        else:
            return None

    def parse(self, response):
        list_rumah = response.css('ul.listing-list')
        print(list_rumah)
        for url in list_rumah.css('a::attr(href)'):
            yield response.follow(url, callback=self.parse_detail)
        pages = response.css('li.ant-pagination-item::attr(title)').getall()
        for i in pages:
            try:
                if int(i)==self.page+1:
                    self.page=int(i)
                    yield response.follow(self.start_urls[0]+'/?page={}'.format(self.page), callback=self.parse)
                    break
            except ValueError:
                continue

    def parse_detail(self, response):
        item = RumahItem()
        price = response.css('div.property-price').css('span::text').get()
        item['price']=self.get_number(price)
        item['address'] = response.css('span.property-address sale-default::text').get()
        item['supplyImageUrls'] = response.css('header').xpath('..').css('img::attr(src)').get()
        item['luastanah'] = self.get_number(response.css('div.property-areas-info').css('li::text').getall()[1])
        item['luasbangunan'] = self.get_number(response.css('div.property-areas-info').css('li::text').getall()[0])
        item['deskripsi'] = response.css('pre.property-description::text').get().replace('\n',',')
        item['title'] = response.css('h2.description-title::text').get()
        item['url'] = response.url
        yield item


from Driver import Driver
import uart as smbus
import time

class Mag3110Driver(Driver):
    DEV_ADDR = 0x0E
    REG_DR_STATUS = 0x00
    REG_WHO_AM_I = 0x07
    REG_TEMP = 0x0F
    REG_SYSMOD = 0x08
    REG_CTRL_REG1 = 0x10
    REG_CTRL_REG2 = 0x11

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        try:
            self.bus.close()
        except:
            pass

    def __init__(self, i2c_id:int, supply_type:str, read_freq=40):
        self.name = "MAG3110"#_{}".format(supply_type)
        self.bus = smbus.SMBus(i2c_id)
        time.sleep(1)

        status = self.bus.read_byte_data(self.DEV_ADDR, self.REG_WHO_AM_I)
        if status == 0xC4: # 0xC4
            self.bus.write_byte_data(self.DEV_ADDR, self.REG_SYSMOD, 0x01) # set 40Hz aquisition, 16b resolution, automatic trigger data, raw mode
            self.bus.write_byte_data(self.DEV_ADDR, self.REG_CTRL_REG1, 0x09) # set 40Hz aquisition, 16b resolution, automatic trigger data, raw mode
            self.bus.write_byte_data(self.DEV_ADDR, self.REG_CTRL_REG2, 0xA0)
        else:
            raise Exception("Cannot connect to device, wrong ID={} expected 0xC4".format(hex(status)))

    def getMeasurement(self, readTemp=False):
        measurement = []
        while True:
            if (self.bus.read_byte_data(self.DEV_ADDR, self.REG_DR_STATUS) & 0x08) == 0x08:
                break

        data = self.bus.read_i2c_block_data(self.DEV_ADDR, 0x01, 6)
        for i in [0,2,4]:
            value = ((data[i]<<8 & 0xFF00)|data[i+1])
            if value > 32767:
                value -= 65536
            measurement.append(value)

        if readTemp:
            temp = self.bus.read_byte_data(self.DEV_ADDR, self.REG_TEMP)
            measurement.append(temp)

        return measurement

    def scaleMeasurement(self, measurement):
        return [m/20 for m in measurement]


# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
# MIT License. See license.txt

from __future__ import unicode_literals
import webnotes

workflow_names = {}
def get_workflow_name(doctype):
	global workflow_names
	if not doctype in workflow_names:
		workflow_name = webnotes.conn.get_value("Workflow", {"document_type": doctype,
			"is_active": "1"}, "name")

		# no active? get default workflow
		if not workflow_name:
			workflow_name = webnotes.conn.get_value("Workflow", {"document_type": doctype},
			"name")

		workflow_names[doctype] = workflow_name

	return workflow_names[doctype]

def get_default_state(doctype):
	workflow_name = get_workflow_name(doctype)
	return webnotes.conn.get_value("Workflow Document State", {"parent":doctype,
		"idx":1}, "state")

def get_state_fieldname(doctype):
	workflow_name = get_workflow_name(doctype)
	return webnotes.conn.get_value("Workflow", workflow_name, "workflow_state_field")


import seaborn as sns
import matplotlib.pyplot as plt
from wordcloud import WordCloud

from collections import Counter
from nltk.corpus import stopwords

import pandas as pd
import numpy as np

from sklearn.decomposition import PCA
from sklearn.manifold import TSNE

import yaml

with open('config.yaml', 'r') as f:
    config = yaml.load(f, Loader=yaml.FullLoader)

QUERIES_COL = config['queries_col']

def most_common_words(df, queries_col=QUERIES_COL, n_commons=25, remove_stopwords=True, by='word'):
    """
    checks for the most important [queries | individual words] in the search corpus.

    :param df: Pandas DataFrame of which one column in test search queries
    :param queries_col: the search queries column
    :param n_commons: number of top most common
    :param remove_stopwords: use NLTK to remove english stopwords
    :param by: 'word' returns most common *words*, 'query' returns most common *complete queries*.
    :return: Pandas DataFrame with most common words
    """
    if by == 'word':
        entries = df[queries_col].str.cat(sep=' ')
        entries = entries.split(' ')
    elif by == 'query':
        entries = df[queries_col]
    else:
        raise Exception ('The keyword argument "separate" should either be set to "word" or "query"')

    c = Counter(entries)
    commons = c.most_common(n_commons)
    commons = pd.DataFrame(commons)

    if remove_stopwords:
        sw_ind = commons[0].apply(lambda word: word in stopwords.words('english'))
        ind_to_drop = commons[sw_ind].index
        commons.drop(index=ind_to_drop, inplace=True)

    commons.rename(columns={0: "word", 1: "count"}, inplace=True)

    return commons

def plot_stats(df, queries_col=QUERIES_COL, **mcw_kwargs):
    """
    plots graphic information about individual words in the search query corpus.

    :param df: Pandas DataFrame
    :param queries_col: the column that holds the queries in df
    :param top_n: top n words to show
    :param separate: if True, returns most common *words*. Otherwise returns most common *complete queries*.
    :return: None
    """
    commons_df = most_common_words(df, queries_col, **mcw_kwargs)
    top_n = mcw_kwargs['n_commons']

    total = commons_df['count'].sum(axis=0)
    commons_df['freq'] = commons_df['count'].apply(lambda x: x / total)
    if len(commons_df) > top_n:
        commons_df = commons_df[:top_n]

    fig, axes = plt.subplots(1, 2, figsize=(18, 4))

    # plotting the word cloud
    wordcloud = WordCloud()
    commons_dict = dict(zip(commons_df['word'], commons_df['freq']))
    wordcloud.generate_from_frequencies(frequencies=commons_dict)
    axes[0].imshow(wordcloud, interpolation="bilinear")
    axes[0].axis("off")

    # plotting the bar count
    words = commons_df['word']
    counts = commons_df['count']
    sns.barplot(x=words, y=counts, ax=axes[1])
    axes[1].tick_params(axis='x', labelrotation=90)

    plt.suptitle(f'Most Common {top_n} {"Words" if mcw_kwargs["by"] == "word" else "Queries"}')

    plt.show()

    return None

def tsnescatterplot(model, word, list_names):
    """ Plot in seaborn the results from the t-SNE dimensionality reduction algorithm of the vectors of a query word,
    its list of most similar words, and a list of words.
    """
    arrays = np.empty((0, 50), dtype='f')
    word_labels = [word]
    color_list = ['red']

    # adds the vector of the query word
    arrays = np.append(arrays, model.wv.__getitem__([word]), axis=0)

    # gets list of most similar words
    close_words = model.wv.most_similar([word])

    # adds the vector for each of the closest words to the array
    for wrd_score in close_words:
        wrd_vector = model.wv.__getitem__([wrd_score[0]])
        word_labels.append(wrd_score[0])
        color_list.append('blue')
        arrays = np.append(arrays, wrd_vector, axis=0)

    # adds the vector for each of the words from list_names to the array
    for wrd in list_names:
        wrd_vector = model.wv.__getitem__([wrd])
        word_labels.append(wrd)
        color_list.append('green')
        arrays = np.append(arrays, wrd_vector, axis=0)

    # Reduces the dimensionality from 300 to 50 dimensions with PCA
    reduc = PCA(n_components=19).fit_transform(arrays)

    # Finds t-SNE coordinates for 2 dimensions
    np.set_printoptions(suppress=True)

    Y = TSNE(n_components=2, random_state=0, perplexity=15).fit_transform(reduc)

    # Sets everything up to plot
    df = pd.DataFrame({'x': [x for x in Y[:, 0]],
                       'y': [y for y in Y[:, 1]],
                       'words': word_labels,
                       'color': color_list})

    fig, _ = plt.subplots()
    fig.set_size_inches(9, 9)

    # Basic plot
    p1 = sns.regplot(data=df,
                     x="x",
                     y="y",
                     fit_reg=False,
                     marker="o",
                     scatter_kws={'s': 40,
                                  'facecolors': df['color']
                                  }
                     )

    # Adds annotations one by one with a loop
    for line in range(0, df.shape[0]):
        p1.text(df["x"][line],
                df['y'][line],
                '  ' + df["words"][line].title(),
                horizontalalignment='left',
                verticalalignment='bottom', size='medium',
                color=df['color'][line],
                weight='normal'
                ).set_size(15)

    plt.xlim(Y[:, 0].min() - 50, Y[:, 0].max() + 50)
    plt.ylim(Y[:, 1].min() - 50, Y[:, 1].max() + 50)

    plt.title('t-SNE visualization for {}'.format(word.title()))


#!/usr/bin/env python3.4
import os.path
from mipsTester import runTests

if os.path.isfile("/usr/home/grads/courses/myy402/bin/MarsMYY402_4_5.jar"):
  # This is the path to MarsMYY402_4_5.jar for the lab machines.
  marsJar = "/usr/home/grads/courses/myy402/bin/MarsMYY402_4_5.jar"
else:
  ##########################################
  # This is my set-up for my machine,
  # MODIFY FOR YOUR PERSONAL COMPUTER SETUP!
  #  USING THE FULL PATH TO MarsMYY402_4_5.jar IN YOUR COMPUTER
  ##########################################
  marsJar = "/home/efthym/t/ca_course/MarsMYY402_4_5.jar"

tests = [ \
 ("Empty string",\
  [('^string:.*$', 'string: .asciiz ""')],\
  {'v1' : 'string'},\
  {}\
 ), \
 ("Multiple spaces",\
  [('^string:.*$', 'string: .asciiz "  a    abc  "')],\
  {'v1' : 'string+7'},\
  {} \
 ), \
 ("Longest at the end",\
  [('^string:.*$', 'string: .asciiz "a longest"')],\
  {'v1' : 'string+2'},\
  {} \
 ), \
 ("Longest at the start",\
  [('^string:.*$', 'string: .asciiz "longest tiny"')],\
  {'v1' : 'string+0'},\
  {} \
 ), \
 ("Equal lengths",\
  [('^string:.*$', 'string: .asciiz "samesize small samesize small "')],\
  {'v1' : 'string+15'},\
  {} \
 ) \
]

# Change verbose below to True, if you get errors to get more detailed information
print (runTests("../lab02.asm", tests, marsJar, verbose=True))


#! coding=utf-8
import click
import glob

def _index_of_substr(the_list, substr):
    for i, val in enumerate(the_list):
        if substr in val:
            return i
    return None

@click.command()
@click.option('--in-dir', help='Slow queries log dir.')
@click.option('--out-file', help='Queries file.')
def log_to_query(in_dir, out_file):
    with open(out_file, 'w') as fwrite:
        errs = []
        slow_prefix = 'elasticsearch_dev_2.1_v2_index_search_slowlog'
        slow_files = glob.glob(in_dir + '/' + slow_prefix + '*')
        for s_file in slow_files:
            with open(s_file) as fread:
                for line in fread:
                    line_list = line.split(', ')
                    ind = _index_of_substr(line_list, 'source')
                    try:
                        query = line_list[ind][7:-1]
                        fwrite.write(query + '\n')
                    except IndexError:
                        errs.append(line)
                        pass
        if errs:
            print('There were {} errors.'.format(len(errs)))
            print(errs)

if __name__ == '__main__':
    log_to_query()


#!/usr/bin/python python3
# -*- coding: utf-8 -*-
"""This is Test Code."""

import yaml
import logging
import tweepy
from flask import Flask, session, redirect, render_template, request

# ログレベル設定
logging.basicConfig(level=logging.INFO)

# パスワード系ファイル読み込み
secret = yaml.load(open('secret.yaml').read())

# Consumer Key
CONSUMER_KEY = secret['CONSUMER_KEY']
# Consumer Secret
CONSUMER_SECRET = secret['CONSUMER_SECRET']
# Callback URL (認証後リダイレクトされるURL)
# CALLBACK_URL = 'https://flask-tweepy.herokuapp.com/'  # Heroku上
CALLBACK_URL = 'http://localhost:5000/'  # ローカル環境

logging.warn('app start!')

# Flask の起動
app = Flask(__name__)
# flask の session を使うにはkeyを設定する必要がある．
app.secret_key = secret['SECRET_KEY']

@app.route('/')
def index():
    """ root ページの表示 """
    # 連携アプリ認証済みなら user の timeline を取得
    timeline = user_timeline()

    # templates/index.html を使ってレンダリング．
    return render_template('index.html', timeline=timeline)

@app.route('/twitter_auth', methods=['GET'])
def twitter_auth():
    """ 連携アプリ認証用URLにリダイレクト """
    # tweepy でアプリのOAuth認証を行う
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET, CALLBACK_URL)

    try:
        # 連携アプリ認証用の URL を取得
        redirect_url = auth.get_authorization_url()
        # 認証後に必要な request_token を session に保存
        session['request_token'] = auth.request_token
    except tweepy.TweepError as e:
        logging.error(str(e))

    # リダイレクト
    return redirect(redirect_url)

def user_timeline():
    """ user の timeline のリストを取得 """
    # request_token と oauth_verifier のチェック
    token = session.pop('request_token', None)
    verifier = request.args.get('oauth_verifier')
    if token is None or verifier is None:
        return False  # 未認証ならFalseを返す

    # tweepy でアプリのOAuth認証を行う
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET, CALLBACK_URL)

    # Access token, Access token secret を取得．
    auth.request_token = token
    try:
        auth.get_access_token(verifier)
    except tweepy.TweepError as e:
        logging.error(str(e))
        return {}

    # デバッグのためアクセストークンを出力
    token_str = '\t'.join((
         'access_token:' + auth.access_token,
         'access_tokne_secret:' + auth.access_token_secret
    ))
    logging.info(token_str)

    # tweepy で Twitter API にアクセス
    api = tweepy.API(auth)

    # user の timeline 内のツイートのリストを最大100件取得して返す
    return api.user_timeline(count=100)

if __name__ == '__main__':
    port = 5000
    app.run(port=port)


#!/usr/bin/env python

"""Python convert tool for generating AMR-WB test key scenarios from input rtpdump file.

"""

__id__ = '$Id$'

__changelist__ = '$Change$'

__datetime__ = '$DateTime$'

__authors__ = 'Craig Johnston <craig.johnston@dolby.com>'

__copyright__ = 'Copyright (C) 2018 Dolby Laboratories Inc. All rights reserved.'

#TODO: Next steps

# Implement reading and writing options:

## Interleaved

## Robust sorting

# clean up read/write functions to remove major duplication of code

# Find similarities and combine the paths in the read and write code

import socket

import struct

import itertools

import sys

#Output and transformation config items are arrays. Every combination of the output arrays will be output to an autogenerated filename

#The naming scheme is as follows:

# "output_(be|(oa[_rs][_il])([_nt]|[_Xf][Yr][Znd]).rtpdump" where:

# be = Bandwidth Efficient

# oa = Octet Aligned

# rs = Robust sorting

# il = Interleaving

# nt = No transformation of packets has been done (just packing format has changed)

# Xf = X normal (non-redundant) frames of audio

# Yr = Y redundant frames of audio

# Znd = Z redundant NO_DATA frames between the normal and redundant frames

from copy import deepcopy as copy

input_filename = 'input.rtpdump'

#Set the input flags based on the input file's format

input_octet_aligned = False

#Set the output flags to the desired file format

output_octet_aligned = [True]

output_robust_sorting = [True, False]

# If output_transform is False, the number of frames per packet will not change and the original packets with their original headers will be output. If True, all packets headers except the first will be generated

# output_normal_frames controls how many non-redundant frames will be in each packet - Must be 1 or more

# output_redundant_frames controls how many redundant frames will be in each packet - Must be 0 or more

# output_redundant_no_datas controls how many NO_DATA to insert between non-redundant and redundant frames - Must be 0 or more and must be 0 if output_redundant_frames is 0

output_transform = [False, True]

output_normal_frames = [3,4]

output_redundant_frames = [1,2, 4]

output_redundant_no_datas = [0, 1, 5]

#Not supported

input_robust_sorting = False

input_interleaved = False

output_interleaved = [False]

class AMRConfig:

  def __init__(self, octet_aligned, robust_sorting, interleaved):

    self.octet_aligned = octet_aligned

    self.robust_sorting = robust_sorting

    self.interleaved = interleaved

  def __str__(self):

    res = 'OA' if self.octet_aligned else 'BE'

    if self.robust_sorting:

     res += ' RS'

    if self.interleaved:

     res += ' IL'

    return res

class TransformConfig:

  def __init__(self, normal_frames, redundant_frames, redundant_no_datas):

    self.normal_frames = normal_frames

    self.redundant_frames = redundant_frames

    self.redundant_no_datas = redundant_no_datas

class BitStreamReader:

  def __init__(self, data):

    self.data = data

    self.byte_idx = 0

    self.bit_idx = 0

  def skip_byte_pad(self):

    if self.bit_idx != 0:

      self.bit_idx = 0

      self.byte_idx += 1

  def read_bit(self):

    res = self.data[self.byte_idx] & (0x80 >> self.bit_idx)

    self.bit_idx += 1

    if self.bit_idx == 8:

      self.bit_idx = 0

      self.byte_idx += 1

    return 1 if res else 0

  def read_bits(self, count):

    if False: # and count <= 8 - self.bit_idx:

      #Simple case - Just reading from the current byte

      full_mask = [0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff]

      res = (self.data[self.byte_idx] & (full_mask[count] << (8 - count - self.bit_idx))) >> (8 - count - self.bit_idx)

      self.bit_idx += count

      if self.bit_idx == 8:

        self.bit_idx = 0

        self.byte_idx += 1

      return res

    elif count <= 8:

      #Simplish case where there's only one byte of output - Use lazy approach

      res = 0

      while count:

        count -=1

        res <<= 1

        res |= self.read_bit()

      return res

    else:

      #Lazy approach for multiple bytes output

      byte_count = int(count / 8)

      bit_count = count % 8

      #print('Need to read %u bytes and %u bits' % (byte_count, bit_count))

      res = b''

      while byte_count:

        #print(byte_count)

        res += bytes([self.read_bits(8)])

        byte_count -= 1

      res += bytes([self.read_bits(bit_count)])

      return res

class BitStreamWriter:

  def __init__(self):

    self.data = bytearray()

    self.byte_idx = -1

    self.bit_idx = 8

  def pad_to_byte(self):

    if self.bit_idx != 8:

      self.bit_idx = 8

  def write_bit(self, b):

    if self.bit_idx == 8:

      self.bit_idx = 0

      self.data.append(0)

      self.byte_idx += 1

    if b:

      self.data[-1] |= (1 << 7 - self.bit_idx)

    self.bit_idx += 1

    #print('Writing', 1 if b else 0, self.data)

  def write_bits(self, bits, count):

    if count == 0:

      return

    #print('Write %u bits:' % (count), bits)

    byte_count = int(count / 8)

    bit_count = count % 8

    #print(byte_count, bit_count)

    if byte_count == 1 and bit_count == 0:

      byte_count = 0

      bit_count = 8

      #bits = bits[0]

    while byte_count:

      self.write_bits(bits[0], 8)

      bits = bits[1:]

      byte_count -= 1

    #When this loop completes the type of bits is b'' (one character only) or int. The rest of the code expects an int, so convert if needed

    if type(bits) is bytes:

      bits = bits[0]

    temp_bit_count = 0

    #print(byte_count, bit_count, bits)

    while temp_bit_count < bit_count:

      self.write_bit(bits & ((0x01 << (bit_count - 1)) >> (temp_bit_count)))

      temp_bit_count += 1

class AMRPayload:

  def __init__(self, cfg, data = None):

    self.cmr = 0x0f

    self.ill = 0

    self.ilp = 0

    self.frames = []

    self.cfg = cfg

    if data == None:

      return

    bsr = BitStreamReader(data)

    self.cmr = bsr.read_bits(4)

    #print(hex(self.cmr))

    if self.cfg.octet_aligned:

      reserved = bsr.read_bits(4)

      #TODO: Check reserved bits are 0?

      if self.cfg.interleaved:

        self.ill = (data[idx] & 0xf0) >> 4

        self.ilp = data[idx] & 0x0f

        idx += 1

      more = 1

      toc = []

      while more:

        more = bsr.read_bit()

        ft = bsr.read_bits(4)

        q = bsr.read_bit()

        bsr.skip_byte_pad()

        toc.append((ft, q))

        if ft == 14:

          #print('SPEECH_LOST')

          pass

        if ft == 15:

          #print('NO_DATA')

          pass

        if self.cfg.robust_sorting:

          bits_needed = []

          for e in toc:

            bits_needed.append(self.ft_to_bits[e[0]])

          #TODO: Implement reading of robust sorting

          pass

        else:

          for e in toc:

            count = self.ft_to_bits[e[0]]

            amr_bits = b''

            if count:

              #print('Need to read %u bits' % (count))

              amr_bits = bsr.read_bits(count)

              bsr.skip_byte_pad()

              #print(amr_bits)

              #print(str((len(bsr.data) - bsr.byte_idx - 1) * 8 + 8-bsr.bit_idx) + ' bits left over')

            self.frames.append((e[0], e[1], amr_bits))

            #print(len(self.frames))

    else:  #Not octet-aligned

      more = 1

      toc = []

      while more:

        more = bsr.read_bit()

        ft = bsr.read_bits(4)

        q = bsr.read_bit()

        toc.append((ft, q))

        if ft == 14:

          #print('SPEECH_LOST')

          pass

        if ft == 15:

          #print('NO_DATA')

          pass

        for e in toc:

          count = self.ft_to_bits[e[0]]

          amr_bits = b''

          if count:

            #print('Need to read %u bits' % (count))

            amr_bits = bsr.read_bits(count)

            #print(amr_bits)

            #print(str((len(bsr.data) - bsr.byte_idx - 1) * 8 + 8-bsr.bit_idx) + ' bits left over')

          self.frames.append((e[0], e[1], amr_bits))

          #print(len(self.frames))

          #self.xyx = self.pjq

  def write(self):

    bsw = BitStreamWriter()

    bsw.write_bits(self.cmr, 4)

    if self.cfg.octet_aligned:

      bsw.write_bits(0, 4)

      if self.cfg.interleaved:

        #TODO Implement

        pass

      temp_frames = self.frames[:-1]

      last_frame = self.frames[-1]

      #print('XXXX:', len(self.frames), len(temp_frames))

      for t in temp_frames:

        bsw.write_bit(1)

        bsw.write_bits(t[0], 4)

        bsw.write_bit(t[1])

        bsw.write_bits(0, 2) #Padding

      #Last entry in TOC

      bsw.write_bit(0)

      bsw.write_bits(last_frame[0], 4)

      bsw.write_bit(last_frame[1])

      bsw.pad_to_byte() #Padding

      if self.cfg.robust_sorting:

        temp = []

        for t in self.frames:

          if self.ft_to_bits[t[0]] != 0:    #Skip NO_DATA

            tbsw = BitStreamWriter()

            tbsw.write_bits(t[2], self.ft_to_bits[t[0]])

            tbsw.pad_to_byte() #Padding

            temp.append((self.ft_to_bits[t[0]], tbsw.data))

        i = 0

        done = 0

        while done != len(temp):

          for t in temp:

            if i < len(t[1]):

              bsw.write_bits(t[1][i], 8)

            if i == len(t[1]):   #Once we've passed the end, we're done with this frame

              done += 1

          i += 1

      else:

        #Write rest of the payload

        for t in self.frames:

          bsw.write_bits(t[2], self.ft_to_bits[t[0]])

          bsw.pad_to_byte() #Padding

    else:  #Not octet-aligned

      #Write TOC

      temp_frames = self.frames[:-1]

      last_frame = self.frames[-1]

      for t in temp_frames:

        bsw.write_bit(1)

        bsw.write_bits(t[0], 4)

        bsw.write_bit(t[1])

      #Last entry in TOC

      bsw.write_bit(0)

      bsw.write_bits(last_frame[0], 4)

      bsw.write_bit(last_frame[1])

      #Write rest of the payload

      for t in self.frames:

        bsw.write_bits(t[2], self.ft_to_bits[t[0]])

    return bsw.data

  ft_to_bits = [132, 177, 253, 285, 317, 365, 397, 461, 477, 40, -1, -1, -1, -1, 0, 0]

class RTPPacket:

  def __init__(self, amr_cfg, data = None):

    self.version = 0

    self.padcount = 0;

    self.marker = 0

    self.sequence = 0

    self.timestamp = 0

    self.ssrc = 0

    self.csrc = []

    self.extension_type = 0

    self.extension_data = b''

    self.payload = b''

    self.amr = AMRPayload(amr_cfg)

    self.payload_type = 0xff

    if data == None:

      return

    self.version = (data[0] & 0xC0) >> 6

    self.padcount = data[-1] if (data[0] & 0x20) else 0

    extension = data[0] & 0x10

    csrc_count = data[0] & 0x0f

    self.marker = data[1] & 0x80

    self.payload_type = data[1] & 0x7f

    (self.sequence, self.timestamp, self.ssrc) = struct.unpack_from('!HII', data[2:12])

    idx = 12

    while csrc_count:

      self.csrc.append(struct.unpack_from('!I', data[idx:idx+4]))

      idx += 4

      csrc_count -= 1

    if extension:

      self.extension_type = struct.unpack_from('!H', data[idx:idx+2])

      idx += 2

      extension_size = struct.unpack_from('!H', data[idx:idx+2]) * 4

      idx += 2

      self.extension_data = data[idx:idx+extension_size]

      idx += extension_size

    self.payload = data[idx:] if self.padcount == 0 else data[idx:-self.padcount]

    self.amr = AMRPayload(amr_cfg, self.payload)

  def write(self):

    res = b''

    res += bytes([(self.version << 6) | ((1 << 5) if self.padcount else 0) | ((1 << 4) if self.extension_type else 0) | len(self.csrc)])

    res += bytes([((1 << 7) if self.marker else 0) | self.payload_type])

    #print((self.sequence, self.timestamp, self.ssrc))

    res += struct.pack('!HII', self.sequence, self.timestamp, self.ssrc)

    for c in self.csrc:

      res += struct.pack('!I', c)

    if self.extension_type:

      res += struct.pack('!H', self.extension_type)

      res += struct.pack('!H', len(self.extension_data))

      res += self.extension_data

    res += self.amr.write()

    return res

class RTPDumpPacket:

  def __init__(self, amr_cfg, length, time, data = None):

    self.length = length

    self.time = time

    self.data = b''

    self.rtp = RTPPacket(amr_cfg)

    if data == None:

      return

    #print(self.length, 'bytes read for packet')

    self.data = data

    self.rtp = RTPPacket(amr_cfg, data)

  def write(self):

    rtp_data = self.rtp.write()

    #print('RTP len:', len(rtp_data), 'RTPDUMP len:', self.length)

    self.length = len(rtp_data)

    #print(self.length, 'bytes written for packet')

    res = struct.pack('!HHI', self.length + 8, self.length, self.time)  #TODO: We are assuming that all packets were fully captured - Verify?

    res += rtp_data

    return res

class RTPDump:

  def __init__(self, amr_cfg, f = None):

    self.error = ''    #If not empty, the reason the file is invalid

    self.destination_addr = ''

    self.destination_port = 0

    self.binary_header = b''

    self.packets = []

    if f == None:

      return

    self.read_header(f)

    if self.error != '':

      return

    self.read_dest_address(f)

    if self.error != '':

      return

    self.read_dest_port(f)

    if self.error != '':

      return

    #Just save the bytes foir the source address info and GMT start time and hen output them again when writing

    self.binary_header = f.read(16)

    data = f.read(8)

    while len(data) == 8:

      pkt_hdr = struct.unpack('!HHI', data)

      self.packets.append(RTPDumpPacket(amr_cfg, pkt_hdr[1], pkt_hdr[2], f.read(pkt_hdr[0] - 8)))

      data = f.read(8)

    print(str(len(self.packets)) + " packets read")

  def set_output_cfg(self, amr_cfg):

    for i in range(len(self.packets)):

      self.packets[i].rtp.amr.cfg = amr_cfg

  def write(self, filename):

    with open(filename, 'wb') as f:

      f.write(b'#!rtpplay1.0 ')

      f.write(self.destination_addr.encode('utf-8'))

      f.write(b'/')

      f.write(str(self.destination_port).encode('utf-8'))

      f.write(b'\n')

      f.write(self.binary_header)

      for p in self.packets:

        f.write(p.write())

  def read_header(self, f):

    if f.read(9) != b'#!rtpplay':

      self.error = 'Missing magic values'

      return

    ver = f.read(3).decode('utf-8')

    if ver != '1.0':

      self.error = 'Invalid version'

      return

    if f.read(1) != b' ':

      self.error = 'Missing space after version'

      return

    return ver

  def read_until(self, f, char, limit):

    b = f.read(1)

    data = b''

    while b != char:

      data += b

      if len(data) >= limit:

         return b''

      b = f.read(1)

    return data

  def read_dest_address(self, f):

    data = self.read_until(f, b'/', 40)

    text = data.decode('utf-8')

    text = text.split('.')

    if len(text) != 4:

      self.error = 'Unable to find valid destination address'

      return

    addr = []

    try:

      for t in text:

        addr.append(str(int(t)))

    except ValueError:

      self.error = 'Unable to find valid destination address'

      return

    self.destination_addr = '.'.join(addr)

  def read_dest_port(self, f):

    data = self.read_until(f, b'\n', 40)

    text = data.decode('utf-8')

    try:

      self.destination_port = int(text)

    except ValueError:

      self.error = 'Unable to find read valid destination port'

def read_rtp_dump(filename, amr_cfg):

  with open(filename, 'rb') as f:

    return RTPDump(amr_cfg, f)

def transform(rtp_dump, amr_cfg, trans_cfg):

  if trans_cfg == None:

    return rtp_dump

  res = RTPDump(amr_cfg)

  res.destination_addr = rtp_dump.destination_addr

  res.destination_port = rtp_dump.destination_port

  res.binary_header = rtp_dump.binary_header

  time = rtp_dump.packets[0].time

  first_rtp = rtp_dump.packets[0].rtp

  frames = []

  for p in rtp_dump.packets:

    frames.extend(p.rtp.amr.frames)

  #print(len(frames), 'frames in total')

  for i in range(0, len(frames) + trans_cfg.redundant_frames + trans_cfg.redundant_no_datas, trans_cfg.normal_frames):

    #Create new packet

    newp = RTPDumpPacket(amr_cfg, 0, time)

    time += 20 * trans_cfg.normal_frames

    newp.rtp = copy(first_rtp)

    #Overwite timetamp and marker and amr payloads

    newp.rtp.timestamp = first_rtp.timestamp + i * (20 * 16)

    newp.rtp.sequence = first_rtp.sequence + int(i/trans_cfg.normal_frames)

    newp.rtp.marker = 1 if i == 0 else 0

    redundant = []

    no_datas = []

    normal = []

    #Insert redundant frames

    for k in range(trans_cfg.redundant_frames):

      j = k - trans_cfg.redundant_frames - trans_cfg.redundant_no_datas

      if i + j >= 0:

        if i + j < len(frames):

          redundant.append(frames[i + j])

      else:

        redundant.append((15, 1, b'')) #use NO_DATA as the redundant data (because we don't have the frame available (off the leading edge))

    for k in range(trans_cfg.redundant_no_datas):

        no_datas.append((15, 1, b''))

    #Now the normal frames

    for j in range(trans_cfg.normal_frames):

      if i + j < len(frames):

        normal.append(frames[i + j])

    #Construct so that there are no trailing NO_FRAMES

    temp = copy(redundant)

    if len(normal) > 0:

      temp.extend(no_datas)

    temp.extend(normal)

    #print('New packet with', len(temp), 'AMR-WB frames', len(redundant), len(no_datas), len(normal))

    if len(temp) > 0: #Avoid empty packets

      newp.rtp.amr.frames = temp

      res.packets.append(newp)

  return res

def generate_filename(amr_cfg, trans_cfg = None):

  res = 'output_' + ('oa' if amr_cfg.octet_aligned else 'be')

  if amr_cfg.robust_sorting:

    res += '_rs'

  if amr_cfg.interleaved:

    res += '_il'

  if trans_cfg == None:

    res += '_nt'

  else:

    res += '_' + str(trans_cfg.normal_frames) + 'f'

    if trans_cfg.redundant_frames:

      res += '_' + str(trans_cfg.redundant_frames) + 'r'

    if trans_cfg.redundant_no_datas:

      res += '_' + str(trans_cfg.redundant_no_datas) + 'nd'

  res += '.rtpdump'

  return res

def write_rtp_dump(rtp_dump, output_cfg, trans_cfg):

  rtp_dump.set_output_cfg(output_cfg)

  output_filename = generate_filename(output_cfg, trans_cfg)

  print('Writing', output_filename)

  rtp_dump.write(output_filename)

input_cfg = AMRConfig(input_octet_aligned, input_robust_sorting, input_interleaved)

rtp_dump = read_rtp_dump(input_filename, input_cfg)

if rtp_dump.error != '':

  print('Failure to read file:', rtp_dump.error)

  sys.exit(0)

#Generate and execute every config combination desired

output_configs = []

for align in output_octet_aligned:

  if align:

    for rs in output_robust_sorting:

      for il in output_interleaved:

        output_configs.append(AMRConfig(align, rs, il))

  else:

    output_configs.append(AMRConfig(align, False, False))

trans_configs = []

for trans in output_transform:

  if trans:

    for f in output_normal_frames:

      for r in output_redundant_frames:

        if r != 0:    #Only allow NO_DATA if redundancy used

          for nd in output_redundant_no_datas:

            trans_configs.append(TransformConfig(f, r, nd))

        else:

          trans_configs.append(TransformConfig(f, r, 0))

  else:

    trans_configs.append(None)

#Execute

for oc in output_configs:

  for tc in trans_configs:

    output_rtp_dump = transform(rtp_dump, oc, tc)

    write_rtp_dump(output_rtp_dump, oc, tc)


# import the necessary packages
from __future__ import print_function
from bin.shapedetector import ShapeDetector
from PIL import Image
from PIL import ImageTk
import numpy as np
import tkinter as tki
import threading
import imutils
import cv2
import sqlite3 as sq

class PhotoBoothApp:

    def __init__(self, vs, outputPath):
        # store the video stream object and output path, then initialize
        # the most recently read frame, thread for reading frames, and
        # the thread stop event
        self.vs = vs
        self.outputPath = outputPath
        self.frame = None
        self.thread = None
        self.stopEvent = None
        self.shape = None

        # initialize the root window and image panel
        self.root = tki.Tk()
        self.panel = None

        # start a thread that constantly pools the video sensor for
        # the most recently read frame
        self.stopEvent = threading.Event()
        self.thread = threading.Thread(target=self.videoLoop, args=())
        self.thread.start()

        # Select from which ever compound lift is selected
        self.frame2 = tki.LabelFrame(self.root)
        self.current_row = 0

        #create a label for databases entry
        self.jb_label = tki.Label(self.frame2, text="Jenis Buku: ", font=18)
        self.jb_label.grid(row=self.current_row, column=0)
        self.jb_text = tki.StringVar()
        self.jb_entry = tki.Entry(self.frame2, textvariable=self.jb_text, font=18)
        self.jb_entry.grid(row=self.current_row, column=1)
        self.current_row += 1

        self.k_label = tki.Label(self.frame2, text="Kode: ", font=18)
        self.k_label.grid(row=self.current_row, column=0)
        self.k_text = tki.StringVar()
        self.k_entry = tki.Entry(self.frame2, textvariable=self.k_text, font=18)
        self.k_entry.grid(row=self.current_row, column=1)
        self.current_row += 1

        self.n_label = tki.Label(self.frame2, text="Nama Pengarang: ", font=18)
        self.n_label.grid(row=self.current_row, column=0)
        self.n_text = tki.StringVar()
        self.n_entry = tki.Entry(self.frame2, textvariable=self.n_text, font=18)
        self.n_entry.grid(row=self.current_row, column=1)
        self.current_row += 1

        self.p_label = tki.Label(self.frame2, text="Penerbit: ", font=18)
        self.p_label.grid(row=self.current_row, column=0)
        self.p_text = tki.StringVar()
        self.p_entry = tki.Entry(self.frame2, textvariable=self.p_text, font=18)
        self.p_entry.grid(row=self.current_row, column=1)
        self.current_row += 1

        self.ko_label = tki.Label(self.frame2, text="Kota: ", font=18)
        self.ko_label.grid(row=self.current_row, column=0)
        self.ko_text = tki.StringVar()
        self.ko_entry = tki.Entry(self.frame2, textvariable=self.ko_text, font=18)
        self.ko_entry.grid(row=self.current_row, column=1)
        self.current_row += 1

        self.t_label = tki.Label(self.frame2, text="Tahun: ", font=18)
        self.t_label.grid(row=self.current_row, column=0)
        self.t_text = tki.StringVar()
        self.t_entry = tki.Entry(self.frame2, textvariable=self.t_text, font=18)
        self.t_entry.grid(row=self.current_row, column=1)
        self.current_row += 1

        self.kol_text = tki.StringVar()
        self.gab_label = tki.Message(self.frame2, textvariable=self.kol_text, font=18, width=500)
        self.gab_label.grid(row=self.current_row)
        self.current_row += 1
        self.current_row += 1

        # create code input
        self.kin_label = tki.Label(self.frame2, text="Input kode: ", font=18)
        self.kin_label.grid(row=self.current_row, column=0)
        self.current_row += 1
        self.kin_text = tki.StringVar()
        self.kin_entry = tki.Entry(self.frame2, textvariable=self.kin_text, font=18)
        self.kin_entry.grid(row=self.current_row, column=0)
        self.inkode = tki.Button(self.frame2, text="input",
                               command=self.inputkode)
        self.inkode.grid(row=self.current_row, column=1)
        self.current_row += 1

        # create the button
        self.btn1 = tki.Button(self.frame2, text="Tambah Koleksi",
                               command=self.pluskoleksi)
        self.btn1.grid(row=self.current_row, column=0)
        self.btn2 = tki.Button(self.frame2, text="New Capture",
                               command=self.resume)
        self.current_row += 1
        self.btn2.grid(row=self.current_row, column=1)
        self.btn1 = tki.Button(self.frame2, text="Kurang Koleksi",
                               command=self.minkoleksi)
        self.btn1.grid(row=self.current_row, column=0)
        self.frame2.pack(side="right", fill='y')

        # set a callback to handle when the window is closed
        self.root.wm_title("Book Code")
        self.root.wm_protocol("WM_DELETE_WINDOW", self.onClose)

    def videoLoop(self):
        # DISCLAIMER:
        # I'm not a GUI developer, nor do I even pretend to be. This
        # try/except statement is a pretty ugly hack to get around
        # a RunTime error that Tkinter throws due to threading

        # threshold up and low din hsv
        lower = {'blue': (110, 100, 100), 'red': (-10, 100, 100), 'green': (40, 70, 70),
                 'yellow': (20, 100, 117)}  # assign new item lower['blue'] = (93, 10, 0)
        upper = {'blue': (130, 255, 255), 'red': (10, 255, 255), 'green': (70, 255, 255),  'yellow': (40, 255, 255)}

        # database session
        con = sq.connect('data_buku.db')  # dB browser for sqlite needed
        con.text_factory = str
        self.c = con.cursor()  # SQLite command, to connect to db so 'execute' method can be called

        try:
            # keep looping over frames until we are instructed to stop
            while not self.stopEvent.is_set():
                # grab the frame from the video stream and resize it to
                # have a maximum width of 300 pixels
                self.frame = self.vs.read()
                self.frame = imutils.resize(self.frame, width=300)

                # OpenCV represents images in BGR order; however PIL
                # represents images in RGB order, so we need to swap
                # the channels, then convert to PIL and ImageTk format
                blurred = cv2.GaussianBlur(self.frame, (11, 11), 0)
                image = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)

                #make a call for ShapeDetector class
                sd = ShapeDetector()

                # for each color in dictionary check object in frame
                for self.key, value in upper.items():
                    # construct a mask for the color from dictionary`1, then perform
                    # a series of dilations and erosions to remove any small
                    # blobs left in the mask
                    kernel = np.ones((9, 9), np.uint8)
                    mask = cv2.inRange(image, lower[self.key], upper[self.key])
                    mask = cv2.erode(mask, None, iterations=2)
                    mask = cv2.dilate(mask, None, iterations=2)

                    # find contours in the mask and initialize the current
                    # (x, y, w, h) center of the rectangle
                    cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
                                            cv2.CHAIN_APPROX_SIMPLE)[-2]
                    # cnts = cnts[0] if imutils.is_cv2() else cnts[1]
                    center = None

                    # only proceed if at least one contour was found
                    if len(cnts) > 0:
                        # find the largest contour in the mask, then use
                        # it to compute the minimum enclosing circle and
                        # centroid
                        c = max(cnts, key=cv2.contourArea)
                        (x, y, w, h) = cv2.boundingRect(c)
                        M = cv2.moments(c)
                        if (M["m00"] == 0):
                            M["m00"] = 1
                        center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

                        if (x+y+w+h) >400:
                            #Do when reach the minimum contours length
                            self.shape = sd.detect(c)
                            cv2.putText(image, self.key, (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 0), 2)
                            # c = c.astype("float")
                            c = c.astype("int")
                            cv2.drawContours(image, [c], -1, (0, 0, 0), 2)
                            cv2.putText(image, self.shape, (x + 15, y + 15), cv2.FONT_HERSHEY_SIMPLEX,
                                    0.5, (0, 0, 0), 2)
                            self.txt = self.key + " " + self.shape

                            #Gets data from database
                            plus = 'FROM buku INNER JOIN kode_buku ON buku.id = kode_buku.id WHERE kode_buku.kode = "' + self.txt + '"'
                            self.c.execute('SELECT jenis_buku ' + plus)

                            data = self.c.fetchall()  # Gets the data from the table

                            # Inserts record row by row in label
                            for row in data:
                                self.jb_text.set(row)

                            self.c.execute(
                                'SELECT buku.kode ' + plus)

                            data2 = self.c.fetchall()

                            for row in data2:
                                self.k_text.set(row)

                            self.c.execute(
                                'SELECT nama_pengarang ' + plus)

                            data3 = self.c.fetchall()

                            for row in data3:
                                self.n_text.set(row)

                            self.c.execute(
                                'SELECT penerbit ' + plus)

                            data4 = self.c.fetchall()

                            for row in data4:
                                self.p_text.set(row)

                            self.c.execute(
                                'SELECT kota ' + plus)

                            data5 = self.c.fetchall()

                            for row in data5:
                                self.ko_text.set(row)

                            self.c.execute(
                                'SELECT tahun ' + plus)

                            data6 = self.c.fetchall()

                            for row in data6:
                                self.t_text.set(row)

                            self.c.execute("SELECT 'Tersedia ' || koleksi || ' koleksi dari total ' || total || ' koleksi' " + plus)
                            data7 = self.c.fetchall()

                            for row in data7:
                                self.kol_text.set(row)

                            con.commit()
                            self.stopEvent.set()

                #Show image in GUI
                image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR)
                image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                image = Image.fromarray(image)
                image = ImageTk.PhotoImage(image)

                # if the panel is not None, we need to initialize it
                if self.panel is None:
                    self.panel = tki.Label(image=image)
                    self.panel.image = image
                    self.panel.pack(side="left", padx=10, pady=10)

                # otherwise, simply update the panel
                else:
                    self.panel.configure(image=image)
                    self.panel.image = image

        except RuntimeError:
            print("[INFO] caught a RuntimeError")

    def takeSnapshot(self):
        #Stop the Thread
        self.stopEvent.set()

    def resume(self):
        #Resume the Thread
        self.stopEvent = threading.Event()
        self.thread = threading.Thread(target=self.videoLoop, args=())
        self.thread.start()

    def onClose(self):
        # set the stop event, cleanup the camera, and allow the rest of
        # the quit process to continue
        print("[INFO] closing...")
        self.stopEvent.set()
        self.vs.stop()
        self.root.quit()

    def pluskoleksi(self):
        # database session
        con = sq.connect('data_buku.db')  # dB browser for sqlite needed
        con.text_factory = int
        c = con.cursor()  # SQLite command, to connect to db so 'execute' method can be called
        b = con.cursor()
        d = con.cursor()

        kode = self.k_text.get()
        kode = kode.translate({ord(i): None for i in "('),"})

        plus = 'FROM buku WHERE kode = "' + kode + '"'
        c.execute('SELECT buku.id ' + plus)

        b.execute('SELECT koleksi ' + plus)

        d.execute('SELECT total ' + plus)

        data1 = c.fetchall()
        data1 = data1[0][0]

        data2 = b.fetchall()
        data2 = data2[0][0]

        data2 = int(data2)
        data3 = d.fetchall()
        data3 = data3[0][0]

        data3 = int(data3)
        if data2 < data3:
            w = 1 + data2
            c.execute(
                    'UPDATE buku SET koleksi = ' + str(w) + ' WHERE buku.id = ' + str(data1))
            print("done")
        else:
            print("Maaf, koleksi buku sudah lengkap")

        con.commit()

    def inputkode(self):
        con = sq.connect('data_buku.db')  # dB browser for sqlite needed
        c = con.cursor()  # SQLite command, to connect to db so 'execute' method can be called

        self.kodetext = self.kin_text.get()

        plus1 = 'FROM buku WHERE kode = "' + self.kodetext + '"'
        c.execute('SELECT jenis_buku ' + plus1)

        data = c.fetchall()  # Gets the data from the table

        # Inserts record row by row in label
        for row in data:
            self.jb_text.set(row)

        c.execute(
            'SELECT buku.kode ' + plus1)

        data2 = c.fetchall()

        for row in data2:
            self.k_text.set(row)

        c.execute(
            'SELECT nama_pengarang ' + plus1)

        data3 = c.fetchall()

        for row in data3:
            self.n_text.set(row)

        c.execute(
            'SELECT penerbit ' + plus1)

        data4 = c.fetchall()

        for row in data4:
            self.p_text.set(row)

        c.execute(
            'SELECT kota ' + plus1)

        data5 = c.fetchall()

        for row in data5:
            self.ko_text.set(row)

        c.execute(
            'SELECT tahun ' + plus1)

        data6 = c.fetchall()

        for row in data6:
            self.t_text.set(row)

        c.execute(
            "SELECT 'Tersedia ' || koleksi || ' koleksi dari total ' || total || ' koleksi' " + plus1)
        data7 = c.fetchall()

        for row in data7:
            self.kol_text.set(row)

        con.commit()
        self.stopEvent.set()

    def minkoleksi(self):
        # database session
        con = sq.connect('data_buku.db')  # dB browser for sqlite needed
        con.text_factory = int
        c = con.cursor()  # SQLite command, to connect to db so 'execute' method can be called
        b = con.cursor()
        d = con.cursor()

        kode = self.k_text.get()
        kode = kode.translate({ord(i): None for i in "('),"})

        plus = 'FROM buku WHERE kode = "' + kode + '"'
        c.execute('SELECT buku.id ' + plus)

        b.execute('SELECT koleksi ' + plus)

        d.execute('SELECT total ' + plus)

        data1 = c.fetchall()
        data1 = data1[0][0]

        data2 = b.fetchall()
        data2 = data2[0][0]

        data3 = d.fetchall()
        data3 = data3[0][0]

        data3 = int(data3)
        data2 = int(data2)
        if data2 > 0:
            w = data2 - 1
            c.execute(
                    'UPDATE buku SET koleksi = ' + str(w) + ' WHERE buku.id = ' + str(data1))
            print("done")
            con.commit()
        else:
            print("Maaf, koleksi telah kosong")


from PIL import Image
import time
import numpy as np
import argparse
import random
from progressbar import ProgressBar
import cv2

import utils
import interval_tools as tools

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

p = argparse.ArgumentParser(description="pixel mangle an image")
p.add_argument("-ins", "--inputs",	help="input]", required=True)

__args = p.parse_args()

image_input_paths = __args.inputs

print ("Input: " + str(__args.inputs))

##########################################
#random thresholds

thresholds_dict = {1 : [[0.1 , 0.3 ], [0.7 , 0.9 ]],
			  2 : [[0.2 , 0.6 ], [0.4 , 0.8 ]],
			  3 : [[0.05, 0.42], [0.75, 0.9 ]],
			  4 : [[0.25, 0.38], [0.55, 0.95]],
			  5 : [[0.4 , 0.6 ], [0.05, 0.8 ]],
			  6 : [[0.5 , 0.7 ], [0.3 , 0.9 ]]}

def rndvar(value):

	value += random.uniform(0.1, -0.1)
	if value < 0:
		return 0
	elif value > 1:
		return 1
	else:
		return value

def rand_thresholds():

	thr = thresholds_dict[random.randint(1, 6)]

	t1 = rndvar(thr[0][0])
	t2 = rndvar(thr[1][0])
	t3 = 0
	u1 = rndvar(thr[0][1])
	u2 = rndvar(thr[1][1])
	u3 = 1

	thresholds = [t1, u1, t2, u2, t3, u3]

	return thresholds

##########################################
# random interval functions

functions = {1 : "v",
			       2 : "h",
			       3 : "s",
			       4 : "l",
			       5 : "y",
			       6 : "i",
			       7 : "q",
			       8 : "max_rgb",
			       9 : "min_rgb",
			       10 : "max_hsv",
			       11 : "min_hsv",
			       12 : "max_yiq",
			       13 : "min_yiq",
			       14 : "r",
			       15 : "g",
			       16 : "b"}

def rand_intfunc():
	i1 = utils.read_pixel_function(functions[random.randint(1, 16)])
	i2 = utils.read_pixel_function(functions[random.randint(1, 16)])
	i3 = utils.read_pixel_function(functions[random.randint(1, 16)])

	int_funcs = [i1, i2, i3]

	return int_funcs

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

def get_cmd(param_string):
	p_string = param_string.split('__')
	thrshs = p_string[0].split('-')
	funcs = p_string[1].split('-')

	head = 'python gcp_newsort.py -ins '
	inp = 'sparamtest.png'

	threshold_string = f' -t {thrshs[0]} -tt {thrshs[2]} -ttt {thrshs[4]} -u {thrshs[1]} -uu {thrshs[3]} -uuu {thrshs[5]} '
	w_int_f = f' -i {funcs[0]} -ii {funcs[1]} -iii {funcs[2]} '

	cmd = f'%s%s%s%s\n' % (head, inp, w_int_f, threshold_string)
	print(cmd)

	return cmd

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

def evaluate(count_dict):

	triple = (count_dict[1],count_dict[2],count_dict[3])
	diff12 = max(triple[0],triple[1])-min(triple[0],triple[1])
	diff23 = max(triple[1],triple[2])-min(triple[1],triple[2])
	diff31 = max(triple[2],triple[0])-min(triple[2],triple[0])

	d_value = diff12 + diff23 + diff31
	print(d_value)

	return d_value

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

def main(image_input_paths):

	input_img = Image.open(image_input_paths)
	img_x = input_img.size[1]
	img_y = input_img.size[0]
	input_img = input_img.convert('RGB')

	bands = input_img.getbands()
	bl = len(bands)

	pixels = np.array(input_img.getdata(), np.uint8).reshape(input_img.size[1], input_img.size[0], bl)

	sdim = int(min(input_img.size)/(max(input_img.size)/100))
	ldim = 100

	if input_img.size[0] > input_img.size[0]:
		new_dim = tuple([ldim, sdim])
	else:
		new_dim = tuple([sdim, ldim])

	pixels = cv2.resize(pixels, new_dim)

	siz = input_img.size

	param_sets = []
	param_eval = []

	for n in range(300):   # # # # # # # # # # # # # # # # # # # # # # # # # # # #   Poolgröße
		thresholds = rand_thresholds()
		int_funcs = rand_intfunc()

		interval_array = tools.find_intervals(pixels, thresholds, int_funcs)

		unique, counts = np.unique(interval_array, return_counts=True)
		count_dict = dict(zip(unique, counts))

		print(unique)

		if len(unique) < 3:
			pass

		else:
			value = evaluate(count_dict)

			param_sets.append("%s-%s-%s-%s-0-1__%s-%s-%s" % (thresholds[0],thresholds[1],thresholds[2],thresholds[3],int_funcs[0],int_funcs[1],int_funcs[2]))
			param_eval.append(value)

		count_dict.clear()

	param_pairs = list(zip(param_sets, param_eval))
	param_pairs.sort(key=lambda pair: pair[1], reverse=True)

	return param_pairs

	#for l in range(0, 4):
	#	print(get_cmd(param_pairs[l][0]), '+\n')
	#
	#for l in range(-1, -5):
	#	print(get_cmd(param_pairs[l][0]), '/\n')
    #
	#for l in range(round(len(param_pairs)/2-2), round(len(param_pairs)/2+3)):
	#	print(get_cmd(param_pairs[l][0]), '-\n')

if __name__ == "__main__":
	paths = image_input_paths
	erg = main(paths)

	with open('erg.txt', 'w') as f:
		for i in erg:
			f.write(get_cmd(str(i[0])) + '\n')


#coding=utf-8
#lizhenwei		2017-08-04		create

import os,sys

from PyQt5.QtCore import (Qt,pyqtSignal,pyqtSlot,QModelIndex)
from PyQt5.QtGui import (QBrush,QStandardItemModel,QStandardItem,QMouseEvent)
from PyQt5.QtWidgets import (QApplication,QTableView,QItemDelegate,QAbstractItemView)

class stateDelegate(QItemDelegate):
	'状态item托管'
	sgReverseState = pyqtSignal(QModelIndex)

	def __init__(self,colno=2,enClick=False):
		super(stateDelegate,self).__init__()

		self.colno = colno
		self.enClick = enClick

	def paint(self, painter, option, index):
		if index.column() == self.colno:
			val = index.data()
			d = min(option.rect.width(),option.rect.height())
			painter.save()
			if 0 == val:
				# 0为绿色
				painter.setBrush(QBrush(Qt.green))
			elif 1 == val:
				painter.setBrush(QBrush(Qt.red))
			painter.setPen(Qt.NoPen);
			painter.drawEllipse(option.rect.center(),d>>1,d>>1);
			painter.restore();

		else:
			super(stateDelegate,self).paint(painter,option,index)

	def editorEvent(self,event, model, option, index):
		msEvent = QMouseEvent(event)
		if (msEvent.type() == msEvent.MouseButtonRelease) and option.rect.contains(msEvent.x(),msEvent.y()):
			if self.enClick:
				self.sgReverseState.emit(index)

		return super(stateDelegate,self).editorEvent(event,model,option,index);

class ioTableView(QTableView):

	# 设置bit状态, addr,state
	sgSetBitState = pyqtSignal(int,int)

	def __init__(self,mode):
		'mode: 0输入，1输出'
		super(ioTableView,self).__init__()
		self.mode = mode
		self.initUI()

	def initUI(self):
		self.itemModel = QStandardItemModel()

		headers = ("地址","名称","状态")
		self.itemModel.setHorizontalHeaderLabels(headers)

		self.stateDelegate = stateDelegate(2, self.mode==1)

		self.setModel(self.itemModel)
		self.setItemDelegateForColumn(2,self.stateDelegate)

		stateSize = 36
		self.verticalHeader().setDefaultSectionSize(stateSize*1.2)
		self.verticalHeader().hide()
		self.setColumnWidth(2,stateSize)
		self.setEditTriggers(QAbstractItemView.NoEditTriggers)
		self.setSelectionMode(QAbstractItemView.NoSelection)

		self.stateDelegate.sgReverseState.connect(self.onReverseState)

		# self.setSortingEnabled(True)
		# self.sortByColumn(0,Qt.AscendingOrder)

	def loadIOCfgDict(self,iodict):
		'加载io列表'
		for idname,cfg in iodict.items():
			addrItem = QStandardItem()
			addrItem.setData(cfg["addr"],Qt.EditRole)
			nameItem = QStandardItem(cfg["name"])
			stateItem = QStandardItem()
			stateItem.setData(cfg["state"],Qt.EditRole)
			self.itemModel.appendRow([addrItem,nameItem,stateItem])
		self.sortByColumn(0,Qt.AscendingOrder)

	@pyqtSlot(QModelIndex)
	def onReverseState(self,idx):
		'翻转电平'
		preVal = idx.data()
		curVal = preVal^1
		self.itemModel.setData(idx,curVal,Qt.EditRole)
		addr = self.itemModel.index(idx.row(),0).data()
		self.sgSetBitState.emit(addr,curVal)

	@pyqtSlot(int,int)
	def onSetBitState(self,addr,state):
		for i in range(self.itemModel.rowCount()):
			taddr = self.itemModel.item(i,0).data(Qt.EditRole)
			if addr == taddr:
				self.itemModel.item(i,2).setData(state,Qt.EditRole)
				break

def test():
	app = QApplication(sys.argv)
	inview = ioTableView(1)
	in0 = {"addr":0,"name":"test0","state":0}
	in1 = {"addr":1,"name":"test1","state":1}
	iodict = {"in0":in0, "in1":in1}
	inview.loadIOCfgDict(iodict)
	inview.show()
	sys.exit(app.exec_())

if __name__ == '__main__':
	test()


# uncompyle6 version 3.7.4
# Python bytecode 2.6 (62161)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: build/bdist.macosx-10.6-x86_64/egg/netlogger/tests/component/testParseHsiXfer.py
# Compiled at: 2009-12-08 17:43:28
"""
Unittests for sge.py
"""
__author__ = 'Shreyas Cholia scholia@lbl.gov'
__rcsid__ = '$Id$'
import unittest
from netlogger import nlapi
from netlogger.tests import shared
from netlogger.parsers.modules import hsi_xfer
from netlogger.parsers.base import NLFastParser

class TestCase(shared.BaseParserTestCase):
    basename = 'hsi_xfer-'
    parser_class = hsi_xfer.Parser

    def __init__(self, *args, **kw):
        shared.BaseParserTestCase.__init__(self, *args, **kw)
        self._p = NLFastParser()
        nlapi.clearGuid()

    def testBasic(self):
        """Test for correctly parsed lines
        """

        def _test(e, num):
            self.assertEquals(len(e), 14, "Expected 14 items, got %d in '%s'" % (
             len(e), e))
            self.failIf(int(e['uid']) < 0, 'Invalid UID')
            self.failIf(int(e['pid']) < 0, 'Invalid PID')
            self.assertNotEquals(e['hostname'], '')

        filename = 'xferlog'
        expected = len(list(file(self.getFullPath(filename))))
        self.checkGood(filename=filename, test=_test, num_expected=expected)

def suite():
    return shared.suite(TestCase)

if __name__ == '__main__':
    shared.main()


import sys
import random
sys.path.append('src')
try:
    from game import Game
    from unit import Scout

    from strategies.lesson_strategies.level_1.berserker_strategy_level_1 import BerserkerStrategyLevel1
    from strategies.lesson_strategies.level_1.random_strategy_level_1 import RandomStrategyLevel1
    from strategies.lesson_strategies.level_1.dumb_strategy_level_1 import DumbStrategyLevel1
    from strategies.lesson_strategies.level_1.flanker_strategy_level_1 import FlankerStrategyLevel1
    from strategies.lesson_strategies.level_1.ripple_strategy_level_1 import RippleStrategyLevel1

    from player import Player
    from otest import do_assert, assert_bool, color_print, cstring
except ImportError as e:
    print(e)

print("Playing games...")

def matchup(type1, type2):
    wins = [0, 0]
    games = 1000
    for _ in range(games):
        game = Game((5, 5), logging=False, rendering=False, game_level=1, die_size=10)
        first_player = random.choice([0, 1])
        p1 = Player(type1(first_player), "Player1", (2, 0), game)
        p2 = Player(type2(1-first_player), "Player2", (2, 4), game)
        if first_player == 0:
            game.add_player(p1)
            game.add_player(p2)
        else:
            game.add_player(p2)
            game.add_player(p1)

        game.start()

        if game.run_until_completion(max_turns=100):
            wins[[type1, type2].index(type(game.winner.strat))] += 1
        else:
            print("Tie!")
    wins = [w/games for w in wins]
    return wins

print(cstring("\n &5Berserker vs Dumb Strategy wins"))
print(matchup(BerserkerStrategyLevel1, DumbStrategyLevel1))

print(cstring("\n &5Berserker vs Random Strategy wins"))
print(matchup(BerserkerStrategyLevel1, RandomStrategyLevel1))

print(cstring("\n &5Flanker vs Random Strategy wins"))
print(matchup(FlankerStrategyLevel1, RandomStrategyLevel1))

print(cstring("\n &5Flanker vs Berserker Strategy wins"))
print(matchup(FlankerStrategyLevel1, BerserkerStrategyLevel1))

print(cstring("\n &5Ripple vs Berserker Strategy wins"))
print(matchup(RippleStrategyLevel1, BerserkerStrategyLevel1))

print(cstring("&4All matchups passed!"))


# Desenvolva um programa que leia seis números inteiros e
#  mostre a soma apenas daqueles que forem pares.
#  Se o valor digitado for ímpar, desconsidere-o.#
soma = cont = 0
for c in range(1, 7):
    num = int(input('digite o {} valor: '.format(c)))
    if num %2 ==0:
        soma += num
        cont += 1
print('voce digitou {} valores pares \n e a soma dos pares é {}'. format(cont, soma))


######################
# Updating GTM tags  #
######################

import argparse
import httplib2
from apiclient.discovery import build
from oauth2client import client
from oauth2client import file
from oauth2client import tools
from pprint import pprint

CLIENT_SECRETS = 'client_secrets.json'
SCOPE = ['https://www.googleapis.com/auth/tagmanager.readonly',
         'https://www.googleapis.com/auth/tagmanager.edit.containers',
         'https://www.googleapis.com/auth/tagmanager.edit.containerversions',
         "https://www.googleapis.com/auth/tagmanager.publish"]
# SCOPE = ['https://www.googleapis.com/auth/tagmanager.edit.containers']

# Parse command-line arguments
parser = argparse.ArgumentParser(parents = [tools.argparser])
flags = parser.parse_args()

# Set up a Flow object to be used if we need to authenticate
flow = client.flow_from_clientsecrets(
    CLIENT_SECRETS,
    scope = SCOPE,
    message = tools.message_if_missing(CLIENT_SECRETS))
# Prepare credentials, and authorize the HTTP object with them.
# If the credentials don't exist or are invalid, run through the native client
# flow. The Storage object will ensure that if successful, the good
# credentials will be written back to a file.
storage = file.Storage('tagmanager.dat')
credentials = storage.get()
if credentials is None or credentials.invalid:
    credentials = tools.run_flow(flow, storage, flags)
http = credentials.authorize(http = httplib2.Http())

# Build the service object
service = build('tagmanager', 'v1', http = http)

# # Input version
# account_id = raw_input('Please enter account ID: ')
# container_id = raw_input('Please enter container ID: ')

account_id = '126511049'
container_id = '1571633'

# Get the list of all tags
tags = service.accounts().containers().tags().list(
    accountId = account_id,
    containerId = container_id
).execute()

# Make a local list of tags you want to update
tags_to_update = []
for tag in tags.get('tags', []):
    # Specify conditions on tag you want to update here
    if '_import_1' in tag['name'] and tag['type'] == 'ua':
        tags_to_update.append(tag)

# Make local changes to tags you want to update here
for tag in tags_to_update:
    # Here we changed tags' names:
    # tag['name'] = 'IF ' + tag['name']
    # Here we change UA tracking ID:
    next((x for x in tag['parameter'] if x['key'] == 'trackingId'), None)['value'] = '{{00 UA ID Interflora.pl}}'

# Push the changes to GTM by passing the list with changed tags
for tag in tags_to_update:
    del tag['accountId']
    del tag['containerId']
    tag_ID = tag.get('tagId')
    del tag['tagId']

    tags_update = service.accounts().containers().tags().update(
        accountId = account_id,
        containerId = container_id,
        tagId = tag_ID,
        body = tag
    ).execute()

quit()


# -*- coding: utf-8 -*-
"""
Created on Wed May 27 16:22:48 2020

@author: JIHU
"""
from initialize import *
from pyeasyga import pyeasyga
import random
import numpy as np
import pickle
from itertools import product
from mip import Model, xsum, maximize, BINARY, INTEGER, CONTINUOUS

import time

compT0 = time.clock()

def distance(pointc, point): ##이동거리
    dist = abs(pointc[0]-point[0])+abs(pointc[1]-point[1])
    return dist

with open('data/Triplist_original.pickle','rb') as f:
    Triplist = pickle.load(f)
Triplist = Triplist[:20]

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

def makeNodelist(data):
    global station_o
    global station_d
    ODlist = [T.OD for T in data]
    Plist = [(OD[0],OD[1]) for OD in ODlist]
    Dlist = [(OD[2],OD[3]) for OD in ODlist]
    nodelist = [station_o]+Plist+Dlist+[station_d]
    return nodelist

def makeSet(data):
    global v_avg
    nodelist = makeNodelist(data)
    n = len(data)
    P = list(range(1,n+1))
    D = list(range(n+1,2*n+1))
    PD = P+D
    N = [0]+PD+[2*n+1]
    t = [[1/6 + distance(nodelist[i], nodelist[j])/v_avg for j in N] for i in N]#travel time b/w each node
    q = [0]+[1]*n+[-1]*n+[0]#load at node i, pickup 1 delivery -1
    c = [[1 if ((i in P) and (j in D)) else 0 for j in N] for i in N]
    return nodelist,n,P,D,PD,N,t,q,c

def makeData(data): #x,v,B,Q
    nodelist,n,P,D,PD,N,t,q,c = makeSet(data)
    x = [[(i,j) for j in N] for i in N]
    x = sum(x,[])
    t = [[1/6 + distance(nodelist[i], nodelist[j])/v_avg for j in N] for i in N]#travel time b/w each node
    t = sum(t,[])
    x = list(zip(x,t)) # ((x,y),t)
#    v = [(i,i,i) for i in PD]
#    B = [i for i in N]
#    Q = [i for i in N]
    return x#+v+B+Q

def makeDvar(individual):
#    nodelist,n,P,D,PD,N,t,q,c = makeSet(data)
    x = individual[:(4* (n+1)**2)]
#    v = individual[(4* (n+1)**2):(4* (n+1)**2)+len(N)]
#    B = individual[(4* (n+1)**2)+len(N):(4* (n+1)**2)+len(N)*2]
#    Q = individual[(4* (n+1)**2)+len(N)*2:]

    tmp = int(np.sqrt(len(x)))
    x=[x[i:i+tmp] for i in range(0,len(x),tmp)]

    return x#,v,B,Q

def timecheck(route):
    global Triplist
    nodelist,n,P,D,PD,N,t,q,c = makeSet(Triplist)

    timem = 0
    for r in route:
        time = 0
        r=[0]+r+[N[-1]]
        for i in range(len(r)-1):
            time += t[r[i]][r[i+1]]
        timem = max(timem, time)
    return timem

def calculate():
    global wd
    nodelist,n,P,D,PD,N,t,q,c = makeSet(Triplist)
    savename = wd+'/garoute.pickle'
    with open(savename,'rb') as f:
        route = pickle.load(f)

    B = [0]*len(N)
    Q = [0]*len(N)
    for r in route:
        for i in range(len(r)-1):
            B[r[i+1]] = B[r[i]]+t[r[i]][r[i+1]]
            Q[r[i+1]] = Q[r[i]]+q[r[i+1]]
    return B,Q

############################################################
nodelist,n,P,D,PD,N,t,q,c = makeSet(Triplist)
data = makeData(Triplist)
ga = pyeasyga.GeneticAlgorithm(data,
                               mutation_probability=0,
                               generations=10,
                               maximise_fitness=False)

def create_individual(data): #individual
    global Triplist
    global capacity
    global numofveh
    global totaltime
    nodelist,n,P,D,PD,N,t,q,c = makeSet(Triplist)

    ### route decision
    route = [[]]*numofveh
    x = [[0 for j in N] for i in N]
    B = [0]*len(N)
    Q = [0]*len(N)

    P_tmp = list(range(1,n+1))

    while(True):
#        print('check create_individual')
        idx = random.choice(P_tmp) #trip선택
        P_tmp.remove(idx)
        idx_route = random.choice(range(len(route)))#route 선택
        if len(route[idx_route])==0:
            route[idx_route].append(idx)
            route[idx_route].append(n+idx)
        else:
            l = route[idx_route]
            l = [random.choice(range(len(l))),random.choice(range(len(l)))]
            l.sort()
            route[idx_route] = route[idx_route][:l[0]]+[idx]+route[idx_route][l[0]:l[1]]+[n+idx]+route[idx_route][l[1]:]
        if timecheck(route)>totaltime or len(P_tmp)==0 : break

    for i,r in enumerate(route):
        route[i] = [0]+r+[N[-1]]

    #### x value / time / occ / v setting
    global wd
    for r in route:
        for i in range(len(r)-1):
            x[r[i]][r[i+1]]=1
            B[r[i+1]] = B[r[i]]+t[r[i]][r[i+1]]
            Q[r[i+1]] = Q[r[i]]+q[r[i+1]]

    savename = wd+'/garoute.pickle'
    with open(savename, 'wb') as f:
        pickle.dump(route, f)

    v=[0]*(2*n+2)

    individual_v = v
    individual_B = B
    individual_x = list(np.array(x).flatten())
    individual_Q = [random.randint(0, capacity) for _ in range(2*n+2)]
    return individual_x #+ individual_v + individual_B + individual_Q #len: 548
ga.create_individual = create_individual
#individual = create_individual(data)

def fitness(individual, data):
    global Triplist
    global capacity
    nodelist,n,P,D,PD,N,t,q,c = makeSet(Triplist)
    #unchanged values: n,P,D,PD,N,t,q,c
    #changed values: x,v,B,Q
    x = makeDvar(individual)#,v,B,Q
    B,Q = calculate()
    while (True):
        condition = 1
        # route num / flow
        for j in PD:
            if not ( sum(np.array([x[i][j] for i in N]).flatten())<=1):
                condition = 0
#                print('flow in error')
#                print(i)
#                print(j)
#            if not ( v[j] >= j*x[0][j]):condition = 0
#            if not ( v[j] <= j * x[0][j] - n*(x[0][j]-1)):condition = 0
        for i in PD:
            if not ( sum(np.array([x[i][j] for j in N]).flatten())<=1):
                condition = 0
#                print('flow out error')
        # timelimit
#        for product1 in list(product(N,N)):
#            (i,j) = product1
#            if not ( Q[j] >= Q[i] + q[j] - 100*(1-x[i][j])):
#                condition = 0
#                print('capacity  error1')
#        for i in P:
#            pass
#            if not ( v[n+i] == v[i]):condition = 0
#            if not ( v[i] <= numofveh):condition = 0
        #capacitylimit
        for i in N:
#            if not ( q[i] <= Q[i]):
#                condition = 0
#                print('capacity  error2')
            if not ( capacity >= Q[i]):
                condition = 0
#                print('capacity  error3')
        #veh id
#        for product1 in list(product(PD,PD)):
#            (i,j) = product1
#            if not ( v[j] >= v[i] + n*(x[i][j]-1)):condition = 0
#            if not ( v[j] <= v[i] + n * (1-x[i][j])):condition = 0

        if condition==1:
            fitness1 = sum([c[i][j] * x[i][j] for i in N for j in N])
#            print('ok')
            print('fitness '+str(fitness1))
        else: fitness1 =0

        if fitness1!=0:
            break

    return fitness1

ga.fitness_function = fitness
ga.run()
CompuataionT = time.clock()-compT0

sol = ga.best_individual()

#B,Q


"""Client library for Akt Direkt service."""

import threading
import urllib.parse
from oauthlib.oauth2 import BackendApplicationClient, TokenExpiredError, OAuth2Error
from requests.auth import HTTPBasicAuth
from requests_oauthlib import OAuth2Session

__copyright__ = """

    Copyright 2018 Lantmäteriet

    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.

"""

class AktDirectClient:
    """Client library for Akt Direkt service."""

    def __init__(self, service_url, consumer_key, consumer_secret, token_url):
        """Initialize client with configuration given by Lantmäteriet."""
        self.service_url = service_url
        self.consumer_key = consumer_key
        self.consumer_secret = consumer_secret
        self.token_url = token_url
        self._update_token_recursion_limiter = threading.Semaphore(
            1
        )  # Used to stop recursion in error handling
        self._init_lock = threading.RLock()
        self._initialize()

    def _initialize(self):
        """Initialize/reinitialize client libraries."""
        with self._init_lock:
            self.auth = HTTPBasicAuth(self.consumer_key, self.consumer_secret)
            self.client = BackendApplicationClient(client_id=self.consumer_key)
            self.oauth = OAuth2Session(client=self.client)
            self.update_token()

    def update_token(self):
        """Fetch new token from server.

        Get a access token using your consumer key and secret,
        the token will be used to access the service.
        Note that a token has a limited life.
        """
        with self._init_lock:
            try:
                token = self.oauth.fetch_token(token_url=self.token_url, auth=self.auth)
                print(f"fetched new token: {token}")
                self.oauth.token = token
            except OAuth2Error as err:
                # We have seen a case where update_token on expiration fails but reinitializaton
                # works, so if update fails we try to reinitialize.
                # But initilize calls update_token so if it's a "real" error we needs to stop the error
                # handling from going into a recursion.
                with self._update_token_recursion_limiter as recursion_ok:
                    if not recursion_ok:
                        raise

                    print(
                        "Got OAuth2Error when trying to update token, will reinitialize. error was: ",
                        err,
                    )
                    self._initialize()

    def _call_service(self, rel_path, params=None, stream=False):
        """Call the service and handle token expiration.

        This method is used by the get_ and test_ methods in this class.

        returns a requests response object
        """
        # without the / the last element in service_url may be replaced
        url = urllib.parse.urljoin(self.service_url + "/", rel_path)
        try:
            res = self.oauth.get(url, stream=stream, params=params)
        except TokenExpiredError:
            # If the token has expired get a new one and retry
            self.update_token()
            res = self.oauth.get(url, stream=stream, params=params)
        except OAuth2Error as err:
            # This is not a case we have seen but to be on the safe side we try to reinitialize
            # if it happens.
            print(
                "Got OAuth2Error other than TokenExpiredError, will reinitialize. error was: ",
                err,
            )
            self._initialize()
            res = self.oauth.get(url, stream=stream, params=params)
        if not res.ok:
            # If update_token() fails the next call will result in a 401
            # We can choose to reinitialize on all errors instead of only 401 because
            # the Akt-Direkt API do not use HTTP error codes as part of the API.
            print(
                "Got an HTTP response >= 400, will reinitialize and try again, error was: ",
                res.status_code,
                res.text,
            )
            self._initialize()
            res = self.oauth.get(url, stream=stream, params=params)

        print(f"Called {res.request.url}", params)
        if not res.ok:
            print("Call failed, status-code:", res.status_code)
            print(res.text)
        return res

    def get_djvu(self, archive, id_):
        """Get the dossier as a DjVU.

        returns a requests response object
        """
        rel_path = "document/bundle.djvu"
        params = {"archive": archive, "id": id_}
        res = self._call_service(rel_path, stream=True, params=params)
        return res

    def get_djvu_djvu_on_error(self, archive, id_):
        """Get the dossier as a DjVU.

        returns a requests response object
        """
        rel_path = "document/djvu_on_error/bundle.djvu"
        params = {"archive": archive, "id": id_}
        res = self._call_service(rel_path, stream=True, params=params)
        return res

    def get_ping(self):
        """Make a communication test with Akt Direkt.

        This call test your configuration, communications, authentication and authorization.

        returns a requests response object
        """
        rel_path = "ping"
        res = self._call_service(rel_path)

        return res

    def test_connection(self):
        """Make a communication test with Akt Direkt.

        This call test your configuration, communications, authentication and authorization.

        returns a boolean, True if everything is OK or False if not.
        """
        res = self.get_ping()
        return res.ok