Advertisement
albrnick

formula.py

Dec 6th, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 KB | None | 0 0
  1. #!/bin/env python
  2.  
  3. import re
  4. import math
  5.  
  6. FILENAME = 'formulas.txt'
  7.  
  8. GROUP_REG = re.compile( '(.+)H1:(.+)H2:(.+)H3:(.+)H4:(.+)', re.MULTILINE | re.DOTALL )
  9.  
  10.  
  11. def getGroupString():
  12.     """
  13.    Get our next group to process! (Right now we only have one, but THE Future!)
  14.    """
  15.  
  16.     f = file( FILENAME )
  17.     s = f.read()
  18.     print s
  19.  
  20.     f.close()
  21.  
  22.     return( s )
  23.  
  24.  
  25. def convertFormula( formula ):
  26.     """
  27.    Converts formula functions to ptyhon equiv.
  28.    Also strips newlines
  29.    """
  30.    
  31.     formula = formula.replace( 'TanH', 'math.tanh')
  32.     formula = formula.replace( '\n', '')
  33.    
  34.     return( formula )
  35.  
  36.  
  37. def getResultForGroup( group_str, inputs ):
  38.     """
  39.    Progress the raw group string, and return the numeric result
  40.    """
  41.  
  42.     match = GROUP_REG.search( group_str )
  43.     if not match:
  44.         raise Exception( 'Error parsing group string:\n%s' % group_str )
  45.  
  46.     formula = match.group(1)
  47.     h1 = match.group(2)
  48.     h2 = match.group(3)
  49.     h3 = match.group(4)
  50.     h4 = match.group(5)
  51.  
  52.     if True:
  53.         print formula
  54.         print h1
  55.         print h2
  56.         print h3
  57.         print h4
  58.  
  59.     formula = convertFormula( formula )
  60.     h1 = convertFormula( h1 )
  61.     h2 = convertFormula( h2 )
  62.     h3 = convertFormula( h3 )
  63.     h4 = convertFormula( h4 )
  64.    
  65.     math.tanh
  66.     inputs['math'] = math
  67.     h1_result = eval( h1, inputs )
  68.     h2_result = eval( h2, inputs )
  69.     h3_result = eval( h3, inputs )
  70.     h4_result = eval( h4, inputs )
  71.    
  72.     if True:
  73.         print h1_result, h2_result, h3_result, h4_result
  74.    
  75.     formula_result = eval( formula, { 'math': math,
  76.                                       'H1': h1_result,
  77.                                       'H2': h2_result,
  78.                                       'H3': h3_result,
  79.                                       'H4': h4_result,
  80.                                       })
  81.     if True:
  82.         print formula_result
  83.  
  84.     return( formula_result )
  85.  
  86.  
  87. def main():
  88.    
  89.     group_str = getGroupString()
  90.  
  91.     getResultForGroup( group_str, {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5,
  92.                                    'F': 6, 'G': 7, 'H': 8, 'I': 9, 'J': 10,
  93.                                    'K': 11, 'L': 12, 'M': 13})
  94.    
  95.  
  96.  
  97. if __name__ == '__main__':
  98.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement