<commit_before># -*- coding:utf-8 -*-
# Author: hankcs
# Date: 2019-08-27 00:01
import datetime
import logging
import sys
import time
from typing import Union

from hanlp.utils.log_util import ErasablePrinter, color_format, color_format_len

def human_time_delta(days, hours, minutes, seconds, delimiter=' ') -> str:
    units = locals().copy()
    units.pop('delimiter')
    non_zero = False
    result = []
    for key, val in sorted(units.items()):
        append = False
        if non_zero:
            append = True
        elif val:
            non_zero = True
            append = True
        if append:
            result.append('{} {}'.format(val, key[0]))
    if not non_zero:
        return '0 s'
    return delimiter.join(result)

def seconds_to_time_delta(seconds):
    seconds = round(seconds)
    days = seconds // 86400
    hours = seconds // 3600 % 24
    minutes = seconds // 60 % 60
    seconds = seconds % 60
    return days, hours, minutes, seconds

def report_time_delta(seconds, human=True):
    days, hours, minutes, seconds = seconds_to_time_delta(seconds)
    if human:
        return human_time_delta(days, hours, minutes, seconds)
    return days, hours, minutes, seconds

class HumanTimeDelta(object):

    def __init__(self, delta_seconds) -> None:
        super().__init__()
        self.delta_seconds = delta_seconds

    def report(self, human=True):
        return report_time_delta(self.delta_seconds, human)

    def __str__(self) -> str:
        return self.report(human=True)

    def __truediv__(self, scalar):
        return HumanTimeDelta(self.delta_seconds / scalar)

class CountdownTimer(ErasablePrinter):

    def __init__(self, total: int) -> None:
        super().__init__()
        self.total = total
        self.current = 0
        self.start = time.time()
        self.finished_in = None
        self.last_log_time = 0

    def update(self, n=1):
        self.current += n
        self.current = min(self.total, self.current)
        if self.current == self.total:
            self.finished_in = time.time() - self.start

    @property
    def ratio(self) -> str:
        return f'{self.current}/{self.total}'

    @property
    def ratio_percentage(self) -> str:
        return f'{self.current / self.total:.2%}'

    @property
    def eta(self) -> float:
        elapsed = self.elapsed
        if self.finished_in:
            eta = 0
        else:
            eta = elapsed / max(self.current, 0.1) * (self.total - self.current)

        return eta

    @property
    def elapsed(self) -> float:
        if self.finished_in:
            elapsed = self.finished_in
        else:
            elapsed = time.time() - self.start
        return elapsed

    @property
    def elapsed_human(self) -> str:
        return human_time_delta(*seconds_to_time_delta(self.elapsed))

    @property
    def elapsed_average(self) -> float:
        return self.elapsed / self.current

    @property
    def elapsed_average_human(self) -> str:
        return human_time_delta(*seconds_to_time_delta(self.elapsed_average))

    @property
    def eta_human(self) -> str:
        return human_time_delta(*seconds_to_time_delta(self.eta))

    @property
    def total_time(self) -> float:
        elapsed = self.elapsed
        if self.finished_in:
            t = self.finished_in
        else:
            t = elapsed / max(self.current, 1) * self.total

        return t

    @property
    def total_time_human(self) -> str:
        return human_time_delta(*seconds_to_time_delta(self.total_time))

    def stop(self, total=None):
        if not self.finished_in or total:
            self.finished_in = time.time() - self.start
            if not total:
                self.total = self.current
            else:
                self.current = total
                self.total = total

    @property
    def et_eta(self):
        _ = self.elapsed
        if self.finished_in:
            return self.elapsed
        else:
            return self.eta

    @property
    def et_eta_human(self):
        text = human_time_delta(*seconds_to_time_delta(self.et_eta))
        if self.finished_in:
            return f'ET: {text}'
        else:
            return f'ETA: {text}'

    @property
    def finished(self):
        return self.total == self.current

    def log(self, info=None, ratio_percentage=True, ratio=True, step=1, interval=0.5, erase=True,
            logger: Union[logging.Logger, bool] = None, newline=False, ratio_width=None):
        self.update(step)
        now = time.time()
        if now - self.last_log_time > interval or self.finished:
            cells = []
            if ratio_percentage:
                cells.append(self.ratio_percentage)
            if ratio:
                ratio = self.ratio
                if not ratio_width:
                    ratio_width = self.ratio_width
                ratio = ratio.rjust(ratio_width)
                cells.append(ratio)
            cells += [info, self.et_eta_human]
            cells = [x for x in cells if x]
            msg = f'{" ".join(cells)}'
            self.last_log_time = now
            self.print(msg, newline, erase, logger)

    @property
    def ratio_width(self) -> int:
        return len(f'{self.total}') * 2 + 1

    def print(self, msg, newline=False, erase=True, logger=None):
        self.erase()
        msg_len = 0 if newline else len(msg)
        if self.finished and logger:
            sys.stdout.flush()
            if isinstance(logger, logging.Logger):
                logger.info(msg)
        else:
            msg, msg_len = color_format_len(msg)
            sys.stdout.write(msg)
            if newline:
                sys.stdout.write('\n')
                msg_len = 0
        self._last_print_width = msg_len
        if self.finished and not logger:
            if erase:
                self.erase()
            else:
                sys.stdout.write("\n")
                self._last_print_width = 0
        sys.stdout.flush()

class Timer(object):
    def __init__(self) -> None:
        self.last = time.time()

    def start(self):
        self.last = time.time()

    def stop(self) -> HumanTimeDelta:
        now = time.time()
        seconds = now - self.last
        self.last = now
        return HumanTimeDelta(seconds)

def now_human(year='y'):
    now = datetime.datetime.now()
    return now.strftime(f"%{year}-%m-%d %H:%M:%S")

def now_datetime():
    return now_human('Y')

def now_filename(fmt="%y%m%d_%H%M%S"):
    """Generate filename using current datetime, in 20180102_030405 format

    Args:
      fmt:  (Default value = "%y%m%d_%H%M%S")

    Returns:

    """
    now = datetime.datetime.now()
    return now.strftime(fmt)
<commit_msg>CountdownTimer use stdout to match with logger<commit_after># -*- coding:utf-8 -*-
# Author: hankcs
# Date: 2019-08-27 00:01
import datetime
import logging
import sys
import time
from typing import Union

from hanlp.utils.log_util import ErasablePrinter, color_format, color_format_len

def human_time_delta(days, hours, minutes, seconds, delimiter=' ') -> str:
    units = locals().copy()
    units.pop('delimiter')
    non_zero = False
    result = []
    for key, val in sorted(units.items()):
        append = False
        if non_zero:
            append = True
        elif val:
            non_zero = True
            append = True
        if append:
            result.append('{} {}'.format(val, key[0]))
    if not non_zero:
        return '0 s'
    return delimiter.join(result)

def seconds_to_time_delta(seconds):
    seconds = round(seconds)
    days = seconds // 86400
    hours = seconds // 3600 % 24
    minutes = seconds // 60 % 60
    seconds = seconds % 60
    return days, hours, minutes, seconds

def report_time_delta(seconds, human=True):
    days, hours, minutes, seconds = seconds_to_time_delta(seconds)
    if human:
        return human_time_delta(days, hours, minutes, seconds)
    return days, hours, minutes, seconds

class HumanTimeDelta(object):

    def __init__(self, delta_seconds) -> None:
        super().__init__()
        self.delta_seconds = delta_seconds

    def report(self, human=True):
        return report_time_delta(self.delta_seconds, human)

    def __str__(self) -> str:
        return self.report(human=True)

    def __truediv__(self, scalar):
        return HumanTimeDelta(self.delta_seconds / scalar)

class CountdownTimer(ErasablePrinter):

    def __init__(self, total: int, out=sys.stdout) -> None:
        super().__init__(out=out)
        self.total = total
        self.current = 0
        self.start = time.time()
        self.finished_in = None
        self.last_log_time = 0

    def update(self, n=1):
        self.current += n
        self.current = min(self.total, self.current)
        if self.current == self.total:
            self.finished_in = time.time() - self.start

    @property
    def ratio(self) -> str:
        return f'{self.current}/{self.total}'

    @property
    def ratio_percentage(self) -> str:
        return f'{self.current / self.total:.2%}'

    @property
    def eta(self) -> float:
        elapsed = self.elapsed
        if self.finished_in:
            eta = 0
        else:
            eta = elapsed / max(self.current, 0.1) * (self.total - self.current)

        return eta

    @property
    def elapsed(self) -> float:
        if self.finished_in:
            elapsed = self.finished_in
        else:
            elapsed = time.time() - self.start
        return elapsed

    @property
    def elapsed_human(self) -> str:
        return human_time_delta(*seconds_to_time_delta(self.elapsed))

    @property
    def elapsed_average(self) -> float:
        return self.elapsed / self.current

    @property
    def elapsed_average_human(self) -> str:
        return human_time_delta(*seconds_to_time_delta(self.elapsed_average))

    @property
    def eta_human(self) -> str:
        return human_time_delta(*seconds_to_time_delta(self.eta))

    @property
    def total_time(self) -> float:
        elapsed = self.elapsed
        if self.finished_in:
            t = self.finished_in
        else:
            t = elapsed / max(self.current, 1) * self.total

        return t

    @property
    def total_time_human(self) -> str:
        return human_time_delta(*seconds_to_time_delta(self.total_time))

    def stop(self, total=None):
        if not self.finished_in or total:
            self.finished_in = time.time() - self.start
            if not total:
                self.total = self.current
            else:
                self.current = total
                self.total = total

    @property
    def et_eta(self):
        _ = self.elapsed
        if self.finished_in:
            return self.elapsed
        else:
            return self.eta

    @property
    def et_eta_human(self):
        text = human_time_delta(*seconds_to_time_delta(self.et_eta))
        if self.finished_in:
            return f'ET: {text}'
        else:
            return f'ETA: {text}'

    @property
    def finished(self):
        return self.total == self.current

    def log(self, info=None, ratio_percentage=True, ratio=True, step=1, interval=0.5, erase=True,
            logger: Union[logging.Logger, bool] = None, newline=False, ratio_width=None):
        self.update(step)
        now = time.time()
        if now - self.last_log_time > interval or self.finished:
            cells = []
            if ratio_percentage:
                cells.append(self.ratio_percentage)
            if ratio:
                ratio = self.ratio
                if not ratio_width:
                    ratio_width = self.ratio_width
                ratio = ratio.rjust(ratio_width)
                cells.append(ratio)
            cells += [info, self.et_eta_human]
            cells = [x for x in cells if x]
            msg = f'{" ".join(cells)}'
            self.last_log_time = now
            self.print(msg, newline, erase, logger)

    @property
    def ratio_width(self) -> int:
        return len(f'{self.total}') * 2 + 1

    def print(self, msg, newline=False, erase=True, logger=None):
        self.erase()
        msg_len = 0 if newline else len(msg)
        if self.finished and logger:
            sys.stdout.flush()
            if isinstance(logger, logging.Logger):
                logger.info(msg)
        else:
            msg, msg_len = color_format_len(msg)
            sys.stdout.write(msg)
            if newline:
                sys.stdout.write('\n')
                msg_len = 0
        self._last_print_width = msg_len
        if self.finished and not logger:
            if erase:
                self.erase()
            else:
                sys.stdout.write("\n")
                self._last_print_width = 0
        sys.stdout.flush()

class Timer(object):
    def __init__(self) -> None:
        self.last = time.time()

    def start(self):
        self.last = time.time()

    def stop(self) -> HumanTimeDelta:
        now = time.time()
        seconds = now - self.last
        self.last = now
        return HumanTimeDelta(seconds)

def now_human(year='y'):
    now = datetime.datetime.now()
    return now.strftime(f"%{year}-%m-%d %H:%M:%S")

def now_datetime():
    return now_human('Y')

def now_filename(fmt="%y%m%d_%H%M%S"):
    """Generate filename using current datetime, in 20180102_030405 format

    Args:
      fmt:  (Default value = "%y%m%d_%H%M%S")

    Returns:

    """
    now = datetime.datetime.now()
    return now.strftime(fmt)
<|endoftext|>


<commit_before>import re
import W
import macfs
import os
import MacPrefs
import MacOS
import string
import webbrowser

app = W.getapplication()

_titlepat = re.compile('<title>\([^<]*\)</title>')

def sucktitle(path):
	f = open(path)
	text = f.read(1024) # assume the title is in the first 1024 bytes
	f.close()
	lowertext = text.lower()
	matcher = _titlepat.search(lowertext)
	if matcher:
		return matcher.group(1)
	return path

def verifydocpath(docpath):
	try:
		tut = os.path.join(docpath, "tut")
		lib = os.path.join(docpath, "lib")
		ref = os.path.join(docpath, "ref")
		for path in [tut, lib, ref]:
			if not os.path.exists(path):
				return 0
	except:
		return 0
	return 1

class TwoLineList(W.List):

	LDEF_ID = 468

	def createlist(self):
		from Carbon import List
		self._calcbounds()
		self.SetPort()
		rect = self._bounds
		rect = rect[0]+1, rect[1]+1, rect[2]-16, rect[3]-1
		self._list = List.LNew(rect, (0, 0, 1, 0), (0, 28), self.LDEF_ID, self._parentwindow.wid,
					0, 1, 0, 1)
		self.set(self.items)

_resultscounter = 1

class Results:

	def __init__(self, hits):
		global _resultscounter
		hits = map(lambda (path, hits): (sucktitle(path), path, hits), hits)
		hits.sort()
		self.hits = hits
		nicehits = map(
				lambda (title, path, hits):
				title + '\r' + string.join(
				map(lambda (c, p): "%s (%d)" % (p, c), hits), ', '), hits)
		nicehits.sort()
		self.w = W.Window((440, 300), "Search results %d" % _resultscounter, minsize = (200, 100))
		self.w.results = W.TwoLineList((-1, -1, 1, -14), nicehits, self.listhit)
		self.w.open()
		self.w.bind('return', self.listhit)
		self.w.bind('enter', self.listhit)
		_resultscounter = _resultscounter + 1

	def listhit(self, isdbl = 1):
		if isdbl:
			for i in self.w.results.getselection():
				path = self.hits[i][1]
				url = "file://" + "/".join(path.split(":"))
				webbrowser.open(url)

class Status:

	def __init__(self):
		self.w = W.Dialog((440, 64), "Searching\xc9")
		self.w.searching = W.TextBox((4, 4, -4, 16), "")
		self.w.hits = W.TextBox((4, 24, -4, 16), "Hits: 0")
		self.w.canceltip = W.TextBox((4, 44, -4, 16), "Type cmd-period (.) to cancel.")
		self.w.open()

	def set(self, path, hits):
		self.w.searching.set(path)
		self.w.hits.set('Hits: ' + `hits`)
		app.breathe()

	def close(self):
		self.w.close()

def match(text, patterns, all):
	hits = []
	hitsappend = hits.append
	stringcount = string.count
	for pat in patterns:
		c = stringcount(text, pat)
		if c > 0:
			hitsappend((c, pat))
		elif all:
			hits[:] = []
			break
	hits.sort()
	hits.reverse()
	return hits

def dosearch(docpath, searchstring, settings):
	(docpath, kind, case, word, tut, lib, ref, ext, api) = settings
	books = [(tut, 'tut'), (lib, 'lib'), (ref, 'ref'), (ext, 'ext'), (api, 'api')]
	if not case:
		searchstring = string.lower(searchstring)

	if kind == 1:
		patterns = string.split(searchstring)
		all = 1
	elif kind == 2:
		patterns = string.split(searchstring)
		all = 0
	else:
		patterns = [searchstring]
		all = 0 # not relevant

	ospathjoin = os.path.join
	stringlower = string.lower
	status = Status()
	statusset = status.set
	_match = match
	_open = open
	hits = {}
	try:
		MacOS.EnableAppswitch(0)
		try:
			for do, name in books:
				if not do:
					continue
				bookpath = ospathjoin(docpath, name)
				if not os.path.exists(bookpath):
					continue
				files = os.listdir(bookpath)
				for file in files:
					fullpath = ospathjoin(bookpath, file)
					if fullpath[-5:] <> '.html':
						continue
					statusset(fullpath, len(hits))
					f = _open(fullpath)
					text = f.read()
					if not case:
						text = stringlower(text)
					f.close()
					filehits = _match(text, patterns, all)
					if filehits:
						hits[fullpath] = filehits
		finally:
			MacOS.EnableAppswitch(-1)
			status.close()
	except KeyboardInterrupt:
		pass
	hits = hits.items()
	hits.sort()
	return hits

