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   
13  import time as tm 
14   
15 -class TotalReflectionError(Exception):
16 '''TotalReflectionError class. 17 18 Is raised when an interaction results in total reflection. 19 20 *=== Attributes ===* 21 Message: exception message. [string] 22 23 24 ''' 25
26 - def __init__(self, message):
27 '''TotalReflectionError exception initializer. 28 29 ''' 30 self.Message = message
31
32 - def __str__(self):
33 '''Printing error function. 34 35 ''' 36 return repr(self.Message)
37
38 -class InputError(Exception):
39 '''InputError class. 40 41 Is raised when the input .tia file parsing to input data failed. 42 43 *=== Attributes ===* 44 Message: exception message. [string] 45 46 ''' 47
48 - def __init__(self, message):
49 '''InputError exception initializer. 50 51 ''' 52 self.Message = message
53
54 - def __str__(self):
55 '''Printing error function 56 57 ''' 58 return repr(self.Message)
59
60 -def timer(func):
61 '''Decorator function to log execution time of other functions.''' 62 63 def wrapped(*args, **kw): 64 t1 = tm.time() 65 func(*args, **kw) 66 t2 = tm.time() 67 dt = t2 -t1 68 st = "theia: Debug: " + str(func.__name__) + " exec with '" \ 69 + str(*args) + "' in " + str(dt*1000.) + "ms." 70 print st
71 72 return wrapped 73
74 -def formatter(stringList):
75 '''Returns a formatted version of the text formed by the list of lines. 76 ''' 77 count = 0 # counts '{' and '}' 78 ans = "" 79 for line in stringList: 80 if '}' in line: 81 count = count - 1 82 ans = ans + count * '\t' + line + '\n' 83 if '{' in line: 84 count = count + 1 85 86 return ans
87