Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/env python
- import re
- import math
- FILENAME = 'formulas.txt'
- GROUP_REG = re.compile( '(.+)H1:(.+)H2:(.+)H3:(.+)H4:(.+)', re.MULTILINE | re.DOTALL )
- def getGroupString():
- """
- Get our next group to process! (Right now we only have one, but THE Future!)
- """
- f = file( FILENAME )
- s = f.read()
- print s
- f.close()
- return( s )
- def convertFormula( formula ):
- """
- Converts formula functions to ptyhon equiv.
- Also strips newlines
- """
- formula = formula.replace( 'TanH', 'math.tanh')
- formula = formula.replace( '\n', '')
- return( formula )
- def getResultForGroup( group_str, inputs ):
- """
- Progress the raw group string, and return the numeric result
- """
- match = GROUP_REG.search( group_str )
- if not match:
- raise Exception( 'Error parsing group string:\n%s' % group_str )
- formula = match.group(1)
- h1 = match.group(2)
- h2 = match.group(3)
- h3 = match.group(4)
- h4 = match.group(5)
- if True:
- print formula
- print h1
- print h2
- print h3
- print h4
- formula = convertFormula( formula )
- h1 = convertFormula( h1 )
- h2 = convertFormula( h2 )
- h3 = convertFormula( h3 )
- h4 = convertFormula( h4 )
- math.tanh
- inputs['math'] = math
- h1_result = eval( h1, inputs )
- h2_result = eval( h2, inputs )
- h3_result = eval( h3, inputs )
- h4_result = eval( h4, inputs )
- if True:
- print h1_result, h2_result, h3_result, h4_result
- formula_result = eval( formula, { 'math': math,
- 'H1': h1_result,
- 'H2': h2_result,
- 'H3': h3_result,
- 'H4': h4_result,
- })
- if True:
- print formula_result
- return( formula_result )
- def main():
- group_str = getGroupString()
- getResultForGroup( group_str, {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5,
- 'F': 6, 'G': 7, 'H': 8, 'I': 9, 'J': 10,
- 'K': 11, 'L': 12, 'M': 13})
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement