1 | # ---------------------------------------------------------------------- |
---|
2 | # COMPONENT: number |
---|
3 | # |
---|
4 | # ====================================================================== |
---|
5 | # AUTHOR: Michael McLennan, Purdue University |
---|
6 | # Copyright (c) 2005 Purdue Research Foundation, West Lafayette, IN |
---|
7 | # ====================================================================== |
---|
8 | import Rappture |
---|
9 | import Rappture.Units |
---|
10 | |
---|
11 | class number: |
---|
12 | def __init__(self,path,id,**kwargs): |
---|
13 | """ |
---|
14 | Blah, blah... |
---|
15 | """ |
---|
16 | |
---|
17 | self.basePath = path |
---|
18 | self.path = path + ".number(" + id + ")" |
---|
19 | self.id = id |
---|
20 | self.attribs = kwargs |
---|
21 | |
---|
22 | def put(self,lib): |
---|
23 | lib.put(self.path) |
---|
24 | for key,val in self.attribs.items(): |
---|
25 | lib.put(self.path+'.'+key, str(val)) |
---|
26 | |
---|
27 | def __coerce__(self,other): |
---|
28 | attrList = Rappture.driver.children(self.path, "id") |
---|
29 | attrName = 'default' |
---|
30 | for attr in attrList: |
---|
31 | if attr == 'current': |
---|
32 | attrName = attr |
---|
33 | s = Rappture.driver.get(self.path+'.'+attrName) |
---|
34 | # return (float(s),other) |
---|
35 | # |
---|
36 | # the following code doesnt work if the <units> tag does not exist |
---|
37 | # becasue it sends an empty string to convert and it fails there |
---|
38 | toUnit = Rappture.driver.get(self.path+'.units') |
---|
39 | if (toUnit != None): |
---|
40 | val = Rappture.Units.convert(s,to=toUnit,units="off") |
---|
41 | else : |
---|
42 | # you have to hope the user didn't put units onto the <default> |
---|
43 | # value (which is unlikely in most cases, btw) |
---|
44 | val = float(s) |
---|
45 | return (val,other) |
---|