class PyDocSearch:

	def __init__(self):
		prefs = MacPrefs.GetPrefs(W.getapplication().preffilepath)
		try:
			(docpath, kind, case, word, tut, lib, ref, ext, api) = prefs.docsearchengine
		except:
			(docpath, kind, case, word, tut, lib, ref, ext, api) = prefs.docsearchengine = \
				("", 0, 0, 0, 1, 1, 0, 0, 0)

		if docpath and not verifydocpath(docpath):
			docpath = ""

		self.w = W.Window((400, 200), "Search the Python Documentation")
		self.w.searchtext = W.EditText((10, 10, -100, 20), callback = self.checkbuttons)
		self.w.searchbutton = W.Button((-90, 12, 80, 16), "Search", self.search)
		buttons = []

		gutter = 10
		width = 130
		bookstart = width + 2 * gutter
		self.w.phraseradio = W.RadioButton((10, 38, width, 16), "As a phrase", buttons)
		self.w.allwordsradio = W.RadioButton((10, 58, width, 16), "All words", buttons)
		self.w.anywordsradio = W.RadioButton((10, 78, width, 16), "Any word", buttons)
		self.w.casesens = W.CheckBox((10, 98, width, 16), "Case sensitive")
		self.w.wholewords = W.CheckBox((10, 118, width, 16), "Whole words")
		self.w.tutorial = W.CheckBox((bookstart, 38, -10, 16), "Tutorial")
		self.w.library = W.CheckBox((bookstart, 58, -10, 16), "Library reference")
		self.w.langueref = W.CheckBox((bookstart, 78, -10, 16), "Lanuage reference manual")
		self.w.extending = W.CheckBox((bookstart, 98, -10, 16), "Extending & embedding")
		self.w.api = W.CheckBox((bookstart, 118, -10, 16), "C/C++ API")

		self.w.setdocfolderbutton = W.Button((10, -30, 100, 16), "Set doc folder", self.setdocpath)

		if docpath:
			self.w.setdefaultbutton(self.w.searchbutton)
		else:
			self.w.setdefaultbutton(self.w.setdocfolderbutton)

		self.docpath = docpath
		if not docpath:
			docpath = "(please select the Python html documentation folder)"
		self.w.docfolder = W.TextBox((120, -28, -10, 16), docpath)

		[self.w.phraseradio, self.w.allwordsradio, self.w.anywordsradio][kind].set(1)

		self.w.casesens.set(case)
		self.w.wholewords.set(word)
		self.w.tutorial.set(tut)
		self.w.library.set(lib)
		self.w.langueref.set(ref)
		self.w.extending.set(ext)
		self.w.api.set(api)

		self.w.open()
		self.w.wholewords.enable(0)
		self.w.bind('<close>', self.close)
		self.w.searchbutton.enable(0)

	def search(self):
		hits = dosearch(self.docpath, self.w.searchtext.get(), self.getsettings())
		if hits:
			Results(hits)
		elif hasattr(MacOS, 'SysBeep'):
			MacOS.SysBeep(0)

	def setdocpath(self):
		fss, ok = macfs.GetDirectory()
		if ok:
			docpath = fss.as_pathname()
			if not verifydocpath(docpath):
				W.Message("This does not seem to be a Python documentation folder...")
			else:
				self.docpath = docpath
				self.w.docfolder.set(docpath)
				self.w.setdefaultbutton(self.w.searchbutton)

	def close(self):
		prefs = MacPrefs.GetPrefs(W.getapplication().preffilepath)
		prefs.docsearchengine = self.getsettings()

	def getsettings(self):
		radiobuttons = [self.w.phraseradio, self.w.allwordsradio, self.w.anywordsradio]
		for i in range(3):
			if radiobuttons[i].get():
				kind = i
				break
		docpath = self.docpath
		case = self.w.casesens.get()
		word = self.w.wholewords.get()
		tut = self.w.tutorial.get()
		lib = self.w.library.get()
		ref = self.w.langueref.get()
		ext = self.w.extending.get()
		api = self.w.api.get()
		return (docpath, kind, case, word, tut, lib, ref, ext, api)

	def checkbuttons(self):
		self.w.searchbutton.enable(not not self.w.searchtext.get())
<commit_msg>removed some dead code.<commit_after>import re
import W
import macfs
import os
import MacPrefs
import MacOS
import string
import webbrowser

app = W.getapplication()

_titlepat = re.compile('<title>\([^<]*\)</title>')

def sucktitle(path):
	f = open(path)
	text = f.read(1024) # assume the title is in the first 1024 bytes
	f.close()
	lowertext = text.lower()
	matcher = _titlepat.search(lowertext)
	if matcher:
		return matcher.group(1)
	return path

def verifydocpath(docpath):
	try:
		tut = os.path.join(docpath, "tut")
		lib = os.path.join(docpath, "lib")
		ref = os.path.join(docpath, "ref")
		for path in [tut, lib, ref]:
			if not os.path.exists(path):
				return 0
	except:
		return 0
	return 1

_resultscounter = 1

class Results:

	def __init__(self, hits):
		global _resultscounter
		hits = map(lambda (path, hits): (sucktitle(path), path, hits), hits)
		hits.sort()
		self.hits = hits
		nicehits = map(
				lambda (title, path, hits):
				title + '\r' + string.join(
				map(lambda (c, p): "%s (%d)" % (p, c), hits), ', '), hits)
		nicehits.sort()
		self.w = W.Window((440, 300), "Search results %d" % _resultscounter, minsize = (200, 100))
		self.w.results = W.TwoLineList((-1, -1, 1, -14), nicehits, self.listhit)
		self.w.open()
		self.w.bind('return', self.listhit)
		self.w.bind('enter', self.listhit)
		_resultscounter = _resultscounter + 1

	def listhit(self, isdbl = 1):
		if isdbl:
			for i in self.w.results.getselection():
				path = self.hits[i][1]
				url = "file://" + "/".join(path.split(":"))
				webbrowser.open(url)

class Status:

	def __init__(self):
		self.w = W.Dialog((440, 64), "Searching\xc9")
		self.w.searching = W.TextBox((4, 4, -4, 16), "")
		self.w.hits = W.TextBox((4, 24, -4, 16), "Hits: 0")
		self.w.canceltip = W.TextBox((4, 44, -4, 16), "Type cmd-period (.) to cancel.")
		self.w.open()

	def set(self, path, hits):
		self.w.searching.set(path)
		self.w.hits.set('Hits: ' + `hits`)
		app.breathe()

	def close(self):
		self.w.close()

def match(text, patterns, all):
	hits = []
	hitsappend = hits.append
	stringcount = string.count
	for pat in patterns:
		c = stringcount(text, pat)
		if c > 0:
			hitsappend((c, pat))
		elif all:
			hits[:] = []
			break
	hits.sort()
	hits.reverse()
	return hits

def dosearch(docpath, searchstring, settings):
	(docpath, kind, case, word, tut, lib, ref, ext, api) = settings
	books = [(tut, 'tut'), (lib, 'lib'), (ref, 'ref'), (ext, 'ext'), (api, 'api')]
	if not case:
		searchstring = string.lower(searchstring)

	if kind == 1:
		patterns = string.split(searchstring)
		all = 1
	elif kind == 2:
		patterns = string.split(searchstring)
		all = 0
	else:
		patterns = [searchstring]
		all = 0 # not relevant

	ospathjoin = os.path.join
	stringlower = string.lower
	status = Status()
	statusset = status.set
	_match = match
	_open = open
	hits = {}
	try:
		MacOS.EnableAppswitch(0)
		try:
			for do, name in books:
				if not do:
					continue
				bookpath = ospathjoin(docpath, name)
				if not os.path.exists(bookpath):
					continue
				files = os.listdir(bookpath)
				for file in files:
					fullpath = ospathjoin(bookpath, file)
					if fullpath[-5:] <> '.html':
						continue
					statusset(fullpath, len(hits))
					f = _open(fullpath)
					text = f.read()
					if not case:
						text = stringlower(text)
					f.close()
					filehits = _match(text, patterns, all)
					if filehits:
						hits[fullpath] = filehits
		finally:
			MacOS.EnableAppswitch(-1)
			status.close()
	except KeyboardInterrupt:
		pass
	hits = hits.items()
	hits.sort()
	return hits

class PyDocSearch:

	def __init__(self):
		prefs = MacPrefs.GetPrefs(W.getapplication().preffilepath)
		try:
			(docpath, kind, case, word, tut, lib, ref, ext, api) = prefs.docsearchengine
		except:
			(docpath, kind, case, word, tut, lib, ref, ext, api) = prefs.docsearchengine = \
				("", 0, 0, 0, 1, 1, 0, 0, 0)

		if docpath and not verifydocpath(docpath):
			docpath = ""

		self.w = W.Window((400, 200), "Search the Python Documentation")
		self.w.searchtext = W.EditText((10, 10, -100, 20), callback = self.checkbuttons)
		self.w.searchbutton = W.Button((-90, 12, 80, 16), "Search", self.search)
		buttons = []

		gutter = 10
		width = 130
		bookstart = width + 2 * gutter
		self.w.phraseradio = W.RadioButton((10, 38, width, 16), "As a phrase", buttons)
		self.w.allwordsradio = W.RadioButton((10, 58, width, 16), "All words", buttons)
		self.w.anywordsradio = W.RadioButton((10, 78, width, 16), "Any word", buttons)
		self.w.casesens = W.CheckBox((10, 98, width, 16), "Case sensitive")
		self.w.wholewords = W.CheckBox((10, 118, width, 16), "Whole words")
		self.w.tutorial = W.CheckBox((bookstart, 38, -10, 16), "Tutorial")
		self.w.library = W.CheckBox((bookstart, 58, -10, 16), "Library reference")
		self.w.langueref = W.CheckBox((bookstart, 78, -10, 16), "Lanuage reference manual")
		self.w.extending = W.CheckBox((bookstart, 98, -10, 16), "Extending & embedding")
		self.w.api = W.CheckBox((bookstart, 118, -10, 16), "C/C++ API")

		self.w.setdocfolderbutton = W.Button((10, -30, 100, 16), "Set doc folder", self.setdocpath)

		if docpath:
			self.w.setdefaultbutton(self.w.searchbutton)
		else:
			self.w.setdefaultbutton(self.w.setdocfolderbutton)

		self.docpath = docpath
		if not docpath:
			docpath = "(please select the Python html documentation folder)"
		self.w.docfolder = W.TextBox((120, -28, -10, 16), docpath)

		[self.w.phraseradio, self.w.allwordsradio, self.w.anywordsradio][kind].set(1)

		self.w.casesens.set(case)
		self.w.wholewords.set(word)
		self.w.tutorial.set(tut)
		self.w.library.set(lib)
		self.w.langueref.set(ref)
		self.w.extending.set(ext)
		self.w.api.set(api)

		self.w.open()
		self.w.wholewords.enable(0)
		self.w.bind('<close>', self.close)
		self.w.searchbutton.enable(0)

	def search(self):
		hits = dosearch(self.docpath, self.w.searchtext.get(), self.getsettings())
		if hits:
			Results(hits)
		elif hasattr(MacOS, 'SysBeep'):
			MacOS.SysBeep(0)

	def setdocpath(self):
		fss, ok = macfs.GetDirectory()
		if ok:
			docpath = fss.as_pathname()
			if not verifydocpath(docpath):
				W.Message("This does not seem to be a Python documentation folder...")
			else:
				self.docpath = docpath
				self.w.docfolder.set(docpath)
				self.w.setdefaultbutton(self.w.searchbutton)

	def close(self):
		prefs = MacPrefs.GetPrefs(W.getapplication().preffilepath)
		prefs.docsearchengine = self.getsettings()

	def getsettings(self):
		radiobuttons = [self.w.phraseradio, self.w.allwordsradio, self.w.anywordsradio]
		for i in range(3):
			if radiobuttons[i].get():
				kind = i
				break
		docpath = self.docpath
		case = self.w.casesens.get()
		word = self.w.wholewords.get()
		tut = self.w.tutorial.get()
		lib = self.w.library.get()
		ref = self.w.langueref.get()
		ext = self.w.extending.get()
		api = self.w.api.get()
		return (docpath, kind, case, word, tut, lib, ref, ext, api)

	def checkbuttons(self):
		self.w.searchbutton.enable(not not self.w.searchtext.get())
