improved unicode handling.

background message is now stoppable.
background message not “jumping” (multiplying) anymore.
This commit is contained in:
Philip Eisenlohr 2015-11-20 16:15:34 +00:00
parent 40c2a5dd6d
commit 38c536b6b7
1 changed files with 41 additions and 22 deletions

View File

@ -5,14 +5,21 @@ import sys,time,random,threading,os,subprocess,shlex
import numpy as np
from optparse import Option
# -----------------------------
def srepr(arg,
glue = '\n'):
# -----------------------------
if (not hasattr(arg, "strip") and
hasattr(arg, "__getitem__") or
hasattr(arg, "__iter__")):
return glue.join(srepr(x) for x in arg)
return arg if isinstance(arg,basestring) else repr(arg)
# -----------------------------
def croak(what,
newline = True):
# -----------------------------
sys.stderr.write(('\n'.join(map(str,what)) if not hasattr(what, "strip")
and hasattr(what, "__getitem__")
or hasattr(what, "__iter__") else str(what))
+('\n' if newline else ''))
sys.stderr.write(srepr(what,glue = '\n') + ('\n' if newline else ''))
sys.stderr.flush()
# -----------------------------
@ -23,7 +30,7 @@ def report(who,what):
# -----------------------------
def emph(what):
# -----------------------------
return '\033[1m'+str(what)+'\033[0m'
return '\033[1m'+srepr(what)+'\033[0m'
# -----------------------------
# Matlab like trigonometric functions that take and return angles in degrees.
@ -67,29 +74,29 @@ class extendableOption(Option):
else:
Option.take_action(self, action, dest, opt, value, values, parser)
# -----------------------------
class backgroundMessage(threading.Thread):
# -----------------------------
choices = {'bounce':['_','o','O','°','¯','¯','°','O','o',],
'circle': [u'\u25f4',u'\u25f5',u'\u25f6',u'\u25f7'],
'hexagon': [u'\u2b22',u'\u2b23'],
'pentagon': [u'\u2b20',u'\u2b54'],
'square': [u'\u2596',u'\u2598',u'\u259d',u'\u2597'],
choices = {'bounce': ['_','o','O','°','¯','¯','°','O','o',],
'circle': [u'\u25f4',u'\u25f5',u'\u25f6',u'\u25f7'],
'hexagon': [u'\u2b22',u'\u2b23'],
'square': [u'\u2596',u'\u2598',u'\u259d',u'\u2597'],
'triangle': [u'\u140a',u'\u140a',u'\u1403',u'\u1405',u'\u1405',u'\u1403'],
'amoeba': [u'\u2596',u'\u258f',u'\u2598',u'\u2594',u'\u259d',u'\u2595',u'\u2597',u'\u2582'],
'beat': [u'\u2581',u'\u2582',u'\u2583',u'\u2584',u'\u2585',u'\u2586',u'\u2587',u'\u2588',u'\u2587',u'\u2586',u'\u2585',u'\u2584',u'\u2583',u'\u2582',],
'prison': [u'\u168b',u'\u168c',u'\u168d',u'\u168f',u'\u168e',u'\u168d',u'\u168c',u'\u168b',],
'breath': [u'\u1690',u'\u1691',u'\u1692',u'\u1693',u'\u1694',u'\u1693',u'\u1692',u'\u1691',u'\u1690'],
'pulse': ['·','',u'\u25cf',u'\u25cf','',],
'ant': [u'\u2801',u'\u2802',u'\u2810',u'\u2820',u'\u2804',u'\u2840',u'\u2880',u'\u2820',u'\u2804',u'\u2802',u'\u2810',u'\u2808'],
'classic':['-', '\\', '|', '/',],
'amoeba': [u'\u2596',u'\u258f',u'\u2598',u'\u2594',u'\u259d',u'\u2595',u'\u2597',u'\u2582'],
'beat': [u'\u2581',u'\u2582',u'\u2583',u'\u2585',u'\u2586',u'\u2587',u'\u2587',u'\u2586',u'\u2585',u'\u2583',u'\u2582',],
'prison': [u'\u168b',u'\u168c',u'\u168d',u'\u168f',u'\u168e',u'\u168d',u'\u168c',u'\u168b',],
'breath': [u'\u1690',u'\u1691',u'\u1692',u'\u1693',u'\u1694',u'\u1693',u'\u1692',u'\u1691',u'\u1690',],
'pulse': [u'·',u'',u'\u25cf',u'\u25cf',u'',],
'ant': [u'\u2801',u'\u2802',u'\u2810',u'\u2820',u'\u2804',u'\u2840',u'\u2880',u'\u2820',u'\u2804',u'\u2802',u'\u2810',u'\u2808'],
'juggle': [u'\ua708',u'\ua709',u'\ua70a',u'\ua70b',u'\ua70c',u'\ua711',u'\ua710',u'\ua70f',u'\ua70d',],
'classic': ['-', '\\', '|', '/',],
}
def __init__(self,
symbol = None,
wait = 0.1):
threading.Thread.__init__(self)
super(backgroundMessage, self).__init__()
self._stop = threading.Event()
self.message = ''
self.new_message = ''
self.counter = 0
@ -101,9 +108,17 @@ class backgroundMessage(threading.Thread):
length = len(self.symbols[self.counter] + self.gap + self.message)
sys.stderr.write(chr(8)*length + ' '*length + chr(8)*length)
sys.stderr.write('')
sys.stderr.flush()
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.is_set()
def run(self):
while not threading.enumerate()[0]._Thread__stopped:
# while not threading.enumerate()[0]._Thread__stopped:
while not self.stopped():
time.sleep(self.waittime)
self.update_message()
self.__quit__()
@ -114,14 +129,18 @@ class backgroundMessage(threading.Thread):
def print_message(self):
length = len(self.symbols[self.counter] + self.gap + self.message)
sys.stderr.write(chr(8)*length + ' '*length + chr(8)*length) # delete former message
sys.stderr.write(self.symbols[self.counter] + self.gap + self.new_message) # print new message
sys.stderr.write(chr(8)*length + ' '*length + chr(8)*length + \
self.symbols[self.counter] + self.gap + self.new_message) # delete former and print new message
sys.stderr.flush()
self.message = self.new_message
def update_message(self):
self.counter = (self.counter + 1)%len(self.symbols)
self.print_message()
def animation(self,which = None):
return ''.join(self.choices[which]) if which in self.choices else ''
'''
Non-linear least square fitting (Levenberg-Marquardt method) with
the bounded parameters.