From b7f078971bead09aeac7066faf77abb2be9e927b Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Wed, 15 Oct 2014 08:32:53 +0000 Subject: [PATCH] added background threading (might be used in more than one script) --- lib/damask/util.py | 78 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 58 insertions(+), 20 deletions(-) diff --git a/lib/damask/util.py b/lib/damask/util.py index 9709b8eea..c045673ea 100644 --- a/lib/damask/util.py +++ b/lib/damask/util.py @@ -1,22 +1,33 @@ # -*- coding: UTF-8 no BOM -*- # damask utility functions -import math +import threading +import numpy as np from optparse import OptionParser, Option -try: - import numpy -except: - numpy=None - +# ----------------------------- # Matlab like trigonometric functions that take and return angles in degrees. +# ----------------------------- for f in ['cos', 'sin', 'tan']: - if numpy: - exec('def %sd(deg): return (numpy.%s(numpy.deg2rad(deg)))'%(f,f)) - exec('def a%sd(val): return (numpy.rad2deg(numpy.arc%s(val)))'%(f,f)) - else: - exec('def %sd(deg): return (math.%s(deg/180.*math.pi))'%(f,f)) - exec('def a%sd(val): return (math.a%s(val)*180./math.pi)'%(f,f)) + exec('def %sd(deg): return (np.%s(np.deg2rad(deg)))'%(f,f)) + exec('def a%sd(val): return (np.rad2deg(np.arc%s(val)))'%(f,f)) + + +# ----------------------------- +def gridLocation(idx,res): +# ----------------------------- + return ( idx % res[0], \ + ( idx // res[0]) % res[1], \ + ( idx // res[0] // res[1]) % res[2] ) + + +# ----------------------------- +def gridIndex(location,res): +# ----------------------------- + return ( location[0] % res[0] + \ + ( location[1] % res[1]) * res[0] + \ + ( location[2] % res[2]) * res[1] * res[0] ) + # ----------------------------- class extendableOption(Option): @@ -37,13 +48,40 @@ class extendableOption(Option): Option.take_action(self, action, dest, opt, value, values, parser) -def gridLocation(idx,res): - return ( idx % res[0], \ - ( idx // res[0]) % res[1], \ - ( idx // res[0] // res[1]) % res[2] ) +# ----------------------------- +class backgroundMessage(threading.Thread): +# ----------------------------- + + def __init__(self): + threading.Thread.__init__(self) + self.message = '' + self.new_message = '' + self.counter = 0 + self.symbols = ['- ', '\ ', '| ', '/ '] + self.waittime = 0.5 + + def __quit__(self): + length = len(self.message) + len(self.symbols[self.counter]) + sys.stderr.write(chr(8)*length + ' '*length + chr(8)*length) + sys.stderr.write('') + + def run(self): + while not threading.enumerate()[0]._Thread__stopped: + time.sleep(self.waittime) + self.update_message() + self.__quit__() -def gridIndex(location,res): - return ( location[0] % res[0] + \ - ( location[1] % res[1]) * res[0] + \ - ( location[2] % res[2]) * res[1] * res[0] ) + def set_message(self, new_message): + self.new_message = new_message + self.print_message() + + def print_message(self): + length = len(self.message) + len(self.symbols[self.counter]) + sys.stderr.write(chr(8)*length + ' '*length + chr(8)*length) # delete former message + sys.stderr.write(self.symbols[self.counter] + self.new_message) # print new message + self.message = self.new_message + + def update_message(self): + self.counter = (self.counter + 1)%len(self.symbols) + self.print_message()