<|endoftext|>


<commit_before># CREATED: 2/9/16 2:27 PM by Justin Salamon <justin.salamon@nyu.edu>

import mir_eval
import numpy as np
import glob
import json
from nose.tools import raises
import warnings

A_TOL = 1e-12

# Path to the fixture files
REF_GLOB = 'tests/data/transcription/ref*.txt'
EST_GLOB = 'tests/data/transcription/est*.txt'
SCORES_GLOB = 'tests/data/transcription/output*.json'

REF = np.array([
    [0.100, 0.300, 220.000],
    [0.300, 0.400, 246.942],
    [0.500, 0.600, 277.183],
    [0.550, 0.650, 293.665]])

EST = np.array([
    [0.120,   0.290,   225.000],
    [0.300,   0.340,   246.942],
    [0.500,   0.600,   500.000],
    [0.550,   0.600,   293.665],
    [0.560,   0.650,   293.665]])

SCORES = {
    "Precision": 0.4,
    "Recall": 0.5,
    "F-measure": 0.4444444444444445,
    "Average_Overlap_Ratio": 0.675,
    "Precision_no_offset": 0.6,
    "Recall_no_offset": 0.75,
    "F-measure_no_offset": 0.6666666666666665,
    "Average_Overlap_Ratio_no_offset": 0.5833333333333333
}

ONSET_SCORES = {
    "Onset_Precision": 0.8,
    "Onset_Recall": 1.0,
    "Onset_F-measure": 0.8888888888888889,
}

OFFSET_SCORES = {
    "Offset_Precision": 0.6,
    "Offset_Recall": 0.75,
    "Offset_F-measure": 0.6666666666666665,
}

def test_match_note_offsets():

    ref_int = REF[:, :2]
    est_int = EST[:, :2]

    matching = (
        mir_eval.transcription.match_note_offsets(ref_int, est_int))

    assert matching == [(0, 0), (2, 2), (3, 3)]

def test_match_note_offsets_strict():

    ref_int = REF[:, :2]
    est_int = EST[:, :2]

    matching = (
        mir_eval.transcription.match_note_offsets(ref_int, est_int, strict=True))

    assert matching == [(0, 0), (2, 2), (3, 4)]

def test_match_note_onsets():

    ref_int = REF[:, :2]
    est_int = EST[:, :2]

    matching = (
        mir_eval.transcription.match_note_onsets(ref_int, est_int))

    assert matching == [(0, 0), (1, 1), (2, 2), (3, 3)]

def test_match_note_onsets_strict():

    ref_int = REF[:, :2]
    est_int = EST[:, :2]

    matching = (
        mir_eval.transcription.match_note_onsets(ref_int, est_int, strict=True))

    assert matching == [(0, 0), (1, 1), (2, 2), (3, 3)]

def test_match_notes():

    ref_int, ref_pitch = REF[:, :2], REF[:, 2]
    est_int, est_pitch = EST[:, :2], EST[:, 2]

    matching = (
        mir_eval.transcription.match_notes(ref_int, ref_pitch, est_int,
                                           est_pitch))

    assert matching == [(0, 0), (3, 3)]

    matching = (
        mir_eval.transcription.match_notes(ref_int, ref_pitch, est_int,
                                           est_pitch, offset_ratio=None))

    assert matching == [(0, 0), (1, 1), (3, 3)]

def test_match_notes_strict():

    ref_int, ref_pitch = np.array([[0, 1]]), np.array([100])
    est_int, est_pitch = np.array([[0.05, 1]]), np.array([100])

    matching = (
        mir_eval.transcription.match_notes(ref_int, ref_pitch, est_int,
                                           est_pitch, strict=True))

    assert matching == []

def test_precision_recall_f1_overlap():

    # load test data
    ref_int, ref_pitch = REF[:, :2], REF[:, 2]
    est_int, est_pitch = EST[:, :2], EST[:, 2]

    precision, recall, f_measure, avg_overlap_ratio = (
        mir_eval.transcription.precision_recall_f1_overlap(
            ref_int, ref_pitch, est_int, est_pitch))

    scores_gen = np.array([precision, recall, f_measure, avg_overlap_ratio])
    scores_exp = np.array([SCORES['Precision'], SCORES['Recall'],
                           SCORES['F-measure'],
                           SCORES['Average_Overlap_Ratio']])
    assert np.allclose(scores_exp, scores_gen, atol=A_TOL)

    precision, recall, f_measure, avg_overlap_ratio = (
        mir_eval.transcription.precision_recall_f1_overlap(
            ref_int, ref_pitch, est_int, est_pitch, offset_ratio=None))

    scores_gen = np.array([precision, recall, f_measure, avg_overlap_ratio])
    scores_exp = np.array([SCORES['Precision_no_offset'],
                           SCORES['Recall_no_offset'],
                           SCORES['F-measure_no_offset'],
                           SCORES['Average_Overlap_Ratio_no_offset']])
    assert np.allclose(scores_exp, scores_gen, atol=A_TOL)

def __check_score(score, expected_score):
    assert np.allclose(score, expected_score, atol=A_TOL)

def test_onset_precision_recall_f1():

    # load test data
    ref_int = REF[:, :2]
    est_int = EST[:, :2]

    precision, recall, f_measure = (
        mir_eval.transcription.onset_precision_recall_f1(ref_int, est_int))

    scores_gen = np.array([precision, recall, f_measure])
    scores_exp = np.array([ONSET_SCORES['Onset_Precision'],
                           ONSET_SCORES['Onset_Recall'],
                           ONSET_SCORES['Onset_F-measure']])
    assert np.allclose(scores_exp, scores_gen, atol=A_TOL)

def test_offset_precision_recall_f1():

    # load test data
    ref_int = REF[:, :2]
    est_int = EST[:, :2]

    precision, recall, f_measure = (
        mir_eval.transcription.offset_precision_recall_f1(ref_int, est_int))

    scores_gen = np.array([precision, recall, f_measure])
    scores_exp = np.array([OFFSET_SCORES['Offset_Precision'],
                           OFFSET_SCORES['Offset_Recall'],
                           OFFSET_SCORES['Offset_F-measure']])
    assert np.allclose(scores_exp, scores_gen, atol=A_TOL)

def test_regression():

    # Regression tests
    ref_files = sorted(glob.glob(REF_GLOB))
    est_files = sorted(glob.glob(EST_GLOB))
    sco_files = sorted(glob.glob(SCORES_GLOB))

    for ref_f, est_f, sco_f in zip(ref_files, est_files, sco_files):
        with open(sco_f, 'r') as f:
            expected_scores = json.load(f)
        # Load in reference transcription
        ref_int, ref_pitch = mir_eval.io.load_valued_intervals(ref_f)
        # Load in estimated transcription
        est_int, est_pitch = mir_eval.io.load_valued_intervals(est_f)
        scores = mir_eval.transcription.evaluate(ref_int, ref_pitch, est_int,
                                                 est_pitch)
        for metric in scores:
            # This is a simple hack to make nosetest's messages more useful
            yield (__check_score, scores[metric], expected_scores[metric])

def test_invalid_pitch():

    ref_int, ref_pitch = np.array([[0, 1]]), np.array([-100])
    est_int, est_pitch = np.array([[0, 1]]), np.array([100])

    yield (raises(ValueError)(mir_eval.transcription.validate),
           ref_int, ref_pitch, est_int, est_pitch)
    yield (raises(ValueError)(mir_eval.transcription.validate),
           est_int, est_pitch, ref_int, ref_pitch)

def test_inconsistent_int_pitch():

    ref_int, ref_pitch = np.array([[0, 1], [2, 3]]), np.array([100])
    est_int, est_pitch = np.array([[0, 1]]), np.array([100])

    yield (raises(ValueError)(mir_eval.transcription.validate),
           ref_int, ref_pitch, est_int, est_pitch)
    yield (raises(ValueError)(mir_eval.transcription.validate),
           est_int, est_pitch, ref_int, ref_pitch)

