added background threading (might be used in more than one script)

This commit is contained in:
Martin Diehl 2014-10-15 08:32:53 +00:00
parent 1f795d674c
commit b7f078971b
1 changed files with 58 additions and 20 deletions

View File

@ -1,22 +1,33 @@
# -*- coding: UTF-8 no BOM -*- # -*- coding: UTF-8 no BOM -*-
# damask utility functions # damask utility functions
import math import threading
import numpy as np
from optparse import OptionParser, Option from optparse import OptionParser, Option
try: # -----------------------------
import numpy
except:
numpy=None
# Matlab like trigonometric functions that take and return angles in degrees. # Matlab like trigonometric functions that take and return angles in degrees.
# -----------------------------
for f in ['cos', 'sin', 'tan']: for f in ['cos', 'sin', 'tan']:
if numpy: exec('def %sd(deg): return (np.%s(np.deg2rad(deg)))'%(f,f))
exec('def %sd(deg): return (numpy.%s(numpy.deg2rad(deg)))'%(f,f)) exec('def a%sd(val): return (np.rad2deg(np.arc%s(val)))'%(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)) 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): class extendableOption(Option):
@ -37,13 +48,40 @@ class extendableOption(Option):
Option.take_action(self, action, dest, opt, value, values, parser) Option.take_action(self, action, dest, opt, value, values, parser)
def gridLocation(idx,res): # -----------------------------
return ( idx % res[0], \ class backgroundMessage(threading.Thread):
( idx // res[0]) % res[1], \ # -----------------------------
( idx // res[0] // res[1]) % res[2] )
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): def set_message(self, new_message):
return ( location[0] % res[0] + \ self.new_message = new_message
( location[1] % res[1]) * res[0] + \ self.print_message()
( location[2] % res[2]) * res[1] * res[0] )
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()