Package theia :: Package helpers :: Module tools
[hide private]
[frames] | no frames]

Source Code for Module theia.helpers.tools

  1  '''Defines some generic functions for theia.''' 
  2   
  3  # Provides: 
  4  #   class TotalReflectionError 
  5  #       __init__ 
  6  #       __str__ 
  7  #   class InputError 
  8  #       __init__ 
  9  #       __str___ 
 10  #   timer 
 11  #   formatter 
 12  #   shortRef 
 13   
 14  import time as tm 
 15  import re 
 16   
17 -class TotalReflectionError(Exception):
18 '''TotalReflectionError class. 19 20 Is raised when an interaction results in total reflection. 21 22 *=== Attributes ===* 23 Message: exception message. [string] 24 25 26 ''' 27
28 - def __init__(self, message):
29 '''TotalReflectionError exception initializer. 30 31 ''' 32 self.Message = message
33
34 - def __str__(self):
35 '''Printing error function. 36 37 ''' 38 return repr(self.Message)
39
40 -class InputError(Exception):
41 '''InputError class. 42 43 Is raised when the input .tia file parsing to input data failed. 44 45 *=== Attributes ===* 46 Message: exception message. [string] 47 48 ''' 49
50 - def __init__(self, message):
51 '''InputError exception initializer. 52 53 ''' 54 self.Message = message
55
56 - def __str__(self):
57 '''Printing error function 58 59 ''' 60 return repr(self.Message)
61
62 -def timer(func):
63 '''Decorator function to log execution time of other functions.''' 64 65 def wrapped(*args, **kw): 66 t1 = tm.time() 67 func(*args, **kw) 68 t2 = tm.time() 69 dt = t2 -t1 70 st = "theia: Debug: %s exec with '%s' in %s ms." \ 71 % (str(func.__name__), str(*args), str(dt*1000.)) 72 print st
73 74 return wrapped 75
76 -def formatter(stringList):
77 '''Returns a formatted version of the text formed by the list of lines.''' 78 count = 0 # counts '{' and '}' 79 ans = "" 80 for line in stringList: 81 if '}' in line: 82 count = count - 1 83 ans = ans + count * '\t' + line + '\n' 84 if '{' in line: 85 count = count + 1 86 87 return ans
88
89 -def shortRef(inStr):
90 '''Returns the short reference corresponding to string. 91 92 Change all 't2nrt' in 'R', all 't(2n+1)rt' in 'T' and 'r' in 'R' 93 in string. Used to write short references to beams. 94 95 ''' 96 f = inStr.find('-') 97 prefix = inStr[:f+1] 98 s1 = re.sub("tr(rr)*t", "R", inStr[f+1:]) 99 s2 = re.sub("t(rr)+t", "T", s1) 100 s3 = re.sub("tt", "T", s2) 101 s4 = re.sub("t", "T", s3) 102 return prefix + re.sub("r", "R", s4)
103