def test_empty_ref():

    warnings.resetwarnings()
    warnings.simplefilter('always')
    with warnings.catch_warnings(record=True) as out:

        ref_int, ref_pitch = np.empty(shape=(0, 2)), np.array([])
        est_int, est_pitch = np.array([[0, 1]]), np.array([100])

        mir_eval.transcription.validate(ref_int, ref_pitch, est_int, est_pitch)

        # Make sure that the warning triggered
        assert len(out) > 0

        # And that the category is correct
        assert out[0].category is UserWarning

        # And that it says the right thing (roughly)
        assert 'empty' in str(out[0].message).lower()

def test_empty_est():

    warnings.resetwarnings()
    warnings.simplefilter('always')
    with warnings.catch_warnings(record=True) as out:

        ref_int, ref_pitch = np.array([[0, 1]]), np.array([100])
        est_int, est_pitch = np.empty(shape=(0, 2)), np.array([])

        mir_eval.transcription.validate(ref_int, ref_pitch, est_int, est_pitch)

        # Make sure that the warning triggered
        assert len(out) > 0

        # And that the category is correct
        assert out[0].category is UserWarning

        # And that it says the right thing (roughly)
        assert 'empty' in str(out[0].message).lower()

def test_precision_recall_f1_overlap_empty():

    ref_int, ref_pitch = np.empty(shape=(0, 2)), np.array([])
    est_int, est_pitch = np.array([[0, 1]]), np.array([100])

    precision, recall, f1, avg_overlap_ratio = (
        mir_eval.transcription.precision_recall_f1_overlap(
            ref_int, ref_pitch, est_int, est_pitch))

    assert (precision, recall, f1) == (0, 0, 0)

    precision, recall, f1, avg_overlap_ratio = (
        mir_eval.transcription.precision_recall_f1_overlap(
            est_int, est_pitch, ref_int, ref_pitch))

    assert (precision, recall, f1) == (0, 0, 0)

def test_onset_precision_recall_f1_empty():

    ref_int = np.empty(shape=(0, 2))
    est_int = np.array([[0, 1]])

    precision, recall, f1 = (
        mir_eval.transcription.onset_precision_recall_f1(ref_int, est_int))

    assert (precision, recall, f1) == (0, 0, 0)

    precision, recall, f1 = (
        mir_eval.transcription.onset_precision_recall_f1(est_int, ref_int))

    assert (precision, recall, f1) == (0, 0, 0)

def test_offset_precision_recall_f1_empty():

    ref_int = np.empty(shape=(0, 2))
    est_int = np.array([[0, 1]])

    precision, recall, f1 = (
        mir_eval.transcription.offset_precision_recall_f1(ref_int, est_int))

    assert (precision, recall, f1) == (0, 0, 0)

    precision, recall, f1 = (
        mir_eval.transcription.offset_precision_recall_f1(est_int, ref_int))

    assert (precision, recall, f1) == (0, 0, 0)
<commit_msg>Make PEP8 complient<commit_after># CREATED: 2/9/16 2:27 PM by Justin Salamon <justin.salamon@nyu.edu>

import mir_eval
import numpy as np
import glob
import json
from nose.tools import raises
import warnings

A_TOL = 1e-12

# Path to the fixture files
REF_GLOB = 'tests/data/transcription/ref*.txt'
EST_GLOB = 'tests/data/transcription/est*.txt'
SCORES_GLOB = 'tests/data/transcription/output*.json'

REF = np.array([
    [0.100, 0.300, 220.000],
    [0.300, 0.400, 246.942],
    [0.500, 0.600, 277.183],
    [0.550, 0.650, 293.665]])

EST = np.array([
    [0.120,   0.290,   225.000],
    [0.300,   0.340,   246.942],
    [0.500,   0.600,   500.000],
    [0.550,   0.600,   293.665],
    [0.560,   0.650,   293.665]])

SCORES = {
    "Precision": 0.4,
    "Recall": 0.5,
    "F-measure": 0.4444444444444445,
    "Average_Overlap_Ratio": 0.675,
    "Precision_no_offset": 0.6,
    "Recall_no_offset": 0.75,
    "F-measure_no_offset": 0.6666666666666665,
    "Average_Overlap_Ratio_no_offset": 0.5833333333333333
}

ONSET_SCORES = {
    "Onset_Precision": 0.8,
    "Onset_Recall": 1.0,
    "Onset_F-measure": 0.8888888888888889,
}

OFFSET_SCORES = {
    "Offset_Precision": 0.6,
    "Offset_Recall": 0.75,
    "Offset_F-measure": 0.6666666666666665,
}

def test_match_note_offsets():

    ref_int = REF[:, :2]
    est_int = EST[:, :2]

    matching = (
        mir_eval.transcription.match_note_offsets(ref_int, est_int))

    assert matching == [(0, 0), (2, 2), (3, 3)]

def test_match_note_offsets_strict():

    ref_int = REF[:, :2]
    est_int = EST[:, :2]

    matching = (
        mir_eval.transcription.match_note_offsets(
            ref_int, est_int, strict=True))

    assert matching == [(0, 0), (2, 2), (3, 4)]

def test_match_note_onsets():

    ref_int = REF[:, :2]
    est_int = EST[:, :2]

    matching = (
        mir_eval.transcription.match_note_onsets(ref_int, est_int))

    assert matching == [(0, 0), (1, 1), (2, 2), (3, 3)]

def test_match_note_onsets_strict():

    ref_int = REF[:, :2]
    est_int = EST[:, :2]

    matching = (
        mir_eval.transcription.match_note_onsets(
            ref_int, est_int, strict=True))

    assert matching == [(0, 0), (1, 1), (2, 2), (3, 3)]

def test_match_notes():

    ref_int, ref_pitch = REF[:, :2], REF[:, 2]
    est_int, est_pitch = EST[:, :2], EST[:, 2]

    matching = (
        mir_eval.transcription.match_notes(ref_int, ref_pitch, est_int,
                                           est_pitch))

    assert matching == [(0, 0), (3, 3)]

    matching = (
        mir_eval.transcription.match_notes(ref_int, ref_pitch, est_int,
                                           est_pitch, offset_ratio=None))

    assert matching == [(0, 0), (1, 1), (3, 3)]

def test_match_notes_strict():

    ref_int, ref_pitch = np.array([[0, 1]]), np.array([100])
    est_int, est_pitch = np.array([[0.05, 1]]), np.array([100])

    matching = (
        mir_eval.transcription.match_notes(ref_int, ref_pitch, est_int,
                                           est_pitch, strict=True))

    assert matching == []

def test_precision_recall_f1_overlap():

    # load test data
    ref_int, ref_pitch = REF[:, :2], REF[:, 2]
    est_int, est_pitch = EST[:, :2], EST[:, 2]

    precision, recall, f_measure, avg_overlap_ratio = (
        mir_eval.transcription.precision_recall_f1_overlap(
            ref_int, ref_pitch, est_int, est_pitch))

    scores_gen = np.array([precision, recall, f_measure, avg_overlap_ratio])
    scores_exp = np.array([SCORES['Precision'], SCORES['Recall'],
                           SCORES['F-measure'],
                           SCORES['Average_Overlap_Ratio']])
    assert np.allclose(scores_exp, scores_gen, atol=A_TOL)

    precision, recall, f_measure, avg_overlap_ratio = (
        mir_eval.transcription.precision_recall_f1_overlap(
            ref_int, ref_pitch, est_int, est_pitch, offset_ratio=None))

    scores_gen = np.array([precision, recall, f_measure, avg_overlap_ratio])
    scores_exp = np.array([SCORES['Precision_no_offset'],
                           SCORES['Recall_no_offset'],
                           SCORES['F-measure_no_offset'],
                           SCORES['Average_Overlap_Ratio_no_offset']])
    assert np.allclose(scores_exp, scores_gen, atol=A_TOL)

def __check_score(score, expected_score):
    assert np.allclose(score, expected_score, atol=A_TOL)

def test_onset_precision_recall_f1():

    # load test data
    ref_int = REF[:, :2]
    est_int = EST[:, :2]

    precision, recall, f_measure = (
        mir_eval.transcription.onset_precision_recall_f1(ref_int, est_int))

    scores_gen = np.array([precision, recall, f_measure])
    scores_exp = np.array([ONSET_SCORES['Onset_Precision'],
                           ONSET_SCORES['Onset_Recall'],
                           ONSET_SCORES['Onset_F-measure']])
    assert np.allclose(scores_exp, scores_gen, atol=A_TOL)

def test_offset_precision_recall_f1():

    # load test data
    ref_int = REF[:, :2]
    est_int = EST[:, :2]

    precision, recall, f_measure = (
        mir_eval.transcription.offset_precision_recall_f1(ref_int, est_int))

    scores_gen = np.array([precision, recall, f_measure])
    scores_exp = np.array([OFFSET_SCORES['Offset_Precision'],
                           OFFSET_SCORES['Offset_Recall'],
                           OFFSET_SCORES['Offset_F-measure']])
    assert np.allclose(scores_exp, scores_gen, atol=A_TOL)

def test_regression():

    # Regression tests
    ref_files = sorted(glob.glob(REF_GLOB))
    est_files = sorted(glob.glob(EST_GLOB))
    sco_files = sorted(glob.glob(SCORES_GLOB))

    for ref_f, est_f, sco_f in zip(ref_files, est_files, sco_files):
        with open(sco_f, 'r') as f:
            expected_scores = json.load(f)
        # Load in reference transcription
        ref_int, ref_pitch = mir_eval.io.load_valued_intervals(ref_f)
        # Load in estimated transcription
        est_int, est_pitch = mir_eval.io.load_valued_intervals(est_f)
        scores = mir_eval.transcription.evaluate(ref_int, ref_pitch, est_int,
                                                 est_pitch)
        for metric in scores:
            # This is a simple hack to make nosetest's messages more useful
            yield (__check_score, scores[metric], expected_scores[metric])

def test_invalid_pitch():

    ref_int, ref_pitch = np.array([[0, 1]]), np.array([-100])
    est_int, est_pitch = np.array([[0, 1]]), np.array([100])

    yield (raises(ValueError)(mir_eval.transcription.validate),
           ref_int, ref_pitch, est_int, est_pitch)
    yield (raises(ValueError)(mir_eval.transcription.validate),
           est_int, est_pitch, ref_int, ref_pitch)

def test_inconsistent_int_pitch():

    ref_int, ref_pitch = np.array([[0, 1], [2, 3]]), np.array([100])
    est_int, est_pitch = np.array([[0, 1]]), np.array([100])

    yield (raises(ValueError)(mir_eval.transcription.validate),
           ref_int, ref_pitch, est_int, est_pitch)
    yield (raises(ValueError)(mir_eval.transcription.validate),
           est_int, est_pitch, ref_int, ref_pitch)

def test_empty_ref():

    warnings.resetwarnings()
    warnings.simplefilter('always')
    with warnings.catch_warnings(record=True) as out:

        ref_int, ref_pitch = np.empty(shape=(0, 2)), np.array([])
        est_int, est_pitch = np.array([[0, 1]]), np.array([100])

        mir_eval.transcription.validate(ref_int, ref_pitch, est_int, est_pitch)

        # Make sure that the warning triggered
        assert len(out) > 0

        # And that the category is correct
        assert out[0].category is UserWarning

        # And that it says the right thing (roughly)
        assert 'empty' in str(out[0].message).lower()

def test_empty_est():

    warnings.resetwarnings()
    warnings.simplefilter('always')
    with warnings.catch_warnings(record=True) as out:

        ref_int, ref_pitch = np.array([[0, 1]]), np.array([100])
        est_int, est_pitch = np.empty(shape=(0, 2)), np.array([])

        mir_eval.transcription.validate(ref_int, ref_pitch, est_int, est_pitch)

        # Make sure that the warning triggered
        assert len(out) > 0

        # And that the category is correct
        assert out[0].category is UserWarning

        # And that it says the right thing (roughly)
        assert 'empty' in str(out[0].message).lower()

def test_precision_recall_f1_overlap_empty():

    ref_int, ref_pitch = np.empty(shape=(0, 2)), np.array([])
    est_int, est_pitch = np.array([[0, 1]]), np.array([100])

    precision, recall, f1, avg_overlap_ratio = (
        mir_eval.transcription.precision_recall_f1_overlap(
            ref_int, ref_pitch, est_int, est_pitch))

    assert (precision, recall, f1) == (0, 0, 0)

    precision, recall, f1, avg_overlap_ratio = (
        mir_eval.transcription.precision_recall_f1_overlap(
            est_int, est_pitch, ref_int, ref_pitch))

    assert (precision, recall, f1) == (0, 0, 0)

def test_onset_precision_recall_f1_empty():

    ref_int = np.empty(shape=(0, 2))
    est_int = np.array([[0, 1]])

    precision, recall, f1 = (
        mir_eval.transcription.onset_precision_recall_f1(ref_int, est_int))

    assert (precision, recall, f1) == (0, 0, 0)

    precision, recall, f1 = (
        mir_eval.transcription.onset_precision_recall_f1(est_int, ref_int))

    assert (precision, recall, f1) == (0, 0, 0)

def test_offset_precision_recall_f1_empty():

    ref_int = np.empty(shape=(0, 2))
    est_int = np.array([[0, 1]])

    precision, recall, f1 = (
        mir_eval.transcription.offset_precision_recall_f1(ref_int, est_int))

    assert (precision, recall, f1) == (0, 0, 0)

    precision, recall, f1 = (
        mir_eval.transcription.offset_precision_recall_f1(est_int, ref_int))

    assert (precision, recall, f1) == (0, 0, 0)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

class AlarmManager(object):

    def __init__(self):
        pass

    def create(self, params):
        print("alarm create!!")
        print(params)

    def read(self, params):
        print("alarm read!!")
        print(params)

    def update(self, params):
        print("alarm update!!")
        print(params)

    def delete(self, params):
        print("alarm delete!!")
        print(params)
<commit_msg>Add: AlarmManager create -> write_file (json)<commit_after># -*- coding: utf-8 -*-

import os
import schedule
import threading
import time

from slacker import Slacker
from utils.data_handler import DataHandler

class AlarmManager(object):

    def __init__(self):
        SLACK_TOKEN = os.environ["STALKER_BOT_TOKEN"]
        self.slacker = Slacker(SLACK_TOKEN)
        self.data_handler = DataHandler()
        self.fname = "scheduler.json"

    def create(self, params):
        print("alarm create!!")
        params = params[0].split(" + ")
        input_text = params[0]
        input_period = params[1]

        schedule_data = self.data_handler.read_file(self.fname)

        if schedule_data == {}:
            s_index = 1
        else:
            s_index = schedule_data['count'] + 1

        input_schedule = { "text": input_text, "period": input_period}
        schedule_data[s_index] = input_schedule
        schedule_data["count"] = s_index

        self.data_handler.write_file(self.fname, schedule_data)
        self.slacker.chat.post_message(channel="#bot_test",
                                       text="알람이 등록되었습니다.",
                                       as_user=True)

    def read(self, params):
        print("alarm read!!")
        print(params)

    def update(self, params):
        print("alarm update!!")
        print(params)

    def delete(self, params):
        print("alarm delete!!")
        print(params)

    def run_schedule(self):
        self.__set_schedules()
        schedule.run_continuously()

    def __set_schedules(self):

        def send_message(text="input text"):
            self.slacker.chat.post_message(channel="#bot_test",
                                           text=text,
                                           as_user=True)

        schedule_data = self.data_handler.read_file(self.fname)
        for k,v in schedule_data.items():
            if type(v) == type({}):
                period = v["period"].split(" ")
                number = int(period[0])
                datetime_unit = period[1]
                param = {"text": v["text"]}

                getattr(schedule.every(number), datetime_unit).do(self.__run_threaded,
                                                                  send_message, param)

    def __run_threaded(self, job_func, param):
        job_thread = threading.Thread(target=job_func, kwargs=param)
        job_thread.start()

<|endoftext|>


<commit_before>#!/usr/bin/python
import platform
import sys

class TestSetup:
    _win = {}
    _linux = {}
    _osx = {}
    _options = []

    def __init__(self, win=None, linux=None, osx=None, options=None):
        self._win = {} if win is None else win
        self._linux = {} if linux is None else linux
        self._osx = {} if osx is None else osx
        self._options = options

    def get_package_options(self):
        return self._options

    def get_build_modes(self):
        platform = sys.platform
        if platform.startswith("win"):
            return self._win.keys()
        elif platform.startswith("linux"):
            return self._linux.keys()
        elif platform.startswith("darwin"):
            return self._osx.keys()
        else:
            raise RuntimeError("Unknown platform " + platform)

    def _get_list_of_test_configurations(self, builder):
        platform = sys.platform
        configurations = []
        if platform.startswith("win"):
            if self._win[builder]:
                configurations.extend(self._win[builder])
        elif platform.startswith("linux"):
            if self._linux[builder]:
                configurations.extend(self._linux[builder])
        elif platform.startswith("darwin"):
            if self._osx[builder]:
                configurations.extend(self._osx[builder])
        else:
            raise RuntimeError("Unknown platform " + platform)
        return configurations

    def get_variations(self, builder, excluded_variations, bitdepth):
        variations = set()
        for i in self._get_list_of_test_configurations(builder):
            if not excluded_variations or i.get_name() not in excluded_variations:
                if bitdepth == "*" or bitdepth == i.arch():
                    variations.add(i)
        return variations

def test_option_setup(opt_parser):
    def store_value_option(option, opt_str, value, parser):
        if not parser.values.Flavours:
            parser.values.Flavours = []
        parser.values.Flavours.append("%s=%s" % (opt_str, value))
    def store_bool_option(option, opt_str, value, parser):
        if not parser.values.Flavours:
            parser.values.Flavours = []
        parser.values.Flavours.append(opt_str)
    for opt, help_text in ConfigOptions.get_value_options():
        option_name = "--%s" % opt
        opt_parser.add_option(option_name,
                              dest="Flavours",
                              type="string",
                              action="callback",
                              callback=store_value_option,
                              default=None,
                              help=help_text)
    for opt, help_text in ConfigOptions.get_bool_options():
        option_name = "--%s" % opt
        opt_parser.add_option(option_name,
                              dest="Flavours",
                              action="callback",
                              callback=store_bool_option,
                              default=None,
                              help=help_text)

class ConfigOptions(object):
    _valueOptions = {}
    _boolOptions = {}

    def __init__(self, name, arch):
        self._name = name
        self._arch = arch
        self._platforms = []
        self._argList = []
        if ("32" == arch or "64" == arch):
            self._argList.append("--C.bitdepth=%s"%arch)

    def get_name(self):
        return self._name

    def get_arguments(self):
        return self._argList

    def platforms(self):
        return self._platforms

    def arch(self):
        return self._arch

    def __repr__(self):
        return '%s-%s' % (self._name, self._arch)

    @staticmethod
    def register_value_option(platform, option_tuple):
        if platform not in ConfigOptions._valueOptions:
            ConfigOptions._valueOptions[platform] = set()
        ConfigOptions._valueOptions[platform].add(option_tuple)

    @staticmethod
    def register_bool_option(platform, option_tuple):
        if platform not in ConfigOptions._boolOptions:
            ConfigOptions._boolOptions[platform] = set()
        ConfigOptions._boolOptions[platform].add(option_tuple)

    @staticmethod
    def get_value_options():
        return ConfigOptions._valueOptions.get(platform.system(), [])

    @staticmethod
    def get_bool_options():
        return ConfigOptions._boolOptions.get(platform.system(), [])

class VisualCCommon(ConfigOptions):
    def __init__(self, arch):
        super(VisualCCommon, self).__init__("VisualC", arch)
        self._argList.append("--C.toolchain=VisualC")
        self._platforms = []
        if arch == "32":
            self._platforms.append("Win32")
        elif arch == "64":
            self._platforms.append("x64")
        elif arch == "*":
            self._platforms.extend(["Win32","x64"])
        else:
            raise RuntimeError("Unrecognized arch: %s" % arch)
        ConfigOptions.register_value_option("Windows", ("VisualC.version", "Set the VisualC version"))
        ConfigOptions.register_value_option("Windows", ("WindowsSDK.version", "Set the WindowsSDK version"))
        ConfigOptions.register_value_option("Windows", ("MakeFile.format", "Choose the format of MakeFiles generated"))
        ConfigOptions.register_bool_option("Windows", ("VisualC.discoverprereleases", "Discover prerelease versions of VisualC"))

class VisualC64(VisualCCommon):
    def __init__(self):
        super(VisualC64, self).__init__("64")

class VisualC32(VisualCCommon):
    def __init__(self):
        super(VisualC32, self).__init__("32")

class Mingw32(ConfigOptions):
    def __init__(self):
        super(Mingw32, self).__init__("Mingw", "32")
        self._argList.append("--C.toolchain=Mingw")
        ConfigOptions.register_value_option("Windows", ("Mingw.version", "Set the Mingw version"))

class GccCommon(ConfigOptions):
    def __init__(self, arch):
        super(GccCommon, self).__init__("Gcc", arch)
        ConfigOptions.register_value_option("Linux", ("Gcc.version", "Set the Gcc version"))

class Gcc64(GccCommon):
    def __init__(self):
        super(Gcc64, self).__init__("64")

class Gcc32(GccCommon):
    def __init__(self):
        super(Gcc32, self).__init__("32")

class ClangCommon(ConfigOptions):
    def __init__(self, arch):
        super(ClangCommon, self).__init__("Clang", arch)
        ConfigOptions.register_value_option("Darwin", ("Clang.version", "Set the Clang version"))

class Clang32(ClangCommon):
    def __init__(self):
        super(Clang32, self).__init__("32")

class Clang64(ClangCommon):
    def __init__(self):
        super(Clang64, self).__init__("64")

visualc = VisualCCommon("*")
visualc64 = VisualC64()
visualc32 = VisualC32()
mingw32 = Mingw32()
gcc = GccCommon("*")
gcc32 = Gcc32()
gcc64 = Gcc64()
clang = ClangCommon("*")
clang32 = Clang32()
clang64 = Clang64()

""" Moved to bam-csharp
configs["CodeGenTest2"] = TestSetup(win={"Native":[visualc64,mingw32],"MakeFile":[visualc64,mingw32]},
                                    linux={"Native":[gcc64],"MakeFile":[gcc64]},
                                    osx={"Native":[clang64],"MakeFile":[clang64]})
"""
""" Moved to bam-csharp
configs["CSharpTest1"] = TestSetup(win={"Native":None,"MakeFile":None,"VSSolution":None},
                                   linux={"Native":None,"MakeFile":None},
                                   osx={"Native":None,"MakeFile":None})
"""
"""
configs["MixedModeCpp"] = TestSetup(win={"Native":[visualc64],"VSSolution":[visualc64],"MakeFile":[visualc64]})
"""
""" Moved to bam-csharp
configs["MixedTest"] = TestSetup(win={"Native":[visualc64],"MakeFile":[visualc64]})
"""
""" Moved to bam-hpc
configs["OpenCLTest1"] = TestSetup(win={"Native":[visualc64],"VSSolution":[visualc64],"MakeFile":[visualc64]})
"""
""" Moved to bam-graphicssdk
"""
""" Moved to bam-csharp
configs["WPFTest"] = TestSetup(win={"VSSolution":None})
"""
<commit_msg>[tests] Ensure that TestConfig options are always a list, even if just a string is passed<commit_after>#!/usr/bin/python
import platform
import sys

class TestSetup:
    _win = {}
    _linux = {}
    _osx = {}
    _options = []

    def __init__(self, win=None, linux=None, osx=None, options=None):
        self._win = {} if win is None else win
        self._linux = {} if linux is None else linux
        self._osx = {} if osx is None else osx
        if not isinstance(options, (list, tuple)):
            options = [ options ]
        self._options = options

    def get_package_options(self):
        return self._options

    def get_build_modes(self):
        platform = sys.platform
        if platform.startswith("win"):
            return self._win.keys()
        elif platform.startswith("linux"):
            return self._linux.keys()
        elif platform.startswith("darwin"):
            return self._osx.keys()
        else:
            raise RuntimeError("Unknown platform " + platform)

    def _get_list_of_test_configurations(self, builder):
        platform = sys.platform
        configurations = []
        if platform.startswith("win"):
            if self._win[builder]:
                configurations.extend(self._win[builder])
        elif platform.startswith("linux"):
            if self._linux[builder]:
                configurations.extend(self._linux[builder])
        elif platform.startswith("darwin"):
            if self._osx[builder]:
                configurations.extend(self._osx[builder])
        else:
            raise RuntimeError("Unknown platform " + platform)
        return configurations

    def get_variations(self, builder, excluded_variations, bitdepth):
        variations = set()
        for i in self._get_list_of_test_configurations(builder):
            if not excluded_variations or i.get_name() not in excluded_variations:
                if bitdepth == "*" or bitdepth == i.arch():
                    variations.add(i)
        return variations

def test_option_setup(opt_parser):
    def store_value_option(option, opt_str, value, parser):
        if not parser.values.Flavours:
            parser.values.Flavours = []
        parser.values.Flavours.append("%s=%s" % (opt_str, value))
    def store_bool_option(option, opt_str, value, parser):
        if not parser.values.Flavours:
            parser.values.Flavours = []
        parser.values.Flavours.append(opt_str)
    for opt, help_text in ConfigOptions.get_value_options():
        option_name = "--%s" % opt
        opt_parser.add_option(option_name,
                              dest="Flavours",
                              type="string",
                              action="callback",
                              callback=store_value_option,
                              default=None,
                              help=help_text)
    for opt, help_text in ConfigOptions.get_bool_options():
        option_name = "--%s" % opt
        opt_parser.add_option(option_name,
                              dest="Flavours",
                              action="callback",
                              callback=store_bool_option,
                              default=None,
                              help=help_text)

class ConfigOptions(object):
    _valueOptions = {}
    _boolOptions = {}

    def __init__(self, name, arch):
        self._name = name
        self._arch = arch
        self._platforms = []
        self._argList = []
        if ("32" == arch or "64" == arch):
            self._argList.append("--C.bitdepth=%s"%arch)

    def get_name(self):
        return self._name

    def get_arguments(self):
        return self._argList

    def platforms(self):
        return self._platforms

    def arch(self):
        return self._arch

    def __repr__(self):
        return '%s-%s' % (self._name, self._arch)

    @staticmethod
    def register_value_option(platform, option_tuple):
        if platform not in ConfigOptions._valueOptions:
            ConfigOptions._valueOptions[platform] = set()
        ConfigOptions._valueOptions[platform].add(option_tuple)

    @staticmethod
    def register_bool_option(platform, option_tuple):
        if platform not in ConfigOptions._boolOptions:
            ConfigOptions._boolOptions[platform] = set()
        ConfigOptions._boolOptions[platform].add(option_tuple)

    @staticmethod
    def get_value_options():
        return ConfigOptions._valueOptions.get(platform.system(), [])

    @staticmethod
    def get_bool_options():
        return ConfigOptions._boolOptions.get(platform.system(), [])

class VisualCCommon(ConfigOptions):
    def __init__(self, arch):
        super(VisualCCommon, self).__init__("VisualC", arch)
        self._argList.append("--C.toolchain=VisualC")
        self._platforms = []
        if arch == "32":
            self._platforms.append("Win32")
        elif arch == "64":
            self._platforms.append("x64")
        elif arch == "*":
            self._platforms.extend(["Win32","x64"])
        else:
            raise RuntimeError("Unrecognized arch: %s" % arch)
        ConfigOptions.register_value_option("Windows", ("VisualC.version", "Set the VisualC version"))
        ConfigOptions.register_value_option("Windows", ("WindowsSDK.version", "Set the WindowsSDK version"))
        ConfigOptions.register_value_option("Windows", ("MakeFile.format", "Choose the format of MakeFiles generated"))
        ConfigOptions.register_bool_option("Windows", ("VisualC.discoverprereleases", "Discover prerelease versions of VisualC"))

