git.fiddlerwoaroof.com
Browse code

Initial Commit

The program basically works, but could probably use some tweaking here
and there.

fiddlerwoaroof authored on 09/02/2015 01:28:55
Showing 5 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,29 @@
1
+Copyright (c) 2011 Edward Langley
2
+All rights reserved.
3
+
4
+Redistribution and use in source and binary forms, with or without
5
+modification, are permitted provided that the following conditions
6
+are met:
7
+
8
+Redistributions of source code must retain the above copyright notice,
9
+this list of conditions and the following disclaimer.
10
+
11
+Redistributions in binary form must reproduce the above copyright
12
+notice, this list of conditions and the following disclaimer in the
13
+documentation and/or other materials provided with the distribution.
14
+
15
+Neither the name of the project's author nor the names of its
16
+contributors may be used to endorse or promote products derived from
17
+this software without specific prior written permission.
18
+
19
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22
+FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
25
+TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
+PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
+LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
+NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0 30
new file mode 100644
... ...
@@ -0,0 +1,3 @@
1
+A fairly straightforward calculator program.
2
+It reads a template file and then either injects values from the command line or prompts for appropriate
3
+values
0 4
new file mode 100644
... ...
@@ -0,0 +1,5 @@
1
+- Check if stdin is a terminal: if not, don't prompt for values
2
+- Perhaps allow a YAML metadata block at the beginning of the file?
3
+  - It could, for example, specify the permitted variable inputs
4
+- Perhaps implement a custom expression evaluator, rather than just using eval()
5
+
0 6
new file mode 100644
... ...
@@ -0,0 +1,82 @@
1
+from __future__ import division
2
+import argparse
3
+import collections
4
+import os
5
+
6
+def parse_line(line):
7
+    line = line.split(':')
8
+    if len(line) > 2:
9
+        expr = line.pop().strip()
10
+        var = line.pop().strip()
11
+        desc = ':'.join(line).strip()
12
+    else:
13
+        expr = ''
14
+        var = line.pop().strip()
15
+        desc = ':'.join(line).strip()
16
+    return expr,var,desc
17
+
18
+def eval_line(expr,variables):
19
+    return eval(expr, {}, variables)
20
+
21
+def run_template(t, values):
22
+    with open(t) as f:
23
+        variables = {}
24
+        for line in f:
25
+            line = line.strip()
26
+            if line == '':
27
+                print;continue
28
+            expr, var, desc = parse_line(line)
29
+            if expr == '':
30
+                if values != []:
31
+                    expr = values.pop(0)
32
+                else:
33
+                    expr = raw_input('%s? ' % desc)
34
+                res = variables[var] = eval_line(expr, variables)
35
+            else:
36
+                res = variables[var] = eval_line(expr, variables)
37
+            print '%s: %s = %s' % (desc, var, res)
38
+
39
+def get_template_info(t, verbose=False):
40
+    with open(t) as f:
41
+        tmpl = f.read()
42
+        variables = ' '.join(var for expr,var,desc in map(parse_line, tmpl.split('\n')) if expr == '' and var != '')
43
+        print 'Template Usage: %s %s' % (os.path.basename(t).rpartition('.')[0], variables)
44
+        if verbose:
45
+            print
46
+            print 'Template Source:'
47
+            print tmpl
48
+
49
+
50
+
51
+def main():
52
+    template_dir = os.environ.get('SYMBCALC_TEMPLATES', 'templates')
53
+    argparser = argparse.ArgumentParser()
54
+    argparser.add_argument('--template-dir', dest='td', default=template_dir)
55
+    argparser.add_argument('template', nargs='?')
56
+    argparser.add_argument('values', nargs='*')
57
+    argparser.add_argument('-i', action='store_true')
58
+    argparser.add_argument('-v', action='store_true')
59
+    args = argparser.parse_args()
60
+    template = None
61
+    if args.template:
62
+        template = '%s.tmpl' % os.path.join(args.td, args.template)
63
+
64
+    if args.i:
65
+        if template:
66
+            get_template_info(template, args.v)
67
+        else:
68
+            print 'You must supply a template to get info about'
69
+    elif args.template:
70
+        try:
71
+            run_template(template, args.values)
72
+        except (KeyboardInterrupt, EOFError): pass
73
+    else:
74
+        print 'Available Templates:'
75
+        print '---'
76
+        for x in os.listdir(args.td):
77
+            if not x.endswith('.tmpl'): continue
78
+            print x.rpartition('.')[0]
79
+
80
+
81
+if __name__ == '__main__':
82
+    main()
0 83
new file mode 100644
... ...
@@ -0,0 +1,6 @@
1
+Amount of Alcholic Beverage #1 (mL): a 
2
+Concentration (%): b
3
+Alcohol in Beverage #1 (mL): t: a*b/100
4
+
5
+Concentration of Alcoholic Beverage #2 (%): c
6
+Equivalent Volume of Beverage #2 (mL): t/c*100