class VisualC64(VisualCCommon):
    def __init__(self):
        super(VisualC64, self).__init__("64")

class VisualC32(VisualCCommon):
    def __init__(self):
        super(VisualC32, self).__init__("32")

class Mingw32(ConfigOptions):
    def __init__(self):
        super(Mingw32, self).__init__("Mingw", "32")
        self._argList.append("--C.toolchain=Mingw")
        ConfigOptions.register_value_option("Windows", ("Mingw.version", "Set the Mingw version"))

class GccCommon(ConfigOptions):
    def __init__(self, arch):
        super(GccCommon, self).__init__("Gcc", arch)
        ConfigOptions.register_value_option("Linux", ("Gcc.version", "Set the Gcc version"))

class Gcc64(GccCommon):
    def __init__(self):
        super(Gcc64, self).__init__("64")

class Gcc32(GccCommon):
    def __init__(self):
        super(Gcc32, self).__init__("32")

class ClangCommon(ConfigOptions):
    def __init__(self, arch):
        super(ClangCommon, self).__init__("Clang", arch)
        ConfigOptions.register_value_option("Darwin", ("Clang.version", "Set the Clang version"))

class Clang32(ClangCommon):
    def __init__(self):
        super(Clang32, self).__init__("32")

class Clang64(ClangCommon):
    def __init__(self):
        super(Clang64, self).__init__("64")

visualc = VisualCCommon("*")
visualc64 = VisualC64()
visualc32 = VisualC32()
mingw32 = Mingw32()
gcc = GccCommon("*")
gcc32 = Gcc32()
gcc64 = Gcc64()
clang = ClangCommon("*")
clang32 = Clang32()
clang64 = Clang64()

""" Moved to bam-csharp
configs["CodeGenTest2"] = TestSetup(win={"Native":[visualc64,mingw32],"MakeFile":[visualc64,mingw32]},
                                    linux={"Native":[gcc64],"MakeFile":[gcc64]},
                                    osx={"Native":[clang64],"MakeFile":[clang64]})
"""
""" Moved to bam-csharp
configs["CSharpTest1"] = TestSetup(win={"Native":None,"MakeFile":None,"VSSolution":None},
                                   linux={"Native":None,"MakeFile":None},
                                   osx={"Native":None,"MakeFile":None})
"""
"""
configs["MixedModeCpp"] = TestSetup(win={"Native":[visualc64],"VSSolution":[visualc64],"MakeFile":[visualc64]})
"""
""" Moved to bam-csharp
configs["MixedTest"] = TestSetup(win={"Native":[visualc64],"MakeFile":[visualc64]})
"""
""" Moved to bam-hpc
configs["OpenCLTest1"] = TestSetup(win={"Native":[visualc64],"VSSolution":[visualc64],"MakeFile":[visualc64]})
"""
""" Moved to bam-graphicssdk
"""
""" Moved to bam-csharp
configs["WPFTest"] = TestSetup(win={"VSSolution":None})
"""
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

class AlarmManager(object):

    def __init__(self):
        pass

    def create(self):
        pass

    def read(self):
        pass

    def update(self):
        pass

    def delete(self):
        pass
<commit_msg>Add: AlarmManager - CRUD<commit_after># -*- coding: utf-8 -*-

class AlarmManager(object):

    def __init__(self):
        pass

    def create(self, params):
        print("alarm create!!")
        print(params)

    def read(self, params):
        print("alarm read!!")
        print(params)

    def update(self, params):
        print("alarm update!!")
        print(params)

    def delete(self, params):
        print("alarm delete!!")
        print(params)
<|endoftext|>


<commit_before># Copyright 2011 Element 34
#
# 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.
"""
===============
SaunterTestCase
===============
"""
import ConfigParser
import logging
import os

from saunter.SeleniumWrapper import SeleniumWrapper as wrapper

import saunter.ConfigWrapper
try:
    if saunter.ConfigWrapper.ConfigWrapper().config.getboolean("SauceLabs", "ondemand"):
        import json
except ConfigParser.NoSectionError as e:
    if "DOCGENERATION" not in os.environ:
        raise

from saunter.testcase.base import BaseTestCase

class SaunterTestCase(BaseTestCase):
    """
    Parent class of all script classes used for custom asserts (usually 'soft' asserts) and shared fixture setup
    and teardown
    """
    def setUp(self):
        """
        Default setup method for all scripts. Connects either to the RC server configured in conf/selenium.ini
        or to Sauce Labs OnDemand
        """
        self.verificationErrors = []
        self.cf = saunter.ConfigWrapper.ConfigWrapper().config
        self.cf.set("Saunter", "name", self._testMethodName)
        if self.cf.getboolean("SauceLabs", "ondemand"):
            host = self.cf.get("SauceLabs", "server_host")
            port = self.cf.get("SauceLabs", "server_port")
            j = {}
            j['username'] = self.cf.get("SauceLabs", "username")
            j['access-key'] = self.cf.get("SauceLabs", "key")
            j['os'] = self.cf.get("SauceLabs", "os")
            j['browser'] = self.cf.get("SauceLabs", "browser")
            if j['browser'][0] == "*":
                j['browser'] = j['browser'][1:]
            j['browser-version'] = self.cf.get("SauceLabs", "browser_version")
            browser = json.dumps(j)
        else:
            host = self.cf.get("Selenium", "server_host")
            port = self.cf.get("Selenium", "server_port")
            browser = self.cf.get("Selenium", "browser")

        self.selenium = wrapper().remote_control(host, port, browser, self.cf.get("Selenium", "base_url"))
        self.selenium.start()

        if self.cf.getboolean("SauceLabs", "ondemand"):
            wrapper().sauce_session = self.selenium.get_eval("selenium.sessionId")

        self.selenium.window_maximize()
        if self.cf.has_option("Selenium", "timeout"):
            self.selenium.set_timeout(self.cf.getint("Selenium", "timeout") * 1000)
        self.selenium.open('/');

    def tearDown(self):
        """
        Default teardown method for all scripts. If run through Sauce Labs OnDemand, the job name, status and tags
        are updated. Also the video and server log are downloaded if so configured.
        """
        if not self.cf.getboolean("SauceLabs", "ondemand"):
            self.selenium.take_named_screenshot("final")
        self.selenium.stop()
        self.assertEqual([], self.verificationErrors)<commit_msg>instead of getting / on the server at browser start are now getting the base url which has the server and everything in it -- much like how webdriver does it<commit_after># Copyright 2011 Element 34
#
# 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.
"""
===============
SaunterTestCase
===============
"""
import ConfigParser
import logging
import os

from saunter.SeleniumWrapper import SeleniumWrapper as wrapper

import saunter.ConfigWrapper
try:
    if saunter.ConfigWrapper.ConfigWrapper().config.getboolean("SauceLabs", "ondemand"):
        import json
except ConfigParser.NoSectionError as e:
    if "DOCGENERATION" not in os.environ:
        raise

from saunter.testcase.base import BaseTestCase

class SaunterTestCase(BaseTestCase):
    """
    Parent class of all script classes used for custom asserts (usually 'soft' asserts) and shared fixture setup
    and teardown
    """
    def setUp(self):
        """
        Default setup method for all scripts. Connects either to the RC server configured in conf/selenium.ini
        or to Sauce Labs OnDemand
        """
        self.verificationErrors = []
        self.cf = saunter.ConfigWrapper.ConfigWrapper().config
        self.cf.set("Saunter", "name", self._testMethodName)
        if self.cf.getboolean("SauceLabs", "ondemand"):
            host = self.cf.get("SauceLabs", "server_host")
            port = self.cf.get("SauceLabs", "server_port")
            j = {}
            j['username'] = self.cf.get("SauceLabs", "username")
            j['access-key'] = self.cf.get("SauceLabs", "key")
            j['os'] = self.cf.get("SauceLabs", "os")
            j['browser'] = self.cf.get("SauceLabs", "browser")
            if j['browser'][0] == "*":
                j['browser'] = j['browser'][1:]
            j['browser-version'] = self.cf.get("SauceLabs", "browser_version")
            browser = json.dumps(j)
        else:
            host = self.cf.get("Selenium", "server_host")
            port = self.cf.get("Selenium", "server_port")
            browser = self.cf.get("Selenium", "browser")

        self.selenium = wrapper().remote_control(host, port, browser, self.cf.get("Selenium", "base_url"))
        self.selenium.start()

        if self.cf.getboolean("SauceLabs", "ondemand"):
            wrapper().sauce_session = self.selenium.get_eval("selenium.sessionId")

        self.selenium.window_maximize()
        if self.cf.has_option("Selenium", "timeout"):
            self.selenium.set_timeout(self.cf.getint("Selenium", "timeout") * 1000)
        self.selenium.open(self.cf.get("Selenium", "base_url"));

    def tearDown(self):
        """
        Default teardown method for all scripts. If run through Sauce Labs OnDemand, the job name, status and tags
        are updated. Also the video and server log are downloaded if so configured.
        """
        if not self.cf.getboolean("SauceLabs", "ondemand"):
            self.selenium.take_named_screenshot("final")
        self.selenium.stop()
        self.assertEqual([], self.verificationErrors)<|endoftext|>